Merge sqlite-release(3.42.0) into prerelease-integration
[sqlcipher.git] / src / test1.c
blob7a9ed9c9fb2b854e09610ab71508b1f5d081876f
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 # include <windows.h>
20 #else
21 # include <unistd.h>
22 # if defined(__APPLE__)
23 # include <sys/param.h>
24 # include <sys/sysctl.h>
25 # endif
26 #endif
28 #include "vdbeInt.h"
29 #if defined(INCLUDE_SQLITE_TCL_H)
30 # include "sqlite_tcl.h"
31 #else
32 # include "tcl.h"
33 #endif
34 #include <stdlib.h>
35 #include <string.h>
38 ** This is a copy of the first part of the SqliteDb structure in
39 ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
40 ** can extract the sqlite3* pointer from an existing Tcl SQLite
41 ** connection.
43 struct SqliteDb {
44 sqlite3 *db;
48 ** Convert text generated by the "%p" conversion format back into
49 ** a pointer.
51 static int testHexToInt(int h){
52 if( h>='0' && h<='9' ){
53 return h - '0';
54 }else if( h>='a' && h<='f' ){
55 return h - 'a' + 10;
56 }else{
57 assert( h>='A' && h<='F' );
58 return h - 'A' + 10;
61 void *sqlite3TestTextToPtr(const char *z){
62 void *p;
63 u64 v;
64 u32 v2;
65 if( z[0]=='0' && z[1]=='x' ){
66 z += 2;
68 v = 0;
69 while( *z ){
70 v = (v<<4) + testHexToInt(*z);
71 z++;
73 if( sizeof(p)==sizeof(v) ){
74 memcpy(&p, &v, sizeof(p));
75 }else{
76 assert( sizeof(p)==sizeof(v2) );
77 v2 = (u32)v;
78 memcpy(&p, &v2, sizeof(p));
80 return p;
85 ** A TCL command that returns the address of the sqlite* pointer
86 ** for an sqlite connection instance. Bad things happen if the
87 ** input is not an sqlite connection.
89 static int SQLITE_TCLAPI get_sqlite_pointer(
90 void * clientData,
91 Tcl_Interp *interp,
92 int objc,
93 Tcl_Obj *CONST objv[]
95 struct SqliteDb *p;
96 Tcl_CmdInfo cmdInfo;
97 char zBuf[100];
98 if( objc!=2 ){
99 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
100 return TCL_ERROR;
102 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
103 Tcl_AppendResult(interp, "command not found: ",
104 Tcl_GetString(objv[1]), (char*)0);
105 return TCL_ERROR;
107 p = (struct SqliteDb*)cmdInfo.objClientData;
108 sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", p->db);
109 Tcl_AppendResult(interp, zBuf, 0);
110 return TCL_OK;
114 ** Decode a pointer to an sqlite3 object.
116 int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
117 struct SqliteDb *p;
118 Tcl_CmdInfo cmdInfo;
119 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
120 p = (struct SqliteDb*)cmdInfo.objClientData;
121 *ppDb = p->db;
122 }else{
123 *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
125 return TCL_OK;
128 #if SQLITE_OS_WIN
130 ** Decode a Win32 HANDLE object.
132 int getWin32Handle(Tcl_Interp *interp, const char *zA, LPHANDLE phFile){
133 *phFile = (HANDLE)sqlite3TestTextToPtr(zA);
134 return TCL_OK;
136 #endif
138 extern const char *sqlite3ErrName(int);
139 #define t1ErrorName sqlite3ErrName
142 ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
143 ** fact that the sqlite3* is the first field in the Vdbe structure.
145 #define StmtToDb(X) sqlite3_db_handle(X)
148 ** Check a return value to make sure it agrees with the results
149 ** from sqlite3_errcode.
151 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
152 if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK
153 && sqlite3_errcode(db)!=rc ){
154 char zBuf[200];
155 int r2 = sqlite3_errcode(db);
156 sqlite3_snprintf(sizeof(zBuf), zBuf,
157 "error code %s (%d) does not match sqlite3_errcode %s (%d)",
158 t1ErrorName(rc), rc, t1ErrorName(r2), r2);
159 Tcl_ResetResult(interp);
160 Tcl_AppendResult(interp, zBuf, 0);
161 return 1;
163 return 0;
167 ** Decode a pointer to an sqlite3_stmt object.
169 static int getStmtPointer(
170 Tcl_Interp *interp,
171 const char *zArg,
172 sqlite3_stmt **ppStmt
174 *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
175 return TCL_OK;
179 ** Generate a text representation of a pointer that can be understood
180 ** by the getDbPointer and getVmPointer routines above.
182 ** The problem is, on some machines (Solaris) if you do a printf with
183 ** "%p" you cannot turn around and do a scanf with the same "%p" and
184 ** get your pointer back. You have to prepend a "0x" before it will
185 ** work. Or at least that is what is reported to me (drh). But this
186 ** behavior varies from machine to machine. The solution used her is
187 ** to test the string right after it is generated to see if it can be
188 ** understood by scanf, and if not, try prepending an "0x" to see if
189 ** that helps. If nothing works, a fatal error is generated.
191 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
192 sqlite3_snprintf(100, zPtr, "%p", p);
193 return TCL_OK;
197 ** The callback routine for sqlite3_exec_printf().
199 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
200 Tcl_DString *str = (Tcl_DString*)pArg;
201 int i;
203 if( Tcl_DStringLength(str)==0 ){
204 for(i=0; i<argc; i++){
205 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
208 for(i=0; i<argc; i++){
209 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
211 return 0;
215 ** The I/O tracing callback.
217 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
218 static FILE *iotrace_file = 0;
219 static void io_trace_callback(const char *zFormat, ...){
220 va_list ap;
221 va_start(ap, zFormat);
222 vfprintf(iotrace_file, zFormat, ap);
223 va_end(ap);
224 fflush(iotrace_file);
226 #endif
229 ** Usage: io_trace FILENAME
231 ** Turn I/O tracing on or off. If FILENAME is not an empty string,
232 ** I/O tracing begins going into FILENAME. If FILENAME is an empty
233 ** string, I/O tracing is turned off.
235 static int SQLITE_TCLAPI test_io_trace(
236 void *NotUsed,
237 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
238 int argc, /* Number of arguments */
239 char **argv /* Text of each argument */
241 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
242 if( argc!=2 ){
243 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
244 " FILENAME\"", 0);
245 return TCL_ERROR;
247 if( iotrace_file ){
248 if( iotrace_file!=stdout && iotrace_file!=stderr ){
249 fclose(iotrace_file);
251 iotrace_file = 0;
252 sqlite3IoTrace = 0;
254 if( argv[1][0] ){
255 if( strcmp(argv[1],"stdout")==0 ){
256 iotrace_file = stdout;
257 }else if( strcmp(argv[1],"stderr")==0 ){
258 iotrace_file = stderr;
259 }else{
260 iotrace_file = fopen(argv[1], "w");
262 sqlite3IoTrace = io_trace_callback;
264 #endif
265 return TCL_OK;
269 ** Usage: clang_sanitize_address
271 ** Returns true if the program was compiled using clang with the
272 ** -fsanitize=address switch on the command line. False otherwise.
274 ** Also return true if the OMIT_MISUSE environment variable exists.
276 static int SQLITE_TCLAPI clang_sanitize_address(
277 void *NotUsed,
278 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
279 int argc, /* Number of arguments */
280 char **argv /* Text of each argument */
282 int res = 0;
283 #if defined(__has_feature)
284 # if __has_feature(address_sanitizer)
285 res = 1;
286 # endif
287 #endif
288 #ifdef __SANITIZE_ADDRESS__
289 res = 1;
290 #endif
291 if( res==0 && getenv("OMIT_MISUSE")!=0 ) res = 1;
292 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
293 return TCL_OK;
297 ** Usage: sqlite3_exec_printf DB FORMAT STRING
299 ** Invoke the sqlite3_exec_printf() interface using the open database
300 ** DB. The SQL is the string FORMAT. The format string should contain
301 ** one %s or %q. STRING is the value inserted into %s or %q.
303 static int SQLITE_TCLAPI test_exec_printf(
304 void *NotUsed,
305 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
306 int argc, /* Number of arguments */
307 char **argv /* Text of each argument */
309 sqlite3 *db;
310 Tcl_DString str;
311 int rc;
312 char *zErr = 0;
313 char *zSql;
314 char zBuf[30];
315 if( argc!=4 ){
316 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
317 " DB FORMAT STRING", 0);
318 return TCL_ERROR;
320 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
321 Tcl_DStringInit(&str);
322 zSql = sqlite3_mprintf(argv[2], argv[3]);
323 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
324 sqlite3_free(zSql);
325 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
326 Tcl_AppendElement(interp, zBuf);
327 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
328 Tcl_DStringFree(&str);
329 if( zErr ) sqlite3_free(zErr);
330 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
331 return TCL_OK;
335 ** Usage: sqlite3_exec_hex DB HEX
337 ** Invoke the sqlite3_exec() on a string that is obtained by translating
338 ** HEX into ASCII. Most characters are translated as is. %HH becomes
339 ** a hex character.
341 static int SQLITE_TCLAPI test_exec_hex(
342 void *NotUsed,
343 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
344 int argc, /* Number of arguments */
345 char **argv /* Text of each argument */
347 sqlite3 *db;
348 Tcl_DString str;
349 int rc, i, j;
350 char *zErr = 0;
351 char *zHex;
352 char zSql[501];
353 char zBuf[30];
354 if( argc!=3 ){
355 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
356 " DB HEX", 0);
357 return TCL_ERROR;
359 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
360 zHex = argv[2];
361 for(i=j=0; i<(sizeof(zSql)-1) && zHex[j]; i++, j++){
362 if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){
363 zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]);
364 j += 2;
365 }else{
366 zSql[i] = zHex[j];
369 zSql[i] = 0;
370 Tcl_DStringInit(&str);
371 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
372 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
373 Tcl_AppendElement(interp, zBuf);
374 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
375 Tcl_DStringFree(&str);
376 if( zErr ) sqlite3_free(zErr);
377 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
378 return TCL_OK;
382 ** Usage: db_enter DB
383 ** db_leave DB
385 ** Enter or leave the mutex on a database connection.
387 static int SQLITE_TCLAPI db_enter(
388 void *NotUsed,
389 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
390 int argc, /* Number of arguments */
391 char **argv /* Text of each argument */
393 sqlite3 *db;
394 if( argc!=2 ){
395 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
396 " DB", 0);
397 return TCL_ERROR;
399 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
400 sqlite3_mutex_enter(db->mutex);
401 return TCL_OK;
403 static int SQLITE_TCLAPI db_leave(
404 void *NotUsed,
405 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
406 int argc, /* Number of arguments */
407 char **argv /* Text of each argument */
409 sqlite3 *db;
410 if( argc!=2 ){
411 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
412 " DB", 0);
413 return TCL_ERROR;
415 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
416 sqlite3_mutex_leave(db->mutex);
417 return TCL_OK;
421 ** Usage: sqlite3_exec DB SQL
423 ** Invoke the sqlite3_exec interface using the open database DB
425 static int SQLITE_TCLAPI test_exec(
426 void *NotUsed,
427 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
428 int argc, /* Number of arguments */
429 char **argv /* Text of each argument */
431 sqlite3 *db;
432 Tcl_DString str;
433 int rc;
434 char *zErr = 0;
435 char *zSql;
436 int i, j;
437 char zBuf[30];
438 if( argc!=3 ){
439 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
440 " DB SQL", 0);
441 return TCL_ERROR;
443 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
444 Tcl_DStringInit(&str);
445 zSql = sqlite3_mprintf("%s", argv[2]);
446 for(i=j=0; zSql[i];){
447 if( zSql[i]=='%' ){
448 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
449 i += 3;
450 }else{
451 zSql[j++] = zSql[i++];
454 zSql[j] = 0;
455 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
456 sqlite3_free(zSql);
457 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
458 Tcl_AppendElement(interp, zBuf);
459 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
460 Tcl_DStringFree(&str);
461 if( zErr ) sqlite3_free(zErr);
462 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
463 return TCL_OK;
467 ** Usage: sqlite3_exec_nr DB SQL
469 ** Invoke the sqlite3_exec interface using the open database DB. Discard
470 ** all results
472 static int SQLITE_TCLAPI test_exec_nr(
473 void *NotUsed,
474 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
475 int argc, /* Number of arguments */
476 char **argv /* Text of each argument */
478 sqlite3 *db;
479 int rc;
480 char *zErr = 0;
481 if( argc!=3 ){
482 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
483 " DB SQL", 0);
484 return TCL_ERROR;
486 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
487 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
488 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
489 return TCL_OK;
493 ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
495 ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
496 ** concatenate arg0 through argn using separator as the separator.
497 ** Return the result.
499 static int SQLITE_TCLAPI test_mprintf_z(
500 void *NotUsed,
501 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
502 int argc, /* Number of arguments */
503 char **argv /* Text of each argument */
505 char *zResult = 0;
506 int i;
508 for(i=2; i<argc && (i==2 || zResult); i++){
509 zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]);
511 Tcl_AppendResult(interp, zResult, 0);
512 sqlite3_free(zResult);
513 return TCL_OK;
517 ** Usage: sqlite3_mprintf_n_test STRING
519 ** Test the %n format of sqlite_mprintf(). Return the length of the
520 ** input string.
522 static int SQLITE_TCLAPI test_mprintf_n(
523 void *NotUsed,
524 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
525 int argc, /* Number of arguments */
526 char **argv /* Text of each argument */
528 char *zStr;
529 int n = 0;
530 zStr = sqlite3_mprintf("%s%n", argv[1], &n);
531 sqlite3_free(zStr);
532 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
533 return TCL_OK;
537 ** Usage: sqlite3_snprintf_int SIZE FORMAT INT
539 ** Test the of sqlite3_snprintf() routine. SIZE is the size of the
540 ** output buffer in bytes. The maximum size is 100. FORMAT is the
541 ** format string. INT is a single integer argument. The FORMAT
542 ** string must require no more than this one integer argument. If
543 ** You pass in a format string that requires more than one argument,
544 ** bad things will happen.
546 static int SQLITE_TCLAPI test_snprintf_int(
547 void *NotUsed,
548 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
549 int argc, /* Number of arguments */
550 char **argv /* Text of each argument */
552 char zStr[100];
553 int n = atoi(argv[1]);
554 const char *zFormat = argv[2];
555 int a1 = atoi(argv[3]);
556 if( n>sizeof(zStr) ) n = sizeof(zStr);
557 sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
558 sqlite3_snprintf(n, zStr, zFormat, a1);
559 Tcl_AppendResult(interp, zStr, 0);
560 return TCL_OK;
563 #ifndef SQLITE_OMIT_GET_TABLE
566 ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
568 ** Invoke the sqlite3_get_table_printf() interface using the open database
569 ** DB. The SQL is the string FORMAT. The format string should contain
570 ** one %s or %q. STRING is the value inserted into %s or %q.
572 static int SQLITE_TCLAPI test_get_table_printf(
573 void *NotUsed,
574 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
575 int argc, /* Number of arguments */
576 char **argv /* Text of each argument */
578 sqlite3 *db;
579 Tcl_DString str;
580 int rc;
581 char *zErr = 0;
582 int nRow = 0, nCol = 0;
583 char **aResult;
584 int i;
585 char zBuf[30];
586 char *zSql;
587 int resCount = -1;
588 if( argc==5 ){
589 if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
591 if( argc!=4 && argc!=5 ){
592 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
593 " DB FORMAT STRING ?COUNT?", 0);
594 return TCL_ERROR;
596 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
597 Tcl_DStringInit(&str);
598 zSql = sqlite3_mprintf(argv[2],argv[3]);
599 if( argc==5 ){
600 rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
601 }else{
602 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
603 resCount = (nRow+1)*nCol;
605 sqlite3_free(zSql);
606 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
607 Tcl_AppendElement(interp, zBuf);
608 if( rc==SQLITE_OK ){
609 if( argc==4 ){
610 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nRow);
611 Tcl_AppendElement(interp, zBuf);
612 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nCol);
613 Tcl_AppendElement(interp, zBuf);
615 for(i=0; i<resCount; i++){
616 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
618 }else{
619 Tcl_AppendElement(interp, zErr);
621 sqlite3_free_table(aResult);
622 if( zErr ) sqlite3_free(zErr);
623 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
624 return TCL_OK;
627 #endif /* SQLITE_OMIT_GET_TABLE */
631 ** Usage: sqlite3_last_insert_rowid DB
633 ** Returns the integer ROWID of the most recent insert.
635 static int SQLITE_TCLAPI test_last_rowid(
636 void *NotUsed,
637 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
638 int argc, /* Number of arguments */
639 char **argv /* Text of each argument */
641 sqlite3 *db;
642 char zBuf[30];
644 if( argc!=2 ){
645 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
646 return TCL_ERROR;
648 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
649 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", sqlite3_last_insert_rowid(db));
650 Tcl_AppendResult(interp, zBuf, 0);
651 return SQLITE_OK;
655 ** Usage: sqlite3_key DB KEY
657 ** Set the codec key.
659 static int SQLITE_TCLAPI test_key(
660 void *NotUsed,
661 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
662 int argc, /* Number of arguments */
663 char **argv /* Text of each argument */
665 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
666 sqlite3 *db;
667 const char *zKey;
668 int nKey;
669 if( argc!=3 ){
670 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
671 " FILENAME\"", 0);
672 return TCL_ERROR;
674 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
675 zKey = argv[2];
676 nKey = strlen(zKey);
677 sqlite3_key(db, zKey, nKey);
678 #endif
679 return TCL_OK;
683 ** Usage: sqlite3_rekey DB KEY
685 ** Change the codec key.
687 static int SQLITE_TCLAPI test_rekey(
688 void *NotUsed,
689 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
690 int argc, /* Number of arguments */
691 char **argv /* Text of each argument */
693 #ifdef SQLITE_HAS_CODEC
694 sqlite3 *db;
695 const char *zKey;
696 int nKey;
697 if( argc!=3 ){
698 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
699 " FILENAME\"", 0);
700 return TCL_ERROR;
702 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
703 zKey = argv[2];
704 nKey = strlen(zKey);
705 sqlite3_rekey(db, zKey, nKey);
706 #endif
707 return TCL_OK;
711 ** Usage: sqlite3_close DB
713 ** Closes the database opened by sqlite3_open.
715 static int SQLITE_TCLAPI sqlite_test_close(
716 void *NotUsed,
717 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
718 int argc, /* Number of arguments */
719 char **argv /* Text of each argument */
721 sqlite3 *db;
722 int rc;
723 if( argc!=2 ){
724 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
725 " FILENAME\"", 0);
726 return TCL_ERROR;
728 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
729 rc = sqlite3_close(db);
730 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
731 return TCL_OK;
735 ** Usage: sqlite3_close_v2 DB
737 ** Closes the database opened by sqlite3_open.
739 static int SQLITE_TCLAPI sqlite_test_close_v2(
740 void *NotUsed,
741 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
742 int argc, /* Number of arguments */
743 char **argv /* Text of each argument */
745 sqlite3 *db;
746 int rc;
747 if( argc!=2 ){
748 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
749 " FILENAME\"", 0);
750 return TCL_ERROR;
752 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
753 rc = sqlite3_close_v2(db);
754 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
755 return TCL_OK;
759 ** Implementation of the x_coalesce() function.
760 ** Return the first argument non-NULL argument.
762 static void t1_ifnullFunc(
763 sqlite3_context *context,
764 int argc,
765 sqlite3_value **argv
767 int i;
768 for(i=0; i<argc; i++){
769 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
770 int n = sqlite3_value_bytes(argv[i]);
771 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
772 n, SQLITE_TRANSIENT);
773 break;
779 ** These are test functions. hex8() interprets its argument as
780 ** UTF8 and returns a hex encoding. hex16le() interprets its argument
781 ** as UTF16le and returns a hex encoding.
783 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
784 const unsigned char *z;
785 int i;
786 char zBuf[200];
787 z = sqlite3_value_text(argv[0]);
788 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
789 sqlite3_snprintf(sizeof(zBuf)-i*2, &zBuf[i*2], "%02x", z[i]);
791 zBuf[i*2] = 0;
792 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
794 #ifndef SQLITE_OMIT_UTF16
795 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
796 const unsigned short int *z;
797 int i;
798 char zBuf[400];
799 z = sqlite3_value_text16(argv[0]);
800 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
801 sqlite3_snprintf(sizeof(zBuf)-i*4, &zBuf[i*4],"%04x", z[i]&0xff);
803 zBuf[i*4] = 0;
804 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
806 #endif
809 ** A structure into which to accumulate text.
811 struct dstr {
812 int nAlloc; /* Space allocated */
813 int nUsed; /* Space used */
814 char *z; /* The space */
818 ** Append text to a dstr
820 static void dstrAppend(struct dstr *p, const char *z, int divider){
821 int n = (int)strlen(z);
822 if( p->nUsed + n + 2 > p->nAlloc ){
823 char *zNew;
824 p->nAlloc = p->nAlloc*2 + n + 200;
825 zNew = sqlite3_realloc(p->z, p->nAlloc);
826 if( zNew==0 ){
827 sqlite3_free(p->z);
828 memset(p, 0, sizeof(*p));
829 return;
831 p->z = zNew;
833 if( divider && p->nUsed>0 ){
834 p->z[p->nUsed++] = divider;
836 memcpy(&p->z[p->nUsed], z, n+1);
837 p->nUsed += n;
841 ** Invoked for each callback from sqlite3ExecFunc
843 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
844 struct dstr *p = (struct dstr*)pData;
845 int i;
846 for(i=0; i<argc; i++){
847 if( argv[i]==0 ){
848 dstrAppend(p, "NULL", ' ');
849 }else{
850 dstrAppend(p, argv[i], ' ');
853 return 0;
857 ** Implementation of the x_sqlite_exec() function. This function takes
858 ** a single argument and attempts to execute that argument as SQL code.
859 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
861 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
862 ** from within a function call.
864 ** This routine simulates the effect of having two threads attempt to
865 ** use the same database at the same time.
867 static void sqlite3ExecFunc(
868 sqlite3_context *context,
869 int argc,
870 sqlite3_value **argv
872 struct dstr x;
873 memset(&x, 0, sizeof(x));
874 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
875 (char*)sqlite3_value_text(argv[0]),
876 execFuncCallback, &x, 0);
877 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
878 sqlite3_free(x.z);
882 ** Implementation of tkt2213func(), a scalar function that takes exactly
883 ** one argument. It has two interesting features:
885 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
886 ** If the three pointers returned are not the same an SQL error is raised.
888 ** * Otherwise it returns a copy of the text representation of its
889 ** argument in such a way as the VDBE representation is a Mem* cell
890 ** with the MEM_Term flag clear.
892 ** Ticket #2213 can therefore be tested by evaluating the following
893 ** SQL expression:
895 ** tkt2213func(tkt2213func('a string'));
897 static void tkt2213Function(
898 sqlite3_context *context,
899 int argc,
900 sqlite3_value **argv
902 int nText;
903 unsigned char const *zText1;
904 unsigned char const *zText2;
905 unsigned char const *zText3;
907 nText = sqlite3_value_bytes(argv[0]);
908 zText1 = sqlite3_value_text(argv[0]);
909 zText2 = sqlite3_value_text(argv[0]);
910 zText3 = sqlite3_value_text(argv[0]);
912 if( zText1!=zText2 || zText2!=zText3 ){
913 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
914 }else{
915 char *zCopy = (char *)sqlite3_malloc(nText);
916 memcpy(zCopy, zText1, nText);
917 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
922 ** The following SQL function takes 4 arguments. The 2nd and
923 ** 4th argument must be one of these strings: 'text', 'text16',
924 ** or 'blob' corresponding to API functions
926 ** sqlite3_value_text()
927 ** sqlite3_value_text16()
928 ** sqlite3_value_blob()
930 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
931 ** corresponding to APIs:
933 ** sqlite3_value_bytes()
934 ** sqlite3_value_bytes16()
935 ** noop
937 ** The APIs designated by the 2nd through 4th arguments are applied
938 ** to the first argument in order. If the pointers returned by the
939 ** second and fourth are different, this routine returns 1. Otherwise,
940 ** this routine returns 0.
942 ** This function is used to test to see when returned pointers from
943 ** the _text(), _text16() and _blob() APIs become invalidated.
945 static void ptrChngFunction(
946 sqlite3_context *context,
947 int argc,
948 sqlite3_value **argv
950 const void *p1, *p2;
951 const char *zCmd;
952 if( argc!=4 ) return;
953 zCmd = (const char*)sqlite3_value_text(argv[1]);
954 if( zCmd==0 ) return;
955 if( strcmp(zCmd,"text")==0 ){
956 p1 = (const void*)sqlite3_value_text(argv[0]);
957 #ifndef SQLITE_OMIT_UTF16
958 }else if( strcmp(zCmd, "text16")==0 ){
959 p1 = (const void*)sqlite3_value_text16(argv[0]);
960 #endif
961 }else if( strcmp(zCmd, "blob")==0 ){
962 p1 = (const void*)sqlite3_value_blob(argv[0]);
963 }else{
964 return;
966 zCmd = (const char*)sqlite3_value_text(argv[2]);
967 if( zCmd==0 ) return;
968 if( strcmp(zCmd,"bytes")==0 ){
969 sqlite3_value_bytes(argv[0]);
970 #ifndef SQLITE_OMIT_UTF16
971 }else if( strcmp(zCmd, "bytes16")==0 ){
972 sqlite3_value_bytes16(argv[0]);
973 #endif
974 }else if( strcmp(zCmd, "noop")==0 ){
975 /* do nothing */
976 }else{
977 return;
979 zCmd = (const char*)sqlite3_value_text(argv[3]);
980 if( zCmd==0 ) return;
981 if( strcmp(zCmd,"text")==0 ){
982 p2 = (const void*)sqlite3_value_text(argv[0]);
983 #ifndef SQLITE_OMIT_UTF16
984 }else if( strcmp(zCmd, "text16")==0 ){
985 p2 = (const void*)sqlite3_value_text16(argv[0]);
986 #endif
987 }else if( strcmp(zCmd, "blob")==0 ){
988 p2 = (const void*)sqlite3_value_blob(argv[0]);
989 }else{
990 return;
992 sqlite3_result_int(context, p1!=p2);
996 ** This SQL function returns a different answer each time it is called, even if
997 ** the arguments are the same.
999 static void nondeterministicFunction(
1000 sqlite3_context *context,
1001 int argc,
1002 sqlite3_value **argv
1004 static int cnt = 0;
1005 sqlite3_result_int(context, cnt++);
1009 ** This SQL function returns the integer value of its argument as a MEM_IntReal
1010 ** value.
1012 static void intrealFunction(
1013 sqlite3_context *context,
1014 int argc,
1015 sqlite3_value **argv
1017 sqlite3_int64 v = sqlite3_value_int64(argv[0]);
1018 sqlite3_result_int64(context, v);
1019 sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, context);
1023 ** Usage: sqlite3_create_function DB
1025 ** Call the sqlite3_create_function API on the given database in order
1026 ** to create a function named "x_coalesce". This function does the same thing
1027 ** as the "coalesce" function. This function also registers an SQL function
1028 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
1029 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
1030 ** The effect is similar to trying to use the same database connection from
1031 ** two threads at the same time.
1033 ** The original motivation for this routine was to be able to call the
1034 ** sqlite3_create_function function while a query is in progress in order
1035 ** to test the SQLITE_MISUSE detection logic.
1037 static int SQLITE_TCLAPI test_create_function(
1038 void *NotUsed,
1039 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1040 int argc, /* Number of arguments */
1041 char **argv /* Text of each argument */
1043 int rc;
1044 sqlite3 *db;
1046 if( argc!=2 ){
1047 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1048 " DB\"", 0);
1049 return TCL_ERROR;
1051 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1052 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0,
1053 t1_ifnullFunc, 0, 0);
1054 if( rc==SQLITE_OK ){
1055 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1056 0, hex8Func, 0, 0);
1058 #ifndef SQLITE_OMIT_UTF16
1059 if( rc==SQLITE_OK ){
1060 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
1061 0, hex16Func, 0, 0);
1063 #endif
1064 if( rc==SQLITE_OK ){
1065 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
1066 tkt2213Function, 0, 0);
1068 if( rc==SQLITE_OK ){
1069 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
1070 ptrChngFunction, 0, 0);
1073 /* Functions counter1() and counter2() have the same implementation - they
1074 ** both return an ascending integer with each call. But counter1() is marked
1075 ** as non-deterministic and counter2() is marked as deterministic.
1077 if( rc==SQLITE_OK ){
1078 rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
1079 0, nondeterministicFunction, 0, 0);
1081 if( rc==SQLITE_OK ){
1082 rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
1083 0, nondeterministicFunction, 0, 0);
1086 /* The intreal() function converts its argument to an integer and returns
1087 ** it as a MEM_IntReal.
1089 if( rc==SQLITE_OK ){
1090 rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8,
1091 0, intrealFunction, 0, 0);
1094 #ifndef SQLITE_OMIT_UTF16
1095 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1096 ** because it is not tested anywhere else. */
1097 if( rc==SQLITE_OK ){
1098 const void *zUtf16;
1099 sqlite3_value *pVal;
1100 sqlite3_mutex_enter(db->mutex);
1101 pVal = sqlite3ValueNew(db);
1102 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
1103 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1104 if( db->mallocFailed ){
1105 rc = SQLITE_NOMEM;
1106 }else{
1107 rc = sqlite3_create_function16(db, zUtf16,
1108 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
1110 sqlite3ValueFree(pVal);
1111 sqlite3_mutex_leave(db->mutex);
1113 #endif
1115 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1116 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1117 return TCL_OK;
1121 ** Usage: sqlite3_drop_modules DB ?NAME ...?
1123 ** Invoke the sqlite3_drop_modules(D,L) interface on database
1124 ** connection DB, in order to drop all modules except those named in
1125 ** the argument.
1127 static int SQLITE_TCLAPI test_drop_modules(
1128 void *NotUsed,
1129 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1130 int argc, /* Number of arguments */
1131 char **argv /* Text of each argument */
1133 sqlite3 *db;
1135 if( argc<2 ){
1136 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1137 " DB\"", 0);
1138 return TCL_ERROR;
1140 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1141 #ifndef SQLITE_OMIT_VIRTUALTABLE
1142 sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0);
1143 #endif
1144 return TCL_OK;
1148 ** Routines to implement the x_count() aggregate function.
1150 ** x_count() counts the number of non-null arguments. But there are
1151 ** some twists for testing purposes.
1153 ** If the argument to x_count() is 40 then a UTF-8 error is reported
1154 ** on the step function. If x_count(41) is seen, then a UTF-16 error
1155 ** is reported on the step function. If the total count is 42, then
1156 ** a UTF-8 error is reported on the finalize function.
1158 typedef struct t1CountCtx t1CountCtx;
1159 struct t1CountCtx {
1160 int n;
1162 static void t1CountStep(
1163 sqlite3_context *context,
1164 int argc,
1165 sqlite3_value **argv
1167 t1CountCtx *p;
1168 p = sqlite3_aggregate_context(context, sizeof(*p));
1169 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
1170 p->n++;
1172 if( argc>0 ){
1173 int v = sqlite3_value_int(argv[0]);
1174 if( v==40 ){
1175 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
1176 #ifndef SQLITE_OMIT_UTF16
1177 }else if( v==41 ){
1178 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1179 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
1180 #endif
1184 static void t1CountFinalize(sqlite3_context *context){
1185 t1CountCtx *p;
1186 p = sqlite3_aggregate_context(context, sizeof(*p));
1187 if( p ){
1188 if( p->n==42 ){
1189 sqlite3_result_error(context, "x_count totals to 42", -1);
1190 }else{
1191 sqlite3_result_int(context, p ? p->n : 0);
1196 #ifndef SQLITE_OMIT_DEPRECATED
1197 static void legacyCountStep(
1198 sqlite3_context *context,
1199 int argc,
1200 sqlite3_value **argv
1202 /* no-op */
1205 static void legacyCountFinalize(sqlite3_context *context){
1206 sqlite3_result_int(context, sqlite3_aggregate_count(context));
1208 #endif
1211 ** Usage: sqlite3_create_aggregate DB
1213 ** Call the sqlite3_create_function API on the given database in order
1214 ** to create a function named "x_count". This function is similar
1215 ** to the built-in count() function, with a few special quirks
1216 ** for testing the sqlite3_result_error() APIs.
1218 ** The original motivation for this routine was to be able to call the
1219 ** sqlite3_create_aggregate function while a query is in progress in order
1220 ** to test the SQLITE_MISUSE detection logic. See misuse.test.
1222 ** This routine was later extended to test the use of sqlite3_result_error()
1223 ** within aggregate functions.
1225 ** Later: It is now also extended to register the aggregate function
1226 ** "legacy_count()" with the supplied database handle. This is used
1227 ** to test the deprecated sqlite3_aggregate_count() API.
1229 static int SQLITE_TCLAPI test_create_aggregate(
1230 void *NotUsed,
1231 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1232 int argc, /* Number of arguments */
1233 char **argv /* Text of each argument */
1235 sqlite3 *db;
1236 int rc;
1237 if( argc!=2 ){
1238 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1239 " FILENAME\"", 0);
1240 return TCL_ERROR;
1242 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1243 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
1244 t1CountStep,t1CountFinalize);
1245 if( rc==SQLITE_OK ){
1246 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
1247 t1CountStep,t1CountFinalize);
1249 #ifndef SQLITE_OMIT_DEPRECATED
1250 if( rc==SQLITE_OK ){
1251 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1252 legacyCountStep, legacyCountFinalize
1255 #endif
1256 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1257 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1258 return TCL_OK;
1263 ** Usage: printf TEXT
1265 ** Send output to printf. Use this rather than puts to merge the output
1266 ** in the correct sequence with debugging printfs inserted into C code.
1267 ** Puts uses a separate buffer and debugging statements will be out of
1268 ** sequence if it is used.
1270 static int SQLITE_TCLAPI test_printf(
1271 void *NotUsed,
1272 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1273 int argc, /* Number of arguments */
1274 char **argv /* Text of each argument */
1276 if( argc!=2 ){
1277 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1278 " TEXT\"", 0);
1279 return TCL_ERROR;
1281 printf("%s\n", argv[1]);
1282 return TCL_OK;
1288 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1290 ** Call mprintf with three integer arguments
1292 static int SQLITE_TCLAPI sqlite3_mprintf_int(
1293 void *NotUsed,
1294 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1295 int argc, /* Number of arguments */
1296 char **argv /* Text of each argument */
1298 int a[3], i;
1299 char *z;
1300 if( argc!=5 ){
1301 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1302 " FORMAT INT INT INT\"", 0);
1303 return TCL_ERROR;
1305 for(i=2; i<5; i++){
1306 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1308 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1309 Tcl_AppendResult(interp, z, 0);
1310 sqlite3_free(z);
1311 return TCL_OK;
1315 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1317 ** Call mprintf with three 64-bit integer arguments
1319 static int SQLITE_TCLAPI sqlite3_mprintf_int64(
1320 void *NotUsed,
1321 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1322 int argc, /* Number of arguments */
1323 char **argv /* Text of each argument */
1325 int i;
1326 sqlite_int64 a[3];
1327 char *z;
1328 if( argc!=5 ){
1329 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1330 " FORMAT INT INT INT\"", 0);
1331 return TCL_ERROR;
1333 for(i=2; i<5; i++){
1334 if( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){
1335 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1336 return TCL_ERROR;
1339 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1340 Tcl_AppendResult(interp, z, 0);
1341 sqlite3_free(z);
1342 return TCL_OK;
1346 ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1348 ** Call mprintf with three long integer arguments. This might be the
1349 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1350 ** platform.
1352 static int SQLITE_TCLAPI sqlite3_mprintf_long(
1353 void *NotUsed,
1354 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1355 int argc, /* Number of arguments */
1356 char **argv /* Text of each argument */
1358 int i;
1359 long int a[3];
1360 int b[3];
1361 char *z;
1362 if( argc!=5 ){
1363 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1364 " FORMAT INT INT INT\"", 0);
1365 return TCL_ERROR;
1367 for(i=2; i<5; i++){
1368 if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
1369 a[i-2] = (long int)b[i-2];
1370 a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
1372 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1373 Tcl_AppendResult(interp, z, 0);
1374 sqlite3_free(z);
1375 return TCL_OK;
1379 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1381 ** Call mprintf with two integer arguments and one string argument
1383 static int SQLITE_TCLAPI sqlite3_mprintf_str(
1384 void *NotUsed,
1385 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1386 int argc, /* Number of arguments */
1387 char **argv /* Text of each argument */
1389 int a[3], i;
1390 char *z;
1391 if( argc<4 || argc>5 ){
1392 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1393 " FORMAT INT INT ?STRING?\"", 0);
1394 return TCL_ERROR;
1396 for(i=2; i<4; i++){
1397 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1399 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1400 Tcl_AppendResult(interp, z, 0);
1401 sqlite3_free(z);
1402 return TCL_OK;
1406 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1408 ** Call mprintf with two integer arguments and one string argument
1410 static int SQLITE_TCLAPI sqlite3_snprintf_str(
1411 void *NotUsed,
1412 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1413 int argc, /* Number of arguments */
1414 char **argv /* Text of each argument */
1416 int a[3], i;
1417 int n;
1418 char *z;
1419 if( argc<5 || argc>6 ){
1420 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1421 " INT FORMAT INT INT ?STRING?\"", 0);
1422 return TCL_ERROR;
1424 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1425 if( n<0 ){
1426 Tcl_AppendResult(interp, "N must be non-negative", 0);
1427 return TCL_ERROR;
1429 for(i=3; i<5; i++){
1430 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1432 z = sqlite3_malloc( n+1 );
1433 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1434 Tcl_AppendResult(interp, z, 0);
1435 sqlite3_free(z);
1436 return TCL_OK;
1440 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1442 ** Call mprintf with two integer arguments and one double argument
1444 static int SQLITE_TCLAPI sqlite3_mprintf_double(
1445 void *NotUsed,
1446 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1447 int argc, /* Number of arguments */
1448 char **argv /* Text of each argument */
1450 int a[3], i;
1451 double r;
1452 char *z;
1453 if( argc!=5 ){
1454 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1455 " FORMAT INT INT DOUBLE\"", 0);
1456 return TCL_ERROR;
1458 for(i=2; i<4; i++){
1459 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1461 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1462 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1463 Tcl_AppendResult(interp, z, 0);
1464 sqlite3_free(z);
1465 return TCL_OK;
1469 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1471 ** Call mprintf with a single double argument which is the product of the
1472 ** two arguments given above. This is used to generate overflow and underflow
1473 ** doubles to test that they are converted properly.
1475 static int SQLITE_TCLAPI sqlite3_mprintf_scaled(
1476 void *NotUsed,
1477 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1478 int argc, /* Number of arguments */
1479 char **argv /* Text of each argument */
1481 int i;
1482 double r[2];
1483 char *z;
1484 if( argc!=4 ){
1485 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1486 " FORMAT DOUBLE DOUBLE\"", 0);
1487 return TCL_ERROR;
1489 for(i=2; i<4; i++){
1490 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1492 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1493 Tcl_AppendResult(interp, z, 0);
1494 sqlite3_free(z);
1495 return TCL_OK;
1499 ** Usage: sqlite3_mprintf_stronly FORMAT STRING
1501 ** Call mprintf with a single double argument which is the product of the
1502 ** two arguments given above. This is used to generate overflow and underflow
1503 ** doubles to test that they are converted properly.
1505 static int SQLITE_TCLAPI sqlite3_mprintf_stronly(
1506 void *NotUsed,
1507 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1508 int argc, /* Number of arguments */
1509 char **argv /* Text of each argument */
1511 char *z;
1512 if( argc!=3 ){
1513 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1514 " FORMAT STRING\"", 0);
1515 return TCL_ERROR;
1517 z = sqlite3_mprintf(argv[1], argv[2]);
1518 Tcl_AppendResult(interp, z, 0);
1519 sqlite3_free(z);
1520 return TCL_OK;
1524 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1526 ** Call mprintf with a single double argument which is derived from the
1527 ** hexadecimal encoding of an IEEE double.
1529 static int SQLITE_TCLAPI sqlite3_mprintf_hexdouble(
1530 void *NotUsed,
1531 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1532 int argc, /* Number of arguments */
1533 char **argv /* Text of each argument */
1535 char *z;
1536 double r;
1537 unsigned int x1, x2;
1538 sqlite_uint64 d;
1539 if( argc!=3 ){
1540 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1541 " FORMAT STRING\"", 0);
1542 return TCL_ERROR;
1544 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1545 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1546 return TCL_ERROR;
1548 d = x2;
1549 d = (d<<32) + x1;
1550 memcpy(&r, &d, sizeof(r));
1551 z = sqlite3_mprintf(argv[1], r);
1552 Tcl_AppendResult(interp, z, 0);
1553 sqlite3_free(z);
1554 return TCL_OK;
1558 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1561 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1562 static int SQLITE_TCLAPI test_enable_shared(
1563 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1564 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1565 int objc, /* Number of arguments */
1566 Tcl_Obj *CONST objv[] /* Command arguments */
1568 int rc;
1569 int enable;
1570 int ret = 0;
1572 if( objc!=2 && objc!=1 ){
1573 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
1574 return TCL_ERROR;
1576 ret = sqlite3GlobalConfig.sharedCacheEnabled;
1578 if( objc==2 ){
1579 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1580 return TCL_ERROR;
1582 rc = sqlite3_enable_shared_cache(enable);
1583 if( rc!=SQLITE_OK ){
1584 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1585 return TCL_ERROR;
1588 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1589 return TCL_OK;
1591 #endif
1596 ** Usage: sqlite3_extended_result_codes DB BOOLEAN
1599 static int SQLITE_TCLAPI test_extended_result_codes(
1600 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1601 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1602 int objc, /* Number of arguments */
1603 Tcl_Obj *CONST objv[] /* Command arguments */
1605 int enable;
1606 sqlite3 *db;
1608 if( objc!=3 ){
1609 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1610 return TCL_ERROR;
1612 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1613 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1614 sqlite3_extended_result_codes(db, enable);
1615 return TCL_OK;
1619 ** Usage: sqlite3_libversion_number
1622 static int SQLITE_TCLAPI test_libversion_number(
1623 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1624 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1625 int objc, /* Number of arguments */
1626 Tcl_Obj *CONST objv[] /* Command arguments */
1628 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1629 return TCL_OK;
1633 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1636 static int SQLITE_TCLAPI test_table_column_metadata(
1637 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1638 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1639 int objc, /* Number of arguments */
1640 Tcl_Obj *CONST objv[] /* Command arguments */
1642 sqlite3 *db;
1643 const char *zDb;
1644 const char *zTbl;
1645 const char *zCol;
1646 int rc;
1647 Tcl_Obj *pRet;
1649 const char *zDatatype;
1650 const char *zCollseq;
1651 int notnull;
1652 int primarykey;
1653 int autoincrement;
1655 if( objc!=5 && objc!=4 ){
1656 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1657 return TCL_ERROR;
1659 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1660 zDb = Tcl_GetString(objv[2]);
1661 zTbl = Tcl_GetString(objv[3]);
1662 zCol = objc==5 ? Tcl_GetString(objv[4]) : 0;
1664 if( strlen(zDb)==0 ) zDb = 0;
1666 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1667 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1669 if( rc!=SQLITE_OK ){
1670 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1671 return TCL_ERROR;
1674 pRet = Tcl_NewObj();
1675 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1676 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1677 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1678 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1679 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1680 Tcl_SetObjResult(interp, pRet);
1682 return TCL_OK;
1685 #ifndef SQLITE_OMIT_INCRBLOB
1687 static int SQLITE_TCLAPI blobHandleFromObj(
1688 Tcl_Interp *interp,
1689 Tcl_Obj *pObj,
1690 sqlite3_blob **ppBlob
1692 char *z;
1693 int n;
1695 z = Tcl_GetStringFromObj(pObj, &n);
1696 if( n==0 ){
1697 *ppBlob = 0;
1698 }else{
1699 int notUsed;
1700 Tcl_Channel channel;
1701 ClientData instanceData;
1703 channel = Tcl_GetChannel(interp, z, &notUsed);
1704 if( !channel ) return TCL_ERROR;
1706 Tcl_Flush(channel);
1707 Tcl_Seek(channel, 0, SEEK_SET);
1709 instanceData = Tcl_GetChannelInstanceData(channel);
1710 *ppBlob = *((sqlite3_blob **)instanceData);
1713 return TCL_OK;
1716 static int SQLITE_TCLAPI test_blob_reopen(
1717 ClientData clientData, /* Not used */
1718 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1719 int objc, /* Number of arguments */
1720 Tcl_Obj *CONST objv[] /* Command arguments */
1722 Tcl_WideInt iRowid;
1723 sqlite3_blob *pBlob;
1724 int rc;
1726 if( objc!=3 ){
1727 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
1728 return TCL_ERROR;
1731 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
1732 if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
1734 rc = sqlite3_blob_reopen(pBlob, iRowid);
1735 if( rc!=SQLITE_OK ){
1736 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
1739 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1742 #endif
1745 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1747 ** This Tcl proc is used for testing the experimental
1748 ** sqlite3_create_collation_v2() interface.
1750 struct TestCollationX {
1751 Tcl_Interp *interp;
1752 Tcl_Obj *pCmp;
1753 Tcl_Obj *pDel;
1755 typedef struct TestCollationX TestCollationX;
1756 static void testCreateCollationDel(void *pCtx){
1757 TestCollationX *p = (TestCollationX *)pCtx;
1759 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1760 if( rc!=TCL_OK ){
1761 Tcl_BackgroundError(p->interp);
1764 Tcl_DecrRefCount(p->pCmp);
1765 Tcl_DecrRefCount(p->pDel);
1766 sqlite3_free((void *)p);
1768 static int testCreateCollationCmp(
1769 void *pCtx,
1770 int nLeft,
1771 const void *zLeft,
1772 int nRight,
1773 const void *zRight
1775 TestCollationX *p = (TestCollationX *)pCtx;
1776 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1777 int iRes = 0;
1779 Tcl_IncrRefCount(pScript);
1780 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1781 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1783 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1784 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1786 Tcl_BackgroundError(p->interp);
1788 Tcl_DecrRefCount(pScript);
1790 return iRes;
1792 static int SQLITE_TCLAPI test_create_collation_v2(
1793 ClientData clientData, /* Not used */
1794 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1795 int objc, /* Number of arguments */
1796 Tcl_Obj *CONST objv[] /* Command arguments */
1798 TestCollationX *p;
1799 sqlite3 *db;
1800 int rc;
1802 if( objc!=5 ){
1803 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1804 return TCL_ERROR;
1806 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1808 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1809 p->pCmp = objv[3];
1810 p->pDel = objv[4];
1811 p->interp = interp;
1812 Tcl_IncrRefCount(p->pCmp);
1813 Tcl_IncrRefCount(p->pDel);
1815 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1816 (void *)p, testCreateCollationCmp, testCreateCollationDel
1818 if( rc!=SQLITE_MISUSE ){
1819 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1820 "an invalid encoding", (char*)0);
1821 return TCL_ERROR;
1823 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1824 (void *)p, testCreateCollationCmp, testCreateCollationDel
1826 return TCL_OK;
1830 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
1832 ** Available switches are:
1834 ** -func SCRIPT
1835 ** -step SCRIPT
1836 ** -final SCRIPT
1837 ** -destroy SCRIPT
1839 typedef struct CreateFunctionV2 CreateFunctionV2;
1840 struct CreateFunctionV2 {
1841 Tcl_Interp *interp;
1842 Tcl_Obj *pFunc; /* Script for function invocation */
1843 Tcl_Obj *pStep; /* Script for agg. step invocation */
1844 Tcl_Obj *pFinal; /* Script for agg. finalization invocation */
1845 Tcl_Obj *pDestroy; /* Destructor script */
1847 static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1849 static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1851 static void cf2Final(sqlite3_context *ctx){
1853 static void cf2Destroy(void *pUser){
1854 CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
1856 if( p->interp && p->pDestroy ){
1857 int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
1858 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
1861 if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
1862 if( p->pStep ) Tcl_DecrRefCount(p->pStep);
1863 if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
1864 if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
1865 sqlite3_free(p);
1867 static int SQLITE_TCLAPI test_create_function_v2(
1868 ClientData clientData, /* Not used */
1869 Tcl_Interp *interp, /* The invoking TCL interpreter */
1870 int objc, /* Number of arguments */
1871 Tcl_Obj *CONST objv[] /* Command arguments */
1873 sqlite3 *db;
1874 const char *zFunc;
1875 int nArg;
1876 int enc;
1877 CreateFunctionV2 *p;
1878 int i;
1879 int rc;
1881 struct EncTable {
1882 const char *zEnc;
1883 int enc;
1884 } aEnc[] = {
1885 {"utf8", SQLITE_UTF8 },
1886 {"utf16", SQLITE_UTF16 },
1887 {"utf16le", SQLITE_UTF16LE },
1888 {"utf16be", SQLITE_UTF16BE },
1889 {"any", SQLITE_ANY },
1890 {"0", 0 },
1891 {0, 0 }
1894 if( objc<5 || (objc%2)==0 ){
1895 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
1896 return TCL_ERROR;
1899 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1900 zFunc = Tcl_GetString(objv[2]);
1901 if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
1902 if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
1903 "encoding", 0, &enc)
1905 return TCL_ERROR;
1907 enc = aEnc[enc].enc;
1909 p = sqlite3_malloc(sizeof(CreateFunctionV2));
1910 assert( p );
1911 memset(p, 0, sizeof(CreateFunctionV2));
1912 p->interp = interp;
1914 for(i=5; i<objc; i+=2){
1915 int iSwitch;
1916 const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
1917 if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
1918 sqlite3_free(p);
1919 return TCL_ERROR;
1922 switch( iSwitch ){
1923 case 0: p->pFunc = objv[i+1]; break;
1924 case 1: p->pStep = objv[i+1]; break;
1925 case 2: p->pFinal = objv[i+1]; break;
1926 case 3: p->pDestroy = objv[i+1]; break;
1929 if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
1930 if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
1931 if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
1932 if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
1934 if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
1935 if( p->pStep ) Tcl_IncrRefCount(p->pStep);
1936 if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
1937 if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
1939 rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
1940 (p->pFunc ? cf2Func : 0),
1941 (p->pStep ? cf2Step : 0),
1942 (p->pFinal ? cf2Final : 0),
1943 cf2Destroy
1945 if( rc!=SQLITE_OK ){
1946 Tcl_ResetResult(interp);
1947 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
1948 return TCL_ERROR;
1950 return TCL_OK;
1954 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1956 static int SQLITE_TCLAPI test_load_extension(
1957 ClientData clientData, /* Not used */
1958 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1959 int objc, /* Number of arguments */
1960 Tcl_Obj *CONST objv[] /* Command arguments */
1962 Tcl_CmdInfo cmdInfo;
1963 sqlite3 *db;
1964 int rc;
1965 char *zDb;
1966 char *zFile;
1967 char *zProc = 0;
1968 char *zErr = 0;
1970 if( objc!=4 && objc!=3 ){
1971 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1972 return TCL_ERROR;
1974 zDb = Tcl_GetString(objv[1]);
1975 zFile = Tcl_GetString(objv[2]);
1976 if( objc==4 ){
1977 zProc = Tcl_GetString(objv[3]);
1980 /* Extract the C database handle from the Tcl command name */
1981 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1982 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1983 return TCL_ERROR;
1985 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1986 assert(db);
1988 /* Call the underlying C function. If an error occurs, set rc to
1989 ** TCL_ERROR and load any error string into the interpreter. If no
1990 ** error occurs, set rc to TCL_OK.
1992 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1993 rc = SQLITE_ERROR;
1994 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1995 (void)zProc;
1996 (void)zFile;
1997 #else
1998 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
1999 #endif
2000 if( rc!=SQLITE_OK ){
2001 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
2002 rc = TCL_ERROR;
2003 }else{
2004 rc = TCL_OK;
2006 sqlite3_free(zErr);
2008 return rc;
2012 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
2014 static int SQLITE_TCLAPI test_enable_load(
2015 ClientData clientData, /* Not used */
2016 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2017 int objc, /* Number of arguments */
2018 Tcl_Obj *CONST objv[] /* Command arguments */
2020 Tcl_CmdInfo cmdInfo;
2021 sqlite3 *db;
2022 char *zDb;
2023 int onoff;
2025 if( objc!=3 ){
2026 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
2027 return TCL_ERROR;
2029 zDb = Tcl_GetString(objv[1]);
2031 /* Extract the C database handle from the Tcl command name */
2032 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
2033 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
2034 return TCL_ERROR;
2036 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
2037 assert(db);
2039 /* Get the onoff parameter */
2040 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
2041 return TCL_ERROR;
2044 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2045 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
2046 return TCL_ERROR;
2047 #else
2048 sqlite3_enable_load_extension(db, onoff);
2049 return TCL_OK;
2050 #endif
2054 ** Usage: sqlite_abort
2056 ** Shutdown the process immediately. This is not a clean shutdown.
2057 ** This command is used to test the recoverability of a database in
2058 ** the event of a program crash.
2060 static int SQLITE_TCLAPI sqlite_abort(
2061 void *NotUsed,
2062 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2063 int argc, /* Number of arguments */
2064 char **argv /* Text of each argument */
2066 #if defined(_MSC_VER)
2067 /* We do this, otherwise the test will halt with a popup message
2068 * that we have to click away before the test will continue.
2070 _set_abort_behavior( 0, _CALL_REPORTFAULT );
2071 #endif
2072 exit(255);
2073 assert( interp==0 ); /* This will always fail */
2074 return TCL_OK;
2078 ** The following routine is a user-defined SQL function whose purpose
2079 ** is to test the sqlite_set_result() API.
2081 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2082 while( argc>=2 ){
2083 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
2084 if( zArg0 ){
2085 if( 0==sqlite3StrICmp(zArg0, "int") ){
2086 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
2087 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
2088 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
2089 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
2090 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
2091 SQLITE_TRANSIENT);
2092 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
2093 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
2094 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
2095 sqlite3_result_null(context);
2096 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
2097 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
2098 }else{
2099 goto error_out;
2101 }else{
2102 goto error_out;
2104 argc -= 2;
2105 argv += 2;
2107 return;
2109 error_out:
2110 sqlite3_result_error(context,"first argument should be one of: "
2111 "int int64 string double null value", -1);
2115 ** Usage: sqlite_register_test_function DB NAME
2117 ** Register the test SQL function on the database DB under the name NAME.
2119 static int SQLITE_TCLAPI test_register_func(
2120 void *NotUsed,
2121 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2122 int argc, /* Number of arguments */
2123 char **argv /* Text of each argument */
2125 sqlite3 *db;
2126 int rc;
2127 if( argc!=3 ){
2128 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2129 " DB FUNCTION-NAME", 0);
2130 return TCL_ERROR;
2132 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2133 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
2134 testFunc, 0, 0);
2135 if( rc!=0 ){
2136 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2137 return TCL_ERROR;
2139 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2140 return TCL_OK;
2144 ** Usage: sqlite3_finalize STMT
2146 ** Finalize a statement handle.
2148 static int SQLITE_TCLAPI test_finalize(
2149 void * clientData,
2150 Tcl_Interp *interp,
2151 int objc,
2152 Tcl_Obj *CONST objv[]
2154 sqlite3_stmt *pStmt;
2155 int rc;
2156 sqlite3 *db = 0;
2158 if( objc!=2 ){
2159 Tcl_AppendResult(interp, "wrong # args: should be \"",
2160 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2161 return TCL_ERROR;
2164 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2166 if( pStmt ){
2167 db = StmtToDb(pStmt);
2169 rc = sqlite3_finalize(pStmt);
2170 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2171 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2172 return TCL_OK;
2176 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2178 ** Get the value of a status counter from a statement.
2180 static int SQLITE_TCLAPI test_stmt_status(
2181 void * clientData,
2182 Tcl_Interp *interp,
2183 int objc,
2184 Tcl_Obj *CONST objv[]
2186 int iValue;
2187 int i, op = 0, resetFlag;
2188 const char *zOpName;
2189 sqlite3_stmt *pStmt;
2191 static const struct {
2192 const char *zName;
2193 int op;
2194 } aOp[] = {
2195 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
2196 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
2197 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
2198 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP },
2199 { "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE },
2200 { "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN },
2201 { "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED },
2203 if( objc!=4 ){
2204 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2205 return TCL_ERROR;
2207 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2208 zOpName = Tcl_GetString(objv[2]);
2209 for(i=0; i<ArraySize(aOp); i++){
2210 if( strcmp(aOp[i].zName, zOpName)==0 ){
2211 op = aOp[i].op;
2212 break;
2215 if( i>=ArraySize(aOp) ){
2216 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2218 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2219 iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2220 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2221 return TCL_OK;
2224 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2226 ** Usage: sqlite3_stmt_scanstatus ?-flags FLAGS? STMT IDX
2228 static int SQLITE_TCLAPI test_stmt_scanstatus(
2229 void * clientData,
2230 Tcl_Interp *interp,
2231 int objc,
2232 Tcl_Obj *CONST objv[]
2234 sqlite3_stmt *pStmt; /* First argument */
2235 int idx; /* Second argument */
2237 const char *zName;
2238 const char *zExplain;
2239 sqlite3_int64 nLoop;
2240 sqlite3_int64 nVisit;
2241 sqlite3_int64 nCycle;
2242 double rEst;
2243 int res;
2244 int flags = 0;
2245 int iSelectId = 0;
2246 int iParentId = 0;
2248 if( objc==5 ){
2249 struct Flag {
2250 const char *zFlag;
2251 int flag;
2252 } aTbl[] = {
2253 {"complex", SQLITE_SCANSTAT_COMPLEX},
2254 {0, 0}
2257 Tcl_Obj **aFlag = 0;
2258 int nFlag = 0;
2259 int ii;
2261 if( Tcl_ListObjGetElements(interp, objv[2], &nFlag, &aFlag) ){
2262 return TCL_ERROR;
2264 for(ii=0; ii<nFlag; ii++){
2265 int iVal = 0;
2266 int res = Tcl_GetIndexFromObjStruct(
2267 interp, aFlag[ii], aTbl, sizeof(aTbl[0]), "flag", 0, &iVal
2269 if( res ) return TCL_ERROR;
2270 flags |= aTbl[iVal].flag;
2274 if( objc!=3 && objc!=5 ){
2275 Tcl_WrongNumArgs(interp, 1, objv, "-flags FLAGS STMT IDX");
2276 return TCL_ERROR;
2278 if( getStmtPointer(interp, Tcl_GetString(objv[objc-2]), &pStmt)
2279 || Tcl_GetIntFromObj(interp, objv[objc-1], &idx)
2281 return TCL_ERROR;
2284 if( idx<0 ){
2285 Tcl_Obj *pRet = Tcl_NewObj();
2286 res = sqlite3_stmt_scanstatus_v2(
2287 pStmt, -1, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle
2289 sqlite3_stmt_scanstatus_v2(
2290 pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle);
2291 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1));
2292 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle));
2293 Tcl_SetObjResult(interp, pRet);
2294 }else{
2295 res = sqlite3_stmt_scanstatus_v2(
2296 pStmt, idx, SQLITE_SCANSTAT_NLOOP, flags, (void*)&nLoop
2298 if( res==0 ){
2299 Tcl_Obj *pRet = Tcl_NewObj();
2300 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
2301 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
2302 sqlite3_stmt_scanstatus_v2(
2303 pStmt, idx, SQLITE_SCANSTAT_NVISIT, flags, (void*)&nVisit);
2304 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
2305 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
2306 sqlite3_stmt_scanstatus_v2(
2307 pStmt, idx, SQLITE_SCANSTAT_EST, flags, (void*)&rEst);
2308 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
2309 Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
2310 sqlite3_stmt_scanstatus_v2(
2311 pStmt, idx, SQLITE_SCANSTAT_NAME, flags, (void*)&zName);
2312 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
2313 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
2314 sqlite3_stmt_scanstatus_v2(
2315 pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, flags, (void*)&zExplain);
2316 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
2317 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
2318 sqlite3_stmt_scanstatus_v2(
2319 pStmt, idx, SQLITE_SCANSTAT_SELECTID, flags, (void*)&iSelectId);
2320 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iSelectId", -1));
2321 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iSelectId));
2322 sqlite3_stmt_scanstatus_v2(
2323 pStmt, idx, SQLITE_SCANSTAT_PARENTID, flags, (void*)&iParentId);
2324 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iParentId", -1));
2325 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iParentId));
2326 sqlite3_stmt_scanstatus_v2(
2327 pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle);
2328 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1));
2329 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle));
2330 Tcl_SetObjResult(interp, pRet);
2331 }else{
2332 Tcl_ResetResult(interp);
2335 return TCL_OK;
2339 ** Usage: sqlite3_stmt_scanstatus_reset STMT
2341 static int SQLITE_TCLAPI test_stmt_scanstatus_reset(
2342 void * clientData,
2343 Tcl_Interp *interp,
2344 int objc,
2345 Tcl_Obj *CONST objv[]
2347 sqlite3_stmt *pStmt; /* First argument */
2348 if( objc!=2 ){
2349 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2350 return TCL_ERROR;
2352 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2353 sqlite3_stmt_scanstatus_reset(pStmt);
2354 return TCL_OK;
2356 #endif
2358 #ifdef SQLITE_ENABLE_SQLLOG
2360 ** Usage: sqlite3_config_sqllog
2362 ** Zero the SQLITE_CONFIG_SQLLOG configuration
2364 static int SQLITE_TCLAPI test_config_sqllog(
2365 void * clientData,
2366 Tcl_Interp *interp,
2367 int objc,
2368 Tcl_Obj *CONST objv[]
2370 if( objc!=1 ){
2371 Tcl_WrongNumArgs(interp, 1, objv, "");
2372 return TCL_ERROR;
2374 sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
2375 return TCL_OK;
2377 #endif
2380 ** Usage: sqlite3_config_sorterref
2382 ** Set the SQLITE_CONFIG_SORTERREF_SIZE configuration option
2384 static int SQLITE_TCLAPI test_config_sorterref(
2385 void * clientData,
2386 Tcl_Interp *interp,
2387 int objc,
2388 Tcl_Obj *CONST objv[]
2390 int iVal;
2391 if( objc!=2 ){
2392 Tcl_WrongNumArgs(interp, 1, objv, "NBYTE");
2393 return TCL_ERROR;
2395 if( Tcl_GetIntFromObj(interp, objv[1], &iVal) ) return TCL_ERROR;
2396 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, iVal);
2397 return TCL_OK;
2401 ** Usage: vfs_current_time_int64
2403 ** Return the value returned by the default VFS's xCurrentTimeInt64 method.
2405 static int SQLITE_TCLAPI vfsCurrentTimeInt64(
2406 void * clientData,
2407 Tcl_Interp *interp,
2408 int objc,
2409 Tcl_Obj *CONST objv[]
2411 i64 t;
2412 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
2413 if( objc!=1 ){
2414 Tcl_WrongNumArgs(interp, 1, objv, "");
2415 return TCL_ERROR;
2417 pVfs->xCurrentTimeInt64(pVfs, &t);
2418 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
2419 return TCL_OK;
2422 #ifndef SQLITE_OMIT_VIRTUALTABLE
2424 ** Usage: create_null_module DB NAME
2426 static int SQLITE_TCLAPI test_create_null_module(
2427 void * clientData,
2428 Tcl_Interp *interp,
2429 int objc,
2430 Tcl_Obj *CONST objv[]
2432 sqlite3 *db;
2433 char *zName;
2435 if( objc!=3 ){
2436 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2437 return TCL_ERROR;
2439 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2440 zName = Tcl_GetString(objv[2]);
2442 sqlite3_create_module(db, zName, 0, 0);
2443 return TCL_OK;
2445 #endif /* SQLITE_OMIT_VIRTUALTABLE */
2447 #ifdef SQLITE_ENABLE_SNAPSHOT
2449 ** Usage: sqlite3_snapshot_get DB DBNAME
2451 static int SQLITE_TCLAPI test_snapshot_get(
2452 void * clientData,
2453 Tcl_Interp *interp,
2454 int objc,
2455 Tcl_Obj *CONST objv[]
2457 int rc;
2458 sqlite3 *db;
2459 char *zName;
2460 sqlite3_snapshot *pSnapshot = 0;
2462 if( objc!=3 ){
2463 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2464 return TCL_ERROR;
2466 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2467 zName = Tcl_GetString(objv[2]);
2469 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2470 if( rc!=SQLITE_OK ){
2471 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2472 return TCL_ERROR;
2473 }else{
2474 char zBuf[100];
2475 if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
2476 Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
2478 return TCL_OK;
2480 #endif /* SQLITE_ENABLE_SNAPSHOT */
2482 #ifdef SQLITE_ENABLE_SNAPSHOT
2484 ** Usage: sqlite3_snapshot_recover DB DBNAME
2486 static int SQLITE_TCLAPI test_snapshot_recover(
2487 void * clientData,
2488 Tcl_Interp *interp,
2489 int objc,
2490 Tcl_Obj *CONST objv[]
2492 int rc;
2493 sqlite3 *db;
2494 char *zName;
2496 if( objc!=3 ){
2497 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2498 return TCL_ERROR;
2500 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2501 zName = Tcl_GetString(objv[2]);
2503 rc = sqlite3_snapshot_recover(db, zName);
2504 if( rc!=SQLITE_OK ){
2505 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2506 return TCL_ERROR;
2507 }else{
2508 Tcl_ResetResult(interp);
2510 return TCL_OK;
2512 #endif /* SQLITE_ENABLE_SNAPSHOT */
2514 #ifdef SQLITE_ENABLE_SNAPSHOT
2516 ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
2518 static int SQLITE_TCLAPI test_snapshot_open(
2519 void * clientData,
2520 Tcl_Interp *interp,
2521 int objc,
2522 Tcl_Obj *CONST objv[]
2524 int rc;
2525 sqlite3 *db;
2526 char *zName;
2527 sqlite3_snapshot *pSnapshot;
2529 if( objc!=4 ){
2530 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2531 return TCL_ERROR;
2533 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2534 zName = Tcl_GetString(objv[2]);
2535 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
2537 rc = sqlite3_snapshot_open(db, zName, pSnapshot);
2538 if( rc!=SQLITE_OK ){
2539 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2540 return TCL_ERROR;
2541 }else{
2542 Tcl_ResetResult(interp);
2544 return TCL_OK;
2546 #endif /* SQLITE_ENABLE_SNAPSHOT */
2548 #ifdef SQLITE_ENABLE_SNAPSHOT
2550 ** Usage: sqlite3_snapshot_free SNAPSHOT
2552 static int SQLITE_TCLAPI test_snapshot_free(
2553 void * clientData,
2554 Tcl_Interp *interp,
2555 int objc,
2556 Tcl_Obj *CONST objv[]
2558 sqlite3_snapshot *pSnapshot;
2559 if( objc!=2 ){
2560 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
2561 return TCL_ERROR;
2563 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2564 sqlite3_snapshot_free(pSnapshot);
2565 return TCL_OK;
2567 #endif /* SQLITE_ENABLE_SNAPSHOT */
2569 #ifdef SQLITE_ENABLE_SNAPSHOT
2571 ** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2
2573 static int SQLITE_TCLAPI test_snapshot_cmp(
2574 void * clientData,
2575 Tcl_Interp *interp,
2576 int objc,
2577 Tcl_Obj *CONST objv[]
2579 int res;
2580 sqlite3_snapshot *p1;
2581 sqlite3_snapshot *p2;
2582 if( objc!=3 ){
2583 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2584 return TCL_ERROR;
2586 p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2587 p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2]));
2588 res = sqlite3_snapshot_cmp(p1, p2);
2589 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2590 return TCL_OK;
2592 #endif /* SQLITE_ENABLE_SNAPSHOT */
2594 #ifdef SQLITE_ENABLE_SNAPSHOT
2596 ** Usage: sqlite3_snapshot_get_blob DB DBNAME
2598 static int SQLITE_TCLAPI test_snapshot_get_blob(
2599 void * clientData,
2600 Tcl_Interp *interp,
2601 int objc,
2602 Tcl_Obj *CONST objv[]
2604 int rc;
2605 sqlite3 *db;
2606 char *zName;
2607 sqlite3_snapshot *pSnapshot = 0;
2609 if( objc!=3 ){
2610 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2611 return TCL_ERROR;
2613 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2614 zName = Tcl_GetString(objv[2]);
2616 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2617 if( rc!=SQLITE_OK ){
2618 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2619 return TCL_ERROR;
2620 }else{
2621 Tcl_SetObjResult(interp,
2622 Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot))
2624 sqlite3_snapshot_free(pSnapshot);
2626 return TCL_OK;
2628 #endif /* SQLITE_ENABLE_SNAPSHOT */
2630 #ifdef SQLITE_ENABLE_SNAPSHOT
2632 ** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT
2634 static int SQLITE_TCLAPI test_snapshot_open_blob(
2635 void * clientData,
2636 Tcl_Interp *interp,
2637 int objc,
2638 Tcl_Obj *CONST objv[]
2640 int rc;
2641 sqlite3 *db;
2642 char *zName;
2643 unsigned char *pBlob;
2644 int nBlob;
2646 if( objc!=4 ){
2647 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2648 return TCL_ERROR;
2650 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2651 zName = Tcl_GetString(objv[2]);
2652 pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob);
2653 if( nBlob!=sizeof(sqlite3_snapshot) ){
2654 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2655 return TCL_ERROR;
2657 rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob);
2658 if( rc!=SQLITE_OK ){
2659 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2660 return TCL_ERROR;
2662 return TCL_OK;
2664 #endif /* SQLITE_ENABLE_SNAPSHOT */
2666 #ifdef SQLITE_ENABLE_SNAPSHOT
2668 ** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2
2670 static int SQLITE_TCLAPI test_snapshot_cmp_blob(
2671 void * clientData,
2672 Tcl_Interp *interp,
2673 int objc,
2674 Tcl_Obj *CONST objv[]
2676 int res;
2677 unsigned char *p1;
2678 unsigned char *p2;
2679 int n1;
2680 int n2;
2682 if( objc!=3 ){
2683 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2684 return TCL_ERROR;
2687 p1 = Tcl_GetByteArrayFromObj(objv[1], &n1);
2688 p2 = Tcl_GetByteArrayFromObj(objv[2], &n2);
2690 if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){
2691 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2692 return TCL_ERROR;
2695 res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2);
2696 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2697 return TCL_OK;
2699 #endif /* SQLITE_ENABLE_SNAPSHOT */
2702 ** Usage: sqlite3_delete_database FILENAME
2704 int sqlite3_delete_database(const char*); /* in test_delete.c */
2705 static int SQLITE_TCLAPI test_delete_database(
2706 void * clientData,
2707 Tcl_Interp *interp,
2708 int objc,
2709 Tcl_Obj *CONST objv[]
2711 int rc;
2712 const char *zFile;
2713 if( objc!=2 ){
2714 Tcl_WrongNumArgs(interp, 1, objv, "FILE");
2715 return TCL_ERROR;
2717 zFile = (const char*)Tcl_GetString(objv[1]);
2718 rc = sqlite3_delete_database(zFile);
2720 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2721 return TCL_OK;
2725 ** Usage: atomic_batch_write PATH
2727 static int SQLITE_TCLAPI test_atomic_batch_write(
2728 void * clientData,
2729 Tcl_Interp *interp,
2730 int objc,
2731 Tcl_Obj *CONST objv[]
2733 char *zFile = 0; /* Path to file to test */
2734 sqlite3 *db = 0; /* Database handle */
2735 sqlite3_file *pFd = 0; /* SQLite fd open on zFile */
2736 int bRes = 0; /* Integer result of this command */
2737 int dc = 0; /* Device-characteristics mask */
2738 int rc; /* sqlite3_open() return code */
2740 if( objc!=2 ){
2741 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
2742 return TCL_ERROR;
2744 zFile = Tcl_GetString(objv[1]);
2746 rc = sqlite3_open(zFile, &db);
2747 if( rc!=SQLITE_OK ){
2748 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
2749 sqlite3_close(db);
2750 return TCL_ERROR;
2753 rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
2754 dc = pFd->pMethods->xDeviceCharacteristics(pFd);
2755 if( dc & SQLITE_IOCAP_BATCH_ATOMIC ){
2756 bRes = 1;
2759 Tcl_SetObjResult(interp, Tcl_NewIntObj(bRes));
2760 sqlite3_close(db);
2761 return TCL_OK;
2765 ** Usage: sqlite3_next_stmt DB STMT
2767 ** Return the next statment in sequence after STMT.
2769 static int SQLITE_TCLAPI test_next_stmt(
2770 void * clientData,
2771 Tcl_Interp *interp,
2772 int objc,
2773 Tcl_Obj *CONST objv[]
2775 sqlite3_stmt *pStmt;
2776 sqlite3 *db = 0;
2777 char zBuf[50];
2779 if( objc!=3 ){
2780 Tcl_AppendResult(interp, "wrong # args: should be \"",
2781 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2782 return TCL_ERROR;
2785 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2786 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2787 pStmt = sqlite3_next_stmt(db, pStmt);
2788 if( pStmt ){
2789 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2790 Tcl_AppendResult(interp, zBuf, 0);
2792 return TCL_OK;
2796 ** Usage: sqlite3_stmt_readonly STMT
2798 ** Return true if STMT is a NULL pointer or a pointer to a statement
2799 ** that is guaranteed to leave the database unmodified.
2801 static int SQLITE_TCLAPI test_stmt_readonly(
2802 void * clientData,
2803 Tcl_Interp *interp,
2804 int objc,
2805 Tcl_Obj *CONST objv[]
2807 sqlite3_stmt *pStmt;
2808 int rc;
2810 if( objc!=2 ){
2811 Tcl_AppendResult(interp, "wrong # args: should be \"",
2812 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2813 return TCL_ERROR;
2816 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2817 rc = sqlite3_stmt_readonly(pStmt);
2818 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2819 return TCL_OK;
2823 ** Usage: sqlite3_stmt_isexplain STMT
2825 ** Return 1, 2, or 0 respectively if STMT is an EXPLAIN statement, an
2826 ** EXPLAIN QUERY PLAN statement or an ordinary statement or NULL pointer.
2828 static int SQLITE_TCLAPI test_stmt_isexplain(
2829 void * clientData,
2830 Tcl_Interp *interp,
2831 int objc,
2832 Tcl_Obj *CONST objv[]
2834 sqlite3_stmt *pStmt;
2835 int rc;
2837 if( objc!=2 ){
2838 Tcl_AppendResult(interp, "wrong # args: should be \"",
2839 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2840 return TCL_ERROR;
2843 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2844 rc = sqlite3_stmt_isexplain(pStmt);
2845 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
2846 return TCL_OK;
2850 ** Usage: sqlite3_stmt_busy STMT
2852 ** Return true if STMT is a non-NULL pointer to a statement
2853 ** that has been stepped but not to completion.
2855 static int SQLITE_TCLAPI test_stmt_busy(
2856 void * clientData,
2857 Tcl_Interp *interp,
2858 int objc,
2859 Tcl_Obj *CONST objv[]
2861 sqlite3_stmt *pStmt;
2862 int rc;
2864 if( objc!=2 ){
2865 Tcl_AppendResult(interp, "wrong # args: should be \"",
2866 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2867 return TCL_ERROR;
2870 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2871 rc = sqlite3_stmt_busy(pStmt);
2872 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2873 return TCL_OK;
2877 ** Usage: uses_stmt_journal STMT
2879 ** Return true if STMT uses a statement journal.
2881 static int SQLITE_TCLAPI uses_stmt_journal(
2882 void * clientData,
2883 Tcl_Interp *interp,
2884 int objc,
2885 Tcl_Obj *CONST objv[]
2887 sqlite3_stmt *pStmt;
2889 if( objc!=2 ){
2890 Tcl_AppendResult(interp, "wrong # args: should be \"",
2891 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2892 return TCL_ERROR;
2895 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2896 sqlite3_stmt_readonly(pStmt);
2897 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
2898 return TCL_OK;
2903 ** Usage: sqlite3_reset STMT
2905 ** Reset a statement handle.
2907 static int SQLITE_TCLAPI test_reset(
2908 void * clientData,
2909 Tcl_Interp *interp,
2910 int objc,
2911 Tcl_Obj *CONST objv[]
2913 sqlite3_stmt *pStmt;
2914 int rc;
2916 if( objc!=2 ){
2917 Tcl_AppendResult(interp, "wrong # args: should be \"",
2918 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2919 return TCL_ERROR;
2922 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2924 rc = sqlite3_reset(pStmt);
2925 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
2926 return TCL_ERROR;
2928 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2930 if( rc ){
2931 return TCL_ERROR;
2934 return TCL_OK;
2938 ** Usage: sqlite3_expired STMT
2940 ** Return TRUE if a recompilation of the statement is recommended.
2942 static int SQLITE_TCLAPI test_expired(
2943 void * clientData,
2944 Tcl_Interp *interp,
2945 int objc,
2946 Tcl_Obj *CONST objv[]
2948 #ifndef SQLITE_OMIT_DEPRECATED
2949 sqlite3_stmt *pStmt;
2950 if( objc!=2 ){
2951 Tcl_AppendResult(interp, "wrong # args: should be \"",
2952 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2953 return TCL_ERROR;
2955 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2956 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
2957 #endif
2958 return TCL_OK;
2962 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
2964 ** Transfer all bindings from FROMSTMT over to TOSTMT
2966 static int SQLITE_TCLAPI test_transfer_bind(
2967 void * clientData,
2968 Tcl_Interp *interp,
2969 int objc,
2970 Tcl_Obj *CONST objv[]
2972 #ifndef SQLITE_OMIT_DEPRECATED
2973 sqlite3_stmt *pStmt1, *pStmt2;
2974 if( objc!=3 ){
2975 Tcl_AppendResult(interp, "wrong # args: should be \"",
2976 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2977 return TCL_ERROR;
2979 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2980 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2981 Tcl_SetObjResult(interp,
2982 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
2983 #endif
2984 return TCL_OK;
2988 ** Usage: sqlite3_changes DB
2990 ** Return the number of changes made to the database by the last SQL
2991 ** execution.
2993 static int SQLITE_TCLAPI test_changes(
2994 void * clientData,
2995 Tcl_Interp *interp,
2996 int objc,
2997 Tcl_Obj *CONST objv[]
2999 sqlite3 *db;
3000 if( objc!=2 ){
3001 Tcl_AppendResult(interp, "wrong # args: should be \"",
3002 Tcl_GetString(objv[0]), " DB", 0);
3003 return TCL_ERROR;
3005 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3006 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
3007 return TCL_OK;
3011 ** This is the "static_bind_value" that variables are bound to when
3012 ** the FLAG option of sqlite3_bind is "static"
3014 static char *sqlite_static_bind_value = 0;
3015 static int sqlite_static_bind_nbyte = 0;
3018 ** Usage: sqlite3_bind VM IDX VALUE FLAGS
3020 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
3021 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
3022 ** ignored and the value is set to NULL. If FLAGS=="static" then
3023 ** the value is set to the value of a static variable named
3024 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
3025 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
3026 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
3028 static int SQLITE_TCLAPI test_bind(
3029 void *NotUsed,
3030 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3031 int argc, /* Number of arguments */
3032 char **argv /* Text of each argument */
3034 sqlite3_stmt *pStmt;
3035 int rc;
3036 int idx;
3037 if( argc!=5 ){
3038 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3039 " VM IDX VALUE (null|static|normal)\"", 0);
3040 return TCL_ERROR;
3042 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
3043 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
3044 if( strcmp(argv[4],"null")==0 ){
3045 rc = sqlite3_bind_null(pStmt, idx);
3046 }else if( strcmp(argv[4],"static")==0 ){
3047 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
3048 }else if( strcmp(argv[4],"static-nbytes")==0 ){
3049 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
3050 sqlite_static_bind_nbyte, 0);
3051 }else if( strcmp(argv[4],"normal")==0 ){
3052 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
3053 }else if( strcmp(argv[4],"blob10")==0 ){
3054 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
3055 }else{
3056 Tcl_AppendResult(interp, "4th argument should be "
3057 "\"null\" or \"static\" or \"normal\"", 0);
3058 return TCL_ERROR;
3060 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3061 if( rc ){
3062 char zBuf[50];
3063 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3064 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
3065 return TCL_ERROR;
3067 return TCL_OK;
3070 #ifndef SQLITE_OMIT_UTF16
3072 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
3074 ** This function is used to test that SQLite selects the correct collation
3075 ** sequence callback when multiple versions (for different text encodings)
3076 ** are available.
3078 ** Calling this routine registers the collation sequence "test_collate"
3079 ** with database handle <db>. The second argument must be a list of three
3080 ** boolean values. If the first is true, then a version of test_collate is
3081 ** registered for UTF-8, if the second is true, a version is registered for
3082 ** UTF-16le, if the third is true, a UTF-16be version is available.
3083 ** Previous versions of test_collate are deleted.
3085 ** The collation sequence test_collate is implemented by calling the
3086 ** following TCL script:
3088 ** "test_collate <enc> <lhs> <rhs>"
3090 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
3091 ** The <enc> parameter is the encoding of the collation function that
3092 ** SQLite selected to call. The TCL test script implements the
3093 ** "test_collate" proc.
3095 ** Note that this will only work with one interpreter at a time, as the
3096 ** interp pointer to use when evaluating the TCL script is stored in
3097 ** pTestCollateInterp.
3099 static Tcl_Interp* pTestCollateInterp;
3100 static int test_collate_func(
3101 void *pCtx,
3102 int nA, const void *zA,
3103 int nB, const void *zB
3105 Tcl_Interp *i = pTestCollateInterp;
3106 int encin = SQLITE_PTR_TO_INT(pCtx);
3107 int res;
3108 int n;
3110 sqlite3_value *pVal;
3111 Tcl_Obj *pX;
3113 pX = Tcl_NewStringObj("test_collate", -1);
3114 Tcl_IncrRefCount(pX);
3116 switch( encin ){
3117 case SQLITE_UTF8:
3118 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
3119 break;
3120 case SQLITE_UTF16LE:
3121 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
3122 break;
3123 case SQLITE_UTF16BE:
3124 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
3125 break;
3126 default:
3127 assert(0);
3130 sqlite3BeginBenignMalloc();
3131 pVal = sqlite3ValueNew(0);
3132 if( pVal ){
3133 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
3134 n = sqlite3_value_bytes(pVal);
3135 Tcl_ListObjAppendElement(i,pX,
3136 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
3137 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
3138 n = sqlite3_value_bytes(pVal);
3139 Tcl_ListObjAppendElement(i,pX,
3140 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
3141 sqlite3ValueFree(pVal);
3143 sqlite3EndBenignMalloc();
3145 Tcl_EvalObjEx(i, pX, 0);
3146 Tcl_DecrRefCount(pX);
3147 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
3148 return res;
3150 static int SQLITE_TCLAPI test_collate(
3151 void * clientData,
3152 Tcl_Interp *interp,
3153 int objc,
3154 Tcl_Obj *CONST objv[]
3156 sqlite3 *db;
3157 int val;
3158 sqlite3_value *pVal;
3159 int rc;
3161 if( objc!=5 ) goto bad_args;
3162 pTestCollateInterp = interp;
3163 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3165 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3166 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
3167 (void *)SQLITE_UTF8, val?test_collate_func:0);
3168 if( rc==SQLITE_OK ){
3169 const void *zUtf16;
3170 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3171 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
3172 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
3173 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3175 #if 0
3176 if( sqlite3_iMallocFail>0 ){
3177 sqlite3_iMallocFail++;
3179 #endif
3180 sqlite3_mutex_enter(db->mutex);
3181 pVal = sqlite3ValueNew(db);
3182 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
3183 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
3184 if( db->mallocFailed ){
3185 rc = SQLITE_NOMEM;
3186 }else{
3187 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
3188 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
3190 sqlite3ValueFree(pVal);
3191 sqlite3_mutex_leave(db->mutex);
3193 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3195 if( rc!=SQLITE_OK ){
3196 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3197 return TCL_ERROR;
3199 return TCL_OK;
3201 bad_args:
3202 Tcl_AppendResult(interp, "wrong # args: should be \"",
3203 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3204 return TCL_ERROR;
3208 ** Usage: add_test_utf16bin_collate <db ptr>
3210 ** Add a utf-16 collation sequence named "utf16bin" to the database
3211 ** handle. This collation sequence compares arguments in the same way as the
3212 ** built-in collation "binary".
3214 static int test_utf16bin_collate_func(
3215 void *pCtx,
3216 int nA, const void *zA,
3217 int nB, const void *zB
3219 int nCmp = (nA>nB ? nB : nA);
3220 int res = memcmp(zA, zB, nCmp);
3221 if( res==0 ) res = nA - nB;
3222 return res;
3224 static int SQLITE_TCLAPI test_utf16bin_collate(
3225 void * clientData,
3226 Tcl_Interp *interp,
3227 int objc,
3228 Tcl_Obj *CONST objv[]
3230 sqlite3 *db;
3231 int rc;
3233 if( objc!=2 ) goto bad_args;
3234 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3236 rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0,
3237 test_utf16bin_collate_func
3239 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3240 return TCL_OK;
3242 bad_args:
3243 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3244 return TCL_ERROR;
3248 ** When the collation needed callback is invoked, record the name of
3249 ** the requested collating function here. The recorded name is linked
3250 ** to a TCL variable and used to make sure that the requested collation
3251 ** name is correct.
3253 static char zNeededCollation[200];
3254 static char *pzNeededCollation = zNeededCollation;
3258 ** Called when a collating sequence is needed. Registered using
3259 ** sqlite3_collation_needed16().
3261 static void test_collate_needed_cb(
3262 void *pCtx,
3263 sqlite3 *db,
3264 int eTextRep,
3265 const void *pName
3267 int enc = ENC(db);
3268 int i;
3269 char *z;
3270 for(z = (char*)pName, i=0; *z || z[1]; z++){
3271 if( *z ) zNeededCollation[i++] = *z;
3273 zNeededCollation[i] = 0;
3274 sqlite3_create_collation(
3275 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
3279 ** Usage: add_test_collate_needed DB
3281 static int SQLITE_TCLAPI test_collate_needed(
3282 void * clientData,
3283 Tcl_Interp *interp,
3284 int objc,
3285 Tcl_Obj *CONST objv[]
3287 sqlite3 *db;
3288 int rc;
3290 if( objc!=2 ) goto bad_args;
3291 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3292 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
3293 zNeededCollation[0] = 0;
3294 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3295 return TCL_OK;
3297 bad_args:
3298 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3299 return TCL_ERROR;
3303 ** tclcmd: add_alignment_test_collations DB
3305 ** Add two new collating sequences to the database DB
3307 ** utf16_aligned
3308 ** utf16_unaligned
3310 ** Both collating sequences use the same sort order as BINARY.
3311 ** The only difference is that the utf16_aligned collating
3312 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
3313 ** Both collating functions increment the unaligned utf16 counter
3314 ** whenever they see a string that begins on an odd byte boundary.
3316 static int unaligned_string_counter = 0;
3317 static int alignmentCollFunc(
3318 void *NotUsed,
3319 int nKey1, const void *pKey1,
3320 int nKey2, const void *pKey2
3322 int rc, n;
3323 n = nKey1<nKey2 ? nKey1 : nKey2;
3324 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
3325 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
3326 rc = memcmp(pKey1, pKey2, n);
3327 if( rc==0 ){
3328 rc = nKey1 - nKey2;
3330 return rc;
3332 static int SQLITE_TCLAPI add_alignment_test_collations(
3333 void * clientData,
3334 Tcl_Interp *interp,
3335 int objc,
3336 Tcl_Obj *CONST objv[]
3338 sqlite3 *db;
3339 if( objc>=2 ){
3340 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3341 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
3342 0, alignmentCollFunc);
3343 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
3344 0, alignmentCollFunc);
3346 return SQLITE_OK;
3348 #endif /* !defined(SQLITE_OMIT_UTF16) */
3351 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
3353 ** This function is used to test that SQLite selects the correct user
3354 ** function callback when multiple versions (for different text encodings)
3355 ** are available.
3357 ** Calling this routine registers up to three versions of the user function
3358 ** "test_function" with database handle <db>. If the second argument is
3359 ** true, then a version of test_function is registered for UTF-8, if the
3360 ** third is true, a version is registered for UTF-16le, if the fourth is
3361 ** true, a UTF-16be version is available. Previous versions of
3362 ** test_function are deleted.
3364 ** The user function is implemented by calling the following TCL script:
3366 ** "test_function <enc> <arg>"
3368 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
3369 ** single argument passed to the SQL function. The value returned by
3370 ** the TCL script is used as the return value of the SQL function. It
3371 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
3372 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
3373 ** prefers UTF-16BE.
3375 #ifndef SQLITE_OMIT_UTF16
3376 static void test_function_utf8(
3377 sqlite3_context *pCtx,
3378 int nArg,
3379 sqlite3_value **argv
3381 Tcl_Interp *interp;
3382 Tcl_Obj *pX;
3383 sqlite3_value *pVal;
3384 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3385 pX = Tcl_NewStringObj("test_function", -1);
3386 Tcl_IncrRefCount(pX);
3387 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
3388 Tcl_ListObjAppendElement(interp, pX,
3389 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3390 Tcl_EvalObjEx(interp, pX, 0);
3391 Tcl_DecrRefCount(pX);
3392 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
3393 pVal = sqlite3ValueNew(0);
3394 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3395 SQLITE_UTF8, SQLITE_STATIC);
3396 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
3397 -1, SQLITE_TRANSIENT);
3398 sqlite3ValueFree(pVal);
3400 static void test_function_utf16le(
3401 sqlite3_context *pCtx,
3402 int nArg,
3403 sqlite3_value **argv
3405 Tcl_Interp *interp;
3406 Tcl_Obj *pX;
3407 sqlite3_value *pVal;
3408 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3409 pX = Tcl_NewStringObj("test_function", -1);
3410 Tcl_IncrRefCount(pX);
3411 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
3412 Tcl_ListObjAppendElement(interp, pX,
3413 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3414 Tcl_EvalObjEx(interp, pX, 0);
3415 Tcl_DecrRefCount(pX);
3416 pVal = sqlite3ValueNew(0);
3417 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3418 SQLITE_UTF8, SQLITE_STATIC);
3419 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
3420 sqlite3ValueFree(pVal);
3422 static void test_function_utf16be(
3423 sqlite3_context *pCtx,
3424 int nArg,
3425 sqlite3_value **argv
3427 Tcl_Interp *interp;
3428 Tcl_Obj *pX;
3429 sqlite3_value *pVal;
3430 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3431 pX = Tcl_NewStringObj("test_function", -1);
3432 Tcl_IncrRefCount(pX);
3433 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
3434 Tcl_ListObjAppendElement(interp, pX,
3435 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3436 Tcl_EvalObjEx(interp, pX, 0);
3437 Tcl_DecrRefCount(pX);
3438 pVal = sqlite3ValueNew(0);
3439 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3440 SQLITE_UTF8, SQLITE_STATIC);
3441 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
3442 -1, SQLITE_TRANSIENT);
3443 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
3444 -1, SQLITE_TRANSIENT);
3445 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
3446 -1, SQLITE_TRANSIENT);
3447 sqlite3ValueFree(pVal);
3449 #endif /* SQLITE_OMIT_UTF16 */
3450 static int SQLITE_TCLAPI test_function(
3451 void * clientData,
3452 Tcl_Interp *interp,
3453 int objc,
3454 Tcl_Obj *CONST objv[]
3456 #ifndef SQLITE_OMIT_UTF16
3457 sqlite3 *db;
3458 int val;
3460 if( objc!=5 ) goto bad_args;
3461 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3463 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3464 if( val ){
3465 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
3466 interp, test_function_utf8, 0, 0);
3468 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3469 if( val ){
3470 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
3471 interp, test_function_utf16le, 0, 0);
3473 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3474 if( val ){
3475 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
3476 interp, test_function_utf16be, 0, 0);
3479 return TCL_OK;
3480 bad_args:
3481 Tcl_AppendResult(interp, "wrong # args: should be \"",
3482 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3483 #endif /* SQLITE_OMIT_UTF16 */
3484 return TCL_ERROR;
3488 ** Usage: sqlite3_test_errstr <err code>
3490 ** Test that the english language string equivalents for sqlite error codes
3491 ** are sane. The parameter is an integer representing an sqlite error code.
3492 ** The result is a list of two elements, the string representation of the
3493 ** error code and the english language explanation.
3495 static int SQLITE_TCLAPI test_errstr(
3496 void * clientData,
3497 Tcl_Interp *interp,
3498 int objc,
3499 Tcl_Obj *CONST objv[]
3501 char *zCode;
3502 int i;
3503 if( objc!=1 ){
3504 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
3507 zCode = Tcl_GetString(objv[1]);
3508 for(i=0; i<200; i++){
3509 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
3511 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
3512 return TCL_OK;
3516 ** Usage: breakpoint
3518 ** This routine exists for one purpose - to provide a place to put a
3519 ** breakpoint with GDB that can be triggered using TCL code. The use
3520 ** for this is when a particular test fails on (say) the 1485th iteration.
3521 ** In the TCL test script, we can add code like this:
3523 ** if {$i==1485} breakpoint
3525 ** Then run testfixture in the debugger and wait for the breakpoint to
3526 ** fire. Then additional breakpoints can be set to trace down the bug.
3528 static int SQLITE_TCLAPI test_breakpoint(
3529 void *NotUsed,
3530 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3531 int argc, /* Number of arguments */
3532 char **argv /* Text of each argument */
3534 return TCL_OK; /* Do nothing */
3538 ** Usage: sqlite3_bind_zeroblob STMT IDX N
3540 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
3541 ** IDX is the index of a wildcard in the prepared statement. This command
3542 ** binds a N-byte zero-filled BLOB to the wildcard.
3544 static int SQLITE_TCLAPI test_bind_zeroblob(
3545 void * clientData,
3546 Tcl_Interp *interp,
3547 int objc,
3548 Tcl_Obj *CONST objv[]
3550 sqlite3_stmt *pStmt;
3551 int idx;
3552 int n;
3553 int rc;
3555 if( objc!=4 ){
3556 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3557 return TCL_ERROR;
3560 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3561 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3562 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3564 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
3565 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3566 if( rc!=SQLITE_OK ){
3567 return TCL_ERROR;
3570 return TCL_OK;
3574 ** Usage: sqlite3_bind_zeroblob64 STMT IDX N
3576 ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement.
3577 ** IDX is the index of a wildcard in the prepared statement. This command
3578 ** binds a N-byte zero-filled BLOB to the wildcard.
3580 static int SQLITE_TCLAPI test_bind_zeroblob64(
3581 void * clientData,
3582 Tcl_Interp *interp,
3583 int objc,
3584 Tcl_Obj *CONST objv[]
3586 sqlite3_stmt *pStmt;
3587 int idx;
3588 Tcl_WideInt n;
3589 int rc;
3591 if( objc!=4 ){
3592 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3593 return TCL_ERROR;
3596 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3597 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3598 if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3600 rc = sqlite3_bind_zeroblob64(pStmt, idx, n);
3601 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3602 if( rc!=SQLITE_OK ){
3603 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3604 return TCL_ERROR;
3607 return TCL_OK;
3611 ** Usage: sqlite3_bind_int STMT N VALUE
3613 ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
3614 ** N is the index of a wildcard in the prepared statement. This command
3615 ** binds a 32-bit integer VALUE to that wildcard.
3617 static int SQLITE_TCLAPI test_bind_int(
3618 void * clientData,
3619 Tcl_Interp *interp,
3620 int objc,
3621 Tcl_Obj *CONST objv[]
3623 sqlite3_stmt *pStmt;
3624 int idx;
3625 int value;
3626 int rc;
3628 if( objc!=4 ){
3629 Tcl_AppendResult(interp, "wrong # args: should be \"",
3630 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3631 return TCL_ERROR;
3634 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3635 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3636 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3638 rc = sqlite3_bind_int(pStmt, idx, value);
3639 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3640 if( rc!=SQLITE_OK ){
3641 return TCL_ERROR;
3644 return TCL_OK;
3649 ** Usage: intarray_addr INT ...
3651 ** Return the address of a C-language array of 32-bit integers.
3653 ** Space to hold the array is obtained from malloc(). Call this procedure once
3654 ** with no arguments in order to release memory. Each call to this procedure
3655 ** overwrites the previous array.
3657 static int SQLITE_TCLAPI test_intarray_addr(
3658 void * clientData,
3659 Tcl_Interp *interp,
3660 int objc,
3661 Tcl_Obj *CONST objv[]
3663 int i;
3664 static int *p = 0;
3666 sqlite3_free(p);
3667 p = 0;
3668 if( objc>1 ){
3669 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3670 if( p==0 ) return TCL_ERROR;
3671 for(i=0; i<objc-1; i++){
3672 if( Tcl_GetIntFromObj(interp, objv[1+i], &p[i]) ){
3673 sqlite3_free(p);
3674 p = 0;
3675 return TCL_ERROR;
3679 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3680 return TCL_OK;
3683 ** Usage: intarray_addr INT ...
3685 ** Return the address of a C-language array of 32-bit integers.
3687 ** Space to hold the array is obtained from malloc(). Call this procedure once
3688 ** with no arguments in order to release memory. Each call to this procedure
3689 ** overwrites the previous array.
3691 static int SQLITE_TCLAPI test_int64array_addr(
3692 void * clientData,
3693 Tcl_Interp *interp,
3694 int objc,
3695 Tcl_Obj *CONST objv[]
3697 int i;
3698 static sqlite3_int64 *p = 0;
3700 sqlite3_free(p);
3701 p = 0;
3702 if( objc>1 ){
3703 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3704 if( p==0 ) return TCL_ERROR;
3705 for(i=0; i<objc-1; i++){
3706 Tcl_WideInt v;
3707 if( Tcl_GetWideIntFromObj(interp, objv[1+i], &v) ){
3708 sqlite3_free(p);
3709 p = 0;
3710 return TCL_ERROR;
3712 p[i] = v;
3715 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3716 return TCL_OK;
3719 ** Usage: doublearray_addr INT ...
3721 ** Return the address of a C-language array of doubles.
3723 ** Space to hold the array is obtained from malloc(). Call this procedure once
3724 ** with no arguments in order to release memory. Each call to this procedure
3725 ** overwrites the previous array.
3727 static int SQLITE_TCLAPI test_doublearray_addr(
3728 void * clientData,
3729 Tcl_Interp *interp,
3730 int objc,
3731 Tcl_Obj *CONST objv[]
3733 int i;
3734 static double *p = 0;
3736 sqlite3_free(p);
3737 p = 0;
3738 if( objc>1 ){
3739 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3740 if( p==0 ) return TCL_ERROR;
3741 for(i=0; i<objc-1; i++){
3742 if( Tcl_GetDoubleFromObj(interp, objv[1+i], &p[i]) ){
3743 sqlite3_free(p);
3744 p = 0;
3745 return TCL_ERROR;
3749 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3750 return TCL_OK;
3753 ** Usage: textarray_addr TEXT ...
3755 ** Return the address of a C-language array of strings.
3757 ** Space to hold the array is obtained from malloc(). Call this procedure once
3758 ** with no arguments in order to release memory. Each call to this procedure
3759 ** overwrites the previous array.
3761 static int SQLITE_TCLAPI test_textarray_addr(
3762 void * clientData,
3763 Tcl_Interp *interp,
3764 int objc,
3765 Tcl_Obj *CONST objv[]
3767 int i;
3768 static int n = 0;
3769 static char **p = 0;
3771 for(i=0; i<n; i++) sqlite3_free(p[i]);
3772 sqlite3_free(p);
3773 p = 0;
3774 if( objc>1 ){
3775 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3776 if( p==0 ) return TCL_ERROR;
3777 for(i=0; i<objc-1; i++){
3778 p[i] = sqlite3_mprintf("%s", Tcl_GetString(objv[1+i]));
3781 n = objc-1;
3782 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((sqlite3_int64)p));
3783 return TCL_OK;
3788 ** Usage: sqlite3_bind_int64 STMT N VALUE
3790 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
3791 ** N is the index of a wildcard in the prepared statement. This command
3792 ** binds a 64-bit integer VALUE to that wildcard.
3794 static int SQLITE_TCLAPI test_bind_int64(
3795 void * clientData,
3796 Tcl_Interp *interp,
3797 int objc,
3798 Tcl_Obj *CONST objv[]
3800 sqlite3_stmt *pStmt;
3801 int idx;
3802 Tcl_WideInt value;
3803 int rc;
3805 if( objc!=4 ){
3806 Tcl_AppendResult(interp, "wrong # args: should be \"",
3807 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3808 return TCL_ERROR;
3811 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3812 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3813 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3815 rc = sqlite3_bind_int64(pStmt, idx, value);
3816 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3817 if( rc!=SQLITE_OK ){
3818 return TCL_ERROR;
3821 return TCL_OK;
3826 ** Usage: sqlite3_bind_double STMT N VALUE
3828 ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
3829 ** N is the index of a wildcard in the prepared statement. This command
3830 ** binds a 64-bit integer VALUE to that wildcard.
3832 static int SQLITE_TCLAPI test_bind_double(
3833 void * clientData,
3834 Tcl_Interp *interp,
3835 int objc,
3836 Tcl_Obj *CONST objv[]
3838 sqlite3_stmt *pStmt;
3839 int idx;
3840 double value = 0;
3841 int rc;
3842 const char *zVal;
3843 int i;
3844 static const struct {
3845 const char *zName; /* Name of the special floating point value */
3846 unsigned int iUpper; /* Upper 32 bits */
3847 unsigned int iLower; /* Lower 32 bits */
3848 } aSpecialFp[] = {
3849 { "NaN", 0x7fffffff, 0xffffffff },
3850 { "SNaN", 0x7ff7ffff, 0xffffffff },
3851 { "-NaN", 0xffffffff, 0xffffffff },
3852 { "-SNaN", 0xfff7ffff, 0xffffffff },
3853 { "+Inf", 0x7ff00000, 0x00000000 },
3854 { "-Inf", 0xfff00000, 0x00000000 },
3855 { "Epsilon", 0x00000000, 0x00000001 },
3856 { "-Epsilon", 0x80000000, 0x00000001 },
3857 { "NaN0", 0x7ff80000, 0x00000000 },
3858 { "-NaN0", 0xfff80000, 0x00000000 },
3861 if( objc!=4 ){
3862 Tcl_AppendResult(interp, "wrong # args: should be \"",
3863 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3864 return TCL_ERROR;
3867 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3868 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3870 /* Intercept the string "NaN" and generate a NaN value for it.
3871 ** All other strings are passed through to Tcl_GetDoubleFromObj().
3872 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
3873 ** contain a bug.
3875 zVal = Tcl_GetString(objv[3]);
3876 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
3877 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
3878 sqlite3_uint64 x;
3879 x = aSpecialFp[i].iUpper;
3880 x <<= 32;
3881 x |= aSpecialFp[i].iLower;
3882 assert( sizeof(value)==8 );
3883 assert( sizeof(x)==8 );
3884 memcpy(&value, &x, 8);
3885 break;
3888 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
3889 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
3890 return TCL_ERROR;
3892 rc = sqlite3_bind_double(pStmt, idx, value);
3893 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3894 if( rc!=SQLITE_OK ){
3895 return TCL_ERROR;
3898 return TCL_OK;
3902 ** Usage: sqlite3_bind_null STMT N
3904 ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
3905 ** N is the index of a wildcard in the prepared statement. This command
3906 ** binds a NULL to the wildcard.
3908 static int SQLITE_TCLAPI test_bind_null(
3909 void * clientData,
3910 Tcl_Interp *interp,
3911 int objc,
3912 Tcl_Obj *CONST objv[]
3914 sqlite3_stmt *pStmt;
3915 int idx;
3916 int rc;
3918 if( objc!=3 ){
3919 Tcl_AppendResult(interp, "wrong # args: should be \"",
3920 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
3921 return TCL_ERROR;
3924 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3925 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3927 rc = sqlite3_bind_null(pStmt, idx);
3928 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3929 if( rc!=SQLITE_OK ){
3930 return TCL_ERROR;
3933 return TCL_OK;
3937 ** Usage: sqlite3_bind_text STMT N STRING BYTES
3939 ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
3940 ** N is the index of a wildcard in the prepared statement. This command
3941 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
3942 ** long.
3944 static int SQLITE_TCLAPI test_bind_text(
3945 void * clientData,
3946 Tcl_Interp *interp,
3947 int objc,
3948 Tcl_Obj *CONST objv[]
3950 sqlite3_stmt *pStmt;
3951 int idx;
3952 int trueLength = 0;
3953 int bytes;
3954 char *value;
3955 int rc;
3956 char *toFree = 0;
3958 if( objc!=5 ){
3959 Tcl_AppendResult(interp, "wrong # args: should be \"",
3960 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3961 return TCL_ERROR;
3964 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3965 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3966 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &trueLength);
3967 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3968 if( bytes<0 ){
3969 toFree = malloc( trueLength + 1 );
3970 if( toFree==0 ){
3971 Tcl_AppendResult(interp, "out of memory", (void*)0);
3972 return TCL_ERROR;
3974 memcpy(toFree, value, trueLength);
3975 toFree[trueLength] = 0;
3976 value = toFree;
3978 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
3979 free(toFree);
3980 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3981 if( rc!=SQLITE_OK ){
3982 Tcl_AppendResult(interp, sqlite3ErrName(rc), (void*)0);
3983 return TCL_ERROR;
3986 return TCL_OK;
3990 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
3992 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
3993 ** N is the index of a wildcard in the prepared statement. This command
3994 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
3995 ** long.
3997 static int SQLITE_TCLAPI test_bind_text16(
3998 void * clientData,
3999 Tcl_Interp *interp,
4000 int objc,
4001 Tcl_Obj *CONST objv[]
4003 #ifndef SQLITE_OMIT_UTF16
4004 sqlite3_stmt *pStmt;
4005 int idx;
4006 int bytes;
4007 char *value;
4008 char *toFree = 0;
4009 int rc;
4010 int trueLength = 0;
4012 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
4013 Tcl_Obj *oStmt = objv[objc-4];
4014 Tcl_Obj *oN = objv[objc-3];
4015 Tcl_Obj *oString = objv[objc-2];
4016 Tcl_Obj *oBytes = objv[objc-1];
4018 if( objc!=5 && objc!=6){
4019 Tcl_AppendResult(interp, "wrong # args: should be \"",
4020 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
4021 return TCL_ERROR;
4024 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
4025 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
4026 value = (char*)Tcl_GetByteArrayFromObj(oString, &trueLength);
4027 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
4028 if( bytes<0 && xDel==SQLITE_TRANSIENT ){
4029 toFree = malloc( trueLength + 3 );
4030 if( toFree==0 ){
4031 Tcl_AppendResult(interp, "out of memory", (void*)0);
4032 return TCL_ERROR;
4034 memcpy(toFree, value, trueLength);
4035 memset(toFree+trueLength, 0, 3);
4036 value = toFree;
4038 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
4039 free(toFree);
4040 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4041 if( rc!=SQLITE_OK ){
4042 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
4043 return TCL_ERROR;
4046 #endif /* SQLITE_OMIT_UTF16 */
4047 return TCL_OK;
4051 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
4053 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
4054 ** N is the index of a wildcard in the prepared statement. This command
4055 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
4057 static int SQLITE_TCLAPI test_bind_blob(
4058 void * clientData,
4059 Tcl_Interp *interp,
4060 int objc,
4061 Tcl_Obj *CONST objv[]
4063 sqlite3_stmt *pStmt;
4064 int len, idx;
4065 int bytes;
4066 char *value;
4067 int rc;
4068 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
4070 if( objc!=5 && objc!=6 ){
4071 Tcl_AppendResult(interp, "wrong # args: should be \"",
4072 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
4073 return TCL_ERROR;
4076 if( objc==6 ){
4077 xDestructor = SQLITE_STATIC;
4078 objv++;
4081 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4082 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4084 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &len);
4085 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
4087 if( bytes>len ){
4088 char zBuf[200];
4089 sqlite3_snprintf(sizeof(zBuf), zBuf,
4090 "cannot use %d blob bytes, have %d", bytes, len);
4091 Tcl_AppendResult(interp, zBuf, (char*)0);
4092 return TCL_ERROR;
4095 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
4096 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4097 if( rc!=SQLITE_OK ){
4098 return TCL_ERROR;
4101 return TCL_OK;
4105 ** Usage: sqlite3_bind_value_from_preupdate STMT N NEW|OLD IDX
4107 ** Test the sqlite3_bind_value interface using sqlite3_value objects
4108 ** obtained from either sqlite3_preupdate_new() (if arg[3]=="new") or
4109 ** sqlite3_preupdate_old() if (arg[3]=="old"). IDX is the index to
4110 ** pass to the sqlite3_preupdate_xxx() function.
4112 static int SQLITE_TCLAPI test_bind_value_from_preupdate(
4113 void * clientData,
4114 Tcl_Interp *interp,
4115 int objc,
4116 Tcl_Obj *CONST objv[]
4118 sqlite3_stmt *pStmt;
4119 int idx;
4120 int bidx;
4121 const char *z3 = 0;
4122 sqlite3 *db = 0;
4123 sqlite3_value *pVal = 0;
4125 if( objc!=5 ){
4126 Tcl_WrongNumArgs(interp, 1, objv, "STMT N NEW|OLD IDX");
4127 return TCL_ERROR;
4130 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4131 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4132 z3 = Tcl_GetString(objv[3]);
4133 if( Tcl_GetIntFromObj(interp, objv[4], &bidx) ) return TCL_ERROR;
4134 db = sqlite3_db_handle(pStmt);
4136 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4137 if( z3[0]=='n' ){
4138 sqlite3_preupdate_new(db, bidx, &pVal);
4139 }else if( z3[0]=='o' ){
4140 sqlite3_preupdate_old(db, bidx, &pVal);
4141 }else{
4142 Tcl_AppendResult(interp, "expected new or old, got: ", z3, (char*)0);
4143 return TCL_ERROR;
4145 sqlite3_bind_value(pStmt, idx, pVal);
4146 #endif
4148 return TCL_OK;
4152 ** Usage: sqlite3_bind_value_from_select STMT N SELECT
4154 ** Test the sqlite3_bind_value interface. STMT is a prepared statement.
4155 ** N is the index of a wildcard in the prepared statement.
4157 static int SQLITE_TCLAPI test_bind_value_from_select(
4158 void * clientData,
4159 Tcl_Interp *interp,
4160 int objc,
4161 Tcl_Obj *CONST objv[]
4163 sqlite3_stmt *pStmt;
4164 sqlite3_stmt *pStmt2;
4165 int idx;
4166 const char *zSql = 0;
4167 sqlite3 *db = 0;
4168 int rc = SQLITE_OK;
4170 if( objc!=4 ){
4171 Tcl_WrongNumArgs(interp, 1, objv, "STMT N SELECT");
4172 return TCL_ERROR;
4175 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4176 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4177 zSql = Tcl_GetString(objv[3]);
4178 db = sqlite3_db_handle(pStmt);
4180 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt2, 0);
4181 if( rc!=SQLITE_OK ){
4182 Tcl_AppendResult(interp, "error in SQL: ", sqlite3_errmsg(db), (char*)0);
4183 return TCL_ERROR;
4185 if( sqlite3_step(pStmt2)==SQLITE_ROW ){
4186 sqlite3_value *pVal = sqlite3_column_value(pStmt2, 0);
4187 sqlite3_bind_value(pStmt, idx, pVal);
4189 rc = sqlite3_finalize(pStmt2);
4190 if( rc!=SQLITE_OK ){
4191 Tcl_AppendResult(interp,
4192 "error runnning SQL: ", sqlite3_errmsg(db), (char*)0
4194 return TCL_ERROR;
4197 return TCL_OK;
4200 #ifdef _WIN32
4201 struct iovec {
4202 void *iov_base;
4203 size_t iov_len;
4205 #else
4206 # include <sys/uio.h>
4207 #endif
4209 #ifndef SQLITE_OMIT_VIRTUALTABLE
4211 ** sqlite3_carray_bind [options...] STMT NAME VALUE ...
4213 ** Options:
4214 ** -transient
4215 ** -static
4216 ** -int32
4217 ** -int64
4218 ** -double
4219 ** -text
4220 ** -blob
4222 ** Each call clears static data. Called with no options does nothing
4223 ** but clear static data.
4225 static int SQLITE_TCLAPI test_carray_bind(
4226 void * clientData,
4227 Tcl_Interp *interp,
4228 int objc,
4229 Tcl_Obj *CONST objv[]
4231 sqlite3_stmt *pStmt;
4232 int eType = 0; /* CARRAY_INT32 */
4233 int nData = 0;
4234 void *aData = 0;
4235 int isTransient = 0;
4236 int isStatic = 0;
4237 int idx;
4238 int i, j;
4239 int rc;
4240 void (*xDel)(void*) = sqlite3_free;
4241 static void *aStaticData = 0;
4242 static int nStaticData = 0;
4243 static int eStaticType = 0;
4244 extern int sqlite3_carray_bind(
4245 sqlite3_stmt *pStmt,
4246 int i,
4247 void *aData,
4248 int nData,
4249 int mFlags,
4250 void (*xDestroy)(void*)
4253 if( aStaticData ){
4254 /* Always clear preexisting static data on every call */
4255 if( eStaticType==3 ){
4256 for(i=0; i<nStaticData; i++){
4257 sqlite3_free(((char**)aStaticData)[i]);
4260 if( eStaticType==4 ){
4261 for(i=0; i<nStaticData; i++){
4262 sqlite3_free(((struct iovec*)aStaticData)[i].iov_base);
4265 sqlite3_free(aStaticData);
4266 aStaticData = 0;
4267 nStaticData = 0;
4268 eStaticType = 0;
4270 if( objc==1 ) return TCL_OK;
4272 for(i=1; i<objc && Tcl_GetString(objv[i])[0]=='-'; i++){
4273 const char *z = Tcl_GetString(objv[i]);
4274 if( strcmp(z, "-transient")==0 ){
4275 isTransient = 1;
4276 xDel = SQLITE_TRANSIENT;
4277 }else
4278 if( strcmp(z, "-static")==0 ){
4279 isStatic = 1;
4280 xDel = SQLITE_STATIC;
4281 }else
4282 if( strcmp(z, "-int32")==0 ){
4283 eType = 0; /* CARRAY_INT32 */
4284 }else
4285 if( strcmp(z, "-int64")==0 ){
4286 eType = 1; /* CARRAY_INT64 */
4287 }else
4288 if( strcmp(z, "-double")==0 ){
4289 eType = 2; /* CARRAY_DOUBLE */
4290 }else
4291 if( strcmp(z, "-text")==0 ){
4292 eType = 3; /* CARRAY_TEXT */
4293 }else
4294 if( strcmp(z, "-blob")==0 ){
4295 eType = 4; /* CARRAY_BLOB */
4296 }else
4297 if( strcmp(z, "--")==0 ){
4298 break;
4299 }else
4301 Tcl_AppendResult(interp, "unknown option: ", z, (char*)0);
4302 return TCL_ERROR;
4305 if( eType==3 && !isStatic && !isTransient ){
4306 Tcl_AppendResult(interp, "text data must be either -static or -transient",
4307 (char*)0);
4308 return TCL_ERROR;
4310 if( eType==4 && !isStatic && !isTransient ){
4311 Tcl_AppendResult(interp, "blob data must be either -static or -transient",
4312 (char*)0);
4313 return TCL_ERROR;
4315 if( isStatic && isTransient ){
4316 Tcl_AppendResult(interp, "cannot be both -static and -transient",
4317 (char*)0);
4318 return TCL_ERROR;
4320 if( objc-i < 2 ){
4321 Tcl_WrongNumArgs(interp, 1, objv, "[OPTIONS] STMT IDX VALUE ...");
4322 return TCL_ERROR;
4324 if( getStmtPointer(interp, Tcl_GetString(objv[i]), &pStmt) ) return TCL_ERROR;
4325 i++;
4326 if( Tcl_GetIntFromObj(interp, objv[i], &idx) ) return TCL_ERROR;
4327 i++;
4328 nData = objc - i;
4329 switch( eType + 5*(nData<=0) ){
4330 case 0: { /* INT32 */
4331 int *a = sqlite3_malloc( sizeof(int)*nData );
4332 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4333 for(j=0; j<nData; j++){
4334 int v;
4335 if( Tcl_GetIntFromObj(interp, objv[i+j], &v) ){
4336 sqlite3_free(a);
4337 return TCL_ERROR;
4339 a[j] = v;
4341 aData = a;
4342 break;
4344 case 1: { /* INT64 */
4345 sqlite3_int64 *a = sqlite3_malloc( sizeof(sqlite3_int64)*nData );
4346 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4347 for(j=0; j<nData; j++){
4348 Tcl_WideInt v;
4349 if( Tcl_GetWideIntFromObj(interp, objv[i+j], &v) ){
4350 sqlite3_free(a);
4351 return TCL_ERROR;
4353 a[j] = v;
4355 aData = a;
4356 break;
4358 case 2: { /* DOUBLE */
4359 double *a = sqlite3_malloc( sizeof(double)*nData );
4360 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4361 for(j=0; j<nData; j++){
4362 double v;
4363 if( Tcl_GetDoubleFromObj(interp, objv[i+j], &v) ){
4364 sqlite3_free(a);
4365 return TCL_ERROR;
4367 a[j] = v;
4369 aData = a;
4370 break;
4372 case 3: { /* TEXT */
4373 char **a = sqlite3_malloc( sizeof(char*)*nData );
4374 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4375 for(j=0; j<nData; j++){
4376 const char *v = Tcl_GetString(objv[i+j]);
4377 a[j] = sqlite3_mprintf("%s", v);
4379 aData = a;
4380 break;
4382 case 4: { /* BLOB */
4383 struct iovec *a = sqlite3_malloc( sizeof(struct iovec)*nData );
4384 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4385 for(j=0; j<nData; j++){
4386 int n = 0;
4387 unsigned char *v = Tcl_GetByteArrayFromObj(objv[i+i], &n);
4388 a[j].iov_len = n;
4389 a[j].iov_base = sqlite3_malloc64( n );
4390 if( a[j].iov_base==0 ){
4391 a[j].iov_len = 0;
4392 }else{
4393 memcpy(a[j].iov_base, v, n);
4396 aData = a;
4397 break;
4399 case 5: { /* nData==0 */
4400 aData = "";
4401 xDel = SQLITE_STATIC;
4402 isTransient = 0;
4403 isStatic = 0;
4404 break;
4407 if( isStatic ){
4408 aStaticData = aData;
4409 nStaticData = nData;
4410 eStaticType = eType;
4412 rc = sqlite3_carray_bind(pStmt, idx, aData, nData, eType, xDel);
4413 if( isTransient ){
4414 if( eType==3 ){
4415 for(i=0; i<nData; i++) sqlite3_free(((char**)aData)[i]);
4417 if( eType==4 ){
4418 for(i=0; i<nData; i++) sqlite3_free(((struct iovec*)aData)[i].iov_base);
4420 sqlite3_free(aData);
4422 carray_bind_done:
4423 if( rc ){
4424 Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0);
4425 return TCL_ERROR;
4427 return TCL_OK;
4429 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4432 ** Usage: sqlite3_bind_parameter_count STMT
4434 ** Return the number of wildcards in the given statement.
4436 static int SQLITE_TCLAPI test_bind_parameter_count(
4437 void * clientData,
4438 Tcl_Interp *interp,
4439 int objc,
4440 Tcl_Obj *CONST objv[]
4442 sqlite3_stmt *pStmt;
4444 if( objc!=2 ){
4445 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4446 return TCL_ERROR;
4448 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4449 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
4450 return TCL_OK;
4454 ** Usage: sqlite3_bind_parameter_name STMT N
4456 ** Return the name of the Nth wildcard. The first wildcard is 1.
4457 ** An empty string is returned if N is out of range or if the wildcard
4458 ** is nameless.
4460 static int SQLITE_TCLAPI test_bind_parameter_name(
4461 void * clientData,
4462 Tcl_Interp *interp,
4463 int objc,
4464 Tcl_Obj *CONST objv[]
4466 sqlite3_stmt *pStmt;
4467 int i;
4469 if( objc!=3 ){
4470 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
4471 return TCL_ERROR;
4473 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4474 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
4475 Tcl_SetObjResult(interp,
4476 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
4478 return TCL_OK;
4482 ** Usage: sqlite3_bind_parameter_index STMT NAME
4484 ** Return the index of the wildcard called NAME. Return 0 if there is
4485 ** no such wildcard.
4487 static int SQLITE_TCLAPI test_bind_parameter_index(
4488 void * clientData,
4489 Tcl_Interp *interp,
4490 int objc,
4491 Tcl_Obj *CONST objv[]
4493 sqlite3_stmt *pStmt;
4495 if( objc!=3 ){
4496 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
4497 return TCL_ERROR;
4499 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4500 Tcl_SetObjResult(interp,
4501 Tcl_NewIntObj(
4502 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
4505 return TCL_OK;
4509 ** Usage: sqlite3_clear_bindings STMT
4512 static int SQLITE_TCLAPI test_clear_bindings(
4513 void * clientData,
4514 Tcl_Interp *interp,
4515 int objc,
4516 Tcl_Obj *CONST objv[]
4518 sqlite3_stmt *pStmt;
4520 if( objc!=2 ){
4521 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4522 return TCL_ERROR;
4524 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4525 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
4526 return TCL_OK;
4530 ** Usage: sqlite3_sleep MILLISECONDS
4532 static int SQLITE_TCLAPI test_sleep(
4533 void * clientData,
4534 Tcl_Interp *interp,
4535 int objc,
4536 Tcl_Obj *CONST objv[]
4538 int ms;
4540 if( objc!=2 ){
4541 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
4542 return TCL_ERROR;
4544 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
4545 return TCL_ERROR;
4547 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
4548 return TCL_OK;
4552 ** Usage: sqlite3_extended_errcode DB
4554 ** Return the string representation of the most recent sqlite3_* API
4555 ** error code. e.g. "SQLITE_ERROR".
4557 static int SQLITE_TCLAPI test_ex_errcode(
4558 void * clientData,
4559 Tcl_Interp *interp,
4560 int objc,
4561 Tcl_Obj *CONST objv[]
4563 sqlite3 *db;
4564 int rc;
4566 if( objc!=2 ){
4567 Tcl_AppendResult(interp, "wrong # args: should be \"",
4568 Tcl_GetString(objv[0]), " DB", 0);
4569 return TCL_ERROR;
4571 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4572 rc = sqlite3_extended_errcode(db);
4573 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4574 return TCL_OK;
4579 ** Usage: sqlite3_errcode DB
4581 ** Return the string representation of the most recent sqlite3_* API
4582 ** error code. e.g. "SQLITE_ERROR".
4584 static int SQLITE_TCLAPI test_errcode(
4585 void * clientData,
4586 Tcl_Interp *interp,
4587 int objc,
4588 Tcl_Obj *CONST objv[]
4590 sqlite3 *db;
4591 int rc;
4593 if( objc!=2 ){
4594 Tcl_AppendResult(interp, "wrong # args: should be \"",
4595 Tcl_GetString(objv[0]), " DB", 0);
4596 return TCL_ERROR;
4598 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4599 rc = sqlite3_errcode(db);
4600 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4601 return TCL_OK;
4605 ** Usage: sqlite3_errmsg DB
4607 ** Returns the UTF-8 representation of the error message string for the
4608 ** most recent sqlite3_* API call.
4610 static int SQLITE_TCLAPI test_errmsg(
4611 void * clientData,
4612 Tcl_Interp *interp,
4613 int objc,
4614 Tcl_Obj *CONST objv[]
4616 sqlite3 *db;
4617 const char *zErr;
4619 if( objc!=2 ){
4620 Tcl_AppendResult(interp, "wrong # args: should be \"",
4621 Tcl_GetString(objv[0]), " DB", 0);
4622 return TCL_ERROR;
4624 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4626 zErr = sqlite3_errmsg(db);
4627 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
4628 return TCL_OK;
4633 ** Usage: sqlite3_error_offset DB
4635 ** Return the byte offset into the input UTF8 SQL for the most recent
4636 ** error, or -1 of the error does not refer to a specific token.
4638 static int SQLITE_TCLAPI test_error_offset(
4639 void * clientData,
4640 Tcl_Interp *interp,
4641 int objc,
4642 Tcl_Obj *CONST objv[]
4644 sqlite3 *db;
4645 int iByteOffset;
4647 if( objc!=2 ){
4648 Tcl_AppendResult(interp, "wrong # args: should be \"",
4649 Tcl_GetString(objv[0]), " DB", 0);
4650 return TCL_ERROR;
4652 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4654 iByteOffset = sqlite3_error_offset(db);
4655 Tcl_SetObjResult(interp, Tcl_NewIntObj(iByteOffset));
4656 return TCL_OK;
4660 ** Usage: test_errmsg16 DB
4662 ** Returns the UTF-16 representation of the error message string for the
4663 ** most recent sqlite3_* API call. This is a byte array object at the TCL
4664 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
4665 ** UTF-16 string.
4667 static int SQLITE_TCLAPI test_errmsg16(
4668 void * clientData,
4669 Tcl_Interp *interp,
4670 int objc,
4671 Tcl_Obj *CONST objv[]
4673 #ifndef SQLITE_OMIT_UTF16
4674 sqlite3 *db;
4675 const void *zErr;
4676 const char *z;
4677 int bytes = 0;
4679 if( objc!=2 ){
4680 Tcl_AppendResult(interp, "wrong # args: should be \"",
4681 Tcl_GetString(objv[0]), " DB", 0);
4682 return TCL_ERROR;
4684 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4686 zErr = sqlite3_errmsg16(db);
4687 if( zErr ){
4688 z = zErr;
4689 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
4691 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
4692 #endif /* SQLITE_OMIT_UTF16 */
4693 return TCL_OK;
4697 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
4699 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4700 ** database handle <DB>. The parameter <tailval> is the name of a global
4701 ** variable that is set to the unused portion of <sql> (if any). A
4702 ** STMT handle is returned.
4704 static int SQLITE_TCLAPI test_prepare(
4705 void * clientData,
4706 Tcl_Interp *interp,
4707 int objc,
4708 Tcl_Obj *CONST objv[]
4710 sqlite3 *db;
4711 const char *zSql;
4712 int bytes;
4713 const char *zTail = 0;
4714 sqlite3_stmt *pStmt = 0;
4715 char zBuf[50];
4716 int rc;
4718 if( objc!=5 && objc!=4 ){
4719 Tcl_AppendResult(interp, "wrong # args: should be \"",
4720 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4721 return TCL_ERROR;
4723 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4724 zSql = Tcl_GetString(objv[2]);
4725 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4727 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4728 Tcl_ResetResult(interp);
4729 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4730 if( zTail && objc>=5 ){
4731 if( bytes>=0 ){
4732 bytes = bytes - (int)(zTail-zSql);
4734 if( (int)strlen(zTail)<bytes ){
4735 bytes = (int)strlen(zTail);
4737 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4739 if( rc!=SQLITE_OK ){
4740 assert( pStmt==0 );
4741 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4742 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4743 return TCL_ERROR;
4746 if( pStmt ){
4747 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4748 Tcl_AppendResult(interp, zBuf, 0);
4750 return TCL_OK;
4754 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
4756 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4757 ** database handle <DB>. The parameter <tailval> is the name of a global
4758 ** variable that is set to the unused portion of <sql> (if any). A
4759 ** STMT handle is returned.
4761 static int SQLITE_TCLAPI test_prepare_v2(
4762 void * clientData,
4763 Tcl_Interp *interp,
4764 int objc,
4765 Tcl_Obj *CONST objv[]
4767 sqlite3 *db;
4768 const char *zSql;
4769 char *zCopy = 0; /* malloc() copy of zSql */
4770 int bytes;
4771 const char *zTail = 0;
4772 const char **pzTail;
4773 sqlite3_stmt *pStmt = 0;
4774 char zBuf[50];
4775 int rc;
4777 if( objc!=5 && objc!=4 ){
4778 Tcl_AppendResult(interp, "wrong # args: should be \"",
4779 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
4780 return TCL_ERROR;
4782 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4783 zSql = Tcl_GetString(objv[2]);
4784 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4786 /* Instead of using zSql directly, make a copy into a buffer obtained
4787 ** directly from malloc(). The idea is to make it easier for valgrind
4788 ** to spot buffer overreads. */
4789 if( bytes>=0 ){
4790 zCopy = malloc(bytes);
4791 memcpy(zCopy, zSql, bytes);
4792 }else{
4793 int n = (int)strlen(zSql) + 1;
4794 zCopy = malloc(n);
4795 memcpy(zCopy, zSql, n);
4797 pzTail = objc>=5 ? &zTail : 0;
4798 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail);
4799 if( objc>=5 ){
4800 zTail = &zSql[(zTail - zCopy)];
4802 free(zCopy);
4804 assert(rc==SQLITE_OK || pStmt==0);
4805 Tcl_ResetResult(interp);
4806 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4807 if( rc==SQLITE_OK && objc>=5 && zTail ){
4808 if( bytes>=0 ){
4809 bytes = bytes - (int)(zTail-zSql);
4811 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4813 if( rc!=SQLITE_OK ){
4814 assert( pStmt==0 );
4815 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4816 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4817 return TCL_ERROR;
4820 if( pStmt ){
4821 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4822 Tcl_AppendResult(interp, zBuf, 0);
4824 return TCL_OK;
4828 ** Usage: sqlite3_prepare_v3 DB sql bytes flags ?tailvar?
4830 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4831 ** database handle <DB> and flags <flags>. The parameter <tailval> is
4832 ** the name of a global variable that is set to the unused portion of
4833 ** <sql> (if any). A STMT handle is returned.
4835 static int SQLITE_TCLAPI test_prepare_v3(
4836 void * clientData,
4837 Tcl_Interp *interp,
4838 int objc,
4839 Tcl_Obj *CONST objv[]
4841 sqlite3 *db;
4842 const char *zSql;
4843 char *zCopy = 0; /* malloc() copy of zSql */
4844 int bytes, flags;
4845 const char *zTail = 0;
4846 const char **pzTail;
4847 sqlite3_stmt *pStmt = 0;
4848 char zBuf[50];
4849 int rc;
4851 if( objc!=6 && objc!=5 ){
4852 Tcl_AppendResult(interp, "wrong # args: should be \"",
4853 Tcl_GetString(objv[0]), " DB sql bytes flags tailvar", 0);
4854 return TCL_ERROR;
4856 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4857 zSql = Tcl_GetString(objv[2]);
4858 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4859 if( Tcl_GetIntFromObj(interp, objv[4], &flags) ) return TCL_ERROR;
4861 /* Instead of using zSql directly, make a copy into a buffer obtained
4862 ** directly from malloc(). The idea is to make it easier for valgrind
4863 ** to spot buffer overreads. */
4864 if( bytes>=0 ){
4865 zCopy = malloc(bytes);
4866 memcpy(zCopy, zSql, bytes);
4867 }else{
4868 int n = (int)strlen(zSql) + 1;
4869 zCopy = malloc(n);
4870 memcpy(zCopy, zSql, n);
4872 pzTail = objc>=6 ? &zTail : 0;
4873 rc = sqlite3_prepare_v3(db, zCopy, bytes, (unsigned int)flags,&pStmt,pzTail);
4874 free(zCopy);
4875 zTail = &zSql[(zTail - zCopy)];
4877 assert(rc==SQLITE_OK || pStmt==0);
4878 Tcl_ResetResult(interp);
4879 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4880 if( rc==SQLITE_OK && zTail && objc>=6 ){
4881 if( bytes>=0 ){
4882 bytes = bytes - (int)(zTail-zSql);
4884 Tcl_ObjSetVar2(interp, objv[5], 0, Tcl_NewStringObj(zTail, bytes), 0);
4886 if( rc!=SQLITE_OK ){
4887 assert( pStmt==0 );
4888 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4889 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4890 return TCL_ERROR;
4893 if( pStmt ){
4894 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4895 Tcl_AppendResult(interp, zBuf, 0);
4897 return TCL_OK;
4901 ** Usage: sqlite3_prepare_tkt3134 DB
4903 ** Generate a prepared statement for a zero-byte string as a test
4904 ** for ticket #3134. The string should be preceded by a zero byte.
4906 static int SQLITE_TCLAPI test_prepare_tkt3134(
4907 void * clientData,
4908 Tcl_Interp *interp,
4909 int objc,
4910 Tcl_Obj *CONST objv[]
4912 sqlite3 *db;
4913 static const char zSql[] = "\000SELECT 1";
4914 sqlite3_stmt *pStmt = 0;
4915 char zBuf[50];
4916 int rc;
4918 if( objc!=2 ){
4919 Tcl_AppendResult(interp, "wrong # args: should be \"",
4920 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
4921 return TCL_ERROR;
4923 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4924 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
4925 assert(rc==SQLITE_OK || pStmt==0);
4926 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4927 if( rc!=SQLITE_OK ){
4928 assert( pStmt==0 );
4929 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4930 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4931 return TCL_ERROR;
4934 if( pStmt ){
4935 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4936 Tcl_AppendResult(interp, zBuf, 0);
4938 return TCL_OK;
4942 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
4944 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4945 ** database handle <DB>. The parameter <tailval> is the name of a global
4946 ** variable that is set to the unused portion of <sql> (if any). A
4947 ** STMT handle is returned.
4949 static int SQLITE_TCLAPI test_prepare16(
4950 void * clientData,
4951 Tcl_Interp *interp,
4952 int objc,
4953 Tcl_Obj *CONST objv[]
4955 #ifndef SQLITE_OMIT_UTF16
4956 sqlite3 *db;
4957 const void *zSql;
4958 const void *zTail = 0;
4959 Tcl_Obj *pTail = 0;
4960 sqlite3_stmt *pStmt = 0;
4961 char zBuf[50];
4962 int rc;
4963 int bytes; /* The integer specified as arg 3 */
4964 int objlen; /* The byte-array length of arg 2 */
4966 if( objc!=5 && objc!=4 ){
4967 Tcl_AppendResult(interp, "wrong # args: should be \"",
4968 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4969 return TCL_ERROR;
4971 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4972 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
4973 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4975 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4976 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4977 if( rc ){
4978 return TCL_ERROR;
4981 if( objc>=5 ){
4982 if( zTail ){
4983 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
4984 }else{
4985 objlen = 0;
4987 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
4988 Tcl_IncrRefCount(pTail);
4989 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
4990 Tcl_DecrRefCount(pTail);
4993 if( pStmt ){
4994 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4996 Tcl_AppendResult(interp, zBuf, 0);
4997 #endif /* SQLITE_OMIT_UTF16 */
4998 return TCL_OK;
5002 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
5004 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
5005 ** database handle <DB>. The parameter <tailval> is the name of a global
5006 ** variable that is set to the unused portion of <sql> (if any). A
5007 ** STMT handle is returned.
5009 static int SQLITE_TCLAPI test_prepare16_v2(
5010 void * clientData,
5011 Tcl_Interp *interp,
5012 int objc,
5013 Tcl_Obj *CONST objv[]
5015 #ifndef SQLITE_OMIT_UTF16
5016 sqlite3 *db;
5017 const void *zSql;
5018 const void *zTail = 0;
5019 Tcl_Obj *pTail = 0;
5020 sqlite3_stmt *pStmt = 0;
5021 char zBuf[50];
5022 int rc;
5023 int bytes; /* The integer specified as arg 3 */
5024 int objlen; /* The byte-array length of arg 2 */
5026 if( objc!=5 && objc!=4 ){
5027 Tcl_AppendResult(interp, "wrong # args: should be \"",
5028 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
5029 return TCL_ERROR;
5031 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5032 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
5033 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5035 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
5036 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5037 if( rc ){
5038 return TCL_ERROR;
5041 if( objc>=5 ){
5042 if( zTail ){
5043 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
5044 }else{
5045 objlen = 0;
5047 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
5048 Tcl_IncrRefCount(pTail);
5049 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
5050 Tcl_DecrRefCount(pTail);
5053 if( pStmt ){
5054 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5056 Tcl_AppendResult(interp, zBuf, 0);
5057 #endif /* SQLITE_OMIT_UTF16 */
5058 return TCL_OK;
5062 ** Usage: sqlite3_open filename ?options-list?
5064 static int SQLITE_TCLAPI test_open(
5065 void * clientData,
5066 Tcl_Interp *interp,
5067 int objc,
5068 Tcl_Obj *CONST objv[]
5070 const char *zFilename;
5071 sqlite3 *db;
5072 char zBuf[100];
5074 if( objc!=3 && objc!=2 && objc!=1 ){
5075 Tcl_AppendResult(interp, "wrong # args: should be \"",
5076 Tcl_GetString(objv[0]), " filename options-list", 0);
5077 return TCL_ERROR;
5080 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
5081 sqlite3_open(zFilename, &db);
5083 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5084 Tcl_AppendResult(interp, zBuf, 0);
5085 return TCL_OK;
5089 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
5091 static int SQLITE_TCLAPI test_open_v2(
5092 void * clientData,
5093 Tcl_Interp *interp,
5094 int objc,
5095 Tcl_Obj *CONST objv[]
5097 const char *zFilename;
5098 const char *zVfs;
5099 int flags = 0;
5100 sqlite3 *db;
5101 int rc;
5102 char zBuf[100];
5104 int nFlag;
5105 Tcl_Obj **apFlag;
5106 int i;
5108 if( objc!=4 ){
5109 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
5110 return TCL_ERROR;
5112 zFilename = Tcl_GetString(objv[1]);
5113 zVfs = Tcl_GetString(objv[3]);
5114 if( zVfs[0]==0x00 ) zVfs = 0;
5116 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
5117 if( rc!=TCL_OK ) return rc;
5118 for(i=0; i<nFlag; i++){
5119 int iFlag;
5120 struct OpenFlag {
5121 const char *zFlag;
5122 int flag;
5123 } aFlag[] = {
5124 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
5125 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
5126 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
5127 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
5128 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
5129 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
5130 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
5131 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
5132 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
5133 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
5134 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
5135 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
5136 { "SQLITE_OPEN_SUPER_JOURNAL", SQLITE_OPEN_SUPER_JOURNAL },
5137 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
5138 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
5139 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
5140 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
5141 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
5142 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
5143 { "SQLITE_OPEN_EXRESCODE", SQLITE_OPEN_EXRESCODE },
5144 { 0, 0 }
5146 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
5147 "flag", 0, &iFlag
5149 if( rc!=TCL_OK ) return rc;
5150 flags |= aFlag[iFlag].flag;
5153 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
5154 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5155 Tcl_AppendResult(interp, zBuf, 0);
5156 return TCL_OK;
5160 ** Usage: sqlite3_open16 filename options
5162 static int SQLITE_TCLAPI test_open16(
5163 void * clientData,
5164 Tcl_Interp *interp,
5165 int objc,
5166 Tcl_Obj *CONST objv[]
5168 #ifndef SQLITE_OMIT_UTF16
5169 const void *zFilename;
5170 sqlite3 *db;
5171 char zBuf[100];
5173 if( objc!=3 ){
5174 Tcl_AppendResult(interp, "wrong # args: should be \"",
5175 Tcl_GetString(objv[0]), " filename options-list", 0);
5176 return TCL_ERROR;
5179 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
5180 sqlite3_open16(zFilename, &db);
5182 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5183 Tcl_AppendResult(interp, zBuf, 0);
5184 #endif /* SQLITE_OMIT_UTF16 */
5185 return TCL_OK;
5189 ** Usage: sqlite3_complete16 <UTF-16 string>
5191 ** Return 1 if the supplied argument is a complete SQL statement, or zero
5192 ** otherwise.
5194 static int SQLITE_TCLAPI test_complete16(
5195 void * clientData,
5196 Tcl_Interp *interp,
5197 int objc,
5198 Tcl_Obj *CONST objv[]
5200 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
5201 char *zBuf;
5203 if( objc!=2 ){
5204 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
5205 return TCL_ERROR;
5208 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
5209 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
5210 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
5211 return TCL_OK;
5215 ** Usage: sqlite3_normalize SQL
5217 ** Return the normalized value for an SQL statement.
5219 static int SQLITE_TCLAPI test_normalize(
5220 void * clientData,
5221 Tcl_Interp *interp,
5222 int objc,
5223 Tcl_Obj *CONST objv[]
5225 char *zSql;
5226 char *zNorm;
5227 extern char *sqlite3_normalize(const char*);
5229 if( objc!=2 ){
5230 Tcl_WrongNumArgs(interp, 1, objv, "SQL");
5231 return TCL_ERROR;
5234 zSql = (char*)Tcl_GetString(objv[1]);
5235 zNorm = sqlite3_normalize(zSql);
5236 if( zNorm ){
5237 Tcl_SetObjResult(interp, Tcl_NewStringObj(zNorm, -1));
5238 sqlite3_free(zNorm);
5240 return TCL_OK;
5244 ** Usage: sqlite3_step STMT
5246 ** Advance the statement to the next row.
5248 static int SQLITE_TCLAPI test_step(
5249 void * clientData,
5250 Tcl_Interp *interp,
5251 int objc,
5252 Tcl_Obj *CONST objv[]
5254 sqlite3_stmt *pStmt;
5255 int rc;
5257 if( objc!=2 ){
5258 Tcl_AppendResult(interp, "wrong # args: should be \"",
5259 Tcl_GetString(objv[0]), " STMT", 0);
5260 return TCL_ERROR;
5263 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5264 rc = sqlite3_step(pStmt);
5266 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
5267 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
5268 return TCL_OK;
5271 static int SQLITE_TCLAPI test_sql(
5272 void * clientData,
5273 Tcl_Interp *interp,
5274 int objc,
5275 Tcl_Obj *CONST objv[]
5277 sqlite3_stmt *pStmt;
5279 if( objc!=2 ){
5280 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5281 return TCL_ERROR;
5284 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5285 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
5286 return TCL_OK;
5288 static int SQLITE_TCLAPI test_ex_sql(
5289 void * clientData,
5290 Tcl_Interp *interp,
5291 int objc,
5292 Tcl_Obj *CONST objv[]
5294 sqlite3_stmt *pStmt;
5295 char *z;
5297 if( objc!=2 ){
5298 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5299 return TCL_ERROR;
5302 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5303 z = sqlite3_expanded_sql(pStmt);
5304 Tcl_SetResult(interp, z, TCL_VOLATILE);
5305 sqlite3_free(z);
5306 return TCL_OK;
5308 #ifdef SQLITE_ENABLE_NORMALIZE
5309 static int SQLITE_TCLAPI test_norm_sql(
5310 void * clientData,
5311 Tcl_Interp *interp,
5312 int objc,
5313 Tcl_Obj *CONST objv[]
5315 sqlite3_stmt *pStmt;
5317 if( objc!=2 ){
5318 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5319 return TCL_ERROR;
5322 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5323 Tcl_SetResult(interp, (char *)sqlite3_normalized_sql(pStmt), TCL_VOLATILE);
5324 return TCL_OK;
5326 #endif /* SQLITE_ENABLE_NORMALIZE */
5329 ** Usage: sqlite3_column_count STMT
5331 ** Return the number of columns returned by the sql statement STMT.
5333 static int SQLITE_TCLAPI test_column_count(
5334 void * clientData,
5335 Tcl_Interp *interp,
5336 int objc,
5337 Tcl_Obj *CONST objv[]
5339 sqlite3_stmt *pStmt;
5341 if( objc!=2 ){
5342 Tcl_AppendResult(interp, "wrong # args: should be \"",
5343 Tcl_GetString(objv[0]), " STMT column", 0);
5344 return TCL_ERROR;
5347 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5349 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
5350 return TCL_OK;
5354 ** Usage: sqlite3_column_type STMT column
5356 ** Return the type of the data in column 'column' of the current row.
5358 static int SQLITE_TCLAPI test_column_type(
5359 void * clientData,
5360 Tcl_Interp *interp,
5361 int objc,
5362 Tcl_Obj *CONST objv[]
5364 sqlite3_stmt *pStmt;
5365 int col;
5366 int tp;
5368 if( objc!=3 ){
5369 Tcl_AppendResult(interp, "wrong # args: should be \"",
5370 Tcl_GetString(objv[0]), " STMT column", 0);
5371 return TCL_ERROR;
5374 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5375 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5377 tp = sqlite3_column_type(pStmt, col);
5378 switch( tp ){
5379 case SQLITE_INTEGER:
5380 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
5381 break;
5382 case SQLITE_NULL:
5383 Tcl_SetResult(interp, "NULL", TCL_STATIC);
5384 break;
5385 case SQLITE_FLOAT:
5386 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
5387 break;
5388 case SQLITE_TEXT:
5389 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
5390 break;
5391 case SQLITE_BLOB:
5392 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
5393 break;
5394 default:
5395 assert(0);
5398 return TCL_OK;
5402 ** Usage: sqlite3_column_int64 STMT column
5404 ** Return the data in column 'column' of the current row cast as an
5405 ** wide (64-bit) integer.
5407 static int SQLITE_TCLAPI test_column_int64(
5408 void * clientData,
5409 Tcl_Interp *interp,
5410 int objc,
5411 Tcl_Obj *CONST objv[]
5413 sqlite3_stmt *pStmt;
5414 int col;
5415 i64 iVal;
5417 if( objc!=3 ){
5418 Tcl_AppendResult(interp, "wrong # args: should be \"",
5419 Tcl_GetString(objv[0]), " STMT column", 0);
5420 return TCL_ERROR;
5423 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5424 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5426 iVal = sqlite3_column_int64(pStmt, col);
5427 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
5428 return TCL_OK;
5432 ** Usage: sqlite3_column_blob STMT column
5434 static int SQLITE_TCLAPI test_column_blob(
5435 void * clientData,
5436 Tcl_Interp *interp,
5437 int objc,
5438 Tcl_Obj *CONST objv[]
5440 sqlite3_stmt *pStmt;
5441 int col;
5443 int len;
5444 const void *pBlob;
5446 if( objc!=3 ){
5447 Tcl_AppendResult(interp, "wrong # args: should be \"",
5448 Tcl_GetString(objv[0]), " STMT column", 0);
5449 return TCL_ERROR;
5452 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5453 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5455 len = sqlite3_column_bytes(pStmt, col);
5456 pBlob = sqlite3_column_blob(pStmt, col);
5457 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
5458 return TCL_OK;
5462 ** Usage: sqlite3_column_double STMT column
5464 ** Return the data in column 'column' of the current row cast as a double.
5466 static int SQLITE_TCLAPI test_column_double(
5467 void * clientData,
5468 Tcl_Interp *interp,
5469 int objc,
5470 Tcl_Obj *CONST objv[]
5472 sqlite3_stmt *pStmt;
5473 int col;
5474 double rVal;
5476 if( objc!=3 ){
5477 Tcl_AppendResult(interp, "wrong # args: should be \"",
5478 Tcl_GetString(objv[0]), " STMT column", 0);
5479 return TCL_ERROR;
5482 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5483 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5485 rVal = sqlite3_column_double(pStmt, col);
5486 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
5487 return TCL_OK;
5491 ** Usage: sqlite3_data_count STMT
5493 ** Return the number of columns returned by the sql statement STMT.
5495 static int SQLITE_TCLAPI test_data_count(
5496 void * clientData,
5497 Tcl_Interp *interp,
5498 int objc,
5499 Tcl_Obj *CONST objv[]
5501 sqlite3_stmt *pStmt;
5503 if( objc!=2 ){
5504 Tcl_AppendResult(interp, "wrong # args: should be \"",
5505 Tcl_GetString(objv[0]), " STMT column", 0);
5506 return TCL_ERROR;
5509 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5511 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
5512 return TCL_OK;
5516 ** Usage: sqlite3_column_text STMT column
5518 ** Usage: sqlite3_column_decltype STMT column
5520 ** Usage: sqlite3_column_name STMT column
5522 static int SQLITE_TCLAPI test_stmt_utf8(
5523 void * clientData, /* Pointer to SQLite API function to be invoke */
5524 Tcl_Interp *interp,
5525 int objc,
5526 Tcl_Obj *CONST objv[]
5528 sqlite3_stmt *pStmt;
5529 int col;
5530 const char *(*xFunc)(sqlite3_stmt*, int);
5531 const char *zRet;
5533 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
5534 if( objc!=3 ){
5535 Tcl_AppendResult(interp, "wrong # args: should be \"",
5536 Tcl_GetString(objv[0]), " STMT column", 0);
5537 return TCL_ERROR;
5540 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5541 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5542 zRet = xFunc(pStmt, col);
5543 if( zRet ){
5544 Tcl_SetResult(interp, (char *)zRet, 0);
5546 return TCL_OK;
5549 static int SQLITE_TCLAPI test_global_recover(
5550 void * clientData,
5551 Tcl_Interp *interp,
5552 int objc,
5553 Tcl_Obj *CONST objv[]
5555 #ifndef SQLITE_OMIT_DEPRECATED
5556 int rc;
5557 if( objc!=1 ){
5558 Tcl_WrongNumArgs(interp, 1, objv, "");
5559 return TCL_ERROR;
5561 rc = sqlite3_global_recover();
5562 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5563 #endif
5564 return TCL_OK;
5568 ** Usage: sqlite3_column_text STMT column
5570 ** Usage: sqlite3_column_decltype STMT column
5572 ** Usage: sqlite3_column_name STMT column
5574 static int SQLITE_TCLAPI test_stmt_utf16(
5575 void * clientData, /* Pointer to SQLite API function to be invoked */
5576 Tcl_Interp *interp,
5577 int objc,
5578 Tcl_Obj *CONST objv[]
5580 #ifndef SQLITE_OMIT_UTF16
5581 sqlite3_stmt *pStmt;
5582 int col;
5583 Tcl_Obj *pRet;
5584 const void *zName16;
5585 const void *(*xFunc)(sqlite3_stmt*, int);
5587 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
5588 if( objc!=3 ){
5589 Tcl_AppendResult(interp, "wrong # args: should be \"",
5590 Tcl_GetString(objv[0]), " STMT column", 0);
5591 return TCL_ERROR;
5594 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5595 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5597 zName16 = xFunc(pStmt, col);
5598 if( zName16 ){
5599 int n;
5600 const char *z = zName16;
5601 for(n=0; z[n] || z[n+1]; n+=2){}
5602 pRet = Tcl_NewByteArrayObj(zName16, n+2);
5603 Tcl_SetObjResult(interp, pRet);
5605 #endif /* SQLITE_OMIT_UTF16 */
5607 return TCL_OK;
5611 ** Usage: sqlite3_column_int STMT column
5613 ** Usage: sqlite3_column_bytes STMT column
5615 ** Usage: sqlite3_column_bytes16 STMT column
5618 static int SQLITE_TCLAPI test_stmt_int(
5619 void * clientData, /* Pointer to SQLite API function to be invoked */
5620 Tcl_Interp *interp,
5621 int objc,
5622 Tcl_Obj *CONST objv[]
5624 sqlite3_stmt *pStmt;
5625 int col;
5626 int (*xFunc)(sqlite3_stmt*, int);
5628 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
5629 if( objc!=3 ){
5630 Tcl_AppendResult(interp, "wrong # args: should be \"",
5631 Tcl_GetString(objv[0]), " STMT column", 0);
5632 return TCL_ERROR;
5635 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5636 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5638 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
5639 return TCL_OK;
5643 ** Usage: sqlite3_interrupt DB
5645 ** Trigger an interrupt on DB
5647 static int SQLITE_TCLAPI test_interrupt(
5648 void * clientData,
5649 Tcl_Interp *interp,
5650 int argc,
5651 char **argv
5653 sqlite3 *db;
5654 if( argc!=2 ){
5655 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5656 return TCL_ERROR;
5658 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5659 sqlite3_interrupt(db);
5660 return TCL_OK;
5664 ** Usage: sqlite3_is_interrupted DB
5666 ** return true if an interrupt is current in effect on DB
5668 static int SQLITE_TCLAPI test_is_interrupted(
5669 void * clientData,
5670 Tcl_Interp *interp,
5671 int argc,
5672 char **argv
5674 sqlite3 *db;
5675 int rc;
5676 if( argc!=2 ){
5677 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5678 return TCL_ERROR;
5680 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5681 rc = sqlite3_is_interrupted(db);
5682 Tcl_AppendResult(interp, rc ? "1" : "0", (void*)0);
5683 return TCL_OK;
5687 ** Usage: sqlite_delete_function DB function-name
5689 ** Delete the user function 'function-name' from database handle DB. It
5690 ** is assumed that the user function was created as UTF8, any number of
5691 ** arguments (the way the TCL interface does it).
5693 static int SQLITE_TCLAPI delete_function(
5694 void * clientData,
5695 Tcl_Interp *interp,
5696 int argc,
5697 char **argv
5699 int rc;
5700 sqlite3 *db;
5701 if( argc!=3 ){
5702 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5703 " DB function-name", 0);
5704 return TCL_ERROR;
5706 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5707 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
5708 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5709 return TCL_OK;
5713 ** Usage: sqlite_delete_collation DB collation-name
5715 ** Delete the collation sequence 'collation-name' from database handle
5716 ** DB. It is assumed that the collation sequence was created as UTF8 (the
5717 ** way the TCL interface does it).
5719 static int SQLITE_TCLAPI delete_collation(
5720 void * clientData,
5721 Tcl_Interp *interp,
5722 int argc,
5723 char **argv
5725 int rc;
5726 sqlite3 *db;
5727 if( argc!=3 ){
5728 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5729 " DB function-name", 0);
5730 return TCL_ERROR;
5732 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5733 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
5734 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5735 return TCL_OK;
5739 ** Usage: sqlite3_get_autocommit DB
5741 ** Return true if the database DB is currently in auto-commit mode.
5742 ** Return false if not.
5744 static int SQLITE_TCLAPI get_autocommit(
5745 void * clientData,
5746 Tcl_Interp *interp,
5747 int argc,
5748 char **argv
5750 char zBuf[30];
5751 sqlite3 *db;
5752 if( argc!=2 ){
5753 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5754 " DB", 0);
5755 return TCL_ERROR;
5757 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5758 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db));
5759 Tcl_AppendResult(interp, zBuf, 0);
5760 return TCL_OK;
5764 ** Usage: sqlite3_busy_timeout DB MS
5766 ** Set the busy timeout. This is more easily done using the timeout
5767 ** method of the TCL interface. But we need a way to test the case
5768 ** where it returns SQLITE_MISUSE.
5770 static int SQLITE_TCLAPI test_busy_timeout(
5771 void * clientData,
5772 Tcl_Interp *interp,
5773 int argc,
5774 char **argv
5776 int rc, ms;
5777 sqlite3 *db;
5778 if( argc!=3 ){
5779 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5780 " DB", 0);
5781 return TCL_ERROR;
5783 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5784 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
5785 rc = sqlite3_busy_timeout(db, ms);
5786 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
5787 return TCL_OK;
5791 ** Usage: tcl_variable_type VARIABLENAME
5793 ** Return the name of the internal representation for the
5794 ** value of the given variable.
5796 static int SQLITE_TCLAPI tcl_variable_type(
5797 void * clientData,
5798 Tcl_Interp *interp,
5799 int objc,
5800 Tcl_Obj *CONST objv[]
5802 Tcl_Obj *pVar;
5803 if( objc!=2 ){
5804 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
5805 return TCL_ERROR;
5807 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
5808 if( pVar==0 ) return TCL_ERROR;
5809 if( pVar->typePtr ){
5810 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
5812 return TCL_OK;
5816 ** Usage: sqlite3_release_memory ?N?
5818 ** Attempt to release memory currently held but not actually required.
5819 ** The integer N is the number of bytes we are trying to release. The
5820 ** return value is the amount of memory actually released.
5822 static int SQLITE_TCLAPI test_release_memory(
5823 void * clientData,
5824 Tcl_Interp *interp,
5825 int objc,
5826 Tcl_Obj *CONST objv[]
5828 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
5829 int N;
5830 int amt;
5831 if( objc!=1 && objc!=2 ){
5832 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
5833 return TCL_ERROR;
5835 if( objc==2 ){
5836 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
5837 }else{
5838 N = -1;
5840 amt = sqlite3_release_memory(N);
5841 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
5842 #endif
5843 return TCL_OK;
5848 ** Usage: sqlite3_db_release_memory DB
5850 ** Attempt to release memory currently held by database DB. Return the
5851 ** result code (which in the current implementation is always zero).
5853 static int SQLITE_TCLAPI test_db_release_memory(
5854 void * clientData,
5855 Tcl_Interp *interp,
5856 int objc,
5857 Tcl_Obj *CONST objv[]
5859 sqlite3 *db;
5860 int rc;
5861 if( objc!=2 ){
5862 Tcl_WrongNumArgs(interp, 1, objv, "DB");
5863 return TCL_ERROR;
5865 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5866 rc = sqlite3_db_release_memory(db);
5867 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5868 return TCL_OK;
5872 ** Usage: sqlite3_db_cacheflush DB
5874 ** Attempt to flush any dirty pages to disk.
5876 static int SQLITE_TCLAPI test_db_cacheflush(
5877 void * clientData,
5878 Tcl_Interp *interp,
5879 int objc,
5880 Tcl_Obj *CONST objv[]
5882 sqlite3 *db;
5883 int rc;
5884 if( objc!=2 ){
5885 Tcl_WrongNumArgs(interp, 1, objv, "DB");
5886 return TCL_ERROR;
5888 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5889 rc = sqlite3_db_cacheflush(db);
5890 if( rc ){
5891 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
5892 return TCL_ERROR;
5895 Tcl_ResetResult(interp);
5896 return TCL_OK;
5900 ** Usage: sqlite3_system_errno DB
5902 ** Return the low-level system errno value.
5904 static int SQLITE_TCLAPI test_system_errno(
5905 void * clientData,
5906 Tcl_Interp *interp,
5907 int objc,
5908 Tcl_Obj *CONST objv[]
5910 sqlite3 *db;
5911 int iErrno;
5912 if( objc!=2 ){
5913 Tcl_WrongNumArgs(interp, 1, objv, "DB");
5914 return TCL_ERROR;
5916 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5917 iErrno = sqlite3_system_errno(db);
5918 Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno));
5919 return TCL_OK;
5923 ** Usage: sqlite3_db_filename DB DBNAME
5925 ** Return the name of a file associated with a database.
5927 static int SQLITE_TCLAPI test_db_filename(
5928 void * clientData,
5929 Tcl_Interp *interp,
5930 int objc,
5931 Tcl_Obj *CONST objv[]
5933 sqlite3 *db;
5934 const char *zDbName;
5935 if( objc!=3 ){
5936 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
5937 return TCL_ERROR;
5939 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5940 zDbName = Tcl_GetString(objv[2]);
5941 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
5942 return TCL_OK;
5946 ** Usage: sqlite3_db_readonly DB DBNAME
5948 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does
5949 ** not exist.
5951 static int SQLITE_TCLAPI test_db_readonly(
5952 void * clientData,
5953 Tcl_Interp *interp,
5954 int objc,
5955 Tcl_Obj *CONST objv[]
5957 sqlite3 *db;
5958 const char *zDbName;
5959 if( objc!=3 ){
5960 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
5961 return TCL_ERROR;
5963 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5964 zDbName = Tcl_GetString(objv[2]);
5965 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
5966 return TCL_OK;
5970 ** Usage: sqlite3_soft_heap_limit ?N?
5972 ** Query or set the soft heap limit for the current thread. The
5973 ** limit is only changed if the N is present. The previous limit
5974 ** is returned.
5976 static int SQLITE_TCLAPI test_soft_heap_limit(
5977 void * clientData,
5978 Tcl_Interp *interp,
5979 int objc,
5980 Tcl_Obj *CONST objv[]
5982 sqlite3_int64 amt;
5983 Tcl_WideInt N = -1;
5984 if( objc!=1 && objc!=2 ){
5985 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
5986 return TCL_ERROR;
5988 if( objc==2 ){
5989 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
5991 amt = sqlite3_soft_heap_limit64(N);
5992 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
5993 return TCL_OK;
5997 ** Usage: sqlite3_hard_heap_limit ?N?
5999 ** Query or set the hard heap limit for the current thread. The
6000 ** limit is only changed if the N is present. The previous limit
6001 ** is returned.
6003 static int SQLITE_TCLAPI test_hard_heap_limit(
6004 void * clientData,
6005 Tcl_Interp *interp,
6006 int objc,
6007 Tcl_Obj *CONST objv[]
6009 sqlite3_int64 amt;
6010 Tcl_WideInt N = -1;
6011 if( objc!=1 && objc!=2 ){
6012 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
6013 return TCL_ERROR;
6015 if( objc==2 ){
6016 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
6018 amt = sqlite3_hard_heap_limit64(N);
6019 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
6020 return TCL_OK;
6024 ** Usage: sqlite3_thread_cleanup
6026 ** Call the sqlite3_thread_cleanup API.
6028 static int SQLITE_TCLAPI test_thread_cleanup(
6029 void * clientData,
6030 Tcl_Interp *interp,
6031 int objc,
6032 Tcl_Obj *CONST objv[]
6034 #ifndef SQLITE_OMIT_DEPRECATED
6035 sqlite3_thread_cleanup();
6036 #endif
6037 return TCL_OK;
6041 ** Usage: sqlite3_pager_refcounts DB
6043 ** Return a list of numbers which are the PagerRefcount for all
6044 ** pagers on each database connection.
6046 static int SQLITE_TCLAPI test_pager_refcounts(
6047 void * clientData,
6048 Tcl_Interp *interp,
6049 int objc,
6050 Tcl_Obj *CONST objv[]
6052 sqlite3 *db;
6053 int i;
6054 int v, *a;
6055 Tcl_Obj *pResult;
6057 if( objc!=2 ){
6058 Tcl_AppendResult(interp, "wrong # args: should be \"",
6059 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6060 return TCL_ERROR;
6062 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6063 pResult = Tcl_NewObj();
6064 for(i=0; i<db->nDb; i++){
6065 if( db->aDb[i].pBt==0 ){
6066 v = -1;
6067 }else{
6068 sqlite3_mutex_enter(db->mutex);
6069 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
6070 v = a[0];
6071 sqlite3_mutex_leave(db->mutex);
6073 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
6075 Tcl_SetObjResult(interp, pResult);
6076 return TCL_OK;
6081 ** tclcmd: working_64bit_int
6083 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
6084 ** leads to a number of test failures. The present command checks the
6085 ** TCL build to see whether or not it supports 64-bit integers. It
6086 ** returns TRUE if it does and FALSE if not.
6088 ** This command is used to warn users that their TCL build is defective
6089 ** and that the errors they are seeing in the test scripts might be
6090 ** a result of their defective TCL rather than problems in SQLite.
6092 static int SQLITE_TCLAPI working_64bit_int(
6093 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6094 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6095 int objc, /* Number of arguments */
6096 Tcl_Obj *CONST objv[] /* Command arguments */
6098 Tcl_Obj *pTestObj;
6099 int working = 0;
6101 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
6102 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
6103 Tcl_DecrRefCount(pTestObj);
6104 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
6105 return TCL_OK;
6110 ** tclcmd: vfs_unlink_test
6112 ** This TCL command unregisters the primary VFS and then registers
6113 ** it back again. This is used to test the ability to register a
6114 ** VFS when none are previously registered, and the ability to
6115 ** unregister the only available VFS. Ticket #2738
6117 static int SQLITE_TCLAPI vfs_unlink_test(
6118 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6119 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6120 int objc, /* Number of arguments */
6121 Tcl_Obj *CONST objv[] /* Command arguments */
6123 int i;
6124 sqlite3_vfs *pMain;
6125 sqlite3_vfs *apVfs[20];
6126 sqlite3_vfs one, two;
6128 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
6129 one.zName = "__one";
6130 two.zName = "__two";
6132 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
6133 ** change the default VFS
6135 pMain = sqlite3_vfs_find(0);
6136 sqlite3_vfs_register(&one, 0);
6137 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
6138 sqlite3_vfs_register(&two, 0);
6139 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
6141 /* We can find a VFS by its name */
6142 assert( sqlite3_vfs_find("__one")==&one );
6143 assert( sqlite3_vfs_find("__two")==&two );
6145 /* Calling sqlite_vfs_register with non-zero second parameter changes the
6146 ** default VFS, even if the 1st parameter is an existig VFS that is
6147 ** previously registered as the non-default.
6149 sqlite3_vfs_register(&one, 1);
6150 assert( sqlite3_vfs_find("__one")==&one );
6151 assert( sqlite3_vfs_find("__two")==&two );
6152 assert( sqlite3_vfs_find(0)==&one );
6153 sqlite3_vfs_register(&two, 1);
6154 assert( sqlite3_vfs_find("__one")==&one );
6155 assert( sqlite3_vfs_find("__two")==&two );
6156 assert( sqlite3_vfs_find(0)==&two );
6157 if( pMain ){
6158 sqlite3_vfs_register(pMain, 1);
6159 assert( sqlite3_vfs_find("__one")==&one );
6160 assert( sqlite3_vfs_find("__two")==&two );
6161 assert( sqlite3_vfs_find(0)==pMain );
6164 /* Unlink the default VFS. Repeat until there are no more VFSes
6165 ** registered.
6167 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
6168 apVfs[i] = sqlite3_vfs_find(0);
6169 if( apVfs[i] ){
6170 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
6171 sqlite3_vfs_unregister(apVfs[i]);
6172 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
6175 assert( 0==sqlite3_vfs_find(0) );
6177 /* Register the main VFS as non-default (will be made default, since
6178 ** it'll be the only one in existence).
6180 sqlite3_vfs_register(pMain, 0);
6181 assert( sqlite3_vfs_find(0)==pMain );
6183 /* Un-register the main VFS again to restore an empty VFS list */
6184 sqlite3_vfs_unregister(pMain);
6185 assert( 0==sqlite3_vfs_find(0) );
6187 /* Relink all VFSes in reverse order. */
6188 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
6189 if( apVfs[i] ){
6190 sqlite3_vfs_register(apVfs[i], 1);
6191 assert( apVfs[i]==sqlite3_vfs_find(0) );
6192 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
6196 /* Unregister out sample VFSes. */
6197 sqlite3_vfs_unregister(&one);
6198 sqlite3_vfs_unregister(&two);
6200 /* Unregistering a VFS that is not currently registered is harmless */
6201 sqlite3_vfs_unregister(&one);
6202 sqlite3_vfs_unregister(&two);
6203 assert( sqlite3_vfs_find("__one")==0 );
6204 assert( sqlite3_vfs_find("__two")==0 );
6206 /* We should be left with the original default VFS back as the
6207 ** original */
6208 assert( sqlite3_vfs_find(0)==pMain );
6210 return TCL_OK;
6214 ** tclcmd: vfs_initfail_test
6216 ** This TCL command attempts to vfs_find and vfs_register when the
6217 ** sqlite3_initialize() interface is failing. All calls should fail.
6219 static int SQLITE_TCLAPI vfs_initfail_test(
6220 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6221 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6222 int objc, /* Number of arguments */
6223 Tcl_Obj *CONST objv[] /* Command arguments */
6225 sqlite3_vfs one;
6226 one.zName = "__one";
6228 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6229 sqlite3_vfs_register(&one, 0);
6230 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6231 sqlite3_vfs_register(&one, 1);
6232 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6233 return TCL_OK;
6237 ** Saved VFSes
6239 static sqlite3_vfs *apVfs[20];
6240 static int nVfs = 0;
6243 ** tclcmd: vfs_unregister_all
6245 ** Unregister all VFSes.
6247 static int SQLITE_TCLAPI vfs_unregister_all(
6248 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6249 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6250 int objc, /* Number of arguments */
6251 Tcl_Obj *CONST objv[] /* Command arguments */
6253 int i;
6254 for(i=0; i<ArraySize(apVfs); i++){
6255 apVfs[i] = sqlite3_vfs_find(0);
6256 if( apVfs[i]==0 ) break;
6257 sqlite3_vfs_unregister(apVfs[i]);
6259 nVfs = i;
6260 return TCL_OK;
6263 ** tclcmd: vfs_reregister_all
6265 ** Restore all VFSes that were removed using vfs_unregister_all. Taking
6266 ** care to put the linked list back together in the same order as it was
6267 ** in before vfs_unregister_all was invoked.
6269 static int SQLITE_TCLAPI vfs_reregister_all(
6270 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6271 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6272 int objc, /* Number of arguments */
6273 Tcl_Obj *CONST objv[] /* Command arguments */
6275 int i;
6276 for(i=nVfs-1; i>=0; i--){
6277 sqlite3_vfs_register(apVfs[i], 1);
6279 return TCL_OK;
6284 ** tclcmd: file_control_test DB
6286 ** This TCL command runs the sqlite3_file_control interface and
6287 ** verifies correct operation of the same.
6289 static int SQLITE_TCLAPI file_control_test(
6290 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6291 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6292 int objc, /* Number of arguments */
6293 Tcl_Obj *CONST objv[] /* Command arguments */
6295 int iArg = 0;
6296 sqlite3 *db;
6297 int rc;
6299 if( objc!=2 ){
6300 Tcl_AppendResult(interp, "wrong # args: should be \"",
6301 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6302 return TCL_ERROR;
6304 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6305 rc = sqlite3_file_control(db, 0, 0, &iArg);
6306 assert( rc==SQLITE_NOTFOUND );
6307 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
6308 assert( rc==SQLITE_ERROR );
6309 rc = sqlite3_file_control(db, "main", -1, &iArg);
6310 assert( rc==SQLITE_NOTFOUND );
6311 rc = sqlite3_file_control(db, "temp", -1, &iArg);
6312 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
6314 return TCL_OK;
6319 ** tclcmd: file_control_lasterrno_test DB
6321 ** This TCL command runs the sqlite3_file_control interface and
6322 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
6324 static int SQLITE_TCLAPI file_control_lasterrno_test(
6325 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6326 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6327 int objc, /* Number of arguments */
6328 Tcl_Obj *CONST objv[] /* Command arguments */
6330 int iArg = 0;
6331 sqlite3 *db;
6332 int rc;
6334 if( objc!=2 ){
6335 Tcl_AppendResult(interp, "wrong # args: should be \"",
6336 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6337 return TCL_ERROR;
6339 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6340 return TCL_ERROR;
6342 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
6343 if( rc ){
6344 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6345 return TCL_ERROR;
6347 if( iArg!=0 ) {
6348 Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
6349 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
6350 return TCL_ERROR;
6352 return TCL_OK;
6356 ** tclcmd: file_control_data_version DB DBNAME
6358 ** This TCL command runs the sqlite3_file_control with the
6359 ** SQLITE_FCNTL_DATA_VERSION opcode, returning the result.
6361 static int SQLITE_TCLAPI file_control_data_version(
6362 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6363 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6364 int objc, /* Number of arguments */
6365 Tcl_Obj *CONST objv[] /* Command arguments */
6367 unsigned int iVers; /* data version */
6368 char *zDb; /* Db name ("main", "temp" etc.) */
6369 sqlite3 *db; /* Database handle */
6370 int rc; /* file_control() return code */
6371 char zBuf[100];
6373 if( objc!=3 && objc!=2 ){
6374 Tcl_WrongNumArgs(interp, 1, objv, "DB [DBNAME]");
6375 return TCL_ERROR;
6377 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6378 return TCL_ERROR;
6380 zDb = objc==3 ? Tcl_GetString(objv[2]) : NULL;
6382 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_DATA_VERSION, (void *)&iVers);
6383 if( rc ){
6384 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6385 return TCL_ERROR;
6386 }else{
6387 sqlite3_snprintf(sizeof(zBuf),zBuf,"%u",iVers);
6388 Tcl_SetResult(interp, (char *)zBuf, TCL_VOLATILE);
6389 return TCL_OK;
6394 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
6396 ** This TCL command runs the sqlite3_file_control interface and
6397 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6398 ** SQLITE_SET_LOCKPROXYFILE verbs.
6400 static int SQLITE_TCLAPI file_control_chunksize_test(
6401 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6402 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6403 int objc, /* Number of arguments */
6404 Tcl_Obj *CONST objv[] /* Command arguments */
6406 int nSize; /* New chunk size */
6407 char *zDb; /* Db name ("main", "temp" etc.) */
6408 sqlite3 *db; /* Database handle */
6409 int rc; /* file_control() return code */
6411 if( objc!=4 ){
6412 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
6413 return TCL_ERROR;
6415 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6416 || Tcl_GetIntFromObj(interp, objv[3], &nSize)
6418 return TCL_ERROR;
6420 zDb = Tcl_GetString(objv[2]);
6421 if( zDb[0]=='\0' ) zDb = NULL;
6423 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
6424 if( rc ){
6425 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6426 return TCL_ERROR;
6428 return TCL_OK;
6432 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
6434 ** This TCL command runs the sqlite3_file_control interface
6435 ** with SQLITE_FCNTL_SIZE_HINT
6437 static int SQLITE_TCLAPI file_control_sizehint_test(
6438 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6439 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6440 int objc, /* Number of arguments */
6441 Tcl_Obj *CONST objv[] /* Command arguments */
6443 Tcl_WideInt nSize; /* Hinted size */
6444 char *zDb; /* Db name ("main", "temp" etc.) */
6445 sqlite3 *db; /* Database handle */
6446 int rc; /* file_control() return code */
6448 if( objc!=4 ){
6449 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
6450 return TCL_ERROR;
6452 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6453 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
6455 return TCL_ERROR;
6457 zDb = Tcl_GetString(objv[2]);
6458 if( zDb[0]=='\0' ) zDb = NULL;
6460 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
6461 if( rc ){
6462 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6463 return TCL_ERROR;
6465 return TCL_OK;
6469 ** tclcmd: file_control_lockproxy_test DB PWD
6471 ** This TCL command runs the sqlite3_file_control interface and
6472 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6473 ** SQLITE_SET_LOCKPROXYFILE verbs.
6475 static int SQLITE_TCLAPI file_control_lockproxy_test(
6476 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6477 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6478 int objc, /* Number of arguments */
6479 Tcl_Obj *CONST objv[] /* Command arguments */
6481 sqlite3 *db;
6483 if( objc!=3 ){
6484 Tcl_AppendResult(interp, "wrong # args: should be \"",
6485 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
6486 return TCL_ERROR;
6488 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6489 return TCL_ERROR;
6492 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
6493 # if defined(__APPLE__)
6494 # define SQLITE_ENABLE_LOCKING_STYLE 1
6495 # else
6496 # define SQLITE_ENABLE_LOCKING_STYLE 0
6497 # endif
6498 #endif
6499 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
6501 char *testPath;
6502 int rc;
6503 int nPwd;
6504 const char *zPwd;
6505 char proxyPath[400];
6507 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
6508 if( sizeof(proxyPath)<nPwd+20 ){
6509 Tcl_AppendResult(interp, "PWD too big", (void*)0);
6510 return TCL_ERROR;
6512 sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd);
6513 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
6514 if( rc ){
6515 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6516 return TCL_ERROR;
6518 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
6519 if( strncmp(proxyPath,testPath,11) ){
6520 Tcl_AppendResult(interp, "Lock proxy file did not match the "
6521 "previously assigned value", 0);
6522 return TCL_ERROR;
6524 if( rc ){
6525 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6526 return TCL_ERROR;
6528 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
6529 if( rc ){
6530 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6531 return TCL_ERROR;
6534 #endif
6535 return TCL_OK;
6538 #if SQLITE_OS_WIN
6540 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY
6542 ** This TCL command runs the sqlite3_file_control interface with
6543 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
6545 static int SQLITE_TCLAPI file_control_win32_av_retry(
6546 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6547 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6548 int objc, /* Number of arguments */
6549 Tcl_Obj *CONST objv[] /* Command arguments */
6551 sqlite3 *db;
6552 int rc;
6553 int a[2];
6554 char z[100];
6556 if( objc!=4 ){
6557 Tcl_AppendResult(interp, "wrong # args: should be \"",
6558 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
6559 return TCL_ERROR;
6561 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6562 return TCL_ERROR;
6564 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
6565 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
6566 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
6567 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
6568 Tcl_AppendResult(interp, z, (char*)0);
6569 return TCL_OK;
6573 ** tclcmd: file_control_win32_get_handle DB
6575 ** This TCL command runs the sqlite3_file_control interface with
6576 ** the SQLITE_FCNTL_WIN32_GET_HANDLE opcode.
6578 static int file_control_win32_get_handle(
6579 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6580 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6581 int objc, /* Number of arguments */
6582 Tcl_Obj *CONST objv[] /* Command arguments */
6584 sqlite3 *db;
6585 int rc;
6586 HANDLE hFile = NULL;
6587 char z[100];
6589 if( objc!=2 ){
6590 Tcl_AppendResult(interp, "wrong # args: should be \"",
6591 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6592 return TCL_ERROR;
6594 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6595 return TCL_ERROR;
6597 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_GET_HANDLE,
6598 (void*)&hFile);
6599 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
6600 Tcl_AppendResult(interp, z, (char*)0);
6601 return TCL_OK;
6605 ** tclcmd: file_control_win32_set_handle DB HANDLE
6607 ** This TCL command runs the sqlite3_file_control interface with
6608 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
6610 static int SQLITE_TCLAPI file_control_win32_set_handle(
6611 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6612 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6613 int objc, /* Number of arguments */
6614 Tcl_Obj *CONST objv[] /* Command arguments */
6616 sqlite3 *db;
6617 int rc;
6618 HANDLE hFile = NULL;
6619 char z[100];
6621 if( objc!=3 ){
6622 Tcl_AppendResult(interp, "wrong # args: should be \"",
6623 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0);
6624 return TCL_ERROR;
6626 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6627 return TCL_ERROR;
6629 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){
6630 return TCL_ERROR;
6632 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE,
6633 (void*)&hFile);
6634 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
6635 Tcl_AppendResult(interp, z, (char*)0);
6636 return TCL_OK;
6638 #endif
6641 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG
6643 ** This TCL command runs the sqlite3_file_control interface with
6644 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
6646 static int SQLITE_TCLAPI file_control_persist_wal(
6647 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6648 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6649 int objc, /* Number of arguments */
6650 Tcl_Obj *CONST objv[] /* Command arguments */
6652 sqlite3 *db;
6653 int rc;
6654 int bPersist;
6655 char z[100];
6657 if( objc!=3 ){
6658 Tcl_AppendResult(interp, "wrong # args: should be \"",
6659 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
6660 return TCL_ERROR;
6662 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6663 return TCL_ERROR;
6665 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
6666 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
6667 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
6668 Tcl_AppendResult(interp, z, (char*)0);
6669 return TCL_OK;
6673 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG
6675 ** This TCL command runs the sqlite3_file_control interface with
6676 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
6678 static int SQLITE_TCLAPI file_control_powersafe_overwrite(
6679 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6680 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6681 int objc, /* Number of arguments */
6682 Tcl_Obj *CONST objv[] /* Command arguments */
6684 sqlite3 *db;
6685 int rc;
6686 int b;
6687 char z[100];
6689 if( objc!=3 ){
6690 Tcl_AppendResult(interp, "wrong # args: should be \"",
6691 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
6692 return TCL_ERROR;
6694 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6695 return TCL_ERROR;
6697 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
6698 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
6699 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
6700 Tcl_AppendResult(interp, z, (char*)0);
6701 return TCL_OK;
6706 ** tclcmd: file_control_vfsname DB ?AUXDB?
6708 ** Return a string that describes the stack of VFSes.
6710 static int SQLITE_TCLAPI file_control_vfsname(
6711 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6712 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6713 int objc, /* Number of arguments */
6714 Tcl_Obj *CONST objv[] /* Command arguments */
6716 sqlite3 *db;
6717 const char *zDbName = "main";
6718 char *zVfsName = 0;
6720 if( objc!=2 && objc!=3 ){
6721 Tcl_AppendResult(interp, "wrong # args: should be \"",
6722 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6723 return TCL_ERROR;
6725 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6726 return TCL_ERROR;
6728 if( objc==3 ){
6729 zDbName = Tcl_GetString(objv[2]);
6731 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
6732 Tcl_AppendResult(interp, zVfsName, (char*)0);
6733 sqlite3_free(zVfsName);
6734 return TCL_OK;
6738 ** tclcmd: file_control_reservebytes DB N
6740 static int SQLITE_TCLAPI file_control_reservebytes(
6741 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6742 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6743 int objc, /* Number of arguments */
6744 Tcl_Obj *CONST objv[] /* Command arguments */
6746 sqlite3 *db;
6747 const char *zDbName = "main";
6748 int n = 0;
6749 int rc;
6751 if( objc!=3 ){
6752 Tcl_WrongNumArgs(interp, 1, objv, "DB N");
6753 return TCL_ERROR;
6755 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6756 || Tcl_GetIntFromObj(interp, objv[2], &n)
6758 return TCL_ERROR;
6761 rc = sqlite3_file_control(db, zDbName, SQLITE_FCNTL_RESERVE_BYTES, (void*)&n);
6762 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
6763 return TCL_OK;
6768 ** tclcmd: file_control_tempfilename DB ?AUXDB?
6770 ** Return a string that is a temporary filename
6772 static int SQLITE_TCLAPI file_control_tempfilename(
6773 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6774 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6775 int objc, /* Number of arguments */
6776 Tcl_Obj *CONST objv[] /* Command arguments */
6778 sqlite3 *db;
6779 const char *zDbName = "main";
6780 char *zTName = 0;
6782 if( objc!=2 && objc!=3 ){
6783 Tcl_AppendResult(interp, "wrong # args: should be \"",
6784 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6785 return TCL_ERROR;
6787 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6788 return TCL_ERROR;
6790 if( objc==3 ){
6791 zDbName = Tcl_GetString(objv[2]);
6793 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
6794 Tcl_AppendResult(interp, zTName, (char*)0);
6795 sqlite3_free(zTName);
6796 return TCL_OK;
6800 ** tclcmd: file_control_external_reader DB ?AUXDB?
6802 ** Return a string that is a temporary filename
6804 static int SQLITE_TCLAPI file_control_external_reader(
6805 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6806 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6807 int objc, /* Number of arguments */
6808 Tcl_Obj *CONST objv[] /* Command arguments */
6810 sqlite3 *db;
6811 const char *zName = "main";
6812 int iRes = 0;
6813 int rc = SQLITE_OK;
6815 if( objc!=2 && objc!=3 ){
6816 Tcl_AppendResult(interp, "wrong # args: should be \"",
6817 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6818 return TCL_ERROR;
6820 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6821 return TCL_ERROR;
6823 if( objc==3 ){
6824 zName = Tcl_GetString(objv[2]);
6826 rc = sqlite3_file_control(db, zName, SQLITE_FCNTL_EXTERNAL_READER, &iRes);
6827 if( rc!=SQLITE_OK ){
6828 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6829 return TCL_ERROR;
6831 Tcl_SetObjResult(interp, Tcl_NewIntObj(iRes));
6832 return TCL_OK;
6837 ** tclcmd: sqlite3_vfs_list
6839 ** Return a tcl list containing the names of all registered vfs's.
6841 static int SQLITE_TCLAPI vfs_list(
6842 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6843 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6844 int objc, /* Number of arguments */
6845 Tcl_Obj *CONST objv[] /* Command arguments */
6847 sqlite3_vfs *pVfs;
6848 Tcl_Obj *pRet = Tcl_NewObj();
6849 if( objc!=1 ){
6850 Tcl_WrongNumArgs(interp, 1, objv, "");
6851 return TCL_ERROR;
6853 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
6854 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
6856 Tcl_SetObjResult(interp, pRet);
6857 return TCL_OK;
6861 ** tclcmd: sqlite3_limit DB ID VALUE
6863 ** This TCL command runs the sqlite3_limit interface and
6864 ** verifies correct operation of the same.
6866 static int SQLITE_TCLAPI test_limit(
6867 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6868 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6869 int objc, /* Number of arguments */
6870 Tcl_Obj *CONST objv[] /* Command arguments */
6872 sqlite3 *db;
6873 int rc;
6874 static const struct {
6875 char *zName;
6876 int id;
6877 } aId[] = {
6878 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
6879 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
6880 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
6881 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
6882 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
6883 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
6884 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
6885 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
6886 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
6887 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
6888 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
6889 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS },
6891 /* Out of range test cases */
6892 { "SQLITE_LIMIT_TOOSMALL", -1, },
6893 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 },
6895 int i, id = 0;
6896 int val;
6897 const char *zId;
6899 if( objc!=4 ){
6900 Tcl_AppendResult(interp, "wrong # args: should be \"",
6901 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
6902 return TCL_ERROR;
6904 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6905 zId = Tcl_GetString(objv[2]);
6906 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
6907 if( strcmp(zId, aId[i].zName)==0 ){
6908 id = aId[i].id;
6909 break;
6912 if( i>=sizeof(aId)/sizeof(aId[0]) ){
6913 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
6914 return TCL_ERROR;
6916 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
6917 rc = sqlite3_limit(db, id, val);
6918 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6919 return TCL_OK;
6923 ** tclcmd: save_prng_state
6925 ** Save the state of the pseudo-random number generator.
6926 ** At the same time, verify that sqlite3_test_control works even when
6927 ** called with an out-of-range opcode.
6929 static int SQLITE_TCLAPI save_prng_state(
6930 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6931 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6932 int objc, /* Number of arguments */
6933 Tcl_Obj *CONST objv[] /* Command arguments */
6935 int rc = sqlite3_test_control(9999);
6936 assert( rc==0 );
6937 rc = sqlite3_test_control(-1);
6938 assert( rc==0 );
6939 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
6940 return TCL_OK;
6943 ** tclcmd: restore_prng_state
6945 static int SQLITE_TCLAPI restore_prng_state(
6946 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6947 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6948 int objc, /* Number of arguments */
6949 Tcl_Obj *CONST objv[] /* Command arguments */
6951 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
6952 return TCL_OK;
6955 ** tclcmd: reset_prng_state
6957 static int SQLITE_TCLAPI reset_prng_state(
6958 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6959 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6960 int objc, /* Number of arguments */
6961 Tcl_Obj *CONST objv[] /* Command arguments */
6963 sqlite3_randomness(0,0);
6964 return TCL_OK;
6967 ** tclcmd: prng_seed INT ?DB?
6969 ** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB.
6970 ** INT is an integer. DB is a database connection, or a NULL pointer if
6971 ** omitted.
6973 ** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema
6974 ** cookie for DB, or to INT if the schema cookie happens to be zero.
6976 ** When INT!=0 and DB==0, set the PRNG seed to just INT.
6978 ** If INT==0 and DB==0 then use the default procedure of calling the
6979 ** xRandomness method on the default VFS to get the PRNG seed.
6981 static int SQLITE_TCLAPI prng_seed(
6982 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6983 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6984 int objc, /* Number of arguments */
6985 Tcl_Obj *CONST objv[] /* Command arguments */
6987 int i = 0;
6988 sqlite3 *db = 0;
6989 if( objc!=2 && objc!=3 ){
6990 Tcl_WrongNumArgs(interp, 1, objv, "SEED ?DB?");
6991 return TCL_ERROR;
6993 if( Tcl_GetIntFromObj(interp,objv[1],&i) ) return TCL_ERROR;
6994 if( objc==3 && getDbPointer(interp, Tcl_GetString(objv[2]), &db) ){
6995 return TCL_ERROR;
6997 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, i, db);
6998 return TCL_OK;
7002 ** tclcmd: extra_schema_checks BOOLEAN
7004 ** Enable or disable schema checks when parsing the sqlite_schema file.
7005 ** This is always enabled in production, but it is sometimes useful to
7006 ** disable the checks in order to make some internal error states reachable
7007 ** for testing.
7009 static int SQLITE_TCLAPI extra_schema_checks(
7010 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7011 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7012 int objc, /* Number of arguments */
7013 Tcl_Obj *CONST objv[] /* Command arguments */
7015 int i = 0;
7016 if( objc!=2 ){
7017 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
7018 return TCL_ERROR;
7020 if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7021 sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, i);
7022 return TCL_OK;
7026 ** tclcmd: database_may_be_corrupt
7028 ** Indicate that database files might be corrupt. In other words, set the normal
7029 ** state of operation.
7031 static int SQLITE_TCLAPI database_may_be_corrupt(
7032 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7033 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7034 int objc, /* Number of arguments */
7035 Tcl_Obj *CONST objv[] /* Command arguments */
7037 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0);
7038 return TCL_OK;
7041 ** tclcmd: database_never_corrupt
7043 ** Indicate that database files are always well-formed. This enables
7044 ** extra assert() statements that test conditions that are always true
7045 ** for well-formed databases.
7047 static int SQLITE_TCLAPI database_never_corrupt(
7048 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7049 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7050 int objc, /* Number of arguments */
7051 Tcl_Obj *CONST objv[] /* Command arguments */
7053 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1);
7054 return TCL_OK;
7058 ** tclcmd: pcache_stats
7060 static int SQLITE_TCLAPI test_pcache_stats(
7061 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7062 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7063 int objc, /* Number of arguments */
7064 Tcl_Obj *CONST objv[] /* Command arguments */
7066 int nMin;
7067 int nMax;
7068 int nCurrent;
7069 int nRecyclable;
7070 Tcl_Obj *pRet;
7072 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
7074 pRet = Tcl_NewObj();
7075 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
7076 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
7077 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
7078 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
7079 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
7080 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
7081 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
7082 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
7084 Tcl_SetObjResult(interp, pRet);
7086 return TCL_OK;
7089 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7090 static void test_unlock_notify_cb(void **aArg, int nArg){
7091 int ii;
7092 for(ii=0; ii<nArg; ii++){
7093 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
7096 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
7099 ** tclcmd: sqlite3_unlock_notify db
7101 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7102 static int SQLITE_TCLAPI test_unlock_notify(
7103 ClientData clientData, /* Unused */
7104 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7105 int objc, /* Number of arguments */
7106 Tcl_Obj *CONST objv[] /* Command arguments */
7108 sqlite3 *db;
7109 int rc;
7111 if( objc!=2 ){
7112 Tcl_WrongNumArgs(interp, 1, objv, "DB");
7113 return TCL_ERROR;
7116 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7117 return TCL_ERROR;
7119 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
7120 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7121 return TCL_OK;
7123 #endif
7126 ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
7128 static int SQLITE_TCLAPI test_wal_checkpoint(
7129 ClientData clientData, /* Unused */
7130 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7131 int objc, /* Number of arguments */
7132 Tcl_Obj *CONST objv[] /* Command arguments */
7134 char *zDb = 0;
7135 sqlite3 *db;
7136 int rc;
7138 if( objc!=3 && objc!=2 ){
7139 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
7140 return TCL_ERROR;
7143 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7144 return TCL_ERROR;
7146 if( objc==3 ){
7147 zDb = Tcl_GetString(objv[2]);
7149 rc = sqlite3_wal_checkpoint(db, zDb);
7150 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7151 return TCL_OK;
7155 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
7157 ** This command calls the wal_checkpoint_v2() function with the specified
7158 ** mode argument (passive, full or restart). If present, the database name
7159 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
7160 ** NAME argument is not present, a NULL pointer is passed instead.
7162 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
7163 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
7164 ** to the error message obtained from sqlite3_errmsg().
7166 ** Otherwise, this command returns a list of three integers. The first integer
7167 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
7168 ** are the values returned via the output parameters by wal_checkpoint_v2() -
7169 ** the number of frames in the log and the number of frames in the log
7170 ** that have been checkpointed.
7172 static int SQLITE_TCLAPI test_wal_checkpoint_v2(
7173 ClientData clientData, /* Unused */
7174 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7175 int objc, /* Number of arguments */
7176 Tcl_Obj *CONST objv[] /* Command arguments */
7178 char *zDb = 0;
7179 sqlite3 *db;
7180 int rc;
7182 int eMode;
7183 int nLog = -555;
7184 int nCkpt = -555;
7185 Tcl_Obj *pRet;
7187 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
7188 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
7189 assert( SQLITE_CHECKPOINT_FULL==1 );
7190 assert( SQLITE_CHECKPOINT_RESTART==2 );
7191 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
7193 if( objc!=3 && objc!=4 ){
7194 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
7195 return TCL_ERROR;
7198 if( objc==4 ){
7199 zDb = Tcl_GetString(objv[3]);
7201 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
7202 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
7203 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
7205 return TCL_ERROR;
7208 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
7209 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
7210 const char *zErrCode = sqlite3ErrName(rc);
7211 Tcl_ResetResult(interp);
7212 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
7213 return TCL_ERROR;
7216 pRet = Tcl_NewObj();
7217 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
7218 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
7219 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
7220 Tcl_SetObjResult(interp, pRet);
7222 return TCL_OK;
7226 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE
7228 static int SQLITE_TCLAPI test_wal_autocheckpoint(
7229 ClientData clientData, /* Unused */
7230 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7231 int objc, /* Number of arguments */
7232 Tcl_Obj *CONST objv[] /* Command arguments */
7234 sqlite3 *db;
7235 int rc;
7236 int iVal;
7239 if( objc!=3 ){
7240 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE");
7241 return TCL_ERROR;
7244 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
7245 || Tcl_GetIntFromObj(0, objv[2], &iVal)
7247 return TCL_ERROR;
7250 rc = sqlite3_wal_autocheckpoint(db, iVal);
7251 Tcl_ResetResult(interp);
7252 if( rc!=SQLITE_OK ){
7253 const char *zErrCode = sqlite3ErrName(rc);
7254 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1));
7255 return TCL_ERROR;
7258 return TCL_OK;
7263 ** tclcmd: test_sqlite3_log ?SCRIPT?
7265 static struct LogCallback {
7266 Tcl_Interp *pInterp;
7267 Tcl_Obj *pObj;
7268 } logcallback = {0, 0};
7269 static void xLogcallback(void *unused, int err, char *zMsg){
7270 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
7271 Tcl_IncrRefCount(pNew);
7272 Tcl_ListObjAppendElement(
7273 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
7275 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
7276 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
7277 Tcl_DecrRefCount(pNew);
7279 static int SQLITE_TCLAPI test_sqlite3_log(
7280 ClientData clientData,
7281 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7282 int objc, /* Number of arguments */
7283 Tcl_Obj *CONST objv[] /* Command arguments */
7285 if( objc>2 ){
7286 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
7287 return TCL_ERROR;
7289 if( logcallback.pObj ){
7290 Tcl_DecrRefCount(logcallback.pObj);
7291 logcallback.pObj = 0;
7292 logcallback.pInterp = 0;
7293 sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0);
7295 if( objc>1 ){
7296 logcallback.pObj = objv[1];
7297 Tcl_IncrRefCount(logcallback.pObj);
7298 logcallback.pInterp = interp;
7299 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0);
7301 return TCL_OK;
7305 ** tcl_objproc COMMANDNAME ARGS...
7307 ** Run a TCL command using its objProc interface. Throw an error if
7308 ** the command has no objProc interface.
7310 static int SQLITE_TCLAPI runAsObjProc(
7311 void * clientData,
7312 Tcl_Interp *interp,
7313 int objc,
7314 Tcl_Obj *CONST objv[]
7316 Tcl_CmdInfo cmdInfo;
7317 if( objc<2 ){
7318 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
7319 return TCL_ERROR;
7321 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
7322 Tcl_AppendResult(interp, "command not found: ",
7323 Tcl_GetString(objv[1]), (char*)0);
7324 return TCL_ERROR;
7326 if( cmdInfo.objProc==0 ){
7327 Tcl_AppendResult(interp, "command has no objProc: ",
7328 Tcl_GetString(objv[1]), (char*)0);
7329 return TCL_ERROR;
7331 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
7334 #ifndef SQLITE_OMIT_EXPLAIN
7336 ** WARNING: The following function, printExplainQueryPlan() is an exact
7337 ** copy of example code from eqp.in (eqp.html). If this code is modified,
7338 ** then the documentation copy needs to be modified as well.
7341 ** Argument pStmt is a prepared SQL statement. This function compiles
7342 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
7343 ** and prints the report to stdout using printf().
7345 int printExplainQueryPlan(sqlite3_stmt *pStmt){
7346 const char *zSql; /* Input SQL */
7347 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */
7348 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
7349 int rc; /* Return code from sqlite3_prepare_v2() */
7351 zSql = sqlite3_sql(pStmt);
7352 if( zSql==0 ) return SQLITE_ERROR;
7354 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
7355 if( zExplain==0 ) return SQLITE_NOMEM;
7357 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
7358 sqlite3_free(zExplain);
7359 if( rc!=SQLITE_OK ) return rc;
7361 while( SQLITE_ROW==sqlite3_step(pExplain) ){
7362 int iSelectid = sqlite3_column_int(pExplain, 0);
7363 int iOrder = sqlite3_column_int(pExplain, 1);
7364 int iFrom = sqlite3_column_int(pExplain, 2);
7365 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
7367 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
7370 return sqlite3_finalize(pExplain);
7373 static int SQLITE_TCLAPI test_print_eqp(
7374 void * clientData,
7375 Tcl_Interp *interp,
7376 int objc,
7377 Tcl_Obj *CONST objv[]
7379 int rc;
7380 sqlite3_stmt *pStmt;
7382 if( objc!=2 ){
7383 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
7384 return TCL_ERROR;
7386 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
7387 rc = printExplainQueryPlan(pStmt);
7388 /* This is needed on Windows so that a test case using this
7389 ** function can open a read pipe and get the output of
7390 ** printExplainQueryPlan() immediately.
7392 fflush(stdout);
7393 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
7394 return TCL_OK;
7396 #endif /* SQLITE_OMIT_EXPLAIN */
7398 #include <time.h>
7400 ** This is an alternative localtime_r() implementation used for testing
7401 ** the 'localtime' and 'utc' modifiers of date-time functions. Because
7402 ** the OS-supplied localtime_r() is locale-dependent, this alternative is
7403 ** provided as a stable test platform.
7405 ** Operation:
7407 ** (1) Localtime is 30 minutes earlier than (west of) UTC on
7408 ** even days (counting from 1970-01-01)
7410 ** (2) Localtime is 30 minutes later than (east of) UTC on odd days.
7412 ** (3) The function fails for the specific date/time value
7413 ** of 2000-05-29 14:16:00 in order to test the ability of
7414 ** SQLite to deal with localtime_r() failures.
7416 static int testLocaltime(const void *aliasT, void *aliasTM){
7417 const time_t t = *(const time_t*)aliasT;
7418 struct tm *pTm = (struct tm *)aliasTM;
7419 time_t altT;
7420 sqlite3_int64 iJD;
7421 int Z, A, B, C, D, E, X1, S;
7423 if( (t/86400) & 1 ){
7424 altT = t + 1800; /* 30 minutes later on odd days */
7425 }else{
7426 altT = t - 1800; /* 30 minutes earlier on even days */
7428 iJD = (sqlite3_int64)(altT + 210866760000);
7429 Z = (int)((iJD + 43200)/86400);
7430 A = (int)((Z - 1867216.25)/36524.25);
7431 A = Z + 1 + A - (A/4);
7432 B = A + 1524;
7433 C = (int)((B - 122.1)/365.25);
7434 D = (36525*(C&32767))/100;
7435 E = (int)((B-D)/30.6001);
7436 X1 = (int)(30.6001*E);
7437 pTm->tm_mday = B - D - X1;
7438 pTm->tm_mon = E<14 ? E-2 : E-14;
7439 pTm->tm_year = (pTm->tm_mon>1 ? C - 4716 : C - 4715) - 1900;
7440 S = (int)((iJD + 43200)%86400);
7441 pTm->tm_hour = S/3600;
7442 pTm->tm_min = (S/60)%60;
7443 pTm->tm_sec = S % 60;
7444 return t==959609760; /* Special case: 2000-05-29 14:16:00 fails */
7448 ** sqlite3_test_control VERB ARGS...
7450 static int SQLITE_TCLAPI test_test_control(
7451 void * clientData,
7452 Tcl_Interp *interp,
7453 int objc,
7454 Tcl_Obj *CONST objv[]
7456 struct Verb {
7457 const char *zName;
7458 int i;
7459 } aVerb[] = {
7460 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
7461 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP },
7462 { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER },
7463 { "SQLITE_TESTCTRL_INTERNAL_FUNCTIONS", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS},
7464 { 0, 0 }
7466 int iVerb;
7467 int iFlag;
7468 int rc;
7470 if( objc<2 ){
7471 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
7472 return TCL_ERROR;
7475 rc = Tcl_GetIndexFromObjStruct(
7476 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
7478 if( rc!=TCL_OK ) return rc;
7480 iFlag = aVerb[iVerb].i;
7481 switch( iFlag ){
7482 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
7483 sqlite3 *db = 0;
7484 if( objc!=3 ){
7485 Tcl_WrongNumArgs(interp, 2, objv, "DB");
7486 return TCL_ERROR;
7488 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7489 sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db);
7490 break;
7492 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
7493 int val;
7494 if( objc!=3 ){
7495 Tcl_WrongNumArgs(interp, 2, objv, "0|1|2");
7496 return TCL_ERROR;
7498 if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR;
7499 sqlite3_test_control(iFlag, val, testLocaltime);
7500 break;
7503 case SQLITE_TESTCTRL_SORTER_MMAP: {
7504 int val;
7505 sqlite3 *db;
7506 if( objc!=4 ){
7507 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT");
7508 return TCL_ERROR;
7510 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7511 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7512 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val);
7513 break;
7516 case SQLITE_TESTCTRL_IMPOSTER: {
7517 int onOff, tnum;
7518 const char *zDbName;
7519 sqlite3 *db;
7520 if( objc!=6 ){
7521 Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum");
7522 return TCL_ERROR;
7524 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7525 zDbName = Tcl_GetString(objv[3]);
7526 if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR;
7527 if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR;
7528 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum);
7529 break;
7533 Tcl_ResetResult(interp);
7534 return TCL_OK;
7537 #if SQLITE_OS_UNIX
7538 #include <sys/time.h>
7539 #include <sys/resource.h>
7541 static int SQLITE_TCLAPI test_getrusage(
7542 void * clientData,
7543 Tcl_Interp *interp,
7544 int objc,
7545 Tcl_Obj *CONST objv[]
7547 char buf[1024];
7548 struct rusage r;
7549 memset(&r, 0, sizeof(r));
7550 getrusage(RUSAGE_SELF, &r);
7552 sqlite3_snprintf(sizeof(buf), buf,
7553 "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
7554 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
7555 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
7556 (int)r.ru_minflt, (int)r.ru_majflt
7558 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
7559 return TCL_OK;
7561 #endif
7563 #if SQLITE_OS_WIN
7565 ** Information passed from the main thread into the windows file locker
7566 ** background thread.
7568 struct win32FileLocker {
7569 char *evName; /* Name of event to signal thread startup */
7570 HANDLE h; /* Handle of the file to be locked */
7571 int delay1; /* Delay before locking */
7572 int delay2; /* Delay before unlocking */
7573 int ok; /* Finished ok */
7574 int err; /* True if an error occurs */
7576 #endif
7579 #if SQLITE_OS_WIN
7580 #include <process.h>
7582 ** The background thread that does file locking.
7584 static void SQLITE_CDECL win32_file_locker(void *pAppData){
7585 struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
7586 if( p->evName ){
7587 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
7588 if ( ev ){
7589 SetEvent(ev);
7590 CloseHandle(ev);
7593 if( p->delay1 ) Sleep(p->delay1);
7594 if( LockFile(p->h, 0, 0, 100000000, 0) ){
7595 Sleep(p->delay2);
7596 UnlockFile(p->h, 0, 0, 100000000, 0);
7597 p->ok = 1;
7598 }else{
7599 p->err = 1;
7601 CloseHandle(p->h);
7602 p->h = 0;
7603 p->delay1 = 0;
7604 p->delay2 = 0;
7606 #endif
7608 #if SQLITE_OS_WIN
7610 ** lock_win32_file FILENAME DELAY1 DELAY2
7612 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
7613 ** Wait DELAY1 milliseconds before acquiring the lock.
7615 static int SQLITE_TCLAPI win32_file_lock(
7616 void * clientData,
7617 Tcl_Interp *interp,
7618 int objc,
7619 Tcl_Obj *CONST objv[]
7621 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
7622 const char *zFilename;
7623 char zBuf[200];
7624 int retry = 0;
7625 HANDLE ev;
7626 DWORD wResult;
7628 if( objc!=4 && objc!=1 ){
7629 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
7630 return TCL_ERROR;
7632 if( objc==1 ){
7633 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
7634 x.ok, x.err, x.delay1, x.delay2, x.h);
7635 Tcl_AppendResult(interp, zBuf, (char*)0);
7636 return TCL_OK;
7638 while( x.h && retry<30 ){
7639 retry++;
7640 Sleep(100);
7642 if( x.h ){
7643 Tcl_AppendResult(interp, "busy", (char*)0);
7644 return TCL_ERROR;
7646 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
7647 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
7648 zFilename = Tcl_GetString(objv[1]);
7649 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
7650 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
7651 FILE_ATTRIBUTE_NORMAL, 0);
7652 if( !x.h ){
7653 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
7654 return TCL_ERROR;
7656 ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
7657 if ( !ev ){
7658 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
7659 return TCL_ERROR;
7661 _beginthread(win32_file_locker, 0, (void*)&x);
7662 Sleep(0);
7663 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
7664 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
7665 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
7666 CloseHandle(ev);
7667 return TCL_ERROR;
7669 CloseHandle(ev);
7670 return TCL_OK;
7674 ** exists_win32_path PATH
7676 ** Returns non-zero if the specified path exists, whose fully qualified name
7677 ** may exceed 260 characters if it is prefixed with "\\?\".
7679 static int SQLITE_TCLAPI win32_exists_path(
7680 void *clientData,
7681 Tcl_Interp *interp,
7682 int objc,
7683 Tcl_Obj *CONST objv[]
7685 if( objc!=2 ){
7686 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
7687 return TCL_ERROR;
7689 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
7690 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
7691 return TCL_OK;
7695 ** find_win32_file PATTERN
7697 ** Returns a list of entries in a directory that match the specified pattern,
7698 ** whose fully qualified name may exceed 248 characters if it is prefixed with
7699 ** "\\?\".
7701 static int SQLITE_TCLAPI win32_find_file(
7702 void *clientData,
7703 Tcl_Interp *interp,
7704 int objc,
7705 Tcl_Obj *CONST objv[]
7707 HANDLE hFindFile = INVALID_HANDLE_VALUE;
7708 WIN32_FIND_DATAW findData;
7709 Tcl_Obj *listObj;
7710 DWORD lastErrno;
7711 if( objc!=2 ){
7712 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
7713 return TCL_ERROR;
7715 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
7716 if( hFindFile==INVALID_HANDLE_VALUE ){
7717 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7718 return TCL_ERROR;
7720 listObj = Tcl_NewObj();
7721 Tcl_IncrRefCount(listObj);
7722 do {
7723 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
7724 findData.cFileName, -1));
7725 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
7726 findData.dwFileAttributes));
7727 } while( FindNextFileW(hFindFile, &findData) );
7728 lastErrno = GetLastError();
7729 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
7730 FindClose(hFindFile);
7731 Tcl_DecrRefCount(listObj);
7732 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7733 return TCL_ERROR;
7735 FindClose(hFindFile);
7736 Tcl_SetObjResult(interp, listObj);
7737 return TCL_OK;
7741 ** delete_win32_file FILENAME
7743 ** Deletes the specified file, whose fully qualified name may exceed 260
7744 ** characters if it is prefixed with "\\?\".
7746 static int SQLITE_TCLAPI win32_delete_file(
7747 void *clientData,
7748 Tcl_Interp *interp,
7749 int objc,
7750 Tcl_Obj *CONST objv[]
7752 if( objc!=2 ){
7753 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
7754 return TCL_ERROR;
7756 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
7757 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7758 return TCL_ERROR;
7760 Tcl_ResetResult(interp);
7761 return TCL_OK;
7765 ** make_win32_dir DIRECTORY
7767 ** Creates the specified directory, whose fully qualified name may exceed 248
7768 ** characters if it is prefixed with "\\?\".
7770 static int SQLITE_TCLAPI win32_mkdir(
7771 void *clientData,
7772 Tcl_Interp *interp,
7773 int objc,
7774 Tcl_Obj *CONST objv[]
7776 if( objc!=2 ){
7777 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
7778 return TCL_ERROR;
7780 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
7781 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7782 return TCL_ERROR;
7784 Tcl_ResetResult(interp);
7785 return TCL_OK;
7789 ** remove_win32_dir DIRECTORY
7791 ** Removes the specified directory, whose fully qualified name may exceed 248
7792 ** characters if it is prefixed with "\\?\".
7794 static int SQLITE_TCLAPI win32_rmdir(
7795 void *clientData,
7796 Tcl_Interp *interp,
7797 int objc,
7798 Tcl_Obj *CONST objv[]
7800 if( objc!=2 ){
7801 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
7802 return TCL_ERROR;
7804 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
7805 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7806 return TCL_ERROR;
7808 Tcl_ResetResult(interp);
7809 return TCL_OK;
7811 #endif
7815 ** optimization_control DB OPT BOOLEAN
7817 ** Enable or disable query optimizations using the sqlite3_test_control()
7818 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
7819 ** OPT is the name of the optimization to be disabled. OPT can also be a
7820 ** list or optimizations names, in which case all optimizations named are
7821 ** enabled or disabled.
7823 ** Each invocation of this control overrides all prior invocations. The
7824 ** changes are not cumulative.
7826 static int SQLITE_TCLAPI optimization_control(
7827 void * clientData,
7828 Tcl_Interp *interp,
7829 int objc,
7830 Tcl_Obj *CONST objv[]
7832 int i;
7833 sqlite3 *db;
7834 const char *zOpt;
7835 int onoff;
7836 int mask = 0;
7837 int cnt = 0;
7838 static const struct {
7839 const char *zOptName;
7840 int mask;
7841 } aOpt[] = {
7842 { "all", SQLITE_AllOpts },
7843 { "none", 0 },
7844 { "query-flattener", SQLITE_QueryFlattener },
7845 { "groupby-order", SQLITE_GroupByOrder },
7846 { "factor-constants", SQLITE_FactorOutConst },
7847 { "distinct-opt", SQLITE_DistinctOpt },
7848 { "cover-idx-scan", SQLITE_CoverIdxScan },
7849 { "order-by-idx-join", SQLITE_OrderByIdxJoin },
7850 { "transitive", SQLITE_Transitive },
7851 { "omit-noop-join", SQLITE_OmitNoopJoin },
7852 { "stat4", SQLITE_Stat4 },
7853 { "skip-scan", SQLITE_SkipScan },
7854 { "push-down", SQLITE_PushDown },
7855 { "balanced-merge", SQLITE_BalancedMerge },
7856 { "propagate-const", SQLITE_PropagateConst },
7859 if( objc!=4 ){
7860 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
7861 return TCL_ERROR;
7863 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7864 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
7865 zOpt = Tcl_GetString(objv[2]);
7866 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
7867 if( strstr(zOpt, aOpt[i].zOptName)!=0 ){
7868 mask |= aOpt[i].mask;
7869 cnt++;
7872 if( onoff ) mask = ~mask;
7873 if( cnt==0 ){
7874 Tcl_AppendResult(interp, "unknown optimization - should be one of:",
7875 (char*)0);
7876 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
7877 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
7879 return TCL_ERROR;
7881 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
7882 Tcl_SetObjResult(interp, Tcl_NewIntObj(mask));
7883 return TCL_OK;
7887 ** load_static_extension DB NAME ...
7889 ** Load one or more statically linked extensions.
7891 static int SQLITE_TCLAPI tclLoadStaticExtensionCmd(
7892 void * clientData,
7893 Tcl_Interp *interp,
7894 int objc,
7895 Tcl_Obj *CONST objv[]
7897 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
7898 extern int sqlite3_appendvfs_init(sqlite3*,char**,const sqlite3_api_routines*);
7899 extern int sqlite3_basexx_init(sqlite3*,char**,const sqlite3_api_routines*);
7900 extern int sqlite3_carray_init(sqlite3*,char**,const sqlite3_api_routines*);
7901 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
7902 extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*);
7903 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
7904 extern int sqlite3_explain_init(sqlite3*,char**,const sqlite3_api_routines*);
7905 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
7906 extern int sqlite3_decimal_init(sqlite3*,char**,const sqlite3_api_routines*);
7907 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
7908 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
7909 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
7910 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
7911 #ifndef SQLITE_OMIT_VIRTUALTABLE
7912 extern int sqlite3_prefixes_init(sqlite3*,char**,const sqlite3_api_routines*);
7913 #endif
7914 extern int sqlite3_qpvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
7915 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
7916 extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*);
7917 extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
7918 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
7919 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
7920 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
7921 extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
7922 #ifdef SQLITE_HAVE_ZLIB
7923 extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*);
7924 #endif
7925 static const struct {
7926 const char *zExtName;
7927 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
7928 } aExtension[] = {
7929 { "amatch", sqlite3_amatch_init },
7930 { "appendvfs", sqlite3_appendvfs_init },
7931 { "basexx", sqlite3_basexx_init },
7932 { "carray", sqlite3_carray_init },
7933 { "closure", sqlite3_closure_init },
7934 { "csv", sqlite3_csv_init },
7935 { "decimal", sqlite3_decimal_init },
7936 { "eval", sqlite3_eval_init },
7937 { "explain", sqlite3_explain_init },
7938 { "fileio", sqlite3_fileio_init },
7939 { "fuzzer", sqlite3_fuzzer_init },
7940 { "ieee754", sqlite3_ieee_init },
7941 { "nextchar", sqlite3_nextchar_init },
7942 { "percentile", sqlite3_percentile_init },
7943 #ifndef SQLITE_OMIT_VIRTUALTABLE
7944 { "prefixes", sqlite3_prefixes_init },
7945 #endif
7946 { "qpvtab", sqlite3_qpvtab_init },
7947 { "regexp", sqlite3_regexp_init },
7948 { "remember", sqlite3_remember_init },
7949 { "series", sqlite3_series_init },
7950 { "spellfix", sqlite3_spellfix_init },
7951 { "totype", sqlite3_totype_init },
7952 { "unionvtab", sqlite3_unionvtab_init },
7953 { "wholenumber", sqlite3_wholenumber_init },
7954 #ifdef SQLITE_HAVE_ZLIB
7955 { "zipfile", sqlite3_zipfile_init },
7956 #endif
7958 sqlite3 *db;
7959 const char *zName;
7960 int i, j, rc;
7961 char *zErrMsg = 0;
7962 if( objc<3 ){
7963 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
7964 return TCL_ERROR;
7966 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7967 for(j=2; j<objc; j++){
7968 zName = Tcl_GetString(objv[j]);
7969 for(i=0; i<ArraySize(aExtension); i++){
7970 if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
7972 if( i>=ArraySize(aExtension) ){
7973 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
7974 return TCL_ERROR;
7976 if( aExtension[i].pInit ){
7977 rc = aExtension[i].pInit(db, &zErrMsg, 0);
7978 }else{
7979 rc = SQLITE_OK;
7981 if( (rc!=SQLITE_OK && rc!=SQLITE_OK_LOAD_PERMANENTLY) || zErrMsg ){
7982 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
7983 (char*)0);
7984 sqlite3_free(zErrMsg);
7985 return TCL_ERROR;
7988 return TCL_OK;
7992 ** sorter_test_fakeheap BOOL
7995 static int SQLITE_TCLAPI sorter_test_fakeheap(
7996 void * clientData,
7997 Tcl_Interp *interp,
7998 int objc,
7999 Tcl_Obj *CONST objv[]
8001 int bArg;
8002 if( objc!=2 ){
8003 Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
8004 return TCL_ERROR;
8007 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){
8008 return TCL_ERROR;
8011 if( bArg ){
8012 if( sqlite3GlobalConfig.pHeap==0 ){
8013 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1);
8015 }else{
8016 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){
8017 sqlite3GlobalConfig.pHeap = 0;
8021 Tcl_ResetResult(interp);
8022 return TCL_OK;
8026 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2
8028 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
8029 ** check that the leftmost and rightmost columns returned are both integers,
8030 ** and that both contain the same value.
8032 ** Then execute statement $SQL2. Check that the statement returns the same
8033 ** set of integers in the same order as in the previous step (using $SQL1).
8035 static int SQLITE_TCLAPI sorter_test_sort4_helper(
8036 void * clientData,
8037 Tcl_Interp *interp,
8038 int objc,
8039 Tcl_Obj *CONST objv[]
8041 const char *zSql1;
8042 const char *zSql2;
8043 int nStep;
8044 int iStep;
8045 unsigned int iCksum1 = 0;
8046 unsigned int iCksum2 = 0;
8047 int rc;
8048 int iB;
8049 sqlite3 *db;
8050 sqlite3_stmt *pStmt;
8052 if( objc!=5 ){
8053 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2");
8054 return TCL_ERROR;
8057 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8058 zSql1 = Tcl_GetString(objv[2]);
8059 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR;
8060 zSql2 = Tcl_GetString(objv[4]);
8062 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0);
8063 if( rc!=SQLITE_OK ) goto sql_error;
8065 iB = sqlite3_column_count(pStmt)-1;
8066 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){
8067 int a = sqlite3_column_int(pStmt, 0);
8068 if( a!=sqlite3_column_int(pStmt, iB) ){
8069 Tcl_AppendResult(interp, "data error: (a!=b)", 0);
8070 return TCL_ERROR;
8073 iCksum1 += (iCksum1 << 3) + (unsigned int)a;
8075 rc = sqlite3_finalize(pStmt);
8076 if( rc!=SQLITE_OK ) goto sql_error;
8078 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0);
8079 if( rc!=SQLITE_OK ) goto sql_error;
8080 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){
8081 int a = sqlite3_column_int(pStmt, 0);
8082 iCksum2 += (iCksum2 << 3) + (unsigned int)a;
8084 rc = sqlite3_finalize(pStmt);
8085 if( rc!=SQLITE_OK ) goto sql_error;
8087 if( iCksum1!=iCksum2 ){
8088 Tcl_AppendResult(interp, "checksum mismatch", 0);
8089 return TCL_ERROR;
8092 return TCL_OK;
8093 sql_error:
8094 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
8095 return TCL_ERROR;
8099 #ifdef SQLITE_USER_AUTHENTICATION
8100 #include "sqlite3userauth.h"
8102 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD
8104 static int SQLITE_TCLAPI test_user_authenticate(
8105 ClientData clientData, /* Unused */
8106 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8107 int objc, /* Number of arguments */
8108 Tcl_Obj *CONST objv[] /* Command arguments */
8110 char *zUser = 0;
8111 char *zPasswd = 0;
8112 int nPasswd = 0;
8113 sqlite3 *db;
8114 int rc;
8116 if( objc!=4 ){
8117 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
8118 return TCL_ERROR;
8120 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8121 return TCL_ERROR;
8123 zUser = Tcl_GetString(objv[2]);
8124 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8125 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
8126 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8127 return TCL_OK;
8129 #endif /* SQLITE_USER_AUTHENTICATION */
8131 #ifdef SQLITE_USER_AUTHENTICATION
8133 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN
8135 static int SQLITE_TCLAPI test_user_add(
8136 ClientData clientData, /* Unused */
8137 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8138 int objc, /* Number of arguments */
8139 Tcl_Obj *CONST objv[] /* Command arguments */
8141 char *zUser = 0;
8142 char *zPasswd = 0;
8143 int nPasswd = 0;
8144 int isAdmin = 0;
8145 sqlite3 *db;
8146 int rc;
8148 if( objc!=5 ){
8149 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
8150 return TCL_ERROR;
8152 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8153 return TCL_ERROR;
8155 zUser = Tcl_GetString(objv[2]);
8156 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8157 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
8158 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
8159 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8160 return TCL_OK;
8162 #endif /* SQLITE_USER_AUTHENTICATION */
8164 #ifdef SQLITE_USER_AUTHENTICATION
8166 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN
8168 static int SQLITE_TCLAPI test_user_change(
8169 ClientData clientData, /* Unused */
8170 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8171 int objc, /* Number of arguments */
8172 Tcl_Obj *CONST objv[] /* Command arguments */
8174 char *zUser = 0;
8175 char *zPasswd = 0;
8176 int nPasswd = 0;
8177 int isAdmin = 0;
8178 sqlite3 *db;
8179 int rc;
8181 if( objc!=5 ){
8182 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
8183 return TCL_ERROR;
8185 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8186 return TCL_ERROR;
8188 zUser = Tcl_GetString(objv[2]);
8189 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8190 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
8191 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
8192 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8193 return TCL_OK;
8195 #endif /* SQLITE_USER_AUTHENTICATION */
8197 #ifdef SQLITE_USER_AUTHENTICATION
8199 ** tclcmd: sqlite3_user_delete DB USERNAME
8201 static int SQLITE_TCLAPI test_user_delete(
8202 ClientData clientData, /* Unused */
8203 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8204 int objc, /* Number of arguments */
8205 Tcl_Obj *CONST objv[] /* Command arguments */
8207 char *zUser = 0;
8208 sqlite3 *db;
8209 int rc;
8211 if( objc!=3 ){
8212 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
8213 return TCL_ERROR;
8215 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8216 return TCL_ERROR;
8218 zUser = Tcl_GetString(objv[2]);
8219 rc = sqlite3_user_delete(db, zUser);
8220 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8221 return TCL_OK;
8223 #endif /* SQLITE_USER_AUTHENTICATION */
8226 ** tclcmd: bad_behavior TYPE
8228 ** Do some things that should trigger a valgrind or -fsanitize=undefined
8229 ** warning. This is used to verify that errors and warnings output by those
8230 ** tools are detected by the test scripts.
8232 ** TYPE BEHAVIOR
8233 ** 1 Overflow a signed integer
8234 ** 2 Jump based on an uninitialized variable
8235 ** 3 Read after free
8236 ** 4 Panic
8238 static int SQLITE_TCLAPI test_bad_behavior(
8239 ClientData clientData, /* Pointer to an integer containing zero */
8240 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8241 int objc, /* Number of arguments */
8242 Tcl_Obj *CONST objv[] /* Command arguments */
8244 int iType;
8245 int xyz;
8246 int i = *(int*)clientData;
8247 int j;
8248 int w[10];
8249 int *a;
8250 if( objc!=2 ){
8251 Tcl_WrongNumArgs(interp, 1, objv, "TYPE");
8252 return TCL_ERROR;
8254 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR;
8255 switch( iType ){
8256 case 1: {
8257 xyz = 0x7fffff00 - i;
8258 xyz += 0x100;
8259 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz));
8260 break;
8262 case 2: {
8263 w[1] = 5;
8264 if( w[i]>0 ) w[1]++;
8265 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1]));
8266 break;
8268 case 3: {
8269 a = malloc( sizeof(int)*10 );
8270 for(j=0; j<10; j++) a[j] = j;
8271 free(a);
8272 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i]));
8273 break;
8275 case 4: {
8276 Tcl_Panic("Deliberate panic");
8277 break;
8280 return TCL_OK;
8284 ** tclcmd: register_dbstat_vtab DB
8286 ** Cause the dbstat virtual table to be available on the connection DB
8288 static int SQLITE_TCLAPI test_register_dbstat_vtab(
8289 void *clientData,
8290 Tcl_Interp *interp,
8291 int objc,
8292 Tcl_Obj *CONST objv[]
8294 #ifdef SQLITE_OMIT_VIRTUALTABLE
8295 Tcl_AppendResult(interp, "dbstat not available because of "
8296 "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
8297 return TCL_ERROR;
8298 #else
8299 struct SqliteDb { sqlite3 *db; };
8300 char *zDb;
8301 Tcl_CmdInfo cmdInfo;
8303 if( objc!=2 ){
8304 Tcl_WrongNumArgs(interp, 1, objv, "DB");
8305 return TCL_ERROR;
8308 zDb = Tcl_GetString(objv[1]);
8309 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
8310 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
8311 sqlite3DbstatRegister(db);
8313 return TCL_OK;
8314 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8318 ** tclcmd: sqlite3_db_config DB SETTING VALUE
8320 ** Invoke sqlite3_db_config() for one of the setting values.
8322 static int SQLITE_TCLAPI test_sqlite3_db_config(
8323 void *clientData,
8324 Tcl_Interp *interp,
8325 int objc,
8326 Tcl_Obj *CONST objv[]
8328 static const struct {
8329 const char *zName;
8330 int eVal;
8331 } aSetting[] = {
8332 { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY },
8333 { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER },
8334 { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8335 { "LOAD_EXTENSION", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8336 { "NO_CKPT_ON_CLOSE", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
8337 { "QPSG", SQLITE_DBCONFIG_ENABLE_QPSG },
8338 { "TRIGGER_EQP", SQLITE_DBCONFIG_TRIGGER_EQP },
8339 { "RESET_DB", SQLITE_DBCONFIG_RESET_DATABASE },
8340 { "DEFENSIVE", SQLITE_DBCONFIG_DEFENSIVE },
8341 { "WRITABLE_SCHEMA", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
8342 { "LEGACY_ALTER_TABLE", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
8343 { "DQS_DML", SQLITE_DBCONFIG_DQS_DML },
8344 { "DQS_DDL", SQLITE_DBCONFIG_DQS_DDL },
8345 { "LEGACY_FILE_FORMAT", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
8346 { "STMT_SCANSTATUS", SQLITE_DBCONFIG_STMT_SCANSTATUS },
8348 int i;
8349 int v = 0;
8350 const char *zSetting;
8351 sqlite3 *db;
8353 if( objc!=4 && objc!=3 ){
8354 Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING [VALUE]");
8355 return TCL_ERROR;
8357 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8358 zSetting = Tcl_GetString(objv[2]);
8359 if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7;
8360 if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9;
8361 if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7;
8362 for(i=0; i<ArraySize(aSetting); i++){
8363 if( strcmp(zSetting, aSetting[i].zName)==0 ) break;
8365 if( i>=ArraySize(aSetting) ){
8366 Tcl_SetObjResult(interp,
8367 Tcl_NewStringObj("unknown sqlite3_db_config setting", -1));
8368 return TCL_ERROR;
8370 if( objc==4 ){
8371 if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR;
8372 }else{
8373 v = -1;
8375 sqlite3_db_config(db, aSetting[i].eVal, v, &v);
8376 Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
8377 return TCL_OK;
8380 ** tclcmd: sqlite3_txn_state DB ?SCHEMA?
8382 ** Invoke sqlite3_txn_state(DB,SCHEMA) and return the
8383 ** numeric value that results. Use NULL for SCHEMA if the 3 argument
8384 ** is omitted.
8386 static int SQLITE_TCLAPI test_sqlite3_txn_state(
8387 void *clientData,
8388 Tcl_Interp *interp,
8389 int objc,
8390 Tcl_Obj *CONST objv[]
8392 sqlite3 *db;
8393 const char *zSchema;
8394 int iTxn;
8396 if( objc!=2 && objc!=3 ){
8397 Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCHEMA?");
8398 return TCL_ERROR;
8400 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8401 zSchema = objc==3 ? Tcl_GetString(objv[2]) : 0;
8402 iTxn = sqlite3_txn_state(db, zSchema);
8403 Tcl_SetObjResult(interp, Tcl_NewIntObj(iTxn));
8404 return TCL_OK;
8408 ** Change the name of the main database schema from "main" to "icecube".
8410 static int SQLITE_TCLAPI test_dbconfig_maindbname_icecube(
8411 void * clientData,
8412 Tcl_Interp *interp,
8413 int objc,
8414 Tcl_Obj *CONST objv[]
8416 int rc;
8417 sqlite3 *db;
8418 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
8419 if( objc!=2 ){
8420 Tcl_WrongNumArgs(interp, 1, objv, "DB");
8421 return TCL_ERROR;
8422 }else{
8423 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8424 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "icecube");
8425 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
8426 return TCL_OK;
8431 ** Usage: sqlite3_mmap_warm DB DBNAME
8433 static int SQLITE_TCLAPI test_mmap_warm(
8434 void * clientData,
8435 Tcl_Interp *interp,
8436 int objc,
8437 Tcl_Obj *CONST objv[]
8439 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
8440 extern int sqlite3_mmap_warm(sqlite3 *db, const char *);
8442 if( objc!=2 && objc!=3 ){
8443 Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?");
8444 return TCL_ERROR;
8445 }else{
8446 int rc;
8447 sqlite3 *db;
8448 const char *zDb = 0;
8449 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8450 if( objc==3 ){
8451 zDb = Tcl_GetString(objv[2]);
8453 rc = sqlite3_mmap_warm(db, zDb);
8454 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
8455 return TCL_OK;
8460 ** Usage: test_write_db DB OFFSET DATA
8462 ** Obtain the sqlite3_file* object for the database file for the "main" db
8463 ** of handle DB. Then invoke its xWrite method to write data DATA to offset
8464 ** OFFSET.
8466 static int SQLITE_TCLAPI test_write_db(
8467 void * clientData,
8468 Tcl_Interp *interp,
8469 int objc,
8470 Tcl_Obj *CONST objv[]
8472 sqlite3 *db = 0;
8473 Tcl_WideInt iOff = 0;
8474 const unsigned char *aData = 0;
8475 int nData = 0;
8476 sqlite3_file *pFile = 0;
8477 int rc;
8479 if( objc!=4 ){
8480 Tcl_WrongNumArgs(interp, 1, objv, "DB OFFSET DATA");
8481 return TCL_ERROR;
8483 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8484 if( Tcl_GetWideIntFromObj(interp, objv[2], &iOff) ) return TCL_ERROR;
8485 aData = Tcl_GetByteArrayFromObj(objv[3], &nData);
8487 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFile);
8488 rc = pFile->pMethods->xWrite(pFile, aData, nData, iOff);
8490 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8491 return TCL_OK;
8495 ** Usage: sqlite3_register_cksumvfs
8498 static int SQLITE_TCLAPI test_register_cksumvfs(
8499 void * clientData,
8500 Tcl_Interp *interp,
8501 int objc,
8502 Tcl_Obj *CONST objv[]
8504 if( objc!=1 ){
8505 Tcl_WrongNumArgs(interp, 1, objv, "");
8506 return TCL_ERROR;
8507 }else{
8508 extern int sqlite3_register_cksumvfs(const char*);
8509 int rc = sqlite3_register_cksumvfs(0);
8510 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8512 return TCL_OK;
8516 ** Usage: sqlite3_unregister_cksumvfs
8519 static int SQLITE_TCLAPI test_unregister_cksumvfs(
8520 void * clientData,
8521 Tcl_Interp *interp,
8522 int objc,
8523 Tcl_Obj *CONST objv[]
8525 if( objc!=1 ){
8526 Tcl_WrongNumArgs(interp, 1, objv, "");
8527 return TCL_ERROR;
8528 }else{
8529 extern int sqlite3_unregister_cksumvfs(void);
8530 int rc = sqlite3_unregister_cksumvfs();
8531 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8533 return TCL_OK;
8537 ** Usage: decode_hexdb TEXT
8539 ** Example: db deserialize [decode_hexdb $output_of_dbtotxt]
8541 ** This routine returns a byte-array for an SQLite database file that
8542 ** is constructed from a text input which is the output of the "dbtotxt"
8543 ** utility.
8545 static int SQLITE_TCLAPI test_decode_hexdb(
8546 void * clientData,
8547 Tcl_Interp *interp,
8548 int objc,
8549 Tcl_Obj *CONST objv[]
8551 const char *zIn = 0;
8552 unsigned char *a = 0;
8553 int n = 0;
8554 int lineno = 0;
8555 int i, iNext;
8556 int iOffset = 0;
8557 int j, k;
8558 int rc;
8559 unsigned int x[16];
8560 if( objc!=2 ){
8561 Tcl_WrongNumArgs(interp, 1, objv, "HEXDB");
8562 return TCL_ERROR;
8564 zIn = Tcl_GetString(objv[1]);
8565 for(i=0; zIn[i]; i=iNext){
8566 lineno++;
8567 for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){}
8568 if( zIn[iNext]=='\n' ) iNext++;
8569 while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; }
8570 if( a==0 ){
8571 int pgsz;
8572 rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz);
8573 if( rc!=2 ) continue;
8574 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){
8575 Tcl_AppendResult(interp, "bad 'pagesize' field", (void*)0);
8576 return TCL_ERROR;
8578 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
8579 if( n<512 ){
8580 Tcl_AppendResult(interp, "bad 'size' field", (void*)0);
8581 return TCL_ERROR;
8583 a = malloc( n );
8584 if( a==0 ){
8585 Tcl_AppendResult(interp, "out of memory", (void*)0);
8586 return TCL_ERROR;
8588 memset(a, 0, n);
8589 continue;
8591 rc = sscanf(zIn+i, "| page %d offset %d", &j, &k);
8592 if( rc==2 ){
8593 iOffset = k;
8594 continue;
8596 rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
8597 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
8598 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
8599 if( rc==17 ){
8600 k = iOffset+j;
8601 if( k+16<=n ){
8602 int ii;
8603 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
8605 continue;
8608 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n));
8609 free(a);
8610 return TCL_OK;
8614 ** Client data for the autovacuum_pages callback.
8616 struct AutovacPageData {
8617 Tcl_Interp *interp;
8618 char *zScript;
8620 typedef struct AutovacPageData AutovacPageData;
8623 ** Callback functions for sqlite3_autovacuum_pages
8625 static unsigned int test_autovacuum_pages_callback(
8626 void *pClientData,
8627 const char *zSchema,
8628 unsigned int nFilePages,
8629 unsigned int nFreePages,
8630 unsigned int nBytePerPage
8632 AutovacPageData *pData = (AutovacPageData*)pClientData;
8633 Tcl_DString str;
8634 unsigned int x;
8635 char zBuf[100];
8636 Tcl_DStringInit(&str);
8637 Tcl_DStringAppend(&str, pData->zScript, -1);
8638 Tcl_DStringAppendElement(&str, zSchema);
8639 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFilePages);
8640 Tcl_DStringAppendElement(&str, zBuf);
8641 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFreePages);
8642 Tcl_DStringAppendElement(&str, zBuf);
8643 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nBytePerPage);
8644 Tcl_DStringAppendElement(&str, zBuf);
8645 Tcl_ResetResult(pData->interp);
8646 Tcl_Eval(pData->interp, Tcl_DStringValue(&str));
8647 Tcl_DStringFree(&str);
8648 x = nFreePages;
8649 (void)Tcl_GetIntFromObj(0, Tcl_GetObjResult(pData->interp), (int*)&x);
8650 return x;
8654 ** Usage: sqlite3_autovacuum_pages DB SCRIPT
8656 ** Add an autovacuum-pages callback to database connection DB. The callback
8657 ** will invoke SCRIPT, after appending parameters.
8659 ** If SCRIPT is an empty string or is omitted, then the callback is
8660 ** cancelled.
8662 static int SQLITE_TCLAPI test_autovacuum_pages(
8663 void * clientData,
8664 Tcl_Interp *interp,
8665 int objc,
8666 Tcl_Obj *CONST objv[]
8668 AutovacPageData *pData;
8669 sqlite3 *db;
8670 int rc;
8671 const char *zScript;
8672 if( objc!=2 && objc!=3 ){
8673 Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCRIPT?");
8674 return TCL_ERROR;
8676 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8677 zScript = objc==3 ? Tcl_GetString(objv[2]) : 0;
8678 if( zScript ){
8679 size_t nScript = strlen(zScript);
8680 pData = sqlite3_malloc64( sizeof(*pData) + nScript + 1 );
8681 if( pData==0 ){
8682 Tcl_AppendResult(interp, "out of memory", (void*)0);
8683 return TCL_ERROR;
8685 pData->interp = interp;
8686 pData->zScript = (char*)&pData[1];
8687 memcpy(pData->zScript, zScript, nScript+1);
8688 rc = sqlite3_autovacuum_pages(db,test_autovacuum_pages_callback,
8689 pData, sqlite3_free);
8690 }else{
8691 rc = sqlite3_autovacuum_pages(db, 0, 0, 0);
8693 if( rc ){
8694 char zBuf[1000];
8695 sqlite3_snprintf(sizeof(zBuf), zBuf,
8696 "sqlite3_autovacuum_pages() returns %d", rc);
8697 Tcl_AppendResult(interp, zBuf, (void*)0);
8698 return TCL_ERROR;
8700 return TCL_OK;
8704 ** Usage: number_of_cores
8706 ** Return a guess at the number of available cores available on the
8707 ** processor on which this process is running.
8709 static int SQLITE_TCLAPI guess_number_of_cores(
8710 void * clientData,
8711 Tcl_Interp *interp,
8712 int objc,
8713 Tcl_Obj *CONST objv[]
8715 unsigned int nCore = 1;
8716 #if SQLITE_OS_WIN
8717 SYSTEM_INFO sysinfo;
8718 GetSystemInfo(&sysinfo);
8719 nCore = (unsigned int)sysinfo.dwNumberOfProcessors;
8720 #elif defined(__APPLE__)
8721 int nm[2];
8722 size_t len = 4;
8723 nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
8724 sysctl(nm, 2, &nCore, &len, NULL, 0);
8725 if( nCore<1 ){
8726 nm[1] = HW_NCPU;
8727 sysctl(nm, 2, &nCore, &len, NULL, 0);
8729 #else
8730 nCore = sysconf(_SC_NPROCESSORS_ONLN);
8731 #endif
8732 if( nCore<=0 ) nCore = 1;
8733 Tcl_SetObjResult(interp, Tcl_NewIntObj((int)nCore));
8734 return SQLITE_OK;
8739 ** Register commands with the TCL interpreter.
8741 int Sqlitetest1_Init(Tcl_Interp *interp){
8742 extern int sqlite3_search_count;
8743 extern int sqlite3_found_count;
8744 extern int sqlite3_interrupt_count;
8745 extern int sqlite3_open_file_count;
8746 extern int sqlite3_sort_count;
8747 extern int sqlite3_current_time;
8748 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
8749 extern int sqlite3_hostid_num;
8750 #endif
8751 extern int sqlite3_max_blobsize;
8752 extern int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport(void*,
8753 Tcl_Interp*,int,Tcl_Obj*CONST*);
8754 static int iZero = 0;
8755 static struct {
8756 char *zName;
8757 Tcl_CmdProc *xProc;
8758 } aCmd[] = {
8759 { "db_enter", (Tcl_CmdProc*)db_enter },
8760 { "db_leave", (Tcl_CmdProc*)db_leave },
8761 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
8762 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
8763 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
8764 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
8765 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
8766 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
8767 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
8768 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
8769 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
8770 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
8771 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
8772 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
8773 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
8774 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
8775 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
8776 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
8777 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
8778 #ifndef SQLITE_OMIT_GET_TABLE
8779 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
8780 #endif
8781 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
8782 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 },
8783 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
8784 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
8785 { "sqlite3_drop_modules", (Tcl_CmdProc*)test_drop_modules },
8786 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
8787 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
8788 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
8789 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
8790 { "sqlite3_key", (Tcl_CmdProc*)test_key },
8791 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
8792 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
8793 { "sqlite3_is_interrupted", (Tcl_CmdProc*)test_is_interrupted },
8794 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
8795 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
8796 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
8797 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
8798 { "printf", (Tcl_CmdProc*)test_printf },
8799 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
8800 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address },
8802 static struct {
8803 char *zName;
8804 Tcl_ObjCmdProc *xProc;
8805 void *clientData;
8806 } aObjCmd[] = {
8807 { "sqlite3_db_config", test_sqlite3_db_config, 0 },
8808 { "sqlite3_txn_state", test_sqlite3_txn_state, 0 },
8809 { "bad_behavior", test_bad_behavior, (void*)&iZero },
8810 { "register_dbstat_vtab", test_register_dbstat_vtab },
8811 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
8812 { "intarray_addr", test_intarray_addr, 0 },
8813 { "int64array_addr", test_int64array_addr, 0 },
8814 { "doublearray_addr", test_doublearray_addr, 0 },
8815 { "textarray_addr", test_textarray_addr, 0 },
8816 { "sqlite3_bind_int", test_bind_int, 0 },
8817 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
8818 { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 },
8819 { "sqlite3_bind_int64", test_bind_int64, 0 },
8820 { "sqlite3_bind_double", test_bind_double, 0 },
8821 { "sqlite3_bind_null", test_bind_null ,0 },
8822 { "sqlite3_bind_text", test_bind_text ,0 },
8823 { "sqlite3_bind_text16", test_bind_text16 ,0 },
8824 { "sqlite3_bind_blob", test_bind_blob ,0 },
8825 { "sqlite3_bind_value_from_select",test_bind_value_from_select ,0 },
8826 { "sqlite3_bind_value_from_preupdate",test_bind_value_from_preupdate ,0 },
8827 #ifndef SQLITE_OMIT_VIRTUALTABLE
8828 { "sqlite3_carray_bind", test_carray_bind ,0 },
8829 #endif
8830 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
8831 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
8832 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
8833 { "sqlite3_clear_bindings", test_clear_bindings, 0},
8834 { "sqlite3_sleep", test_sleep, 0},
8835 { "sqlite3_errcode", test_errcode ,0 },
8836 { "sqlite3_extended_errcode", test_ex_errcode ,0 },
8837 { "sqlite3_errmsg", test_errmsg ,0 },
8838 { "sqlite3_error_offset", test_error_offset ,0 },
8839 { "sqlite3_errmsg16", test_errmsg16 ,0 },
8840 { "sqlite3_open", test_open ,0 },
8841 { "sqlite3_open16", test_open16 ,0 },
8842 { "sqlite3_open_v2", test_open_v2 ,0 },
8843 { "sqlite3_complete16", test_complete16 ,0 },
8844 { "sqlite3_normalize", test_normalize ,0 },
8846 { "sqlite3_prepare", test_prepare ,0 },
8847 { "sqlite3_prepare16", test_prepare16 ,0 },
8848 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
8849 { "sqlite3_prepare_v3", test_prepare_v3 ,0 },
8850 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
8851 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
8852 { "sqlite3_finalize", test_finalize ,0 },
8853 { "sqlite3_stmt_status", test_stmt_status ,0 },
8854 { "sqlite3_reset", test_reset ,0 },
8855 { "sqlite3_expired", test_expired ,0 },
8856 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
8857 { "sqlite3_changes", test_changes ,0 },
8858 { "sqlite3_step", test_step ,0 },
8859 { "sqlite3_sql", test_sql ,0 },
8860 { "sqlite3_expanded_sql", test_ex_sql ,0 },
8861 #ifdef SQLITE_ENABLE_NORMALIZE
8862 { "sqlite3_normalized_sql", test_norm_sql ,0 },
8863 #endif
8864 { "sqlite3_next_stmt", test_next_stmt ,0 },
8865 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 },
8866 { "sqlite3_stmt_isexplain", test_stmt_isexplain,0 },
8867 { "sqlite3_stmt_busy", test_stmt_busy ,0 },
8868 { "uses_stmt_journal", uses_stmt_journal ,0 },
8870 { "sqlite3_release_memory", test_release_memory, 0},
8871 { "sqlite3_db_release_memory", test_db_release_memory, 0},
8872 { "sqlite3_db_cacheflush", test_db_cacheflush, 0},
8873 { "sqlite3_system_errno", test_system_errno, 0},
8874 { "sqlite3_db_filename", test_db_filename, 0},
8875 { "sqlite3_db_readonly", test_db_readonly, 0},
8876 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
8877 { "sqlite3_soft_heap_limit64", test_soft_heap_limit, 0},
8878 { "sqlite3_hard_heap_limit64", test_hard_heap_limit, 0},
8879 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
8880 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
8882 { "sqlite3_load_extension", test_load_extension, 0},
8883 { "sqlite3_enable_load_extension", test_enable_load, 0},
8884 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
8885 { "sqlite3_limit", test_limit, 0},
8886 { "dbconfig_maindbname_icecube", test_dbconfig_maindbname_icecube },
8888 { "save_prng_state", save_prng_state, 0 },
8889 { "restore_prng_state", restore_prng_state, 0 },
8890 { "reset_prng_state", reset_prng_state, 0 },
8891 { "prng_seed", prng_seed, 0 },
8892 { "extra_schema_checks", extra_schema_checks, 0},
8893 { "database_never_corrupt", database_never_corrupt, 0},
8894 { "database_may_be_corrupt", database_may_be_corrupt, 0},
8895 { "optimization_control", optimization_control,0},
8896 #if SQLITE_OS_WIN
8897 { "lock_win32_file", win32_file_lock, 0 },
8898 { "exists_win32_path", win32_exists_path, 0 },
8899 { "find_win32_file", win32_find_file, 0 },
8900 { "delete_win32_file", win32_delete_file, 0 },
8901 { "make_win32_dir", win32_mkdir, 0 },
8902 { "remove_win32_dir", win32_rmdir, 0 },
8903 #endif
8904 { "tcl_objproc", runAsObjProc, 0 },
8906 /* sqlite3_column_*() API */
8907 { "sqlite3_column_count", test_column_count ,0 },
8908 { "sqlite3_data_count", test_data_count ,0 },
8909 { "sqlite3_column_type", test_column_type ,0 },
8910 { "sqlite3_column_blob", test_column_blob ,0 },
8911 { "sqlite3_column_double", test_column_double ,0 },
8912 { "sqlite3_column_int64", test_column_int64 ,0 },
8913 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
8914 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
8915 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
8916 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
8917 #ifndef SQLITE_OMIT_DECLTYPE
8918 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
8919 #endif
8920 #ifdef SQLITE_ENABLE_COLUMN_METADATA
8921 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
8922 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
8923 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
8924 #endif
8926 #ifndef SQLITE_OMIT_UTF16
8927 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
8928 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
8929 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
8930 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
8931 #ifndef SQLITE_OMIT_DECLTYPE
8932 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
8933 #endif
8934 #ifdef SQLITE_ENABLE_COLUMN_METADATA
8935 {"sqlite3_column_database_name16",
8936 test_stmt_utf16, (void*)sqlite3_column_database_name16},
8937 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
8938 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
8939 #endif
8940 #endif
8941 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
8942 { "sqlite3_global_recover", test_global_recover, 0 },
8943 { "working_64bit_int", working_64bit_int, 0 },
8944 { "vfs_unlink_test", vfs_unlink_test, 0 },
8945 { "vfs_initfail_test", vfs_initfail_test, 0 },
8946 { "vfs_unregister_all", vfs_unregister_all, 0 },
8947 { "vfs_reregister_all", vfs_reregister_all, 0 },
8948 { "file_control_test", file_control_test, 0 },
8949 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
8950 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
8951 { "file_control_chunksize_test", file_control_chunksize_test, 0 },
8952 { "file_control_sizehint_test", file_control_sizehint_test, 0 },
8953 { "file_control_data_version", file_control_data_version, 0 },
8954 #if SQLITE_OS_WIN
8955 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 },
8956 { "file_control_win32_get_handle", file_control_win32_get_handle, 0 },
8957 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 },
8958 #endif
8959 { "file_control_persist_wal", file_control_persist_wal, 0 },
8960 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
8961 { "file_control_vfsname", file_control_vfsname, 0 },
8962 { "file_control_reservebytes", file_control_reservebytes, 0 },
8963 { "file_control_tempfilename", file_control_tempfilename, 0 },
8964 { "file_control_external_reader", file_control_external_reader, 0 },
8965 { "sqlite3_vfs_list", vfs_list, 0 },
8966 { "sqlite3_create_function_v2", test_create_function_v2, 0 },
8968 /* Functions from os.h */
8969 #ifndef SQLITE_OMIT_UTF16
8970 { "add_test_collate", test_collate, 0 },
8971 { "add_test_collate_needed", test_collate_needed, 0 },
8972 { "add_test_function", test_function, 0 },
8973 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 },
8974 #endif
8975 { "sqlite3_test_errstr", test_errstr, 0 },
8976 { "tcl_variable_type", tcl_variable_type, 0 },
8977 #ifndef SQLITE_OMIT_SHARED_CACHE
8978 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
8979 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
8980 #endif
8981 { "sqlite3_libversion_number", test_libversion_number, 0 },
8982 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
8983 #ifndef SQLITE_OMIT_INCRBLOB
8984 { "sqlite3_blob_reopen", test_blob_reopen, 0 },
8985 #endif
8986 { "pcache_stats", test_pcache_stats, 0 },
8987 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
8988 { "sqlite3_unlock_notify", test_unlock_notify, 0 },
8989 #endif
8990 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
8991 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 },
8992 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 },
8993 { "test_sqlite3_log", test_sqlite3_log, 0 },
8994 #ifndef SQLITE_OMIT_EXPLAIN
8995 { "print_explain_query_plan", test_print_eqp, 0 },
8996 #endif
8997 { "sqlite3_test_control", test_test_control },
8998 #if SQLITE_OS_UNIX
8999 { "getrusage", test_getrusage },
9000 #endif
9001 { "load_static_extension", tclLoadStaticExtensionCmd },
9002 { "sorter_test_fakeheap", sorter_test_fakeheap },
9003 { "sorter_test_sort4_helper", sorter_test_sort4_helper },
9004 #ifdef SQLITE_USER_AUTHENTICATION
9005 { "sqlite3_user_authenticate", test_user_authenticate, 0 },
9006 { "sqlite3_user_add", test_user_add, 0 },
9007 { "sqlite3_user_change", test_user_change, 0 },
9008 { "sqlite3_user_delete", test_user_delete, 0 },
9009 #endif
9010 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
9011 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 },
9012 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 },
9013 #endif
9014 #ifdef SQLITE_ENABLE_SQLLOG
9015 { "sqlite3_config_sqllog", test_config_sqllog, 0 },
9016 #endif
9017 { "vfs_current_time_int64", vfsCurrentTimeInt64, 0 },
9018 #ifdef SQLITE_ENABLE_SNAPSHOT
9019 { "sqlite3_snapshot_get", test_snapshot_get, 0 },
9020 { "sqlite3_snapshot_open", test_snapshot_open, 0 },
9021 { "sqlite3_snapshot_free", test_snapshot_free, 0 },
9022 { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 },
9023 { "sqlite3_snapshot_recover", test_snapshot_recover, 0 },
9024 { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 },
9025 { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 },
9026 { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 },
9027 #endif
9028 { "sqlite3_delete_database", test_delete_database, 0 },
9029 { "atomic_batch_write", test_atomic_batch_write, 0 },
9030 { "sqlite3_mmap_warm", test_mmap_warm, 0 },
9031 { "sqlite3_config_sorterref", test_config_sorterref, 0 },
9032 { "sqlite3_autovacuum_pages", test_autovacuum_pages, 0 },
9033 { "decode_hexdb", test_decode_hexdb, 0 },
9034 { "test_write_db", test_write_db, 0 },
9035 { "sqlite3_register_cksumvfs", test_register_cksumvfs, 0 },
9036 { "sqlite3_unregister_cksumvfs", test_unregister_cksumvfs, 0 },
9037 { "number_of_cores", guess_number_of_cores, 0 },
9038 #ifndef SQLITE_OMIT_VIRTUALTABLE
9039 { "create_null_module", test_create_null_module, 0 },
9040 #endif
9042 static int bitmask_size = sizeof(Bitmask)*8;
9043 static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
9044 int i;
9045 extern int sqlite3_sync_count, sqlite3_fullsync_count;
9046 extern int sqlite3_opentemp_count;
9047 extern int sqlite3_like_count;
9048 extern int sqlite3_xferopt_count;
9049 extern int sqlite3_pager_readdb_count;
9050 extern int sqlite3_pager_writedb_count;
9051 extern int sqlite3_pager_writej_count;
9052 #if SQLITE_OS_WIN
9053 extern LONG volatile sqlite3_os_type;
9054 #endif
9055 #ifdef SQLITE_DEBUG
9056 extern u32 sqlite3WhereTrace;
9057 extern int sqlite3OSTrace;
9058 extern int sqlite3WalTrace;
9059 #endif
9060 #ifdef SQLITE_TEST
9061 #ifdef SQLITE_ENABLE_FTS3
9062 extern int sqlite3_fts3_enable_parentheses;
9063 #endif
9064 #endif
9066 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
9067 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
9069 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
9070 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
9071 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
9073 Tcl_LinkVar(interp, "sqlite_search_count",
9074 (char*)&sqlite3_search_count, TCL_LINK_INT);
9075 Tcl_LinkVar(interp, "sqlite_found_count",
9076 (char*)&sqlite3_found_count, TCL_LINK_INT);
9077 Tcl_LinkVar(interp, "sqlite_sort_count",
9078 (char*)&sqlite3_sort_count, TCL_LINK_INT);
9079 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
9080 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
9081 Tcl_LinkVar(interp, "sqlite_like_count",
9082 (char*)&sqlite3_like_count, TCL_LINK_INT);
9083 Tcl_LinkVar(interp, "sqlite_interrupt_count",
9084 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
9085 Tcl_LinkVar(interp, "sqlite_open_file_count",
9086 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
9087 Tcl_LinkVar(interp, "sqlite_current_time",
9088 (char*)&sqlite3_current_time, TCL_LINK_INT);
9089 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
9090 Tcl_LinkVar(interp, "sqlite_hostid_num",
9091 (char*)&sqlite3_hostid_num, TCL_LINK_INT);
9092 #endif
9093 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
9094 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
9095 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
9096 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
9097 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
9098 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
9099 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
9100 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
9101 #ifndef SQLITE_OMIT_UTF16
9102 Tcl_LinkVar(interp, "unaligned_string_counter",
9103 (char*)&unaligned_string_counter, TCL_LINK_INT);
9104 #endif
9105 #ifndef SQLITE_OMIT_UTF16
9106 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
9107 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
9108 #endif
9109 #if SQLITE_OS_WIN
9110 Tcl_LinkVar(interp, "sqlite_os_type",
9111 (char*)&sqlite3_os_type, TCL_LINK_LONG);
9112 #endif
9113 #ifdef SQLITE_TEST
9115 static const char *query_plan = "*** OBSOLETE VARIABLE ***";
9116 Tcl_LinkVar(interp, "sqlite_query_plan",
9117 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
9119 #endif
9120 #ifdef SQLITE_DEBUG
9121 Tcl_LinkVar(interp, "sqlite_where_trace",
9122 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
9123 Tcl_LinkVar(interp, "sqlite_os_trace",
9124 (char*)&sqlite3OSTrace, TCL_LINK_INT);
9125 #ifndef SQLITE_OMIT_WAL
9126 Tcl_LinkVar(interp, "sqlite_wal_trace",
9127 (char*)&sqlite3WalTrace, TCL_LINK_INT);
9128 #endif
9129 #endif
9130 #ifndef SQLITE_OMIT_DISKIO
9131 Tcl_LinkVar(interp, "sqlite_opentemp_count",
9132 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
9133 #endif
9134 Tcl_LinkVar(interp, "sqlite_static_bind_value",
9135 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
9136 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
9137 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
9138 Tcl_LinkVar(interp, "sqlite_temp_directory",
9139 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
9140 Tcl_LinkVar(interp, "sqlite_data_directory",
9141 (char*)&sqlite3_data_directory, TCL_LINK_STRING);
9142 Tcl_LinkVar(interp, "bitmask_size",
9143 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
9144 Tcl_LinkVar(interp, "longdouble_size",
9145 (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
9146 Tcl_LinkVar(interp, "sqlite_sync_count",
9147 (char*)&sqlite3_sync_count, TCL_LINK_INT);
9148 Tcl_LinkVar(interp, "sqlite_fullsync_count",
9149 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
9150 #if defined(SQLITE_ENABLE_TREETRACE)
9151 Tcl_LinkVar(interp, "sqlite3_unsupported_treetrace",
9152 (char*)&sqlite3TreeTrace, TCL_LINK_INT);
9153 #endif
9154 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
9155 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
9156 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
9157 #endif
9158 return TCL_OK;