Enhance the string formatter (used by printf()) so that the width and
[sqlite.git] / src / test1.c
blobbc8f389dbde4a83ced0a716abf27c2fbe6196b6a
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
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"
17 #if SQLITE_OS_WIN
18 # include "os_win.h"
19 #endif
21 #include "vdbeInt.h"
22 #if defined(INCLUDE_SQLITE_TCL_H)
23 # include "sqlite_tcl.h"
24 #else
25 # include "tcl.h"
26 #endif
27 #include <stdlib.h>
28 #include <string.h>
31 ** This is a copy of the first part of the SqliteDb structure in
32 ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
33 ** can extract the sqlite3* pointer from an existing Tcl SQLite
34 ** connection.
36 struct SqliteDb {
37 sqlite3 *db;
41 ** Convert text generated by the "%p" conversion format back into
42 ** a pointer.
44 static int testHexToInt(int h){
45 if( h>='0' && h<='9' ){
46 return h - '0';
47 }else if( h>='a' && h<='f' ){
48 return h - 'a' + 10;
49 }else{
50 assert( h>='A' && h<='F' );
51 return h - 'A' + 10;
54 void *sqlite3TestTextToPtr(const char *z){
55 void *p;
56 u64 v;
57 u32 v2;
58 if( z[0]=='0' && z[1]=='x' ){
59 z += 2;
61 v = 0;
62 while( *z ){
63 v = (v<<4) + testHexToInt(*z);
64 z++;
66 if( sizeof(p)==sizeof(v) ){
67 memcpy(&p, &v, sizeof(p));
68 }else{
69 assert( sizeof(p)==sizeof(v2) );
70 v2 = (u32)v;
71 memcpy(&p, &v2, sizeof(p));
73 return p;
78 ** A TCL command that returns the address of the sqlite* pointer
79 ** for an sqlite connection instance. Bad things happen if the
80 ** input is not an sqlite connection.
82 static int SQLITE_TCLAPI get_sqlite_pointer(
83 void * clientData,
84 Tcl_Interp *interp,
85 int objc,
86 Tcl_Obj *CONST objv[]
88 struct SqliteDb *p;
89 Tcl_CmdInfo cmdInfo;
90 char zBuf[100];
91 if( objc!=2 ){
92 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
93 return TCL_ERROR;
95 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
96 Tcl_AppendResult(interp, "command not found: ",
97 Tcl_GetString(objv[1]), (char*)0);
98 return TCL_ERROR;
100 p = (struct SqliteDb*)cmdInfo.objClientData;
101 sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", p->db);
102 Tcl_AppendResult(interp, zBuf, 0);
103 return TCL_OK;
107 ** Decode a pointer to an sqlite3 object.
109 int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
110 struct SqliteDb *p;
111 Tcl_CmdInfo cmdInfo;
112 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
113 p = (struct SqliteDb*)cmdInfo.objClientData;
114 *ppDb = p->db;
115 }else{
116 *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
118 return TCL_OK;
121 #if SQLITE_OS_WIN
123 ** Decode a Win32 HANDLE object.
125 int getWin32Handle(Tcl_Interp *interp, const char *zA, LPHANDLE phFile){
126 *phFile = (HANDLE)sqlite3TestTextToPtr(zA);
127 return TCL_OK;
129 #endif
131 extern const char *sqlite3ErrName(int);
132 #define t1ErrorName sqlite3ErrName
135 ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
136 ** fact that the sqlite3* is the first field in the Vdbe structure.
138 #define StmtToDb(X) sqlite3_db_handle(X)
141 ** Check a return value to make sure it agrees with the results
142 ** from sqlite3_errcode.
144 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
145 if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK
146 && sqlite3_errcode(db)!=rc ){
147 char zBuf[200];
148 int r2 = sqlite3_errcode(db);
149 sqlite3_snprintf(sizeof(zBuf), zBuf,
150 "error code %s (%d) does not match sqlite3_errcode %s (%d)",
151 t1ErrorName(rc), rc, t1ErrorName(r2), r2);
152 Tcl_ResetResult(interp);
153 Tcl_AppendResult(interp, zBuf, 0);
154 return 1;
156 return 0;
160 ** Decode a pointer to an sqlite3_stmt object.
162 static int getStmtPointer(
163 Tcl_Interp *interp,
164 const char *zArg,
165 sqlite3_stmt **ppStmt
167 *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
168 return TCL_OK;
172 ** Generate a text representation of a pointer that can be understood
173 ** by the getDbPointer and getVmPointer routines above.
175 ** The problem is, on some machines (Solaris) if you do a printf with
176 ** "%p" you cannot turn around and do a scanf with the same "%p" and
177 ** get your pointer back. You have to prepend a "0x" before it will
178 ** work. Or at least that is what is reported to me (drh). But this
179 ** behavior varies from machine to machine. The solution used her is
180 ** to test the string right after it is generated to see if it can be
181 ** understood by scanf, and if not, try prepending an "0x" to see if
182 ** that helps. If nothing works, a fatal error is generated.
184 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
185 sqlite3_snprintf(100, zPtr, "%p", p);
186 return TCL_OK;
190 ** The callback routine for sqlite3_exec_printf().
192 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
193 Tcl_DString *str = (Tcl_DString*)pArg;
194 int i;
196 if( Tcl_DStringLength(str)==0 ){
197 for(i=0; i<argc; i++){
198 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
201 for(i=0; i<argc; i++){
202 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
204 return 0;
208 ** The I/O tracing callback.
210 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
211 static FILE *iotrace_file = 0;
212 static void io_trace_callback(const char *zFormat, ...){
213 va_list ap;
214 va_start(ap, zFormat);
215 vfprintf(iotrace_file, zFormat, ap);
216 va_end(ap);
217 fflush(iotrace_file);
219 #endif
222 ** Usage: io_trace FILENAME
224 ** Turn I/O tracing on or off. If FILENAME is not an empty string,
225 ** I/O tracing begins going into FILENAME. If FILENAME is an empty
226 ** string, I/O tracing is turned off.
228 static int SQLITE_TCLAPI test_io_trace(
229 void *NotUsed,
230 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
231 int argc, /* Number of arguments */
232 char **argv /* Text of each argument */
234 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
235 if( argc!=2 ){
236 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
237 " FILENAME\"", 0);
238 return TCL_ERROR;
240 if( iotrace_file ){
241 if( iotrace_file!=stdout && iotrace_file!=stderr ){
242 fclose(iotrace_file);
244 iotrace_file = 0;
245 sqlite3IoTrace = 0;
247 if( argv[1][0] ){
248 if( strcmp(argv[1],"stdout")==0 ){
249 iotrace_file = stdout;
250 }else if( strcmp(argv[1],"stderr")==0 ){
251 iotrace_file = stderr;
252 }else{
253 iotrace_file = fopen(argv[1], "w");
255 sqlite3IoTrace = io_trace_callback;
257 #endif
258 return TCL_OK;
262 ** Usage: clang_sanitize_address
264 ** Returns true if the program was compiled using clang with the
265 ** -fsanitize=address switch on the command line. False otherwise.
267 ** Also return true if the OMIT_MISUSE environment variable exists.
269 static int SQLITE_TCLAPI clang_sanitize_address(
270 void *NotUsed,
271 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
272 int argc, /* Number of arguments */
273 char **argv /* Text of each argument */
275 int res = 0;
276 #if defined(__has_feature)
277 # if __has_feature(address_sanitizer)
278 res = 1;
279 # endif
280 #endif
281 #ifdef __SANITIZE_ADDRESS__
282 res = 1;
283 #endif
284 if( res==0 && getenv("OMIT_MISUSE")!=0 ) res = 1;
285 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
286 return TCL_OK;
290 ** Usage: sqlite3_exec_printf DB FORMAT STRING
292 ** Invoke the sqlite3_exec_printf() interface using the open database
293 ** DB. The SQL is the string FORMAT. The format string should contain
294 ** one %s or %q. STRING is the value inserted into %s or %q.
296 static int SQLITE_TCLAPI test_exec_printf(
297 void *NotUsed,
298 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
299 int argc, /* Number of arguments */
300 char **argv /* Text of each argument */
302 sqlite3 *db;
303 Tcl_DString str;
304 int rc;
305 char *zErr = 0;
306 char *zSql;
307 char zBuf[30];
308 if( argc!=4 ){
309 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
310 " DB FORMAT STRING", 0);
311 return TCL_ERROR;
313 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
314 Tcl_DStringInit(&str);
315 zSql = sqlite3_mprintf(argv[2], argv[3]);
316 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
317 sqlite3_free(zSql);
318 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
319 Tcl_AppendElement(interp, zBuf);
320 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
321 Tcl_DStringFree(&str);
322 if( zErr ) sqlite3_free(zErr);
323 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
324 return TCL_OK;
328 ** Usage: sqlite3_exec_hex DB HEX
330 ** Invoke the sqlite3_exec() on a string that is obtained by translating
331 ** HEX into ASCII. Most characters are translated as is. %HH becomes
332 ** a hex character.
334 static int SQLITE_TCLAPI test_exec_hex(
335 void *NotUsed,
336 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
337 int argc, /* Number of arguments */
338 char **argv /* Text of each argument */
340 sqlite3 *db;
341 Tcl_DString str;
342 int rc, i, j;
343 char *zErr = 0;
344 char *zHex;
345 char zSql[501];
346 char zBuf[30];
347 if( argc!=3 ){
348 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
349 " DB HEX", 0);
350 return TCL_ERROR;
352 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
353 zHex = argv[2];
354 for(i=j=0; i<(sizeof(zSql)-1) && zHex[j]; i++, j++){
355 if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){
356 zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]);
357 j += 2;
358 }else{
359 zSql[i] = zHex[j];
362 zSql[i] = 0;
363 Tcl_DStringInit(&str);
364 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
365 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
366 Tcl_AppendElement(interp, zBuf);
367 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
368 Tcl_DStringFree(&str);
369 if( zErr ) sqlite3_free(zErr);
370 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
371 return TCL_OK;
375 ** Usage: db_enter DB
376 ** db_leave DB
378 ** Enter or leave the mutex on a database connection.
380 static int SQLITE_TCLAPI db_enter(
381 void *NotUsed,
382 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
383 int argc, /* Number of arguments */
384 char **argv /* Text of each argument */
386 sqlite3 *db;
387 if( argc!=2 ){
388 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
389 " DB", 0);
390 return TCL_ERROR;
392 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
393 sqlite3_mutex_enter(db->mutex);
394 return TCL_OK;
396 static int SQLITE_TCLAPI db_leave(
397 void *NotUsed,
398 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
399 int argc, /* Number of arguments */
400 char **argv /* Text of each argument */
402 sqlite3 *db;
403 if( argc!=2 ){
404 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
405 " DB", 0);
406 return TCL_ERROR;
408 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
409 sqlite3_mutex_leave(db->mutex);
410 return TCL_OK;
414 ** Usage: sqlite3_exec DB SQL
416 ** Invoke the sqlite3_exec interface using the open database DB
418 static int SQLITE_TCLAPI test_exec(
419 void *NotUsed,
420 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
421 int argc, /* Number of arguments */
422 char **argv /* Text of each argument */
424 sqlite3 *db;
425 Tcl_DString str;
426 int rc;
427 char *zErr = 0;
428 char *zSql;
429 int i, j;
430 char zBuf[30];
431 if( argc!=3 ){
432 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
433 " DB SQL", 0);
434 return TCL_ERROR;
436 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
437 Tcl_DStringInit(&str);
438 zSql = sqlite3_mprintf("%s", argv[2]);
439 for(i=j=0; zSql[i];){
440 if( zSql[i]=='%' ){
441 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
442 i += 3;
443 }else{
444 zSql[j++] = zSql[i++];
447 zSql[j] = 0;
448 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
449 sqlite3_free(zSql);
450 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
451 Tcl_AppendElement(interp, zBuf);
452 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
453 Tcl_DStringFree(&str);
454 if( zErr ) sqlite3_free(zErr);
455 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
456 return TCL_OK;
460 ** Usage: sqlite3_exec_nr DB SQL
462 ** Invoke the sqlite3_exec interface using the open database DB. Discard
463 ** all results
465 static int SQLITE_TCLAPI test_exec_nr(
466 void *NotUsed,
467 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
468 int argc, /* Number of arguments */
469 char **argv /* Text of each argument */
471 sqlite3 *db;
472 int rc;
473 char *zErr = 0;
474 if( argc!=3 ){
475 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
476 " DB SQL", 0);
477 return TCL_ERROR;
479 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
480 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
481 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
482 return TCL_OK;
486 ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
488 ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
489 ** concatenate arg0 through argn using separator as the separator.
490 ** Return the result.
492 static int SQLITE_TCLAPI test_mprintf_z(
493 void *NotUsed,
494 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
495 int argc, /* Number of arguments */
496 char **argv /* Text of each argument */
498 char *zResult = 0;
499 int i;
501 for(i=2; i<argc && (i==2 || zResult); i++){
502 zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]);
504 Tcl_AppendResult(interp, zResult, 0);
505 sqlite3_free(zResult);
506 return TCL_OK;
510 ** Usage: sqlite3_mprintf_n_test STRING
512 ** Test the %n format of sqlite_mprintf(). Return the length of the
513 ** input string.
515 static int SQLITE_TCLAPI test_mprintf_n(
516 void *NotUsed,
517 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
518 int argc, /* Number of arguments */
519 char **argv /* Text of each argument */
521 char *zStr;
522 int n = 0;
523 zStr = sqlite3_mprintf("%s%n", argv[1], &n);
524 sqlite3_free(zStr);
525 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
526 return TCL_OK;
530 ** Usage: sqlite3_snprintf_int SIZE FORMAT INT
532 ** Test the of sqlite3_snprintf() routine. SIZE is the size of the
533 ** output buffer in bytes. The maximum size is 100. FORMAT is the
534 ** format string. INT is a single integer argument. The FORMAT
535 ** string must require no more than this one integer argument. If
536 ** You pass in a format string that requires more than one argument,
537 ** bad things will happen.
539 static int SQLITE_TCLAPI test_snprintf_int(
540 void *NotUsed,
541 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
542 int argc, /* Number of arguments */
543 char **argv /* Text of each argument */
545 char zStr[100];
546 int n = atoi(argv[1]);
547 const char *zFormat = argv[2];
548 int a1 = atoi(argv[3]);
549 if( n>sizeof(zStr) ) n = sizeof(zStr);
550 sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
551 sqlite3_snprintf(n, zStr, zFormat, a1);
552 Tcl_AppendResult(interp, zStr, 0);
553 return TCL_OK;
556 #ifndef SQLITE_OMIT_GET_TABLE
559 ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
561 ** Invoke the sqlite3_get_table_printf() interface using the open database
562 ** DB. The SQL is the string FORMAT. The format string should contain
563 ** one %s or %q. STRING is the value inserted into %s or %q.
565 static int SQLITE_TCLAPI test_get_table_printf(
566 void *NotUsed,
567 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
568 int argc, /* Number of arguments */
569 char **argv /* Text of each argument */
571 sqlite3 *db;
572 Tcl_DString str;
573 int rc;
574 char *zErr = 0;
575 int nRow = 0, nCol = 0;
576 char **aResult;
577 int i;
578 char zBuf[30];
579 char *zSql;
580 int resCount = -1;
581 if( argc==5 ){
582 if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
584 if( argc!=4 && argc!=5 ){
585 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
586 " DB FORMAT STRING ?COUNT?", 0);
587 return TCL_ERROR;
589 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
590 Tcl_DStringInit(&str);
591 zSql = sqlite3_mprintf(argv[2],argv[3]);
592 if( argc==5 ){
593 rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
594 }else{
595 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
596 resCount = (nRow+1)*nCol;
598 sqlite3_free(zSql);
599 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
600 Tcl_AppendElement(interp, zBuf);
601 if( rc==SQLITE_OK ){
602 if( argc==4 ){
603 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nRow);
604 Tcl_AppendElement(interp, zBuf);
605 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nCol);
606 Tcl_AppendElement(interp, zBuf);
608 for(i=0; i<resCount; i++){
609 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
611 }else{
612 Tcl_AppendElement(interp, zErr);
614 sqlite3_free_table(aResult);
615 if( zErr ) sqlite3_free(zErr);
616 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
617 return TCL_OK;
620 #endif /* SQLITE_OMIT_GET_TABLE */
624 ** Usage: sqlite3_last_insert_rowid DB
626 ** Returns the integer ROWID of the most recent insert.
628 static int SQLITE_TCLAPI test_last_rowid(
629 void *NotUsed,
630 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
631 int argc, /* Number of arguments */
632 char **argv /* Text of each argument */
634 sqlite3 *db;
635 char zBuf[30];
637 if( argc!=2 ){
638 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
639 return TCL_ERROR;
641 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
642 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", sqlite3_last_insert_rowid(db));
643 Tcl_AppendResult(interp, zBuf, 0);
644 return SQLITE_OK;
648 ** Usage: sqlite3_key DB KEY
650 ** Set the codec key.
652 static int SQLITE_TCLAPI test_key(
653 void *NotUsed,
654 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
655 int argc, /* Number of arguments */
656 char **argv /* Text of each argument */
658 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
659 sqlite3 *db;
660 const char *zKey;
661 int nKey;
662 if( argc!=3 ){
663 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
664 " FILENAME\"", 0);
665 return TCL_ERROR;
667 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
668 zKey = argv[2];
669 nKey = strlen(zKey);
670 sqlite3_key(db, zKey, nKey);
671 #endif
672 return TCL_OK;
676 ** Usage: sqlite3_rekey DB KEY
678 ** Change the codec key.
680 static int SQLITE_TCLAPI test_rekey(
681 void *NotUsed,
682 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
683 int argc, /* Number of arguments */
684 char **argv /* Text of each argument */
686 #ifdef SQLITE_HAS_CODEC
687 sqlite3 *db;
688 const char *zKey;
689 int nKey;
690 if( argc!=3 ){
691 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
692 " FILENAME\"", 0);
693 return TCL_ERROR;
695 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
696 zKey = argv[2];
697 nKey = strlen(zKey);
698 sqlite3_rekey(db, zKey, nKey);
699 #endif
700 return TCL_OK;
704 ** Usage: sqlite3_close DB
706 ** Closes the database opened by sqlite3_open.
708 static int SQLITE_TCLAPI sqlite_test_close(
709 void *NotUsed,
710 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
711 int argc, /* Number of arguments */
712 char **argv /* Text of each argument */
714 sqlite3 *db;
715 int rc;
716 if( argc!=2 ){
717 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
718 " FILENAME\"", 0);
719 return TCL_ERROR;
721 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
722 rc = sqlite3_close(db);
723 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
724 return TCL_OK;
728 ** Usage: sqlite3_close_v2 DB
730 ** Closes the database opened by sqlite3_open.
732 static int SQLITE_TCLAPI sqlite_test_close_v2(
733 void *NotUsed,
734 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
735 int argc, /* Number of arguments */
736 char **argv /* Text of each argument */
738 sqlite3 *db;
739 int rc;
740 if( argc!=2 ){
741 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
742 " FILENAME\"", 0);
743 return TCL_ERROR;
745 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
746 rc = sqlite3_close_v2(db);
747 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
748 return TCL_OK;
752 ** Implementation of the x_coalesce() function.
753 ** Return the first argument non-NULL argument.
755 static void t1_ifnullFunc(
756 sqlite3_context *context,
757 int argc,
758 sqlite3_value **argv
760 int i;
761 for(i=0; i<argc; i++){
762 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
763 int n = sqlite3_value_bytes(argv[i]);
764 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
765 n, SQLITE_TRANSIENT);
766 break;
772 ** These are test functions. hex8() interprets its argument as
773 ** UTF8 and returns a hex encoding. hex16le() interprets its argument
774 ** as UTF16le and returns a hex encoding.
776 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
777 const unsigned char *z;
778 int i;
779 char zBuf[200];
780 z = sqlite3_value_text(argv[0]);
781 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
782 sqlite3_snprintf(sizeof(zBuf)-i*2, &zBuf[i*2], "%02x", z[i]);
784 zBuf[i*2] = 0;
785 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
787 #ifndef SQLITE_OMIT_UTF16
788 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
789 const unsigned short int *z;
790 int i;
791 char zBuf[400];
792 z = sqlite3_value_text16(argv[0]);
793 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
794 sqlite3_snprintf(sizeof(zBuf)-i*4, &zBuf[i*4],"%04x", z[i]&0xff);
796 zBuf[i*4] = 0;
797 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
799 #endif
802 ** A structure into which to accumulate text.
804 struct dstr {
805 int nAlloc; /* Space allocated */
806 int nUsed; /* Space used */
807 char *z; /* The space */
811 ** Append text to a dstr
813 static void dstrAppend(struct dstr *p, const char *z, int divider){
814 int n = (int)strlen(z);
815 if( p->nUsed + n + 2 > p->nAlloc ){
816 char *zNew;
817 p->nAlloc = p->nAlloc*2 + n + 200;
818 zNew = sqlite3_realloc(p->z, p->nAlloc);
819 if( zNew==0 ){
820 sqlite3_free(p->z);
821 memset(p, 0, sizeof(*p));
822 return;
824 p->z = zNew;
826 if( divider && p->nUsed>0 ){
827 p->z[p->nUsed++] = divider;
829 memcpy(&p->z[p->nUsed], z, n+1);
830 p->nUsed += n;
834 ** Invoked for each callback from sqlite3ExecFunc
836 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
837 struct dstr *p = (struct dstr*)pData;
838 int i;
839 for(i=0; i<argc; i++){
840 if( argv[i]==0 ){
841 dstrAppend(p, "NULL", ' ');
842 }else{
843 dstrAppend(p, argv[i], ' ');
846 return 0;
850 ** Implementation of the x_sqlite_exec() function. This function takes
851 ** a single argument and attempts to execute that argument as SQL code.
852 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
854 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
855 ** from within a function call.
857 ** This routine simulates the effect of having two threads attempt to
858 ** use the same database at the same time.
860 static void sqlite3ExecFunc(
861 sqlite3_context *context,
862 int argc,
863 sqlite3_value **argv
865 struct dstr x;
866 memset(&x, 0, sizeof(x));
867 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
868 (char*)sqlite3_value_text(argv[0]),
869 execFuncCallback, &x, 0);
870 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
871 sqlite3_free(x.z);
875 ** Implementation of tkt2213func(), a scalar function that takes exactly
876 ** one argument. It has two interesting features:
878 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
879 ** If the three pointers returned are not the same an SQL error is raised.
881 ** * Otherwise it returns a copy of the text representation of its
882 ** argument in such a way as the VDBE representation is a Mem* cell
883 ** with the MEM_Term flag clear.
885 ** Ticket #2213 can therefore be tested by evaluating the following
886 ** SQL expression:
888 ** tkt2213func(tkt2213func('a string'));
890 static void tkt2213Function(
891 sqlite3_context *context,
892 int argc,
893 sqlite3_value **argv
895 int nText;
896 unsigned char const *zText1;
897 unsigned char const *zText2;
898 unsigned char const *zText3;
900 nText = sqlite3_value_bytes(argv[0]);
901 zText1 = sqlite3_value_text(argv[0]);
902 zText2 = sqlite3_value_text(argv[0]);
903 zText3 = sqlite3_value_text(argv[0]);
905 if( zText1!=zText2 || zText2!=zText3 ){
906 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
907 }else{
908 char *zCopy = (char *)sqlite3_malloc(nText);
909 memcpy(zCopy, zText1, nText);
910 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
915 ** The following SQL function takes 4 arguments. The 2nd and
916 ** 4th argument must be one of these strings: 'text', 'text16',
917 ** or 'blob' corresponding to API functions
919 ** sqlite3_value_text()
920 ** sqlite3_value_text16()
921 ** sqlite3_value_blob()
923 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
924 ** corresponding to APIs:
926 ** sqlite3_value_bytes()
927 ** sqlite3_value_bytes16()
928 ** noop
930 ** The APIs designated by the 2nd through 4th arguments are applied
931 ** to the first argument in order. If the pointers returned by the
932 ** second and fourth are different, this routine returns 1. Otherwise,
933 ** this routine returns 0.
935 ** This function is used to test to see when returned pointers from
936 ** the _text(), _text16() and _blob() APIs become invalidated.
938 static void ptrChngFunction(
939 sqlite3_context *context,
940 int argc,
941 sqlite3_value **argv
943 const void *p1, *p2;
944 const char *zCmd;
945 if( argc!=4 ) return;
946 zCmd = (const char*)sqlite3_value_text(argv[1]);
947 if( zCmd==0 ) return;
948 if( strcmp(zCmd,"text")==0 ){
949 p1 = (const void*)sqlite3_value_text(argv[0]);
950 #ifndef SQLITE_OMIT_UTF16
951 }else if( strcmp(zCmd, "text16")==0 ){
952 p1 = (const void*)sqlite3_value_text16(argv[0]);
953 #endif
954 }else if( strcmp(zCmd, "blob")==0 ){
955 p1 = (const void*)sqlite3_value_blob(argv[0]);
956 }else{
957 return;
959 zCmd = (const char*)sqlite3_value_text(argv[2]);
960 if( zCmd==0 ) return;
961 if( strcmp(zCmd,"bytes")==0 ){
962 sqlite3_value_bytes(argv[0]);
963 #ifndef SQLITE_OMIT_UTF16
964 }else if( strcmp(zCmd, "bytes16")==0 ){
965 sqlite3_value_bytes16(argv[0]);
966 #endif
967 }else if( strcmp(zCmd, "noop")==0 ){
968 /* do nothing */
969 }else{
970 return;
972 zCmd = (const char*)sqlite3_value_text(argv[3]);
973 if( zCmd==0 ) return;
974 if( strcmp(zCmd,"text")==0 ){
975 p2 = (const void*)sqlite3_value_text(argv[0]);
976 #ifndef SQLITE_OMIT_UTF16
977 }else if( strcmp(zCmd, "text16")==0 ){
978 p2 = (const void*)sqlite3_value_text16(argv[0]);
979 #endif
980 }else if( strcmp(zCmd, "blob")==0 ){
981 p2 = (const void*)sqlite3_value_blob(argv[0]);
982 }else{
983 return;
985 sqlite3_result_int(context, p1!=p2);
989 ** This SQL function returns a different answer each time it is called, even if
990 ** the arguments are the same.
992 static void nondeterministicFunction(
993 sqlite3_context *context,
994 int argc,
995 sqlite3_value **argv
997 static int cnt = 0;
998 sqlite3_result_int(context, cnt++);
1002 ** Usage: sqlite3_create_function DB
1004 ** Call the sqlite3_create_function API on the given database in order
1005 ** to create a function named "x_coalesce". This function does the same thing
1006 ** as the "coalesce" function. This function also registers an SQL function
1007 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
1008 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
1009 ** The effect is similar to trying to use the same database connection from
1010 ** two threads at the same time.
1012 ** The original motivation for this routine was to be able to call the
1013 ** sqlite3_create_function function while a query is in progress in order
1014 ** to test the SQLITE_MISUSE detection logic.
1016 static int SQLITE_TCLAPI test_create_function(
1017 void *NotUsed,
1018 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1019 int argc, /* Number of arguments */
1020 char **argv /* Text of each argument */
1022 int rc;
1023 sqlite3 *db;
1025 if( argc!=2 ){
1026 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1027 " DB\"", 0);
1028 return TCL_ERROR;
1030 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1031 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0,
1032 t1_ifnullFunc, 0, 0);
1033 if( rc==SQLITE_OK ){
1034 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1035 0, hex8Func, 0, 0);
1037 #ifndef SQLITE_OMIT_UTF16
1038 if( rc==SQLITE_OK ){
1039 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
1040 0, hex16Func, 0, 0);
1042 #endif
1043 if( rc==SQLITE_OK ){
1044 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
1045 tkt2213Function, 0, 0);
1047 if( rc==SQLITE_OK ){
1048 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
1049 ptrChngFunction, 0, 0);
1052 /* Functions counter1() and counter2() have the same implementation - they
1053 ** both return an ascending integer with each call. But counter1() is marked
1054 ** as non-deterministic and counter2() is marked as deterministic.
1056 if( rc==SQLITE_OK ){
1057 rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
1058 0, nondeterministicFunction, 0, 0);
1060 if( rc==SQLITE_OK ){
1061 rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
1062 0, nondeterministicFunction, 0, 0);
1065 #ifndef SQLITE_OMIT_UTF16
1066 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1067 ** because it is not tested anywhere else. */
1068 if( rc==SQLITE_OK ){
1069 const void *zUtf16;
1070 sqlite3_value *pVal;
1071 sqlite3_mutex_enter(db->mutex);
1072 pVal = sqlite3ValueNew(db);
1073 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
1074 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1075 if( db->mallocFailed ){
1076 rc = SQLITE_NOMEM;
1077 }else{
1078 rc = sqlite3_create_function16(db, zUtf16,
1079 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
1081 sqlite3ValueFree(pVal);
1082 sqlite3_mutex_leave(db->mutex);
1084 #endif
1086 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1087 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1088 return TCL_OK;
1092 ** Routines to implement the x_count() aggregate function.
1094 ** x_count() counts the number of non-null arguments. But there are
1095 ** some twists for testing purposes.
1097 ** If the argument to x_count() is 40 then a UTF-8 error is reported
1098 ** on the step function. If x_count(41) is seen, then a UTF-16 error
1099 ** is reported on the step function. If the total count is 42, then
1100 ** a UTF-8 error is reported on the finalize function.
1102 typedef struct t1CountCtx t1CountCtx;
1103 struct t1CountCtx {
1104 int n;
1106 static void t1CountStep(
1107 sqlite3_context *context,
1108 int argc,
1109 sqlite3_value **argv
1111 t1CountCtx *p;
1112 p = sqlite3_aggregate_context(context, sizeof(*p));
1113 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
1114 p->n++;
1116 if( argc>0 ){
1117 int v = sqlite3_value_int(argv[0]);
1118 if( v==40 ){
1119 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
1120 #ifndef SQLITE_OMIT_UTF16
1121 }else if( v==41 ){
1122 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1123 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
1124 #endif
1128 static void t1CountFinalize(sqlite3_context *context){
1129 t1CountCtx *p;
1130 p = sqlite3_aggregate_context(context, sizeof(*p));
1131 if( p ){
1132 if( p->n==42 ){
1133 sqlite3_result_error(context, "x_count totals to 42", -1);
1134 }else{
1135 sqlite3_result_int(context, p ? p->n : 0);
1140 #ifndef SQLITE_OMIT_DEPRECATED
1141 static void legacyCountStep(
1142 sqlite3_context *context,
1143 int argc,
1144 sqlite3_value **argv
1146 /* no-op */
1149 static void legacyCountFinalize(sqlite3_context *context){
1150 sqlite3_result_int(context, sqlite3_aggregate_count(context));
1152 #endif
1155 ** Usage: sqlite3_create_aggregate DB
1157 ** Call the sqlite3_create_function API on the given database in order
1158 ** to create a function named "x_count". This function is similar
1159 ** to the built-in count() function, with a few special quirks
1160 ** for testing the sqlite3_result_error() APIs.
1162 ** The original motivation for this routine was to be able to call the
1163 ** sqlite3_create_aggregate function while a query is in progress in order
1164 ** to test the SQLITE_MISUSE detection logic. See misuse.test.
1166 ** This routine was later extended to test the use of sqlite3_result_error()
1167 ** within aggregate functions.
1169 ** Later: It is now also extended to register the aggregate function
1170 ** "legacy_count()" with the supplied database handle. This is used
1171 ** to test the deprecated sqlite3_aggregate_count() API.
1173 static int SQLITE_TCLAPI test_create_aggregate(
1174 void *NotUsed,
1175 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1176 int argc, /* Number of arguments */
1177 char **argv /* Text of each argument */
1179 sqlite3 *db;
1180 int rc;
1181 if( argc!=2 ){
1182 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1183 " FILENAME\"", 0);
1184 return TCL_ERROR;
1186 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1187 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
1188 t1CountStep,t1CountFinalize);
1189 if( rc==SQLITE_OK ){
1190 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
1191 t1CountStep,t1CountFinalize);
1193 #ifndef SQLITE_OMIT_DEPRECATED
1194 if( rc==SQLITE_OK ){
1195 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1196 legacyCountStep, legacyCountFinalize
1199 #endif
1200 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1201 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1202 return TCL_OK;
1207 ** Usage: printf TEXT
1209 ** Send output to printf. Use this rather than puts to merge the output
1210 ** in the correct sequence with debugging printfs inserted into C code.
1211 ** Puts uses a separate buffer and debugging statements will be out of
1212 ** sequence if it is used.
1214 static int SQLITE_TCLAPI test_printf(
1215 void *NotUsed,
1216 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1217 int argc, /* Number of arguments */
1218 char **argv /* Text of each argument */
1220 if( argc!=2 ){
1221 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1222 " TEXT\"", 0);
1223 return TCL_ERROR;
1225 printf("%s\n", argv[1]);
1226 return TCL_OK;
1232 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1234 ** Call mprintf with three integer arguments
1236 static int SQLITE_TCLAPI sqlite3_mprintf_int(
1237 void *NotUsed,
1238 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1239 int argc, /* Number of arguments */
1240 char **argv /* Text of each argument */
1242 int a[3], i;
1243 char *z;
1244 if( argc!=5 ){
1245 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1246 " FORMAT INT INT INT\"", 0);
1247 return TCL_ERROR;
1249 for(i=2; i<5; i++){
1250 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1252 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1253 Tcl_AppendResult(interp, z, 0);
1254 sqlite3_free(z);
1255 return TCL_OK;
1259 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1261 ** Call mprintf with three 64-bit integer arguments
1263 static int SQLITE_TCLAPI sqlite3_mprintf_int64(
1264 void *NotUsed,
1265 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1266 int argc, /* Number of arguments */
1267 char **argv /* Text of each argument */
1269 int i;
1270 sqlite_int64 a[3];
1271 char *z;
1272 if( argc!=5 ){
1273 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1274 " FORMAT INT INT INT\"", 0);
1275 return TCL_ERROR;
1277 for(i=2; i<5; i++){
1278 if( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){
1279 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1280 return TCL_ERROR;
1283 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1284 Tcl_AppendResult(interp, z, 0);
1285 sqlite3_free(z);
1286 return TCL_OK;
1290 ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1292 ** Call mprintf with three long integer arguments. This might be the
1293 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1294 ** platform.
1296 static int SQLITE_TCLAPI sqlite3_mprintf_long(
1297 void *NotUsed,
1298 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1299 int argc, /* Number of arguments */
1300 char **argv /* Text of each argument */
1302 int i;
1303 long int a[3];
1304 int b[3];
1305 char *z;
1306 if( argc!=5 ){
1307 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1308 " FORMAT INT INT INT\"", 0);
1309 return TCL_ERROR;
1311 for(i=2; i<5; i++){
1312 if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
1313 a[i-2] = (long int)b[i-2];
1314 a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
1316 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1317 Tcl_AppendResult(interp, z, 0);
1318 sqlite3_free(z);
1319 return TCL_OK;
1323 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1325 ** Call mprintf with two integer arguments and one string argument
1327 static int SQLITE_TCLAPI sqlite3_mprintf_str(
1328 void *NotUsed,
1329 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1330 int argc, /* Number of arguments */
1331 char **argv /* Text of each argument */
1333 int a[3], i;
1334 char *z;
1335 if( argc<4 || argc>5 ){
1336 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1337 " FORMAT INT INT ?STRING?\"", 0);
1338 return TCL_ERROR;
1340 for(i=2; i<4; i++){
1341 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1343 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1344 Tcl_AppendResult(interp, z, 0);
1345 sqlite3_free(z);
1346 return TCL_OK;
1350 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1352 ** Call mprintf with two integer arguments and one string argument
1354 static int SQLITE_TCLAPI sqlite3_snprintf_str(
1355 void *NotUsed,
1356 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1357 int argc, /* Number of arguments */
1358 char **argv /* Text of each argument */
1360 int a[3], i;
1361 int n;
1362 char *z;
1363 if( argc<5 || argc>6 ){
1364 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1365 " INT FORMAT INT INT ?STRING?\"", 0);
1366 return TCL_ERROR;
1368 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1369 if( n<0 ){
1370 Tcl_AppendResult(interp, "N must be non-negative", 0);
1371 return TCL_ERROR;
1373 for(i=3; i<5; i++){
1374 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1376 z = sqlite3_malloc( n+1 );
1377 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1378 Tcl_AppendResult(interp, z, 0);
1379 sqlite3_free(z);
1380 return TCL_OK;
1384 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1386 ** Call mprintf with two integer arguments and one double argument
1388 static int SQLITE_TCLAPI sqlite3_mprintf_double(
1389 void *NotUsed,
1390 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1391 int argc, /* Number of arguments */
1392 char **argv /* Text of each argument */
1394 int a[3], i;
1395 double r;
1396 char *z;
1397 if( argc!=5 ){
1398 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1399 " FORMAT INT INT DOUBLE\"", 0);
1400 return TCL_ERROR;
1402 for(i=2; i<4; i++){
1403 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1405 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1406 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1407 Tcl_AppendResult(interp, z, 0);
1408 sqlite3_free(z);
1409 return TCL_OK;
1413 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1415 ** Call mprintf with a single double argument which is the product of the
1416 ** two arguments given above. This is used to generate overflow and underflow
1417 ** doubles to test that they are converted properly.
1419 static int SQLITE_TCLAPI sqlite3_mprintf_scaled(
1420 void *NotUsed,
1421 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1422 int argc, /* Number of arguments */
1423 char **argv /* Text of each argument */
1425 int i;
1426 double r[2];
1427 char *z;
1428 if( argc!=4 ){
1429 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1430 " FORMAT DOUBLE DOUBLE\"", 0);
1431 return TCL_ERROR;
1433 for(i=2; i<4; i++){
1434 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1436 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1437 Tcl_AppendResult(interp, z, 0);
1438 sqlite3_free(z);
1439 return TCL_OK;
1443 ** Usage: sqlite3_mprintf_stronly FORMAT STRING
1445 ** Call mprintf with a single double argument which is the product of the
1446 ** two arguments given above. This is used to generate overflow and underflow
1447 ** doubles to test that they are converted properly.
1449 static int SQLITE_TCLAPI sqlite3_mprintf_stronly(
1450 void *NotUsed,
1451 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1452 int argc, /* Number of arguments */
1453 char **argv /* Text of each argument */
1455 char *z;
1456 if( argc!=3 ){
1457 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1458 " FORMAT STRING\"", 0);
1459 return TCL_ERROR;
1461 z = sqlite3_mprintf(argv[1], argv[2]);
1462 Tcl_AppendResult(interp, z, 0);
1463 sqlite3_free(z);
1464 return TCL_OK;
1468 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1470 ** Call mprintf with a single double argument which is derived from the
1471 ** hexadecimal encoding of an IEEE double.
1473 static int SQLITE_TCLAPI sqlite3_mprintf_hexdouble(
1474 void *NotUsed,
1475 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1476 int argc, /* Number of arguments */
1477 char **argv /* Text of each argument */
1479 char *z;
1480 double r;
1481 unsigned int x1, x2;
1482 sqlite_uint64 d;
1483 if( argc!=3 ){
1484 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1485 " FORMAT STRING\"", 0);
1486 return TCL_ERROR;
1488 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1489 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1490 return TCL_ERROR;
1492 d = x2;
1493 d = (d<<32) + x1;
1494 memcpy(&r, &d, sizeof(r));
1495 z = sqlite3_mprintf(argv[1], r);
1496 Tcl_AppendResult(interp, z, 0);
1497 sqlite3_free(z);
1498 return TCL_OK;
1502 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1505 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1506 static int SQLITE_TCLAPI test_enable_shared(
1507 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1508 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1509 int objc, /* Number of arguments */
1510 Tcl_Obj *CONST objv[] /* Command arguments */
1512 int rc;
1513 int enable;
1514 int ret = 0;
1516 if( objc!=2 && objc!=1 ){
1517 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
1518 return TCL_ERROR;
1520 ret = sqlite3GlobalConfig.sharedCacheEnabled;
1522 if( objc==2 ){
1523 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1524 return TCL_ERROR;
1526 rc = sqlite3_enable_shared_cache(enable);
1527 if( rc!=SQLITE_OK ){
1528 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1529 return TCL_ERROR;
1532 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1533 return TCL_OK;
1535 #endif
1540 ** Usage: sqlite3_extended_result_codes DB BOOLEAN
1543 static int SQLITE_TCLAPI test_extended_result_codes(
1544 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1545 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1546 int objc, /* Number of arguments */
1547 Tcl_Obj *CONST objv[] /* Command arguments */
1549 int enable;
1550 sqlite3 *db;
1552 if( objc!=3 ){
1553 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1554 return TCL_ERROR;
1556 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1557 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1558 sqlite3_extended_result_codes(db, enable);
1559 return TCL_OK;
1563 ** Usage: sqlite3_libversion_number
1566 static int SQLITE_TCLAPI test_libversion_number(
1567 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1568 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1569 int objc, /* Number of arguments */
1570 Tcl_Obj *CONST objv[] /* Command arguments */
1572 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1573 return TCL_OK;
1577 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1580 static int SQLITE_TCLAPI test_table_column_metadata(
1581 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1582 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1583 int objc, /* Number of arguments */
1584 Tcl_Obj *CONST objv[] /* Command arguments */
1586 sqlite3 *db;
1587 const char *zDb;
1588 const char *zTbl;
1589 const char *zCol;
1590 int rc;
1591 Tcl_Obj *pRet;
1593 const char *zDatatype;
1594 const char *zCollseq;
1595 int notnull;
1596 int primarykey;
1597 int autoincrement;
1599 if( objc!=5 && objc!=4 ){
1600 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1601 return TCL_ERROR;
1603 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1604 zDb = Tcl_GetString(objv[2]);
1605 zTbl = Tcl_GetString(objv[3]);
1606 zCol = objc==5 ? Tcl_GetString(objv[4]) : 0;
1608 if( strlen(zDb)==0 ) zDb = 0;
1610 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1611 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1613 if( rc!=SQLITE_OK ){
1614 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1615 return TCL_ERROR;
1618 pRet = Tcl_NewObj();
1619 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1620 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1621 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1622 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1623 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1624 Tcl_SetObjResult(interp, pRet);
1626 return TCL_OK;
1629 #ifndef SQLITE_OMIT_INCRBLOB
1631 static int SQLITE_TCLAPI blobHandleFromObj(
1632 Tcl_Interp *interp,
1633 Tcl_Obj *pObj,
1634 sqlite3_blob **ppBlob
1636 char *z;
1637 int n;
1639 z = Tcl_GetStringFromObj(pObj, &n);
1640 if( n==0 ){
1641 *ppBlob = 0;
1642 }else{
1643 int notUsed;
1644 Tcl_Channel channel;
1645 ClientData instanceData;
1647 channel = Tcl_GetChannel(interp, z, &notUsed);
1648 if( !channel ) return TCL_ERROR;
1650 Tcl_Flush(channel);
1651 Tcl_Seek(channel, 0, SEEK_SET);
1653 instanceData = Tcl_GetChannelInstanceData(channel);
1654 *ppBlob = *((sqlite3_blob **)instanceData);
1657 return TCL_OK;
1660 static int SQLITE_TCLAPI test_blob_reopen(
1661 ClientData clientData, /* Not used */
1662 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1663 int objc, /* Number of arguments */
1664 Tcl_Obj *CONST objv[] /* Command arguments */
1666 Tcl_WideInt iRowid;
1667 sqlite3_blob *pBlob;
1668 int rc;
1670 if( objc!=3 ){
1671 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
1672 return TCL_ERROR;
1675 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
1676 if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
1678 rc = sqlite3_blob_reopen(pBlob, iRowid);
1679 if( rc!=SQLITE_OK ){
1680 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
1683 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1686 #endif
1689 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1691 ** This Tcl proc is used for testing the experimental
1692 ** sqlite3_create_collation_v2() interface.
1694 struct TestCollationX {
1695 Tcl_Interp *interp;
1696 Tcl_Obj *pCmp;
1697 Tcl_Obj *pDel;
1699 typedef struct TestCollationX TestCollationX;
1700 static void testCreateCollationDel(void *pCtx){
1701 TestCollationX *p = (TestCollationX *)pCtx;
1703 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1704 if( rc!=TCL_OK ){
1705 Tcl_BackgroundError(p->interp);
1708 Tcl_DecrRefCount(p->pCmp);
1709 Tcl_DecrRefCount(p->pDel);
1710 sqlite3_free((void *)p);
1712 static int testCreateCollationCmp(
1713 void *pCtx,
1714 int nLeft,
1715 const void *zLeft,
1716 int nRight,
1717 const void *zRight
1719 TestCollationX *p = (TestCollationX *)pCtx;
1720 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1721 int iRes = 0;
1723 Tcl_IncrRefCount(pScript);
1724 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1725 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1727 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1728 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1730 Tcl_BackgroundError(p->interp);
1732 Tcl_DecrRefCount(pScript);
1734 return iRes;
1736 static int SQLITE_TCLAPI test_create_collation_v2(
1737 ClientData clientData, /* Not used */
1738 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1739 int objc, /* Number of arguments */
1740 Tcl_Obj *CONST objv[] /* Command arguments */
1742 TestCollationX *p;
1743 sqlite3 *db;
1744 int rc;
1746 if( objc!=5 ){
1747 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1748 return TCL_ERROR;
1750 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1752 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1753 p->pCmp = objv[3];
1754 p->pDel = objv[4];
1755 p->interp = interp;
1756 Tcl_IncrRefCount(p->pCmp);
1757 Tcl_IncrRefCount(p->pDel);
1759 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1760 (void *)p, testCreateCollationCmp, testCreateCollationDel
1762 if( rc!=SQLITE_MISUSE ){
1763 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1764 "an invalid encoding", (char*)0);
1765 return TCL_ERROR;
1767 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1768 (void *)p, testCreateCollationCmp, testCreateCollationDel
1770 return TCL_OK;
1774 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
1776 ** Available switches are:
1778 ** -func SCRIPT
1779 ** -step SCRIPT
1780 ** -final SCRIPT
1781 ** -destroy SCRIPT
1783 typedef struct CreateFunctionV2 CreateFunctionV2;
1784 struct CreateFunctionV2 {
1785 Tcl_Interp *interp;
1786 Tcl_Obj *pFunc; /* Script for function invocation */
1787 Tcl_Obj *pStep; /* Script for agg. step invocation */
1788 Tcl_Obj *pFinal; /* Script for agg. finalization invocation */
1789 Tcl_Obj *pDestroy; /* Destructor script */
1791 static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1793 static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1795 static void cf2Final(sqlite3_context *ctx){
1797 static void cf2Destroy(void *pUser){
1798 CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
1800 if( p->interp && p->pDestroy ){
1801 int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
1802 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
1805 if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
1806 if( p->pStep ) Tcl_DecrRefCount(p->pStep);
1807 if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
1808 if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
1809 sqlite3_free(p);
1811 static int SQLITE_TCLAPI test_create_function_v2(
1812 ClientData clientData, /* Not used */
1813 Tcl_Interp *interp, /* The invoking TCL interpreter */
1814 int objc, /* Number of arguments */
1815 Tcl_Obj *CONST objv[] /* Command arguments */
1817 sqlite3 *db;
1818 const char *zFunc;
1819 int nArg;
1820 int enc;
1821 CreateFunctionV2 *p;
1822 int i;
1823 int rc;
1825 struct EncTable {
1826 const char *zEnc;
1827 int enc;
1828 } aEnc[] = {
1829 {"utf8", SQLITE_UTF8 },
1830 {"utf16", SQLITE_UTF16 },
1831 {"utf16le", SQLITE_UTF16LE },
1832 {"utf16be", SQLITE_UTF16BE },
1833 {"any", SQLITE_ANY },
1834 {"0", 0 }
1837 if( objc<5 || (objc%2)==0 ){
1838 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
1839 return TCL_ERROR;
1842 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1843 zFunc = Tcl_GetString(objv[2]);
1844 if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
1845 if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
1846 "encoding", 0, &enc)
1848 return TCL_ERROR;
1850 enc = aEnc[enc].enc;
1852 p = sqlite3_malloc(sizeof(CreateFunctionV2));
1853 assert( p );
1854 memset(p, 0, sizeof(CreateFunctionV2));
1855 p->interp = interp;
1857 for(i=5; i<objc; i+=2){
1858 int iSwitch;
1859 const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
1860 if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
1861 sqlite3_free(p);
1862 return TCL_ERROR;
1865 switch( iSwitch ){
1866 case 0: p->pFunc = objv[i+1]; break;
1867 case 1: p->pStep = objv[i+1]; break;
1868 case 2: p->pFinal = objv[i+1]; break;
1869 case 3: p->pDestroy = objv[i+1]; break;
1872 if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
1873 if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
1874 if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
1875 if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
1877 if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
1878 if( p->pStep ) Tcl_IncrRefCount(p->pStep);
1879 if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
1880 if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
1882 rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
1883 (p->pFunc ? cf2Func : 0),
1884 (p->pStep ? cf2Step : 0),
1885 (p->pFinal ? cf2Final : 0),
1886 cf2Destroy
1888 if( rc!=SQLITE_OK ){
1889 Tcl_ResetResult(interp);
1890 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
1891 return TCL_ERROR;
1893 return TCL_OK;
1897 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1899 static int SQLITE_TCLAPI test_load_extension(
1900 ClientData clientData, /* Not used */
1901 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1902 int objc, /* Number of arguments */
1903 Tcl_Obj *CONST objv[] /* Command arguments */
1905 Tcl_CmdInfo cmdInfo;
1906 sqlite3 *db;
1907 int rc;
1908 char *zDb;
1909 char *zFile;
1910 char *zProc = 0;
1911 char *zErr = 0;
1913 if( objc!=4 && objc!=3 ){
1914 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1915 return TCL_ERROR;
1917 zDb = Tcl_GetString(objv[1]);
1918 zFile = Tcl_GetString(objv[2]);
1919 if( objc==4 ){
1920 zProc = Tcl_GetString(objv[3]);
1923 /* Extract the C database handle from the Tcl command name */
1924 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1925 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1926 return TCL_ERROR;
1928 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1929 assert(db);
1931 /* Call the underlying C function. If an error occurs, set rc to
1932 ** TCL_ERROR and load any error string into the interpreter. If no
1933 ** error occurs, set rc to TCL_OK.
1935 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1936 rc = SQLITE_ERROR;
1937 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1938 (void)zProc;
1939 (void)zFile;
1940 #else
1941 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
1942 #endif
1943 if( rc!=SQLITE_OK ){
1944 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1945 rc = TCL_ERROR;
1946 }else{
1947 rc = TCL_OK;
1949 sqlite3_free(zErr);
1951 return rc;
1955 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1957 static int SQLITE_TCLAPI test_enable_load(
1958 ClientData clientData, /* Not used */
1959 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1960 int objc, /* Number of arguments */
1961 Tcl_Obj *CONST objv[] /* Command arguments */
1963 Tcl_CmdInfo cmdInfo;
1964 sqlite3 *db;
1965 char *zDb;
1966 int onoff;
1968 if( objc!=3 ){
1969 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1970 return TCL_ERROR;
1972 zDb = Tcl_GetString(objv[1]);
1974 /* Extract the C database handle from the Tcl command name */
1975 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1976 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1977 return TCL_ERROR;
1979 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1980 assert(db);
1982 /* Get the onoff parameter */
1983 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1984 return TCL_ERROR;
1987 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1988 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1989 return TCL_ERROR;
1990 #else
1991 sqlite3_enable_load_extension(db, onoff);
1992 return TCL_OK;
1993 #endif
1997 ** Usage: sqlite_abort
1999 ** Shutdown the process immediately. This is not a clean shutdown.
2000 ** This command is used to test the recoverability of a database in
2001 ** the event of a program crash.
2003 static int SQLITE_TCLAPI sqlite_abort(
2004 void *NotUsed,
2005 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2006 int argc, /* Number of arguments */
2007 char **argv /* Text of each argument */
2009 #if defined(_MSC_VER)
2010 /* We do this, otherwise the test will halt with a popup message
2011 * that we have to click away before the test will continue.
2013 _set_abort_behavior( 0, _CALL_REPORTFAULT );
2014 #endif
2015 exit(255);
2016 assert( interp==0 ); /* This will always fail */
2017 return TCL_OK;
2021 ** The following routine is a user-defined SQL function whose purpose
2022 ** is to test the sqlite_set_result() API.
2024 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2025 while( argc>=2 ){
2026 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
2027 if( zArg0 ){
2028 if( 0==sqlite3StrICmp(zArg0, "int") ){
2029 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
2030 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
2031 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
2032 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
2033 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
2034 SQLITE_TRANSIENT);
2035 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
2036 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
2037 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
2038 sqlite3_result_null(context);
2039 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
2040 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
2041 }else{
2042 goto error_out;
2044 }else{
2045 goto error_out;
2047 argc -= 2;
2048 argv += 2;
2050 return;
2052 error_out:
2053 sqlite3_result_error(context,"first argument should be one of: "
2054 "int int64 string double null value", -1);
2058 ** Usage: sqlite_register_test_function DB NAME
2060 ** Register the test SQL function on the database DB under the name NAME.
2062 static int SQLITE_TCLAPI test_register_func(
2063 void *NotUsed,
2064 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2065 int argc, /* Number of arguments */
2066 char **argv /* Text of each argument */
2068 sqlite3 *db;
2069 int rc;
2070 if( argc!=3 ){
2071 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2072 " DB FUNCTION-NAME", 0);
2073 return TCL_ERROR;
2075 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2076 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
2077 testFunc, 0, 0);
2078 if( rc!=0 ){
2079 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2080 return TCL_ERROR;
2082 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2083 return TCL_OK;
2087 ** Usage: sqlite3_finalize STMT
2089 ** Finalize a statement handle.
2091 static int SQLITE_TCLAPI test_finalize(
2092 void * clientData,
2093 Tcl_Interp *interp,
2094 int objc,
2095 Tcl_Obj *CONST objv[]
2097 sqlite3_stmt *pStmt;
2098 int rc;
2099 sqlite3 *db = 0;
2101 if( objc!=2 ){
2102 Tcl_AppendResult(interp, "wrong # args: should be \"",
2103 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2104 return TCL_ERROR;
2107 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2109 if( pStmt ){
2110 db = StmtToDb(pStmt);
2112 rc = sqlite3_finalize(pStmt);
2113 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2114 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2115 return TCL_OK;
2119 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2121 ** Get the value of a status counter from a statement.
2123 static int SQLITE_TCLAPI test_stmt_status(
2124 void * clientData,
2125 Tcl_Interp *interp,
2126 int objc,
2127 Tcl_Obj *CONST objv[]
2129 int iValue;
2130 int i, op = 0, resetFlag;
2131 const char *zOpName;
2132 sqlite3_stmt *pStmt;
2134 static const struct {
2135 const char *zName;
2136 int op;
2137 } aOp[] = {
2138 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
2139 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
2140 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
2141 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP },
2142 { "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE },
2143 { "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN },
2144 { "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED },
2146 if( objc!=4 ){
2147 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2148 return TCL_ERROR;
2150 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2151 zOpName = Tcl_GetString(objv[2]);
2152 for(i=0; i<ArraySize(aOp); i++){
2153 if( strcmp(aOp[i].zName, zOpName)==0 ){
2154 op = aOp[i].op;
2155 break;
2158 if( i>=ArraySize(aOp) ){
2159 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2161 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2162 iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2163 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2164 return TCL_OK;
2167 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2169 ** Usage: sqlite3_stmt_scanstatus STMT IDX
2171 static int SQLITE_TCLAPI test_stmt_scanstatus(
2172 void * clientData,
2173 Tcl_Interp *interp,
2174 int objc,
2175 Tcl_Obj *CONST objv[]
2177 sqlite3_stmt *pStmt; /* First argument */
2178 int idx; /* Second argument */
2180 const char *zName;
2181 const char *zExplain;
2182 sqlite3_int64 nLoop;
2183 sqlite3_int64 nVisit;
2184 double rEst;
2185 int res;
2187 if( objc!=3 ){
2188 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX");
2189 return TCL_ERROR;
2191 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2192 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2194 res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop);
2195 if( res==0 ){
2196 Tcl_Obj *pRet = Tcl_NewObj();
2197 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
2198 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
2199 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2200 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
2201 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
2202 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst);
2203 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
2204 Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
2205 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName);
2206 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
2207 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
2208 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2209 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
2210 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
2211 Tcl_SetObjResult(interp, pRet);
2212 }else{
2213 Tcl_ResetResult(interp);
2215 return TCL_OK;
2219 ** Usage: sqlite3_stmt_scanstatus_reset STMT
2221 static int SQLITE_TCLAPI test_stmt_scanstatus_reset(
2222 void * clientData,
2223 Tcl_Interp *interp,
2224 int objc,
2225 Tcl_Obj *CONST objv[]
2227 sqlite3_stmt *pStmt; /* First argument */
2228 if( objc!=2 ){
2229 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2230 return TCL_ERROR;
2232 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2233 sqlite3_stmt_scanstatus_reset(pStmt);
2234 return TCL_OK;
2236 #endif
2238 #ifdef SQLITE_ENABLE_SQLLOG
2240 ** Usage: sqlite3_config_sqllog
2242 ** Zero the SQLITE_CONFIG_SQLLOG configuration
2244 static int SQLITE_TCLAPI test_config_sqllog(
2245 void * clientData,
2246 Tcl_Interp *interp,
2247 int objc,
2248 Tcl_Obj *CONST objv[]
2250 if( objc!=1 ){
2251 Tcl_WrongNumArgs(interp, 1, objv, "");
2252 return TCL_ERROR;
2254 sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
2255 return TCL_OK;
2257 #endif
2260 ** Usage: vfs_current_time_int64
2262 ** Return the value returned by the default VFS's xCurrentTimeInt64 method.
2264 static int SQLITE_TCLAPI vfsCurrentTimeInt64(
2265 void * clientData,
2266 Tcl_Interp *interp,
2267 int objc,
2268 Tcl_Obj *CONST objv[]
2270 i64 t;
2271 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
2272 if( objc!=1 ){
2273 Tcl_WrongNumArgs(interp, 1, objv, "");
2274 return TCL_ERROR;
2276 pVfs->xCurrentTimeInt64(pVfs, &t);
2277 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
2278 return TCL_OK;
2281 #ifdef SQLITE_ENABLE_SNAPSHOT
2283 ** Usage: sqlite3_snapshot_get DB DBNAME
2285 static int SQLITE_TCLAPI test_snapshot_get(
2286 void * clientData,
2287 Tcl_Interp *interp,
2288 int objc,
2289 Tcl_Obj *CONST objv[]
2291 int rc;
2292 sqlite3 *db;
2293 char *zName;
2294 sqlite3_snapshot *pSnapshot = 0;
2296 if( objc!=3 ){
2297 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2298 return TCL_ERROR;
2300 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2301 zName = Tcl_GetString(objv[2]);
2303 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2304 if( rc!=SQLITE_OK ){
2305 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2306 return TCL_ERROR;
2307 }else{
2308 char zBuf[100];
2309 if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
2310 Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
2312 return TCL_OK;
2314 #endif /* SQLITE_ENABLE_SNAPSHOT */
2316 #ifdef SQLITE_ENABLE_SNAPSHOT
2318 ** Usage: sqlite3_snapshot_recover DB DBNAME
2320 static int SQLITE_TCLAPI test_snapshot_recover(
2321 void * clientData,
2322 Tcl_Interp *interp,
2323 int objc,
2324 Tcl_Obj *CONST objv[]
2326 int rc;
2327 sqlite3 *db;
2328 char *zName;
2330 if( objc!=3 ){
2331 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2332 return TCL_ERROR;
2334 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2335 zName = Tcl_GetString(objv[2]);
2337 rc = sqlite3_snapshot_recover(db, zName);
2338 if( rc!=SQLITE_OK ){
2339 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2340 return TCL_ERROR;
2341 }else{
2342 Tcl_ResetResult(interp);
2344 return TCL_OK;
2346 #endif /* SQLITE_ENABLE_SNAPSHOT */
2348 #ifdef SQLITE_ENABLE_SNAPSHOT
2350 ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
2352 static int SQLITE_TCLAPI test_snapshot_open(
2353 void * clientData,
2354 Tcl_Interp *interp,
2355 int objc,
2356 Tcl_Obj *CONST objv[]
2358 int rc;
2359 sqlite3 *db;
2360 char *zName;
2361 sqlite3_snapshot *pSnapshot;
2363 if( objc!=4 ){
2364 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2365 return TCL_ERROR;
2367 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2368 zName = Tcl_GetString(objv[2]);
2369 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
2371 rc = sqlite3_snapshot_open(db, zName, pSnapshot);
2372 if( rc!=SQLITE_OK ){
2373 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2374 return TCL_ERROR;
2376 return TCL_OK;
2378 #endif /* SQLITE_ENABLE_SNAPSHOT */
2380 #ifdef SQLITE_ENABLE_SNAPSHOT
2382 ** Usage: sqlite3_snapshot_free SNAPSHOT
2384 static int SQLITE_TCLAPI test_snapshot_free(
2385 void * clientData,
2386 Tcl_Interp *interp,
2387 int objc,
2388 Tcl_Obj *CONST objv[]
2390 sqlite3_snapshot *pSnapshot;
2391 if( objc!=2 ){
2392 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
2393 return TCL_ERROR;
2395 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2396 sqlite3_snapshot_free(pSnapshot);
2397 return TCL_OK;
2399 #endif /* SQLITE_ENABLE_SNAPSHOT */
2401 #ifdef SQLITE_ENABLE_SNAPSHOT
2403 ** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2
2405 static int SQLITE_TCLAPI test_snapshot_cmp(
2406 void * clientData,
2407 Tcl_Interp *interp,
2408 int objc,
2409 Tcl_Obj *CONST objv[]
2411 int res;
2412 sqlite3_snapshot *p1;
2413 sqlite3_snapshot *p2;
2414 if( objc!=3 ){
2415 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2416 return TCL_ERROR;
2418 p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2419 p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2]));
2420 res = sqlite3_snapshot_cmp(p1, p2);
2421 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2422 return TCL_OK;
2424 #endif /* SQLITE_ENABLE_SNAPSHOT */
2426 #ifdef SQLITE_ENABLE_SNAPSHOT
2428 ** Usage: sqlite3_snapshot_get_blob DB DBNAME
2430 static int SQLITE_TCLAPI test_snapshot_get_blob(
2431 void * clientData,
2432 Tcl_Interp *interp,
2433 int objc,
2434 Tcl_Obj *CONST objv[]
2436 int rc;
2437 sqlite3 *db;
2438 char *zName;
2439 sqlite3_snapshot *pSnapshot = 0;
2441 if( objc!=3 ){
2442 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2443 return TCL_ERROR;
2445 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2446 zName = Tcl_GetString(objv[2]);
2448 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2449 if( rc!=SQLITE_OK ){
2450 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2451 return TCL_ERROR;
2452 }else{
2453 Tcl_SetObjResult(interp,
2454 Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot))
2456 sqlite3_snapshot_free(pSnapshot);
2458 return TCL_OK;
2460 #endif /* SQLITE_ENABLE_SNAPSHOT */
2462 #ifdef SQLITE_ENABLE_SNAPSHOT
2464 ** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT
2466 static int SQLITE_TCLAPI test_snapshot_open_blob(
2467 void * clientData,
2468 Tcl_Interp *interp,
2469 int objc,
2470 Tcl_Obj *CONST objv[]
2472 int rc;
2473 sqlite3 *db;
2474 char *zName;
2475 unsigned char *pBlob;
2476 int nBlob;
2478 if( objc!=4 ){
2479 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2480 return TCL_ERROR;
2482 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2483 zName = Tcl_GetString(objv[2]);
2484 pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob);
2485 if( nBlob!=sizeof(sqlite3_snapshot) ){
2486 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2487 return TCL_ERROR;
2489 rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob);
2490 if( rc!=SQLITE_OK ){
2491 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2492 return TCL_ERROR;
2494 return TCL_OK;
2496 #endif /* SQLITE_ENABLE_SNAPSHOT */
2498 #ifdef SQLITE_ENABLE_SNAPSHOT
2500 ** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2
2502 static int SQLITE_TCLAPI test_snapshot_cmp_blob(
2503 void * clientData,
2504 Tcl_Interp *interp,
2505 int objc,
2506 Tcl_Obj *CONST objv[]
2508 int res;
2509 unsigned char *p1;
2510 unsigned char *p2;
2511 int n1;
2512 int n2;
2514 if( objc!=3 ){
2515 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2516 return TCL_ERROR;
2519 p1 = Tcl_GetByteArrayFromObj(objv[1], &n1);
2520 p2 = Tcl_GetByteArrayFromObj(objv[2], &n2);
2522 if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){
2523 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2524 return TCL_ERROR;
2527 res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2);
2528 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2529 return TCL_OK;
2531 #endif /* SQLITE_ENABLE_SNAPSHOT */
2534 ** Usage: sqlite3_delete_database FILENAME
2536 int sqlite3_delete_database(const char*); /* in test_delete.c */
2537 static int SQLITE_TCLAPI test_delete_database(
2538 void * clientData,
2539 Tcl_Interp *interp,
2540 int objc,
2541 Tcl_Obj *CONST objv[]
2543 int rc;
2544 const char *zFile;
2545 if( objc!=2 ){
2546 Tcl_WrongNumArgs(interp, 1, objv, "FILE");
2547 return TCL_ERROR;
2549 zFile = (const char*)Tcl_GetString(objv[1]);
2550 rc = sqlite3_delete_database(zFile);
2552 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2553 return TCL_OK;
2557 ** Usage: atomic_batch_write PATH
2559 static int SQLITE_TCLAPI test_atomic_batch_write(
2560 void * clientData,
2561 Tcl_Interp *interp,
2562 int objc,
2563 Tcl_Obj *CONST objv[]
2565 char *zFile = 0; /* Path to file to test */
2566 sqlite3 *db = 0; /* Database handle */
2567 sqlite3_file *pFd = 0; /* SQLite fd open on zFile */
2568 int bRes = 0; /* Integer result of this command */
2569 int dc = 0; /* Device-characteristics mask */
2570 int rc; /* sqlite3_open() return code */
2572 if( objc!=2 ){
2573 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
2574 return TCL_ERROR;
2576 zFile = Tcl_GetString(objv[1]);
2578 rc = sqlite3_open(zFile, &db);
2579 if( rc!=SQLITE_OK ){
2580 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
2581 sqlite3_close(db);
2582 return TCL_ERROR;
2585 rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
2586 dc = pFd->pMethods->xDeviceCharacteristics(pFd);
2587 if( dc & SQLITE_IOCAP_BATCH_ATOMIC ){
2588 bRes = 1;
2591 Tcl_SetObjResult(interp, Tcl_NewIntObj(bRes));
2592 sqlite3_close(db);
2593 return TCL_OK;
2597 ** Usage: sqlite3_next_stmt DB STMT
2599 ** Return the next statment in sequence after STMT.
2601 static int SQLITE_TCLAPI test_next_stmt(
2602 void * clientData,
2603 Tcl_Interp *interp,
2604 int objc,
2605 Tcl_Obj *CONST objv[]
2607 sqlite3_stmt *pStmt;
2608 sqlite3 *db = 0;
2609 char zBuf[50];
2611 if( objc!=3 ){
2612 Tcl_AppendResult(interp, "wrong # args: should be \"",
2613 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2614 return TCL_ERROR;
2617 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2618 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2619 pStmt = sqlite3_next_stmt(db, pStmt);
2620 if( pStmt ){
2621 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2622 Tcl_AppendResult(interp, zBuf, 0);
2624 return TCL_OK;
2628 ** Usage: sqlite3_stmt_readonly STMT
2630 ** Return true if STMT is a NULL pointer or a pointer to a statement
2631 ** that is guaranteed to leave the database unmodified.
2633 static int SQLITE_TCLAPI test_stmt_readonly(
2634 void * clientData,
2635 Tcl_Interp *interp,
2636 int objc,
2637 Tcl_Obj *CONST objv[]
2639 sqlite3_stmt *pStmt;
2640 int rc;
2642 if( objc!=2 ){
2643 Tcl_AppendResult(interp, "wrong # args: should be \"",
2644 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2645 return TCL_ERROR;
2648 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2649 rc = sqlite3_stmt_readonly(pStmt);
2650 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2651 return TCL_OK;
2655 ** Usage: sqlite3_stmt_busy STMT
2657 ** Return true if STMT is a non-NULL pointer to a statement
2658 ** that has been stepped but not to completion.
2660 static int SQLITE_TCLAPI test_stmt_busy(
2661 void * clientData,
2662 Tcl_Interp *interp,
2663 int objc,
2664 Tcl_Obj *CONST objv[]
2666 sqlite3_stmt *pStmt;
2667 int rc;
2669 if( objc!=2 ){
2670 Tcl_AppendResult(interp, "wrong # args: should be \"",
2671 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2672 return TCL_ERROR;
2675 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2676 rc = sqlite3_stmt_busy(pStmt);
2677 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2678 return TCL_OK;
2682 ** Usage: uses_stmt_journal STMT
2684 ** Return true if STMT uses a statement journal.
2686 static int SQLITE_TCLAPI uses_stmt_journal(
2687 void * clientData,
2688 Tcl_Interp *interp,
2689 int objc,
2690 Tcl_Obj *CONST objv[]
2692 sqlite3_stmt *pStmt;
2694 if( objc!=2 ){
2695 Tcl_AppendResult(interp, "wrong # args: should be \"",
2696 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2697 return TCL_ERROR;
2700 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2701 sqlite3_stmt_readonly(pStmt);
2702 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
2703 return TCL_OK;
2708 ** Usage: sqlite3_reset STMT
2710 ** Reset a statement handle.
2712 static int SQLITE_TCLAPI test_reset(
2713 void * clientData,
2714 Tcl_Interp *interp,
2715 int objc,
2716 Tcl_Obj *CONST objv[]
2718 sqlite3_stmt *pStmt;
2719 int rc;
2721 if( objc!=2 ){
2722 Tcl_AppendResult(interp, "wrong # args: should be \"",
2723 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2724 return TCL_ERROR;
2727 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2729 rc = sqlite3_reset(pStmt);
2730 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
2731 return TCL_ERROR;
2733 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2735 if( rc ){
2736 return TCL_ERROR;
2739 return TCL_OK;
2743 ** Usage: sqlite3_expired STMT
2745 ** Return TRUE if a recompilation of the statement is recommended.
2747 static int SQLITE_TCLAPI test_expired(
2748 void * clientData,
2749 Tcl_Interp *interp,
2750 int objc,
2751 Tcl_Obj *CONST objv[]
2753 #ifndef SQLITE_OMIT_DEPRECATED
2754 sqlite3_stmt *pStmt;
2755 if( objc!=2 ){
2756 Tcl_AppendResult(interp, "wrong # args: should be \"",
2757 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2758 return TCL_ERROR;
2760 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2761 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
2762 #endif
2763 return TCL_OK;
2767 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
2769 ** Transfer all bindings from FROMSTMT over to TOSTMT
2771 static int SQLITE_TCLAPI test_transfer_bind(
2772 void * clientData,
2773 Tcl_Interp *interp,
2774 int objc,
2775 Tcl_Obj *CONST objv[]
2777 #ifndef SQLITE_OMIT_DEPRECATED
2778 sqlite3_stmt *pStmt1, *pStmt2;
2779 if( objc!=3 ){
2780 Tcl_AppendResult(interp, "wrong # args: should be \"",
2781 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2782 return TCL_ERROR;
2784 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2785 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2786 Tcl_SetObjResult(interp,
2787 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
2788 #endif
2789 return TCL_OK;
2793 ** Usage: sqlite3_changes DB
2795 ** Return the number of changes made to the database by the last SQL
2796 ** execution.
2798 static int SQLITE_TCLAPI test_changes(
2799 void * clientData,
2800 Tcl_Interp *interp,
2801 int objc,
2802 Tcl_Obj *CONST objv[]
2804 sqlite3 *db;
2805 if( objc!=2 ){
2806 Tcl_AppendResult(interp, "wrong # args: should be \"",
2807 Tcl_GetString(objv[0]), " DB", 0);
2808 return TCL_ERROR;
2810 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2811 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2812 return TCL_OK;
2816 ** This is the "static_bind_value" that variables are bound to when
2817 ** the FLAG option of sqlite3_bind is "static"
2819 static char *sqlite_static_bind_value = 0;
2820 static int sqlite_static_bind_nbyte = 0;
2823 ** Usage: sqlite3_bind VM IDX VALUE FLAGS
2825 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
2826 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
2827 ** ignored and the value is set to NULL. If FLAGS=="static" then
2828 ** the value is set to the value of a static variable named
2829 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
2830 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
2831 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
2833 static int SQLITE_TCLAPI test_bind(
2834 void *NotUsed,
2835 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2836 int argc, /* Number of arguments */
2837 char **argv /* Text of each argument */
2839 sqlite3_stmt *pStmt;
2840 int rc;
2841 int idx;
2842 if( argc!=5 ){
2843 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2844 " VM IDX VALUE (null|static|normal)\"", 0);
2845 return TCL_ERROR;
2847 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
2848 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2849 if( strcmp(argv[4],"null")==0 ){
2850 rc = sqlite3_bind_null(pStmt, idx);
2851 }else if( strcmp(argv[4],"static")==0 ){
2852 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
2853 }else if( strcmp(argv[4],"static-nbytes")==0 ){
2854 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2855 sqlite_static_bind_nbyte, 0);
2856 }else if( strcmp(argv[4],"normal")==0 ){
2857 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
2858 }else if( strcmp(argv[4],"blob10")==0 ){
2859 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
2860 }else{
2861 Tcl_AppendResult(interp, "4th argument should be "
2862 "\"null\" or \"static\" or \"normal\"", 0);
2863 return TCL_ERROR;
2865 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2866 if( rc ){
2867 char zBuf[50];
2868 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
2869 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
2870 return TCL_ERROR;
2872 return TCL_OK;
2875 #ifndef SQLITE_OMIT_UTF16
2877 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2879 ** This function is used to test that SQLite selects the correct collation
2880 ** sequence callback when multiple versions (for different text encodings)
2881 ** are available.
2883 ** Calling this routine registers the collation sequence "test_collate"
2884 ** with database handle <db>. The second argument must be a list of three
2885 ** boolean values. If the first is true, then a version of test_collate is
2886 ** registered for UTF-8, if the second is true, a version is registered for
2887 ** UTF-16le, if the third is true, a UTF-16be version is available.
2888 ** Previous versions of test_collate are deleted.
2890 ** The collation sequence test_collate is implemented by calling the
2891 ** following TCL script:
2893 ** "test_collate <enc> <lhs> <rhs>"
2895 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2896 ** The <enc> parameter is the encoding of the collation function that
2897 ** SQLite selected to call. The TCL test script implements the
2898 ** "test_collate" proc.
2900 ** Note that this will only work with one interpreter at a time, as the
2901 ** interp pointer to use when evaluating the TCL script is stored in
2902 ** pTestCollateInterp.
2904 static Tcl_Interp* pTestCollateInterp;
2905 static int test_collate_func(
2906 void *pCtx,
2907 int nA, const void *zA,
2908 int nB, const void *zB
2910 Tcl_Interp *i = pTestCollateInterp;
2911 int encin = SQLITE_PTR_TO_INT(pCtx);
2912 int res;
2913 int n;
2915 sqlite3_value *pVal;
2916 Tcl_Obj *pX;
2918 pX = Tcl_NewStringObj("test_collate", -1);
2919 Tcl_IncrRefCount(pX);
2921 switch( encin ){
2922 case SQLITE_UTF8:
2923 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2924 break;
2925 case SQLITE_UTF16LE:
2926 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2927 break;
2928 case SQLITE_UTF16BE:
2929 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2930 break;
2931 default:
2932 assert(0);
2935 sqlite3BeginBenignMalloc();
2936 pVal = sqlite3ValueNew(0);
2937 if( pVal ){
2938 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2939 n = sqlite3_value_bytes(pVal);
2940 Tcl_ListObjAppendElement(i,pX,
2941 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2942 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2943 n = sqlite3_value_bytes(pVal);
2944 Tcl_ListObjAppendElement(i,pX,
2945 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2946 sqlite3ValueFree(pVal);
2948 sqlite3EndBenignMalloc();
2950 Tcl_EvalObjEx(i, pX, 0);
2951 Tcl_DecrRefCount(pX);
2952 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2953 return res;
2955 static int SQLITE_TCLAPI test_collate(
2956 void * clientData,
2957 Tcl_Interp *interp,
2958 int objc,
2959 Tcl_Obj *CONST objv[]
2961 sqlite3 *db;
2962 int val;
2963 sqlite3_value *pVal;
2964 int rc;
2966 if( objc!=5 ) goto bad_args;
2967 pTestCollateInterp = interp;
2968 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2970 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2971 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2972 (void *)SQLITE_UTF8, val?test_collate_func:0);
2973 if( rc==SQLITE_OK ){
2974 const void *zUtf16;
2975 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2976 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2977 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2978 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2980 #if 0
2981 if( sqlite3_iMallocFail>0 ){
2982 sqlite3_iMallocFail++;
2984 #endif
2985 sqlite3_mutex_enter(db->mutex);
2986 pVal = sqlite3ValueNew(db);
2987 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
2988 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
2989 if( db->mallocFailed ){
2990 rc = SQLITE_NOMEM;
2991 }else{
2992 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
2993 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
2995 sqlite3ValueFree(pVal);
2996 sqlite3_mutex_leave(db->mutex);
2998 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3000 if( rc!=SQLITE_OK ){
3001 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3002 return TCL_ERROR;
3004 return TCL_OK;
3006 bad_args:
3007 Tcl_AppendResult(interp, "wrong # args: should be \"",
3008 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3009 return TCL_ERROR;
3013 ** Usage: add_test_utf16bin_collate <db ptr>
3015 ** Add a utf-16 collation sequence named "utf16bin" to the database
3016 ** handle. This collation sequence compares arguments in the same way as the
3017 ** built-in collation "binary".
3019 static int test_utf16bin_collate_func(
3020 void *pCtx,
3021 int nA, const void *zA,
3022 int nB, const void *zB
3024 int nCmp = (nA>nB ? nB : nA);
3025 int res = memcmp(zA, zB, nCmp);
3026 if( res==0 ) res = nA - nB;
3027 return res;
3029 static int SQLITE_TCLAPI test_utf16bin_collate(
3030 void * clientData,
3031 Tcl_Interp *interp,
3032 int objc,
3033 Tcl_Obj *CONST objv[]
3035 sqlite3 *db;
3036 int rc;
3038 if( objc!=2 ) goto bad_args;
3039 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3041 rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0,
3042 test_utf16bin_collate_func
3044 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3045 return TCL_OK;
3047 bad_args:
3048 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3049 return TCL_ERROR;
3053 ** When the collation needed callback is invoked, record the name of
3054 ** the requested collating function here. The recorded name is linked
3055 ** to a TCL variable and used to make sure that the requested collation
3056 ** name is correct.
3058 static char zNeededCollation[200];
3059 static char *pzNeededCollation = zNeededCollation;
3063 ** Called when a collating sequence is needed. Registered using
3064 ** sqlite3_collation_needed16().
3066 static void test_collate_needed_cb(
3067 void *pCtx,
3068 sqlite3 *db,
3069 int eTextRep,
3070 const void *pName
3072 int enc = ENC(db);
3073 int i;
3074 char *z;
3075 for(z = (char*)pName, i=0; *z || z[1]; z++){
3076 if( *z ) zNeededCollation[i++] = *z;
3078 zNeededCollation[i] = 0;
3079 sqlite3_create_collation(
3080 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
3084 ** Usage: add_test_collate_needed DB
3086 static int SQLITE_TCLAPI test_collate_needed(
3087 void * clientData,
3088 Tcl_Interp *interp,
3089 int objc,
3090 Tcl_Obj *CONST objv[]
3092 sqlite3 *db;
3093 int rc;
3095 if( objc!=2 ) goto bad_args;
3096 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3097 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
3098 zNeededCollation[0] = 0;
3099 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3100 return TCL_OK;
3102 bad_args:
3103 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3104 return TCL_ERROR;
3108 ** tclcmd: add_alignment_test_collations DB
3110 ** Add two new collating sequences to the database DB
3112 ** utf16_aligned
3113 ** utf16_unaligned
3115 ** Both collating sequences use the same sort order as BINARY.
3116 ** The only difference is that the utf16_aligned collating
3117 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
3118 ** Both collating functions increment the unaligned utf16 counter
3119 ** whenever they see a string that begins on an odd byte boundary.
3121 static int unaligned_string_counter = 0;
3122 static int alignmentCollFunc(
3123 void *NotUsed,
3124 int nKey1, const void *pKey1,
3125 int nKey2, const void *pKey2
3127 int rc, n;
3128 n = nKey1<nKey2 ? nKey1 : nKey2;
3129 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
3130 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
3131 rc = memcmp(pKey1, pKey2, n);
3132 if( rc==0 ){
3133 rc = nKey1 - nKey2;
3135 return rc;
3137 static int SQLITE_TCLAPI add_alignment_test_collations(
3138 void * clientData,
3139 Tcl_Interp *interp,
3140 int objc,
3141 Tcl_Obj *CONST objv[]
3143 sqlite3 *db;
3144 if( objc>=2 ){
3145 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3146 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
3147 0, alignmentCollFunc);
3148 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
3149 0, alignmentCollFunc);
3151 return SQLITE_OK;
3153 #endif /* !defined(SQLITE_OMIT_UTF16) */
3156 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
3158 ** This function is used to test that SQLite selects the correct user
3159 ** function callback when multiple versions (for different text encodings)
3160 ** are available.
3162 ** Calling this routine registers up to three versions of the user function
3163 ** "test_function" with database handle <db>. If the second argument is
3164 ** true, then a version of test_function is registered for UTF-8, if the
3165 ** third is true, a version is registered for UTF-16le, if the fourth is
3166 ** true, a UTF-16be version is available. Previous versions of
3167 ** test_function are deleted.
3169 ** The user function is implemented by calling the following TCL script:
3171 ** "test_function <enc> <arg>"
3173 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
3174 ** single argument passed to the SQL function. The value returned by
3175 ** the TCL script is used as the return value of the SQL function. It
3176 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
3177 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
3178 ** prefers UTF-16BE.
3180 #ifndef SQLITE_OMIT_UTF16
3181 static void test_function_utf8(
3182 sqlite3_context *pCtx,
3183 int nArg,
3184 sqlite3_value **argv
3186 Tcl_Interp *interp;
3187 Tcl_Obj *pX;
3188 sqlite3_value *pVal;
3189 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3190 pX = Tcl_NewStringObj("test_function", -1);
3191 Tcl_IncrRefCount(pX);
3192 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
3193 Tcl_ListObjAppendElement(interp, pX,
3194 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3195 Tcl_EvalObjEx(interp, pX, 0);
3196 Tcl_DecrRefCount(pX);
3197 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
3198 pVal = sqlite3ValueNew(0);
3199 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3200 SQLITE_UTF8, SQLITE_STATIC);
3201 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
3202 -1, SQLITE_TRANSIENT);
3203 sqlite3ValueFree(pVal);
3205 static void test_function_utf16le(
3206 sqlite3_context *pCtx,
3207 int nArg,
3208 sqlite3_value **argv
3210 Tcl_Interp *interp;
3211 Tcl_Obj *pX;
3212 sqlite3_value *pVal;
3213 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3214 pX = Tcl_NewStringObj("test_function", -1);
3215 Tcl_IncrRefCount(pX);
3216 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
3217 Tcl_ListObjAppendElement(interp, pX,
3218 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3219 Tcl_EvalObjEx(interp, pX, 0);
3220 Tcl_DecrRefCount(pX);
3221 pVal = sqlite3ValueNew(0);
3222 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3223 SQLITE_UTF8, SQLITE_STATIC);
3224 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
3225 sqlite3ValueFree(pVal);
3227 static void test_function_utf16be(
3228 sqlite3_context *pCtx,
3229 int nArg,
3230 sqlite3_value **argv
3232 Tcl_Interp *interp;
3233 Tcl_Obj *pX;
3234 sqlite3_value *pVal;
3235 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3236 pX = Tcl_NewStringObj("test_function", -1);
3237 Tcl_IncrRefCount(pX);
3238 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
3239 Tcl_ListObjAppendElement(interp, pX,
3240 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3241 Tcl_EvalObjEx(interp, pX, 0);
3242 Tcl_DecrRefCount(pX);
3243 pVal = sqlite3ValueNew(0);
3244 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3245 SQLITE_UTF8, SQLITE_STATIC);
3246 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
3247 -1, SQLITE_TRANSIENT);
3248 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
3249 -1, SQLITE_TRANSIENT);
3250 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
3251 -1, SQLITE_TRANSIENT);
3252 sqlite3ValueFree(pVal);
3254 #endif /* SQLITE_OMIT_UTF16 */
3255 static int SQLITE_TCLAPI test_function(
3256 void * clientData,
3257 Tcl_Interp *interp,
3258 int objc,
3259 Tcl_Obj *CONST objv[]
3261 #ifndef SQLITE_OMIT_UTF16
3262 sqlite3 *db;
3263 int val;
3265 if( objc!=5 ) goto bad_args;
3266 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3268 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3269 if( val ){
3270 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
3271 interp, test_function_utf8, 0, 0);
3273 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3274 if( val ){
3275 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
3276 interp, test_function_utf16le, 0, 0);
3278 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3279 if( val ){
3280 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
3281 interp, test_function_utf16be, 0, 0);
3284 return TCL_OK;
3285 bad_args:
3286 Tcl_AppendResult(interp, "wrong # args: should be \"",
3287 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3288 #endif /* SQLITE_OMIT_UTF16 */
3289 return TCL_ERROR;
3293 ** Usage: sqlite3_test_errstr <err code>
3295 ** Test that the english language string equivalents for sqlite error codes
3296 ** are sane. The parameter is an integer representing an sqlite error code.
3297 ** The result is a list of two elements, the string representation of the
3298 ** error code and the english language explanation.
3300 static int SQLITE_TCLAPI test_errstr(
3301 void * clientData,
3302 Tcl_Interp *interp,
3303 int objc,
3304 Tcl_Obj *CONST objv[]
3306 char *zCode;
3307 int i;
3308 if( objc!=1 ){
3309 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
3312 zCode = Tcl_GetString(objv[1]);
3313 for(i=0; i<200; i++){
3314 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
3316 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
3317 return TCL_OK;
3321 ** Usage: breakpoint
3323 ** This routine exists for one purpose - to provide a place to put a
3324 ** breakpoint with GDB that can be triggered using TCL code. The use
3325 ** for this is when a particular test fails on (say) the 1485th iteration.
3326 ** In the TCL test script, we can add code like this:
3328 ** if {$i==1485} breakpoint
3330 ** Then run testfixture in the debugger and wait for the breakpoint to
3331 ** fire. Then additional breakpoints can be set to trace down the bug.
3333 static int SQLITE_TCLAPI test_breakpoint(
3334 void *NotUsed,
3335 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3336 int argc, /* Number of arguments */
3337 char **argv /* Text of each argument */
3339 return TCL_OK; /* Do nothing */
3343 ** Usage: sqlite3_bind_zeroblob STMT IDX N
3345 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
3346 ** IDX is the index of a wildcard in the prepared statement. This command
3347 ** binds a N-byte zero-filled BLOB to the wildcard.
3349 static int SQLITE_TCLAPI test_bind_zeroblob(
3350 void * clientData,
3351 Tcl_Interp *interp,
3352 int objc,
3353 Tcl_Obj *CONST objv[]
3355 sqlite3_stmt *pStmt;
3356 int idx;
3357 int n;
3358 int rc;
3360 if( objc!=4 ){
3361 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3362 return TCL_ERROR;
3365 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3366 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3367 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3369 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
3370 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3371 if( rc!=SQLITE_OK ){
3372 return TCL_ERROR;
3375 return TCL_OK;
3379 ** Usage: sqlite3_bind_zeroblob64 STMT IDX N
3381 ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement.
3382 ** IDX is the index of a wildcard in the prepared statement. This command
3383 ** binds a N-byte zero-filled BLOB to the wildcard.
3385 static int SQLITE_TCLAPI test_bind_zeroblob64(
3386 void * clientData,
3387 Tcl_Interp *interp,
3388 int objc,
3389 Tcl_Obj *CONST objv[]
3391 sqlite3_stmt *pStmt;
3392 int idx;
3393 Tcl_WideInt n;
3394 int rc;
3396 if( objc!=4 ){
3397 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3398 return TCL_ERROR;
3401 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3402 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3403 if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3405 rc = sqlite3_bind_zeroblob64(pStmt, idx, n);
3406 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3407 if( rc!=SQLITE_OK ){
3408 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3409 return TCL_ERROR;
3412 return TCL_OK;
3416 ** Usage: sqlite3_bind_int STMT N VALUE
3418 ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
3419 ** N is the index of a wildcard in the prepared statement. This command
3420 ** binds a 32-bit integer VALUE to that wildcard.
3422 static int SQLITE_TCLAPI test_bind_int(
3423 void * clientData,
3424 Tcl_Interp *interp,
3425 int objc,
3426 Tcl_Obj *CONST objv[]
3428 sqlite3_stmt *pStmt;
3429 int idx;
3430 int value;
3431 int rc;
3433 if( objc!=4 ){
3434 Tcl_AppendResult(interp, "wrong # args: should be \"",
3435 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3436 return TCL_ERROR;
3439 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3440 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3441 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3443 rc = sqlite3_bind_int(pStmt, idx, value);
3444 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3445 if( rc!=SQLITE_OK ){
3446 return TCL_ERROR;
3449 return TCL_OK;
3454 ** Usage: intarray_addr INT ...
3456 ** Return the address of a C-language array of 32-bit integers.
3458 ** Space to hold the array is obtained from malloc(). Call this procedure once
3459 ** with no arguments in order to release memory. Each call to this procedure
3460 ** overwrites the previous array.
3462 static int SQLITE_TCLAPI test_intarray_addr(
3463 void * clientData,
3464 Tcl_Interp *interp,
3465 int objc,
3466 Tcl_Obj *CONST objv[]
3468 int i;
3469 static int *p = 0;
3471 sqlite3_free(p);
3472 p = 0;
3473 if( objc>1 ){
3474 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3475 if( p==0 ) return TCL_ERROR;
3476 for(i=0; i<objc-1; i++){
3477 if( Tcl_GetIntFromObj(interp, objv[1+i], &p[i]) ){
3478 sqlite3_free(p);
3479 p = 0;
3480 return TCL_ERROR;
3484 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3485 return TCL_OK;
3488 ** Usage: intarray_addr INT ...
3490 ** Return the address of a C-language array of 32-bit integers.
3492 ** Space to hold the array is obtained from malloc(). Call this procedure once
3493 ** with no arguments in order to release memory. Each call to this procedure
3494 ** overwrites the previous array.
3496 static int SQLITE_TCLAPI test_int64array_addr(
3497 void * clientData,
3498 Tcl_Interp *interp,
3499 int objc,
3500 Tcl_Obj *CONST objv[]
3502 int i;
3503 static sqlite3_int64 *p = 0;
3505 sqlite3_free(p);
3506 p = 0;
3507 if( objc>1 ){
3508 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3509 if( p==0 ) return TCL_ERROR;
3510 for(i=0; i<objc-1; i++){
3511 Tcl_WideInt v;
3512 if( Tcl_GetWideIntFromObj(interp, objv[1+i], &v) ){
3513 sqlite3_free(p);
3514 p = 0;
3515 return TCL_ERROR;
3517 p[i] = v;
3520 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3521 return TCL_OK;
3524 ** Usage: doublearray_addr INT ...
3526 ** Return the address of a C-language array of doubles.
3528 ** Space to hold the array is obtained from malloc(). Call this procedure once
3529 ** with no arguments in order to release memory. Each call to this procedure
3530 ** overwrites the previous array.
3532 static int SQLITE_TCLAPI test_doublearray_addr(
3533 void * clientData,
3534 Tcl_Interp *interp,
3535 int objc,
3536 Tcl_Obj *CONST objv[]
3538 int i;
3539 static double *p = 0;
3541 sqlite3_free(p);
3542 p = 0;
3543 if( objc>1 ){
3544 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3545 if( p==0 ) return TCL_ERROR;
3546 for(i=0; i<objc-1; i++){
3547 if( Tcl_GetDoubleFromObj(interp, objv[1+i], &p[i]) ){
3548 sqlite3_free(p);
3549 p = 0;
3550 return TCL_ERROR;
3554 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3555 return TCL_OK;
3558 ** Usage: textarray_addr TEXT ...
3560 ** Return the address of a C-language array of strings.
3562 ** Space to hold the array is obtained from malloc(). Call this procedure once
3563 ** with no arguments in order to release memory. Each call to this procedure
3564 ** overwrites the previous array.
3566 static int SQLITE_TCLAPI test_textarray_addr(
3567 void * clientData,
3568 Tcl_Interp *interp,
3569 int objc,
3570 Tcl_Obj *CONST objv[]
3572 int i;
3573 static int n = 0;
3574 static char **p = 0;
3576 for(i=0; i<n; i++) sqlite3_free(p[i]);
3577 sqlite3_free(p);
3578 p = 0;
3579 if( objc>1 ){
3580 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3581 if( p==0 ) return TCL_ERROR;
3582 for(i=0; i<objc-1; i++){
3583 p[i] = sqlite3_mprintf("%s", Tcl_GetString(objv[1+i]));
3586 n = objc-1;
3587 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3588 return TCL_OK;
3593 ** Usage: sqlite3_bind_int64 STMT N VALUE
3595 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
3596 ** N is the index of a wildcard in the prepared statement. This command
3597 ** binds a 64-bit integer VALUE to that wildcard.
3599 static int SQLITE_TCLAPI test_bind_int64(
3600 void * clientData,
3601 Tcl_Interp *interp,
3602 int objc,
3603 Tcl_Obj *CONST objv[]
3605 sqlite3_stmt *pStmt;
3606 int idx;
3607 Tcl_WideInt value;
3608 int rc;
3610 if( objc!=4 ){
3611 Tcl_AppendResult(interp, "wrong # args: should be \"",
3612 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3613 return TCL_ERROR;
3616 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3617 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3618 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3620 rc = sqlite3_bind_int64(pStmt, idx, value);
3621 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3622 if( rc!=SQLITE_OK ){
3623 return TCL_ERROR;
3626 return TCL_OK;
3631 ** Usage: sqlite3_bind_double STMT N VALUE
3633 ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
3634 ** N is the index of a wildcard in the prepared statement. This command
3635 ** binds a 64-bit integer VALUE to that wildcard.
3637 static int SQLITE_TCLAPI test_bind_double(
3638 void * clientData,
3639 Tcl_Interp *interp,
3640 int objc,
3641 Tcl_Obj *CONST objv[]
3643 sqlite3_stmt *pStmt;
3644 int idx;
3645 double value = 0;
3646 int rc;
3647 const char *zVal;
3648 int i;
3649 static const struct {
3650 const char *zName; /* Name of the special floating point value */
3651 unsigned int iUpper; /* Upper 32 bits */
3652 unsigned int iLower; /* Lower 32 bits */
3653 } aSpecialFp[] = {
3654 { "NaN", 0x7fffffff, 0xffffffff },
3655 { "SNaN", 0x7ff7ffff, 0xffffffff },
3656 { "-NaN", 0xffffffff, 0xffffffff },
3657 { "-SNaN", 0xfff7ffff, 0xffffffff },
3658 { "+Inf", 0x7ff00000, 0x00000000 },
3659 { "-Inf", 0xfff00000, 0x00000000 },
3660 { "Epsilon", 0x00000000, 0x00000001 },
3661 { "-Epsilon", 0x80000000, 0x00000001 },
3662 { "NaN0", 0x7ff80000, 0x00000000 },
3663 { "-NaN0", 0xfff80000, 0x00000000 },
3666 if( objc!=4 ){
3667 Tcl_AppendResult(interp, "wrong # args: should be \"",
3668 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3669 return TCL_ERROR;
3672 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3673 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3675 /* Intercept the string "NaN" and generate a NaN value for it.
3676 ** All other strings are passed through to Tcl_GetDoubleFromObj().
3677 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
3678 ** contain a bug.
3680 zVal = Tcl_GetString(objv[3]);
3681 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
3682 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
3683 sqlite3_uint64 x;
3684 x = aSpecialFp[i].iUpper;
3685 x <<= 32;
3686 x |= aSpecialFp[i].iLower;
3687 assert( sizeof(value)==8 );
3688 assert( sizeof(x)==8 );
3689 memcpy(&value, &x, 8);
3690 break;
3693 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
3694 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
3695 return TCL_ERROR;
3697 rc = sqlite3_bind_double(pStmt, idx, value);
3698 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3699 if( rc!=SQLITE_OK ){
3700 return TCL_ERROR;
3703 return TCL_OK;
3707 ** Usage: sqlite3_bind_null STMT N
3709 ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
3710 ** N is the index of a wildcard in the prepared statement. This command
3711 ** binds a NULL to the wildcard.
3713 static int SQLITE_TCLAPI test_bind_null(
3714 void * clientData,
3715 Tcl_Interp *interp,
3716 int objc,
3717 Tcl_Obj *CONST objv[]
3719 sqlite3_stmt *pStmt;
3720 int idx;
3721 int rc;
3723 if( objc!=3 ){
3724 Tcl_AppendResult(interp, "wrong # args: should be \"",
3725 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
3726 return TCL_ERROR;
3729 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3730 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3732 rc = sqlite3_bind_null(pStmt, idx);
3733 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3734 if( rc!=SQLITE_OK ){
3735 return TCL_ERROR;
3738 return TCL_OK;
3742 ** Usage: sqlite3_bind_text STMT N STRING BYTES
3744 ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
3745 ** N is the index of a wildcard in the prepared statement. This command
3746 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
3747 ** long.
3749 static int SQLITE_TCLAPI test_bind_text(
3750 void * clientData,
3751 Tcl_Interp *interp,
3752 int objc,
3753 Tcl_Obj *CONST objv[]
3755 sqlite3_stmt *pStmt;
3756 int idx;
3757 int bytes;
3758 char *value;
3759 int rc;
3761 if( objc!=5 ){
3762 Tcl_AppendResult(interp, "wrong # args: should be \"",
3763 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3764 return TCL_ERROR;
3767 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3768 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3769 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
3770 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3772 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
3773 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3774 if( rc!=SQLITE_OK ){
3775 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3776 return TCL_ERROR;
3779 return TCL_OK;
3783 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
3785 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
3786 ** N is the index of a wildcard in the prepared statement. This command
3787 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
3788 ** long.
3790 static int SQLITE_TCLAPI test_bind_text16(
3791 void * clientData,
3792 Tcl_Interp *interp,
3793 int objc,
3794 Tcl_Obj *CONST objv[]
3796 #ifndef SQLITE_OMIT_UTF16
3797 sqlite3_stmt *pStmt;
3798 int idx;
3799 int bytes;
3800 char *value;
3801 int rc;
3803 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
3804 Tcl_Obj *oStmt = objv[objc-4];
3805 Tcl_Obj *oN = objv[objc-3];
3806 Tcl_Obj *oString = objv[objc-2];
3807 Tcl_Obj *oBytes = objv[objc-1];
3809 if( objc!=5 && objc!=6){
3810 Tcl_AppendResult(interp, "wrong # args: should be \"",
3811 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3812 return TCL_ERROR;
3815 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
3816 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
3817 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
3818 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
3820 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
3821 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3822 if( rc!=SQLITE_OK ){
3823 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3824 return TCL_ERROR;
3827 #endif /* SQLITE_OMIT_UTF16 */
3828 return TCL_OK;
3832 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
3834 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
3835 ** N is the index of a wildcard in the prepared statement. This command
3836 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
3838 static int SQLITE_TCLAPI test_bind_blob(
3839 void * clientData,
3840 Tcl_Interp *interp,
3841 int objc,
3842 Tcl_Obj *CONST objv[]
3844 sqlite3_stmt *pStmt;
3845 int len, idx;
3846 int bytes;
3847 char *value;
3848 int rc;
3849 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
3851 if( objc!=5 && objc!=6 ){
3852 Tcl_AppendResult(interp, "wrong # args: should be \"",
3853 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
3854 return TCL_ERROR;
3857 if( objc==6 ){
3858 xDestructor = SQLITE_STATIC;
3859 objv++;
3862 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3863 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3865 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &len);
3866 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3868 if( bytes>len ){
3869 char zBuf[200];
3870 sqlite3_snprintf(sizeof(zBuf), zBuf,
3871 "cannot use %d blob bytes, have %d", bytes, len);
3872 Tcl_AppendResult(interp, zBuf, -1);
3873 return TCL_ERROR;
3876 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
3877 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3878 if( rc!=SQLITE_OK ){
3879 return TCL_ERROR;
3882 return TCL_OK;
3886 ** Usage: sqlite3_bind_parameter_count STMT
3888 ** Return the number of wildcards in the given statement.
3890 static int SQLITE_TCLAPI test_bind_parameter_count(
3891 void * clientData,
3892 Tcl_Interp *interp,
3893 int objc,
3894 Tcl_Obj *CONST objv[]
3896 sqlite3_stmt *pStmt;
3898 if( objc!=2 ){
3899 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3900 return TCL_ERROR;
3902 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3903 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
3904 return TCL_OK;
3908 ** Usage: sqlite3_bind_parameter_name STMT N
3910 ** Return the name of the Nth wildcard. The first wildcard is 1.
3911 ** An empty string is returned if N is out of range or if the wildcard
3912 ** is nameless.
3914 static int SQLITE_TCLAPI test_bind_parameter_name(
3915 void * clientData,
3916 Tcl_Interp *interp,
3917 int objc,
3918 Tcl_Obj *CONST objv[]
3920 sqlite3_stmt *pStmt;
3921 int i;
3923 if( objc!=3 ){
3924 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
3925 return TCL_ERROR;
3927 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3928 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
3929 Tcl_SetObjResult(interp,
3930 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
3932 return TCL_OK;
3936 ** Usage: sqlite3_bind_parameter_index STMT NAME
3938 ** Return the index of the wildcard called NAME. Return 0 if there is
3939 ** no such wildcard.
3941 static int SQLITE_TCLAPI test_bind_parameter_index(
3942 void * clientData,
3943 Tcl_Interp *interp,
3944 int objc,
3945 Tcl_Obj *CONST objv[]
3947 sqlite3_stmt *pStmt;
3949 if( objc!=3 ){
3950 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
3951 return TCL_ERROR;
3953 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3954 Tcl_SetObjResult(interp,
3955 Tcl_NewIntObj(
3956 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
3959 return TCL_OK;
3963 ** Usage: sqlite3_clear_bindings STMT
3966 static int SQLITE_TCLAPI test_clear_bindings(
3967 void * clientData,
3968 Tcl_Interp *interp,
3969 int objc,
3970 Tcl_Obj *CONST objv[]
3972 sqlite3_stmt *pStmt;
3974 if( objc!=2 ){
3975 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3976 return TCL_ERROR;
3978 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3979 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
3980 return TCL_OK;
3984 ** Usage: sqlite3_sleep MILLISECONDS
3986 static int SQLITE_TCLAPI test_sleep(
3987 void * clientData,
3988 Tcl_Interp *interp,
3989 int objc,
3990 Tcl_Obj *CONST objv[]
3992 int ms;
3994 if( objc!=2 ){
3995 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
3996 return TCL_ERROR;
3998 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
3999 return TCL_ERROR;
4001 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
4002 return TCL_OK;
4006 ** Usage: sqlite3_extended_errcode DB
4008 ** Return the string representation of the most recent sqlite3_* API
4009 ** error code. e.g. "SQLITE_ERROR".
4011 static int SQLITE_TCLAPI test_ex_errcode(
4012 void * clientData,
4013 Tcl_Interp *interp,
4014 int objc,
4015 Tcl_Obj *CONST objv[]
4017 sqlite3 *db;
4018 int rc;
4020 if( objc!=2 ){
4021 Tcl_AppendResult(interp, "wrong # args: should be \"",
4022 Tcl_GetString(objv[0]), " DB", 0);
4023 return TCL_ERROR;
4025 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4026 rc = sqlite3_extended_errcode(db);
4027 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4028 return TCL_OK;
4033 ** Usage: sqlite3_errcode DB
4035 ** Return the string representation of the most recent sqlite3_* API
4036 ** error code. e.g. "SQLITE_ERROR".
4038 static int SQLITE_TCLAPI test_errcode(
4039 void * clientData,
4040 Tcl_Interp *interp,
4041 int objc,
4042 Tcl_Obj *CONST objv[]
4044 sqlite3 *db;
4045 int rc;
4047 if( objc!=2 ){
4048 Tcl_AppendResult(interp, "wrong # args: should be \"",
4049 Tcl_GetString(objv[0]), " DB", 0);
4050 return TCL_ERROR;
4052 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4053 rc = sqlite3_errcode(db);
4054 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4055 return TCL_OK;
4059 ** Usage: sqlite3_errmsg DB
4061 ** Returns the UTF-8 representation of the error message string for the
4062 ** most recent sqlite3_* API call.
4064 static int SQLITE_TCLAPI test_errmsg(
4065 void * clientData,
4066 Tcl_Interp *interp,
4067 int objc,
4068 Tcl_Obj *CONST objv[]
4070 sqlite3 *db;
4071 const char *zErr;
4073 if( objc!=2 ){
4074 Tcl_AppendResult(interp, "wrong # args: should be \"",
4075 Tcl_GetString(objv[0]), " DB", 0);
4076 return TCL_ERROR;
4078 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4080 zErr = sqlite3_errmsg(db);
4081 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
4082 return TCL_OK;
4086 ** Usage: test_errmsg16 DB
4088 ** Returns the UTF-16 representation of the error message string for the
4089 ** most recent sqlite3_* API call. This is a byte array object at the TCL
4090 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
4091 ** UTF-16 string.
4093 static int SQLITE_TCLAPI test_errmsg16(
4094 void * clientData,
4095 Tcl_Interp *interp,
4096 int objc,
4097 Tcl_Obj *CONST objv[]
4099 #ifndef SQLITE_OMIT_UTF16
4100 sqlite3 *db;
4101 const void *zErr;
4102 const char *z;
4103 int bytes = 0;
4105 if( objc!=2 ){
4106 Tcl_AppendResult(interp, "wrong # args: should be \"",
4107 Tcl_GetString(objv[0]), " DB", 0);
4108 return TCL_ERROR;
4110 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4112 zErr = sqlite3_errmsg16(db);
4113 if( zErr ){
4114 z = zErr;
4115 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
4117 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
4118 #endif /* SQLITE_OMIT_UTF16 */
4119 return TCL_OK;
4123 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
4125 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4126 ** database handle <DB>. The parameter <tailval> is the name of a global
4127 ** variable that is set to the unused portion of <sql> (if any). A
4128 ** STMT handle is returned.
4130 static int SQLITE_TCLAPI test_prepare(
4131 void * clientData,
4132 Tcl_Interp *interp,
4133 int objc,
4134 Tcl_Obj *CONST objv[]
4136 sqlite3 *db;
4137 const char *zSql;
4138 int bytes;
4139 const char *zTail = 0;
4140 sqlite3_stmt *pStmt = 0;
4141 char zBuf[50];
4142 int rc;
4144 if( objc!=5 && objc!=4 ){
4145 Tcl_AppendResult(interp, "wrong # args: should be \"",
4146 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4147 return TCL_ERROR;
4149 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4150 zSql = Tcl_GetString(objv[2]);
4151 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4153 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4154 Tcl_ResetResult(interp);
4155 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4156 if( zTail && objc>=5 ){
4157 if( bytes>=0 ){
4158 bytes = bytes - (int)(zTail-zSql);
4160 if( (int)strlen(zTail)<bytes ){
4161 bytes = (int)strlen(zTail);
4163 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4165 if( rc!=SQLITE_OK ){
4166 assert( pStmt==0 );
4167 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4168 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4169 return TCL_ERROR;
4172 if( pStmt ){
4173 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4174 Tcl_AppendResult(interp, zBuf, 0);
4176 return TCL_OK;
4180 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
4182 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4183 ** database handle <DB>. The parameter <tailval> is the name of a global
4184 ** variable that is set to the unused portion of <sql> (if any). A
4185 ** STMT handle is returned.
4187 static int SQLITE_TCLAPI test_prepare_v2(
4188 void * clientData,
4189 Tcl_Interp *interp,
4190 int objc,
4191 Tcl_Obj *CONST objv[]
4193 sqlite3 *db;
4194 const char *zSql;
4195 char *zCopy = 0; /* malloc() copy of zSql */
4196 int bytes;
4197 const char *zTail = 0;
4198 sqlite3_stmt *pStmt = 0;
4199 char zBuf[50];
4200 int rc;
4202 if( objc!=5 && objc!=4 ){
4203 Tcl_AppendResult(interp, "wrong # args: should be \"",
4204 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
4205 return TCL_ERROR;
4207 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4208 zSql = Tcl_GetString(objv[2]);
4209 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4211 /* Instead of using zSql directly, make a copy into a buffer obtained
4212 ** directly from malloc(). The idea is to make it easier for valgrind
4213 ** to spot buffer overreads. */
4214 if( bytes>=0 ){
4215 zCopy = malloc(bytes);
4216 memcpy(zCopy, zSql, bytes);
4217 }else{
4218 int n = (int)strlen(zSql) + 1;
4219 zCopy = malloc(n);
4220 memcpy(zCopy, zSql, n);
4222 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0);
4223 free(zCopy);
4224 zTail = &zSql[(zTail - zCopy)];
4226 assert(rc==SQLITE_OK || pStmt==0);
4227 Tcl_ResetResult(interp);
4228 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4229 if( rc==SQLITE_OK && zTail && objc>=5 ){
4230 if( bytes>=0 ){
4231 bytes = bytes - (int)(zTail-zSql);
4233 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4235 if( rc!=SQLITE_OK ){
4236 assert( pStmt==0 );
4237 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4238 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4239 return TCL_ERROR;
4242 if( pStmt ){
4243 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4244 Tcl_AppendResult(interp, zBuf, 0);
4246 return TCL_OK;
4250 ** Usage: sqlite3_prepare_tkt3134 DB
4252 ** Generate a prepared statement for a zero-byte string as a test
4253 ** for ticket #3134. The string should be preceded by a zero byte.
4255 static int SQLITE_TCLAPI test_prepare_tkt3134(
4256 void * clientData,
4257 Tcl_Interp *interp,
4258 int objc,
4259 Tcl_Obj *CONST objv[]
4261 sqlite3 *db;
4262 static const char zSql[] = "\000SELECT 1";
4263 sqlite3_stmt *pStmt = 0;
4264 char zBuf[50];
4265 int rc;
4267 if( objc!=2 ){
4268 Tcl_AppendResult(interp, "wrong # args: should be \"",
4269 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
4270 return TCL_ERROR;
4272 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4273 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
4274 assert(rc==SQLITE_OK || pStmt==0);
4275 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4276 if( rc!=SQLITE_OK ){
4277 assert( pStmt==0 );
4278 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4279 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4280 return TCL_ERROR;
4283 if( pStmt ){
4284 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4285 Tcl_AppendResult(interp, zBuf, 0);
4287 return TCL_OK;
4291 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
4293 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4294 ** database handle <DB>. The parameter <tailval> is the name of a global
4295 ** variable that is set to the unused portion of <sql> (if any). A
4296 ** STMT handle is returned.
4298 static int SQLITE_TCLAPI test_prepare16(
4299 void * clientData,
4300 Tcl_Interp *interp,
4301 int objc,
4302 Tcl_Obj *CONST objv[]
4304 #ifndef SQLITE_OMIT_UTF16
4305 sqlite3 *db;
4306 const void *zSql;
4307 const void *zTail = 0;
4308 Tcl_Obj *pTail = 0;
4309 sqlite3_stmt *pStmt = 0;
4310 char zBuf[50];
4311 int rc;
4312 int bytes; /* The integer specified as arg 3 */
4313 int objlen; /* The byte-array length of arg 2 */
4315 if( objc!=5 && objc!=4 ){
4316 Tcl_AppendResult(interp, "wrong # args: should be \"",
4317 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4318 return TCL_ERROR;
4320 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4321 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
4322 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4324 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4325 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4326 if( rc ){
4327 return TCL_ERROR;
4330 if( objc>=5 ){
4331 if( zTail ){
4332 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
4333 }else{
4334 objlen = 0;
4336 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
4337 Tcl_IncrRefCount(pTail);
4338 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
4339 Tcl_DecrRefCount(pTail);
4342 if( pStmt ){
4343 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4345 Tcl_AppendResult(interp, zBuf, 0);
4346 #endif /* SQLITE_OMIT_UTF16 */
4347 return TCL_OK;
4351 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
4353 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4354 ** database handle <DB>. The parameter <tailval> is the name of a global
4355 ** variable that is set to the unused portion of <sql> (if any). A
4356 ** STMT handle is returned.
4358 static int SQLITE_TCLAPI test_prepare16_v2(
4359 void * clientData,
4360 Tcl_Interp *interp,
4361 int objc,
4362 Tcl_Obj *CONST objv[]
4364 #ifndef SQLITE_OMIT_UTF16
4365 sqlite3 *db;
4366 const void *zSql;
4367 const void *zTail = 0;
4368 Tcl_Obj *pTail = 0;
4369 sqlite3_stmt *pStmt = 0;
4370 char zBuf[50];
4371 int rc;
4372 int bytes; /* The integer specified as arg 3 */
4373 int objlen; /* The byte-array length of arg 2 */
4375 if( objc!=5 && objc!=4 ){
4376 Tcl_AppendResult(interp, "wrong # args: should be \"",
4377 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4378 return TCL_ERROR;
4380 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4381 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
4382 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4384 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4385 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4386 if( rc ){
4387 return TCL_ERROR;
4390 if( objc>=5 ){
4391 if( zTail ){
4392 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
4393 }else{
4394 objlen = 0;
4396 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
4397 Tcl_IncrRefCount(pTail);
4398 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
4399 Tcl_DecrRefCount(pTail);
4402 if( pStmt ){
4403 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4405 Tcl_AppendResult(interp, zBuf, 0);
4406 #endif /* SQLITE_OMIT_UTF16 */
4407 return TCL_OK;
4411 ** Usage: sqlite3_open filename ?options-list?
4413 static int SQLITE_TCLAPI test_open(
4414 void * clientData,
4415 Tcl_Interp *interp,
4416 int objc,
4417 Tcl_Obj *CONST objv[]
4419 const char *zFilename;
4420 sqlite3 *db;
4421 char zBuf[100];
4423 if( objc!=3 && objc!=2 && objc!=1 ){
4424 Tcl_AppendResult(interp, "wrong # args: should be \"",
4425 Tcl_GetString(objv[0]), " filename options-list", 0);
4426 return TCL_ERROR;
4429 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
4430 sqlite3_open(zFilename, &db);
4432 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4433 Tcl_AppendResult(interp, zBuf, 0);
4434 return TCL_OK;
4438 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
4440 static int SQLITE_TCLAPI test_open_v2(
4441 void * clientData,
4442 Tcl_Interp *interp,
4443 int objc,
4444 Tcl_Obj *CONST objv[]
4446 const char *zFilename;
4447 const char *zVfs;
4448 int flags = 0;
4449 sqlite3 *db;
4450 int rc;
4451 char zBuf[100];
4453 int nFlag;
4454 Tcl_Obj **apFlag;
4455 int i;
4457 if( objc!=4 ){
4458 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
4459 return TCL_ERROR;
4461 zFilename = Tcl_GetString(objv[1]);
4462 zVfs = Tcl_GetString(objv[3]);
4463 if( zVfs[0]==0x00 ) zVfs = 0;
4465 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
4466 if( rc!=TCL_OK ) return rc;
4467 for(i=0; i<nFlag; i++){
4468 int iFlag;
4469 struct OpenFlag {
4470 const char *zFlag;
4471 int flag;
4472 } aFlag[] = {
4473 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
4474 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
4475 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
4476 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
4477 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
4478 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
4479 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
4480 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
4481 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
4482 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
4483 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
4484 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
4485 { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL },
4486 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
4487 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
4488 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
4489 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
4490 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
4491 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
4492 { 0, 0 }
4494 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
4495 "flag", 0, &iFlag
4497 if( rc!=TCL_OK ) return rc;
4498 flags |= aFlag[iFlag].flag;
4501 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
4502 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4503 Tcl_AppendResult(interp, zBuf, 0);
4504 return TCL_OK;
4508 ** Usage: sqlite3_open16 filename options
4510 static int SQLITE_TCLAPI test_open16(
4511 void * clientData,
4512 Tcl_Interp *interp,
4513 int objc,
4514 Tcl_Obj *CONST objv[]
4516 #ifndef SQLITE_OMIT_UTF16
4517 const void *zFilename;
4518 sqlite3 *db;
4519 char zBuf[100];
4521 if( objc!=3 ){
4522 Tcl_AppendResult(interp, "wrong # args: should be \"",
4523 Tcl_GetString(objv[0]), " filename options-list", 0);
4524 return TCL_ERROR;
4527 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
4528 sqlite3_open16(zFilename, &db);
4530 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4531 Tcl_AppendResult(interp, zBuf, 0);
4532 #endif /* SQLITE_OMIT_UTF16 */
4533 return TCL_OK;
4537 ** Usage: sqlite3_complete16 <UTF-16 string>
4539 ** Return 1 if the supplied argument is a complete SQL statement, or zero
4540 ** otherwise.
4542 static int SQLITE_TCLAPI test_complete16(
4543 void * clientData,
4544 Tcl_Interp *interp,
4545 int objc,
4546 Tcl_Obj *CONST objv[]
4548 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
4549 char *zBuf;
4551 if( objc!=2 ){
4552 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
4553 return TCL_ERROR;
4556 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
4557 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
4558 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
4559 return TCL_OK;
4563 ** Usage: sqlite3_normalize SQL
4565 ** Return the normalized value for an SQL statement.
4567 static int SQLITE_TCLAPI test_normalize(
4568 void * clientData,
4569 Tcl_Interp *interp,
4570 int objc,
4571 Tcl_Obj *CONST objv[]
4573 char *zSql;
4574 char *zNorm;
4575 extern char *sqlite3_normalize(const char*);
4577 if( objc!=2 ){
4578 Tcl_WrongNumArgs(interp, 1, objv, "SQL");
4579 return TCL_ERROR;
4582 zSql = (char*)Tcl_GetString(objv[1]);
4583 zNorm = sqlite3_normalize(zSql);
4584 if( zNorm ){
4585 Tcl_SetObjResult(interp, Tcl_NewStringObj(zNorm, -1));
4586 sqlite3_free(zNorm);
4588 return TCL_OK;
4592 ** Usage: sqlite3_step STMT
4594 ** Advance the statement to the next row.
4596 static int SQLITE_TCLAPI test_step(
4597 void * clientData,
4598 Tcl_Interp *interp,
4599 int objc,
4600 Tcl_Obj *CONST objv[]
4602 sqlite3_stmt *pStmt;
4603 int rc;
4605 if( objc!=2 ){
4606 Tcl_AppendResult(interp, "wrong # args: should be \"",
4607 Tcl_GetString(objv[0]), " STMT", 0);
4608 return TCL_ERROR;
4611 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4612 rc = sqlite3_step(pStmt);
4614 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
4615 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
4616 return TCL_OK;
4619 static int SQLITE_TCLAPI test_sql(
4620 void * clientData,
4621 Tcl_Interp *interp,
4622 int objc,
4623 Tcl_Obj *CONST objv[]
4625 sqlite3_stmt *pStmt;
4627 if( objc!=2 ){
4628 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4629 return TCL_ERROR;
4632 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4633 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
4634 return TCL_OK;
4636 static int SQLITE_TCLAPI test_ex_sql(
4637 void * clientData,
4638 Tcl_Interp *interp,
4639 int objc,
4640 Tcl_Obj *CONST objv[]
4642 sqlite3_stmt *pStmt;
4643 char *z;
4645 if( objc!=2 ){
4646 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4647 return TCL_ERROR;
4650 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4651 z = sqlite3_expanded_sql(pStmt);
4652 Tcl_SetResult(interp, z, TCL_VOLATILE);
4653 sqlite3_free(z);
4654 return TCL_OK;
4658 ** Usage: sqlite3_column_count STMT
4660 ** Return the number of columns returned by the sql statement STMT.
4662 static int SQLITE_TCLAPI test_column_count(
4663 void * clientData,
4664 Tcl_Interp *interp,
4665 int objc,
4666 Tcl_Obj *CONST objv[]
4668 sqlite3_stmt *pStmt;
4670 if( objc!=2 ){
4671 Tcl_AppendResult(interp, "wrong # args: should be \"",
4672 Tcl_GetString(objv[0]), " STMT column", 0);
4673 return TCL_ERROR;
4676 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4678 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
4679 return TCL_OK;
4683 ** Usage: sqlite3_column_type STMT column
4685 ** Return the type of the data in column 'column' of the current row.
4687 static int SQLITE_TCLAPI test_column_type(
4688 void * clientData,
4689 Tcl_Interp *interp,
4690 int objc,
4691 Tcl_Obj *CONST objv[]
4693 sqlite3_stmt *pStmt;
4694 int col;
4695 int tp;
4697 if( objc!=3 ){
4698 Tcl_AppendResult(interp, "wrong # args: should be \"",
4699 Tcl_GetString(objv[0]), " STMT column", 0);
4700 return TCL_ERROR;
4703 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4704 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4706 tp = sqlite3_column_type(pStmt, col);
4707 switch( tp ){
4708 case SQLITE_INTEGER:
4709 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
4710 break;
4711 case SQLITE_NULL:
4712 Tcl_SetResult(interp, "NULL", TCL_STATIC);
4713 break;
4714 case SQLITE_FLOAT:
4715 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
4716 break;
4717 case SQLITE_TEXT:
4718 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
4719 break;
4720 case SQLITE_BLOB:
4721 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
4722 break;
4723 default:
4724 assert(0);
4727 return TCL_OK;
4731 ** Usage: sqlite3_column_int64 STMT column
4733 ** Return the data in column 'column' of the current row cast as an
4734 ** wide (64-bit) integer.
4736 static int SQLITE_TCLAPI test_column_int64(
4737 void * clientData,
4738 Tcl_Interp *interp,
4739 int objc,
4740 Tcl_Obj *CONST objv[]
4742 sqlite3_stmt *pStmt;
4743 int col;
4744 i64 iVal;
4746 if( objc!=3 ){
4747 Tcl_AppendResult(interp, "wrong # args: should be \"",
4748 Tcl_GetString(objv[0]), " STMT column", 0);
4749 return TCL_ERROR;
4752 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4753 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4755 iVal = sqlite3_column_int64(pStmt, col);
4756 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
4757 return TCL_OK;
4761 ** Usage: sqlite3_column_blob STMT column
4763 static int SQLITE_TCLAPI test_column_blob(
4764 void * clientData,
4765 Tcl_Interp *interp,
4766 int objc,
4767 Tcl_Obj *CONST objv[]
4769 sqlite3_stmt *pStmt;
4770 int col;
4772 int len;
4773 const void *pBlob;
4775 if( objc!=3 ){
4776 Tcl_AppendResult(interp, "wrong # args: should be \"",
4777 Tcl_GetString(objv[0]), " STMT column", 0);
4778 return TCL_ERROR;
4781 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4782 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4784 len = sqlite3_column_bytes(pStmt, col);
4785 pBlob = sqlite3_column_blob(pStmt, col);
4786 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
4787 return TCL_OK;
4791 ** Usage: sqlite3_column_double STMT column
4793 ** Return the data in column 'column' of the current row cast as a double.
4795 static int SQLITE_TCLAPI test_column_double(
4796 void * clientData,
4797 Tcl_Interp *interp,
4798 int objc,
4799 Tcl_Obj *CONST objv[]
4801 sqlite3_stmt *pStmt;
4802 int col;
4803 double rVal;
4805 if( objc!=3 ){
4806 Tcl_AppendResult(interp, "wrong # args: should be \"",
4807 Tcl_GetString(objv[0]), " STMT column", 0);
4808 return TCL_ERROR;
4811 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4812 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4814 rVal = sqlite3_column_double(pStmt, col);
4815 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
4816 return TCL_OK;
4820 ** Usage: sqlite3_data_count STMT
4822 ** Return the number of columns returned by the sql statement STMT.
4824 static int SQLITE_TCLAPI test_data_count(
4825 void * clientData,
4826 Tcl_Interp *interp,
4827 int objc,
4828 Tcl_Obj *CONST objv[]
4830 sqlite3_stmt *pStmt;
4832 if( objc!=2 ){
4833 Tcl_AppendResult(interp, "wrong # args: should be \"",
4834 Tcl_GetString(objv[0]), " STMT column", 0);
4835 return TCL_ERROR;
4838 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4840 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
4841 return TCL_OK;
4845 ** Usage: sqlite3_column_text STMT column
4847 ** Usage: sqlite3_column_decltype STMT column
4849 ** Usage: sqlite3_column_name STMT column
4851 static int SQLITE_TCLAPI test_stmt_utf8(
4852 void * clientData, /* Pointer to SQLite API function to be invoke */
4853 Tcl_Interp *interp,
4854 int objc,
4855 Tcl_Obj *CONST objv[]
4857 sqlite3_stmt *pStmt;
4858 int col;
4859 const char *(*xFunc)(sqlite3_stmt*, int);
4860 const char *zRet;
4862 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
4863 if( objc!=3 ){
4864 Tcl_AppendResult(interp, "wrong # args: should be \"",
4865 Tcl_GetString(objv[0]), " STMT column", 0);
4866 return TCL_ERROR;
4869 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4870 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4871 zRet = xFunc(pStmt, col);
4872 if( zRet ){
4873 Tcl_SetResult(interp, (char *)zRet, 0);
4875 return TCL_OK;
4878 static int SQLITE_TCLAPI test_global_recover(
4879 void * clientData,
4880 Tcl_Interp *interp,
4881 int objc,
4882 Tcl_Obj *CONST objv[]
4884 #ifndef SQLITE_OMIT_DEPRECATED
4885 int rc;
4886 if( objc!=1 ){
4887 Tcl_WrongNumArgs(interp, 1, objv, "");
4888 return TCL_ERROR;
4890 rc = sqlite3_global_recover();
4891 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4892 #endif
4893 return TCL_OK;
4897 ** Usage: sqlite3_column_text STMT column
4899 ** Usage: sqlite3_column_decltype STMT column
4901 ** Usage: sqlite3_column_name STMT column
4903 static int SQLITE_TCLAPI test_stmt_utf16(
4904 void * clientData, /* Pointer to SQLite API function to be invoked */
4905 Tcl_Interp *interp,
4906 int objc,
4907 Tcl_Obj *CONST objv[]
4909 #ifndef SQLITE_OMIT_UTF16
4910 sqlite3_stmt *pStmt;
4911 int col;
4912 Tcl_Obj *pRet;
4913 const void *zName16;
4914 const void *(*xFunc)(sqlite3_stmt*, int);
4916 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
4917 if( objc!=3 ){
4918 Tcl_AppendResult(interp, "wrong # args: should be \"",
4919 Tcl_GetString(objv[0]), " STMT column", 0);
4920 return TCL_ERROR;
4923 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4924 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4926 zName16 = xFunc(pStmt, col);
4927 if( zName16 ){
4928 int n;
4929 const char *z = zName16;
4930 for(n=0; z[n] || z[n+1]; n+=2){}
4931 pRet = Tcl_NewByteArrayObj(zName16, n+2);
4932 Tcl_SetObjResult(interp, pRet);
4934 #endif /* SQLITE_OMIT_UTF16 */
4936 return TCL_OK;
4940 ** Usage: sqlite3_column_int STMT column
4942 ** Usage: sqlite3_column_bytes STMT column
4944 ** Usage: sqlite3_column_bytes16 STMT column
4947 static int SQLITE_TCLAPI test_stmt_int(
4948 void * clientData, /* Pointer to SQLite API function to be invoked */
4949 Tcl_Interp *interp,
4950 int objc,
4951 Tcl_Obj *CONST objv[]
4953 sqlite3_stmt *pStmt;
4954 int col;
4955 int (*xFunc)(sqlite3_stmt*, int);
4957 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
4958 if( objc!=3 ){
4959 Tcl_AppendResult(interp, "wrong # args: should be \"",
4960 Tcl_GetString(objv[0]), " STMT column", 0);
4961 return TCL_ERROR;
4964 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4965 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4967 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
4968 return TCL_OK;
4972 ** Usage: sqlite_set_magic DB MAGIC-NUMBER
4974 ** Set the db->magic value. This is used to test error recovery logic.
4976 static int SQLITE_TCLAPI sqlite_set_magic(
4977 void * clientData,
4978 Tcl_Interp *interp,
4979 int argc,
4980 char **argv
4982 sqlite3 *db;
4983 if( argc!=3 ){
4984 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4985 " DB MAGIC", 0);
4986 return TCL_ERROR;
4988 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4989 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
4990 db->magic = SQLITE_MAGIC_OPEN;
4991 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
4992 db->magic = SQLITE_MAGIC_CLOSED;
4993 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
4994 db->magic = SQLITE_MAGIC_BUSY;
4995 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
4996 db->magic = SQLITE_MAGIC_ERROR;
4997 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){
4998 return TCL_ERROR;
5000 return TCL_OK;
5004 ** Usage: sqlite3_interrupt DB
5006 ** Trigger an interrupt on DB
5008 static int SQLITE_TCLAPI test_interrupt(
5009 void * clientData,
5010 Tcl_Interp *interp,
5011 int argc,
5012 char **argv
5014 sqlite3 *db;
5015 if( argc!=2 ){
5016 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5017 return TCL_ERROR;
5019 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5020 sqlite3_interrupt(db);
5021 return TCL_OK;
5025 ** Usage: sqlite_delete_function DB function-name
5027 ** Delete the user function 'function-name' from database handle DB. It
5028 ** is assumed that the user function was created as UTF8, any number of
5029 ** arguments (the way the TCL interface does it).
5031 static int SQLITE_TCLAPI delete_function(
5032 void * clientData,
5033 Tcl_Interp *interp,
5034 int argc,
5035 char **argv
5037 int rc;
5038 sqlite3 *db;
5039 if( argc!=3 ){
5040 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5041 " DB function-name", 0);
5042 return TCL_ERROR;
5044 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5045 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
5046 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5047 return TCL_OK;
5051 ** Usage: sqlite_delete_collation DB collation-name
5053 ** Delete the collation sequence 'collation-name' from database handle
5054 ** DB. It is assumed that the collation sequence was created as UTF8 (the
5055 ** way the TCL interface does it).
5057 static int SQLITE_TCLAPI delete_collation(
5058 void * clientData,
5059 Tcl_Interp *interp,
5060 int argc,
5061 char **argv
5063 int rc;
5064 sqlite3 *db;
5065 if( argc!=3 ){
5066 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5067 " DB function-name", 0);
5068 return TCL_ERROR;
5070 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5071 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
5072 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5073 return TCL_OK;
5077 ** Usage: sqlite3_get_autocommit DB
5079 ** Return true if the database DB is currently in auto-commit mode.
5080 ** Return false if not.
5082 static int SQLITE_TCLAPI get_autocommit(
5083 void * clientData,
5084 Tcl_Interp *interp,
5085 int argc,
5086 char **argv
5088 char zBuf[30];
5089 sqlite3 *db;
5090 if( argc!=2 ){
5091 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5092 " DB", 0);
5093 return TCL_ERROR;
5095 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5096 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db));
5097 Tcl_AppendResult(interp, zBuf, 0);
5098 return TCL_OK;
5102 ** Usage: sqlite3_busy_timeout DB MS
5104 ** Set the busy timeout. This is more easily done using the timeout
5105 ** method of the TCL interface. But we need a way to test the case
5106 ** where it returns SQLITE_MISUSE.
5108 static int SQLITE_TCLAPI test_busy_timeout(
5109 void * clientData,
5110 Tcl_Interp *interp,
5111 int argc,
5112 char **argv
5114 int rc, ms;
5115 sqlite3 *db;
5116 if( argc!=3 ){
5117 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5118 " DB", 0);
5119 return TCL_ERROR;
5121 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5122 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
5123 rc = sqlite3_busy_timeout(db, ms);
5124 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
5125 return TCL_OK;
5129 ** Usage: tcl_variable_type VARIABLENAME
5131 ** Return the name of the internal representation for the
5132 ** value of the given variable.
5134 static int SQLITE_TCLAPI tcl_variable_type(
5135 void * clientData,
5136 Tcl_Interp *interp,
5137 int objc,
5138 Tcl_Obj *CONST objv[]
5140 Tcl_Obj *pVar;
5141 if( objc!=2 ){
5142 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
5143 return TCL_ERROR;
5145 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
5146 if( pVar==0 ) return TCL_ERROR;
5147 if( pVar->typePtr ){
5148 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
5150 return TCL_OK;
5154 ** Usage: sqlite3_release_memory ?N?
5156 ** Attempt to release memory currently held but not actually required.
5157 ** The integer N is the number of bytes we are trying to release. The
5158 ** return value is the amount of memory actually released.
5160 static int SQLITE_TCLAPI test_release_memory(
5161 void * clientData,
5162 Tcl_Interp *interp,
5163 int objc,
5164 Tcl_Obj *CONST objv[]
5166 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
5167 int N;
5168 int amt;
5169 if( objc!=1 && objc!=2 ){
5170 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
5171 return TCL_ERROR;
5173 if( objc==2 ){
5174 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
5175 }else{
5176 N = -1;
5178 amt = sqlite3_release_memory(N);
5179 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
5180 #endif
5181 return TCL_OK;
5186 ** Usage: sqlite3_db_release_memory DB
5188 ** Attempt to release memory currently held by database DB. Return the
5189 ** result code (which in the current implementation is always zero).
5191 static int SQLITE_TCLAPI test_db_release_memory(
5192 void * clientData,
5193 Tcl_Interp *interp,
5194 int objc,
5195 Tcl_Obj *CONST objv[]
5197 sqlite3 *db;
5198 int rc;
5199 if( objc!=2 ){
5200 Tcl_WrongNumArgs(interp, 1, objv, "DB");
5201 return TCL_ERROR;
5203 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5204 rc = sqlite3_db_release_memory(db);
5205 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5206 return TCL_OK;
5210 ** Usage: sqlite3_db_cacheflush DB
5212 ** Attempt to flush any dirty pages to disk.
5214 static int SQLITE_TCLAPI test_db_cacheflush(
5215 void * clientData,
5216 Tcl_Interp *interp,
5217 int objc,
5218 Tcl_Obj *CONST objv[]
5220 sqlite3 *db;
5221 int rc;
5222 if( objc!=2 ){
5223 Tcl_WrongNumArgs(interp, 1, objv, "DB");
5224 return TCL_ERROR;
5226 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5227 rc = sqlite3_db_cacheflush(db);
5228 if( rc ){
5229 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
5230 return TCL_ERROR;
5233 Tcl_ResetResult(interp);
5234 return TCL_OK;
5238 ** Usage: sqlite3_system_errno DB
5240 ** Return the low-level system errno value.
5242 static int SQLITE_TCLAPI test_system_errno(
5243 void * clientData,
5244 Tcl_Interp *interp,
5245 int objc,
5246 Tcl_Obj *CONST objv[]
5248 sqlite3 *db;
5249 int iErrno;
5250 if( objc!=2 ){
5251 Tcl_WrongNumArgs(interp, 1, objv, "DB");
5252 return TCL_ERROR;
5254 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5255 iErrno = sqlite3_system_errno(db);
5256 Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno));
5257 return TCL_OK;
5261 ** Usage: sqlite3_db_filename DB DBNAME
5263 ** Return the name of a file associated with a database.
5265 static int SQLITE_TCLAPI test_db_filename(
5266 void * clientData,
5267 Tcl_Interp *interp,
5268 int objc,
5269 Tcl_Obj *CONST objv[]
5271 sqlite3 *db;
5272 const char *zDbName;
5273 if( objc!=3 ){
5274 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
5275 return TCL_ERROR;
5277 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5278 zDbName = Tcl_GetString(objv[2]);
5279 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
5280 return TCL_OK;
5284 ** Usage: sqlite3_db_readonly DB DBNAME
5286 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does
5287 ** not exist.
5289 static int SQLITE_TCLAPI test_db_readonly(
5290 void * clientData,
5291 Tcl_Interp *interp,
5292 int objc,
5293 Tcl_Obj *CONST objv[]
5295 sqlite3 *db;
5296 const char *zDbName;
5297 if( objc!=3 ){
5298 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
5299 return TCL_ERROR;
5301 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5302 zDbName = Tcl_GetString(objv[2]);
5303 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
5304 return TCL_OK;
5308 ** Usage: sqlite3_soft_heap_limit ?N?
5310 ** Query or set the soft heap limit for the current thread. The
5311 ** limit is only changed if the N is present. The previous limit
5312 ** is returned.
5314 static int SQLITE_TCLAPI test_soft_heap_limit(
5315 void * clientData,
5316 Tcl_Interp *interp,
5317 int objc,
5318 Tcl_Obj *CONST objv[]
5320 sqlite3_int64 amt;
5321 Tcl_WideInt N = -1;
5322 if( objc!=1 && objc!=2 ){
5323 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
5324 return TCL_ERROR;
5326 if( objc==2 ){
5327 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
5329 amt = sqlite3_soft_heap_limit64(N);
5330 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
5331 return TCL_OK;
5335 ** Usage: sqlite3_thread_cleanup
5337 ** Call the sqlite3_thread_cleanup API.
5339 static int SQLITE_TCLAPI test_thread_cleanup(
5340 void * clientData,
5341 Tcl_Interp *interp,
5342 int objc,
5343 Tcl_Obj *CONST objv[]
5345 #ifndef SQLITE_OMIT_DEPRECATED
5346 sqlite3_thread_cleanup();
5347 #endif
5348 return TCL_OK;
5352 ** Usage: sqlite3_pager_refcounts DB
5354 ** Return a list of numbers which are the PagerRefcount for all
5355 ** pagers on each database connection.
5357 static int SQLITE_TCLAPI test_pager_refcounts(
5358 void * clientData,
5359 Tcl_Interp *interp,
5360 int objc,
5361 Tcl_Obj *CONST objv[]
5363 sqlite3 *db;
5364 int i;
5365 int v, *a;
5366 Tcl_Obj *pResult;
5368 if( objc!=2 ){
5369 Tcl_AppendResult(interp, "wrong # args: should be \"",
5370 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5371 return TCL_ERROR;
5373 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5374 pResult = Tcl_NewObj();
5375 for(i=0; i<db->nDb; i++){
5376 if( db->aDb[i].pBt==0 ){
5377 v = -1;
5378 }else{
5379 sqlite3_mutex_enter(db->mutex);
5380 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
5381 v = a[0];
5382 sqlite3_mutex_leave(db->mutex);
5384 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
5386 Tcl_SetObjResult(interp, pResult);
5387 return TCL_OK;
5392 ** tclcmd: working_64bit_int
5394 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
5395 ** leads to a number of test failures. The present command checks the
5396 ** TCL build to see whether or not it supports 64-bit integers. It
5397 ** returns TRUE if it does and FALSE if not.
5399 ** This command is used to warn users that their TCL build is defective
5400 ** and that the errors they are seeing in the test scripts might be
5401 ** a result of their defective TCL rather than problems in SQLite.
5403 static int SQLITE_TCLAPI working_64bit_int(
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 */
5409 Tcl_Obj *pTestObj;
5410 int working = 0;
5412 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
5413 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
5414 Tcl_DecrRefCount(pTestObj);
5415 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
5416 return TCL_OK;
5421 ** tclcmd: vfs_unlink_test
5423 ** This TCL command unregisters the primary VFS and then registers
5424 ** it back again. This is used to test the ability to register a
5425 ** VFS when none are previously registered, and the ability to
5426 ** unregister the only available VFS. Ticket #2738
5428 static int SQLITE_TCLAPI vfs_unlink_test(
5429 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5430 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5431 int objc, /* Number of arguments */
5432 Tcl_Obj *CONST objv[] /* Command arguments */
5434 int i;
5435 sqlite3_vfs *pMain;
5436 sqlite3_vfs *apVfs[20];
5437 sqlite3_vfs one, two;
5439 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
5440 one.zName = "__one";
5441 two.zName = "__two";
5443 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
5444 ** change the default VFS
5446 pMain = sqlite3_vfs_find(0);
5447 sqlite3_vfs_register(&one, 0);
5448 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
5449 sqlite3_vfs_register(&two, 0);
5450 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
5452 /* We can find a VFS by its name */
5453 assert( sqlite3_vfs_find("__one")==&one );
5454 assert( sqlite3_vfs_find("__two")==&two );
5456 /* Calling sqlite_vfs_register with non-zero second parameter changes the
5457 ** default VFS, even if the 1st parameter is an existig VFS that is
5458 ** previously registered as the non-default.
5460 sqlite3_vfs_register(&one, 1);
5461 assert( sqlite3_vfs_find("__one")==&one );
5462 assert( sqlite3_vfs_find("__two")==&two );
5463 assert( sqlite3_vfs_find(0)==&one );
5464 sqlite3_vfs_register(&two, 1);
5465 assert( sqlite3_vfs_find("__one")==&one );
5466 assert( sqlite3_vfs_find("__two")==&two );
5467 assert( sqlite3_vfs_find(0)==&two );
5468 if( pMain ){
5469 sqlite3_vfs_register(pMain, 1);
5470 assert( sqlite3_vfs_find("__one")==&one );
5471 assert( sqlite3_vfs_find("__two")==&two );
5472 assert( sqlite3_vfs_find(0)==pMain );
5475 /* Unlink the default VFS. Repeat until there are no more VFSes
5476 ** registered.
5478 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
5479 apVfs[i] = sqlite3_vfs_find(0);
5480 if( apVfs[i] ){
5481 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
5482 sqlite3_vfs_unregister(apVfs[i]);
5483 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
5486 assert( 0==sqlite3_vfs_find(0) );
5488 /* Register the main VFS as non-default (will be made default, since
5489 ** it'll be the only one in existence).
5491 sqlite3_vfs_register(pMain, 0);
5492 assert( sqlite3_vfs_find(0)==pMain );
5494 /* Un-register the main VFS again to restore an empty VFS list */
5495 sqlite3_vfs_unregister(pMain);
5496 assert( 0==sqlite3_vfs_find(0) );
5498 /* Relink all VFSes in reverse order. */
5499 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
5500 if( apVfs[i] ){
5501 sqlite3_vfs_register(apVfs[i], 1);
5502 assert( apVfs[i]==sqlite3_vfs_find(0) );
5503 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
5507 /* Unregister out sample VFSes. */
5508 sqlite3_vfs_unregister(&one);
5509 sqlite3_vfs_unregister(&two);
5511 /* Unregistering a VFS that is not currently registered is harmless */
5512 sqlite3_vfs_unregister(&one);
5513 sqlite3_vfs_unregister(&two);
5514 assert( sqlite3_vfs_find("__one")==0 );
5515 assert( sqlite3_vfs_find("__two")==0 );
5517 /* We should be left with the original default VFS back as the
5518 ** original */
5519 assert( sqlite3_vfs_find(0)==pMain );
5521 return TCL_OK;
5525 ** tclcmd: vfs_initfail_test
5527 ** This TCL command attempts to vfs_find and vfs_register when the
5528 ** sqlite3_initialize() interface is failing. All calls should fail.
5530 static int SQLITE_TCLAPI vfs_initfail_test(
5531 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5532 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5533 int objc, /* Number of arguments */
5534 Tcl_Obj *CONST objv[] /* Command arguments */
5536 sqlite3_vfs one;
5537 one.zName = "__one";
5539 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
5540 sqlite3_vfs_register(&one, 0);
5541 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
5542 sqlite3_vfs_register(&one, 1);
5543 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
5544 return TCL_OK;
5548 ** Saved VFSes
5550 static sqlite3_vfs *apVfs[20];
5551 static int nVfs = 0;
5554 ** tclcmd: vfs_unregister_all
5556 ** Unregister all VFSes.
5558 static int SQLITE_TCLAPI vfs_unregister_all(
5559 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5560 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5561 int objc, /* Number of arguments */
5562 Tcl_Obj *CONST objv[] /* Command arguments */
5564 int i;
5565 for(i=0; i<ArraySize(apVfs); i++){
5566 apVfs[i] = sqlite3_vfs_find(0);
5567 if( apVfs[i]==0 ) break;
5568 sqlite3_vfs_unregister(apVfs[i]);
5570 nVfs = i;
5571 return TCL_OK;
5574 ** tclcmd: vfs_reregister_all
5576 ** Restore all VFSes that were removed using vfs_unregister_all. Taking
5577 ** care to put the linked list back together in the same order as it was
5578 ** in before vfs_unregister_all was invoked.
5580 static int SQLITE_TCLAPI vfs_reregister_all(
5581 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5582 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5583 int objc, /* Number of arguments */
5584 Tcl_Obj *CONST objv[] /* Command arguments */
5586 int i;
5587 for(i=nVfs-1; i>=0; i--){
5588 sqlite3_vfs_register(apVfs[i], 1);
5590 return TCL_OK;
5595 ** tclcmd: file_control_test DB
5597 ** This TCL command runs the sqlite3_file_control interface and
5598 ** verifies correct operation of the same.
5600 static int SQLITE_TCLAPI file_control_test(
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 int iArg = 0;
5607 sqlite3 *db;
5608 int rc;
5610 if( objc!=2 ){
5611 Tcl_AppendResult(interp, "wrong # args: should be \"",
5612 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5613 return TCL_ERROR;
5615 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5616 rc = sqlite3_file_control(db, 0, 0, &iArg);
5617 assert( rc==SQLITE_NOTFOUND );
5618 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
5619 assert( rc==SQLITE_ERROR );
5620 rc = sqlite3_file_control(db, "main", -1, &iArg);
5621 assert( rc==SQLITE_NOTFOUND );
5622 rc = sqlite3_file_control(db, "temp", -1, &iArg);
5623 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
5625 return TCL_OK;
5630 ** tclcmd: file_control_lasterrno_test DB
5632 ** This TCL command runs the sqlite3_file_control interface and
5633 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
5635 static int SQLITE_TCLAPI file_control_lasterrno_test(
5636 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5637 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5638 int objc, /* Number of arguments */
5639 Tcl_Obj *CONST objv[] /* Command arguments */
5641 int iArg = 0;
5642 sqlite3 *db;
5643 int rc;
5645 if( objc!=2 ){
5646 Tcl_AppendResult(interp, "wrong # args: should be \"",
5647 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5648 return TCL_ERROR;
5650 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5651 return TCL_ERROR;
5653 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
5654 if( rc ){
5655 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5656 return TCL_ERROR;
5658 if( iArg!=0 ) {
5659 Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
5660 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
5661 return TCL_ERROR;
5663 return TCL_OK;
5667 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
5669 ** This TCL command runs the sqlite3_file_control interface and
5670 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5671 ** SQLITE_SET_LOCKPROXYFILE verbs.
5673 static int SQLITE_TCLAPI file_control_chunksize_test(
5674 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5675 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5676 int objc, /* Number of arguments */
5677 Tcl_Obj *CONST objv[] /* Command arguments */
5679 int nSize; /* New chunk size */
5680 char *zDb; /* Db name ("main", "temp" etc.) */
5681 sqlite3 *db; /* Database handle */
5682 int rc; /* file_control() return code */
5684 if( objc!=4 ){
5685 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
5686 return TCL_ERROR;
5688 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5689 || Tcl_GetIntFromObj(interp, objv[3], &nSize)
5691 return TCL_ERROR;
5693 zDb = Tcl_GetString(objv[2]);
5694 if( zDb[0]=='\0' ) zDb = NULL;
5696 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
5697 if( rc ){
5698 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5699 return TCL_ERROR;
5701 return TCL_OK;
5705 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
5707 ** This TCL command runs the sqlite3_file_control interface
5708 ** with SQLITE_FCNTL_SIZE_HINT
5710 static int SQLITE_TCLAPI file_control_sizehint_test(
5711 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5712 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5713 int objc, /* Number of arguments */
5714 Tcl_Obj *CONST objv[] /* Command arguments */
5716 Tcl_WideInt nSize; /* Hinted size */
5717 char *zDb; /* Db name ("main", "temp" etc.) */
5718 sqlite3 *db; /* Database handle */
5719 int rc; /* file_control() return code */
5721 if( objc!=4 ){
5722 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
5723 return TCL_ERROR;
5725 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5726 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
5728 return TCL_ERROR;
5730 zDb = Tcl_GetString(objv[2]);
5731 if( zDb[0]=='\0' ) zDb = NULL;
5733 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
5734 if( rc ){
5735 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5736 return TCL_ERROR;
5738 return TCL_OK;
5742 ** tclcmd: file_control_lockproxy_test DB PWD
5744 ** This TCL command runs the sqlite3_file_control interface and
5745 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5746 ** SQLITE_SET_LOCKPROXYFILE verbs.
5748 static int SQLITE_TCLAPI file_control_lockproxy_test(
5749 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5750 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5751 int objc, /* Number of arguments */
5752 Tcl_Obj *CONST objv[] /* Command arguments */
5754 sqlite3 *db;
5756 if( objc!=3 ){
5757 Tcl_AppendResult(interp, "wrong # args: should be \"",
5758 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
5759 return TCL_ERROR;
5761 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5762 return TCL_ERROR;
5765 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
5766 # if defined(__APPLE__)
5767 # define SQLITE_ENABLE_LOCKING_STYLE 1
5768 # else
5769 # define SQLITE_ENABLE_LOCKING_STYLE 0
5770 # endif
5771 #endif
5772 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
5774 char *testPath;
5775 int rc;
5776 int nPwd;
5777 const char *zPwd;
5778 char proxyPath[400];
5780 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
5781 if( sizeof(proxyPath)<nPwd+20 ){
5782 Tcl_AppendResult(interp, "PWD too big", (void*)0);
5783 return TCL_ERROR;
5785 sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd);
5786 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
5787 if( rc ){
5788 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5789 return TCL_ERROR;
5791 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
5792 if( strncmp(proxyPath,testPath,11) ){
5793 Tcl_AppendResult(interp, "Lock proxy file did not match the "
5794 "previously assigned value", 0);
5795 return TCL_ERROR;
5797 if( rc ){
5798 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5799 return TCL_ERROR;
5801 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
5802 if( rc ){
5803 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5804 return TCL_ERROR;
5807 #endif
5808 return TCL_OK;
5811 #if SQLITE_OS_WIN
5813 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY
5815 ** This TCL command runs the sqlite3_file_control interface with
5816 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
5818 static int SQLITE_TCLAPI file_control_win32_av_retry(
5819 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5820 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5821 int objc, /* Number of arguments */
5822 Tcl_Obj *CONST objv[] /* Command arguments */
5824 sqlite3 *db;
5825 int rc;
5826 int a[2];
5827 char z[100];
5829 if( objc!=4 ){
5830 Tcl_AppendResult(interp, "wrong # args: should be \"",
5831 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
5832 return TCL_ERROR;
5834 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5835 return TCL_ERROR;
5837 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
5838 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
5839 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
5840 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
5841 Tcl_AppendResult(interp, z, (char*)0);
5842 return TCL_OK;
5846 ** tclcmd: file_control_win32_get_handle DB
5848 ** This TCL command runs the sqlite3_file_control interface with
5849 ** the SQLITE_FCNTL_WIN32_GET_HANDLE opcode.
5851 static int file_control_win32_get_handle(
5852 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5853 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5854 int objc, /* Number of arguments */
5855 Tcl_Obj *CONST objv[] /* Command arguments */
5857 sqlite3 *db;
5858 int rc;
5859 HANDLE hFile = NULL;
5860 char z[100];
5862 if( objc!=2 ){
5863 Tcl_AppendResult(interp, "wrong # args: should be \"",
5864 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5865 return TCL_ERROR;
5867 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5868 return TCL_ERROR;
5870 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_GET_HANDLE,
5871 (void*)&hFile);
5872 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
5873 Tcl_AppendResult(interp, z, (char*)0);
5874 return TCL_OK;
5878 ** tclcmd: file_control_win32_set_handle DB HANDLE
5880 ** This TCL command runs the sqlite3_file_control interface with
5881 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
5883 static int SQLITE_TCLAPI file_control_win32_set_handle(
5884 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5885 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5886 int objc, /* Number of arguments */
5887 Tcl_Obj *CONST objv[] /* Command arguments */
5889 sqlite3 *db;
5890 int rc;
5891 HANDLE hFile = NULL;
5892 char z[100];
5894 if( objc!=3 ){
5895 Tcl_AppendResult(interp, "wrong # args: should be \"",
5896 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0);
5897 return TCL_ERROR;
5899 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5900 return TCL_ERROR;
5902 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){
5903 return TCL_ERROR;
5905 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE,
5906 (void*)&hFile);
5907 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
5908 Tcl_AppendResult(interp, z, (char*)0);
5909 return TCL_OK;
5911 #endif
5914 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG
5916 ** This TCL command runs the sqlite3_file_control interface with
5917 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
5919 static int SQLITE_TCLAPI file_control_persist_wal(
5920 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5921 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5922 int objc, /* Number of arguments */
5923 Tcl_Obj *CONST objv[] /* Command arguments */
5925 sqlite3 *db;
5926 int rc;
5927 int bPersist;
5928 char z[100];
5930 if( objc!=3 ){
5931 Tcl_AppendResult(interp, "wrong # args: should be \"",
5932 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
5933 return TCL_ERROR;
5935 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5936 return TCL_ERROR;
5938 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
5939 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
5940 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
5941 Tcl_AppendResult(interp, z, (char*)0);
5942 return TCL_OK;
5946 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG
5948 ** This TCL command runs the sqlite3_file_control interface with
5949 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
5951 static int SQLITE_TCLAPI file_control_powersafe_overwrite(
5952 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5953 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5954 int objc, /* Number of arguments */
5955 Tcl_Obj *CONST objv[] /* Command arguments */
5957 sqlite3 *db;
5958 int rc;
5959 int b;
5960 char z[100];
5962 if( objc!=3 ){
5963 Tcl_AppendResult(interp, "wrong # args: should be \"",
5964 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
5965 return TCL_ERROR;
5967 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5968 return TCL_ERROR;
5970 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
5971 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
5972 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
5973 Tcl_AppendResult(interp, z, (char*)0);
5974 return TCL_OK;
5979 ** tclcmd: file_control_vfsname DB ?AUXDB?
5981 ** Return a string that describes the stack of VFSes.
5983 static int SQLITE_TCLAPI file_control_vfsname(
5984 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5985 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
5986 int objc, /* Number of arguments */
5987 Tcl_Obj *CONST objv[] /* Command arguments */
5989 sqlite3 *db;
5990 const char *zDbName = "main";
5991 char *zVfsName = 0;
5993 if( objc!=2 && objc!=3 ){
5994 Tcl_AppendResult(interp, "wrong # args: should be \"",
5995 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
5996 return TCL_ERROR;
5998 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5999 return TCL_ERROR;
6001 if( objc==3 ){
6002 zDbName = Tcl_GetString(objv[2]);
6004 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
6005 Tcl_AppendResult(interp, zVfsName, (char*)0);
6006 sqlite3_free(zVfsName);
6007 return TCL_OK;
6011 ** tclcmd: file_control_tempfilename DB ?AUXDB?
6013 ** Return a string that is a temporary filename
6015 static int SQLITE_TCLAPI file_control_tempfilename(
6016 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6017 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6018 int objc, /* Number of arguments */
6019 Tcl_Obj *CONST objv[] /* Command arguments */
6021 sqlite3 *db;
6022 const char *zDbName = "main";
6023 char *zTName = 0;
6025 if( objc!=2 && objc!=3 ){
6026 Tcl_AppendResult(interp, "wrong # args: should be \"",
6027 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6028 return TCL_ERROR;
6030 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6031 return TCL_ERROR;
6033 if( objc==3 ){
6034 zDbName = Tcl_GetString(objv[2]);
6036 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
6037 Tcl_AppendResult(interp, zTName, (char*)0);
6038 sqlite3_free(zTName);
6039 return TCL_OK;
6044 ** tclcmd: sqlite3_vfs_list
6046 ** Return a tcl list containing the names of all registered vfs's.
6048 static int SQLITE_TCLAPI vfs_list(
6049 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6050 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6051 int objc, /* Number of arguments */
6052 Tcl_Obj *CONST objv[] /* Command arguments */
6054 sqlite3_vfs *pVfs;
6055 Tcl_Obj *pRet = Tcl_NewObj();
6056 if( objc!=1 ){
6057 Tcl_WrongNumArgs(interp, 1, objv, "");
6058 return TCL_ERROR;
6060 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
6061 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
6063 Tcl_SetObjResult(interp, pRet);
6064 return TCL_OK;
6068 ** tclcmd: sqlite3_limit DB ID VALUE
6070 ** This TCL command runs the sqlite3_limit interface and
6071 ** verifies correct operation of the same.
6073 static int SQLITE_TCLAPI test_limit(
6074 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6075 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6076 int objc, /* Number of arguments */
6077 Tcl_Obj *CONST objv[] /* Command arguments */
6079 sqlite3 *db;
6080 int rc;
6081 static const struct {
6082 char *zName;
6083 int id;
6084 } aId[] = {
6085 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
6086 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
6087 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
6088 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
6089 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
6090 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
6091 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
6092 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
6093 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
6094 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
6095 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
6096 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS },
6098 /* Out of range test cases */
6099 { "SQLITE_LIMIT_TOOSMALL", -1, },
6100 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 },
6102 int i, id = 0;
6103 int val;
6104 const char *zId;
6106 if( objc!=4 ){
6107 Tcl_AppendResult(interp, "wrong # args: should be \"",
6108 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
6109 return TCL_ERROR;
6111 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6112 zId = Tcl_GetString(objv[2]);
6113 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
6114 if( strcmp(zId, aId[i].zName)==0 ){
6115 id = aId[i].id;
6116 break;
6119 if( i>=sizeof(aId)/sizeof(aId[0]) ){
6120 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
6121 return TCL_ERROR;
6123 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
6124 rc = sqlite3_limit(db, id, val);
6125 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6126 return TCL_OK;
6130 ** tclcmd: save_prng_state
6132 ** Save the state of the pseudo-random number generator.
6133 ** At the same time, verify that sqlite3_test_control works even when
6134 ** called with an out-of-range opcode.
6136 static int SQLITE_TCLAPI save_prng_state(
6137 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6138 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6139 int objc, /* Number of arguments */
6140 Tcl_Obj *CONST objv[] /* Command arguments */
6142 int rc = sqlite3_test_control(9999);
6143 assert( rc==0 );
6144 rc = sqlite3_test_control(-1);
6145 assert( rc==0 );
6146 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
6147 return TCL_OK;
6150 ** tclcmd: restore_prng_state
6152 static int SQLITE_TCLAPI restore_prng_state(
6153 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6154 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6155 int objc, /* Number of arguments */
6156 Tcl_Obj *CONST objv[] /* Command arguments */
6158 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
6159 return TCL_OK;
6162 ** tclcmd: reset_prng_state
6164 static int SQLITE_TCLAPI reset_prng_state(
6165 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6166 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6167 int objc, /* Number of arguments */
6168 Tcl_Obj *CONST objv[] /* Command arguments */
6170 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
6171 return TCL_OK;
6175 ** tclcmd: database_may_be_corrupt
6177 ** Indicate that database files might be corrupt. In other words, set the normal
6178 ** state of operation.
6180 static int SQLITE_TCLAPI database_may_be_corrupt(
6181 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6182 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6183 int objc, /* Number of arguments */
6184 Tcl_Obj *CONST objv[] /* Command arguments */
6186 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0);
6187 return TCL_OK;
6190 ** tclcmd: database_never_corrupt
6192 ** Indicate that database files are always well-formed. This enables extra assert()
6193 ** statements that test conditions that are always true for well-formed databases.
6195 static int SQLITE_TCLAPI database_never_corrupt(
6196 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6197 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6198 int objc, /* Number of arguments */
6199 Tcl_Obj *CONST objv[] /* Command arguments */
6201 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1);
6202 return TCL_OK;
6206 ** tclcmd: pcache_stats
6208 static int SQLITE_TCLAPI test_pcache_stats(
6209 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6210 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6211 int objc, /* Number of arguments */
6212 Tcl_Obj *CONST objv[] /* Command arguments */
6214 int nMin;
6215 int nMax;
6216 int nCurrent;
6217 int nRecyclable;
6218 Tcl_Obj *pRet;
6220 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
6222 pRet = Tcl_NewObj();
6223 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
6224 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
6225 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
6226 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
6227 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
6228 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
6229 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
6230 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
6232 Tcl_SetObjResult(interp, pRet);
6234 return TCL_OK;
6237 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
6238 static void test_unlock_notify_cb(void **aArg, int nArg){
6239 int ii;
6240 for(ii=0; ii<nArg; ii++){
6241 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
6244 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
6247 ** tclcmd: sqlite3_unlock_notify db
6249 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
6250 static int SQLITE_TCLAPI test_unlock_notify(
6251 ClientData clientData, /* Unused */
6252 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6253 int objc, /* Number of arguments */
6254 Tcl_Obj *CONST objv[] /* Command arguments */
6256 sqlite3 *db;
6257 int rc;
6259 if( objc!=2 ){
6260 Tcl_WrongNumArgs(interp, 1, objv, "DB");
6261 return TCL_ERROR;
6264 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6265 return TCL_ERROR;
6267 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
6268 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6269 return TCL_OK;
6271 #endif
6274 ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
6276 static int SQLITE_TCLAPI test_wal_checkpoint(
6277 ClientData clientData, /* Unused */
6278 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6279 int objc, /* Number of arguments */
6280 Tcl_Obj *CONST objv[] /* Command arguments */
6282 char *zDb = 0;
6283 sqlite3 *db;
6284 int rc;
6286 if( objc!=3 && objc!=2 ){
6287 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
6288 return TCL_ERROR;
6291 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6292 return TCL_ERROR;
6294 if( objc==3 ){
6295 zDb = Tcl_GetString(objv[2]);
6297 rc = sqlite3_wal_checkpoint(db, zDb);
6298 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6299 return TCL_OK;
6303 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
6305 ** This command calls the wal_checkpoint_v2() function with the specified
6306 ** mode argument (passive, full or restart). If present, the database name
6307 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
6308 ** NAME argument is not present, a NULL pointer is passed instead.
6310 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
6311 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
6312 ** to the error message obtained from sqlite3_errmsg().
6314 ** Otherwise, this command returns a list of three integers. The first integer
6315 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
6316 ** are the values returned via the output parameters by wal_checkpoint_v2() -
6317 ** the number of frames in the log and the number of frames in the log
6318 ** that have been checkpointed.
6320 static int SQLITE_TCLAPI test_wal_checkpoint_v2(
6321 ClientData clientData, /* Unused */
6322 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6323 int objc, /* Number of arguments */
6324 Tcl_Obj *CONST objv[] /* Command arguments */
6326 char *zDb = 0;
6327 sqlite3 *db;
6328 int rc;
6330 int eMode;
6331 int nLog = -555;
6332 int nCkpt = -555;
6333 Tcl_Obj *pRet;
6335 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
6336 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
6337 assert( SQLITE_CHECKPOINT_FULL==1 );
6338 assert( SQLITE_CHECKPOINT_RESTART==2 );
6339 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
6341 if( objc!=3 && objc!=4 ){
6342 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
6343 return TCL_ERROR;
6346 if( objc==4 ){
6347 zDb = Tcl_GetString(objv[3]);
6349 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
6350 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
6351 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
6353 return TCL_ERROR;
6356 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
6357 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
6358 const char *zErrCode = sqlite3ErrName(rc);
6359 Tcl_ResetResult(interp);
6360 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
6361 return TCL_ERROR;
6364 pRet = Tcl_NewObj();
6365 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
6366 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
6367 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
6368 Tcl_SetObjResult(interp, pRet);
6370 return TCL_OK;
6374 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE
6376 static int SQLITE_TCLAPI test_wal_autocheckpoint(
6377 ClientData clientData, /* Unused */
6378 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6379 int objc, /* Number of arguments */
6380 Tcl_Obj *CONST objv[] /* Command arguments */
6382 sqlite3 *db;
6383 int rc;
6384 int iVal;
6387 if( objc!=3 ){
6388 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE");
6389 return TCL_ERROR;
6392 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6393 || Tcl_GetIntFromObj(0, objv[2], &iVal)
6395 return TCL_ERROR;
6398 rc = sqlite3_wal_autocheckpoint(db, iVal);
6399 Tcl_ResetResult(interp);
6400 if( rc!=SQLITE_OK ){
6401 const char *zErrCode = sqlite3ErrName(rc);
6402 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1));
6403 return TCL_ERROR;
6406 return TCL_OK;
6411 ** tclcmd: test_sqlite3_log ?SCRIPT?
6413 static struct LogCallback {
6414 Tcl_Interp *pInterp;
6415 Tcl_Obj *pObj;
6416 } logcallback = {0, 0};
6417 static void xLogcallback(void *unused, int err, char *zMsg){
6418 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
6419 Tcl_IncrRefCount(pNew);
6420 Tcl_ListObjAppendElement(
6421 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
6423 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
6424 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
6425 Tcl_DecrRefCount(pNew);
6427 static int SQLITE_TCLAPI test_sqlite3_log(
6428 ClientData clientData,
6429 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6430 int objc, /* Number of arguments */
6431 Tcl_Obj *CONST objv[] /* Command arguments */
6433 if( objc>2 ){
6434 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
6435 return TCL_ERROR;
6437 if( logcallback.pObj ){
6438 Tcl_DecrRefCount(logcallback.pObj);
6439 logcallback.pObj = 0;
6440 logcallback.pInterp = 0;
6441 sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0);
6443 if( objc>1 ){
6444 logcallback.pObj = objv[1];
6445 Tcl_IncrRefCount(logcallback.pObj);
6446 logcallback.pInterp = interp;
6447 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0);
6449 return TCL_OK;
6453 ** tcl_objproc COMMANDNAME ARGS...
6455 ** Run a TCL command using its objProc interface. Throw an error if
6456 ** the command has no objProc interface.
6458 static int SQLITE_TCLAPI runAsObjProc(
6459 void * clientData,
6460 Tcl_Interp *interp,
6461 int objc,
6462 Tcl_Obj *CONST objv[]
6464 Tcl_CmdInfo cmdInfo;
6465 if( objc<2 ){
6466 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
6467 return TCL_ERROR;
6469 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
6470 Tcl_AppendResult(interp, "command not found: ",
6471 Tcl_GetString(objv[1]), (char*)0);
6472 return TCL_ERROR;
6474 if( cmdInfo.objProc==0 ){
6475 Tcl_AppendResult(interp, "command has no objProc: ",
6476 Tcl_GetString(objv[1]), (char*)0);
6477 return TCL_ERROR;
6479 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
6482 #ifndef SQLITE_OMIT_EXPLAIN
6484 ** WARNING: The following function, printExplainQueryPlan() is an exact
6485 ** copy of example code from eqp.in (eqp.html). If this code is modified,
6486 ** then the documentation copy needs to be modified as well.
6489 ** Argument pStmt is a prepared SQL statement. This function compiles
6490 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
6491 ** and prints the report to stdout using printf().
6493 int printExplainQueryPlan(sqlite3_stmt *pStmt){
6494 const char *zSql; /* Input SQL */
6495 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */
6496 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
6497 int rc; /* Return code from sqlite3_prepare_v2() */
6499 zSql = sqlite3_sql(pStmt);
6500 if( zSql==0 ) return SQLITE_ERROR;
6502 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
6503 if( zExplain==0 ) return SQLITE_NOMEM;
6505 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
6506 sqlite3_free(zExplain);
6507 if( rc!=SQLITE_OK ) return rc;
6509 while( SQLITE_ROW==sqlite3_step(pExplain) ){
6510 int iSelectid = sqlite3_column_int(pExplain, 0);
6511 int iOrder = sqlite3_column_int(pExplain, 1);
6512 int iFrom = sqlite3_column_int(pExplain, 2);
6513 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
6515 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
6518 return sqlite3_finalize(pExplain);
6521 static int SQLITE_TCLAPI test_print_eqp(
6522 void * clientData,
6523 Tcl_Interp *interp,
6524 int objc,
6525 Tcl_Obj *CONST objv[]
6527 int rc;
6528 sqlite3_stmt *pStmt;
6530 if( objc!=2 ){
6531 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
6532 return TCL_ERROR;
6534 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
6535 rc = printExplainQueryPlan(pStmt);
6536 /* This is needed on Windows so that a test case using this
6537 ** function can open a read pipe and get the output of
6538 ** printExplainQueryPlan() immediately.
6540 fflush(stdout);
6541 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
6542 return TCL_OK;
6544 #endif /* SQLITE_OMIT_EXPLAIN */
6547 ** sqlite3_test_control VERB ARGS...
6549 static int SQLITE_TCLAPI test_test_control(
6550 void * clientData,
6551 Tcl_Interp *interp,
6552 int objc,
6553 Tcl_Obj *CONST objv[]
6555 struct Verb {
6556 const char *zName;
6557 int i;
6558 } aVerb[] = {
6559 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
6560 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP },
6561 { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER },
6563 int iVerb;
6564 int iFlag;
6565 int rc;
6567 if( objc<2 ){
6568 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
6569 return TCL_ERROR;
6572 rc = Tcl_GetIndexFromObjStruct(
6573 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
6575 if( rc!=TCL_OK ) return rc;
6577 iFlag = aVerb[iVerb].i;
6578 switch( iFlag ){
6579 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
6580 int val;
6581 if( objc!=3 ){
6582 Tcl_WrongNumArgs(interp, 2, objv, "ONOFF");
6583 return TCL_ERROR;
6585 if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
6586 sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val);
6587 break;
6590 case SQLITE_TESTCTRL_SORTER_MMAP: {
6591 int val;
6592 sqlite3 *db;
6593 if( objc!=4 ){
6594 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT");
6595 return TCL_ERROR;
6597 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
6598 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
6599 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val);
6600 break;
6603 case SQLITE_TESTCTRL_IMPOSTER: {
6604 int onOff, tnum;
6605 const char *zDbName;
6606 sqlite3 *db;
6607 if( objc!=6 ){
6608 Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum");
6609 return TCL_ERROR;
6611 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
6612 zDbName = Tcl_GetString(objv[3]);
6613 if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR;
6614 if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR;
6615 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum);
6616 break;
6620 Tcl_ResetResult(interp);
6621 return TCL_OK;
6624 #if SQLITE_OS_UNIX
6625 #include <sys/time.h>
6626 #include <sys/resource.h>
6628 static int SQLITE_TCLAPI test_getrusage(
6629 void * clientData,
6630 Tcl_Interp *interp,
6631 int objc,
6632 Tcl_Obj *CONST objv[]
6634 char buf[1024];
6635 struct rusage r;
6636 memset(&r, 0, sizeof(r));
6637 getrusage(RUSAGE_SELF, &r);
6639 sqlite3_snprintf(sizeof(buf), buf,
6640 "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
6641 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
6642 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
6643 (int)r.ru_minflt, (int)r.ru_majflt
6645 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
6646 return TCL_OK;
6648 #endif
6650 #if SQLITE_OS_WIN
6652 ** Information passed from the main thread into the windows file locker
6653 ** background thread.
6655 struct win32FileLocker {
6656 char *evName; /* Name of event to signal thread startup */
6657 HANDLE h; /* Handle of the file to be locked */
6658 int delay1; /* Delay before locking */
6659 int delay2; /* Delay before unlocking */
6660 int ok; /* Finished ok */
6661 int err; /* True if an error occurs */
6663 #endif
6666 #if SQLITE_OS_WIN
6667 #include <process.h>
6669 ** The background thread that does file locking.
6671 static void SQLITE_CDECL win32_file_locker(void *pAppData){
6672 struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
6673 if( p->evName ){
6674 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
6675 if ( ev ){
6676 SetEvent(ev);
6677 CloseHandle(ev);
6680 if( p->delay1 ) Sleep(p->delay1);
6681 if( LockFile(p->h, 0, 0, 100000000, 0) ){
6682 Sleep(p->delay2);
6683 UnlockFile(p->h, 0, 0, 100000000, 0);
6684 p->ok = 1;
6685 }else{
6686 p->err = 1;
6688 CloseHandle(p->h);
6689 p->h = 0;
6690 p->delay1 = 0;
6691 p->delay2 = 0;
6693 #endif
6695 #if SQLITE_OS_WIN
6697 ** lock_win32_file FILENAME DELAY1 DELAY2
6699 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
6700 ** Wait DELAY1 milliseconds before acquiring the lock.
6702 static int SQLITE_TCLAPI win32_file_lock(
6703 void * clientData,
6704 Tcl_Interp *interp,
6705 int objc,
6706 Tcl_Obj *CONST objv[]
6708 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
6709 const char *zFilename;
6710 char zBuf[200];
6711 int retry = 0;
6712 HANDLE ev;
6713 DWORD wResult;
6715 if( objc!=4 && objc!=1 ){
6716 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
6717 return TCL_ERROR;
6719 if( objc==1 ){
6720 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
6721 x.ok, x.err, x.delay1, x.delay2, x.h);
6722 Tcl_AppendResult(interp, zBuf, (char*)0);
6723 return TCL_OK;
6725 while( x.h && retry<30 ){
6726 retry++;
6727 Sleep(100);
6729 if( x.h ){
6730 Tcl_AppendResult(interp, "busy", (char*)0);
6731 return TCL_ERROR;
6733 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
6734 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
6735 zFilename = Tcl_GetString(objv[1]);
6736 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
6737 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
6738 FILE_ATTRIBUTE_NORMAL, 0);
6739 if( !x.h ){
6740 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
6741 return TCL_ERROR;
6743 ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
6744 if ( !ev ){
6745 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
6746 return TCL_ERROR;
6748 _beginthread(win32_file_locker, 0, (void*)&x);
6749 Sleep(0);
6750 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
6751 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
6752 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
6753 CloseHandle(ev);
6754 return TCL_ERROR;
6756 CloseHandle(ev);
6757 return TCL_OK;
6761 ** exists_win32_path PATH
6763 ** Returns non-zero if the specified path exists, whose fully qualified name
6764 ** may exceed 260 characters if it is prefixed with "\\?\".
6766 static int SQLITE_TCLAPI win32_exists_path(
6767 void *clientData,
6768 Tcl_Interp *interp,
6769 int objc,
6770 Tcl_Obj *CONST objv[]
6772 if( objc!=2 ){
6773 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
6774 return TCL_ERROR;
6776 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
6777 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
6778 return TCL_OK;
6782 ** find_win32_file PATTERN
6784 ** Returns a list of entries in a directory that match the specified pattern,
6785 ** whose fully qualified name may exceed 248 characters if it is prefixed with
6786 ** "\\?\".
6788 static int SQLITE_TCLAPI win32_find_file(
6789 void *clientData,
6790 Tcl_Interp *interp,
6791 int objc,
6792 Tcl_Obj *CONST objv[]
6794 HANDLE hFindFile = INVALID_HANDLE_VALUE;
6795 WIN32_FIND_DATAW findData;
6796 Tcl_Obj *listObj;
6797 DWORD lastErrno;
6798 if( objc!=2 ){
6799 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
6800 return TCL_ERROR;
6802 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
6803 if( hFindFile==INVALID_HANDLE_VALUE ){
6804 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6805 return TCL_ERROR;
6807 listObj = Tcl_NewObj();
6808 Tcl_IncrRefCount(listObj);
6809 do {
6810 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
6811 findData.cFileName, -1));
6812 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
6813 findData.dwFileAttributes));
6814 } while( FindNextFileW(hFindFile, &findData) );
6815 lastErrno = GetLastError();
6816 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
6817 FindClose(hFindFile);
6818 Tcl_DecrRefCount(listObj);
6819 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6820 return TCL_ERROR;
6822 FindClose(hFindFile);
6823 Tcl_SetObjResult(interp, listObj);
6824 return TCL_OK;
6828 ** delete_win32_file FILENAME
6830 ** Deletes the specified file, whose fully qualified name may exceed 260
6831 ** characters if it is prefixed with "\\?\".
6833 static int SQLITE_TCLAPI win32_delete_file(
6834 void *clientData,
6835 Tcl_Interp *interp,
6836 int objc,
6837 Tcl_Obj *CONST objv[]
6839 if( objc!=2 ){
6840 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
6841 return TCL_ERROR;
6843 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
6844 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6845 return TCL_ERROR;
6847 Tcl_ResetResult(interp);
6848 return TCL_OK;
6852 ** make_win32_dir DIRECTORY
6854 ** Creates the specified directory, whose fully qualified name may exceed 248
6855 ** characters if it is prefixed with "\\?\".
6857 static int SQLITE_TCLAPI win32_mkdir(
6858 void *clientData,
6859 Tcl_Interp *interp,
6860 int objc,
6861 Tcl_Obj *CONST objv[]
6863 if( objc!=2 ){
6864 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
6865 return TCL_ERROR;
6867 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
6868 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6869 return TCL_ERROR;
6871 Tcl_ResetResult(interp);
6872 return TCL_OK;
6876 ** remove_win32_dir DIRECTORY
6878 ** Removes the specified directory, whose fully qualified name may exceed 248
6879 ** characters if it is prefixed with "\\?\".
6881 static int SQLITE_TCLAPI win32_rmdir(
6882 void *clientData,
6883 Tcl_Interp *interp,
6884 int objc,
6885 Tcl_Obj *CONST objv[]
6887 if( objc!=2 ){
6888 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
6889 return TCL_ERROR;
6891 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
6892 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6893 return TCL_ERROR;
6895 Tcl_ResetResult(interp);
6896 return TCL_OK;
6898 #endif
6902 ** optimization_control DB OPT BOOLEAN
6904 ** Enable or disable query optimizations using the sqlite3_test_control()
6905 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
6906 ** OPT is the name of the optimization to be disabled.
6908 static int SQLITE_TCLAPI optimization_control(
6909 void * clientData,
6910 Tcl_Interp *interp,
6911 int objc,
6912 Tcl_Obj *CONST objv[]
6914 int i;
6915 sqlite3 *db;
6916 const char *zOpt;
6917 int onoff;
6918 int mask = 0;
6919 static const struct {
6920 const char *zOptName;
6921 int mask;
6922 } aOpt[] = {
6923 { "all", SQLITE_AllOpts },
6924 { "none", 0 },
6925 { "query-flattener", SQLITE_QueryFlattener },
6926 { "column-cache", SQLITE_ColumnCache },
6927 { "groupby-order", SQLITE_GroupByOrder },
6928 { "factor-constants", SQLITE_FactorOutConst },
6929 { "distinct-opt", SQLITE_DistinctOpt },
6930 { "cover-idx-scan", SQLITE_CoverIdxScan },
6931 { "order-by-idx-join", SQLITE_OrderByIdxJoin },
6932 { "transitive", SQLITE_Transitive },
6933 { "omit-noop-join", SQLITE_OmitNoopJoin },
6934 { "stat3", SQLITE_Stat34 },
6935 { "stat4", SQLITE_Stat34 },
6938 if( objc!=4 ){
6939 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
6940 return TCL_ERROR;
6942 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6943 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
6944 zOpt = Tcl_GetString(objv[2]);
6945 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
6946 if( strcmp(zOpt, aOpt[i].zOptName)==0 ){
6947 mask = aOpt[i].mask;
6948 break;
6951 if( onoff ) mask = ~mask;
6952 if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){
6953 Tcl_AppendResult(interp, "unknown optimization - should be one of:",
6954 (char*)0);
6955 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
6956 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
6958 return TCL_ERROR;
6960 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
6961 return TCL_OK;
6965 ** load_static_extension DB NAME ...
6967 ** Load one or more statically linked extensions.
6969 static int SQLITE_TCLAPI tclLoadStaticExtensionCmd(
6970 void * clientData,
6971 Tcl_Interp *interp,
6972 int objc,
6973 Tcl_Obj *CONST objv[]
6975 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
6976 extern int sqlite3_carray_init(sqlite3*,char**,const sqlite3_api_routines*);
6977 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
6978 extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*);
6979 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
6980 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
6981 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
6982 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
6983 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
6984 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
6985 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
6986 extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*);
6987 extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
6988 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
6989 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
6990 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
6991 extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
6992 #ifdef SQLITE_HAVE_ZLIB
6993 extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*);
6994 #endif
6995 static const struct {
6996 const char *zExtName;
6997 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
6998 } aExtension[] = {
6999 { "amatch", sqlite3_amatch_init },
7000 { "carray", sqlite3_carray_init },
7001 { "closure", sqlite3_closure_init },
7002 { "csv", sqlite3_csv_init },
7003 { "eval", sqlite3_eval_init },
7004 { "fileio", sqlite3_fileio_init },
7005 { "fuzzer", sqlite3_fuzzer_init },
7006 { "ieee754", sqlite3_ieee_init },
7007 { "nextchar", sqlite3_nextchar_init },
7008 { "percentile", sqlite3_percentile_init },
7009 { "regexp", sqlite3_regexp_init },
7010 { "remember", sqlite3_remember_init },
7011 { "series", sqlite3_series_init },
7012 { "spellfix", sqlite3_spellfix_init },
7013 { "totype", sqlite3_totype_init },
7014 { "unionvtab", sqlite3_unionvtab_init },
7015 { "wholenumber", sqlite3_wholenumber_init },
7016 #ifdef SQLITE_HAVE_ZLIB
7017 { "zipfile", sqlite3_zipfile_init },
7018 #endif
7020 sqlite3 *db;
7021 const char *zName;
7022 int i, j, rc;
7023 char *zErrMsg = 0;
7024 if( objc<3 ){
7025 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
7026 return TCL_ERROR;
7028 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7029 for(j=2; j<objc; j++){
7030 zName = Tcl_GetString(objv[j]);
7031 for(i=0; i<ArraySize(aExtension); i++){
7032 if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
7034 if( i>=ArraySize(aExtension) ){
7035 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
7036 return TCL_ERROR;
7038 if( aExtension[i].pInit ){
7039 rc = aExtension[i].pInit(db, &zErrMsg, 0);
7040 }else{
7041 rc = SQLITE_OK;
7043 if( rc!=SQLITE_OK || zErrMsg ){
7044 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
7045 (char*)0);
7046 sqlite3_free(zErrMsg);
7047 return TCL_ERROR;
7050 return TCL_OK;
7054 ** sorter_test_fakeheap BOOL
7057 static int SQLITE_TCLAPI sorter_test_fakeheap(
7058 void * clientData,
7059 Tcl_Interp *interp,
7060 int objc,
7061 Tcl_Obj *CONST objv[]
7063 int bArg;
7064 if( objc!=2 ){
7065 Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
7066 return TCL_ERROR;
7069 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){
7070 return TCL_ERROR;
7073 if( bArg ){
7074 if( sqlite3GlobalConfig.pHeap==0 ){
7075 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1);
7077 }else{
7078 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){
7079 sqlite3GlobalConfig.pHeap = 0;
7083 Tcl_ResetResult(interp);
7084 return TCL_OK;
7088 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2
7090 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
7091 ** check that the leftmost and rightmost columns returned are both integers,
7092 ** and that both contain the same value.
7094 ** Then execute statement $SQL2. Check that the statement returns the same
7095 ** set of integers in the same order as in the previous step (using $SQL1).
7097 static int SQLITE_TCLAPI sorter_test_sort4_helper(
7098 void * clientData,
7099 Tcl_Interp *interp,
7100 int objc,
7101 Tcl_Obj *CONST objv[]
7103 const char *zSql1;
7104 const char *zSql2;
7105 int nStep;
7106 int iStep;
7107 unsigned int iCksum1 = 0;
7108 unsigned int iCksum2 = 0;
7109 int rc;
7110 int iB;
7111 sqlite3 *db;
7112 sqlite3_stmt *pStmt;
7114 if( objc!=5 ){
7115 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2");
7116 return TCL_ERROR;
7119 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7120 zSql1 = Tcl_GetString(objv[2]);
7121 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR;
7122 zSql2 = Tcl_GetString(objv[4]);
7124 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0);
7125 if( rc!=SQLITE_OK ) goto sql_error;
7127 iB = sqlite3_column_count(pStmt)-1;
7128 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){
7129 int a = sqlite3_column_int(pStmt, 0);
7130 if( a!=sqlite3_column_int(pStmt, iB) ){
7131 Tcl_AppendResult(interp, "data error: (a!=b)", 0);
7132 return TCL_ERROR;
7135 iCksum1 += (iCksum1 << 3) + (unsigned int)a;
7137 rc = sqlite3_finalize(pStmt);
7138 if( rc!=SQLITE_OK ) goto sql_error;
7140 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0);
7141 if( rc!=SQLITE_OK ) goto sql_error;
7142 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){
7143 int a = sqlite3_column_int(pStmt, 0);
7144 iCksum2 += (iCksum2 << 3) + (unsigned int)a;
7146 rc = sqlite3_finalize(pStmt);
7147 if( rc!=SQLITE_OK ) goto sql_error;
7149 if( iCksum1!=iCksum2 ){
7150 Tcl_AppendResult(interp, "checksum mismatch", 0);
7151 return TCL_ERROR;
7154 return TCL_OK;
7155 sql_error:
7156 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
7157 return TCL_ERROR;
7161 #ifdef SQLITE_USER_AUTHENTICATION
7162 #include "sqlite3userauth.h"
7164 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD
7166 static int SQLITE_TCLAPI test_user_authenticate(
7167 ClientData clientData, /* Unused */
7168 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7169 int objc, /* Number of arguments */
7170 Tcl_Obj *CONST objv[] /* Command arguments */
7172 char *zUser = 0;
7173 char *zPasswd = 0;
7174 int nPasswd = 0;
7175 sqlite3 *db;
7176 int rc;
7178 if( objc!=4 ){
7179 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
7180 return TCL_ERROR;
7182 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7183 return TCL_ERROR;
7185 zUser = Tcl_GetString(objv[2]);
7186 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
7187 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
7188 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7189 return TCL_OK;
7191 #endif /* SQLITE_USER_AUTHENTICATION */
7193 #ifdef SQLITE_USER_AUTHENTICATION
7195 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN
7197 static int SQLITE_TCLAPI test_user_add(
7198 ClientData clientData, /* Unused */
7199 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7200 int objc, /* Number of arguments */
7201 Tcl_Obj *CONST objv[] /* Command arguments */
7203 char *zUser = 0;
7204 char *zPasswd = 0;
7205 int nPasswd = 0;
7206 int isAdmin = 0;
7207 sqlite3 *db;
7208 int rc;
7210 if( objc!=5 ){
7211 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
7212 return TCL_ERROR;
7214 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7215 return TCL_ERROR;
7217 zUser = Tcl_GetString(objv[2]);
7218 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
7219 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
7220 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
7221 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7222 return TCL_OK;
7224 #endif /* SQLITE_USER_AUTHENTICATION */
7226 #ifdef SQLITE_USER_AUTHENTICATION
7228 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN
7230 static int SQLITE_TCLAPI test_user_change(
7231 ClientData clientData, /* Unused */
7232 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7233 int objc, /* Number of arguments */
7234 Tcl_Obj *CONST objv[] /* Command arguments */
7236 char *zUser = 0;
7237 char *zPasswd = 0;
7238 int nPasswd = 0;
7239 int isAdmin = 0;
7240 sqlite3 *db;
7241 int rc;
7243 if( objc!=5 ){
7244 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
7245 return TCL_ERROR;
7247 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7248 return TCL_ERROR;
7250 zUser = Tcl_GetString(objv[2]);
7251 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
7252 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
7253 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
7254 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7255 return TCL_OK;
7257 #endif /* SQLITE_USER_AUTHENTICATION */
7259 #ifdef SQLITE_USER_AUTHENTICATION
7261 ** tclcmd: sqlite3_user_delete DB USERNAME
7263 static int SQLITE_TCLAPI test_user_delete(
7264 ClientData clientData, /* Unused */
7265 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7266 int objc, /* Number of arguments */
7267 Tcl_Obj *CONST objv[] /* Command arguments */
7269 char *zUser = 0;
7270 sqlite3 *db;
7271 int rc;
7273 if( objc!=3 ){
7274 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
7275 return TCL_ERROR;
7277 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7278 return TCL_ERROR;
7280 zUser = Tcl_GetString(objv[2]);
7281 rc = sqlite3_user_delete(db, zUser);
7282 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7283 return TCL_OK;
7285 #endif /* SQLITE_USER_AUTHENTICATION */
7288 ** tclcmd: bad_behavior TYPE
7290 ** Do some things that should trigger a valgrind or -fsanitize=undefined
7291 ** warning. This is used to verify that errors and warnings output by those
7292 ** tools are detected by the test scripts.
7294 ** TYPE BEHAVIOR
7295 ** 1 Overflow a signed integer
7296 ** 2 Jump based on an uninitialized variable
7297 ** 3 Read after free
7298 ** 4 Panic
7300 static int SQLITE_TCLAPI test_bad_behavior(
7301 ClientData clientData, /* Pointer to an integer containing zero */
7302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7303 int objc, /* Number of arguments */
7304 Tcl_Obj *CONST objv[] /* Command arguments */
7306 int iType;
7307 int xyz;
7308 int i = *(int*)clientData;
7309 int j;
7310 int w[10];
7311 int *a;
7312 if( objc!=2 ){
7313 Tcl_WrongNumArgs(interp, 1, objv, "TYPE");
7314 return TCL_ERROR;
7316 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR;
7317 switch( iType ){
7318 case 1: {
7319 xyz = 0x7fffff00 - i;
7320 xyz += 0x100;
7321 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz));
7322 break;
7324 case 2: {
7325 w[1] = 5;
7326 if( w[i]>0 ) w[1]++;
7327 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1]));
7328 break;
7330 case 3: {
7331 a = malloc( sizeof(int)*10 );
7332 for(j=0; j<10; j++) a[j] = j;
7333 free(a);
7334 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i]));
7335 break;
7337 case 4: {
7338 Tcl_Panic("Deliberate panic");
7339 break;
7342 return TCL_OK;
7346 ** tclcmd: register_dbstat_vtab DB
7348 ** Cause the dbstat virtual table to be available on the connection DB
7350 static int SQLITE_TCLAPI test_register_dbstat_vtab(
7351 void *clientData,
7352 Tcl_Interp *interp,
7353 int objc,
7354 Tcl_Obj *CONST objv[]
7356 #ifdef SQLITE_OMIT_VIRTUALTABLE
7357 Tcl_AppendResult(interp, "dbstat not available because of "
7358 "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
7359 return TCL_ERROR;
7360 #else
7361 struct SqliteDb { sqlite3 *db; };
7362 char *zDb;
7363 Tcl_CmdInfo cmdInfo;
7365 if( objc!=2 ){
7366 Tcl_WrongNumArgs(interp, 1, objv, "DB");
7367 return TCL_ERROR;
7370 zDb = Tcl_GetString(objv[1]);
7371 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
7372 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
7373 sqlite3DbstatRegister(db);
7375 return TCL_OK;
7376 #endif /* SQLITE_OMIT_VIRTUALTABLE */
7380 ** tclcmd: sqlite3_db_config DB SETTING VALUE
7382 ** Invoke sqlite3_db_config() for one of the setting values.
7384 static int SQLITE_TCLAPI test_sqlite3_db_config(
7385 void *clientData,
7386 Tcl_Interp *interp,
7387 int objc,
7388 Tcl_Obj *CONST objv[]
7390 static const struct {
7391 const char *zName;
7392 int eVal;
7393 } aSetting[] = {
7394 { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY },
7395 { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER },
7396 { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7397 { "LOAD_EXTENSION", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7398 { "NO_CKPT_ON_CLOSE",SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
7399 { "QPSG", SQLITE_DBCONFIG_ENABLE_QPSG },
7401 int i;
7402 int v;
7403 const char *zSetting;
7404 sqlite3 *db;
7406 if( objc!=4 ){
7407 Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING VALUE");
7408 return TCL_ERROR;
7410 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7411 zSetting = Tcl_GetString(objv[2]);
7412 if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7;
7413 if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9;
7414 if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7;
7415 for(i=0; i<ArraySize(aSetting); i++){
7416 if( strcmp(zSetting, aSetting[i].zName)==0 ) break;
7418 if( i>=ArraySize(aSetting) ){
7419 Tcl_SetObjResult(interp,
7420 Tcl_NewStringObj("unknown sqlite3_db_config setting", -1));
7421 return TCL_ERROR;
7423 if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR;
7424 sqlite3_db_config(db, aSetting[i].eVal, v, &v);
7425 Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
7426 return TCL_OK;
7430 ** Change the name of the main database schema from "main" to "icecube".
7432 static int SQLITE_TCLAPI test_dbconfig_maindbname_icecube(
7433 void * clientData,
7434 Tcl_Interp *interp,
7435 int objc,
7436 Tcl_Obj *CONST objv[]
7438 int rc;
7439 sqlite3 *db;
7440 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
7441 if( objc!=2 ){
7442 Tcl_WrongNumArgs(interp, 1, objv, "DB");
7443 return TCL_ERROR;
7444 }else{
7445 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7446 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "icecube");
7447 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
7448 return TCL_OK;
7453 ** Usage: sqlite3_mmap_warm DB DBNAME
7455 static int SQLITE_TCLAPI test_mmap_warm(
7456 void * clientData,
7457 Tcl_Interp *interp,
7458 int objc,
7459 Tcl_Obj *CONST objv[]
7461 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
7462 extern int sqlite3_mmap_warm(sqlite3 *db, const char *);
7464 if( objc!=2 && objc!=3 ){
7465 Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?");
7466 return TCL_ERROR;
7467 }else{
7468 int rc;
7469 sqlite3 *db;
7470 const char *zDb = 0;
7471 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7472 if( objc==3 ){
7473 zDb = Tcl_GetString(objv[2]);
7475 rc = sqlite3_mmap_warm(db, zDb);
7476 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
7477 return TCL_OK;
7482 ** Register commands with the TCL interpreter.
7484 int Sqlitetest1_Init(Tcl_Interp *interp){
7485 extern int sqlite3_search_count;
7486 extern int sqlite3_found_count;
7487 extern int sqlite3_interrupt_count;
7488 extern int sqlite3_open_file_count;
7489 extern int sqlite3_sort_count;
7490 extern int sqlite3_current_time;
7491 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
7492 extern int sqlite3_hostid_num;
7493 #endif
7494 extern int sqlite3_max_blobsize;
7495 extern int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport(void*,
7496 Tcl_Interp*,int,Tcl_Obj*CONST*);
7497 static int iZero = 0;
7498 static struct {
7499 char *zName;
7500 Tcl_CmdProc *xProc;
7501 } aCmd[] = {
7502 { "db_enter", (Tcl_CmdProc*)db_enter },
7503 { "db_leave", (Tcl_CmdProc*)db_leave },
7504 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
7505 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
7506 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
7507 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
7508 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
7509 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
7510 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
7511 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
7512 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
7513 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
7514 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
7515 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
7516 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
7517 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
7518 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
7519 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
7520 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
7521 #ifndef SQLITE_OMIT_GET_TABLE
7522 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
7523 #endif
7524 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
7525 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 },
7526 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
7527 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
7528 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
7529 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
7530 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
7531 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
7532 { "sqlite3_key", (Tcl_CmdProc*)test_key },
7533 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
7534 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
7535 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
7536 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
7537 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
7538 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
7539 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
7540 { "printf", (Tcl_CmdProc*)test_printf },
7541 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
7542 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address },
7544 static struct {
7545 char *zName;
7546 Tcl_ObjCmdProc *xProc;
7547 void *clientData;
7548 } aObjCmd[] = {
7549 { "sqlite3_db_config", test_sqlite3_db_config, 0 },
7550 { "bad_behavior", test_bad_behavior, (void*)&iZero },
7551 { "register_dbstat_vtab", test_register_dbstat_vtab },
7552 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
7553 { "intarray_addr", test_intarray_addr, 0 },
7554 { "int64array_addr", test_int64array_addr, 0 },
7555 { "doublearray_addr", test_doublearray_addr, 0 },
7556 { "textarray_addr", test_textarray_addr, 0 },
7557 { "sqlite3_bind_int", test_bind_int, 0 },
7558 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
7559 { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 },
7560 { "sqlite3_bind_int64", test_bind_int64, 0 },
7561 { "sqlite3_bind_double", test_bind_double, 0 },
7562 { "sqlite3_bind_null", test_bind_null ,0 },
7563 { "sqlite3_bind_text", test_bind_text ,0 },
7564 { "sqlite3_bind_text16", test_bind_text16 ,0 },
7565 { "sqlite3_bind_blob", test_bind_blob ,0 },
7566 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
7567 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
7568 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
7569 { "sqlite3_clear_bindings", test_clear_bindings, 0},
7570 { "sqlite3_sleep", test_sleep, 0},
7571 { "sqlite3_errcode", test_errcode ,0 },
7572 { "sqlite3_extended_errcode", test_ex_errcode ,0 },
7573 { "sqlite3_errmsg", test_errmsg ,0 },
7574 { "sqlite3_errmsg16", test_errmsg16 ,0 },
7575 { "sqlite3_open", test_open ,0 },
7576 { "sqlite3_open16", test_open16 ,0 },
7577 { "sqlite3_open_v2", test_open_v2 ,0 },
7578 { "sqlite3_complete16", test_complete16 ,0 },
7579 { "sqlite3_normalize", test_normalize ,0 },
7581 { "sqlite3_prepare", test_prepare ,0 },
7582 { "sqlite3_prepare16", test_prepare16 ,0 },
7583 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
7584 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
7585 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
7586 { "sqlite3_finalize", test_finalize ,0 },
7587 { "sqlite3_stmt_status", test_stmt_status ,0 },
7588 { "sqlite3_reset", test_reset ,0 },
7589 { "sqlite3_expired", test_expired ,0 },
7590 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
7591 { "sqlite3_changes", test_changes ,0 },
7592 { "sqlite3_step", test_step ,0 },
7593 { "sqlite3_sql", test_sql ,0 },
7594 { "sqlite3_expanded_sql", test_ex_sql ,0 },
7595 { "sqlite3_next_stmt", test_next_stmt ,0 },
7596 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 },
7597 { "sqlite3_stmt_busy", test_stmt_busy ,0 },
7598 { "uses_stmt_journal", uses_stmt_journal ,0 },
7600 { "sqlite3_release_memory", test_release_memory, 0},
7601 { "sqlite3_db_release_memory", test_db_release_memory, 0},
7602 { "sqlite3_db_cacheflush", test_db_cacheflush, 0},
7603 { "sqlite3_system_errno", test_system_errno, 0},
7604 { "sqlite3_db_filename", test_db_filename, 0},
7605 { "sqlite3_db_readonly", test_db_readonly, 0},
7606 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
7607 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
7608 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
7610 { "sqlite3_load_extension", test_load_extension, 0},
7611 { "sqlite3_enable_load_extension", test_enable_load, 0},
7612 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
7613 { "sqlite3_limit", test_limit, 0},
7614 { "dbconfig_maindbname_icecube", test_dbconfig_maindbname_icecube },
7616 { "save_prng_state", save_prng_state, 0 },
7617 { "restore_prng_state", restore_prng_state, 0 },
7618 { "reset_prng_state", reset_prng_state, 0 },
7619 { "database_never_corrupt", database_never_corrupt, 0},
7620 { "database_may_be_corrupt", database_may_be_corrupt, 0},
7621 { "optimization_control", optimization_control,0},
7622 #if SQLITE_OS_WIN
7623 { "lock_win32_file", win32_file_lock, 0 },
7624 { "exists_win32_path", win32_exists_path, 0 },
7625 { "find_win32_file", win32_find_file, 0 },
7626 { "delete_win32_file", win32_delete_file, 0 },
7627 { "make_win32_dir", win32_mkdir, 0 },
7628 { "remove_win32_dir", win32_rmdir, 0 },
7629 #endif
7630 { "tcl_objproc", runAsObjProc, 0 },
7632 /* sqlite3_column_*() API */
7633 { "sqlite3_column_count", test_column_count ,0 },
7634 { "sqlite3_data_count", test_data_count ,0 },
7635 { "sqlite3_column_type", test_column_type ,0 },
7636 { "sqlite3_column_blob", test_column_blob ,0 },
7637 { "sqlite3_column_double", test_column_double ,0 },
7638 { "sqlite3_column_int64", test_column_int64 ,0 },
7639 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
7640 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
7641 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
7642 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
7643 #ifndef SQLITE_OMIT_DECLTYPE
7644 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
7645 #endif
7646 #ifdef SQLITE_ENABLE_COLUMN_METADATA
7647 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
7648 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
7649 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
7650 #endif
7652 #ifndef SQLITE_OMIT_UTF16
7653 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
7654 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
7655 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
7656 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
7657 #ifndef SQLITE_OMIT_DECLTYPE
7658 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
7659 #endif
7660 #ifdef SQLITE_ENABLE_COLUMN_METADATA
7661 {"sqlite3_column_database_name16",
7662 test_stmt_utf16, (void*)sqlite3_column_database_name16},
7663 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
7664 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
7665 #endif
7666 #endif
7667 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
7668 { "sqlite3_global_recover", test_global_recover, 0 },
7669 { "working_64bit_int", working_64bit_int, 0 },
7670 { "vfs_unlink_test", vfs_unlink_test, 0 },
7671 { "vfs_initfail_test", vfs_initfail_test, 0 },
7672 { "vfs_unregister_all", vfs_unregister_all, 0 },
7673 { "vfs_reregister_all", vfs_reregister_all, 0 },
7674 { "file_control_test", file_control_test, 0 },
7675 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
7676 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
7677 { "file_control_chunksize_test", file_control_chunksize_test, 0 },
7678 { "file_control_sizehint_test", file_control_sizehint_test, 0 },
7679 #if SQLITE_OS_WIN
7680 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 },
7681 { "file_control_win32_get_handle", file_control_win32_get_handle, 0 },
7682 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 },
7683 #endif
7684 { "file_control_persist_wal", file_control_persist_wal, 0 },
7685 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
7686 { "file_control_vfsname", file_control_vfsname, 0 },
7687 { "file_control_tempfilename", file_control_tempfilename, 0 },
7688 { "sqlite3_vfs_list", vfs_list, 0 },
7689 { "sqlite3_create_function_v2", test_create_function_v2, 0 },
7691 /* Functions from os.h */
7692 #ifndef SQLITE_OMIT_UTF16
7693 { "add_test_collate", test_collate, 0 },
7694 { "add_test_collate_needed", test_collate_needed, 0 },
7695 { "add_test_function", test_function, 0 },
7696 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 },
7697 #endif
7698 { "sqlite3_test_errstr", test_errstr, 0 },
7699 { "tcl_variable_type", tcl_variable_type, 0 },
7700 #ifndef SQLITE_OMIT_SHARED_CACHE
7701 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
7702 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
7703 #endif
7704 { "sqlite3_libversion_number", test_libversion_number, 0 },
7705 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
7706 #ifndef SQLITE_OMIT_INCRBLOB
7707 { "sqlite3_blob_reopen", test_blob_reopen, 0 },
7708 #endif
7709 { "pcache_stats", test_pcache_stats, 0 },
7710 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7711 { "sqlite3_unlock_notify", test_unlock_notify, 0 },
7712 #endif
7713 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
7714 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 },
7715 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 },
7716 { "test_sqlite3_log", test_sqlite3_log, 0 },
7717 #ifndef SQLITE_OMIT_EXPLAIN
7718 { "print_explain_query_plan", test_print_eqp, 0 },
7719 #endif
7720 { "sqlite3_test_control", test_test_control },
7721 #if SQLITE_OS_UNIX
7722 { "getrusage", test_getrusage },
7723 #endif
7724 { "load_static_extension", tclLoadStaticExtensionCmd },
7725 { "sorter_test_fakeheap", sorter_test_fakeheap },
7726 { "sorter_test_sort4_helper", sorter_test_sort4_helper },
7727 #ifdef SQLITE_USER_AUTHENTICATION
7728 { "sqlite3_user_authenticate", test_user_authenticate, 0 },
7729 { "sqlite3_user_add", test_user_add, 0 },
7730 { "sqlite3_user_change", test_user_change, 0 },
7731 { "sqlite3_user_delete", test_user_delete, 0 },
7732 #endif
7733 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
7734 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 },
7735 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 },
7736 #endif
7737 #ifdef SQLITE_ENABLE_SQLLOG
7738 { "sqlite3_config_sqllog", test_config_sqllog, 0 },
7739 #endif
7740 { "vfs_current_time_int64", vfsCurrentTimeInt64, 0 },
7741 #ifdef SQLITE_ENABLE_SNAPSHOT
7742 { "sqlite3_snapshot_get", test_snapshot_get, 0 },
7743 { "sqlite3_snapshot_open", test_snapshot_open, 0 },
7744 { "sqlite3_snapshot_free", test_snapshot_free, 0 },
7745 { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 },
7746 { "sqlite3_snapshot_recover", test_snapshot_recover, 0 },
7747 { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 },
7748 { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 },
7749 { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 },
7750 #endif
7751 { "sqlite3_delete_database", test_delete_database, 0 },
7752 { "atomic_batch_write", test_atomic_batch_write, 0 },
7753 { "sqlite3_mmap_warm", test_mmap_warm, 0 },
7755 static int bitmask_size = sizeof(Bitmask)*8;
7756 static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
7757 int i;
7758 extern int sqlite3_sync_count, sqlite3_fullsync_count;
7759 extern int sqlite3_opentemp_count;
7760 extern int sqlite3_like_count;
7761 extern int sqlite3_xferopt_count;
7762 extern int sqlite3_pager_readdb_count;
7763 extern int sqlite3_pager_writedb_count;
7764 extern int sqlite3_pager_writej_count;
7765 #if SQLITE_OS_WIN
7766 extern LONG volatile sqlite3_os_type;
7767 #endif
7768 #ifdef SQLITE_DEBUG
7769 extern int sqlite3WhereTrace;
7770 extern int sqlite3OSTrace;
7771 extern int sqlite3WalTrace;
7772 #endif
7773 #ifdef SQLITE_TEST
7774 #ifdef SQLITE_ENABLE_FTS3
7775 extern int sqlite3_fts3_enable_parentheses;
7776 #endif
7777 #endif
7779 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
7780 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
7782 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
7783 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
7784 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
7786 Tcl_LinkVar(interp, "sqlite_search_count",
7787 (char*)&sqlite3_search_count, TCL_LINK_INT);
7788 Tcl_LinkVar(interp, "sqlite_found_count",
7789 (char*)&sqlite3_found_count, TCL_LINK_INT);
7790 Tcl_LinkVar(interp, "sqlite_sort_count",
7791 (char*)&sqlite3_sort_count, TCL_LINK_INT);
7792 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
7793 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
7794 Tcl_LinkVar(interp, "sqlite_like_count",
7795 (char*)&sqlite3_like_count, TCL_LINK_INT);
7796 Tcl_LinkVar(interp, "sqlite_interrupt_count",
7797 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
7798 Tcl_LinkVar(interp, "sqlite_open_file_count",
7799 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
7800 Tcl_LinkVar(interp, "sqlite_current_time",
7801 (char*)&sqlite3_current_time, TCL_LINK_INT);
7802 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
7803 Tcl_LinkVar(interp, "sqlite_hostid_num",
7804 (char*)&sqlite3_hostid_num, TCL_LINK_INT);
7805 #endif
7806 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
7807 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
7808 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
7809 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
7810 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
7811 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
7812 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
7813 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
7814 #ifndef SQLITE_OMIT_UTF16
7815 Tcl_LinkVar(interp, "unaligned_string_counter",
7816 (char*)&unaligned_string_counter, TCL_LINK_INT);
7817 #endif
7818 #ifndef SQLITE_OMIT_UTF16
7819 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
7820 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
7821 #endif
7822 #if SQLITE_OS_WIN
7823 Tcl_LinkVar(interp, "sqlite_os_type",
7824 (char*)&sqlite3_os_type, TCL_LINK_LONG);
7825 #endif
7826 #ifdef SQLITE_TEST
7828 static const char *query_plan = "*** OBSOLETE VARIABLE ***";
7829 Tcl_LinkVar(interp, "sqlite_query_plan",
7830 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
7832 #endif
7833 #ifdef SQLITE_DEBUG
7834 Tcl_LinkVar(interp, "sqlite_where_trace",
7835 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
7836 Tcl_LinkVar(interp, "sqlite_os_trace",
7837 (char*)&sqlite3OSTrace, TCL_LINK_INT);
7838 #ifndef SQLITE_OMIT_WAL
7839 Tcl_LinkVar(interp, "sqlite_wal_trace",
7840 (char*)&sqlite3WalTrace, TCL_LINK_INT);
7841 #endif
7842 #endif
7843 #ifndef SQLITE_OMIT_DISKIO
7844 Tcl_LinkVar(interp, "sqlite_opentemp_count",
7845 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
7846 #endif
7847 Tcl_LinkVar(interp, "sqlite_static_bind_value",
7848 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
7849 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
7850 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
7851 Tcl_LinkVar(interp, "sqlite_temp_directory",
7852 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
7853 Tcl_LinkVar(interp, "sqlite_data_directory",
7854 (char*)&sqlite3_data_directory, TCL_LINK_STRING);
7855 Tcl_LinkVar(interp, "bitmask_size",
7856 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
7857 Tcl_LinkVar(interp, "longdouble_size",
7858 (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
7859 Tcl_LinkVar(interp, "sqlite_sync_count",
7860 (char*)&sqlite3_sync_count, TCL_LINK_INT);
7861 Tcl_LinkVar(interp, "sqlite_fullsync_count",
7862 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
7863 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
7864 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
7865 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
7866 #endif
7867 return TCL_OK;