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 #include "sqliteInt.h"
27 ** This is a copy of the first part of the SqliteDb structure in
28 ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
29 ** can extract the sqlite3* pointer from an existing Tcl SQLite
37 ** Convert text generated by the "%p" conversion format back into
40 static int testHexToInt(int h
){
41 if( h
>='0' && h
<='9' ){
43 }else if( h
>='a' && h
<='f' ){
46 assert( h
>='A' && h
<='F' );
50 void *sqlite3TestTextToPtr(const char *z
){
54 if( z
[0]=='0' && z
[1]=='x' ){
59 v
= (v
<<4) + testHexToInt(*z
);
62 if( sizeof(p
)==sizeof(v
) ){
63 memcpy(&p
, &v
, sizeof(p
));
65 assert( sizeof(p
)==sizeof(v2
) );
67 memcpy(&p
, &v2
, sizeof(p
));
74 ** A TCL command that returns the address of the sqlite* pointer
75 ** for an sqlite connection instance. Bad things happen if the
76 ** input is not an sqlite connection.
78 static int get_sqlite_pointer(
88 Tcl_WrongNumArgs(interp
, 1, objv
, "SQLITE-CONNECTION");
91 if( !Tcl_GetCommandInfo(interp
, Tcl_GetString(objv
[1]), &cmdInfo
) ){
92 Tcl_AppendResult(interp
, "command not found: ",
93 Tcl_GetString(objv
[1]), (char*)0);
96 p
= (struct SqliteDb
*)cmdInfo
.objClientData
;
97 sprintf(zBuf
, "%p", p
->db
);
98 if( strncmp(zBuf
,"0x",2) ){
99 sprintf(zBuf
, "0x%p", p
->db
);
101 Tcl_AppendResult(interp
, zBuf
, 0);
106 ** Decode a pointer to an sqlite3 object.
108 int getDbPointer(Tcl_Interp
*interp
, const char *zA
, sqlite3
**ppDb
){
111 if( Tcl_GetCommandInfo(interp
, zA
, &cmdInfo
) ){
112 p
= (struct SqliteDb
*)cmdInfo
.objClientData
;
115 *ppDb
= (sqlite3
*)sqlite3TestTextToPtr(zA
);
122 ** Decode a Win32 HANDLE object.
124 int getWin32Handle(Tcl_Interp
*interp
, const char *zA
, LPHANDLE phFile
){
125 *phFile
= (HANDLE
)sqlite3TestTextToPtr(zA
);
130 extern const char *sqlite3ErrName(int);
131 #define t1ErrorName sqlite3ErrName
134 ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
135 ** fact that the sqlite3* is the first field in the Vdbe structure.
137 #define StmtToDb(X) sqlite3_db_handle(X)
140 ** Check a return value to make sure it agrees with the results
141 ** from sqlite3_errcode.
143 int sqlite3TestErrCode(Tcl_Interp
*interp
, sqlite3
*db
, int rc
){
144 if( sqlite3_threadsafe()==0 && rc
!=SQLITE_MISUSE
&& rc
!=SQLITE_OK
145 && sqlite3_errcode(db
)!=rc
){
147 int r2
= sqlite3_errcode(db
);
148 sprintf(zBuf
, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
149 t1ErrorName(rc
), rc
, t1ErrorName(r2
), r2
);
150 Tcl_ResetResult(interp
);
151 Tcl_AppendResult(interp
, zBuf
, 0);
158 ** Decode a pointer to an sqlite3_stmt object.
160 static int getStmtPointer(
163 sqlite3_stmt
**ppStmt
165 *ppStmt
= (sqlite3_stmt
*)sqlite3TestTextToPtr(zArg
);
170 ** Generate a text representation of a pointer that can be understood
171 ** by the getDbPointer and getVmPointer routines above.
173 ** The problem is, on some machines (Solaris) if you do a printf with
174 ** "%p" you cannot turn around and do a scanf with the same "%p" and
175 ** get your pointer back. You have to prepend a "0x" before it will
176 ** work. Or at least that is what is reported to me (drh). But this
177 ** behavior varies from machine to machine. The solution used her is
178 ** to test the string right after it is generated to see if it can be
179 ** understood by scanf, and if not, try prepending an "0x" to see if
180 ** that helps. If nothing works, a fatal error is generated.
182 int sqlite3TestMakePointerStr(Tcl_Interp
*interp
, char *zPtr
, void *p
){
183 sqlite3_snprintf(100, zPtr
, "%p", p
);
188 ** The callback routine for sqlite3_exec_printf().
190 static int exec_printf_cb(void *pArg
, int argc
, char **argv
, char **name
){
191 Tcl_DString
*str
= (Tcl_DString
*)pArg
;
194 if( Tcl_DStringLength(str
)==0 ){
195 for(i
=0; i
<argc
; i
++){
196 Tcl_DStringAppendElement(str
, name
[i
] ? name
[i
] : "NULL");
199 for(i
=0; i
<argc
; i
++){
200 Tcl_DStringAppendElement(str
, argv
[i
] ? argv
[i
] : "NULL");
206 ** The I/O tracing callback.
208 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
209 static FILE *iotrace_file
= 0;
210 static void io_trace_callback(const char *zFormat
, ...){
212 va_start(ap
, zFormat
);
213 vfprintf(iotrace_file
, zFormat
, ap
);
215 fflush(iotrace_file
);
220 ** Usage: io_trace FILENAME
222 ** Turn I/O tracing on or off. If FILENAME is not an empty string,
223 ** I/O tracing begins going into FILENAME. If FILENAME is an empty
224 ** string, I/O tracing is turned off.
226 static int test_io_trace(
228 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
229 int argc
, /* Number of arguments */
230 char **argv
/* Text of each argument */
232 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
234 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
239 if( iotrace_file
!=stdout
&& iotrace_file
!=stderr
){
240 fclose(iotrace_file
);
246 if( strcmp(argv
[1],"stdout")==0 ){
247 iotrace_file
= stdout
;
248 }else if( strcmp(argv
[1],"stderr")==0 ){
249 iotrace_file
= stderr
;
251 iotrace_file
= fopen(argv
[1], "w");
253 sqlite3IoTrace
= io_trace_callback
;
260 ** Usage: clang_sanitize_address
262 ** Returns true if the program was compiled using clang with the
263 ** -fsanitize=address switch on the command line. False otherwise.
265 static int clang_sanitize_address(
267 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
268 int argc
, /* Number of arguments */
269 char **argv
/* Text of each argument */
272 #if defined(__has_feature)
273 # if __has_feature(address_sanitizer)
277 Tcl_SetObjResult(interp
, Tcl_NewIntObj(res
));
282 ** Usage: sqlite3_exec_printf DB FORMAT STRING
284 ** Invoke the sqlite3_exec_printf() interface using the open database
285 ** DB. The SQL is the string FORMAT. The format string should contain
286 ** one %s or %q. STRING is the value inserted into %s or %q.
288 static int test_exec_printf(
290 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
291 int argc
, /* Number of arguments */
292 char **argv
/* Text of each argument */
301 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
302 " DB FORMAT STRING", 0);
305 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
306 Tcl_DStringInit(&str
);
307 zSql
= sqlite3_mprintf(argv
[2], argv
[3]);
308 rc
= sqlite3_exec(db
, zSql
, exec_printf_cb
, &str
, &zErr
);
310 sprintf(zBuf
, "%d", rc
);
311 Tcl_AppendElement(interp
, zBuf
);
312 Tcl_AppendElement(interp
, rc
==SQLITE_OK
? Tcl_DStringValue(&str
) : zErr
);
313 Tcl_DStringFree(&str
);
314 if( zErr
) sqlite3_free(zErr
);
315 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
320 ** Usage: sqlite3_exec_hex DB HEX
322 ** Invoke the sqlite3_exec() on a string that is obtained by translating
323 ** HEX into ASCII. Most characters are translated as is. %HH becomes
326 static int test_exec_hex(
328 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
329 int argc
, /* Number of arguments */
330 char **argv
/* Text of each argument */
340 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
344 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
346 for(i
=j
=0; i
<sizeof(zSql
) && zHex
[j
]; i
++, j
++){
347 if( zHex
[j
]=='%' && zHex
[j
+2] && zHex
[j
+2] ){
348 zSql
[i
] = (testHexToInt(zHex
[j
+1])<<4) + testHexToInt(zHex
[j
+2]);
355 Tcl_DStringInit(&str
);
356 rc
= sqlite3_exec(db
, zSql
, exec_printf_cb
, &str
, &zErr
);
357 sprintf(zBuf
, "%d", rc
);
358 Tcl_AppendElement(interp
, zBuf
);
359 Tcl_AppendElement(interp
, rc
==SQLITE_OK
? Tcl_DStringValue(&str
) : zErr
);
360 Tcl_DStringFree(&str
);
361 if( zErr
) sqlite3_free(zErr
);
362 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
367 ** Usage: db_enter DB
370 ** Enter or leave the mutex on a database connection.
374 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
375 int argc
, /* Number of arguments */
376 char **argv
/* Text of each argument */
380 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
384 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
385 sqlite3_mutex_enter(db
->mutex
);
390 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
391 int argc
, /* Number of arguments */
392 char **argv
/* Text of each argument */
396 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
400 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
401 sqlite3_mutex_leave(db
->mutex
);
406 ** Usage: sqlite3_exec DB SQL
408 ** Invoke the sqlite3_exec interface using the open database DB
410 static int test_exec(
412 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
413 int argc
, /* Number of arguments */
414 char **argv
/* Text of each argument */
424 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
428 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
429 Tcl_DStringInit(&str
);
430 zSql
= sqlite3_mprintf("%s", argv
[2]);
431 for(i
=j
=0; zSql
[i
];){
433 zSql
[j
++] = (testHexToInt(zSql
[i
+1])<<4) + testHexToInt(zSql
[i
+2]);
436 zSql
[j
++] = zSql
[i
++];
440 rc
= sqlite3_exec(db
, zSql
, exec_printf_cb
, &str
, &zErr
);
442 sprintf(zBuf
, "%d", rc
);
443 Tcl_AppendElement(interp
, zBuf
);
444 Tcl_AppendElement(interp
, rc
==SQLITE_OK
? Tcl_DStringValue(&str
) : zErr
);
445 Tcl_DStringFree(&str
);
446 if( zErr
) sqlite3_free(zErr
);
447 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
452 ** Usage: sqlite3_exec_nr DB SQL
454 ** Invoke the sqlite3_exec interface using the open database DB. Discard
457 static int test_exec_nr(
459 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
460 int argc
, /* Number of arguments */
461 char **argv
/* Text of each argument */
467 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
471 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
472 rc
= sqlite3_exec(db
, argv
[2], 0, 0, &zErr
);
473 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
478 ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
480 ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
481 ** concatenate arg0 through argn using separator as the separator.
482 ** Return the result.
484 static int test_mprintf_z(
486 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
487 int argc
, /* Number of arguments */
488 char **argv
/* Text of each argument */
493 for(i
=2; i
<argc
&& (i
==2 || zResult
); i
++){
494 zResult
= sqlite3_mprintf("%z%s%s", zResult
, argv
[1], argv
[i
]);
496 Tcl_AppendResult(interp
, zResult
, 0);
497 sqlite3_free(zResult
);
502 ** Usage: sqlite3_mprintf_n_test STRING
504 ** Test the %n format of sqlite_mprintf(). Return the length of the
507 static int test_mprintf_n(
509 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
510 int argc
, /* Number of arguments */
511 char **argv
/* Text of each argument */
515 zStr
= sqlite3_mprintf("%s%n", argv
[1], &n
);
517 Tcl_SetObjResult(interp
, Tcl_NewIntObj(n
));
522 ** Usage: sqlite3_snprintf_int SIZE FORMAT INT
524 ** Test the of sqlite3_snprintf() routine. SIZE is the size of the
525 ** output buffer in bytes. The maximum size is 100. FORMAT is the
526 ** format string. INT is a single integer argument. The FORMAT
527 ** string must require no more than this one integer argument. If
528 ** You pass in a format string that requires more than one argument,
529 ** bad things will happen.
531 static int test_snprintf_int(
533 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
534 int argc
, /* Number of arguments */
535 char **argv
/* Text of each argument */
538 int n
= atoi(argv
[1]);
539 const char *zFormat
= argv
[2];
540 int a1
= atoi(argv
[3]);
541 if( n
>sizeof(zStr
) ) n
= sizeof(zStr
);
542 sqlite3_snprintf(sizeof(zStr
), zStr
, "abcdefghijklmnopqrstuvwxyz");
543 sqlite3_snprintf(n
, zStr
, zFormat
, a1
);
544 Tcl_AppendResult(interp
, zStr
, 0);
548 #ifndef SQLITE_OMIT_GET_TABLE
551 ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
553 ** Invoke the sqlite3_get_table_printf() interface using the open database
554 ** DB. The SQL is the string FORMAT. The format string should contain
555 ** one %s or %q. STRING is the value inserted into %s or %q.
557 static int test_get_table_printf(
559 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
560 int argc
, /* Number of arguments */
561 char **argv
/* Text of each argument */
574 if( Tcl_GetInt(interp
, argv
[4], &resCount
) ) return TCL_ERROR
;
576 if( argc
!=4 && argc
!=5 ){
577 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
578 " DB FORMAT STRING ?COUNT?", 0);
581 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
582 Tcl_DStringInit(&str
);
583 zSql
= sqlite3_mprintf(argv
[2],argv
[3]);
585 rc
= sqlite3_get_table(db
, zSql
, &aResult
, 0, 0, &zErr
);
587 rc
= sqlite3_get_table(db
, zSql
, &aResult
, &nRow
, &nCol
, &zErr
);
588 resCount
= (nRow
+1)*nCol
;
591 sprintf(zBuf
, "%d", rc
);
592 Tcl_AppendElement(interp
, zBuf
);
595 sprintf(zBuf
, "%d", nRow
);
596 Tcl_AppendElement(interp
, zBuf
);
597 sprintf(zBuf
, "%d", nCol
);
598 Tcl_AppendElement(interp
, zBuf
);
600 for(i
=0; i
<resCount
; i
++){
601 Tcl_AppendElement(interp
, aResult
[i
] ? aResult
[i
] : "NULL");
604 Tcl_AppendElement(interp
, zErr
);
606 sqlite3_free_table(aResult
);
607 if( zErr
) sqlite3_free(zErr
);
608 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
612 #endif /* SQLITE_OMIT_GET_TABLE */
616 ** Usage: sqlite3_last_insert_rowid DB
618 ** Returns the integer ROWID of the most recent insert.
620 static int test_last_rowid(
622 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
623 int argc
, /* Number of arguments */
624 char **argv
/* Text of each argument */
630 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0], " DB\"", 0);
633 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
634 sprintf(zBuf
, "%lld", sqlite3_last_insert_rowid(db
));
635 Tcl_AppendResult(interp
, zBuf
, 0);
640 ** Usage: sqlite3_key DB KEY
642 ** Set the codec key.
646 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
647 int argc
, /* Number of arguments */
648 char **argv
/* Text of each argument */
650 #ifdef SQLITE_HAS_CODEC
655 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
659 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
662 sqlite3_key(db
, zKey
, nKey
);
668 ** Usage: sqlite3_rekey DB KEY
670 ** Change the codec key.
672 static int test_rekey(
674 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
675 int argc
, /* Number of arguments */
676 char **argv
/* Text of each argument */
678 #ifdef SQLITE_HAS_CODEC
683 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
687 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
690 sqlite3_rekey(db
, zKey
, nKey
);
696 ** Usage: sqlite3_close DB
698 ** Closes the database opened by sqlite3_open.
700 static int sqlite_test_close(
702 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
703 int argc
, /* Number of arguments */
704 char **argv
/* Text of each argument */
709 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
713 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
714 rc
= sqlite3_close(db
);
715 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
720 ** Usage: sqlite3_close_v2 DB
722 ** Closes the database opened by sqlite3_open.
724 static int sqlite_test_close_v2(
726 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
727 int argc
, /* Number of arguments */
728 char **argv
/* Text of each argument */
733 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
737 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
738 rc
= sqlite3_close_v2(db
);
739 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
744 ** Implementation of the x_coalesce() function.
745 ** Return the first argument non-NULL argument.
747 static void t1_ifnullFunc(
748 sqlite3_context
*context
,
753 for(i
=0; i
<argc
; i
++){
754 if( SQLITE_NULL
!=sqlite3_value_type(argv
[i
]) ){
755 int n
= sqlite3_value_bytes(argv
[i
]);
756 sqlite3_result_text(context
, (char*)sqlite3_value_text(argv
[i
]),
757 n
, SQLITE_TRANSIENT
);
764 ** These are test functions. hex8() interprets its argument as
765 ** UTF8 and returns a hex encoding. hex16le() interprets its argument
766 ** as UTF16le and returns a hex encoding.
768 static void hex8Func(sqlite3_context
*p
, int argc
, sqlite3_value
**argv
){
769 const unsigned char *z
;
772 z
= sqlite3_value_text(argv
[0]);
773 for(i
=0; i
<sizeof(zBuf
)/2 - 2 && z
[i
]; i
++){
774 sprintf(&zBuf
[i
*2], "%02x", z
[i
]&0xff);
777 sqlite3_result_text(p
, (char*)zBuf
, -1, SQLITE_TRANSIENT
);
779 #ifndef SQLITE_OMIT_UTF16
780 static void hex16Func(sqlite3_context
*p
, int argc
, sqlite3_value
**argv
){
781 const unsigned short int *z
;
784 z
= sqlite3_value_text16(argv
[0]);
785 for(i
=0; i
<sizeof(zBuf
)/4 - 4 && z
[i
]; i
++){
786 sprintf(&zBuf
[i
*4], "%04x", z
[i
]&0xff);
789 sqlite3_result_text(p
, (char*)zBuf
, -1, SQLITE_TRANSIENT
);
794 ** A structure into which to accumulate text.
797 int nAlloc
; /* Space allocated */
798 int nUsed
; /* Space used */
799 char *z
; /* The space */
803 ** Append text to a dstr
805 static void dstrAppend(struct dstr
*p
, const char *z
, int divider
){
806 int n
= (int)strlen(z
);
807 if( p
->nUsed
+ n
+ 2 > p
->nAlloc
){
809 p
->nAlloc
= p
->nAlloc
*2 + n
+ 200;
810 zNew
= sqlite3_realloc(p
->z
, p
->nAlloc
);
813 memset(p
, 0, sizeof(*p
));
818 if( divider
&& p
->nUsed
>0 ){
819 p
->z
[p
->nUsed
++] = divider
;
821 memcpy(&p
->z
[p
->nUsed
], z
, n
+1);
826 ** Invoked for each callback from sqlite3ExecFunc
828 static int execFuncCallback(void *pData
, int argc
, char **argv
, char **NotUsed
){
829 struct dstr
*p
= (struct dstr
*)pData
;
831 for(i
=0; i
<argc
; i
++){
833 dstrAppend(p
, "NULL", ' ');
835 dstrAppend(p
, argv
[i
], ' ');
842 ** Implementation of the x_sqlite_exec() function. This function takes
843 ** a single argument and attempts to execute that argument as SQL code.
844 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
846 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
847 ** from within a function call.
849 ** This routine simulates the effect of having two threads attempt to
850 ** use the same database at the same time.
852 static void sqlite3ExecFunc(
853 sqlite3_context
*context
,
858 memset(&x
, 0, sizeof(x
));
859 (void)sqlite3_exec((sqlite3
*)sqlite3_user_data(context
),
860 (char*)sqlite3_value_text(argv
[0]),
861 execFuncCallback
, &x
, 0);
862 sqlite3_result_text(context
, x
.z
, x
.nUsed
, SQLITE_TRANSIENT
);
867 ** Implementation of tkt2213func(), a scalar function that takes exactly
868 ** one argument. It has two interesting features:
870 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
871 ** If the three pointers returned are not the same an SQL error is raised.
873 ** * Otherwise it returns a copy of the text representation of its
874 ** argument in such a way as the VDBE representation is a Mem* cell
875 ** with the MEM_Term flag clear.
877 ** Ticket #2213 can therefore be tested by evaluating the following
880 ** tkt2213func(tkt2213func('a string'));
882 static void tkt2213Function(
883 sqlite3_context
*context
,
888 unsigned char const *zText1
;
889 unsigned char const *zText2
;
890 unsigned char const *zText3
;
892 nText
= sqlite3_value_bytes(argv
[0]);
893 zText1
= sqlite3_value_text(argv
[0]);
894 zText2
= sqlite3_value_text(argv
[0]);
895 zText3
= sqlite3_value_text(argv
[0]);
897 if( zText1
!=zText2
|| zText2
!=zText3
){
898 sqlite3_result_error(context
, "tkt2213 is not fixed", -1);
900 char *zCopy
= (char *)sqlite3_malloc(nText
);
901 memcpy(zCopy
, zText1
, nText
);
902 sqlite3_result_text(context
, zCopy
, nText
, sqlite3_free
);
907 ** The following SQL function takes 4 arguments. The 2nd and
908 ** 4th argument must be one of these strings: 'text', 'text16',
909 ** or 'blob' corresponding to API functions
911 ** sqlite3_value_text()
912 ** sqlite3_value_text16()
913 ** sqlite3_value_blob()
915 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
916 ** corresponding to APIs:
918 ** sqlite3_value_bytes()
919 ** sqlite3_value_bytes16()
922 ** The APIs designated by the 2nd through 4th arguments are applied
923 ** to the first argument in order. If the pointers returned by the
924 ** second and fourth are different, this routine returns 1. Otherwise,
925 ** this routine returns 0.
927 ** This function is used to test to see when returned pointers from
928 ** the _text(), _text16() and _blob() APIs become invalidated.
930 static void ptrChngFunction(
931 sqlite3_context
*context
,
937 if( argc
!=4 ) return;
938 zCmd
= (const char*)sqlite3_value_text(argv
[1]);
939 if( zCmd
==0 ) return;
940 if( strcmp(zCmd
,"text")==0 ){
941 p1
= (const void*)sqlite3_value_text(argv
[0]);
942 #ifndef SQLITE_OMIT_UTF16
943 }else if( strcmp(zCmd
, "text16")==0 ){
944 p1
= (const void*)sqlite3_value_text16(argv
[0]);
946 }else if( strcmp(zCmd
, "blob")==0 ){
947 p1
= (const void*)sqlite3_value_blob(argv
[0]);
951 zCmd
= (const char*)sqlite3_value_text(argv
[2]);
952 if( zCmd
==0 ) return;
953 if( strcmp(zCmd
,"bytes")==0 ){
954 sqlite3_value_bytes(argv
[0]);
955 #ifndef SQLITE_OMIT_UTF16
956 }else if( strcmp(zCmd
, "bytes16")==0 ){
957 sqlite3_value_bytes16(argv
[0]);
959 }else if( strcmp(zCmd
, "noop")==0 ){
964 zCmd
= (const char*)sqlite3_value_text(argv
[3]);
965 if( zCmd
==0 ) return;
966 if( strcmp(zCmd
,"text")==0 ){
967 p2
= (const void*)sqlite3_value_text(argv
[0]);
968 #ifndef SQLITE_OMIT_UTF16
969 }else if( strcmp(zCmd
, "text16")==0 ){
970 p2
= (const void*)sqlite3_value_text16(argv
[0]);
972 }else if( strcmp(zCmd
, "blob")==0 ){
973 p2
= (const void*)sqlite3_value_blob(argv
[0]);
977 sqlite3_result_int(context
, p1
!=p2
);
981 ** This SQL function returns a different answer each time it is called, even if
982 ** the arguments are the same.
984 static void nondeterministicFunction(
985 sqlite3_context
*context
,
990 sqlite3_result_int(context
, cnt
++);
994 ** Usage: sqlite3_create_function DB
996 ** Call the sqlite3_create_function API on the given database in order
997 ** to create a function named "x_coalesce". This function does the same thing
998 ** as the "coalesce" function. This function also registers an SQL function
999 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
1000 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
1001 ** The effect is similar to trying to use the same database connection from
1002 ** two threads at the same time.
1004 ** The original motivation for this routine was to be able to call the
1005 ** sqlite3_create_function function while a query is in progress in order
1006 ** to test the SQLITE_MISUSE detection logic.
1008 static int test_create_function(
1010 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1011 int argc
, /* Number of arguments */
1012 char **argv
/* Text of each argument */
1018 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1022 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
1023 rc
= sqlite3_create_function(db
, "x_coalesce", -1, SQLITE_UTF8
, 0,
1024 t1_ifnullFunc
, 0, 0);
1025 if( rc
==SQLITE_OK
){
1026 rc
= sqlite3_create_function(db
, "hex8", 1, SQLITE_UTF8
| SQLITE_DETERMINISTIC
,
1029 #ifndef SQLITE_OMIT_UTF16
1030 if( rc
==SQLITE_OK
){
1031 rc
= sqlite3_create_function(db
, "hex16", 1, SQLITE_UTF16
| SQLITE_DETERMINISTIC
,
1032 0, hex16Func
, 0, 0);
1035 if( rc
==SQLITE_OK
){
1036 rc
= sqlite3_create_function(db
, "tkt2213func", 1, SQLITE_ANY
, 0,
1037 tkt2213Function
, 0, 0);
1039 if( rc
==SQLITE_OK
){
1040 rc
= sqlite3_create_function(db
, "pointer_change", 4, SQLITE_ANY
, 0,
1041 ptrChngFunction
, 0, 0);
1044 /* Functions counter1() and counter2() have the same implementation - they
1045 ** both return an ascending integer with each call. But counter1() is marked
1046 ** as non-deterministic and counter2() is marked as deterministic.
1048 if( rc
==SQLITE_OK
){
1049 rc
= sqlite3_create_function(db
, "counter1", -1, SQLITE_UTF8
,
1050 0, nondeterministicFunction
, 0, 0);
1052 if( rc
==SQLITE_OK
){
1053 rc
= sqlite3_create_function(db
, "counter2", -1, SQLITE_UTF8
|SQLITE_DETERMINISTIC
,
1054 0, nondeterministicFunction
, 0, 0);
1057 #ifndef SQLITE_OMIT_UTF16
1058 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1059 ** because it is not tested anywhere else. */
1060 if( rc
==SQLITE_OK
){
1062 sqlite3_value
*pVal
;
1063 sqlite3_mutex_enter(db
->mutex
);
1064 pVal
= sqlite3ValueNew(db
);
1065 sqlite3ValueSetStr(pVal
, -1, "x_sqlite_exec", SQLITE_UTF8
, SQLITE_STATIC
);
1066 zUtf16
= sqlite3ValueText(pVal
, SQLITE_UTF16NATIVE
);
1067 if( db
->mallocFailed
){
1070 rc
= sqlite3_create_function16(db
, zUtf16
,
1071 1, SQLITE_UTF16
, db
, sqlite3ExecFunc
, 0, 0);
1073 sqlite3ValueFree(pVal
);
1074 sqlite3_mutex_leave(db
->mutex
);
1078 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
1079 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), 0);
1084 ** Routines to implement the x_count() aggregate function.
1086 ** x_count() counts the number of non-null arguments. But there are
1087 ** some twists for testing purposes.
1089 ** If the argument to x_count() is 40 then a UTF-8 error is reported
1090 ** on the step function. If x_count(41) is seen, then a UTF-16 error
1091 ** is reported on the step function. If the total count is 42, then
1092 ** a UTF-8 error is reported on the finalize function.
1094 typedef struct t1CountCtx t1CountCtx
;
1098 static void t1CountStep(
1099 sqlite3_context
*context
,
1101 sqlite3_value
**argv
1104 p
= sqlite3_aggregate_context(context
, sizeof(*p
));
1105 if( (argc
==0 || SQLITE_NULL
!=sqlite3_value_type(argv
[0]) ) && p
){
1109 int v
= sqlite3_value_int(argv
[0]);
1111 sqlite3_result_error(context
, "value of 40 handed to x_count", -1);
1112 #ifndef SQLITE_OMIT_UTF16
1114 const char zUtf16ErrMsg
[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1115 sqlite3_result_error16(context
, &zUtf16ErrMsg
[1-SQLITE_BIGENDIAN
], -1);
1120 static void t1CountFinalize(sqlite3_context
*context
){
1122 p
= sqlite3_aggregate_context(context
, sizeof(*p
));
1125 sqlite3_result_error(context
, "x_count totals to 42", -1);
1127 sqlite3_result_int(context
, p
? p
->n
: 0);
1132 #ifndef SQLITE_OMIT_DEPRECATED
1133 static void legacyCountStep(
1134 sqlite3_context
*context
,
1136 sqlite3_value
**argv
1141 static void legacyCountFinalize(sqlite3_context
*context
){
1142 sqlite3_result_int(context
, sqlite3_aggregate_count(context
));
1147 ** Usage: sqlite3_create_aggregate DB
1149 ** Call the sqlite3_create_function API on the given database in order
1150 ** to create a function named "x_count". This function is similar
1151 ** to the built-in count() function, with a few special quirks
1152 ** for testing the sqlite3_result_error() APIs.
1154 ** The original motivation for this routine was to be able to call the
1155 ** sqlite3_create_aggregate function while a query is in progress in order
1156 ** to test the SQLITE_MISUSE detection logic. See misuse.test.
1158 ** This routine was later extended to test the use of sqlite3_result_error()
1159 ** within aggregate functions.
1161 ** Later: It is now also extended to register the aggregate function
1162 ** "legacy_count()" with the supplied database handle. This is used
1163 ** to test the deprecated sqlite3_aggregate_count() API.
1165 static int test_create_aggregate(
1167 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1168 int argc
, /* Number of arguments */
1169 char **argv
/* Text of each argument */
1174 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1178 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
1179 rc
= sqlite3_create_function(db
, "x_count", 0, SQLITE_UTF8
, 0, 0,
1180 t1CountStep
,t1CountFinalize
);
1181 if( rc
==SQLITE_OK
){
1182 rc
= sqlite3_create_function(db
, "x_count", 1, SQLITE_UTF8
, 0, 0,
1183 t1CountStep
,t1CountFinalize
);
1185 #ifndef SQLITE_OMIT_DEPRECATED
1186 if( rc
==SQLITE_OK
){
1187 rc
= sqlite3_create_function(db
, "legacy_count", 0, SQLITE_ANY
, 0, 0,
1188 legacyCountStep
, legacyCountFinalize
1192 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
1193 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), 0);
1199 ** Usage: printf TEXT
1201 ** Send output to printf. Use this rather than puts to merge the output
1202 ** in the correct sequence with debugging printfs inserted into C code.
1203 ** Puts uses a separate buffer and debugging statements will be out of
1204 ** sequence if it is used.
1206 static int test_printf(
1208 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1209 int argc
, /* Number of arguments */
1210 char **argv
/* Text of each argument */
1213 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1217 printf("%s\n", argv
[1]);
1224 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1226 ** Call mprintf with three integer arguments
1228 static int sqlite3_mprintf_int(
1230 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1231 int argc
, /* Number of arguments */
1232 char **argv
/* Text of each argument */
1237 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1238 " FORMAT INT INT INT\"", 0);
1242 if( Tcl_GetInt(interp
, argv
[i
], &a
[i
-2]) ) return TCL_ERROR
;
1244 z
= sqlite3_mprintf(argv
[1], a
[0], a
[1], a
[2]);
1245 Tcl_AppendResult(interp
, z
, 0);
1251 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1253 ** Call mprintf with three 64-bit integer arguments
1255 static int sqlite3_mprintf_int64(
1257 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1258 int argc
, /* Number of arguments */
1259 char **argv
/* Text of each argument */
1265 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1266 " FORMAT INT INT INT\"", 0);
1270 if( sqlite3Atoi64(argv
[i
], &a
[i
-2], 1000000, SQLITE_UTF8
) ){
1271 Tcl_AppendResult(interp
, "argument is not a valid 64-bit integer", 0);
1275 z
= sqlite3_mprintf(argv
[1], a
[0], a
[1], a
[2]);
1276 Tcl_AppendResult(interp
, z
, 0);
1282 ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1284 ** Call mprintf with three long integer arguments. This might be the
1285 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1288 static int sqlite3_mprintf_long(
1290 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1291 int argc
, /* Number of arguments */
1292 char **argv
/* Text of each argument */
1299 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1300 " FORMAT INT INT INT\"", 0);
1304 if( Tcl_GetInt(interp
, argv
[i
], &b
[i
-2]) ) return TCL_ERROR
;
1305 a
[i
-2] = (long int)b
[i
-2];
1306 a
[i
-2] &= (((u64
)1)<<(sizeof(int)*8))-1;
1308 z
= sqlite3_mprintf(argv
[1], a
[0], a
[1], a
[2]);
1309 Tcl_AppendResult(interp
, z
, 0);
1315 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1317 ** Call mprintf with two integer arguments and one string argument
1319 static int sqlite3_mprintf_str(
1321 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1322 int argc
, /* Number of arguments */
1323 char **argv
/* Text of each argument */
1327 if( argc
<4 || argc
>5 ){
1328 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1329 " FORMAT INT INT ?STRING?\"", 0);
1333 if( Tcl_GetInt(interp
, argv
[i
], &a
[i
-2]) ) return TCL_ERROR
;
1335 z
= sqlite3_mprintf(argv
[1], a
[0], a
[1], argc
>4 ? argv
[4] : NULL
);
1336 Tcl_AppendResult(interp
, z
, 0);
1342 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1344 ** Call mprintf with two integer arguments and one string argument
1346 static int sqlite3_snprintf_str(
1348 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1349 int argc
, /* Number of arguments */
1350 char **argv
/* Text of each argument */
1355 if( argc
<5 || argc
>6 ){
1356 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1357 " INT FORMAT INT INT ?STRING?\"", 0);
1360 if( Tcl_GetInt(interp
, argv
[1], &n
) ) return TCL_ERROR
;
1362 Tcl_AppendResult(interp
, "N must be non-negative", 0);
1366 if( Tcl_GetInt(interp
, argv
[i
], &a
[i
-3]) ) return TCL_ERROR
;
1368 z
= sqlite3_malloc( n
+1 );
1369 sqlite3_snprintf(n
, z
, argv
[2], a
[0], a
[1], argc
>4 ? argv
[5] : NULL
);
1370 Tcl_AppendResult(interp
, z
, 0);
1376 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1378 ** Call mprintf with two integer arguments and one double argument
1380 static int sqlite3_mprintf_double(
1382 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1383 int argc
, /* Number of arguments */
1384 char **argv
/* Text of each argument */
1390 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1391 " FORMAT INT INT DOUBLE\"", 0);
1395 if( Tcl_GetInt(interp
, argv
[i
], &a
[i
-2]) ) return TCL_ERROR
;
1397 if( Tcl_GetDouble(interp
, argv
[4], &r
) ) return TCL_ERROR
;
1398 z
= sqlite3_mprintf(argv
[1], a
[0], a
[1], r
);
1399 Tcl_AppendResult(interp
, z
, 0);
1405 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1407 ** Call mprintf with a single double argument which is the product of the
1408 ** two arguments given above. This is used to generate overflow and underflow
1409 ** doubles to test that they are converted properly.
1411 static int sqlite3_mprintf_scaled(
1413 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1414 int argc
, /* Number of arguments */
1415 char **argv
/* Text of each argument */
1421 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1422 " FORMAT DOUBLE DOUBLE\"", 0);
1426 if( Tcl_GetDouble(interp
, argv
[i
], &r
[i
-2]) ) return TCL_ERROR
;
1428 z
= sqlite3_mprintf(argv
[1], r
[0]*r
[1]);
1429 Tcl_AppendResult(interp
, z
, 0);
1435 ** Usage: sqlite3_mprintf_stronly FORMAT STRING
1437 ** Call mprintf with a single double argument which is the product of the
1438 ** two arguments given above. This is used to generate overflow and underflow
1439 ** doubles to test that they are converted properly.
1441 static int sqlite3_mprintf_stronly(
1443 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1444 int argc
, /* Number of arguments */
1445 char **argv
/* Text of each argument */
1449 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1450 " FORMAT STRING\"", 0);
1453 z
= sqlite3_mprintf(argv
[1], argv
[2]);
1454 Tcl_AppendResult(interp
, z
, 0);
1460 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1462 ** Call mprintf with a single double argument which is derived from the
1463 ** hexadecimal encoding of an IEEE double.
1465 static int sqlite3_mprintf_hexdouble(
1467 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1468 int argc
, /* Number of arguments */
1469 char **argv
/* Text of each argument */
1473 unsigned int x1
, x2
;
1476 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
1477 " FORMAT STRING\"", 0);
1480 if( sscanf(argv
[2], "%08x%08x", &x2
, &x1
)!=2 ){
1481 Tcl_AppendResult(interp
, "2nd argument should be 16-characters of hex", 0);
1486 memcpy(&r
, &d
, sizeof(r
));
1487 z
= sqlite3_mprintf(argv
[1], r
);
1488 Tcl_AppendResult(interp
, z
, 0);
1494 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1497 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1498 static int test_enable_shared(
1499 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
1500 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1501 int objc
, /* Number of arguments */
1502 Tcl_Obj
*CONST objv
[] /* Command arguments */
1508 if( objc
!=2 && objc
!=1 ){
1509 Tcl_WrongNumArgs(interp
, 1, objv
, "?BOOLEAN?");
1512 ret
= sqlite3GlobalConfig
.sharedCacheEnabled
;
1515 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &enable
) ){
1518 rc
= sqlite3_enable_shared_cache(enable
);
1519 if( rc
!=SQLITE_OK
){
1520 Tcl_SetResult(interp
, (char *)sqlite3ErrStr(rc
), TCL_STATIC
);
1524 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(ret
));
1532 ** Usage: sqlite3_extended_result_codes DB BOOLEAN
1535 static int test_extended_result_codes(
1536 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
1537 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1538 int objc
, /* Number of arguments */
1539 Tcl_Obj
*CONST objv
[] /* Command arguments */
1545 Tcl_WrongNumArgs(interp
, 1, objv
, "DB BOOLEAN");
1548 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1549 if( Tcl_GetBooleanFromObj(interp
, objv
[2], &enable
) ) return TCL_ERROR
;
1550 sqlite3_extended_result_codes(db
, enable
);
1555 ** Usage: sqlite3_libversion_number
1558 static int test_libversion_number(
1559 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
1560 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1561 int objc
, /* Number of arguments */
1562 Tcl_Obj
*CONST objv
[] /* Command arguments */
1564 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_libversion_number()));
1569 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1572 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1573 static int test_table_column_metadata(
1574 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
1575 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1576 int objc
, /* Number of arguments */
1577 Tcl_Obj
*CONST objv
[] /* Command arguments */
1586 const char *zDatatype
;
1587 const char *zCollseq
;
1593 Tcl_WrongNumArgs(interp
, 1, objv
, "DB dbname tblname colname");
1596 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1597 zDb
= Tcl_GetString(objv
[2]);
1598 zTbl
= Tcl_GetString(objv
[3]);
1599 zCol
= Tcl_GetString(objv
[4]);
1601 if( strlen(zDb
)==0 ) zDb
= 0;
1603 rc
= sqlite3_table_column_metadata(db
, zDb
, zTbl
, zCol
,
1604 &zDatatype
, &zCollseq
, ¬null
, &primarykey
, &autoincrement
);
1606 if( rc
!=SQLITE_OK
){
1607 Tcl_AppendResult(interp
, sqlite3_errmsg(db
), 0);
1611 pRet
= Tcl_NewObj();
1612 Tcl_ListObjAppendElement(0, pRet
, Tcl_NewStringObj(zDatatype
, -1));
1613 Tcl_ListObjAppendElement(0, pRet
, Tcl_NewStringObj(zCollseq
, -1));
1614 Tcl_ListObjAppendElement(0, pRet
, Tcl_NewIntObj(notnull
));
1615 Tcl_ListObjAppendElement(0, pRet
, Tcl_NewIntObj(primarykey
));
1616 Tcl_ListObjAppendElement(0, pRet
, Tcl_NewIntObj(autoincrement
));
1617 Tcl_SetObjResult(interp
, pRet
);
1623 #ifndef SQLITE_OMIT_INCRBLOB
1625 static int blobHandleFromObj(
1628 sqlite3_blob
**ppBlob
1633 z
= Tcl_GetStringFromObj(pObj
, &n
);
1638 Tcl_Channel channel
;
1639 ClientData instanceData
;
1641 channel
= Tcl_GetChannel(interp
, z
, ¬Used
);
1642 if( !channel
) return TCL_ERROR
;
1645 Tcl_Seek(channel
, 0, SEEK_SET
);
1647 instanceData
= Tcl_GetChannelInstanceData(channel
);
1648 *ppBlob
= *((sqlite3_blob
**)instanceData
);
1655 ** sqlite3_blob_bytes CHANNEL
1657 static int test_blob_bytes(
1658 ClientData clientData
, /* Not used */
1659 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1660 int objc
, /* Number of arguments */
1661 Tcl_Obj
*CONST objv
[] /* Command arguments */
1663 sqlite3_blob
*pBlob
;
1667 Tcl_WrongNumArgs(interp
, 1, objv
, "CHANNEL");
1671 if( blobHandleFromObj(interp
, objv
[1], &pBlob
) ) return TCL_ERROR
;
1672 nByte
= sqlite3_blob_bytes(pBlob
);
1673 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nByte
));
1679 ** sqlite3_blob_close CHANNEL
1681 static int test_blob_close(
1682 ClientData clientData
, /* Not used */
1683 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1684 int objc
, /* Number of arguments */
1685 Tcl_Obj
*CONST objv
[] /* Command arguments */
1687 sqlite3_blob
*pBlob
;
1690 Tcl_WrongNumArgs(interp
, 1, objv
, "CHANNEL");
1694 if( blobHandleFromObj(interp
, objv
[1], &pBlob
) ) return TCL_ERROR
;
1695 sqlite3_blob_close(pBlob
);
1701 ** sqlite3_blob_read CHANNEL OFFSET N
1703 ** This command is used to test the sqlite3_blob_read() in ways that
1704 ** the Tcl channel interface does not. The first argument should
1705 ** be the name of a valid channel created by the [incrblob] method
1706 ** of a database handle. This function calls sqlite3_blob_read()
1707 ** to read N bytes from offset OFFSET from the underlying SQLite
1710 ** On success, a byte-array object containing the read data is
1711 ** returned. On failure, the interpreter result is set to the
1712 ** text representation of the returned error code (i.e. "SQLITE_NOMEM")
1713 ** and a Tcl exception is thrown.
1715 static int test_blob_read(
1716 ClientData clientData
, /* Not used */
1717 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1718 int objc
, /* Number of arguments */
1719 Tcl_Obj
*CONST objv
[] /* Command arguments */
1721 sqlite3_blob
*pBlob
;
1724 unsigned char *zBuf
= 0;
1728 Tcl_WrongNumArgs(interp
, 1, objv
, "CHANNEL OFFSET N");
1732 if( blobHandleFromObj(interp
, objv
[1], &pBlob
) ) return TCL_ERROR
;
1733 if( TCL_OK
!=Tcl_GetIntFromObj(interp
, objv
[2], &iOffset
)
1734 || TCL_OK
!=Tcl_GetIntFromObj(interp
, objv
[3], &nByte
)
1740 zBuf
= (unsigned char *)Tcl_Alloc(nByte
);
1742 rc
= sqlite3_blob_read(pBlob
, zBuf
, nByte
, iOffset
);
1743 if( rc
==SQLITE_OK
){
1744 Tcl_SetObjResult(interp
, Tcl_NewByteArrayObj(zBuf
, nByte
));
1746 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1748 Tcl_Free((char *)zBuf
);
1750 return (rc
==SQLITE_OK
? TCL_OK
: TCL_ERROR
);
1754 ** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA?
1756 ** This command is used to test the sqlite3_blob_write() in ways that
1757 ** the Tcl channel interface does not. The first argument should
1758 ** be the name of a valid channel created by the [incrblob] method
1759 ** of a database handle. This function calls sqlite3_blob_write()
1760 ** to write the DATA byte-array to the underlying SQLite blob handle.
1761 ** at offset OFFSET.
1763 ** On success, an empty string is returned. On failure, the interpreter
1764 ** result is set to the text representation of the returned error code
1765 ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
1767 static int test_blob_write(
1768 ClientData clientData
, /* Not used */
1769 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1770 int objc
, /* Number of arguments */
1771 Tcl_Obj
*CONST objv
[] /* Command arguments */
1773 sqlite3_blob
*pBlob
;
1777 unsigned char *zBuf
;
1780 if( objc
!=4 && objc
!=5 ){
1781 Tcl_WrongNumArgs(interp
, 1, objv
, "CHANNEL OFFSET DATA ?NDATA?");
1785 if( blobHandleFromObj(interp
, objv
[1], &pBlob
) ) return TCL_ERROR
;
1786 if( TCL_OK
!=Tcl_GetIntFromObj(interp
, objv
[2], &iOffset
) ){
1790 zBuf
= Tcl_GetByteArrayFromObj(objv
[3], &nBuf
);
1791 if( objc
==5 && Tcl_GetIntFromObj(interp
, objv
[4], &nBuf
) ){
1794 rc
= sqlite3_blob_write(pBlob
, zBuf
, nBuf
, iOffset
);
1795 if( rc
!=SQLITE_OK
){
1796 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1799 return (rc
==SQLITE_OK
? TCL_OK
: TCL_ERROR
);
1802 static int test_blob_reopen(
1803 ClientData clientData
, /* Not used */
1804 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1805 int objc
, /* Number of arguments */
1806 Tcl_Obj
*CONST objv
[] /* Command arguments */
1809 sqlite3_blob
*pBlob
;
1813 Tcl_WrongNumArgs(interp
, 1, objv
, "CHANNEL ROWID");
1817 if( blobHandleFromObj(interp
, objv
[1], &pBlob
) ) return TCL_ERROR
;
1818 if( Tcl_GetWideIntFromObj(interp
, objv
[2], &iRowid
) ) return TCL_ERROR
;
1820 rc
= sqlite3_blob_reopen(pBlob
, iRowid
);
1821 if( rc
!=SQLITE_OK
){
1822 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1825 return (rc
==SQLITE_OK
? TCL_OK
: TCL_ERROR
);
1831 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1833 ** This Tcl proc is used for testing the experimental
1834 ** sqlite3_create_collation_v2() interface.
1836 struct TestCollationX
{
1841 typedef struct TestCollationX TestCollationX
;
1842 static void testCreateCollationDel(void *pCtx
){
1843 TestCollationX
*p
= (TestCollationX
*)pCtx
;
1845 int rc
= Tcl_EvalObjEx(p
->interp
, p
->pDel
, TCL_EVAL_DIRECT
|TCL_EVAL_GLOBAL
);
1847 Tcl_BackgroundError(p
->interp
);
1850 Tcl_DecrRefCount(p
->pCmp
);
1851 Tcl_DecrRefCount(p
->pDel
);
1852 sqlite3_free((void *)p
);
1854 static int testCreateCollationCmp(
1861 TestCollationX
*p
= (TestCollationX
*)pCtx
;
1862 Tcl_Obj
*pScript
= Tcl_DuplicateObj(p
->pCmp
);
1865 Tcl_IncrRefCount(pScript
);
1866 Tcl_ListObjAppendElement(0, pScript
, Tcl_NewStringObj((char *)zLeft
, nLeft
));
1867 Tcl_ListObjAppendElement(0, pScript
, Tcl_NewStringObj((char *)zRight
,nRight
));
1869 if( TCL_OK
!=Tcl_EvalObjEx(p
->interp
, pScript
, TCL_EVAL_DIRECT
|TCL_EVAL_GLOBAL
)
1870 || TCL_OK
!=Tcl_GetIntFromObj(p
->interp
, Tcl_GetObjResult(p
->interp
), &iRes
)
1872 Tcl_BackgroundError(p
->interp
);
1874 Tcl_DecrRefCount(pScript
);
1878 static int test_create_collation_v2(
1879 ClientData clientData
, /* Not used */
1880 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
1881 int objc
, /* Number of arguments */
1882 Tcl_Obj
*CONST objv
[] /* Command arguments */
1889 Tcl_WrongNumArgs(interp
, 1, objv
, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1892 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1894 p
= (TestCollationX
*)sqlite3_malloc(sizeof(TestCollationX
));
1898 Tcl_IncrRefCount(p
->pCmp
);
1899 Tcl_IncrRefCount(p
->pDel
);
1901 rc
= sqlite3_create_collation_v2(db
, Tcl_GetString(objv
[2]), 16,
1902 (void *)p
, testCreateCollationCmp
, testCreateCollationDel
1904 if( rc
!=SQLITE_MISUSE
){
1905 Tcl_AppendResult(interp
, "sqlite3_create_collate_v2() failed to detect "
1906 "an invalid encoding", (char*)0);
1909 rc
= sqlite3_create_collation_v2(db
, Tcl_GetString(objv
[2]), SQLITE_UTF8
,
1910 (void *)p
, testCreateCollationCmp
, testCreateCollationDel
1916 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
1918 ** Available switches are:
1925 typedef struct CreateFunctionV2 CreateFunctionV2
;
1926 struct CreateFunctionV2
{
1928 Tcl_Obj
*pFunc
; /* Script for function invocation */
1929 Tcl_Obj
*pStep
; /* Script for agg. step invocation */
1930 Tcl_Obj
*pFinal
; /* Script for agg. finalization invocation */
1931 Tcl_Obj
*pDestroy
; /* Destructor script */
1933 static void cf2Func(sqlite3_context
*ctx
, int nArg
, sqlite3_value
**aArg
){
1935 static void cf2Step(sqlite3_context
*ctx
, int nArg
, sqlite3_value
**aArg
){
1937 static void cf2Final(sqlite3_context
*ctx
){
1939 static void cf2Destroy(void *pUser
){
1940 CreateFunctionV2
*p
= (CreateFunctionV2
*)pUser
;
1942 if( p
->interp
&& p
->pDestroy
){
1943 int rc
= Tcl_EvalObjEx(p
->interp
, p
->pDestroy
, 0);
1944 if( rc
!=TCL_OK
) Tcl_BackgroundError(p
->interp
);
1947 if( p
->pFunc
) Tcl_DecrRefCount(p
->pFunc
);
1948 if( p
->pStep
) Tcl_DecrRefCount(p
->pStep
);
1949 if( p
->pFinal
) Tcl_DecrRefCount(p
->pFinal
);
1950 if( p
->pDestroy
) Tcl_DecrRefCount(p
->pDestroy
);
1953 static int test_create_function_v2(
1954 ClientData clientData
, /* Not used */
1955 Tcl_Interp
*interp
, /* The invoking TCL interpreter */
1956 int objc
, /* Number of arguments */
1957 Tcl_Obj
*CONST objv
[] /* Command arguments */
1963 CreateFunctionV2
*p
;
1971 {"utf8", SQLITE_UTF8
},
1972 {"utf16", SQLITE_UTF16
},
1973 {"utf16le", SQLITE_UTF16LE
},
1974 {"utf16be", SQLITE_UTF16BE
},
1975 {"any", SQLITE_ANY
},
1979 if( objc
<5 || (objc
%2)==0 ){
1980 Tcl_WrongNumArgs(interp
, 1, objv
, "DB NAME NARG ENC SWITCHES...");
1984 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1985 zFunc
= Tcl_GetString(objv
[2]);
1986 if( Tcl_GetIntFromObj(interp
, objv
[3], &nArg
) ) return TCL_ERROR
;
1987 if( Tcl_GetIndexFromObjStruct(interp
, objv
[4], aEnc
, sizeof(aEnc
[0]),
1988 "encoding", 0, &enc
)
1992 enc
= aEnc
[enc
].enc
;
1994 p
= sqlite3_malloc(sizeof(CreateFunctionV2
));
1996 memset(p
, 0, sizeof(CreateFunctionV2
));
1999 for(i
=5; i
<objc
; i
+=2){
2001 const char *azSwitch
[] = {"-func", "-step", "-final", "-destroy", 0};
2002 if( Tcl_GetIndexFromObj(interp
, objv
[i
], azSwitch
, "switch", 0, &iSwitch
) ){
2008 case 0: p
->pFunc
= objv
[i
+1]; break;
2009 case 1: p
->pStep
= objv
[i
+1]; break;
2010 case 2: p
->pFinal
= objv
[i
+1]; break;
2011 case 3: p
->pDestroy
= objv
[i
+1]; break;
2014 if( p
->pFunc
) p
->pFunc
= Tcl_DuplicateObj(p
->pFunc
);
2015 if( p
->pStep
) p
->pStep
= Tcl_DuplicateObj(p
->pStep
);
2016 if( p
->pFinal
) p
->pFinal
= Tcl_DuplicateObj(p
->pFinal
);
2017 if( p
->pDestroy
) p
->pDestroy
= Tcl_DuplicateObj(p
->pDestroy
);
2019 if( p
->pFunc
) Tcl_IncrRefCount(p
->pFunc
);
2020 if( p
->pStep
) Tcl_IncrRefCount(p
->pStep
);
2021 if( p
->pFinal
) Tcl_IncrRefCount(p
->pFinal
);
2022 if( p
->pDestroy
) Tcl_IncrRefCount(p
->pDestroy
);
2024 rc
= sqlite3_create_function_v2(db
, zFunc
, nArg
, enc
, (void *)p
,
2025 (p
->pFunc
? cf2Func
: 0),
2026 (p
->pStep
? cf2Step
: 0),
2027 (p
->pFinal
? cf2Final
: 0),
2030 if( rc
!=SQLITE_OK
){
2031 Tcl_ResetResult(interp
);
2032 Tcl_AppendResult(interp
, sqlite3ErrName(rc
), 0);
2039 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
2041 static int test_load_extension(
2042 ClientData clientData
, /* Not used */
2043 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
2044 int objc
, /* Number of arguments */
2045 Tcl_Obj
*CONST objv
[] /* Command arguments */
2047 Tcl_CmdInfo cmdInfo
;
2055 if( objc
!=4 && objc
!=3 ){
2056 Tcl_WrongNumArgs(interp
, 1, objv
, "DB-HANDLE FILE ?PROC?");
2059 zDb
= Tcl_GetString(objv
[1]);
2060 zFile
= Tcl_GetString(objv
[2]);
2062 zProc
= Tcl_GetString(objv
[3]);
2065 /* Extract the C database handle from the Tcl command name */
2066 if( !Tcl_GetCommandInfo(interp
, zDb
, &cmdInfo
) ){
2067 Tcl_AppendResult(interp
, "command not found: ", zDb
, (char*)0);
2070 db
= ((struct SqliteDb
*)cmdInfo
.objClientData
)->db
;
2073 /* Call the underlying C function. If an error occurs, set rc to
2074 ** TCL_ERROR and load any error string into the interpreter. If no
2075 ** error occurs, set rc to TCL_OK.
2077 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2079 zErr
= sqlite3_mprintf("this build omits sqlite3_load_extension()");
2081 rc
= sqlite3_load_extension(db
, zFile
, zProc
, &zErr
);
2083 if( rc
!=SQLITE_OK
){
2084 Tcl_SetResult(interp
, zErr
? zErr
: "", TCL_VOLATILE
);
2095 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
2097 static int test_enable_load(
2098 ClientData clientData
, /* Not used */
2099 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
2100 int objc
, /* Number of arguments */
2101 Tcl_Obj
*CONST objv
[] /* Command arguments */
2103 Tcl_CmdInfo cmdInfo
;
2109 Tcl_WrongNumArgs(interp
, 1, objv
, "DB-HANDLE ONOFF");
2112 zDb
= Tcl_GetString(objv
[1]);
2114 /* Extract the C database handle from the Tcl command name */
2115 if( !Tcl_GetCommandInfo(interp
, zDb
, &cmdInfo
) ){
2116 Tcl_AppendResult(interp
, "command not found: ", zDb
, (char*)0);
2119 db
= ((struct SqliteDb
*)cmdInfo
.objClientData
)->db
;
2122 /* Get the onoff parameter */
2123 if( Tcl_GetBooleanFromObj(interp
, objv
[2], &onoff
) ){
2127 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2128 Tcl_AppendResult(interp
, "this build omits sqlite3_load_extension()");
2131 sqlite3_enable_load_extension(db
, onoff
);
2137 ** Usage: sqlite_abort
2139 ** Shutdown the process immediately. This is not a clean shutdown.
2140 ** This command is used to test the recoverability of a database in
2141 ** the event of a program crash.
2143 static int sqlite_abort(
2145 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
2146 int argc
, /* Number of arguments */
2147 char **argv
/* Text of each argument */
2149 #if defined(_MSC_VER)
2150 /* We do this, otherwise the test will halt with a popup message
2151 * that we have to click away before the test will continue.
2153 _set_abort_behavior( 0, _CALL_REPORTFAULT
);
2156 assert( interp
==0 ); /* This will always fail */
2161 ** The following routine is a user-defined SQL function whose purpose
2162 ** is to test the sqlite_set_result() API.
2164 static void testFunc(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
){
2166 const char *zArg0
= (char*)sqlite3_value_text(argv
[0]);
2168 if( 0==sqlite3StrICmp(zArg0
, "int") ){
2169 sqlite3_result_int(context
, sqlite3_value_int(argv
[1]));
2170 }else if( sqlite3StrICmp(zArg0
,"int64")==0 ){
2171 sqlite3_result_int64(context
, sqlite3_value_int64(argv
[1]));
2172 }else if( sqlite3StrICmp(zArg0
,"string")==0 ){
2173 sqlite3_result_text(context
, (char*)sqlite3_value_text(argv
[1]), -1,
2175 }else if( sqlite3StrICmp(zArg0
,"double")==0 ){
2176 sqlite3_result_double(context
, sqlite3_value_double(argv
[1]));
2177 }else if( sqlite3StrICmp(zArg0
,"null")==0 ){
2178 sqlite3_result_null(context
);
2179 }else if( sqlite3StrICmp(zArg0
,"value")==0 ){
2180 sqlite3_result_value(context
, argv
[sqlite3_value_int(argv
[1])]);
2193 sqlite3_result_error(context
,"first argument should be one of: "
2194 "int int64 string double null value", -1);
2198 ** Usage: sqlite_register_test_function DB NAME
2200 ** Register the test SQL function on the database DB under the name NAME.
2202 static int test_register_func(
2204 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
2205 int argc
, /* Number of arguments */
2206 char **argv
/* Text of each argument */
2211 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
2212 " DB FUNCTION-NAME", 0);
2215 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
2216 rc
= sqlite3_create_function(db
, argv
[2], -1, SQLITE_UTF8
, 0,
2219 Tcl_AppendResult(interp
, sqlite3ErrStr(rc
), 0);
2222 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
2227 ** Usage: sqlite3_finalize STMT
2229 ** Finalize a statement handle.
2231 static int test_finalize(
2235 Tcl_Obj
*CONST objv
[]
2237 sqlite3_stmt
*pStmt
;
2242 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2243 Tcl_GetStringFromObj(objv
[0], 0), " <STMT>", 0);
2247 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2250 db
= StmtToDb(pStmt
);
2252 rc
= sqlite3_finalize(pStmt
);
2253 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
2254 if( db
&& sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
2259 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2261 ** Get the value of a status counter from a statement.
2263 static int test_stmt_status(
2267 Tcl_Obj
*CONST objv
[]
2270 int i
, op
, resetFlag
;
2271 const char *zOpName
;
2272 sqlite3_stmt
*pStmt
;
2274 static const struct {
2278 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP
},
2279 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT
},
2280 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX
},
2281 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP
},
2284 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT PARAMETER RESETFLAG");
2287 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2288 zOpName
= Tcl_GetString(objv
[2]);
2289 for(i
=0; i
<ArraySize(aOp
); i
++){
2290 if( strcmp(aOp
[i
].zName
, zOpName
)==0 ){
2295 if( i
>=ArraySize(aOp
) ){
2296 if( Tcl_GetIntFromObj(interp
, objv
[2], &op
) ) return TCL_ERROR
;
2298 if( Tcl_GetBooleanFromObj(interp
, objv
[3], &resetFlag
) ) return TCL_ERROR
;
2299 iValue
= sqlite3_stmt_status(pStmt
, op
, resetFlag
);
2300 Tcl_SetObjResult(interp
, Tcl_NewIntObj(iValue
));
2305 ** Usage: sqlite3_next_stmt DB STMT
2307 ** Return the next statment in sequence after STMT.
2309 static int test_next_stmt(
2313 Tcl_Obj
*CONST objv
[]
2315 sqlite3_stmt
*pStmt
;
2320 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2321 Tcl_GetStringFromObj(objv
[0], 0), " DB STMT", 0);
2325 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2326 if( getStmtPointer(interp
, Tcl_GetString(objv
[2]), &pStmt
) ) return TCL_ERROR
;
2327 pStmt
= sqlite3_next_stmt(db
, pStmt
);
2329 if( sqlite3TestMakePointerStr(interp
, zBuf
, pStmt
) ) return TCL_ERROR
;
2330 Tcl_AppendResult(interp
, zBuf
, 0);
2336 ** Usage: sqlite3_stmt_readonly STMT
2338 ** Return true if STMT is a NULL pointer or a pointer to a statement
2339 ** that is guaranteed to leave the database unmodified.
2341 static int test_stmt_readonly(
2345 Tcl_Obj
*CONST objv
[]
2347 sqlite3_stmt
*pStmt
;
2351 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2352 Tcl_GetStringFromObj(objv
[0], 0), " STMT", 0);
2356 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2357 rc
= sqlite3_stmt_readonly(pStmt
);
2358 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(rc
));
2363 ** Usage: sqlite3_stmt_busy STMT
2365 ** Return true if STMT is a non-NULL pointer to a statement
2366 ** that has been stepped but not to completion.
2368 static int test_stmt_busy(
2372 Tcl_Obj
*CONST objv
[]
2374 sqlite3_stmt
*pStmt
;
2378 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2379 Tcl_GetStringFromObj(objv
[0], 0), " STMT", 0);
2383 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2384 rc
= sqlite3_stmt_busy(pStmt
);
2385 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(rc
));
2390 ** Usage: uses_stmt_journal STMT
2392 ** Return true if STMT uses a statement journal.
2394 static int uses_stmt_journal(
2398 Tcl_Obj
*CONST objv
[]
2400 sqlite3_stmt
*pStmt
;
2403 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2404 Tcl_GetStringFromObj(objv
[0], 0), " STMT", 0);
2408 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2409 sqlite3_stmt_readonly(pStmt
);
2410 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(((Vdbe
*)pStmt
)->usesStmtJournal
));
2416 ** Usage: sqlite3_reset STMT
2418 ** Reset a statement handle.
2420 static int test_reset(
2424 Tcl_Obj
*CONST objv
[]
2426 sqlite3_stmt
*pStmt
;
2430 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2431 Tcl_GetStringFromObj(objv
[0], 0), " <STMT>", 0);
2435 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2437 rc
= sqlite3_reset(pStmt
);
2438 if( pStmt
&& sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ){
2441 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
2451 ** Usage: sqlite3_expired STMT
2453 ** Return TRUE if a recompilation of the statement is recommended.
2455 static int test_expired(
2459 Tcl_Obj
*CONST objv
[]
2461 #ifndef SQLITE_OMIT_DEPRECATED
2462 sqlite3_stmt
*pStmt
;
2464 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2465 Tcl_GetStringFromObj(objv
[0], 0), " <STMT>", 0);
2468 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
2469 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(sqlite3_expired(pStmt
)));
2475 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
2477 ** Transfer all bindings from FROMSTMT over to TOSTMT
2479 static int test_transfer_bind(
2483 Tcl_Obj
*CONST objv
[]
2485 #ifndef SQLITE_OMIT_DEPRECATED
2486 sqlite3_stmt
*pStmt1
, *pStmt2
;
2488 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2489 Tcl_GetStringFromObj(objv
[0], 0), " FROM-STMT TO-STMT", 0);
2492 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt1
)) return TCL_ERROR
;
2493 if( getStmtPointer(interp
, Tcl_GetString(objv
[2]), &pStmt2
)) return TCL_ERROR
;
2494 Tcl_SetObjResult(interp
,
2495 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1
,pStmt2
)));
2501 ** Usage: sqlite3_changes DB
2503 ** Return the number of changes made to the database by the last SQL
2506 static int test_changes(
2510 Tcl_Obj
*CONST objv
[]
2514 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2515 Tcl_GetString(objv
[0]), " DB", 0);
2518 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2519 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_changes(db
)));
2524 ** This is the "static_bind_value" that variables are bound to when
2525 ** the FLAG option of sqlite3_bind is "static"
2527 static char *sqlite_static_bind_value
= 0;
2528 static int sqlite_static_bind_nbyte
= 0;
2531 ** Usage: sqlite3_bind VM IDX VALUE FLAGS
2533 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
2534 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
2535 ** ignored and the value is set to NULL. If FLAGS=="static" then
2536 ** the value is set to the value of a static variable named
2537 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
2538 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
2539 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
2541 static int test_bind(
2543 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
2544 int argc
, /* Number of arguments */
2545 char **argv
/* Text of each argument */
2547 sqlite3_stmt
*pStmt
;
2551 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
2552 " VM IDX VALUE (null|static|normal)\"", 0);
2555 if( getStmtPointer(interp
, argv
[1], &pStmt
) ) return TCL_ERROR
;
2556 if( Tcl_GetInt(interp
, argv
[2], &idx
) ) return TCL_ERROR
;
2557 if( strcmp(argv
[4],"null")==0 ){
2558 rc
= sqlite3_bind_null(pStmt
, idx
);
2559 }else if( strcmp(argv
[4],"static")==0 ){
2560 rc
= sqlite3_bind_text(pStmt
, idx
, sqlite_static_bind_value
, -1, 0);
2561 }else if( strcmp(argv
[4],"static-nbytes")==0 ){
2562 rc
= sqlite3_bind_text(pStmt
, idx
, sqlite_static_bind_value
,
2563 sqlite_static_bind_nbyte
, 0);
2564 }else if( strcmp(argv
[4],"normal")==0 ){
2565 rc
= sqlite3_bind_text(pStmt
, idx
, argv
[3], -1, SQLITE_TRANSIENT
);
2566 }else if( strcmp(argv
[4],"blob10")==0 ){
2567 rc
= sqlite3_bind_text(pStmt
, idx
, "abc\000xyz\000pq", 10, SQLITE_STATIC
);
2569 Tcl_AppendResult(interp
, "4th argument should be "
2570 "\"null\" or \"static\" or \"normal\"", 0);
2573 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
2576 sprintf(zBuf
, "(%d) ", rc
);
2577 Tcl_AppendResult(interp
, zBuf
, sqlite3ErrStr(rc
), 0);
2583 #ifndef SQLITE_OMIT_UTF16
2585 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2587 ** This function is used to test that SQLite selects the correct collation
2588 ** sequence callback when multiple versions (for different text encodings)
2591 ** Calling this routine registers the collation sequence "test_collate"
2592 ** with database handle <db>. The second argument must be a list of three
2593 ** boolean values. If the first is true, then a version of test_collate is
2594 ** registered for UTF-8, if the second is true, a version is registered for
2595 ** UTF-16le, if the third is true, a UTF-16be version is available.
2596 ** Previous versions of test_collate are deleted.
2598 ** The collation sequence test_collate is implemented by calling the
2599 ** following TCL script:
2601 ** "test_collate <enc> <lhs> <rhs>"
2603 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2604 ** The <enc> parameter is the encoding of the collation function that
2605 ** SQLite selected to call. The TCL test script implements the
2606 ** "test_collate" proc.
2608 ** Note that this will only work with one interpreter at a time, as the
2609 ** interp pointer to use when evaluating the TCL script is stored in
2610 ** pTestCollateInterp.
2612 static Tcl_Interp
* pTestCollateInterp
;
2613 static int test_collate_func(
2615 int nA
, const void *zA
,
2616 int nB
, const void *zB
2618 Tcl_Interp
*i
= pTestCollateInterp
;
2619 int encin
= SQLITE_PTR_TO_INT(pCtx
);
2623 sqlite3_value
*pVal
;
2626 pX
= Tcl_NewStringObj("test_collate", -1);
2627 Tcl_IncrRefCount(pX
);
2631 Tcl_ListObjAppendElement(i
,pX
,Tcl_NewStringObj("UTF-8",-1));
2633 case SQLITE_UTF16LE
:
2634 Tcl_ListObjAppendElement(i
,pX
,Tcl_NewStringObj("UTF-16LE",-1));
2636 case SQLITE_UTF16BE
:
2637 Tcl_ListObjAppendElement(i
,pX
,Tcl_NewStringObj("UTF-16BE",-1));
2643 sqlite3BeginBenignMalloc();
2644 pVal
= sqlite3ValueNew(0);
2646 sqlite3ValueSetStr(pVal
, nA
, zA
, encin
, SQLITE_STATIC
);
2647 n
= sqlite3_value_bytes(pVal
);
2648 Tcl_ListObjAppendElement(i
,pX
,
2649 Tcl_NewStringObj((char*)sqlite3_value_text(pVal
),n
));
2650 sqlite3ValueSetStr(pVal
, nB
, zB
, encin
, SQLITE_STATIC
);
2651 n
= sqlite3_value_bytes(pVal
);
2652 Tcl_ListObjAppendElement(i
,pX
,
2653 Tcl_NewStringObj((char*)sqlite3_value_text(pVal
),n
));
2654 sqlite3ValueFree(pVal
);
2656 sqlite3EndBenignMalloc();
2658 Tcl_EvalObjEx(i
, pX
, 0);
2659 Tcl_DecrRefCount(pX
);
2660 Tcl_GetIntFromObj(i
, Tcl_GetObjResult(i
), &res
);
2663 static int test_collate(
2667 Tcl_Obj
*CONST objv
[]
2671 sqlite3_value
*pVal
;
2674 if( objc
!=5 ) goto bad_args
;
2675 pTestCollateInterp
= interp
;
2676 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2678 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[2], &val
) ) return TCL_ERROR
;
2679 rc
= sqlite3_create_collation(db
, "test_collate", SQLITE_UTF8
,
2680 (void *)SQLITE_UTF8
, val
?test_collate_func
:0);
2681 if( rc
==SQLITE_OK
){
2683 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[3], &val
) ) return TCL_ERROR
;
2684 rc
= sqlite3_create_collation(db
, "test_collate", SQLITE_UTF16LE
,
2685 (void *)SQLITE_UTF16LE
, val
?test_collate_func
:0);
2686 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[4], &val
) ) return TCL_ERROR
;
2689 if( sqlite3_iMallocFail
>0 ){
2690 sqlite3_iMallocFail
++;
2693 sqlite3_mutex_enter(db
->mutex
);
2694 pVal
= sqlite3ValueNew(db
);
2695 sqlite3ValueSetStr(pVal
, -1, "test_collate", SQLITE_UTF8
, SQLITE_STATIC
);
2696 zUtf16
= sqlite3ValueText(pVal
, SQLITE_UTF16NATIVE
);
2697 if( db
->mallocFailed
){
2700 rc
= sqlite3_create_collation16(db
, zUtf16
, SQLITE_UTF16BE
,
2701 (void *)SQLITE_UTF16BE
, val
?test_collate_func
:0);
2703 sqlite3ValueFree(pVal
);
2704 sqlite3_mutex_leave(db
->mutex
);
2706 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
2708 if( rc
!=SQLITE_OK
){
2709 Tcl_AppendResult(interp
, sqlite3ErrName(rc
), 0);
2715 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2716 Tcl_GetStringFromObj(objv
[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2721 ** Usage: add_test_utf16bin_collate <db ptr>
2723 ** Add a utf-16 collation sequence named "utf16bin" to the database
2724 ** handle. This collation sequence compares arguments in the same way as the
2725 ** built-in collation "binary".
2727 static int test_utf16bin_collate_func(
2729 int nA
, const void *zA
,
2730 int nB
, const void *zB
2732 int nCmp
= (nA
>nB
? nB
: nA
);
2733 int res
= memcmp(zA
, zB
, nCmp
);
2734 if( res
==0 ) res
= nA
- nB
;
2737 static int test_utf16bin_collate(
2741 Tcl_Obj
*CONST objv
[]
2746 if( objc
!=2 ) goto bad_args
;
2747 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2749 rc
= sqlite3_create_collation(db
, "utf16bin", SQLITE_UTF16
, 0,
2750 test_utf16bin_collate_func
2752 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
2756 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
2761 ** When the collation needed callback is invoked, record the name of
2762 ** the requested collating function here. The recorded name is linked
2763 ** to a TCL variable and used to make sure that the requested collation
2766 static char zNeededCollation
[200];
2767 static char *pzNeededCollation
= zNeededCollation
;
2771 ** Called when a collating sequence is needed. Registered using
2772 ** sqlite3_collation_needed16().
2774 static void test_collate_needed_cb(
2783 for(z
= (char*)pName
, i
=0; *z
|| z
[1]; z
++){
2784 if( *z
) zNeededCollation
[i
++] = *z
;
2786 zNeededCollation
[i
] = 0;
2787 sqlite3_create_collation(
2788 db
, "test_collate", ENC(db
), SQLITE_INT_TO_PTR(enc
), test_collate_func
);
2792 ** Usage: add_test_collate_needed DB
2794 static int test_collate_needed(
2798 Tcl_Obj
*CONST objv
[]
2803 if( objc
!=2 ) goto bad_args
;
2804 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2805 rc
= sqlite3_collation_needed16(db
, 0, test_collate_needed_cb
);
2806 zNeededCollation
[0] = 0;
2807 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
2811 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
2816 ** tclcmd: add_alignment_test_collations DB
2818 ** Add two new collating sequences to the database DB
2823 ** Both collating sequences use the same sort order as BINARY.
2824 ** The only difference is that the utf16_aligned collating
2825 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2826 ** Both collating functions increment the unaligned utf16 counter
2827 ** whenever they see a string that begins on an odd byte boundary.
2829 static int unaligned_string_counter
= 0;
2830 static int alignmentCollFunc(
2832 int nKey1
, const void *pKey1
,
2833 int nKey2
, const void *pKey2
2836 n
= nKey1
<nKey2
? nKey1
: nKey2
;
2837 if( nKey1
>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1
))) ) unaligned_string_counter
++;
2838 if( nKey2
>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2
))) ) unaligned_string_counter
++;
2839 rc
= memcmp(pKey1
, pKey2
, n
);
2845 static int add_alignment_test_collations(
2849 Tcl_Obj
*CONST objv
[]
2853 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2854 sqlite3_create_collation(db
, "utf16_unaligned", SQLITE_UTF16
,
2855 0, alignmentCollFunc
);
2856 sqlite3_create_collation(db
, "utf16_aligned", SQLITE_UTF16_ALIGNED
,
2857 0, alignmentCollFunc
);
2861 #endif /* !defined(SQLITE_OMIT_UTF16) */
2864 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2866 ** This function is used to test that SQLite selects the correct user
2867 ** function callback when multiple versions (for different text encodings)
2870 ** Calling this routine registers up to three versions of the user function
2871 ** "test_function" with database handle <db>. If the second argument is
2872 ** true, then a version of test_function is registered for UTF-8, if the
2873 ** third is true, a version is registered for UTF-16le, if the fourth is
2874 ** true, a UTF-16be version is available. Previous versions of
2875 ** test_function are deleted.
2877 ** The user function is implemented by calling the following TCL script:
2879 ** "test_function <enc> <arg>"
2881 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2882 ** single argument passed to the SQL function. The value returned by
2883 ** the TCL script is used as the return value of the SQL function. It
2884 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2885 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2886 ** prefers UTF-16BE.
2888 #ifndef SQLITE_OMIT_UTF16
2889 static void test_function_utf8(
2890 sqlite3_context
*pCtx
,
2892 sqlite3_value
**argv
2896 sqlite3_value
*pVal
;
2897 interp
= (Tcl_Interp
*)sqlite3_user_data(pCtx
);
2898 pX
= Tcl_NewStringObj("test_function", -1);
2899 Tcl_IncrRefCount(pX
);
2900 Tcl_ListObjAppendElement(interp
, pX
, Tcl_NewStringObj("UTF-8", -1));
2901 Tcl_ListObjAppendElement(interp
, pX
,
2902 Tcl_NewStringObj((char*)sqlite3_value_text(argv
[0]), -1));
2903 Tcl_EvalObjEx(interp
, pX
, 0);
2904 Tcl_DecrRefCount(pX
);
2905 sqlite3_result_text(pCtx
, Tcl_GetStringResult(interp
), -1, SQLITE_TRANSIENT
);
2906 pVal
= sqlite3ValueNew(0);
2907 sqlite3ValueSetStr(pVal
, -1, Tcl_GetStringResult(interp
),
2908 SQLITE_UTF8
, SQLITE_STATIC
);
2909 sqlite3_result_text16be(pCtx
, sqlite3_value_text16be(pVal
),
2910 -1, SQLITE_TRANSIENT
);
2911 sqlite3ValueFree(pVal
);
2913 static void test_function_utf16le(
2914 sqlite3_context
*pCtx
,
2916 sqlite3_value
**argv
2920 sqlite3_value
*pVal
;
2921 interp
= (Tcl_Interp
*)sqlite3_user_data(pCtx
);
2922 pX
= Tcl_NewStringObj("test_function", -1);
2923 Tcl_IncrRefCount(pX
);
2924 Tcl_ListObjAppendElement(interp
, pX
, Tcl_NewStringObj("UTF-16LE", -1));
2925 Tcl_ListObjAppendElement(interp
, pX
,
2926 Tcl_NewStringObj((char*)sqlite3_value_text(argv
[0]), -1));
2927 Tcl_EvalObjEx(interp
, pX
, 0);
2928 Tcl_DecrRefCount(pX
);
2929 pVal
= sqlite3ValueNew(0);
2930 sqlite3ValueSetStr(pVal
, -1, Tcl_GetStringResult(interp
),
2931 SQLITE_UTF8
, SQLITE_STATIC
);
2932 sqlite3_result_text(pCtx
,(char*)sqlite3_value_text(pVal
),-1,SQLITE_TRANSIENT
);
2933 sqlite3ValueFree(pVal
);
2935 static void test_function_utf16be(
2936 sqlite3_context
*pCtx
,
2938 sqlite3_value
**argv
2942 sqlite3_value
*pVal
;
2943 interp
= (Tcl_Interp
*)sqlite3_user_data(pCtx
);
2944 pX
= Tcl_NewStringObj("test_function", -1);
2945 Tcl_IncrRefCount(pX
);
2946 Tcl_ListObjAppendElement(interp
, pX
, Tcl_NewStringObj("UTF-16BE", -1));
2947 Tcl_ListObjAppendElement(interp
, pX
,
2948 Tcl_NewStringObj((char*)sqlite3_value_text(argv
[0]), -1));
2949 Tcl_EvalObjEx(interp
, pX
, 0);
2950 Tcl_DecrRefCount(pX
);
2951 pVal
= sqlite3ValueNew(0);
2952 sqlite3ValueSetStr(pVal
, -1, Tcl_GetStringResult(interp
),
2953 SQLITE_UTF8
, SQLITE_STATIC
);
2954 sqlite3_result_text16(pCtx
, sqlite3_value_text16le(pVal
),
2955 -1, SQLITE_TRANSIENT
);
2956 sqlite3_result_text16be(pCtx
, sqlite3_value_text16le(pVal
),
2957 -1, SQLITE_TRANSIENT
);
2958 sqlite3_result_text16le(pCtx
, sqlite3_value_text16le(pVal
),
2959 -1, SQLITE_TRANSIENT
);
2960 sqlite3ValueFree(pVal
);
2962 #endif /* SQLITE_OMIT_UTF16 */
2963 static int test_function(
2967 Tcl_Obj
*CONST objv
[]
2969 #ifndef SQLITE_OMIT_UTF16
2973 if( objc
!=5 ) goto bad_args
;
2974 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
2976 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[2], &val
) ) return TCL_ERROR
;
2978 sqlite3_create_function(db
, "test_function", 1, SQLITE_UTF8
,
2979 interp
, test_function_utf8
, 0, 0);
2981 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[3], &val
) ) return TCL_ERROR
;
2983 sqlite3_create_function(db
, "test_function", 1, SQLITE_UTF16LE
,
2984 interp
, test_function_utf16le
, 0, 0);
2986 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[4], &val
) ) return TCL_ERROR
;
2988 sqlite3_create_function(db
, "test_function", 1, SQLITE_UTF16BE
,
2989 interp
, test_function_utf16be
, 0, 0);
2994 Tcl_AppendResult(interp
, "wrong # args: should be \"",
2995 Tcl_GetStringFromObj(objv
[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2996 #endif /* SQLITE_OMIT_UTF16 */
3001 ** Usage: sqlite3_test_errstr <err code>
3003 ** Test that the english language string equivalents for sqlite error codes
3004 ** are sane. The parameter is an integer representing an sqlite error code.
3005 ** The result is a list of two elements, the string representation of the
3006 ** error code and the english language explanation.
3008 static int test_errstr(
3012 Tcl_Obj
*CONST objv
[]
3017 Tcl_WrongNumArgs(interp
, 1, objv
, "<error code>");
3020 zCode
= Tcl_GetString(objv
[1]);
3021 for(i
=0; i
<200; i
++){
3022 if( 0==strcmp(t1ErrorName(i
), zCode
) ) break;
3024 Tcl_SetResult(interp
, (char *)sqlite3ErrStr(i
), 0);
3029 ** Usage: breakpoint
3031 ** This routine exists for one purpose - to provide a place to put a
3032 ** breakpoint with GDB that can be triggered using TCL code. The use
3033 ** for this is when a particular test fails on (say) the 1485th iteration.
3034 ** In the TCL test script, we can add code like this:
3036 ** if {$i==1485} breakpoint
3038 ** Then run testfixture in the debugger and wait for the breakpoint to
3039 ** fire. Then additional breakpoints can be set to trace down the bug.
3041 static int test_breakpoint(
3043 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
3044 int argc
, /* Number of arguments */
3045 char **argv
/* Text of each argument */
3047 return TCL_OK
; /* Do nothing */
3051 ** Usage: sqlite3_bind_zeroblob STMT IDX N
3053 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
3054 ** IDX is the index of a wildcard in the prepared statement. This command
3055 ** binds a N-byte zero-filled BLOB to the wildcard.
3057 static int test_bind_zeroblob(
3061 Tcl_Obj
*CONST objv
[]
3063 sqlite3_stmt
*pStmt
;
3069 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT IDX N");
3073 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3074 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3075 if( Tcl_GetIntFromObj(interp
, objv
[3], &n
) ) return TCL_ERROR
;
3077 rc
= sqlite3_bind_zeroblob(pStmt
, idx
, n
);
3078 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3079 if( rc
!=SQLITE_OK
){
3087 ** Usage: sqlite3_bind_int STMT N VALUE
3089 ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
3090 ** N is the index of a wildcard in the prepared statement. This command
3091 ** binds a 32-bit integer VALUE to that wildcard.
3093 static int test_bind_int(
3097 Tcl_Obj
*CONST objv
[]
3099 sqlite3_stmt
*pStmt
;
3105 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3106 Tcl_GetStringFromObj(objv
[0], 0), " STMT N VALUE", 0);
3110 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3111 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3112 if( Tcl_GetIntFromObj(interp
, objv
[3], &value
) ) return TCL_ERROR
;
3114 rc
= sqlite3_bind_int(pStmt
, idx
, value
);
3115 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3116 if( rc
!=SQLITE_OK
){
3125 ** Usage: sqlite3_bind_int64 STMT N VALUE
3127 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
3128 ** N is the index of a wildcard in the prepared statement. This command
3129 ** binds a 64-bit integer VALUE to that wildcard.
3131 static int test_bind_int64(
3135 Tcl_Obj
*CONST objv
[]
3137 sqlite3_stmt
*pStmt
;
3143 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3144 Tcl_GetStringFromObj(objv
[0], 0), " STMT N VALUE", 0);
3148 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3149 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3150 if( Tcl_GetWideIntFromObj(interp
, objv
[3], &value
) ) return TCL_ERROR
;
3152 rc
= sqlite3_bind_int64(pStmt
, idx
, value
);
3153 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3154 if( rc
!=SQLITE_OK
){
3163 ** Usage: sqlite3_bind_double STMT N VALUE
3165 ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
3166 ** N is the index of a wildcard in the prepared statement. This command
3167 ** binds a 64-bit integer VALUE to that wildcard.
3169 static int test_bind_double(
3173 Tcl_Obj
*CONST objv
[]
3175 sqlite3_stmt
*pStmt
;
3181 static const struct {
3182 const char *zName
; /* Name of the special floating point value */
3183 unsigned int iUpper
; /* Upper 32 bits */
3184 unsigned int iLower
; /* Lower 32 bits */
3186 { "NaN", 0x7fffffff, 0xffffffff },
3187 { "SNaN", 0x7ff7ffff, 0xffffffff },
3188 { "-NaN", 0xffffffff, 0xffffffff },
3189 { "-SNaN", 0xfff7ffff, 0xffffffff },
3190 { "+Inf", 0x7ff00000, 0x00000000 },
3191 { "-Inf", 0xfff00000, 0x00000000 },
3192 { "Epsilon", 0x00000000, 0x00000001 },
3193 { "-Epsilon", 0x80000000, 0x00000001 },
3194 { "NaN0", 0x7ff80000, 0x00000000 },
3195 { "-NaN0", 0xfff80000, 0x00000000 },
3199 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3200 Tcl_GetStringFromObj(objv
[0], 0), " STMT N VALUE", 0);
3204 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3205 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3207 /* Intercept the string "NaN" and generate a NaN value for it.
3208 ** All other strings are passed through to Tcl_GetDoubleFromObj().
3209 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
3212 zVal
= Tcl_GetString(objv
[3]);
3213 for(i
=0; i
<sizeof(aSpecialFp
)/sizeof(aSpecialFp
[0]); i
++){
3214 if( strcmp(aSpecialFp
[i
].zName
, zVal
)==0 ){
3216 x
= aSpecialFp
[i
].iUpper
;
3218 x
|= aSpecialFp
[i
].iLower
;
3219 assert( sizeof(value
)==8 );
3220 assert( sizeof(x
)==8 );
3221 memcpy(&value
, &x
, 8);
3225 if( i
>=sizeof(aSpecialFp
)/sizeof(aSpecialFp
[0]) &&
3226 Tcl_GetDoubleFromObj(interp
, objv
[3], &value
) ){
3229 rc
= sqlite3_bind_double(pStmt
, idx
, value
);
3230 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3231 if( rc
!=SQLITE_OK
){
3239 ** Usage: sqlite3_bind_null STMT N
3241 ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
3242 ** N is the index of a wildcard in the prepared statement. This command
3243 ** binds a NULL to the wildcard.
3245 static int test_bind_null(
3249 Tcl_Obj
*CONST objv
[]
3251 sqlite3_stmt
*pStmt
;
3256 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3257 Tcl_GetStringFromObj(objv
[0], 0), " STMT N", 0);
3261 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3262 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3264 rc
= sqlite3_bind_null(pStmt
, idx
);
3265 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3266 if( rc
!=SQLITE_OK
){
3274 ** Usage: sqlite3_bind_text STMT N STRING BYTES
3276 ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
3277 ** N is the index of a wildcard in the prepared statement. This command
3278 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
3281 static int test_bind_text(
3285 Tcl_Obj
*CONST objv
[]
3287 sqlite3_stmt
*pStmt
;
3294 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3295 Tcl_GetStringFromObj(objv
[0], 0), " STMT N VALUE BYTES", 0);
3299 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3300 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3301 value
= (char*)Tcl_GetByteArrayFromObj(objv
[3], &bytes
);
3302 if( Tcl_GetIntFromObj(interp
, objv
[4], &bytes
) ) return TCL_ERROR
;
3304 rc
= sqlite3_bind_text(pStmt
, idx
, value
, bytes
, SQLITE_TRANSIENT
);
3305 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3306 if( rc
!=SQLITE_OK
){
3307 Tcl_AppendResult(interp
, sqlite3ErrName(rc
), 0);
3315 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
3317 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
3318 ** N is the index of a wildcard in the prepared statement. This command
3319 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
3322 static int test_bind_text16(
3326 Tcl_Obj
*CONST objv
[]
3328 #ifndef SQLITE_OMIT_UTF16
3329 sqlite3_stmt
*pStmt
;
3335 void (*xDel
)(void*) = (objc
==6?SQLITE_STATIC
:SQLITE_TRANSIENT
);
3336 Tcl_Obj
*oStmt
= objv
[objc
-4];
3337 Tcl_Obj
*oN
= objv
[objc
-3];
3338 Tcl_Obj
*oString
= objv
[objc
-2];
3339 Tcl_Obj
*oBytes
= objv
[objc
-1];
3341 if( objc
!=5 && objc
!=6){
3342 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3343 Tcl_GetStringFromObj(objv
[0], 0), " STMT N VALUE BYTES", 0);
3347 if( getStmtPointer(interp
, Tcl_GetString(oStmt
), &pStmt
) ) return TCL_ERROR
;
3348 if( Tcl_GetIntFromObj(interp
, oN
, &idx
) ) return TCL_ERROR
;
3349 value
= (char*)Tcl_GetByteArrayFromObj(oString
, 0);
3350 if( Tcl_GetIntFromObj(interp
, oBytes
, &bytes
) ) return TCL_ERROR
;
3352 rc
= sqlite3_bind_text16(pStmt
, idx
, (void *)value
, bytes
, xDel
);
3353 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3354 if( rc
!=SQLITE_OK
){
3355 Tcl_AppendResult(interp
, sqlite3ErrName(rc
), 0);
3359 #endif /* SQLITE_OMIT_UTF16 */
3364 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
3366 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
3367 ** N is the index of a wildcard in the prepared statement. This command
3368 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
3370 static int test_bind_blob(
3374 Tcl_Obj
*CONST objv
[]
3376 sqlite3_stmt
*pStmt
;
3381 sqlite3_destructor_type xDestructor
= SQLITE_TRANSIENT
;
3383 if( objc
!=5 && objc
!=6 ){
3384 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3385 Tcl_GetStringFromObj(objv
[0], 0), " STMT N DATA BYTES", 0);
3390 xDestructor
= SQLITE_STATIC
;
3394 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3395 if( Tcl_GetIntFromObj(interp
, objv
[2], &idx
) ) return TCL_ERROR
;
3396 value
= Tcl_GetString(objv
[3]);
3397 if( Tcl_GetIntFromObj(interp
, objv
[4], &bytes
) ) return TCL_ERROR
;
3399 rc
= sqlite3_bind_blob(pStmt
, idx
, value
, bytes
, xDestructor
);
3400 if( sqlite3TestErrCode(interp
, StmtToDb(pStmt
), rc
) ) return TCL_ERROR
;
3401 if( rc
!=SQLITE_OK
){
3409 ** Usage: sqlite3_bind_parameter_count STMT
3411 ** Return the number of wildcards in the given statement.
3413 static int test_bind_parameter_count(
3417 Tcl_Obj
*CONST objv
[]
3419 sqlite3_stmt
*pStmt
;
3422 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT");
3425 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3426 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt
)));
3431 ** Usage: sqlite3_bind_parameter_name STMT N
3433 ** Return the name of the Nth wildcard. The first wildcard is 1.
3434 ** An empty string is returned if N is out of range or if the wildcard
3437 static int test_bind_parameter_name(
3441 Tcl_Obj
*CONST objv
[]
3443 sqlite3_stmt
*pStmt
;
3447 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT N");
3450 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3451 if( Tcl_GetIntFromObj(interp
, objv
[2], &i
) ) return TCL_ERROR
;
3452 Tcl_SetObjResult(interp
,
3453 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt
,i
),-1)
3459 ** Usage: sqlite3_bind_parameter_index STMT NAME
3461 ** Return the index of the wildcard called NAME. Return 0 if there is
3462 ** no such wildcard.
3464 static int test_bind_parameter_index(
3468 Tcl_Obj
*CONST objv
[]
3470 sqlite3_stmt
*pStmt
;
3473 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT NAME");
3476 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3477 Tcl_SetObjResult(interp
,
3479 sqlite3_bind_parameter_index(pStmt
,Tcl_GetString(objv
[2]))
3486 ** Usage: sqlite3_clear_bindings STMT
3489 static int test_clear_bindings(
3493 Tcl_Obj
*CONST objv
[]
3495 sqlite3_stmt
*pStmt
;
3498 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT");
3501 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
3502 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt
)));
3507 ** Usage: sqlite3_sleep MILLISECONDS
3509 static int test_sleep(
3513 Tcl_Obj
*CONST objv
[]
3518 Tcl_WrongNumArgs(interp
, 1, objv
, "MILLISECONDS");
3521 if( Tcl_GetIntFromObj(interp
, objv
[1], &ms
) ){
3524 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_sleep(ms
)));
3529 ** Usage: sqlite3_extended_errcode DB
3531 ** Return the string representation of the most recent sqlite3_* API
3532 ** error code. e.g. "SQLITE_ERROR".
3534 static int test_ex_errcode(
3538 Tcl_Obj
*CONST objv
[]
3544 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3545 Tcl_GetString(objv
[0]), " DB", 0);
3548 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3549 rc
= sqlite3_extended_errcode(db
);
3550 Tcl_AppendResult(interp
, (char *)t1ErrorName(rc
), 0);
3556 ** Usage: sqlite3_errcode DB
3558 ** Return the string representation of the most recent sqlite3_* API
3559 ** error code. e.g. "SQLITE_ERROR".
3561 static int test_errcode(
3565 Tcl_Obj
*CONST objv
[]
3571 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3572 Tcl_GetString(objv
[0]), " DB", 0);
3575 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3576 rc
= sqlite3_errcode(db
);
3577 Tcl_AppendResult(interp
, (char *)t1ErrorName(rc
), 0);
3582 ** Usage: sqlite3_errmsg DB
3584 ** Returns the UTF-8 representation of the error message string for the
3585 ** most recent sqlite3_* API call.
3587 static int test_errmsg(
3591 Tcl_Obj
*CONST objv
[]
3597 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3598 Tcl_GetString(objv
[0]), " DB", 0);
3601 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3603 zErr
= sqlite3_errmsg(db
);
3604 Tcl_SetObjResult(interp
, Tcl_NewStringObj(zErr
, -1));
3609 ** Usage: test_errmsg16 DB
3611 ** Returns the UTF-16 representation of the error message string for the
3612 ** most recent sqlite3_* API call. This is a byte array object at the TCL
3613 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3616 static int test_errmsg16(
3620 Tcl_Obj
*CONST objv
[]
3622 #ifndef SQLITE_OMIT_UTF16
3629 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3630 Tcl_GetString(objv
[0]), " DB", 0);
3633 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3635 zErr
= sqlite3_errmsg16(db
);
3638 for(bytes
=0; z
[bytes
] || z
[bytes
+1]; bytes
+=2){}
3640 Tcl_SetObjResult(interp
, Tcl_NewByteArrayObj(zErr
, bytes
));
3641 #endif /* SQLITE_OMIT_UTF16 */
3646 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
3648 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3649 ** database handle <DB>. The parameter <tailval> is the name of a global
3650 ** variable that is set to the unused portion of <sql> (if any). A
3651 ** STMT handle is returned.
3653 static int test_prepare(
3657 Tcl_Obj
*CONST objv
[]
3662 const char *zTail
= 0;
3663 sqlite3_stmt
*pStmt
= 0;
3667 if( objc
!=5 && objc
!=4 ){
3668 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3669 Tcl_GetString(objv
[0]), " DB sql bytes ?tailvar?", 0);
3672 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3673 zSql
= Tcl_GetString(objv
[2]);
3674 if( Tcl_GetIntFromObj(interp
, objv
[3], &bytes
) ) return TCL_ERROR
;
3676 rc
= sqlite3_prepare(db
, zSql
, bytes
, &pStmt
, objc
>=5 ? &zTail
: 0);
3677 Tcl_ResetResult(interp
);
3678 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
3679 if( zTail
&& objc
>=5 ){
3681 bytes
= bytes
- (int)(zTail
-zSql
);
3683 if( (int)strlen(zTail
)<bytes
){
3684 bytes
= (int)strlen(zTail
);
3686 Tcl_ObjSetVar2(interp
, objv
[4], 0, Tcl_NewStringObj(zTail
, bytes
), 0);
3688 if( rc
!=SQLITE_OK
){
3690 sprintf(zBuf
, "(%d) ", rc
);
3691 Tcl_AppendResult(interp
, zBuf
, sqlite3_errmsg(db
), 0);
3696 if( sqlite3TestMakePointerStr(interp
, zBuf
, pStmt
) ) return TCL_ERROR
;
3697 Tcl_AppendResult(interp
, zBuf
, 0);
3703 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
3705 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3706 ** database handle <DB>. The parameter <tailval> is the name of a global
3707 ** variable that is set to the unused portion of <sql> (if any). A
3708 ** STMT handle is returned.
3710 static int test_prepare_v2(
3714 Tcl_Obj
*CONST objv
[]
3719 const char *zTail
= 0;
3720 sqlite3_stmt
*pStmt
= 0;
3724 if( objc
!=5 && objc
!=4 ){
3725 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3726 Tcl_GetString(objv
[0]), " DB sql bytes tailvar", 0);
3729 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3730 zSql
= Tcl_GetString(objv
[2]);
3731 if( Tcl_GetIntFromObj(interp
, objv
[3], &bytes
) ) return TCL_ERROR
;
3733 rc
= sqlite3_prepare_v2(db
, zSql
, bytes
, &pStmt
, objc
>=5 ? &zTail
: 0);
3734 assert(rc
==SQLITE_OK
|| pStmt
==0);
3735 Tcl_ResetResult(interp
);
3736 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
3737 if( zTail
&& objc
>=5 ){
3739 bytes
= bytes
- (int)(zTail
-zSql
);
3741 Tcl_ObjSetVar2(interp
, objv
[4], 0, Tcl_NewStringObj(zTail
, bytes
), 0);
3743 if( rc
!=SQLITE_OK
){
3745 sprintf(zBuf
, "(%d) ", rc
);
3746 Tcl_AppendResult(interp
, zBuf
, sqlite3_errmsg(db
), 0);
3751 if( sqlite3TestMakePointerStr(interp
, zBuf
, pStmt
) ) return TCL_ERROR
;
3752 Tcl_AppendResult(interp
, zBuf
, 0);
3758 ** Usage: sqlite3_prepare_tkt3134 DB
3760 ** Generate a prepared statement for a zero-byte string as a test
3761 ** for ticket #3134. The string should be preceded by a zero byte.
3763 static int test_prepare_tkt3134(
3767 Tcl_Obj
*CONST objv
[]
3770 static const char zSql
[] = "\000SELECT 1";
3771 sqlite3_stmt
*pStmt
= 0;
3776 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3777 Tcl_GetString(objv
[0]), " DB sql bytes tailvar", 0);
3780 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3781 rc
= sqlite3_prepare_v2(db
, &zSql
[1], 0, &pStmt
, 0);
3782 assert(rc
==SQLITE_OK
|| pStmt
==0);
3783 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
3784 if( rc
!=SQLITE_OK
){
3786 sprintf(zBuf
, "(%d) ", rc
);
3787 Tcl_AppendResult(interp
, zBuf
, sqlite3_errmsg(db
), 0);
3792 if( sqlite3TestMakePointerStr(interp
, zBuf
, pStmt
) ) return TCL_ERROR
;
3793 Tcl_AppendResult(interp
, zBuf
, 0);
3799 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
3801 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3802 ** database handle <DB>. The parameter <tailval> is the name of a global
3803 ** variable that is set to the unused portion of <sql> (if any). A
3804 ** STMT handle is returned.
3806 static int test_prepare16(
3810 Tcl_Obj
*CONST objv
[]
3812 #ifndef SQLITE_OMIT_UTF16
3815 const void *zTail
= 0;
3817 sqlite3_stmt
*pStmt
= 0;
3820 int bytes
; /* The integer specified as arg 3 */
3821 int objlen
; /* The byte-array length of arg 2 */
3823 if( objc
!=5 && objc
!=4 ){
3824 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3825 Tcl_GetString(objv
[0]), " DB sql bytes ?tailvar?", 0);
3828 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3829 zSql
= Tcl_GetByteArrayFromObj(objv
[2], &objlen
);
3830 if( Tcl_GetIntFromObj(interp
, objv
[3], &bytes
) ) return TCL_ERROR
;
3832 rc
= sqlite3_prepare16(db
, zSql
, bytes
, &pStmt
, objc
>=5 ? &zTail
: 0);
3833 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
3840 objlen
= objlen
- (int)((u8
*)zTail
-(u8
*)zSql
);
3844 pTail
= Tcl_NewByteArrayObj((u8
*)zTail
, objlen
);
3845 Tcl_IncrRefCount(pTail
);
3846 Tcl_ObjSetVar2(interp
, objv
[4], 0, pTail
, 0);
3847 Tcl_DecrRefCount(pTail
);
3851 if( sqlite3TestMakePointerStr(interp
, zBuf
, pStmt
) ) return TCL_ERROR
;
3853 Tcl_AppendResult(interp
, zBuf
, 0);
3854 #endif /* SQLITE_OMIT_UTF16 */
3859 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
3861 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3862 ** database handle <DB>. The parameter <tailval> is the name of a global
3863 ** variable that is set to the unused portion of <sql> (if any). A
3864 ** STMT handle is returned.
3866 static int test_prepare16_v2(
3870 Tcl_Obj
*CONST objv
[]
3872 #ifndef SQLITE_OMIT_UTF16
3875 const void *zTail
= 0;
3877 sqlite3_stmt
*pStmt
= 0;
3880 int bytes
; /* The integer specified as arg 3 */
3881 int objlen
; /* The byte-array length of arg 2 */
3883 if( objc
!=5 && objc
!=4 ){
3884 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3885 Tcl_GetString(objv
[0]), " DB sql bytes ?tailvar?", 0);
3888 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
3889 zSql
= Tcl_GetByteArrayFromObj(objv
[2], &objlen
);
3890 if( Tcl_GetIntFromObj(interp
, objv
[3], &bytes
) ) return TCL_ERROR
;
3892 rc
= sqlite3_prepare16_v2(db
, zSql
, bytes
, &pStmt
, objc
>=5 ? &zTail
: 0);
3893 if( sqlite3TestErrCode(interp
, db
, rc
) ) return TCL_ERROR
;
3900 objlen
= objlen
- (int)((u8
*)zTail
-(u8
*)zSql
);
3904 pTail
= Tcl_NewByteArrayObj((u8
*)zTail
, objlen
);
3905 Tcl_IncrRefCount(pTail
);
3906 Tcl_ObjSetVar2(interp
, objv
[4], 0, pTail
, 0);
3907 Tcl_DecrRefCount(pTail
);
3911 if( sqlite3TestMakePointerStr(interp
, zBuf
, pStmt
) ) return TCL_ERROR
;
3913 Tcl_AppendResult(interp
, zBuf
, 0);
3914 #endif /* SQLITE_OMIT_UTF16 */
3919 ** Usage: sqlite3_open filename ?options-list?
3921 static int test_open(
3925 Tcl_Obj
*CONST objv
[]
3927 const char *zFilename
;
3931 if( objc
!=3 && objc
!=2 && objc
!=1 ){
3932 Tcl_AppendResult(interp
, "wrong # args: should be \"",
3933 Tcl_GetString(objv
[0]), " filename options-list", 0);
3937 zFilename
= objc
>1 ? Tcl_GetString(objv
[1]) : 0;
3938 sqlite3_open(zFilename
, &db
);
3940 if( sqlite3TestMakePointerStr(interp
, zBuf
, db
) ) return TCL_ERROR
;
3941 Tcl_AppendResult(interp
, zBuf
, 0);
3946 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
3948 static int test_open_v2(
3952 Tcl_Obj
*CONST objv
[]
3954 const char *zFilename
;
3966 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME FLAGS VFS");
3969 zFilename
= Tcl_GetString(objv
[1]);
3970 zVfs
= Tcl_GetString(objv
[3]);
3971 if( zVfs
[0]==0x00 ) zVfs
= 0;
3973 rc
= Tcl_ListObjGetElements(interp
, objv
[2], &nFlag
, &apFlag
);
3974 if( rc
!=TCL_OK
) return rc
;
3975 for(i
=0; i
<nFlag
; i
++){
3981 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY
},
3982 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE
},
3983 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE
},
3984 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE
},
3985 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE
},
3986 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY
},
3987 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB
},
3988 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB
},
3989 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB
},
3990 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL
},
3991 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL
},
3992 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL
},
3993 { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL
},
3994 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX
},
3995 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX
},
3996 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE
},
3997 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE
},
3998 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL
},
3999 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI
},
4002 rc
= Tcl_GetIndexFromObjStruct(interp
, apFlag
[i
], aFlag
, sizeof(aFlag
[0]),
4005 if( rc
!=TCL_OK
) return rc
;
4006 flags
|= aFlag
[iFlag
].flag
;
4009 rc
= sqlite3_open_v2(zFilename
, &db
, flags
, zVfs
);
4010 if( sqlite3TestMakePointerStr(interp
, zBuf
, db
) ) return TCL_ERROR
;
4011 Tcl_AppendResult(interp
, zBuf
, 0);
4016 ** Usage: sqlite3_open16 filename options
4018 static int test_open16(
4022 Tcl_Obj
*CONST objv
[]
4024 #ifndef SQLITE_OMIT_UTF16
4025 const void *zFilename
;
4030 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4031 Tcl_GetString(objv
[0]), " filename options-list", 0);
4035 zFilename
= Tcl_GetByteArrayFromObj(objv
[1], 0);
4036 sqlite3_open16(zFilename
, &db
);
4038 if( sqlite3TestMakePointerStr(interp
, zBuf
, db
) ) return TCL_ERROR
;
4039 Tcl_AppendResult(interp
, zBuf
, 0);
4040 #endif /* SQLITE_OMIT_UTF16 */
4045 ** Usage: sqlite3_complete16 <UTF-16 string>
4047 ** Return 1 if the supplied argument is a complete SQL statement, or zero
4050 static int test_complete16(
4054 Tcl_Obj
*CONST objv
[]
4056 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
4060 Tcl_WrongNumArgs(interp
, 1, objv
, "<utf-16 sql>");
4064 zBuf
= (char*)Tcl_GetByteArrayFromObj(objv
[1], 0);
4065 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_complete16(zBuf
)));
4066 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
4071 ** Usage: sqlite3_step STMT
4073 ** Advance the statement to the next row.
4075 static int test_step(
4079 Tcl_Obj
*CONST objv
[]
4081 sqlite3_stmt
*pStmt
;
4085 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4086 Tcl_GetString(objv
[0]), " STMT", 0);
4090 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4091 rc
= sqlite3_step(pStmt
);
4093 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
4094 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), 0);
4098 static int test_sql(
4102 Tcl_Obj
*CONST objv
[]
4104 sqlite3_stmt
*pStmt
;
4107 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT");
4111 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4112 Tcl_SetResult(interp
, (char *)sqlite3_sql(pStmt
), TCL_VOLATILE
);
4117 ** Usage: sqlite3_column_count STMT
4119 ** Return the number of columns returned by the sql statement STMT.
4121 static int test_column_count(
4125 Tcl_Obj
*CONST objv
[]
4127 sqlite3_stmt
*pStmt
;
4130 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4131 Tcl_GetString(objv
[0]), " STMT column", 0);
4135 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4137 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_column_count(pStmt
)));
4142 ** Usage: sqlite3_column_type STMT column
4144 ** Return the type of the data in column 'column' of the current row.
4146 static int test_column_type(
4150 Tcl_Obj
*CONST objv
[]
4152 sqlite3_stmt
*pStmt
;
4157 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4158 Tcl_GetString(objv
[0]), " STMT column", 0);
4162 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4163 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4165 tp
= sqlite3_column_type(pStmt
, col
);
4167 case SQLITE_INTEGER
:
4168 Tcl_SetResult(interp
, "INTEGER", TCL_STATIC
);
4171 Tcl_SetResult(interp
, "NULL", TCL_STATIC
);
4174 Tcl_SetResult(interp
, "FLOAT", TCL_STATIC
);
4177 Tcl_SetResult(interp
, "TEXT", TCL_STATIC
);
4180 Tcl_SetResult(interp
, "BLOB", TCL_STATIC
);
4190 ** Usage: sqlite3_column_int64 STMT column
4192 ** Return the data in column 'column' of the current row cast as an
4193 ** wide (64-bit) integer.
4195 static int test_column_int64(
4199 Tcl_Obj
*CONST objv
[]
4201 sqlite3_stmt
*pStmt
;
4206 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4207 Tcl_GetString(objv
[0]), " STMT column", 0);
4211 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4212 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4214 iVal
= sqlite3_column_int64(pStmt
, col
);
4215 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(iVal
));
4220 ** Usage: sqlite3_column_blob STMT column
4222 static int test_column_blob(
4226 Tcl_Obj
*CONST objv
[]
4228 sqlite3_stmt
*pStmt
;
4235 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4236 Tcl_GetString(objv
[0]), " STMT column", 0);
4240 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4241 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4243 len
= sqlite3_column_bytes(pStmt
, col
);
4244 pBlob
= sqlite3_column_blob(pStmt
, col
);
4245 Tcl_SetObjResult(interp
, Tcl_NewByteArrayObj(pBlob
, len
));
4250 ** Usage: sqlite3_column_double STMT column
4252 ** Return the data in column 'column' of the current row cast as a double.
4254 static int test_column_double(
4258 Tcl_Obj
*CONST objv
[]
4260 sqlite3_stmt
*pStmt
;
4265 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4266 Tcl_GetString(objv
[0]), " STMT column", 0);
4270 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4271 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4273 rVal
= sqlite3_column_double(pStmt
, col
);
4274 Tcl_SetObjResult(interp
, Tcl_NewDoubleObj(rVal
));
4279 ** Usage: sqlite3_data_count STMT
4281 ** Return the number of columns returned by the sql statement STMT.
4283 static int test_data_count(
4287 Tcl_Obj
*CONST objv
[]
4289 sqlite3_stmt
*pStmt
;
4292 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4293 Tcl_GetString(objv
[0]), " STMT column", 0);
4297 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4299 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_data_count(pStmt
)));
4304 ** Usage: sqlite3_column_text STMT column
4306 ** Usage: sqlite3_column_decltype STMT column
4308 ** Usage: sqlite3_column_name STMT column
4310 static int test_stmt_utf8(
4311 void * clientData
, /* Pointer to SQLite API function to be invoke */
4314 Tcl_Obj
*CONST objv
[]
4316 sqlite3_stmt
*pStmt
;
4318 const char *(*xFunc
)(sqlite3_stmt
*, int);
4321 xFunc
= (const char *(*)(sqlite3_stmt
*, int))clientData
;
4323 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4324 Tcl_GetString(objv
[0]), " STMT column", 0);
4328 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4329 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4330 zRet
= xFunc(pStmt
, col
);
4332 Tcl_SetResult(interp
, (char *)zRet
, 0);
4337 static int test_global_recover(
4341 Tcl_Obj
*CONST objv
[]
4343 #ifndef SQLITE_OMIT_DEPRECATED
4346 Tcl_WrongNumArgs(interp
, 1, objv
, "");
4349 rc
= sqlite3_global_recover();
4350 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
4356 ** Usage: sqlite3_column_text STMT column
4358 ** Usage: sqlite3_column_decltype STMT column
4360 ** Usage: sqlite3_column_name STMT column
4362 static int test_stmt_utf16(
4363 void * clientData
, /* Pointer to SQLite API function to be invoked */
4366 Tcl_Obj
*CONST objv
[]
4368 #ifndef SQLITE_OMIT_UTF16
4369 sqlite3_stmt
*pStmt
;
4372 const void *zName16
;
4373 const void *(*xFunc
)(sqlite3_stmt
*, int);
4375 xFunc
= (const void *(*)(sqlite3_stmt
*, int))clientData
;
4377 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4378 Tcl_GetString(objv
[0]), " STMT column", 0);
4382 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4383 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4385 zName16
= xFunc(pStmt
, col
);
4388 const char *z
= zName16
;
4389 for(n
=0; z
[n
] || z
[n
+1]; n
+=2){}
4390 pRet
= Tcl_NewByteArrayObj(zName16
, n
+2);
4391 Tcl_SetObjResult(interp
, pRet
);
4393 #endif /* SQLITE_OMIT_UTF16 */
4399 ** Usage: sqlite3_column_int STMT column
4401 ** Usage: sqlite3_column_bytes STMT column
4403 ** Usage: sqlite3_column_bytes16 STMT column
4406 static int test_stmt_int(
4407 void * clientData
, /* Pointer to SQLite API function to be invoked */
4410 Tcl_Obj
*CONST objv
[]
4412 sqlite3_stmt
*pStmt
;
4414 int (*xFunc
)(sqlite3_stmt
*, int);
4416 xFunc
= (int (*)(sqlite3_stmt
*, int))clientData
;
4418 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4419 Tcl_GetString(objv
[0]), " STMT column", 0);
4423 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
4424 if( Tcl_GetIntFromObj(interp
, objv
[2], &col
) ) return TCL_ERROR
;
4426 Tcl_SetObjResult(interp
, Tcl_NewIntObj(xFunc(pStmt
, col
)));
4431 ** Usage: sqlite_set_magic DB MAGIC-NUMBER
4433 ** Set the db->magic value. This is used to test error recovery logic.
4435 static int sqlite_set_magic(
4443 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
4447 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4448 if( strcmp(argv
[2], "SQLITE_MAGIC_OPEN")==0 ){
4449 db
->magic
= SQLITE_MAGIC_OPEN
;
4450 }else if( strcmp(argv
[2], "SQLITE_MAGIC_CLOSED")==0 ){
4451 db
->magic
= SQLITE_MAGIC_CLOSED
;
4452 }else if( strcmp(argv
[2], "SQLITE_MAGIC_BUSY")==0 ){
4453 db
->magic
= SQLITE_MAGIC_BUSY
;
4454 }else if( strcmp(argv
[2], "SQLITE_MAGIC_ERROR")==0 ){
4455 db
->magic
= SQLITE_MAGIC_ERROR
;
4456 }else if( Tcl_GetInt(interp
, argv
[2], (int*)&db
->magic
) ){
4463 ** Usage: sqlite3_interrupt DB
4465 ** Trigger an interrupt on DB
4467 static int test_interrupt(
4475 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0], " DB", 0);
4478 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4479 sqlite3_interrupt(db
);
4483 static u8
*sqlite3_stack_baseline
= 0;
4486 ** Fill the stack with a known bitpattern.
4488 static void prepStack(void){
4491 for(i
=0; i
<sizeof(bigBuf
)/sizeof(bigBuf
[0]); i
++) bigBuf
[i
] = 0xdeadbeef;
4492 sqlite3_stack_baseline
= (u8
*)&bigBuf
[65536];
4496 ** Get the current stack depth. Used for debugging only.
4498 u64
sqlite3StackDepth(void){
4500 return (u64
)(sqlite3_stack_baseline
- &x
);
4504 ** Usage: sqlite3_stack_used DB SQL
4506 ** Try to measure the amount of stack space used by a call to sqlite3_exec
4508 static int test_stack_used(
4517 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
4521 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4523 (void)sqlite3_exec(db
, argv
[2], 0, 0, 0);
4524 for(i
=65535; i
>=0 && ((u32
*)sqlite3_stack_baseline
)[-i
]==0xdeadbeef; i
--){}
4525 Tcl_SetObjResult(interp
, Tcl_NewIntObj(i
*4));
4530 ** Usage: sqlite_delete_function DB function-name
4532 ** Delete the user function 'function-name' from database handle DB. It
4533 ** is assumed that the user function was created as UTF8, any number of
4534 ** arguments (the way the TCL interface does it).
4536 static int delete_function(
4545 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
4546 " DB function-name", 0);
4549 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4550 rc
= sqlite3_create_function(db
, argv
[2], -1, SQLITE_UTF8
, 0, 0, 0, 0);
4551 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
4556 ** Usage: sqlite_delete_collation DB collation-name
4558 ** Delete the collation sequence 'collation-name' from database handle
4559 ** DB. It is assumed that the collation sequence was created as UTF8 (the
4560 ** way the TCL interface does it).
4562 static int delete_collation(
4571 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
4572 " DB function-name", 0);
4575 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4576 rc
= sqlite3_create_collation(db
, argv
[2], SQLITE_UTF8
, 0, 0);
4577 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
4582 ** Usage: sqlite3_get_autocommit DB
4584 ** Return true if the database DB is currently in auto-commit mode.
4585 ** Return false if not.
4587 static int get_autocommit(
4596 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
4600 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4601 sprintf(zBuf
, "%d", sqlite3_get_autocommit(db
));
4602 Tcl_AppendResult(interp
, zBuf
, 0);
4607 ** Usage: sqlite3_busy_timeout DB MS
4609 ** Set the busy timeout. This is more easily done using the timeout
4610 ** method of the TCL interface. But we need a way to test the case
4611 ** where it returns SQLITE_MISUSE.
4613 static int test_busy_timeout(
4622 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
4626 if( getDbPointer(interp
, argv
[1], &db
) ) return TCL_ERROR
;
4627 if( Tcl_GetInt(interp
, argv
[2], &ms
) ) return TCL_ERROR
;
4628 rc
= sqlite3_busy_timeout(db
, ms
);
4629 Tcl_AppendResult(interp
, sqlite3ErrName(rc
), 0);
4634 ** Usage: tcl_variable_type VARIABLENAME
4636 ** Return the name of the internal representation for the
4637 ** value of the given variable.
4639 static int tcl_variable_type(
4643 Tcl_Obj
*CONST objv
[]
4647 Tcl_WrongNumArgs(interp
, 1, objv
, "VARIABLE");
4650 pVar
= Tcl_GetVar2Ex(interp
, Tcl_GetString(objv
[1]), 0, TCL_LEAVE_ERR_MSG
);
4651 if( pVar
==0 ) return TCL_ERROR
;
4652 if( pVar
->typePtr
){
4653 Tcl_SetObjResult(interp
, Tcl_NewStringObj(pVar
->typePtr
->name
, -1));
4659 ** Usage: sqlite3_release_memory ?N?
4661 ** Attempt to release memory currently held but not actually required.
4662 ** The integer N is the number of bytes we are trying to release. The
4663 ** return value is the amount of memory actually released.
4665 static int test_release_memory(
4669 Tcl_Obj
*CONST objv
[]
4671 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
4674 if( objc
!=1 && objc
!=2 ){
4675 Tcl_WrongNumArgs(interp
, 1, objv
, "?N?");
4679 if( Tcl_GetIntFromObj(interp
, objv
[1], &N
) ) return TCL_ERROR
;
4683 amt
= sqlite3_release_memory(N
);
4684 Tcl_SetObjResult(interp
, Tcl_NewIntObj(amt
));
4691 ** Usage: sqlite3_db_release_memory DB
4693 ** Attempt to release memory currently held by database DB. Return the
4694 ** result code (which in the current implementation is always zero).
4696 static int test_db_release_memory(
4700 Tcl_Obj
*CONST objv
[]
4705 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
4708 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
4709 rc
= sqlite3_db_release_memory(db
);
4710 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
4715 ** Usage: sqlite3_db_filename DB DBNAME
4717 ** Return the name of a file associated with a database.
4719 static int test_db_filename(
4723 Tcl_Obj
*CONST objv
[]
4726 const char *zDbName
;
4728 Tcl_WrongNumArgs(interp
, 1, objv
, "DB DBNAME");
4731 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
4732 zDbName
= Tcl_GetString(objv
[2]);
4733 Tcl_AppendResult(interp
, sqlite3_db_filename(db
, zDbName
), (void*)0);
4738 ** Usage: sqlite3_db_readonly DB DBNAME
4740 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does
4743 static int test_db_readonly(
4747 Tcl_Obj
*CONST objv
[]
4750 const char *zDbName
;
4752 Tcl_WrongNumArgs(interp
, 1, objv
, "DB DBNAME");
4755 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
4756 zDbName
= Tcl_GetString(objv
[2]);
4757 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_db_readonly(db
, zDbName
)));
4762 ** Usage: sqlite3_soft_heap_limit ?N?
4764 ** Query or set the soft heap limit for the current thread. The
4765 ** limit is only changed if the N is present. The previous limit
4768 static int test_soft_heap_limit(
4772 Tcl_Obj
*CONST objv
[]
4776 if( objc
!=1 && objc
!=2 ){
4777 Tcl_WrongNumArgs(interp
, 1, objv
, "?N?");
4781 if( Tcl_GetWideIntFromObj(interp
, objv
[1], &N
) ) return TCL_ERROR
;
4783 amt
= sqlite3_soft_heap_limit64(N
);
4784 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(amt
));
4789 ** Usage: sqlite3_thread_cleanup
4791 ** Call the sqlite3_thread_cleanup API.
4793 static int test_thread_cleanup(
4797 Tcl_Obj
*CONST objv
[]
4799 #ifndef SQLITE_OMIT_DEPRECATED
4800 sqlite3_thread_cleanup();
4806 ** Usage: sqlite3_pager_refcounts DB
4808 ** Return a list of numbers which are the PagerRefcount for all
4809 ** pagers on each database connection.
4811 static int test_pager_refcounts(
4815 Tcl_Obj
*CONST objv
[]
4823 Tcl_AppendResult(interp
, "wrong # args: should be \"",
4824 Tcl_GetStringFromObj(objv
[0], 0), " DB", 0);
4827 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
4828 pResult
= Tcl_NewObj();
4829 for(i
=0; i
<db
->nDb
; i
++){
4830 if( db
->aDb
[i
].pBt
==0 ){
4833 sqlite3_mutex_enter(db
->mutex
);
4834 a
= sqlite3PagerStats(sqlite3BtreePager(db
->aDb
[i
].pBt
));
4836 sqlite3_mutex_leave(db
->mutex
);
4838 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(v
));
4840 Tcl_SetObjResult(interp
, pResult
);
4846 ** tclcmd: working_64bit_int
4848 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4849 ** leads to a number of test failures. The present command checks the
4850 ** TCL build to see whether or not it supports 64-bit integers. It
4851 ** returns TRUE if it does and FALSE if not.
4853 ** This command is used to warn users that their TCL build is defective
4854 ** and that the errors they are seeing in the test scripts might be
4855 ** a result of their defective TCL rather than problems in SQLite.
4857 static int working_64bit_int(
4858 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
4859 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
4860 int objc
, /* Number of arguments */
4861 Tcl_Obj
*CONST objv
[] /* Command arguments */
4866 pTestObj
= Tcl_NewWideIntObj(1000000*(i64
)1234567890);
4867 working
= strcmp(Tcl_GetString(pTestObj
), "1234567890000000")==0;
4868 Tcl_DecrRefCount(pTestObj
);
4869 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(working
));
4875 ** tclcmd: vfs_unlink_test
4877 ** This TCL command unregisters the primary VFS and then registers
4878 ** it back again. This is used to test the ability to register a
4879 ** VFS when none are previously registered, and the ability to
4880 ** unregister the only available VFS. Ticket #2738
4882 static int vfs_unlink_test(
4883 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
4884 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
4885 int objc
, /* Number of arguments */
4886 Tcl_Obj
*CONST objv
[] /* Command arguments */
4890 sqlite3_vfs
*apVfs
[20];
4891 sqlite3_vfs one
, two
;
4893 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
4894 one
.zName
= "__one";
4895 two
.zName
= "__two";
4897 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
4898 ** change the default VFS
4900 pMain
= sqlite3_vfs_find(0);
4901 sqlite3_vfs_register(&one
, 0);
4902 assert( pMain
==0 || pMain
==sqlite3_vfs_find(0) );
4903 sqlite3_vfs_register(&two
, 0);
4904 assert( pMain
==0 || pMain
==sqlite3_vfs_find(0) );
4906 /* We can find a VFS by its name */
4907 assert( sqlite3_vfs_find("__one")==&one
);
4908 assert( sqlite3_vfs_find("__two")==&two
);
4910 /* Calling sqlite_vfs_register with non-zero second parameter changes the
4911 ** default VFS, even if the 1st parameter is an existig VFS that is
4912 ** previously registered as the non-default.
4914 sqlite3_vfs_register(&one
, 1);
4915 assert( sqlite3_vfs_find("__one")==&one
);
4916 assert( sqlite3_vfs_find("__two")==&two
);
4917 assert( sqlite3_vfs_find(0)==&one
);
4918 sqlite3_vfs_register(&two
, 1);
4919 assert( sqlite3_vfs_find("__one")==&one
);
4920 assert( sqlite3_vfs_find("__two")==&two
);
4921 assert( sqlite3_vfs_find(0)==&two
);
4923 sqlite3_vfs_register(pMain
, 1);
4924 assert( sqlite3_vfs_find("__one")==&one
);
4925 assert( sqlite3_vfs_find("__two")==&two
);
4926 assert( sqlite3_vfs_find(0)==pMain
);
4929 /* Unlink the default VFS. Repeat until there are no more VFSes
4932 for(i
=0; i
<sizeof(apVfs
)/sizeof(apVfs
[0]); i
++){
4933 apVfs
[i
] = sqlite3_vfs_find(0);
4935 assert( apVfs
[i
]==sqlite3_vfs_find(apVfs
[i
]->zName
) );
4936 sqlite3_vfs_unregister(apVfs
[i
]);
4937 assert( 0==sqlite3_vfs_find(apVfs
[i
]->zName
) );
4940 assert( 0==sqlite3_vfs_find(0) );
4942 /* Register the main VFS as non-default (will be made default, since
4943 ** it'll be the only one in existence).
4945 sqlite3_vfs_register(pMain
, 0);
4946 assert( sqlite3_vfs_find(0)==pMain
);
4948 /* Un-register the main VFS again to restore an empty VFS list */
4949 sqlite3_vfs_unregister(pMain
);
4950 assert( 0==sqlite3_vfs_find(0) );
4952 /* Relink all VFSes in reverse order. */
4953 for(i
=sizeof(apVfs
)/sizeof(apVfs
[0])-1; i
>=0; i
--){
4955 sqlite3_vfs_register(apVfs
[i
], 1);
4956 assert( apVfs
[i
]==sqlite3_vfs_find(0) );
4957 assert( apVfs
[i
]==sqlite3_vfs_find(apVfs
[i
]->zName
) );
4961 /* Unregister out sample VFSes. */
4962 sqlite3_vfs_unregister(&one
);
4963 sqlite3_vfs_unregister(&two
);
4965 /* Unregistering a VFS that is not currently registered is harmless */
4966 sqlite3_vfs_unregister(&one
);
4967 sqlite3_vfs_unregister(&two
);
4968 assert( sqlite3_vfs_find("__one")==0 );
4969 assert( sqlite3_vfs_find("__two")==0 );
4971 /* We should be left with the original default VFS back as the
4973 assert( sqlite3_vfs_find(0)==pMain
);
4979 ** tclcmd: vfs_initfail_test
4981 ** This TCL command attempts to vfs_find and vfs_register when the
4982 ** sqlite3_initialize() interface is failing. All calls should fail.
4984 static int vfs_initfail_test(
4985 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
4986 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
4987 int objc
, /* Number of arguments */
4988 Tcl_Obj
*CONST objv
[] /* Command arguments */
4991 one
.zName
= "__one";
4993 if( sqlite3_vfs_find(0) ) return TCL_ERROR
;
4994 sqlite3_vfs_register(&one
, 0);
4995 if( sqlite3_vfs_find(0) ) return TCL_ERROR
;
4996 sqlite3_vfs_register(&one
, 1);
4997 if( sqlite3_vfs_find(0) ) return TCL_ERROR
;
5004 static sqlite3_vfs
*apVfs
[20];
5005 static int nVfs
= 0;
5008 ** tclcmd: vfs_unregister_all
5010 ** Unregister all VFSes.
5012 static int vfs_unregister_all(
5013 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5014 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5015 int objc
, /* Number of arguments */
5016 Tcl_Obj
*CONST objv
[] /* Command arguments */
5019 for(i
=0; i
<ArraySize(apVfs
); i
++){
5020 apVfs
[i
] = sqlite3_vfs_find(0);
5021 if( apVfs
[i
]==0 ) break;
5022 sqlite3_vfs_unregister(apVfs
[i
]);
5028 ** tclcmd: vfs_reregister_all
5030 ** Restore all VFSes that were removed using vfs_unregister_all
5032 static int vfs_reregister_all(
5033 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5034 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5035 int objc
, /* Number of arguments */
5036 Tcl_Obj
*CONST objv
[] /* Command arguments */
5039 for(i
=0; i
<nVfs
; i
++){
5040 sqlite3_vfs_register(apVfs
[i
], i
==0);
5047 ** tclcmd: file_control_test DB
5049 ** This TCL command runs the sqlite3_file_control interface and
5050 ** verifies correct operation of the same.
5052 static int file_control_test(
5053 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5054 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5055 int objc
, /* Number of arguments */
5056 Tcl_Obj
*CONST objv
[] /* Command arguments */
5063 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5064 Tcl_GetStringFromObj(objv
[0], 0), " DB", 0);
5067 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
5068 rc
= sqlite3_file_control(db
, 0, 0, &iArg
);
5069 assert( rc
==SQLITE_NOTFOUND
);
5070 rc
= sqlite3_file_control(db
, "notadatabase", SQLITE_FCNTL_LOCKSTATE
, &iArg
);
5071 assert( rc
==SQLITE_ERROR
);
5072 rc
= sqlite3_file_control(db
, "main", -1, &iArg
);
5073 assert( rc
==SQLITE_NOTFOUND
);
5074 rc
= sqlite3_file_control(db
, "temp", -1, &iArg
);
5075 assert( rc
==SQLITE_NOTFOUND
|| rc
==SQLITE_ERROR
);
5082 ** tclcmd: file_control_lasterrno_test DB
5084 ** This TCL command runs the sqlite3_file_control interface and
5085 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
5087 static int file_control_lasterrno_test(
5088 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5089 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5090 int objc
, /* Number of arguments */
5091 Tcl_Obj
*CONST objv
[] /* Command arguments */
5098 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5099 Tcl_GetStringFromObj(objv
[0], 0), " DB", 0);
5102 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5105 rc
= sqlite3_file_control(db
, NULL
, SQLITE_LAST_ERRNO
, &iArg
);
5107 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
5111 Tcl_AppendResult(interp
, "Unexpected non-zero errno: ",
5112 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg
), 0), " ", 0);
5119 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
5121 ** This TCL command runs the sqlite3_file_control interface and
5122 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5123 ** SQLITE_SET_LOCKPROXYFILE verbs.
5125 static int file_control_chunksize_test(
5126 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5127 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5128 int objc
, /* Number of arguments */
5129 Tcl_Obj
*CONST objv
[] /* Command arguments */
5131 int nSize
; /* New chunk size */
5132 char *zDb
; /* Db name ("main", "temp" etc.) */
5133 sqlite3
*db
; /* Database handle */
5134 int rc
; /* file_control() return code */
5137 Tcl_WrongNumArgs(interp
, 1, objv
, "DB DBNAME SIZE");
5140 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
)
5141 || Tcl_GetIntFromObj(interp
, objv
[3], &nSize
)
5145 zDb
= Tcl_GetString(objv
[2]);
5146 if( zDb
[0]=='\0' ) zDb
= NULL
;
5148 rc
= sqlite3_file_control(db
, zDb
, SQLITE_FCNTL_CHUNK_SIZE
, (void *)&nSize
);
5150 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_STATIC
);
5157 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
5159 ** This TCL command runs the sqlite3_file_control interface
5160 ** with SQLITE_FCNTL_SIZE_HINT
5162 static int file_control_sizehint_test(
5163 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5164 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5165 int objc
, /* Number of arguments */
5166 Tcl_Obj
*CONST objv
[] /* Command arguments */
5168 Tcl_WideInt nSize
; /* Hinted size */
5169 char *zDb
; /* Db name ("main", "temp" etc.) */
5170 sqlite3
*db
; /* Database handle */
5171 int rc
; /* file_control() return code */
5174 Tcl_WrongNumArgs(interp
, 1, objv
, "DB DBNAME SIZE");
5177 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
)
5178 || Tcl_GetWideIntFromObj(interp
, objv
[3], &nSize
)
5182 zDb
= Tcl_GetString(objv
[2]);
5183 if( zDb
[0]=='\0' ) zDb
= NULL
;
5185 rc
= sqlite3_file_control(db
, zDb
, SQLITE_FCNTL_SIZE_HINT
, (void *)&nSize
);
5187 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_STATIC
);
5194 ** tclcmd: file_control_lockproxy_test DB PWD
5196 ** This TCL command runs the sqlite3_file_control interface and
5197 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5198 ** SQLITE_SET_LOCKPROXYFILE verbs.
5200 static int file_control_lockproxy_test(
5201 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5202 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5203 int objc
, /* Number of arguments */
5204 Tcl_Obj
*CONST objv
[] /* Command arguments */
5209 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5210 Tcl_GetStringFromObj(objv
[0], 0), " DB PWD", 0);
5213 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5217 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
5218 # if defined(__APPLE__)
5219 # define SQLITE_ENABLE_LOCKING_STYLE 1
5221 # define SQLITE_ENABLE_LOCKING_STYLE 0
5224 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
5230 char proxyPath
[400];
5232 zPwd
= Tcl_GetStringFromObj(objv
[2], &nPwd
);
5233 if( sizeof(proxyPath
)<nPwd
+20 ){
5234 Tcl_AppendResult(interp
, "PWD too big", (void*)0);
5237 sprintf(proxyPath
, "%s/test.proxy", zPwd
);
5238 rc
= sqlite3_file_control(db
, NULL
, SQLITE_SET_LOCKPROXYFILE
, proxyPath
);
5240 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
5243 rc
= sqlite3_file_control(db
, NULL
, SQLITE_GET_LOCKPROXYFILE
, &testPath
);
5244 if( strncmp(proxyPath
,testPath
,11) ){
5245 Tcl_AppendResult(interp
, "Lock proxy file did not match the "
5246 "previously assigned value", 0);
5250 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
5253 rc
= sqlite3_file_control(db
, NULL
, SQLITE_SET_LOCKPROXYFILE
, proxyPath
);
5255 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
5265 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY
5267 ** This TCL command runs the sqlite3_file_control interface with
5268 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
5270 static int file_control_win32_av_retry(
5271 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5272 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5273 int objc
, /* Number of arguments */
5274 Tcl_Obj
*CONST objv
[] /* Command arguments */
5282 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5283 Tcl_GetStringFromObj(objv
[0], 0), " DB NRETRY DELAY", 0);
5286 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5289 if( Tcl_GetIntFromObj(interp
, objv
[2], &a
[0]) ) return TCL_ERROR
;
5290 if( Tcl_GetIntFromObj(interp
, objv
[3], &a
[1]) ) return TCL_ERROR
;
5291 rc
= sqlite3_file_control(db
, NULL
, SQLITE_FCNTL_WIN32_AV_RETRY
, (void*)a
);
5292 sqlite3_snprintf(sizeof(z
), z
, "%d %d %d", rc
, a
[0], a
[1]);
5293 Tcl_AppendResult(interp
, z
, (char*)0);
5298 ** tclcmd: file_control_win32_set_handle DB HANDLE
5300 ** This TCL command runs the sqlite3_file_control interface with
5301 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
5303 static int file_control_win32_set_handle(
5304 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5305 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5306 int objc
, /* Number of arguments */
5307 Tcl_Obj
*CONST objv
[] /* Command arguments */
5311 HANDLE hFile
= NULL
;
5315 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5316 Tcl_GetStringFromObj(objv
[0], 0), " DB HANDLE", 0);
5319 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5322 if( getWin32Handle(interp
, Tcl_GetString(objv
[2]), &hFile
) ){
5325 rc
= sqlite3_file_control(db
, NULL
, SQLITE_FCNTL_WIN32_SET_HANDLE
,
5327 sqlite3_snprintf(sizeof(z
), z
, "%d %p", rc
, (void*)hFile
);
5328 Tcl_AppendResult(interp
, z
, (char*)0);
5334 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG
5336 ** This TCL command runs the sqlite3_file_control interface with
5337 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
5339 static int file_control_persist_wal(
5340 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5341 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5342 int objc
, /* Number of arguments */
5343 Tcl_Obj
*CONST objv
[] /* Command arguments */
5351 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5352 Tcl_GetStringFromObj(objv
[0], 0), " DB FLAG", 0);
5355 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5358 if( Tcl_GetIntFromObj(interp
, objv
[2], &bPersist
) ) return TCL_ERROR
;
5359 rc
= sqlite3_file_control(db
, NULL
, SQLITE_FCNTL_PERSIST_WAL
, (void*)&bPersist
);
5360 sqlite3_snprintf(sizeof(z
), z
, "%d %d", rc
, bPersist
);
5361 Tcl_AppendResult(interp
, z
, (char*)0);
5366 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG
5368 ** This TCL command runs the sqlite3_file_control interface with
5369 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
5371 static int file_control_powersafe_overwrite(
5372 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5373 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5374 int objc
, /* Number of arguments */
5375 Tcl_Obj
*CONST objv
[] /* Command arguments */
5383 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5384 Tcl_GetStringFromObj(objv
[0], 0), " DB FLAG", 0);
5387 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5390 if( Tcl_GetIntFromObj(interp
, objv
[2], &b
) ) return TCL_ERROR
;
5391 rc
= sqlite3_file_control(db
,NULL
,SQLITE_FCNTL_POWERSAFE_OVERWRITE
,(void*)&b
);
5392 sqlite3_snprintf(sizeof(z
), z
, "%d %d", rc
, b
);
5393 Tcl_AppendResult(interp
, z
, (char*)0);
5399 ** tclcmd: file_control_vfsname DB ?AUXDB?
5401 ** Return a string that describes the stack of VFSes.
5403 static int file_control_vfsname(
5404 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5405 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5406 int objc
, /* Number of arguments */
5407 Tcl_Obj
*CONST objv
[] /* Command arguments */
5410 const char *zDbName
= "main";
5413 if( objc
!=2 && objc
!=3 ){
5414 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5415 Tcl_GetStringFromObj(objv
[0], 0), " DB ?AUXDB?", 0);
5418 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5422 zDbName
= Tcl_GetString(objv
[2]);
5424 sqlite3_file_control(db
, zDbName
, SQLITE_FCNTL_VFSNAME
,(void*)&zVfsName
);
5425 Tcl_AppendResult(interp
, zVfsName
, (char*)0);
5426 sqlite3_free(zVfsName
);
5431 ** tclcmd: file_control_tempfilename DB ?AUXDB?
5433 ** Return a string that is a temporary filename
5435 static int file_control_tempfilename(
5436 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5437 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5438 int objc
, /* Number of arguments */
5439 Tcl_Obj
*CONST objv
[] /* Command arguments */
5442 const char *zDbName
= "main";
5445 if( objc
!=2 && objc
!=3 ){
5446 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5447 Tcl_GetStringFromObj(objv
[0], 0), " DB ?AUXDB?", 0);
5450 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5454 zDbName
= Tcl_GetString(objv
[2]);
5456 sqlite3_file_control(db
, zDbName
, SQLITE_FCNTL_TEMPFILENAME
, (void*)&zTName
);
5457 Tcl_AppendResult(interp
, zTName
, (char*)0);
5458 sqlite3_free(zTName
);
5464 ** tclcmd: sqlite3_vfs_list
5466 ** Return a tcl list containing the names of all registered vfs's.
5468 static int vfs_list(
5469 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5470 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5471 int objc
, /* Number of arguments */
5472 Tcl_Obj
*CONST objv
[] /* Command arguments */
5475 Tcl_Obj
*pRet
= Tcl_NewObj();
5477 Tcl_WrongNumArgs(interp
, 1, objv
, "");
5480 for(pVfs
=sqlite3_vfs_find(0); pVfs
; pVfs
=pVfs
->pNext
){
5481 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewStringObj(pVfs
->zName
, -1));
5483 Tcl_SetObjResult(interp
, pRet
);
5488 ** tclcmd: sqlite3_limit DB ID VALUE
5490 ** This TCL command runs the sqlite3_limit interface and
5491 ** verifies correct operation of the same.
5493 static int test_limit(
5494 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5495 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5496 int objc
, /* Number of arguments */
5497 Tcl_Obj
*CONST objv
[] /* Command arguments */
5501 static const struct {
5505 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH
},
5506 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH
},
5507 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN
},
5508 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH
},
5509 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT
},
5510 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP
},
5511 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG
},
5512 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED
},
5513 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH
},
5514 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER
},
5515 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH
},
5516 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS
},
5518 /* Out of range test cases */
5519 { "SQLITE_LIMIT_TOOSMALL", -1, },
5520 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS
+1 },
5527 Tcl_AppendResult(interp
, "wrong # args: should be \"",
5528 Tcl_GetStringFromObj(objv
[0], 0), " DB ID VALUE", 0);
5531 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
5532 zId
= Tcl_GetString(objv
[2]);
5533 for(i
=0; i
<sizeof(aId
)/sizeof(aId
[0]); i
++){
5534 if( strcmp(zId
, aId
[i
].zName
)==0 ){
5539 if( i
>=sizeof(aId
)/sizeof(aId
[0]) ){
5540 Tcl_AppendResult(interp
, "unknown limit type: ", zId
, (char*)0);
5543 if( Tcl_GetIntFromObj(interp
, objv
[3], &val
) ) return TCL_ERROR
;
5544 rc
= sqlite3_limit(db
, id
, val
);
5545 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
5550 ** tclcmd: save_prng_state
5552 ** Save the state of the pseudo-random number generator.
5553 ** At the same time, verify that sqlite3_test_control works even when
5554 ** called with an out-of-range opcode.
5556 static int save_prng_state(
5557 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5558 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5559 int objc
, /* Number of arguments */
5560 Tcl_Obj
*CONST objv
[] /* Command arguments */
5562 int rc
= sqlite3_test_control(9999);
5564 rc
= sqlite3_test_control(-1);
5566 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE
);
5570 ** tclcmd: restore_prng_state
5572 static int restore_prng_state(
5573 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5574 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5575 int objc
, /* Number of arguments */
5576 Tcl_Obj
*CONST objv
[] /* Command arguments */
5578 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE
);
5582 ** tclcmd: reset_prng_state
5584 static int reset_prng_state(
5585 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5586 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5587 int objc
, /* Number of arguments */
5588 Tcl_Obj
*CONST objv
[] /* Command arguments */
5590 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET
);
5595 ** tclcmd: database_may_be_corrupt
5597 ** Indicate that database files might be corrupt. In other words, set the normal
5598 ** state of operation.
5600 static int database_may_be_corrupt(
5601 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5602 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5603 int objc
, /* Number of arguments */
5604 Tcl_Obj
*CONST objv
[] /* Command arguments */
5606 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT
, 0);
5610 ** tclcmd: database_never_corrupt
5612 ** Indicate that database files are always well-formed. This enables extra assert()
5613 ** statements that test conditions that are always true for well-formed databases.
5615 static int database_never_corrupt(
5616 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5617 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5618 int objc
, /* Number of arguments */
5619 Tcl_Obj
*CONST objv
[] /* Command arguments */
5621 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT
, 1);
5626 ** tclcmd: pcache_stats
5628 static int test_pcache_stats(
5629 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
5630 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5631 int objc
, /* Number of arguments */
5632 Tcl_Obj
*CONST objv
[] /* Command arguments */
5640 sqlite3PcacheStats(&nCurrent
, &nMax
, &nMin
, &nRecyclable
);
5642 pRet
= Tcl_NewObj();
5643 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewStringObj("current", -1));
5644 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(nCurrent
));
5645 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewStringObj("max", -1));
5646 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(nMax
));
5647 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewStringObj("min", -1));
5648 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(nMin
));
5649 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewStringObj("recyclable", -1));
5650 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(nRecyclable
));
5652 Tcl_SetObjResult(interp
, pRet
);
5657 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5658 static void test_unlock_notify_cb(void **aArg
, int nArg
){
5660 for(ii
=0; ii
<nArg
; ii
++){
5661 Tcl_EvalEx((Tcl_Interp
*)aArg
[ii
], "unlock_notify", -1, TCL_EVAL_GLOBAL
);
5664 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
5667 ** tclcmd: sqlite3_unlock_notify db
5669 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5670 static int test_unlock_notify(
5671 ClientData clientData
, /* Unused */
5672 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5673 int objc
, /* Number of arguments */
5674 Tcl_Obj
*CONST objv
[] /* Command arguments */
5680 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
5684 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5687 rc
= sqlite3_unlock_notify(db
, test_unlock_notify_cb
, (void *)interp
);
5688 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
5694 ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
5696 static int test_wal_checkpoint(
5697 ClientData clientData
, /* Unused */
5698 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5699 int objc
, /* Number of arguments */
5700 Tcl_Obj
*CONST objv
[] /* Command arguments */
5706 if( objc
!=3 && objc
!=2 ){
5707 Tcl_WrongNumArgs(interp
, 1, objv
, "DB ?NAME?");
5711 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
5715 zDb
= Tcl_GetString(objv
[2]);
5717 rc
= sqlite3_wal_checkpoint(db
, zDb
);
5718 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
5723 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
5725 ** This command calls the wal_checkpoint_v2() function with the specified
5726 ** mode argument (passive, full or restart). If present, the database name
5727 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
5728 ** NAME argument is not present, a NULL pointer is passed instead.
5730 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
5731 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
5732 ** to the error message obtained from sqlite3_errmsg().
5734 ** Otherwise, this command returns a list of three integers. The first integer
5735 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
5736 ** are the values returned via the output parameters by wal_checkpoint_v2() -
5737 ** the number of frames in the log and the number of frames in the log
5738 ** that have been checkpointed.
5740 static int test_wal_checkpoint_v2(
5741 ClientData clientData
, /* Unused */
5742 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5743 int objc
, /* Number of arguments */
5744 Tcl_Obj
*CONST objv
[] /* Command arguments */
5755 const char * aMode
[] = { "passive", "full", "restart", 0 };
5756 assert( SQLITE_CHECKPOINT_PASSIVE
==0 );
5757 assert( SQLITE_CHECKPOINT_FULL
==1 );
5758 assert( SQLITE_CHECKPOINT_RESTART
==2 );
5760 if( objc
!=3 && objc
!=4 ){
5761 Tcl_WrongNumArgs(interp
, 1, objv
, "DB MODE ?NAME?");
5766 zDb
= Tcl_GetString(objv
[3]);
5768 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
)
5769 || Tcl_GetIndexFromObj(interp
, objv
[2], aMode
, "mode", 0, &eMode
)
5774 rc
= sqlite3_wal_checkpoint_v2(db
, zDb
, eMode
, &nLog
, &nCkpt
);
5775 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_BUSY
){
5776 Tcl_SetResult(interp
, (char *)sqlite3_errmsg(db
), TCL_VOLATILE
);
5780 pRet
= Tcl_NewObj();
5781 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(rc
==SQLITE_BUSY
?1:0));
5782 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(nLog
));
5783 Tcl_ListObjAppendElement(interp
, pRet
, Tcl_NewIntObj(nCkpt
));
5784 Tcl_SetObjResult(interp
, pRet
);
5790 ** tclcmd: test_sqlite3_log ?SCRIPT?
5792 static struct LogCallback
{
5793 Tcl_Interp
*pInterp
;
5795 } logcallback
= {0, 0};
5796 static void xLogcallback(void *unused
, int err
, char *zMsg
){
5797 Tcl_Obj
*pNew
= Tcl_DuplicateObj(logcallback
.pObj
);
5798 Tcl_IncrRefCount(pNew
);
5799 Tcl_ListObjAppendElement(
5800 0, pNew
, Tcl_NewStringObj(sqlite3ErrName(err
), -1)
5802 Tcl_ListObjAppendElement(0, pNew
, Tcl_NewStringObj(zMsg
, -1));
5803 Tcl_EvalObjEx(logcallback
.pInterp
, pNew
, TCL_EVAL_GLOBAL
|TCL_EVAL_DIRECT
);
5804 Tcl_DecrRefCount(pNew
);
5806 static int test_sqlite3_log(
5807 ClientData clientData
,
5808 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
5809 int objc
, /* Number of arguments */
5810 Tcl_Obj
*CONST objv
[] /* Command arguments */
5813 Tcl_WrongNumArgs(interp
, 1, objv
, "SCRIPT");
5816 if( logcallback
.pObj
){
5817 Tcl_DecrRefCount(logcallback
.pObj
);
5818 logcallback
.pObj
= 0;
5819 logcallback
.pInterp
= 0;
5820 sqlite3_config(SQLITE_CONFIG_LOG
, 0, 0);
5823 logcallback
.pObj
= objv
[1];
5824 Tcl_IncrRefCount(logcallback
.pObj
);
5825 logcallback
.pInterp
= interp
;
5826 sqlite3_config(SQLITE_CONFIG_LOG
, xLogcallback
, 0);
5832 ** tcl_objproc COMMANDNAME ARGS...
5834 ** Run a TCL command using its objProc interface. Throw an error if
5835 ** the command has no objProc interface.
5837 static int runAsObjProc(
5841 Tcl_Obj
*CONST objv
[]
5843 Tcl_CmdInfo cmdInfo
;
5845 Tcl_WrongNumArgs(interp
, 1, objv
, "COMMAND ...");
5848 if( !Tcl_GetCommandInfo(interp
, Tcl_GetString(objv
[1]), &cmdInfo
) ){
5849 Tcl_AppendResult(interp
, "command not found: ",
5850 Tcl_GetString(objv
[1]), (char*)0);
5853 if( cmdInfo
.objProc
==0 ){
5854 Tcl_AppendResult(interp
, "command has no objProc: ",
5855 Tcl_GetString(objv
[1]), (char*)0);
5858 return cmdInfo
.objProc(cmdInfo
.objClientData
, interp
, objc
-1, objv
+1);
5861 #ifndef SQLITE_OMIT_EXPLAIN
5863 ** WARNING: The following function, printExplainQueryPlan() is an exact
5864 ** copy of example code from eqp.in (eqp.html). If this code is modified,
5865 ** then the documentation copy needs to be modified as well.
5868 ** Argument pStmt is a prepared SQL statement. This function compiles
5869 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
5870 ** and prints the report to stdout using printf().
5872 int printExplainQueryPlan(sqlite3_stmt
*pStmt
){
5873 const char *zSql
; /* Input SQL */
5874 char *zExplain
; /* SQL with EXPLAIN QUERY PLAN prepended */
5875 sqlite3_stmt
*pExplain
; /* Compiled EXPLAIN QUERY PLAN command */
5876 int rc
; /* Return code from sqlite3_prepare_v2() */
5878 zSql
= sqlite3_sql(pStmt
);
5879 if( zSql
==0 ) return SQLITE_ERROR
;
5881 zExplain
= sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql
);
5882 if( zExplain
==0 ) return SQLITE_NOMEM
;
5884 rc
= sqlite3_prepare_v2(sqlite3_db_handle(pStmt
), zExplain
, -1, &pExplain
, 0);
5885 sqlite3_free(zExplain
);
5886 if( rc
!=SQLITE_OK
) return rc
;
5888 while( SQLITE_ROW
==sqlite3_step(pExplain
) ){
5889 int iSelectid
= sqlite3_column_int(pExplain
, 0);
5890 int iOrder
= sqlite3_column_int(pExplain
, 1);
5891 int iFrom
= sqlite3_column_int(pExplain
, 2);
5892 const char *zDetail
= (const char *)sqlite3_column_text(pExplain
, 3);
5894 printf("%d %d %d %s\n", iSelectid
, iOrder
, iFrom
, zDetail
);
5897 return sqlite3_finalize(pExplain
);
5900 static int test_print_eqp(
5904 Tcl_Obj
*CONST objv
[]
5907 sqlite3_stmt
*pStmt
;
5910 Tcl_WrongNumArgs(interp
, 1, objv
, "STMT");
5913 if( getStmtPointer(interp
, Tcl_GetString(objv
[1]), &pStmt
) ) return TCL_ERROR
;
5914 rc
= printExplainQueryPlan(pStmt
);
5915 /* This is needed on Windows so that a test case using this
5916 ** function can open a read pipe and get the output of
5917 ** printExplainQueryPlan() immediately.
5920 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), 0);
5923 #endif /* SQLITE_OMIT_EXPLAIN */
5926 ** sqlite3_test_control VERB ARGS...
5928 static int test_test_control(
5932 Tcl_Obj
*CONST objv
[]
5938 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT
},
5939 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP
},
5946 Tcl_WrongNumArgs(interp
, 1, objv
, "VERB ARGS...");
5950 rc
= Tcl_GetIndexFromObjStruct(
5951 interp
, objv
[1], aVerb
, sizeof(aVerb
[0]), "VERB", 0, &iVerb
5953 if( rc
!=TCL_OK
) return rc
;
5955 iFlag
= aVerb
[iVerb
].i
;
5957 case SQLITE_TESTCTRL_LOCALTIME_FAULT
: {
5960 Tcl_WrongNumArgs(interp
, 2, objv
, "ONOFF");
5963 if( Tcl_GetBooleanFromObj(interp
, objv
[2], &val
) ) return TCL_ERROR
;
5964 sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT
, val
);
5968 case SQLITE_TESTCTRL_SORTER_MMAP
: {
5972 Tcl_WrongNumArgs(interp
, 2, objv
, "DB LIMIT");
5975 if( getDbPointer(interp
, Tcl_GetString(objv
[2]), &db
) ) return TCL_ERROR
;
5976 if( Tcl_GetIntFromObj(interp
, objv
[3], &val
) ) return TCL_ERROR
;
5977 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP
, db
, val
);
5982 Tcl_ResetResult(interp
);
5987 #include <sys/time.h>
5988 #include <sys/resource.h>
5990 static int test_getrusage(
5994 Tcl_Obj
*CONST objv
[]
5998 memset(&r
, 0, sizeof(r
));
5999 getrusage(RUSAGE_SELF
, &r
);
6001 sprintf(buf
, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
6002 (int)r
.ru_utime
.tv_sec
, (int)r
.ru_utime
.tv_usec
,
6003 (int)r
.ru_stime
.tv_sec
, (int)r
.ru_stime
.tv_usec
,
6004 (int)r
.ru_minflt
, (int)r
.ru_majflt
6006 Tcl_SetObjResult(interp
, Tcl_NewStringObj(buf
, -1));
6013 ** Information passed from the main thread into the windows file locker
6014 ** background thread.
6016 struct win32FileLocker
{
6017 char *evName
; /* Name of event to signal thread startup */
6018 HANDLE h
; /* Handle of the file to be locked */
6019 int delay1
; /* Delay before locking */
6020 int delay2
; /* Delay before unlocking */
6021 int ok
; /* Finished ok */
6022 int err
; /* True if an error occurs */
6028 #include <process.h>
6030 ** The background thread that does file locking.
6032 static void win32_file_locker(void *pAppData
){
6033 struct win32FileLocker
*p
= (struct win32FileLocker
*)pAppData
;
6035 HANDLE ev
= OpenEvent(EVENT_MODIFY_STATE
, FALSE
, p
->evName
);
6041 if( p
->delay1
) Sleep(p
->delay1
);
6042 if( LockFile(p
->h
, 0, 0, 100000000, 0) ){
6044 UnlockFile(p
->h
, 0, 0, 100000000, 0);
6058 ** lock_win32_file FILENAME DELAY1 DELAY2
6060 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
6061 ** Wait DELAY1 milliseconds before acquiring the lock.
6063 static int win32_file_lock(
6067 Tcl_Obj
*CONST objv
[]
6069 static struct win32FileLocker x
= { "win32_file_lock", 0, 0, 0, 0, 0 };
6070 const char *zFilename
;
6076 if( objc
!=4 && objc
!=1 ){
6077 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME DELAY1 DELAY2");
6081 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%d %d %d %d %d",
6082 x
.ok
, x
.err
, x
.delay1
, x
.delay2
, x
.h
);
6083 Tcl_AppendResult(interp
, zBuf
, (char*)0);
6086 while( x
.h
&& retry
<30 ){
6091 Tcl_AppendResult(interp
, "busy", (char*)0);
6094 if( Tcl_GetIntFromObj(interp
, objv
[2], &x
.delay1
) ) return TCL_ERROR
;
6095 if( Tcl_GetIntFromObj(interp
, objv
[3], &x
.delay2
) ) return TCL_ERROR
;
6096 zFilename
= Tcl_GetString(objv
[1]);
6097 x
.h
= CreateFile(zFilename
, GENERIC_READ
|GENERIC_WRITE
,
6098 FILE_SHARE_READ
|FILE_SHARE_WRITE
, 0, OPEN_ALWAYS
,
6099 FILE_ATTRIBUTE_NORMAL
, 0);
6101 Tcl_AppendResult(interp
, "cannot open file: ", zFilename
, (char*)0);
6104 ev
= CreateEvent(NULL
, TRUE
, FALSE
, x
.evName
);
6106 Tcl_AppendResult(interp
, "cannot create event: ", x
.evName
, (char*)0);
6109 _beginthread(win32_file_locker
, 0, (void*)&x
);
6111 if ( (wResult
= WaitForSingleObject(ev
, 10000))!=WAIT_OBJECT_0
){
6112 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "0x%x", wResult
);
6113 Tcl_AppendResult(interp
, "wait failed: ", zBuf
, (char*)0);
6122 ** exists_win32_path PATH
6124 ** Returns non-zero if the specified path exists, whose fully qualified name
6125 ** may exceed 260 characters if it is prefixed with "\\?\".
6127 static int win32_exists_path(
6131 Tcl_Obj
*CONST objv
[]
6134 Tcl_WrongNumArgs(interp
, 1, objv
, "PATH");
6137 Tcl_SetObjResult(interp
, Tcl_NewBooleanObj(
6138 GetFileAttributesW( Tcl_GetUnicode(objv
[1]))!=INVALID_FILE_ATTRIBUTES
));
6143 ** find_win32_file PATTERN
6145 ** Returns a list of entries in a directory that match the specified pattern,
6146 ** whose fully qualified name may exceed 248 characters if it is prefixed with
6149 static int win32_find_file(
6153 Tcl_Obj
*CONST objv
[]
6155 HANDLE hFindFile
= INVALID_HANDLE_VALUE
;
6156 WIN32_FIND_DATAW findData
;
6160 Tcl_WrongNumArgs(interp
, 1, objv
, "PATTERN");
6163 hFindFile
= FindFirstFileW(Tcl_GetUnicode(objv
[1]), &findData
);
6164 if( hFindFile
==INVALID_HANDLE_VALUE
){
6165 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(GetLastError()));
6168 listObj
= Tcl_NewObj();
6169 Tcl_IncrRefCount(listObj
);
6171 Tcl_ListObjAppendElement(interp
, listObj
, Tcl_NewUnicodeObj(
6172 findData
.cFileName
, -1));
6173 Tcl_ListObjAppendElement(interp
, listObj
, Tcl_NewWideIntObj(
6174 findData
.dwFileAttributes
));
6175 } while( FindNextFileW(hFindFile
, &findData
) );
6176 lastErrno
= GetLastError();
6177 if( lastErrno
!=NO_ERROR
&& lastErrno
!=ERROR_NO_MORE_FILES
){
6178 FindClose(hFindFile
);
6179 Tcl_DecrRefCount(listObj
);
6180 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(GetLastError()));
6183 FindClose(hFindFile
);
6184 Tcl_SetObjResult(interp
, listObj
);
6189 ** delete_win32_file FILENAME
6191 ** Deletes the specified file, whose fully qualified name may exceed 260
6192 ** characters if it is prefixed with "\\?\".
6194 static int win32_delete_file(
6198 Tcl_Obj
*CONST objv
[]
6201 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME");
6204 if( !DeleteFileW(Tcl_GetUnicode(objv
[1])) ){
6205 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(GetLastError()));
6208 Tcl_ResetResult(interp
);
6213 ** make_win32_dir DIRECTORY
6215 ** Creates the specified directory, whose fully qualified name may exceed 248
6216 ** characters if it is prefixed with "\\?\".
6218 static int win32_mkdir(
6222 Tcl_Obj
*CONST objv
[]
6225 Tcl_WrongNumArgs(interp
, 1, objv
, "DIRECTORY");
6228 if( !CreateDirectoryW(Tcl_GetUnicode(objv
[1]), NULL
) ){
6229 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(GetLastError()));
6232 Tcl_ResetResult(interp
);
6237 ** remove_win32_dir DIRECTORY
6239 ** Removes the specified directory, whose fully qualified name may exceed 248
6240 ** characters if it is prefixed with "\\?\".
6242 static int win32_rmdir(
6246 Tcl_Obj
*CONST objv
[]
6249 Tcl_WrongNumArgs(interp
, 1, objv
, "DIRECTORY");
6252 if( !RemoveDirectoryW(Tcl_GetUnicode(objv
[1])) ){
6253 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(GetLastError()));
6256 Tcl_ResetResult(interp
);
6263 ** optimization_control DB OPT BOOLEAN
6265 ** Enable or disable query optimizations using the sqlite3_test_control()
6266 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
6267 ** OPT is the name of the optimization to be disabled.
6269 static int optimization_control(
6273 Tcl_Obj
*CONST objv
[]
6280 static const struct {
6281 const char *zOptName
;
6284 { "all", SQLITE_AllOpts
},
6286 { "query-flattener", SQLITE_QueryFlattener
},
6287 { "column-cache", SQLITE_ColumnCache
},
6288 { "groupby-order", SQLITE_GroupByOrder
},
6289 { "factor-constants", SQLITE_FactorOutConst
},
6290 { "distinct-opt", SQLITE_DistinctOpt
},
6291 { "cover-idx-scan", SQLITE_CoverIdxScan
},
6292 { "order-by-idx-join", SQLITE_OrderByIdxJoin
},
6293 { "transitive", SQLITE_Transitive
},
6294 { "subquery-coroutine", SQLITE_SubqCoroutine
},
6295 { "omit-noop-join", SQLITE_OmitNoopJoin
},
6296 { "stat3", SQLITE_Stat3
},
6300 Tcl_WrongNumArgs(interp
, 1, objv
, "DB OPT BOOLEAN");
6303 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
6304 if( Tcl_GetBooleanFromObj(interp
, objv
[3], &onoff
) ) return TCL_ERROR
;
6305 zOpt
= Tcl_GetString(objv
[2]);
6306 for(i
=0; i
<sizeof(aOpt
)/sizeof(aOpt
[0]); i
++){
6307 if( strcmp(zOpt
, aOpt
[i
].zOptName
)==0 ){
6308 mask
= aOpt
[i
].mask
;
6312 if( onoff
) mask
= ~mask
;
6313 if( i
>=sizeof(aOpt
)/sizeof(aOpt
[0]) ){
6314 Tcl_AppendResult(interp
, "unknown optimization - should be one of:",
6316 for(i
=0; i
<sizeof(aOpt
)/sizeof(aOpt
[0]); i
++){
6317 Tcl_AppendResult(interp
, " ", aOpt
[i
].zOptName
, (char*)0);
6321 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS
, db
, mask
);
6325 typedef struct sqlite3_api_routines sqlite3_api_routines
;
6327 ** load_static_extension DB NAME ...
6329 ** Load one or more statically linked extensions.
6331 static int tclLoadStaticExtensionCmd(
6335 Tcl_Obj
*CONST objv
[]
6337 extern int sqlite3_amatch_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6338 extern int sqlite3_closure_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6339 extern int sqlite3_fileio_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6340 extern int sqlite3_fuzzer_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6341 extern int sqlite3_ieee_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6342 extern int sqlite3_nextchar_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6343 extern int sqlite3_percentile_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6344 extern int sqlite3_regexp_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6345 extern int sqlite3_spellfix_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6346 extern int sqlite3_totype_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6347 extern int sqlite3_wholenumber_init(sqlite3
*,char**,const sqlite3_api_routines
*);
6348 static const struct {
6349 const char *zExtName
;
6350 int (*pInit
)(sqlite3
*,char**,const sqlite3_api_routines
*);
6352 { "amatch", sqlite3_amatch_init
},
6353 { "closure", sqlite3_closure_init
},
6354 { "fileio", sqlite3_fileio_init
},
6355 { "fuzzer", sqlite3_fuzzer_init
},
6356 { "ieee754", sqlite3_ieee_init
},
6357 { "nextchar", sqlite3_nextchar_init
},
6358 { "percentile", sqlite3_percentile_init
},
6359 { "regexp", sqlite3_regexp_init
},
6360 { "spellfix", sqlite3_spellfix_init
},
6361 { "totype", sqlite3_totype_init
},
6362 { "wholenumber", sqlite3_wholenumber_init
},
6369 Tcl_WrongNumArgs(interp
, 1, objv
, "DB NAME ...");
6372 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
6373 for(j
=2; j
<objc
; j
++){
6374 zName
= Tcl_GetString(objv
[j
]);
6375 for(i
=0; i
<ArraySize(aExtension
); i
++){
6376 if( strcmp(zName
, aExtension
[i
].zExtName
)==0 ) break;
6378 if( i
>=ArraySize(aExtension
) ){
6379 Tcl_AppendResult(interp
, "no such extension: ", zName
, (char*)0);
6382 rc
= aExtension
[i
].pInit(db
, &zErrMsg
, 0);
6383 if( rc
!=SQLITE_OK
|| zErrMsg
){
6384 Tcl_AppendResult(interp
, "initialization of ", zName
, " failed: ", zErrMsg
,
6386 sqlite3_free(zErrMsg
);
6394 ** sorter_test_fakeheap BOOL
6397 static int sorter_test_fakeheap(
6401 Tcl_Obj
*CONST objv
[]
6405 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOL");
6409 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &bArg
) ){
6414 if( sqlite3GlobalConfig
.pHeap
==0 ){
6415 sqlite3GlobalConfig
.pHeap
= SQLITE_INT_TO_PTR(-1);
6418 if( sqlite3GlobalConfig
.pHeap
==SQLITE_INT_TO_PTR(-1) ){
6419 sqlite3GlobalConfig
.pHeap
= 0;
6423 Tcl_ResetResult(interp
);
6428 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2
6430 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
6431 ** check that the leftmost and rightmost columns returned are both integers,
6432 ** and that both contain the same value.
6434 ** Then execute statement $SQL2. Check that the statement returns the same
6435 ** set of integers in the same order as in the previous step (using $SQL1).
6437 static int sorter_test_sort4_helper(
6441 Tcl_Obj
*CONST objv
[]
6452 sqlite3_stmt
*pStmt
;
6455 Tcl_WrongNumArgs(interp
, 1, objv
, "DB SQL1 NSTEP SQL2");
6459 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
6460 zSql1
= Tcl_GetString(objv
[2]);
6461 if( Tcl_GetIntFromObj(interp
, objv
[3], &nStep
) ) return TCL_ERROR
;
6462 zSql2
= Tcl_GetString(objv
[4]);
6464 rc
= sqlite3_prepare_v2(db
, zSql1
, -1, &pStmt
, 0);
6465 if( rc
!=SQLITE_OK
) goto sql_error
;
6467 iB
= sqlite3_column_count(pStmt
)-1;
6468 for(iStep
=0; iStep
<nStep
&& SQLITE_ROW
==sqlite3_step(pStmt
); iStep
++){
6469 int a
= sqlite3_column_int(pStmt
, 0);
6470 if( a
!=sqlite3_column_int(pStmt
, iB
) ){
6471 Tcl_AppendResult(interp
, "data error: (a!=b)", 0);
6475 iCksum1
+= (iCksum1
<< 3) + a
;
6477 rc
= sqlite3_finalize(pStmt
);
6478 if( rc
!=SQLITE_OK
) goto sql_error
;
6480 rc
= sqlite3_prepare_v2(db
, zSql2
, -1, &pStmt
, 0);
6481 if( rc
!=SQLITE_OK
) goto sql_error
;
6482 for(iStep
=0; SQLITE_ROW
==sqlite3_step(pStmt
); iStep
++){
6483 int a
= sqlite3_column_int(pStmt
, 0);
6484 iCksum2
+= (iCksum2
<< 3) + a
;
6486 rc
= sqlite3_finalize(pStmt
);
6487 if( rc
!=SQLITE_OK
) goto sql_error
;
6489 if( iCksum1
!=iCksum2
){
6490 Tcl_AppendResult(interp
, "checksum mismatch", 0);
6496 Tcl_AppendResult(interp
, "sql error: ", sqlite3_errmsg(db
), 0);
6501 #ifdef SQLITE_USER_AUTHENTICATION
6502 #include "sqlite3userauth.h"
6504 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD
6506 static int test_user_authenticate(
6507 ClientData clientData
, /* Unused */
6508 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
6509 int objc
, /* Number of arguments */
6510 Tcl_Obj
*CONST objv
[] /* Command arguments */
6519 Tcl_WrongNumArgs(interp
, 1, objv
, "DB USERNAME PASSWORD");
6522 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
6525 zUser
= Tcl_GetString(objv
[2]);
6526 zPasswd
= Tcl_GetStringFromObj(objv
[3], &nPasswd
);
6527 rc
= sqlite3_user_authenticate(db
, zUser
, zPasswd
, nPasswd
);
6528 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
6531 #endif /* SQLITE_USER_AUTHENTICATION */
6533 #ifdef SQLITE_USER_AUTHENTICATION
6535 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN
6537 static int test_user_add(
6538 ClientData clientData
, /* Unused */
6539 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
6540 int objc
, /* Number of arguments */
6541 Tcl_Obj
*CONST objv
[] /* Command arguments */
6551 Tcl_WrongNumArgs(interp
, 1, objv
, "DB USERNAME PASSWORD ISADMIN");
6554 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
6557 zUser
= Tcl_GetString(objv
[2]);
6558 zPasswd
= Tcl_GetStringFromObj(objv
[3], &nPasswd
);
6559 Tcl_GetBooleanFromObj(interp
, objv
[4], &isAdmin
);
6560 rc
= sqlite3_user_add(db
, zUser
, zPasswd
, nPasswd
, isAdmin
);
6561 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
6564 #endif /* SQLITE_USER_AUTHENTICATION */
6566 #ifdef SQLITE_USER_AUTHENTICATION
6568 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN
6570 static int test_user_change(
6571 ClientData clientData
, /* Unused */
6572 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
6573 int objc
, /* Number of arguments */
6574 Tcl_Obj
*CONST objv
[] /* Command arguments */
6584 Tcl_WrongNumArgs(interp
, 1, objv
, "DB USERNAME PASSWORD ISADMIN");
6587 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
6590 zUser
= Tcl_GetString(objv
[2]);
6591 zPasswd
= Tcl_GetStringFromObj(objv
[3], &nPasswd
);
6592 Tcl_GetBooleanFromObj(interp
, objv
[4], &isAdmin
);
6593 rc
= sqlite3_user_change(db
, zUser
, zPasswd
, nPasswd
, isAdmin
);
6594 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
6597 #endif /* SQLITE_USER_AUTHENTICATION */
6599 #ifdef SQLITE_USER_AUTHENTICATION
6601 ** tclcmd: sqlite3_user_delete DB USERNAME
6603 static int test_user_delete(
6604 ClientData clientData
, /* Unused */
6605 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
6606 int objc
, /* Number of arguments */
6607 Tcl_Obj
*CONST objv
[] /* Command arguments */
6614 Tcl_WrongNumArgs(interp
, 1, objv
, "DB USERNAME");
6617 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ){
6620 zUser
= Tcl_GetString(objv
[2]);
6621 rc
= sqlite3_user_delete(db
, zUser
);
6622 Tcl_SetResult(interp
, (char *)t1ErrorName(rc
), TCL_STATIC
);
6625 #endif /* SQLITE_USER_AUTHENTICATION */
6628 ** Register commands with the TCL interpreter.
6630 int Sqlitetest1_Init(Tcl_Interp
*interp
){
6631 extern int sqlite3_search_count
;
6632 extern int sqlite3_found_count
;
6633 extern int sqlite3_interrupt_count
;
6634 extern int sqlite3_open_file_count
;
6635 extern int sqlite3_sort_count
;
6636 extern int sqlite3_current_time
;
6637 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
6638 extern int sqlite3_hostid_num
;
6640 extern int sqlite3_max_blobsize
;
6641 extern int sqlite3BtreeSharedCacheReport(void*,
6642 Tcl_Interp
*,int,Tcl_Obj
*CONST
*);
6647 { "db_enter", (Tcl_CmdProc
*)db_enter
},
6648 { "db_leave", (Tcl_CmdProc
*)db_leave
},
6649 { "sqlite3_mprintf_int", (Tcl_CmdProc
*)sqlite3_mprintf_int
},
6650 { "sqlite3_mprintf_int64", (Tcl_CmdProc
*)sqlite3_mprintf_int64
},
6651 { "sqlite3_mprintf_long", (Tcl_CmdProc
*)sqlite3_mprintf_long
},
6652 { "sqlite3_mprintf_str", (Tcl_CmdProc
*)sqlite3_mprintf_str
},
6653 { "sqlite3_snprintf_str", (Tcl_CmdProc
*)sqlite3_snprintf_str
},
6654 { "sqlite3_mprintf_stronly", (Tcl_CmdProc
*)sqlite3_mprintf_stronly
},
6655 { "sqlite3_mprintf_double", (Tcl_CmdProc
*)sqlite3_mprintf_double
},
6656 { "sqlite3_mprintf_scaled", (Tcl_CmdProc
*)sqlite3_mprintf_scaled
},
6657 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc
*)sqlite3_mprintf_hexdouble
},
6658 { "sqlite3_mprintf_z_test", (Tcl_CmdProc
*)test_mprintf_z
},
6659 { "sqlite3_mprintf_n_test", (Tcl_CmdProc
*)test_mprintf_n
},
6660 { "sqlite3_snprintf_int", (Tcl_CmdProc
*)test_snprintf_int
},
6661 { "sqlite3_last_insert_rowid", (Tcl_CmdProc
*)test_last_rowid
},
6662 { "sqlite3_exec_printf", (Tcl_CmdProc
*)test_exec_printf
},
6663 { "sqlite3_exec_hex", (Tcl_CmdProc
*)test_exec_hex
},
6664 { "sqlite3_exec", (Tcl_CmdProc
*)test_exec
},
6665 { "sqlite3_exec_nr", (Tcl_CmdProc
*)test_exec_nr
},
6666 #ifndef SQLITE_OMIT_GET_TABLE
6667 { "sqlite3_get_table_printf", (Tcl_CmdProc
*)test_get_table_printf
},
6669 { "sqlite3_close", (Tcl_CmdProc
*)sqlite_test_close
},
6670 { "sqlite3_close_v2", (Tcl_CmdProc
*)sqlite_test_close_v2
},
6671 { "sqlite3_create_function", (Tcl_CmdProc
*)test_create_function
},
6672 { "sqlite3_create_aggregate", (Tcl_CmdProc
*)test_create_aggregate
},
6673 { "sqlite_register_test_function", (Tcl_CmdProc
*)test_register_func
},
6674 { "sqlite_abort", (Tcl_CmdProc
*)sqlite_abort
},
6675 { "sqlite_bind", (Tcl_CmdProc
*)test_bind
},
6676 { "breakpoint", (Tcl_CmdProc
*)test_breakpoint
},
6677 { "sqlite3_key", (Tcl_CmdProc
*)test_key
},
6678 { "sqlite3_rekey", (Tcl_CmdProc
*)test_rekey
},
6679 { "sqlite_set_magic", (Tcl_CmdProc
*)sqlite_set_magic
},
6680 { "sqlite3_interrupt", (Tcl_CmdProc
*)test_interrupt
},
6681 { "sqlite_delete_function", (Tcl_CmdProc
*)delete_function
},
6682 { "sqlite_delete_collation", (Tcl_CmdProc
*)delete_collation
},
6683 { "sqlite3_get_autocommit", (Tcl_CmdProc
*)get_autocommit
},
6684 { "sqlite3_stack_used", (Tcl_CmdProc
*)test_stack_used
},
6685 { "sqlite3_busy_timeout", (Tcl_CmdProc
*)test_busy_timeout
},
6686 { "printf", (Tcl_CmdProc
*)test_printf
},
6687 { "sqlite3IoTrace", (Tcl_CmdProc
*)test_io_trace
},
6688 { "clang_sanitize_address", (Tcl_CmdProc
*)clang_sanitize_address
},
6692 Tcl_ObjCmdProc
*xProc
;
6695 { "sqlite3_connection_pointer", get_sqlite_pointer
, 0 },
6696 { "sqlite3_bind_int", test_bind_int
, 0 },
6697 { "sqlite3_bind_zeroblob", test_bind_zeroblob
, 0 },
6698 { "sqlite3_bind_int64", test_bind_int64
, 0 },
6699 { "sqlite3_bind_double", test_bind_double
, 0 },
6700 { "sqlite3_bind_null", test_bind_null
,0 },
6701 { "sqlite3_bind_text", test_bind_text
,0 },
6702 { "sqlite3_bind_text16", test_bind_text16
,0 },
6703 { "sqlite3_bind_blob", test_bind_blob
,0 },
6704 { "sqlite3_bind_parameter_count", test_bind_parameter_count
, 0},
6705 { "sqlite3_bind_parameter_name", test_bind_parameter_name
, 0},
6706 { "sqlite3_bind_parameter_index", test_bind_parameter_index
, 0},
6707 { "sqlite3_clear_bindings", test_clear_bindings
, 0},
6708 { "sqlite3_sleep", test_sleep
, 0},
6709 { "sqlite3_errcode", test_errcode
,0 },
6710 { "sqlite3_extended_errcode", test_ex_errcode
,0 },
6711 { "sqlite3_errmsg", test_errmsg
,0 },
6712 { "sqlite3_errmsg16", test_errmsg16
,0 },
6713 { "sqlite3_open", test_open
,0 },
6714 { "sqlite3_open16", test_open16
,0 },
6715 { "sqlite3_open_v2", test_open_v2
,0 },
6716 { "sqlite3_complete16", test_complete16
,0 },
6718 { "sqlite3_prepare", test_prepare
,0 },
6719 { "sqlite3_prepare16", test_prepare16
,0 },
6720 { "sqlite3_prepare_v2", test_prepare_v2
,0 },
6721 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134
, 0},
6722 { "sqlite3_prepare16_v2", test_prepare16_v2
,0 },
6723 { "sqlite3_finalize", test_finalize
,0 },
6724 { "sqlite3_stmt_status", test_stmt_status
,0 },
6725 { "sqlite3_reset", test_reset
,0 },
6726 { "sqlite3_expired", test_expired
,0 },
6727 { "sqlite3_transfer_bindings", test_transfer_bind
,0 },
6728 { "sqlite3_changes", test_changes
,0 },
6729 { "sqlite3_step", test_step
,0 },
6730 { "sqlite3_sql", test_sql
,0 },
6731 { "sqlite3_next_stmt", test_next_stmt
,0 },
6732 { "sqlite3_stmt_readonly", test_stmt_readonly
,0 },
6733 { "sqlite3_stmt_busy", test_stmt_busy
,0 },
6734 { "uses_stmt_journal", uses_stmt_journal
,0 },
6736 { "sqlite3_release_memory", test_release_memory
, 0},
6737 { "sqlite3_db_release_memory", test_db_release_memory
, 0},
6738 { "sqlite3_db_filename", test_db_filename
, 0},
6739 { "sqlite3_db_readonly", test_db_readonly
, 0},
6740 { "sqlite3_soft_heap_limit", test_soft_heap_limit
, 0},
6741 { "sqlite3_thread_cleanup", test_thread_cleanup
, 0},
6742 { "sqlite3_pager_refcounts", test_pager_refcounts
, 0},
6744 { "sqlite3_load_extension", test_load_extension
, 0},
6745 { "sqlite3_enable_load_extension", test_enable_load
, 0},
6746 { "sqlite3_extended_result_codes", test_extended_result_codes
, 0},
6747 { "sqlite3_limit", test_limit
, 0},
6749 { "save_prng_state", save_prng_state
, 0 },
6750 { "restore_prng_state", restore_prng_state
, 0 },
6751 { "reset_prng_state", reset_prng_state
, 0 },
6752 { "database_never_corrupt", database_never_corrupt
, 0},
6753 { "database_may_be_corrupt", database_may_be_corrupt
, 0},
6754 { "optimization_control", optimization_control
,0},
6756 { "lock_win32_file", win32_file_lock
, 0 },
6757 { "exists_win32_path", win32_exists_path
, 0 },
6758 { "find_win32_file", win32_find_file
, 0 },
6759 { "delete_win32_file", win32_delete_file
, 0 },
6760 { "make_win32_dir", win32_mkdir
, 0 },
6761 { "remove_win32_dir", win32_rmdir
, 0 },
6763 { "tcl_objproc", runAsObjProc
, 0 },
6765 /* sqlite3_column_*() API */
6766 { "sqlite3_column_count", test_column_count
,0 },
6767 { "sqlite3_data_count", test_data_count
,0 },
6768 { "sqlite3_column_type", test_column_type
,0 },
6769 { "sqlite3_column_blob", test_column_blob
,0 },
6770 { "sqlite3_column_double", test_column_double
,0 },
6771 { "sqlite3_column_int64", test_column_int64
,0 },
6772 { "sqlite3_column_text", test_stmt_utf8
, (void*)sqlite3_column_text
},
6773 { "sqlite3_column_name", test_stmt_utf8
, (void*)sqlite3_column_name
},
6774 { "sqlite3_column_int", test_stmt_int
, (void*)sqlite3_column_int
},
6775 { "sqlite3_column_bytes", test_stmt_int
, (void*)sqlite3_column_bytes
},
6776 #ifndef SQLITE_OMIT_DECLTYPE
6777 { "sqlite3_column_decltype",test_stmt_utf8
,(void*)sqlite3_column_decltype
},
6779 #ifdef SQLITE_ENABLE_COLUMN_METADATA
6780 { "sqlite3_column_database_name",test_stmt_utf8
,(void*)sqlite3_column_database_name
},
6781 { "sqlite3_column_table_name",test_stmt_utf8
,(void*)sqlite3_column_table_name
},
6782 { "sqlite3_column_origin_name",test_stmt_utf8
,(void*)sqlite3_column_origin_name
},
6785 #ifndef SQLITE_OMIT_UTF16
6786 { "sqlite3_column_bytes16", test_stmt_int
, (void*)sqlite3_column_bytes16
},
6787 { "sqlite3_column_text16", test_stmt_utf16
, (void*)sqlite3_column_text16
},
6788 { "sqlite3_column_name16", test_stmt_utf16
, (void*)sqlite3_column_name16
},
6789 { "add_alignment_test_collations", add_alignment_test_collations
, 0 },
6790 #ifndef SQLITE_OMIT_DECLTYPE
6791 { "sqlite3_column_decltype16",test_stmt_utf16
,(void*)sqlite3_column_decltype16
},
6793 #ifdef SQLITE_ENABLE_COLUMN_METADATA
6794 {"sqlite3_column_database_name16",
6795 test_stmt_utf16
, (void*)sqlite3_column_database_name16
},
6796 {"sqlite3_column_table_name16", test_stmt_utf16
, (void*)sqlite3_column_table_name16
},
6797 {"sqlite3_column_origin_name16", test_stmt_utf16
, (void*)sqlite3_column_origin_name16
},
6800 { "sqlite3_create_collation_v2", test_create_collation_v2
, 0 },
6801 { "sqlite3_global_recover", test_global_recover
, 0 },
6802 { "working_64bit_int", working_64bit_int
, 0 },
6803 { "vfs_unlink_test", vfs_unlink_test
, 0 },
6804 { "vfs_initfail_test", vfs_initfail_test
, 0 },
6805 { "vfs_unregister_all", vfs_unregister_all
, 0 },
6806 { "vfs_reregister_all", vfs_reregister_all
, 0 },
6807 { "file_control_test", file_control_test
, 0 },
6808 { "file_control_lasterrno_test", file_control_lasterrno_test
, 0 },
6809 { "file_control_lockproxy_test", file_control_lockproxy_test
, 0 },
6810 { "file_control_chunksize_test", file_control_chunksize_test
, 0 },
6811 { "file_control_sizehint_test", file_control_sizehint_test
, 0 },
6813 { "file_control_win32_av_retry", file_control_win32_av_retry
, 0 },
6814 { "file_control_win32_set_handle", file_control_win32_set_handle
, 0 },
6816 { "file_control_persist_wal", file_control_persist_wal
, 0 },
6817 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite
,0},
6818 { "file_control_vfsname", file_control_vfsname
, 0 },
6819 { "file_control_tempfilename", file_control_tempfilename
, 0 },
6820 { "sqlite3_vfs_list", vfs_list
, 0 },
6821 { "sqlite3_create_function_v2", test_create_function_v2
, 0 },
6823 /* Functions from os.h */
6824 #ifndef SQLITE_OMIT_UTF16
6825 { "add_test_collate", test_collate
, 0 },
6826 { "add_test_collate_needed", test_collate_needed
, 0 },
6827 { "add_test_function", test_function
, 0 },
6828 { "add_test_utf16bin_collate", test_utf16bin_collate
, 0 },
6830 { "sqlite3_test_errstr", test_errstr
, 0 },
6831 { "tcl_variable_type", tcl_variable_type
, 0 },
6832 #ifndef SQLITE_OMIT_SHARED_CACHE
6833 { "sqlite3_enable_shared_cache", test_enable_shared
, 0 },
6834 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport
, 0},
6836 { "sqlite3_libversion_number", test_libversion_number
, 0 },
6837 #ifdef SQLITE_ENABLE_COLUMN_METADATA
6838 { "sqlite3_table_column_metadata", test_table_column_metadata
, 0 },
6840 #ifndef SQLITE_OMIT_INCRBLOB
6841 { "sqlite3_blob_read", test_blob_read
, 0 },
6842 { "sqlite3_blob_write", test_blob_write
, 0 },
6843 { "sqlite3_blob_reopen", test_blob_reopen
, 0 },
6844 { "sqlite3_blob_bytes", test_blob_bytes
, 0 },
6845 { "sqlite3_blob_close", test_blob_close
, 0 },
6847 { "pcache_stats", test_pcache_stats
, 0 },
6848 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
6849 { "sqlite3_unlock_notify", test_unlock_notify
, 0 },
6851 { "sqlite3_wal_checkpoint", test_wal_checkpoint
, 0 },
6852 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2
, 0 },
6853 { "test_sqlite3_log", test_sqlite3_log
, 0 },
6854 #ifndef SQLITE_OMIT_EXPLAIN
6855 { "print_explain_query_plan", test_print_eqp
, 0 },
6857 { "sqlite3_test_control", test_test_control
},
6859 { "getrusage", test_getrusage
},
6861 { "load_static_extension", tclLoadStaticExtensionCmd
},
6862 { "sorter_test_fakeheap", sorter_test_fakeheap
},
6863 { "sorter_test_sort4_helper", sorter_test_sort4_helper
},
6864 #ifdef SQLITE_USER_AUTHENTICATION
6865 { "sqlite3_user_authenticate", test_user_authenticate
, 0 },
6866 { "sqlite3_user_add", test_user_add
, 0 },
6867 { "sqlite3_user_change", test_user_change
, 0 },
6868 { "sqlite3_user_delete", test_user_delete
, 0 },
6872 static int bitmask_size
= sizeof(Bitmask
)*8;
6874 extern int sqlite3_sync_count
, sqlite3_fullsync_count
;
6875 extern int sqlite3_opentemp_count
;
6876 extern int sqlite3_like_count
;
6877 extern int sqlite3_xferopt_count
;
6878 extern int sqlite3_pager_readdb_count
;
6879 extern int sqlite3_pager_writedb_count
;
6880 extern int sqlite3_pager_writej_count
;
6882 extern LONG
volatile sqlite3_os_type
;
6885 extern int sqlite3WhereTrace
;
6886 extern int sqlite3OSTrace
;
6887 extern int sqlite3WalTrace
;
6890 #ifdef SQLITE_ENABLE_FTS3
6891 extern int sqlite3_fts3_enable_parentheses
;
6895 for(i
=0; i
<sizeof(aCmd
)/sizeof(aCmd
[0]); i
++){
6896 Tcl_CreateCommand(interp
, aCmd
[i
].zName
, aCmd
[i
].xProc
, 0, 0);
6898 for(i
=0; i
<sizeof(aObjCmd
)/sizeof(aObjCmd
[0]); i
++){
6899 Tcl_CreateObjCommand(interp
, aObjCmd
[i
].zName
,
6900 aObjCmd
[i
].xProc
, aObjCmd
[i
].clientData
, 0);
6902 Tcl_LinkVar(interp
, "sqlite_search_count",
6903 (char*)&sqlite3_search_count
, TCL_LINK_INT
);
6904 Tcl_LinkVar(interp
, "sqlite_found_count",
6905 (char*)&sqlite3_found_count
, TCL_LINK_INT
);
6906 Tcl_LinkVar(interp
, "sqlite_sort_count",
6907 (char*)&sqlite3_sort_count
, TCL_LINK_INT
);
6908 Tcl_LinkVar(interp
, "sqlite3_max_blobsize",
6909 (char*)&sqlite3_max_blobsize
, TCL_LINK_INT
);
6910 Tcl_LinkVar(interp
, "sqlite_like_count",
6911 (char*)&sqlite3_like_count
, TCL_LINK_INT
);
6912 Tcl_LinkVar(interp
, "sqlite_interrupt_count",
6913 (char*)&sqlite3_interrupt_count
, TCL_LINK_INT
);
6914 Tcl_LinkVar(interp
, "sqlite_open_file_count",
6915 (char*)&sqlite3_open_file_count
, TCL_LINK_INT
);
6916 Tcl_LinkVar(interp
, "sqlite_current_time",
6917 (char*)&sqlite3_current_time
, TCL_LINK_INT
);
6918 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
6919 Tcl_LinkVar(interp
, "sqlite_hostid_num",
6920 (char*)&sqlite3_hostid_num
, TCL_LINK_INT
);
6922 Tcl_LinkVar(interp
, "sqlite3_xferopt_count",
6923 (char*)&sqlite3_xferopt_count
, TCL_LINK_INT
);
6924 Tcl_LinkVar(interp
, "sqlite3_pager_readdb_count",
6925 (char*)&sqlite3_pager_readdb_count
, TCL_LINK_INT
);
6926 Tcl_LinkVar(interp
, "sqlite3_pager_writedb_count",
6927 (char*)&sqlite3_pager_writedb_count
, TCL_LINK_INT
);
6928 Tcl_LinkVar(interp
, "sqlite3_pager_writej_count",
6929 (char*)&sqlite3_pager_writej_count
, TCL_LINK_INT
);
6930 #ifndef SQLITE_OMIT_UTF16
6931 Tcl_LinkVar(interp
, "unaligned_string_counter",
6932 (char*)&unaligned_string_counter
, TCL_LINK_INT
);
6934 #ifndef SQLITE_OMIT_UTF16
6935 Tcl_LinkVar(interp
, "sqlite_last_needed_collation",
6936 (char*)&pzNeededCollation
, TCL_LINK_STRING
|TCL_LINK_READ_ONLY
);
6939 Tcl_LinkVar(interp
, "sqlite_os_type",
6940 (char*)&sqlite3_os_type
, TCL_LINK_LONG
);
6944 static const char *query_plan
= "*** OBSOLETE VARIABLE ***";
6945 Tcl_LinkVar(interp
, "sqlite_query_plan",
6946 (char*)&query_plan
, TCL_LINK_STRING
|TCL_LINK_READ_ONLY
);
6950 Tcl_LinkVar(interp
, "sqlite_where_trace",
6951 (char*)&sqlite3WhereTrace
, TCL_LINK_INT
);
6952 Tcl_LinkVar(interp
, "sqlite_os_trace",
6953 (char*)&sqlite3OSTrace
, TCL_LINK_INT
);
6954 #ifndef SQLITE_OMIT_WAL
6955 Tcl_LinkVar(interp
, "sqlite_wal_trace",
6956 (char*)&sqlite3WalTrace
, TCL_LINK_INT
);
6959 #ifndef SQLITE_OMIT_DISKIO
6960 Tcl_LinkVar(interp
, "sqlite_opentemp_count",
6961 (char*)&sqlite3_opentemp_count
, TCL_LINK_INT
);
6963 Tcl_LinkVar(interp
, "sqlite_static_bind_value",
6964 (char*)&sqlite_static_bind_value
, TCL_LINK_STRING
);
6965 Tcl_LinkVar(interp
, "sqlite_static_bind_nbyte",
6966 (char*)&sqlite_static_bind_nbyte
, TCL_LINK_INT
);
6967 Tcl_LinkVar(interp
, "sqlite_temp_directory",
6968 (char*)&sqlite3_temp_directory
, TCL_LINK_STRING
);
6969 Tcl_LinkVar(interp
, "sqlite_data_directory",
6970 (char*)&sqlite3_data_directory
, TCL_LINK_STRING
);
6971 Tcl_LinkVar(interp
, "bitmask_size",
6972 (char*)&bitmask_size
, TCL_LINK_INT
|TCL_LINK_READ_ONLY
);
6973 Tcl_LinkVar(interp
, "sqlite_sync_count",
6974 (char*)&sqlite3_sync_count
, TCL_LINK_INT
);
6975 Tcl_LinkVar(interp
, "sqlite_fullsync_count",
6976 (char*)&sqlite3_fullsync_count
, TCL_LINK_INT
);
6977 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
6978 Tcl_LinkVar(interp
, "sqlite_fts3_enable_parentheses",
6979 (char*)&sqlite3_fts3_enable_parentheses
, TCL_LINK_INT
);