Replaces use of deprecated WINAPI_FAMILY_APP macro with WINAPI_FAMILY_PC_APP
[sqlcipher.git] / src / test1.c
bloba91653626395e84f154cb3b6d8f63609e93c0663
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 ** These SQL functions attempt to return a value (their first argument)
1024 ** that has been modified to have multiple datatypes. For example both
1025 ** TEXT and INTEGER.
1027 static void addTextTypeFunction(
1028 sqlite3_context *context,
1029 int argc,
1030 sqlite3_value **argv
1032 (void)sqlite3_value_text(argv[0]);
1033 (void)argc;
1034 sqlite3_result_value(context, argv[0]);
1036 static void addIntTypeFunction(
1037 sqlite3_context *context,
1038 int argc,
1039 sqlite3_value **argv
1041 (void)sqlite3_value_int64(argv[0]);
1042 (void)argc;
1043 sqlite3_result_value(context, argv[0]);
1045 static void addRealTypeFunction(
1046 sqlite3_context *context,
1047 int argc,
1048 sqlite3_value **argv
1050 (void)sqlite3_value_double(argv[0]);
1051 (void)argc;
1052 sqlite3_result_value(context, argv[0]);
1056 ** SQL function: strtod(X)
1058 ** Use the C-library strtod() function to convert string X into a double.
1059 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1060 ** routines against the C-library.
1062 static void shellStrtod(
1063 sqlite3_context *pCtx,
1064 int nVal,
1065 sqlite3_value **apVal
1067 char *z = (char*)sqlite3_value_text(apVal[0]);
1068 UNUSED_PARAMETER(nVal);
1069 if( z==0 ) return;
1070 sqlite3_result_double(pCtx, strtod(z,0));
1074 ** SQL function: dtostr(X)
1076 ** Use the C-library printf() function to convert real value X into a string.
1077 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1078 ** routines against the C-library.
1080 static void shellDtostr(
1081 sqlite3_context *pCtx,
1082 int nVal,
1083 sqlite3_value **apVal
1085 double r = sqlite3_value_double(apVal[0]);
1086 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1087 char z[400];
1088 if( n<1 ) n = 1;
1089 if( n>350 ) n = 350;
1090 sprintf(z, "%#+.*e", n, r);
1091 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1096 ** Usage: sqlite3_create_function DB
1098 ** Call the sqlite3_create_function API on the given database in order
1099 ** to create a function named "x_coalesce". This function does the same thing
1100 ** as the "coalesce" function. This function also registers an SQL function
1101 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
1102 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
1103 ** The effect is similar to trying to use the same database connection from
1104 ** two threads at the same time.
1106 ** The original motivation for this routine was to be able to call the
1107 ** sqlite3_create_function function while a query is in progress in order
1108 ** to test the SQLITE_MISUSE detection logic.
1110 static int SQLITE_TCLAPI test_create_function(
1111 void *NotUsed,
1112 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1113 int argc, /* Number of arguments */
1114 char **argv /* Text of each argument */
1116 int rc;
1117 sqlite3 *db;
1119 if( argc!=2 ){
1120 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1121 " DB\"", 0);
1122 return TCL_ERROR;
1124 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1125 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0,
1126 t1_ifnullFunc, 0, 0);
1127 if( rc==SQLITE_OK ){
1128 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1129 0, hex8Func, 0, 0);
1131 #ifndef SQLITE_OMIT_UTF16
1132 if( rc==SQLITE_OK ){
1133 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
1134 0, hex16Func, 0, 0);
1136 #endif
1137 if( rc==SQLITE_OK ){
1138 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
1139 tkt2213Function, 0, 0);
1141 if( rc==SQLITE_OK ){
1142 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
1143 ptrChngFunction, 0, 0);
1146 /* Functions counter1() and counter2() have the same implementation - they
1147 ** both return an ascending integer with each call. But counter1() is marked
1148 ** as non-deterministic and counter2() is marked as deterministic.
1150 if( rc==SQLITE_OK ){
1151 rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
1152 0, nondeterministicFunction, 0, 0);
1154 if( rc==SQLITE_OK ){
1155 rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
1156 0, nondeterministicFunction, 0, 0);
1159 /* The intreal() function converts its argument to an integer and returns
1160 ** it as a MEM_IntReal.
1162 if( rc==SQLITE_OK ){
1163 rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8,
1164 0, intrealFunction, 0, 0);
1167 /* The add_text_type(), add_int_type(), and add_real_type() functions
1168 ** attempt to return a value that has multiple datatypes.
1170 if( rc==SQLITE_OK ){
1171 rc = sqlite3_create_function(db, "add_text_type", 1, SQLITE_UTF8,
1172 0, addTextTypeFunction, 0, 0);
1174 if( rc==SQLITE_OK ){
1175 rc = sqlite3_create_function(db, "add_int_type", 1, SQLITE_UTF8,
1176 0, addIntTypeFunction, 0, 0);
1178 if( rc==SQLITE_OK ){
1179 rc = sqlite3_create_function(db, "add_real_type", 1, SQLITE_UTF8,
1180 0, addRealTypeFunction, 0, 0);
1183 /* Functions strtod() and dtostr() work as in the shell. These routines
1184 ** use the standard C library to convert between floating point and
1185 ** text. This is used to compare SQLite's internal conversion routines
1186 ** against the standard library conversion routines.
1188 ** Both routines copy/pasted from the shell.c.in implementation
1189 ** on 2023-07-03.
1191 if( rc==SQLITE_OK ){
1192 rc = sqlite3_create_function(db, "strtod", 1, SQLITE_UTF8, 0,
1193 shellStrtod, 0, 0);
1195 if( rc==SQLITE_OK ){
1196 rc = sqlite3_create_function(db, "dtostr", 1, SQLITE_UTF8, 0,
1197 shellDtostr, 0, 0);
1199 if( rc==SQLITE_OK ){
1200 rc = sqlite3_create_function(db, "dtostr", 2, SQLITE_UTF8, 0,
1201 shellDtostr, 0, 0);
1204 #ifndef SQLITE_OMIT_UTF16
1205 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1206 ** because it is not tested anywhere else. */
1207 if( rc==SQLITE_OK ){
1208 const void *zUtf16;
1209 sqlite3_value *pVal;
1210 sqlite3_mutex_enter(db->mutex);
1211 pVal = sqlite3ValueNew(db);
1212 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
1213 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1214 if( db->mallocFailed ){
1215 rc = SQLITE_NOMEM;
1216 }else{
1217 rc = sqlite3_create_function16(db, zUtf16,
1218 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
1220 sqlite3ValueFree(pVal);
1221 sqlite3_mutex_leave(db->mutex);
1223 #endif
1225 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1226 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1227 return TCL_OK;
1231 ** Usage: sqlite3_drop_modules DB ?NAME ...?
1233 ** Invoke the sqlite3_drop_modules(D,L) interface on database
1234 ** connection DB, in order to drop all modules except those named in
1235 ** the argument.
1237 static int SQLITE_TCLAPI test_drop_modules(
1238 void *NotUsed,
1239 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1240 int argc, /* Number of arguments */
1241 char **argv /* Text of each argument */
1243 sqlite3 *db;
1245 if( argc<2 ){
1246 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1247 " DB\"", 0);
1248 return TCL_ERROR;
1250 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1251 #ifndef SQLITE_OMIT_VIRTUALTABLE
1252 sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0);
1253 #endif
1254 return TCL_OK;
1258 ** Routines to implement the x_count() aggregate function.
1260 ** x_count() counts the number of non-null arguments. But there are
1261 ** some twists for testing purposes.
1263 ** If the argument to x_count() is 40 then a UTF-8 error is reported
1264 ** on the step function. If x_count(41) is seen, then a UTF-16 error
1265 ** is reported on the step function. If the total count is 42, then
1266 ** a UTF-8 error is reported on the finalize function.
1268 typedef struct t1CountCtx t1CountCtx;
1269 struct t1CountCtx {
1270 int n;
1272 static void t1CountStep(
1273 sqlite3_context *context,
1274 int argc,
1275 sqlite3_value **argv
1277 t1CountCtx *p;
1278 p = sqlite3_aggregate_context(context, sizeof(*p));
1279 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
1280 p->n++;
1282 if( argc>0 ){
1283 int v = sqlite3_value_int(argv[0]);
1284 if( v==40 ){
1285 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
1286 #ifndef SQLITE_OMIT_UTF16
1287 }else if( v==41 ){
1288 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1289 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
1290 #endif
1294 static void t1CountFinalize(sqlite3_context *context){
1295 t1CountCtx *p;
1296 p = sqlite3_aggregate_context(context, sizeof(*p));
1297 if( p ){
1298 if( p->n==42 ){
1299 sqlite3_result_error(context, "x_count totals to 42", -1);
1300 }else{
1301 sqlite3_result_int(context, p ? p->n : 0);
1306 #ifndef SQLITE_OMIT_DEPRECATED
1307 static void legacyCountStep(
1308 sqlite3_context *context,
1309 int argc,
1310 sqlite3_value **argv
1312 /* no-op */
1315 static void legacyCountFinalize(sqlite3_context *context){
1316 sqlite3_result_int(context, sqlite3_aggregate_count(context));
1318 #endif
1321 ** Usage: sqlite3_create_aggregate DB
1323 ** Call the sqlite3_create_function API on the given database in order
1324 ** to create a function named "x_count". This function is similar
1325 ** to the built-in count() function, with a few special quirks
1326 ** for testing the sqlite3_result_error() APIs.
1328 ** The original motivation for this routine was to be able to call the
1329 ** sqlite3_create_aggregate function while a query is in progress in order
1330 ** to test the SQLITE_MISUSE detection logic. See misuse.test.
1332 ** This routine was later extended to test the use of sqlite3_result_error()
1333 ** within aggregate functions.
1335 ** Later: It is now also extended to register the aggregate function
1336 ** "legacy_count()" with the supplied database handle. This is used
1337 ** to test the deprecated sqlite3_aggregate_count() API.
1339 static int SQLITE_TCLAPI test_create_aggregate(
1340 void *NotUsed,
1341 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1342 int argc, /* Number of arguments */
1343 char **argv /* Text of each argument */
1345 sqlite3 *db;
1346 int rc;
1347 if( argc!=2 ){
1348 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1349 " FILENAME\"", 0);
1350 return TCL_ERROR;
1352 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1353 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
1354 t1CountStep,t1CountFinalize);
1355 if( rc==SQLITE_OK ){
1356 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
1357 t1CountStep,t1CountFinalize);
1359 #ifndef SQLITE_OMIT_DEPRECATED
1360 if( rc==SQLITE_OK ){
1361 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1362 legacyCountStep, legacyCountFinalize
1365 #endif
1366 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1367 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1368 return TCL_OK;
1373 ** Usage: printf TEXT
1375 ** Send output to printf. Use this rather than puts to merge the output
1376 ** in the correct sequence with debugging printfs inserted into C code.
1377 ** Puts uses a separate buffer and debugging statements will be out of
1378 ** sequence if it is used.
1380 static int SQLITE_TCLAPI test_printf(
1381 void *NotUsed,
1382 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1383 int argc, /* Number of arguments */
1384 char **argv /* Text of each argument */
1386 if( argc!=2 ){
1387 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1388 " TEXT\"", 0);
1389 return TCL_ERROR;
1391 printf("%s\n", argv[1]);
1392 return TCL_OK;
1398 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1400 ** Call mprintf with three integer arguments
1402 static int SQLITE_TCLAPI sqlite3_mprintf_int(
1403 void *NotUsed,
1404 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1405 int argc, /* Number of arguments */
1406 char **argv /* Text of each argument */
1408 int a[3], i;
1409 char *z;
1410 if( argc!=5 ){
1411 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1412 " FORMAT INT INT INT\"", 0);
1413 return TCL_ERROR;
1415 for(i=2; i<5; i++){
1416 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1418 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1419 Tcl_AppendResult(interp, z, 0);
1420 sqlite3_free(z);
1421 return TCL_OK;
1425 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1427 ** Call mprintf with three 64-bit integer arguments
1429 static int SQLITE_TCLAPI sqlite3_mprintf_int64(
1430 void *NotUsed,
1431 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1432 int argc, /* Number of arguments */
1433 char **argv /* Text of each argument */
1435 int i;
1436 sqlite_int64 a[3];
1437 char *z;
1438 if( argc!=5 ){
1439 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1440 " FORMAT INT INT INT\"", 0);
1441 return TCL_ERROR;
1443 for(i=2; i<5; i++){
1444 if( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){
1445 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1446 return TCL_ERROR;
1449 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1450 Tcl_AppendResult(interp, z, 0);
1451 sqlite3_free(z);
1452 return TCL_OK;
1456 ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1458 ** Call mprintf with three long integer arguments. This might be the
1459 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1460 ** platform.
1462 static int SQLITE_TCLAPI sqlite3_mprintf_long(
1463 void *NotUsed,
1464 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1465 int argc, /* Number of arguments */
1466 char **argv /* Text of each argument */
1468 int i;
1469 long int a[3];
1470 int b[3];
1471 char *z;
1472 if( argc!=5 ){
1473 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1474 " FORMAT INT INT INT\"", 0);
1475 return TCL_ERROR;
1477 for(i=2; i<5; i++){
1478 if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
1479 a[i-2] = (long int)b[i-2];
1480 a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
1482 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1483 Tcl_AppendResult(interp, z, 0);
1484 sqlite3_free(z);
1485 return TCL_OK;
1489 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1491 ** Call mprintf with two integer arguments and one string argument
1493 static int SQLITE_TCLAPI sqlite3_mprintf_str(
1494 void *NotUsed,
1495 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1496 int argc, /* Number of arguments */
1497 char **argv /* Text of each argument */
1499 int a[3], i;
1500 char *z;
1501 if( argc<4 || argc>5 ){
1502 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1503 " FORMAT INT INT ?STRING?\"", 0);
1504 return TCL_ERROR;
1506 for(i=2; i<4; i++){
1507 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1509 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1510 Tcl_AppendResult(interp, z, 0);
1511 sqlite3_free(z);
1512 return TCL_OK;
1516 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1518 ** Call mprintf with two integer arguments and one string argument
1520 static int SQLITE_TCLAPI sqlite3_snprintf_str(
1521 void *NotUsed,
1522 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1523 int argc, /* Number of arguments */
1524 char **argv /* Text of each argument */
1526 int a[3], i;
1527 int n;
1528 char *z;
1529 if( argc<5 || argc>6 ){
1530 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1531 " INT FORMAT INT INT ?STRING?\"", 0);
1532 return TCL_ERROR;
1534 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1535 if( n<0 ){
1536 Tcl_AppendResult(interp, "N must be non-negative", 0);
1537 return TCL_ERROR;
1539 for(i=3; i<5; i++){
1540 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1542 z = sqlite3_malloc( n+1 );
1543 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1544 Tcl_AppendResult(interp, z, 0);
1545 sqlite3_free(z);
1546 return TCL_OK;
1550 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1552 ** Call mprintf with two integer arguments and one double argument
1554 static int SQLITE_TCLAPI sqlite3_mprintf_double(
1555 void *NotUsed,
1556 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1557 int argc, /* Number of arguments */
1558 char **argv /* Text of each argument */
1560 int a[3], i;
1561 double r;
1562 char *z;
1563 if( argc!=5 ){
1564 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1565 " FORMAT INT INT DOUBLE\"", 0);
1566 return TCL_ERROR;
1568 for(i=2; i<4; i++){
1569 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1571 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1572 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1573 Tcl_AppendResult(interp, z, 0);
1574 sqlite3_free(z);
1575 return TCL_OK;
1579 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1581 ** Call mprintf with a single double argument which is the product of the
1582 ** two arguments given above. This is used to generate overflow and underflow
1583 ** doubles to test that they are converted properly.
1585 static int SQLITE_TCLAPI sqlite3_mprintf_scaled(
1586 void *NotUsed,
1587 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1588 int argc, /* Number of arguments */
1589 char **argv /* Text of each argument */
1591 int i;
1592 double r[2];
1593 char *z;
1594 if( argc!=4 ){
1595 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1596 " FORMAT DOUBLE DOUBLE\"", 0);
1597 return TCL_ERROR;
1599 for(i=2; i<4; i++){
1600 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1602 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1603 Tcl_AppendResult(interp, z, 0);
1604 sqlite3_free(z);
1605 return TCL_OK;
1609 ** Usage: sqlite3_mprintf_stronly FORMAT STRING
1611 ** Call mprintf with a single double argument which is the product of the
1612 ** two arguments given above. This is used to generate overflow and underflow
1613 ** doubles to test that they are converted properly.
1615 static int SQLITE_TCLAPI sqlite3_mprintf_stronly(
1616 void *NotUsed,
1617 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1618 int argc, /* Number of arguments */
1619 char **argv /* Text of each argument */
1621 char *z;
1622 if( argc!=3 ){
1623 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1624 " FORMAT STRING\"", 0);
1625 return TCL_ERROR;
1627 z = sqlite3_mprintf(argv[1], argv[2]);
1628 Tcl_AppendResult(interp, z, 0);
1629 sqlite3_free(z);
1630 return TCL_OK;
1634 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1636 ** Call mprintf with a single double argument which is derived from the
1637 ** hexadecimal encoding of an IEEE double.
1639 static int SQLITE_TCLAPI sqlite3_mprintf_hexdouble(
1640 void *NotUsed,
1641 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1642 int argc, /* Number of arguments */
1643 char **argv /* Text of each argument */
1645 char *z;
1646 double r;
1647 unsigned int x1, x2;
1648 sqlite_uint64 d;
1649 if( argc!=3 ){
1650 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1651 " FORMAT STRING\"", 0);
1652 return TCL_ERROR;
1654 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1655 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1656 return TCL_ERROR;
1658 d = x2;
1659 d = (d<<32) + x1;
1660 memcpy(&r, &d, sizeof(r));
1661 z = sqlite3_mprintf(argv[1], r);
1662 Tcl_AppendResult(interp, z, 0);
1663 sqlite3_free(z);
1664 return TCL_OK;
1668 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1671 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1672 static int SQLITE_TCLAPI test_enable_shared(
1673 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1674 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1675 int objc, /* Number of arguments */
1676 Tcl_Obj *CONST objv[] /* Command arguments */
1678 int rc;
1679 int enable;
1680 int ret = 0;
1682 if( objc!=2 && objc!=1 ){
1683 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
1684 return TCL_ERROR;
1686 ret = sqlite3GlobalConfig.sharedCacheEnabled;
1688 if( objc==2 ){
1689 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1690 return TCL_ERROR;
1692 rc = sqlite3_enable_shared_cache(enable);
1693 if( rc!=SQLITE_OK ){
1694 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1695 return TCL_ERROR;
1698 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1699 return TCL_OK;
1701 #endif
1706 ** Usage: sqlite3_extended_result_codes DB BOOLEAN
1709 static int SQLITE_TCLAPI test_extended_result_codes(
1710 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1711 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1712 int objc, /* Number of arguments */
1713 Tcl_Obj *CONST objv[] /* Command arguments */
1715 int enable;
1716 sqlite3 *db;
1718 if( objc!=3 ){
1719 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1720 return TCL_ERROR;
1722 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1723 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1724 sqlite3_extended_result_codes(db, enable);
1725 return TCL_OK;
1729 ** Usage: sqlite3_libversion_number
1732 static int SQLITE_TCLAPI test_libversion_number(
1733 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1734 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1735 int objc, /* Number of arguments */
1736 Tcl_Obj *CONST objv[] /* Command arguments */
1738 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1739 return TCL_OK;
1743 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1746 static int SQLITE_TCLAPI test_table_column_metadata(
1747 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1748 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1749 int objc, /* Number of arguments */
1750 Tcl_Obj *CONST objv[] /* Command arguments */
1752 sqlite3 *db;
1753 const char *zDb;
1754 const char *zTbl;
1755 const char *zCol;
1756 int rc;
1757 Tcl_Obj *pRet;
1759 const char *zDatatype;
1760 const char *zCollseq;
1761 int notnull;
1762 int primarykey;
1763 int autoincrement;
1765 if( objc!=5 && objc!=4 ){
1766 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1767 return TCL_ERROR;
1769 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1770 zDb = Tcl_GetString(objv[2]);
1771 zTbl = Tcl_GetString(objv[3]);
1772 zCol = objc==5 ? Tcl_GetString(objv[4]) : 0;
1774 if( strlen(zDb)==0 ) zDb = 0;
1776 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1777 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1779 if( rc!=SQLITE_OK ){
1780 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1781 return TCL_ERROR;
1784 pRet = Tcl_NewObj();
1785 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1786 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1787 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1788 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1789 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1790 Tcl_SetObjResult(interp, pRet);
1792 return TCL_OK;
1795 #ifndef SQLITE_OMIT_INCRBLOB
1797 static int SQLITE_TCLAPI blobHandleFromObj(
1798 Tcl_Interp *interp,
1799 Tcl_Obj *pObj,
1800 sqlite3_blob **ppBlob
1802 char *z;
1803 int n;
1805 z = Tcl_GetStringFromObj(pObj, &n);
1806 if( n==0 ){
1807 *ppBlob = 0;
1808 }else{
1809 int notUsed;
1810 Tcl_Channel channel;
1811 ClientData instanceData;
1813 channel = Tcl_GetChannel(interp, z, &notUsed);
1814 if( !channel ) return TCL_ERROR;
1816 Tcl_Flush(channel);
1817 Tcl_Seek(channel, 0, SEEK_SET);
1819 instanceData = Tcl_GetChannelInstanceData(channel);
1820 *ppBlob = *((sqlite3_blob **)instanceData);
1823 return TCL_OK;
1826 static int SQLITE_TCLAPI test_blob_reopen(
1827 ClientData clientData, /* Not used */
1828 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1829 int objc, /* Number of arguments */
1830 Tcl_Obj *CONST objv[] /* Command arguments */
1832 Tcl_WideInt iRowid;
1833 sqlite3_blob *pBlob;
1834 int rc;
1836 if( objc!=3 ){
1837 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
1838 return TCL_ERROR;
1841 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
1842 if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
1844 rc = sqlite3_blob_reopen(pBlob, iRowid);
1845 if( rc!=SQLITE_OK ){
1846 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
1849 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1852 #endif
1855 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1857 ** This Tcl proc is used for testing the experimental
1858 ** sqlite3_create_collation_v2() interface.
1860 struct TestCollationX {
1861 Tcl_Interp *interp;
1862 Tcl_Obj *pCmp;
1863 Tcl_Obj *pDel;
1865 typedef struct TestCollationX TestCollationX;
1866 static void testCreateCollationDel(void *pCtx){
1867 TestCollationX *p = (TestCollationX *)pCtx;
1869 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1870 if( rc!=TCL_OK ){
1871 Tcl_BackgroundError(p->interp);
1874 Tcl_DecrRefCount(p->pCmp);
1875 Tcl_DecrRefCount(p->pDel);
1876 sqlite3_free((void *)p);
1878 static int testCreateCollationCmp(
1879 void *pCtx,
1880 int nLeft,
1881 const void *zLeft,
1882 int nRight,
1883 const void *zRight
1885 TestCollationX *p = (TestCollationX *)pCtx;
1886 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1887 int iRes = 0;
1889 Tcl_IncrRefCount(pScript);
1890 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1891 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1893 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1894 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1896 Tcl_BackgroundError(p->interp);
1898 Tcl_DecrRefCount(pScript);
1900 return iRes;
1902 static int SQLITE_TCLAPI test_create_collation_v2(
1903 ClientData clientData, /* Not used */
1904 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1905 int objc, /* Number of arguments */
1906 Tcl_Obj *CONST objv[] /* Command arguments */
1908 TestCollationX *p;
1909 sqlite3 *db;
1910 int rc;
1912 if( objc!=5 ){
1913 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1914 return TCL_ERROR;
1916 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1918 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1919 p->pCmp = objv[3];
1920 p->pDel = objv[4];
1921 p->interp = interp;
1922 Tcl_IncrRefCount(p->pCmp);
1923 Tcl_IncrRefCount(p->pDel);
1925 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1926 (void *)p, testCreateCollationCmp, testCreateCollationDel
1928 if( rc!=SQLITE_MISUSE ){
1929 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1930 "an invalid encoding", (char*)0);
1931 return TCL_ERROR;
1933 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1934 (void *)p, testCreateCollationCmp, testCreateCollationDel
1936 return TCL_OK;
1940 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
1942 ** Available switches are:
1944 ** -func SCRIPT
1945 ** -step SCRIPT
1946 ** -final SCRIPT
1947 ** -destroy SCRIPT
1949 typedef struct CreateFunctionV2 CreateFunctionV2;
1950 struct CreateFunctionV2 {
1951 Tcl_Interp *interp;
1952 Tcl_Obj *pFunc; /* Script for function invocation */
1953 Tcl_Obj *pStep; /* Script for agg. step invocation */
1954 Tcl_Obj *pFinal; /* Script for agg. finalization invocation */
1955 Tcl_Obj *pDestroy; /* Destructor script */
1957 static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1959 static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1961 static void cf2Final(sqlite3_context *ctx){
1963 static void cf2Destroy(void *pUser){
1964 CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
1966 if( p->interp && p->pDestroy ){
1967 int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
1968 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
1971 if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
1972 if( p->pStep ) Tcl_DecrRefCount(p->pStep);
1973 if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
1974 if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
1975 sqlite3_free(p);
1977 static int SQLITE_TCLAPI test_create_function_v2(
1978 ClientData clientData, /* Not used */
1979 Tcl_Interp *interp, /* The invoking TCL interpreter */
1980 int objc, /* Number of arguments */
1981 Tcl_Obj *CONST objv[] /* Command arguments */
1983 sqlite3 *db;
1984 const char *zFunc;
1985 int nArg;
1986 int enc;
1987 CreateFunctionV2 *p;
1988 int i;
1989 int rc;
1991 struct EncTable {
1992 const char *zEnc;
1993 int enc;
1994 } aEnc[] = {
1995 {"utf8", SQLITE_UTF8 },
1996 {"utf16", SQLITE_UTF16 },
1997 {"utf16le", SQLITE_UTF16LE },
1998 {"utf16be", SQLITE_UTF16BE },
1999 {"any", SQLITE_ANY },
2000 {"0", 0 },
2001 {0, 0 }
2004 if( objc<5 || (objc%2)==0 ){
2005 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
2006 return TCL_ERROR;
2009 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2010 zFunc = Tcl_GetString(objv[2]);
2011 if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
2012 if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
2013 "encoding", 0, &enc)
2015 return TCL_ERROR;
2017 enc = aEnc[enc].enc;
2019 p = sqlite3_malloc(sizeof(CreateFunctionV2));
2020 assert( p );
2021 memset(p, 0, sizeof(CreateFunctionV2));
2022 p->interp = interp;
2024 for(i=5; i<objc; i+=2){
2025 int iSwitch;
2026 const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
2027 if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
2028 sqlite3_free(p);
2029 return TCL_ERROR;
2032 switch( iSwitch ){
2033 case 0: p->pFunc = objv[i+1]; break;
2034 case 1: p->pStep = objv[i+1]; break;
2035 case 2: p->pFinal = objv[i+1]; break;
2036 case 3: p->pDestroy = objv[i+1]; break;
2039 if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
2040 if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
2041 if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
2042 if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
2044 if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
2045 if( p->pStep ) Tcl_IncrRefCount(p->pStep);
2046 if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
2047 if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
2049 rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
2050 (p->pFunc ? cf2Func : 0),
2051 (p->pStep ? cf2Step : 0),
2052 (p->pFinal ? cf2Final : 0),
2053 cf2Destroy
2055 if( rc!=SQLITE_OK ){
2056 Tcl_ResetResult(interp);
2057 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
2058 return TCL_ERROR;
2060 return TCL_OK;
2064 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
2066 static int SQLITE_TCLAPI test_load_extension(
2067 ClientData clientData, /* Not used */
2068 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2069 int objc, /* Number of arguments */
2070 Tcl_Obj *CONST objv[] /* Command arguments */
2072 Tcl_CmdInfo cmdInfo;
2073 sqlite3 *db;
2074 int rc;
2075 char *zDb;
2076 char *zFile;
2077 char *zProc = 0;
2078 char *zErr = 0;
2080 if( objc!=4 && objc!=3 ){
2081 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
2082 return TCL_ERROR;
2084 zDb = Tcl_GetString(objv[1]);
2085 zFile = Tcl_GetString(objv[2]);
2086 if( objc==4 ){
2087 zProc = Tcl_GetString(objv[3]);
2090 /* Extract the C database handle from the Tcl command name */
2091 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
2092 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
2093 return TCL_ERROR;
2095 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
2096 assert(db);
2098 /* Call the underlying C function. If an error occurs, set rc to
2099 ** TCL_ERROR and load any error string into the interpreter. If no
2100 ** error occurs, set rc to TCL_OK.
2102 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2103 rc = SQLITE_ERROR;
2104 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
2105 (void)zProc;
2106 (void)zFile;
2107 #else
2108 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
2109 #endif
2110 if( rc!=SQLITE_OK ){
2111 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
2112 rc = TCL_ERROR;
2113 }else{
2114 rc = TCL_OK;
2116 sqlite3_free(zErr);
2118 return rc;
2122 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
2124 static int SQLITE_TCLAPI test_enable_load(
2125 ClientData clientData, /* Not used */
2126 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2127 int objc, /* Number of arguments */
2128 Tcl_Obj *CONST objv[] /* Command arguments */
2130 Tcl_CmdInfo cmdInfo;
2131 sqlite3 *db;
2132 char *zDb;
2133 int onoff;
2135 if( objc!=3 ){
2136 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
2137 return TCL_ERROR;
2139 zDb = Tcl_GetString(objv[1]);
2141 /* Extract the C database handle from the Tcl command name */
2142 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
2143 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
2144 return TCL_ERROR;
2146 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
2147 assert(db);
2149 /* Get the onoff parameter */
2150 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
2151 return TCL_ERROR;
2154 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2155 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
2156 return TCL_ERROR;
2157 #else
2158 sqlite3_enable_load_extension(db, onoff);
2159 return TCL_OK;
2160 #endif
2164 ** Usage: sqlite_abort
2166 ** Shutdown the process immediately. This is not a clean shutdown.
2167 ** This command is used to test the recoverability of a database in
2168 ** the event of a program crash.
2170 static int SQLITE_TCLAPI sqlite_abort(
2171 void *NotUsed,
2172 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2173 int argc, /* Number of arguments */
2174 char **argv /* Text of each argument */
2176 #if defined(_MSC_VER)
2177 /* We do this, otherwise the test will halt with a popup message
2178 * that we have to click away before the test will continue.
2180 _set_abort_behavior( 0, _CALL_REPORTFAULT );
2181 #endif
2182 exit(255);
2183 assert( interp==0 ); /* This will always fail */
2184 return TCL_OK;
2188 ** The following routine is a user-defined SQL function whose purpose
2189 ** is to test the sqlite_set_result() API.
2191 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2192 while( argc>=2 ){
2193 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
2194 if( zArg0 ){
2195 if( 0==sqlite3StrICmp(zArg0, "int") ){
2196 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
2197 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
2198 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
2199 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
2200 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
2201 SQLITE_TRANSIENT);
2202 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
2203 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
2204 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
2205 sqlite3_result_null(context);
2206 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
2207 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
2208 }else{
2209 goto error_out;
2211 }else{
2212 goto error_out;
2214 argc -= 2;
2215 argv += 2;
2217 return;
2219 error_out:
2220 sqlite3_result_error(context,"first argument should be one of: "
2221 "int int64 string double null value", -1);
2225 ** Usage: sqlite_register_test_function DB NAME
2227 ** Register the test SQL function on the database DB under the name NAME.
2229 static int SQLITE_TCLAPI test_register_func(
2230 void *NotUsed,
2231 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2232 int argc, /* Number of arguments */
2233 char **argv /* Text of each argument */
2235 sqlite3 *db;
2236 int rc;
2237 if( argc!=3 ){
2238 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2239 " DB FUNCTION-NAME", 0);
2240 return TCL_ERROR;
2242 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2243 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
2244 testFunc, 0, 0);
2245 if( rc!=0 ){
2246 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2247 return TCL_ERROR;
2249 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2250 return TCL_OK;
2254 ** Usage: sqlite3_finalize STMT
2256 ** Finalize a statement handle.
2258 static int SQLITE_TCLAPI test_finalize(
2259 void * clientData,
2260 Tcl_Interp *interp,
2261 int objc,
2262 Tcl_Obj *CONST objv[]
2264 sqlite3_stmt *pStmt;
2265 int rc;
2266 sqlite3 *db = 0;
2268 if( objc!=2 ){
2269 Tcl_AppendResult(interp, "wrong # args: should be \"",
2270 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2271 return TCL_ERROR;
2274 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2276 if( pStmt ){
2277 db = StmtToDb(pStmt);
2279 rc = sqlite3_finalize(pStmt);
2280 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2281 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2282 return TCL_OK;
2286 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2288 ** Get the value of a status counter from a statement.
2290 static int SQLITE_TCLAPI test_stmt_status(
2291 void * clientData,
2292 Tcl_Interp *interp,
2293 int objc,
2294 Tcl_Obj *CONST objv[]
2296 int iValue;
2297 int i, op = 0, resetFlag;
2298 const char *zOpName;
2299 sqlite3_stmt *pStmt;
2301 static const struct {
2302 const char *zName;
2303 int op;
2304 } aOp[] = {
2305 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
2306 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
2307 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
2308 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP },
2309 { "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE },
2310 { "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN },
2311 { "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED },
2313 if( objc!=4 ){
2314 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2315 return TCL_ERROR;
2317 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2318 zOpName = Tcl_GetString(objv[2]);
2319 for(i=0; i<ArraySize(aOp); i++){
2320 if( strcmp(aOp[i].zName, zOpName)==0 ){
2321 op = aOp[i].op;
2322 break;
2325 if( i>=ArraySize(aOp) ){
2326 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2328 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2329 iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2330 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2331 return TCL_OK;
2334 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2336 ** Usage: sqlite3_stmt_scanstatus ?-flags FLAGS? STMT IDX
2338 static int SQLITE_TCLAPI test_stmt_scanstatus(
2339 void * clientData,
2340 Tcl_Interp *interp,
2341 int objc,
2342 Tcl_Obj *CONST objv[]
2344 sqlite3_stmt *pStmt; /* First argument */
2345 int idx; /* Second argument */
2347 const char *zName;
2348 const char *zExplain;
2349 sqlite3_int64 nLoop;
2350 sqlite3_int64 nVisit;
2351 sqlite3_int64 nCycle;
2352 double rEst;
2353 int res;
2354 int flags = 0;
2355 int iSelectId = 0;
2356 int iParentId = 0;
2357 int bDebug = 0;
2359 if( objc==5 ){
2360 struct Flag {
2361 const char *zFlag;
2362 int flag;
2363 } aTbl[] = {
2364 {"complex", SQLITE_SCANSTAT_COMPLEX},
2365 {"debug", -1},
2366 {0, 0}
2369 Tcl_Obj **aFlag = 0;
2370 int nFlag = 0;
2371 int ii;
2373 if( Tcl_ListObjGetElements(interp, objv[2], &nFlag, &aFlag) ){
2374 return TCL_ERROR;
2376 for(ii=0; ii<nFlag; ii++){
2377 int iVal = 0;
2378 int res = Tcl_GetIndexFromObjStruct(
2379 interp, aFlag[ii], aTbl, sizeof(aTbl[0]), "flag", 0, &iVal
2381 if( res ) return TCL_ERROR;
2382 if( aTbl[iVal].flag==-1 ){
2383 bDebug = 1;
2384 }else{
2385 flags |= aTbl[iVal].flag;
2390 if( objc!=3 && objc!=5 ){
2391 Tcl_WrongNumArgs(interp, 1, objv, "-flags FLAGS STMT IDX");
2392 return TCL_ERROR;
2394 if( getStmtPointer(interp, Tcl_GetString(objv[objc-2]), &pStmt)
2395 || Tcl_GetIntFromObj(interp, objv[objc-1], &idx)
2397 return TCL_ERROR;
2400 if( bDebug && 0==(flags & SQLITE_SCANSTAT_COMPLEX) ){
2401 Tcl_SetObjResult(interp,
2402 Tcl_NewStringObj("may not specify debug without complex", -1)
2404 return TCL_ERROR;
2407 if( idx<0 ){
2408 Tcl_Obj *pRet = Tcl_NewObj();
2409 res = sqlite3_stmt_scanstatus_v2(
2410 pStmt, -1, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle
2412 sqlite3_stmt_scanstatus_v2(
2413 pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle);
2414 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1));
2415 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle));
2416 Tcl_SetObjResult(interp, pRet);
2417 }else{
2418 res = sqlite3_stmt_scanstatus_v2(
2419 pStmt, idx, SQLITE_SCANSTAT_NLOOP, flags, (void*)&nLoop
2421 if( res==0 ){
2422 Tcl_Obj *pRet = Tcl_NewObj();
2423 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
2424 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
2425 sqlite3_stmt_scanstatus_v2(
2426 pStmt, idx, SQLITE_SCANSTAT_NVISIT, flags, (void*)&nVisit);
2427 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
2428 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
2429 sqlite3_stmt_scanstatus_v2(
2430 pStmt, idx, SQLITE_SCANSTAT_EST, flags, (void*)&rEst);
2431 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
2432 Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
2433 sqlite3_stmt_scanstatus_v2(
2434 pStmt, idx, SQLITE_SCANSTAT_NAME, flags, (void*)&zName);
2435 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
2436 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
2437 sqlite3_stmt_scanstatus_v2(
2438 pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, flags, (void*)&zExplain);
2439 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
2440 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
2441 sqlite3_stmt_scanstatus_v2(
2442 pStmt, idx, SQLITE_SCANSTAT_SELECTID, flags, (void*)&iSelectId);
2443 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iSelectId", -1));
2444 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iSelectId));
2445 sqlite3_stmt_scanstatus_v2(
2446 pStmt, idx, SQLITE_SCANSTAT_PARENTID, flags, (void*)&iParentId);
2447 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iParentId", -1));
2448 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iParentId));
2449 sqlite3_stmt_scanstatus_v2(
2450 pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle);
2451 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1));
2452 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle));
2454 if( bDebug ){
2455 int ii;
2456 ScanStatus *pScan = &((Vdbe*)pStmt)->aScan[idx];
2457 Tcl_Obj *pRange = Tcl_NewObj();
2458 Tcl_Obj *pCsr = Tcl_NewObj();
2460 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_loop", -1));
2461 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrLoop));
2462 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_visit", -1));
2463 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrVisit));
2464 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_explain",-1));
2465 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrExplain));
2466 for(ii=0; ii<ArraySize(pScan->aAddrRange)/2; ii++){
2467 int iStart = pScan->aAddrRange[ii*2];
2468 int iEnd = pScan->aAddrRange[ii*2+1];
2469 if( iStart>0 ){
2470 Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iStart));
2471 Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iEnd));
2472 }else if( iStart<0 ){
2473 Tcl_ListObjAppendElement(0, pCsr, Tcl_NewIntObj(iEnd));
2477 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_range", -1));
2478 Tcl_ListObjAppendElement(0, pRet, pRange);
2479 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_csr", -1));
2480 Tcl_ListObjAppendElement(0, pRet, pCsr);
2483 Tcl_SetObjResult(interp, pRet);
2484 }else{
2485 Tcl_ResetResult(interp);
2488 return TCL_OK;
2492 ** Usage: sqlite3_stmt_scanstatus_reset STMT
2494 static int SQLITE_TCLAPI test_stmt_scanstatus_reset(
2495 void * clientData,
2496 Tcl_Interp *interp,
2497 int objc,
2498 Tcl_Obj *CONST objv[]
2500 sqlite3_stmt *pStmt; /* First argument */
2501 if( objc!=2 ){
2502 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2503 return TCL_ERROR;
2505 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2506 sqlite3_stmt_scanstatus_reset(pStmt);
2507 return TCL_OK;
2509 #endif
2511 #ifdef SQLITE_ENABLE_SQLLOG
2513 ** Usage: sqlite3_config_sqllog
2515 ** Zero the SQLITE_CONFIG_SQLLOG configuration
2517 static int SQLITE_TCLAPI test_config_sqllog(
2518 void * clientData,
2519 Tcl_Interp *interp,
2520 int objc,
2521 Tcl_Obj *CONST objv[]
2523 if( objc!=1 ){
2524 Tcl_WrongNumArgs(interp, 1, objv, "");
2525 return TCL_ERROR;
2527 sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
2528 return TCL_OK;
2530 #endif
2533 ** Usage: sqlite3_config_sorterref
2535 ** Set the SQLITE_CONFIG_SORTERREF_SIZE configuration option
2537 static int SQLITE_TCLAPI test_config_sorterref(
2538 void * clientData,
2539 Tcl_Interp *interp,
2540 int objc,
2541 Tcl_Obj *CONST objv[]
2543 int iVal;
2544 if( objc!=2 ){
2545 Tcl_WrongNumArgs(interp, 1, objv, "NBYTE");
2546 return TCL_ERROR;
2548 if( Tcl_GetIntFromObj(interp, objv[1], &iVal) ) return TCL_ERROR;
2549 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, iVal);
2550 return TCL_OK;
2554 ** Usage: vfs_current_time_int64
2556 ** Return the value returned by the default VFS's xCurrentTimeInt64 method.
2558 static int SQLITE_TCLAPI vfsCurrentTimeInt64(
2559 void * clientData,
2560 Tcl_Interp *interp,
2561 int objc,
2562 Tcl_Obj *CONST objv[]
2564 i64 t;
2565 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
2566 if( objc!=1 ){
2567 Tcl_WrongNumArgs(interp, 1, objv, "");
2568 return TCL_ERROR;
2570 pVfs->xCurrentTimeInt64(pVfs, &t);
2571 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
2572 return TCL_OK;
2575 #ifndef SQLITE_OMIT_VIRTUALTABLE
2577 ** Usage: create_null_module DB NAME
2579 static int SQLITE_TCLAPI test_create_null_module(
2580 void * clientData,
2581 Tcl_Interp *interp,
2582 int objc,
2583 Tcl_Obj *CONST objv[]
2585 sqlite3 *db;
2586 char *zName;
2588 if( objc!=3 ){
2589 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2590 return TCL_ERROR;
2592 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2593 zName = Tcl_GetString(objv[2]);
2595 sqlite3_create_module(db, zName, 0, 0);
2596 return TCL_OK;
2598 #endif /* SQLITE_OMIT_VIRTUALTABLE */
2600 #ifdef SQLITE_ENABLE_SNAPSHOT
2602 ** Usage: sqlite3_snapshot_get DB DBNAME
2604 static int SQLITE_TCLAPI test_snapshot_get(
2605 void * clientData,
2606 Tcl_Interp *interp,
2607 int objc,
2608 Tcl_Obj *CONST objv[]
2610 int rc;
2611 sqlite3 *db;
2612 char *zName;
2613 sqlite3_snapshot *pSnapshot = 0;
2615 if( objc!=3 ){
2616 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2617 return TCL_ERROR;
2619 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2620 zName = Tcl_GetString(objv[2]);
2622 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2623 if( rc!=SQLITE_OK ){
2624 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2625 return TCL_ERROR;
2626 }else{
2627 char zBuf[100];
2628 if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
2629 Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
2631 return TCL_OK;
2633 #endif /* SQLITE_ENABLE_SNAPSHOT */
2635 #ifdef SQLITE_ENABLE_SNAPSHOT
2637 ** Usage: sqlite3_snapshot_recover DB DBNAME
2639 static int SQLITE_TCLAPI test_snapshot_recover(
2640 void * clientData,
2641 Tcl_Interp *interp,
2642 int objc,
2643 Tcl_Obj *CONST objv[]
2645 int rc;
2646 sqlite3 *db;
2647 char *zName;
2649 if( objc!=3 ){
2650 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2651 return TCL_ERROR;
2653 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2654 zName = Tcl_GetString(objv[2]);
2656 rc = sqlite3_snapshot_recover(db, zName);
2657 if( rc!=SQLITE_OK ){
2658 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2659 return TCL_ERROR;
2660 }else{
2661 Tcl_ResetResult(interp);
2663 return TCL_OK;
2665 #endif /* SQLITE_ENABLE_SNAPSHOT */
2667 #ifdef SQLITE_ENABLE_SNAPSHOT
2669 ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
2671 static int SQLITE_TCLAPI test_snapshot_open(
2672 void * clientData,
2673 Tcl_Interp *interp,
2674 int objc,
2675 Tcl_Obj *CONST objv[]
2677 int rc;
2678 sqlite3 *db;
2679 char *zName;
2680 sqlite3_snapshot *pSnapshot;
2682 if( objc!=4 ){
2683 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2684 return TCL_ERROR;
2686 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2687 zName = Tcl_GetString(objv[2]);
2688 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
2690 rc = sqlite3_snapshot_open(db, zName, pSnapshot);
2691 if( rc!=SQLITE_OK ){
2692 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2693 return TCL_ERROR;
2694 }else{
2695 Tcl_ResetResult(interp);
2697 return TCL_OK;
2699 #endif /* SQLITE_ENABLE_SNAPSHOT */
2701 #ifdef SQLITE_ENABLE_SNAPSHOT
2703 ** Usage: sqlite3_snapshot_free SNAPSHOT
2705 static int SQLITE_TCLAPI test_snapshot_free(
2706 void * clientData,
2707 Tcl_Interp *interp,
2708 int objc,
2709 Tcl_Obj *CONST objv[]
2711 sqlite3_snapshot *pSnapshot;
2712 if( objc!=2 ){
2713 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
2714 return TCL_ERROR;
2716 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2717 sqlite3_snapshot_free(pSnapshot);
2718 return TCL_OK;
2720 #endif /* SQLITE_ENABLE_SNAPSHOT */
2722 #ifdef SQLITE_ENABLE_SNAPSHOT
2724 ** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2
2726 static int SQLITE_TCLAPI test_snapshot_cmp(
2727 void * clientData,
2728 Tcl_Interp *interp,
2729 int objc,
2730 Tcl_Obj *CONST objv[]
2732 int res;
2733 sqlite3_snapshot *p1;
2734 sqlite3_snapshot *p2;
2735 if( objc!=3 ){
2736 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2737 return TCL_ERROR;
2739 p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2740 p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2]));
2741 res = sqlite3_snapshot_cmp(p1, p2);
2742 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2743 return TCL_OK;
2745 #endif /* SQLITE_ENABLE_SNAPSHOT */
2747 #ifdef SQLITE_ENABLE_SNAPSHOT
2749 ** Usage: sqlite3_snapshot_get_blob DB DBNAME
2751 static int SQLITE_TCLAPI test_snapshot_get_blob(
2752 void * clientData,
2753 Tcl_Interp *interp,
2754 int objc,
2755 Tcl_Obj *CONST objv[]
2757 int rc;
2758 sqlite3 *db;
2759 char *zName;
2760 sqlite3_snapshot *pSnapshot = 0;
2762 if( objc!=3 ){
2763 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2764 return TCL_ERROR;
2766 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2767 zName = Tcl_GetString(objv[2]);
2769 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2770 if( rc!=SQLITE_OK ){
2771 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2772 return TCL_ERROR;
2773 }else{
2774 Tcl_SetObjResult(interp,
2775 Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot))
2777 sqlite3_snapshot_free(pSnapshot);
2779 return TCL_OK;
2781 #endif /* SQLITE_ENABLE_SNAPSHOT */
2783 #ifdef SQLITE_ENABLE_SNAPSHOT
2785 ** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT
2787 static int SQLITE_TCLAPI test_snapshot_open_blob(
2788 void * clientData,
2789 Tcl_Interp *interp,
2790 int objc,
2791 Tcl_Obj *CONST objv[]
2793 int rc;
2794 sqlite3 *db;
2795 char *zName;
2796 unsigned char *pBlob;
2797 int nBlob;
2799 if( objc!=4 ){
2800 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2801 return TCL_ERROR;
2803 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2804 zName = Tcl_GetString(objv[2]);
2805 pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob);
2806 if( nBlob!=sizeof(sqlite3_snapshot) ){
2807 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2808 return TCL_ERROR;
2810 rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob);
2811 if( rc!=SQLITE_OK ){
2812 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2813 return TCL_ERROR;
2815 return TCL_OK;
2817 #endif /* SQLITE_ENABLE_SNAPSHOT */
2819 #ifdef SQLITE_ENABLE_SNAPSHOT
2821 ** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2
2823 static int SQLITE_TCLAPI test_snapshot_cmp_blob(
2824 void * clientData,
2825 Tcl_Interp *interp,
2826 int objc,
2827 Tcl_Obj *CONST objv[]
2829 int res;
2830 unsigned char *p1;
2831 unsigned char *p2;
2832 int n1;
2833 int n2;
2835 if( objc!=3 ){
2836 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2837 return TCL_ERROR;
2840 p1 = Tcl_GetByteArrayFromObj(objv[1], &n1);
2841 p2 = Tcl_GetByteArrayFromObj(objv[2], &n2);
2843 if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){
2844 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2845 return TCL_ERROR;
2848 res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2);
2849 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2850 return TCL_OK;
2852 #endif /* SQLITE_ENABLE_SNAPSHOT */
2855 ** Usage: sqlite3_delete_database FILENAME
2857 int sqlite3_delete_database(const char*); /* in test_delete.c */
2858 static int SQLITE_TCLAPI test_delete_database(
2859 void * clientData,
2860 Tcl_Interp *interp,
2861 int objc,
2862 Tcl_Obj *CONST objv[]
2864 int rc;
2865 const char *zFile;
2866 if( objc!=2 ){
2867 Tcl_WrongNumArgs(interp, 1, objv, "FILE");
2868 return TCL_ERROR;
2870 zFile = (const char*)Tcl_GetString(objv[1]);
2871 rc = sqlite3_delete_database(zFile);
2873 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2874 return TCL_OK;
2878 ** Usage: atomic_batch_write PATH
2880 static int SQLITE_TCLAPI test_atomic_batch_write(
2881 void * clientData,
2882 Tcl_Interp *interp,
2883 int objc,
2884 Tcl_Obj *CONST objv[]
2886 char *zFile = 0; /* Path to file to test */
2887 sqlite3 *db = 0; /* Database handle */
2888 sqlite3_file *pFd = 0; /* SQLite fd open on zFile */
2889 int bRes = 0; /* Integer result of this command */
2890 int dc = 0; /* Device-characteristics mask */
2891 int rc; /* sqlite3_open() return code */
2893 if( objc!=2 ){
2894 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
2895 return TCL_ERROR;
2897 zFile = Tcl_GetString(objv[1]);
2899 rc = sqlite3_open(zFile, &db);
2900 if( rc!=SQLITE_OK ){
2901 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
2902 sqlite3_close(db);
2903 return TCL_ERROR;
2906 rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
2907 dc = pFd->pMethods->xDeviceCharacteristics(pFd);
2908 if( dc & SQLITE_IOCAP_BATCH_ATOMIC ){
2909 bRes = 1;
2912 Tcl_SetObjResult(interp, Tcl_NewIntObj(bRes));
2913 sqlite3_close(db);
2914 return TCL_OK;
2918 ** Usage: sqlite3_next_stmt DB STMT
2920 ** Return the next statement in sequence after STMT.
2922 static int SQLITE_TCLAPI test_next_stmt(
2923 void * clientData,
2924 Tcl_Interp *interp,
2925 int objc,
2926 Tcl_Obj *CONST objv[]
2928 sqlite3_stmt *pStmt;
2929 sqlite3 *db = 0;
2930 char zBuf[50];
2932 if( objc!=3 ){
2933 Tcl_AppendResult(interp, "wrong # args: should be \"",
2934 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2935 return TCL_ERROR;
2938 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2939 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2940 pStmt = sqlite3_next_stmt(db, pStmt);
2941 if( pStmt ){
2942 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2943 Tcl_AppendResult(interp, zBuf, 0);
2945 return TCL_OK;
2949 ** Usage: sqlite3_stmt_readonly STMT
2951 ** Return true if STMT is a NULL pointer or a pointer to a statement
2952 ** that is guaranteed to leave the database unmodified.
2954 static int SQLITE_TCLAPI test_stmt_readonly(
2955 void * clientData,
2956 Tcl_Interp *interp,
2957 int objc,
2958 Tcl_Obj *CONST objv[]
2960 sqlite3_stmt *pStmt;
2961 int rc;
2963 if( objc!=2 ){
2964 Tcl_AppendResult(interp, "wrong # args: should be \"",
2965 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2966 return TCL_ERROR;
2969 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2970 rc = sqlite3_stmt_readonly(pStmt);
2971 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2972 return TCL_OK;
2976 ** Usage: sqlite3_stmt_isexplain STMT
2978 ** Return 1, 2, or 0 respectively if STMT is an EXPLAIN statement, an
2979 ** EXPLAIN QUERY PLAN statement or an ordinary statement or NULL pointer.
2981 static int SQLITE_TCLAPI test_stmt_isexplain(
2982 void * clientData,
2983 Tcl_Interp *interp,
2984 int objc,
2985 Tcl_Obj *CONST objv[]
2987 sqlite3_stmt *pStmt;
2988 int rc;
2990 if( objc!=2 ){
2991 Tcl_AppendResult(interp, "wrong # args: should be \"",
2992 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2993 return TCL_ERROR;
2996 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2997 rc = sqlite3_stmt_isexplain(pStmt);
2998 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
2999 return TCL_OK;
3003 ** Usage: sqlite3_stmt_explain STMT INT
3005 ** Set the explain to normal (0), EXPLAIN (1) or EXPLAIN QUERY PLAN (2).
3007 static int SQLITE_TCLAPI test_stmt_explain(
3008 void * clientData,
3009 Tcl_Interp *interp,
3010 int objc,
3011 Tcl_Obj *CONST objv[]
3013 sqlite3_stmt *pStmt;
3014 int eMode = 0;
3015 int rc;
3017 if( objc!=3 ){
3018 Tcl_AppendResult(interp, "wrong # args: should be \"",
3019 Tcl_GetStringFromObj(objv[0], 0), " STMT INT", 0);
3020 return TCL_ERROR;
3023 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3024 if( Tcl_GetIntFromObj(interp, objv[2], &eMode) ) return TCL_ERROR;
3025 rc = sqlite3_stmt_explain(pStmt, eMode);
3026 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
3027 return TCL_OK;
3031 ** Usage: sqlite3_stmt_busy STMT
3033 ** Return true if STMT is a non-NULL pointer to a statement
3034 ** that has been stepped but not to completion.
3036 static int SQLITE_TCLAPI test_stmt_busy(
3037 void * clientData,
3038 Tcl_Interp *interp,
3039 int objc,
3040 Tcl_Obj *CONST objv[]
3042 sqlite3_stmt *pStmt;
3043 int rc;
3045 if( objc!=2 ){
3046 Tcl_AppendResult(interp, "wrong # args: should be \"",
3047 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
3048 return TCL_ERROR;
3051 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3052 rc = sqlite3_stmt_busy(pStmt);
3053 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
3054 return TCL_OK;
3058 ** Usage: uses_stmt_journal STMT
3060 ** Return true if STMT uses a statement journal.
3062 static int SQLITE_TCLAPI uses_stmt_journal(
3063 void * clientData,
3064 Tcl_Interp *interp,
3065 int objc,
3066 Tcl_Obj *CONST objv[]
3068 sqlite3_stmt *pStmt;
3070 if( objc!=2 ){
3071 Tcl_AppendResult(interp, "wrong # args: should be \"",
3072 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
3073 return TCL_ERROR;
3076 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3077 sqlite3_stmt_readonly(pStmt);
3078 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
3079 return TCL_OK;
3084 ** Usage: sqlite3_reset STMT
3086 ** Reset a statement handle.
3088 static int SQLITE_TCLAPI test_reset(
3089 void * clientData,
3090 Tcl_Interp *interp,
3091 int objc,
3092 Tcl_Obj *CONST objv[]
3094 sqlite3_stmt *pStmt;
3095 int rc;
3097 if( objc!=2 ){
3098 Tcl_AppendResult(interp, "wrong # args: should be \"",
3099 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
3100 return TCL_ERROR;
3103 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3105 rc = sqlite3_reset(pStmt);
3106 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
3107 return TCL_ERROR;
3109 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3111 if( rc ){
3112 return TCL_ERROR;
3115 return TCL_OK;
3119 ** Usage: sqlite3_expired STMT
3121 ** Return TRUE if a recompilation of the statement is recommended.
3123 static int SQLITE_TCLAPI test_expired(
3124 void * clientData,
3125 Tcl_Interp *interp,
3126 int objc,
3127 Tcl_Obj *CONST objv[]
3129 #ifndef SQLITE_OMIT_DEPRECATED
3130 sqlite3_stmt *pStmt;
3131 if( objc!=2 ){
3132 Tcl_AppendResult(interp, "wrong # args: should be \"",
3133 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
3134 return TCL_ERROR;
3136 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3137 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
3138 #endif
3139 return TCL_OK;
3143 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
3145 ** Transfer all bindings from FROMSTMT over to TOSTMT
3147 static int SQLITE_TCLAPI test_transfer_bind(
3148 void * clientData,
3149 Tcl_Interp *interp,
3150 int objc,
3151 Tcl_Obj *CONST objv[]
3153 #ifndef SQLITE_OMIT_DEPRECATED
3154 sqlite3_stmt *pStmt1, *pStmt2;
3155 if( objc!=3 ){
3156 Tcl_AppendResult(interp, "wrong # args: should be \"",
3157 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
3158 return TCL_ERROR;
3160 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
3161 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
3162 Tcl_SetObjResult(interp,
3163 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
3164 #endif
3165 return TCL_OK;
3169 ** Usage: sqlite3_changes DB
3171 ** Return the number of changes made to the database by the last SQL
3172 ** execution.
3174 static int SQLITE_TCLAPI test_changes(
3175 void * clientData,
3176 Tcl_Interp *interp,
3177 int objc,
3178 Tcl_Obj *CONST objv[]
3180 sqlite3 *db;
3181 if( objc!=2 ){
3182 Tcl_AppendResult(interp, "wrong # args: should be \"",
3183 Tcl_GetString(objv[0]), " DB", 0);
3184 return TCL_ERROR;
3186 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3187 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
3188 return TCL_OK;
3192 ** This is the "static_bind_value" that variables are bound to when
3193 ** the FLAG option of sqlite3_bind is "static"
3195 static char *sqlite_static_bind_value = 0;
3196 static int sqlite_static_bind_nbyte = 0;
3199 ** Usage: sqlite3_bind VM IDX VALUE FLAGS
3201 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
3202 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
3203 ** ignored and the value is set to NULL. If FLAGS=="static" then
3204 ** the value is set to the value of a static variable named
3205 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
3206 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
3207 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
3209 static int SQLITE_TCLAPI test_bind(
3210 void *NotUsed,
3211 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3212 int argc, /* Number of arguments */
3213 char **argv /* Text of each argument */
3215 sqlite3_stmt *pStmt;
3216 int rc;
3217 int idx;
3218 if( argc!=5 ){
3219 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3220 " VM IDX VALUE (null|static|normal)\"", 0);
3221 return TCL_ERROR;
3223 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
3224 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
3225 if( strcmp(argv[4],"null")==0 ){
3226 rc = sqlite3_bind_null(pStmt, idx);
3227 }else if( strcmp(argv[4],"static")==0 ){
3228 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
3229 }else if( strcmp(argv[4],"static-nbytes")==0 ){
3230 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
3231 sqlite_static_bind_nbyte, 0);
3232 }else if( strcmp(argv[4],"normal")==0 ){
3233 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
3234 }else if( strcmp(argv[4],"blob10")==0 ){
3235 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
3236 }else{
3237 Tcl_AppendResult(interp, "4th argument should be "
3238 "\"null\" or \"static\" or \"normal\"", 0);
3239 return TCL_ERROR;
3241 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3242 if( rc ){
3243 char zBuf[50];
3244 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3245 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
3246 return TCL_ERROR;
3248 return TCL_OK;
3251 #ifndef SQLITE_OMIT_UTF16
3253 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
3255 ** This function is used to test that SQLite selects the correct collation
3256 ** sequence callback when multiple versions (for different text encodings)
3257 ** are available.
3259 ** Calling this routine registers the collation sequence "test_collate"
3260 ** with database handle <db>. The second argument must be a list of three
3261 ** boolean values. If the first is true, then a version of test_collate is
3262 ** registered for UTF-8, if the second is true, a version is registered for
3263 ** UTF-16le, if the third is true, a UTF-16be version is available.
3264 ** Previous versions of test_collate are deleted.
3266 ** The collation sequence test_collate is implemented by calling the
3267 ** following TCL script:
3269 ** "test_collate <enc> <lhs> <rhs>"
3271 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
3272 ** The <enc> parameter is the encoding of the collation function that
3273 ** SQLite selected to call. The TCL test script implements the
3274 ** "test_collate" proc.
3276 ** Note that this will only work with one interpreter at a time, as the
3277 ** interp pointer to use when evaluating the TCL script is stored in
3278 ** pTestCollateInterp.
3280 static Tcl_Interp* pTestCollateInterp;
3281 static int test_collate_func(
3282 void *pCtx,
3283 int nA, const void *zA,
3284 int nB, const void *zB
3286 Tcl_Interp *i = pTestCollateInterp;
3287 int encin = SQLITE_PTR_TO_INT(pCtx);
3288 int res;
3289 int n;
3291 sqlite3_value *pVal;
3292 Tcl_Obj *pX;
3294 pX = Tcl_NewStringObj("test_collate", -1);
3295 Tcl_IncrRefCount(pX);
3297 switch( encin ){
3298 case SQLITE_UTF8:
3299 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
3300 break;
3301 case SQLITE_UTF16LE:
3302 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
3303 break;
3304 case SQLITE_UTF16BE:
3305 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
3306 break;
3307 default:
3308 assert(0);
3311 sqlite3BeginBenignMalloc();
3312 pVal = sqlite3ValueNew(0);
3313 if( pVal ){
3314 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
3315 n = sqlite3_value_bytes(pVal);
3316 Tcl_ListObjAppendElement(i,pX,
3317 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
3318 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
3319 n = sqlite3_value_bytes(pVal);
3320 Tcl_ListObjAppendElement(i,pX,
3321 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
3322 sqlite3ValueFree(pVal);
3324 sqlite3EndBenignMalloc();
3326 Tcl_EvalObjEx(i, pX, 0);
3327 Tcl_DecrRefCount(pX);
3328 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
3329 return res;
3331 static int SQLITE_TCLAPI test_collate(
3332 void * clientData,
3333 Tcl_Interp *interp,
3334 int objc,
3335 Tcl_Obj *CONST objv[]
3337 sqlite3 *db;
3338 int val;
3339 sqlite3_value *pVal;
3340 int rc;
3342 if( objc!=5 ) goto bad_args;
3343 pTestCollateInterp = interp;
3344 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3346 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3347 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
3348 (void *)SQLITE_UTF8, val?test_collate_func:0);
3349 if( rc==SQLITE_OK ){
3350 const void *zUtf16;
3351 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3352 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
3353 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
3354 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3356 #if 0
3357 if( sqlite3_iMallocFail>0 ){
3358 sqlite3_iMallocFail++;
3360 #endif
3361 sqlite3_mutex_enter(db->mutex);
3362 pVal = sqlite3ValueNew(db);
3363 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
3364 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
3365 if( db->mallocFailed ){
3366 rc = SQLITE_NOMEM;
3367 }else{
3368 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
3369 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
3371 sqlite3ValueFree(pVal);
3372 sqlite3_mutex_leave(db->mutex);
3374 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3376 if( rc!=SQLITE_OK ){
3377 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3378 return TCL_ERROR;
3380 return TCL_OK;
3382 bad_args:
3383 Tcl_AppendResult(interp, "wrong # args: should be \"",
3384 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3385 return TCL_ERROR;
3389 ** Usage: add_test_utf16bin_collate <db ptr>
3391 ** Add a utf-16 collation sequence named "utf16bin" to the database
3392 ** handle. This collation sequence compares arguments in the same way as the
3393 ** built-in collation "binary".
3395 static int test_utf16bin_collate_func(
3396 void *pCtx,
3397 int nA, const void *zA,
3398 int nB, const void *zB
3400 int nCmp = (nA>nB ? nB : nA);
3401 int res = memcmp(zA, zB, nCmp);
3402 if( res==0 ) res = nA - nB;
3403 return res;
3405 static int SQLITE_TCLAPI test_utf16bin_collate(
3406 void * clientData,
3407 Tcl_Interp *interp,
3408 int objc,
3409 Tcl_Obj *CONST objv[]
3411 sqlite3 *db;
3412 int rc;
3414 if( objc!=2 ) goto bad_args;
3415 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3417 rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0,
3418 test_utf16bin_collate_func
3420 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3421 return TCL_OK;
3423 bad_args:
3424 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3425 return TCL_ERROR;
3429 ** When the collation needed callback is invoked, record the name of
3430 ** the requested collating function here. The recorded name is linked
3431 ** to a TCL variable and used to make sure that the requested collation
3432 ** name is correct.
3434 static char zNeededCollation[200];
3435 static char *pzNeededCollation = zNeededCollation;
3439 ** Called when a collating sequence is needed. Registered using
3440 ** sqlite3_collation_needed16().
3442 static void test_collate_needed_cb(
3443 void *pCtx,
3444 sqlite3 *db,
3445 int eTextRep,
3446 const void *pName
3448 int enc = ENC(db);
3449 int i;
3450 char *z;
3451 for(z = (char*)pName, i=0; *z || z[1]; z++){
3452 if( *z ) zNeededCollation[i++] = *z;
3454 zNeededCollation[i] = 0;
3455 sqlite3_create_collation(
3456 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
3460 ** Usage: add_test_collate_needed DB
3462 static int SQLITE_TCLAPI test_collate_needed(
3463 void * clientData,
3464 Tcl_Interp *interp,
3465 int objc,
3466 Tcl_Obj *CONST objv[]
3468 sqlite3 *db;
3469 int rc;
3471 if( objc!=2 ) goto bad_args;
3472 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3473 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
3474 zNeededCollation[0] = 0;
3475 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3476 return TCL_OK;
3478 bad_args:
3479 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3480 return TCL_ERROR;
3484 ** tclcmd: add_alignment_test_collations DB
3486 ** Add two new collating sequences to the database DB
3488 ** utf16_aligned
3489 ** utf16_unaligned
3491 ** Both collating sequences use the same sort order as BINARY.
3492 ** The only difference is that the utf16_aligned collating
3493 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
3494 ** Both collating functions increment the unaligned utf16 counter
3495 ** whenever they see a string that begins on an odd byte boundary.
3497 static int unaligned_string_counter = 0;
3498 static int alignmentCollFunc(
3499 void *NotUsed,
3500 int nKey1, const void *pKey1,
3501 int nKey2, const void *pKey2
3503 int rc, n;
3504 n = nKey1<nKey2 ? nKey1 : nKey2;
3505 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
3506 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
3507 rc = memcmp(pKey1, pKey2, n);
3508 if( rc==0 ){
3509 rc = nKey1 - nKey2;
3511 return rc;
3513 static int SQLITE_TCLAPI add_alignment_test_collations(
3514 void * clientData,
3515 Tcl_Interp *interp,
3516 int objc,
3517 Tcl_Obj *CONST objv[]
3519 sqlite3 *db;
3520 if( objc>=2 ){
3521 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3522 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
3523 0, alignmentCollFunc);
3524 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
3525 0, alignmentCollFunc);
3527 return SQLITE_OK;
3529 #endif /* !defined(SQLITE_OMIT_UTF16) */
3532 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
3534 ** This function is used to test that SQLite selects the correct user
3535 ** function callback when multiple versions (for different text encodings)
3536 ** are available.
3538 ** Calling this routine registers up to three versions of the user function
3539 ** "test_function" with database handle <db>. If the second argument is
3540 ** true, then a version of test_function is registered for UTF-8, if the
3541 ** third is true, a version is registered for UTF-16le, if the fourth is
3542 ** true, a UTF-16be version is available. Previous versions of
3543 ** test_function are deleted.
3545 ** The user function is implemented by calling the following TCL script:
3547 ** "test_function <enc> <arg>"
3549 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
3550 ** single argument passed to the SQL function. The value returned by
3551 ** the TCL script is used as the return value of the SQL function. It
3552 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
3553 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
3554 ** prefers UTF-16BE.
3556 #ifndef SQLITE_OMIT_UTF16
3557 static void test_function_utf8(
3558 sqlite3_context *pCtx,
3559 int nArg,
3560 sqlite3_value **argv
3562 Tcl_Interp *interp;
3563 Tcl_Obj *pX;
3564 sqlite3_value *pVal;
3565 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3566 pX = Tcl_NewStringObj("test_function", -1);
3567 Tcl_IncrRefCount(pX);
3568 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
3569 Tcl_ListObjAppendElement(interp, pX,
3570 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3571 Tcl_EvalObjEx(interp, pX, 0);
3572 Tcl_DecrRefCount(pX);
3573 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
3574 pVal = sqlite3ValueNew(0);
3575 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3576 SQLITE_UTF8, SQLITE_STATIC);
3577 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
3578 -1, SQLITE_TRANSIENT);
3579 sqlite3ValueFree(pVal);
3581 static void test_function_utf16le(
3582 sqlite3_context *pCtx,
3583 int nArg,
3584 sqlite3_value **argv
3586 Tcl_Interp *interp;
3587 Tcl_Obj *pX;
3588 sqlite3_value *pVal;
3589 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3590 pX = Tcl_NewStringObj("test_function", -1);
3591 Tcl_IncrRefCount(pX);
3592 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
3593 Tcl_ListObjAppendElement(interp, pX,
3594 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3595 Tcl_EvalObjEx(interp, pX, 0);
3596 Tcl_DecrRefCount(pX);
3597 pVal = sqlite3ValueNew(0);
3598 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3599 SQLITE_UTF8, SQLITE_STATIC);
3600 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
3601 sqlite3ValueFree(pVal);
3603 static void test_function_utf16be(
3604 sqlite3_context *pCtx,
3605 int nArg,
3606 sqlite3_value **argv
3608 Tcl_Interp *interp;
3609 Tcl_Obj *pX;
3610 sqlite3_value *pVal;
3611 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3612 pX = Tcl_NewStringObj("test_function", -1);
3613 Tcl_IncrRefCount(pX);
3614 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
3615 Tcl_ListObjAppendElement(interp, pX,
3616 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3617 Tcl_EvalObjEx(interp, pX, 0);
3618 Tcl_DecrRefCount(pX);
3619 pVal = sqlite3ValueNew(0);
3620 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3621 SQLITE_UTF8, SQLITE_STATIC);
3622 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
3623 -1, SQLITE_TRANSIENT);
3624 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
3625 -1, SQLITE_TRANSIENT);
3626 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
3627 -1, SQLITE_TRANSIENT);
3628 sqlite3ValueFree(pVal);
3630 #endif /* SQLITE_OMIT_UTF16 */
3631 static int SQLITE_TCLAPI test_function(
3632 void * clientData,
3633 Tcl_Interp *interp,
3634 int objc,
3635 Tcl_Obj *CONST objv[]
3637 #ifndef SQLITE_OMIT_UTF16
3638 sqlite3 *db;
3639 int val;
3641 if( objc!=5 ) goto bad_args;
3642 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3644 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3645 if( val ){
3646 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
3647 interp, test_function_utf8, 0, 0);
3649 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3650 if( val ){
3651 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
3652 interp, test_function_utf16le, 0, 0);
3654 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3655 if( val ){
3656 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
3657 interp, test_function_utf16be, 0, 0);
3660 return TCL_OK;
3661 bad_args:
3662 Tcl_AppendResult(interp, "wrong # args: should be \"",
3663 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3664 #endif /* SQLITE_OMIT_UTF16 */
3665 return TCL_ERROR;
3669 ** Usage: sqlite3_test_errstr <err code>
3671 ** Test that the English language string equivalents for sqlite error codes
3672 ** are sane. The parameter is an integer representing an sqlite error code.
3673 ** The result is a list of two elements, the string representation of the
3674 ** error code and the English language explanation.
3676 static int SQLITE_TCLAPI test_errstr(
3677 void * clientData,
3678 Tcl_Interp *interp,
3679 int objc,
3680 Tcl_Obj *CONST objv[]
3682 char *zCode;
3683 int i;
3684 if( objc!=1 ){
3685 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
3688 zCode = Tcl_GetString(objv[1]);
3689 for(i=0; i<200; i++){
3690 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
3692 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
3693 return TCL_OK;
3697 ** Usage: breakpoint
3699 ** This routine exists for one purpose - to provide a place to put a
3700 ** breakpoint with GDB that can be triggered using TCL code. The use
3701 ** for this is when a particular test fails on (say) the 1485th iteration.
3702 ** In the TCL test script, we can add code like this:
3704 ** if {$i==1485} breakpoint
3706 ** Then run testfixture in the debugger and wait for the breakpoint to
3707 ** fire. Then additional breakpoints can be set to trace down the bug.
3709 static int SQLITE_TCLAPI test_breakpoint(
3710 void *NotUsed,
3711 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3712 int argc, /* Number of arguments */
3713 char **argv /* Text of each argument */
3715 return TCL_OK; /* Do nothing */
3719 ** Usage: sqlite3_bind_zeroblob STMT IDX N
3721 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
3722 ** IDX is the index of a wildcard in the prepared statement. This command
3723 ** binds a N-byte zero-filled BLOB to the wildcard.
3725 static int SQLITE_TCLAPI test_bind_zeroblob(
3726 void * clientData,
3727 Tcl_Interp *interp,
3728 int objc,
3729 Tcl_Obj *CONST objv[]
3731 sqlite3_stmt *pStmt;
3732 int idx;
3733 int n;
3734 int rc;
3736 if( objc!=4 ){
3737 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3738 return TCL_ERROR;
3741 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3742 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3743 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3745 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
3746 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3747 if( rc!=SQLITE_OK ){
3748 return TCL_ERROR;
3751 return TCL_OK;
3755 ** Usage: sqlite3_bind_zeroblob64 STMT IDX N
3757 ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement.
3758 ** IDX is the index of a wildcard in the prepared statement. This command
3759 ** binds a N-byte zero-filled BLOB to the wildcard.
3761 static int SQLITE_TCLAPI test_bind_zeroblob64(
3762 void * clientData,
3763 Tcl_Interp *interp,
3764 int objc,
3765 Tcl_Obj *CONST objv[]
3767 sqlite3_stmt *pStmt;
3768 int idx;
3769 Tcl_WideInt n;
3770 int rc;
3772 if( objc!=4 ){
3773 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3774 return TCL_ERROR;
3777 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3778 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3779 if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3781 rc = sqlite3_bind_zeroblob64(pStmt, idx, n);
3782 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3783 if( rc!=SQLITE_OK ){
3784 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3785 return TCL_ERROR;
3788 return TCL_OK;
3792 ** Usage: sqlite3_bind_int STMT N VALUE
3794 ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
3795 ** N is the index of a wildcard in the prepared statement. This command
3796 ** binds a 32-bit integer VALUE to that wildcard.
3798 static int SQLITE_TCLAPI test_bind_int(
3799 void * clientData,
3800 Tcl_Interp *interp,
3801 int objc,
3802 Tcl_Obj *CONST objv[]
3804 sqlite3_stmt *pStmt;
3805 int idx;
3806 int value;
3807 int rc;
3809 if( objc!=4 ){
3810 Tcl_AppendResult(interp, "wrong # args: should be \"",
3811 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3812 return TCL_ERROR;
3815 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3816 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3817 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3819 rc = sqlite3_bind_int(pStmt, idx, value);
3820 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3821 if( rc!=SQLITE_OK ){
3822 return TCL_ERROR;
3825 return TCL_OK;
3830 ** Usage: intarray_addr INT ...
3832 ** Return the address of a C-language array of 32-bit integers.
3834 ** Space to hold the array is obtained from malloc(). Call this procedure once
3835 ** with no arguments in order to release memory. Each call to this procedure
3836 ** overwrites the previous array.
3838 static int SQLITE_TCLAPI test_intarray_addr(
3839 void * clientData,
3840 Tcl_Interp *interp,
3841 int objc,
3842 Tcl_Obj *CONST objv[]
3844 int i;
3845 static int *p = 0;
3847 sqlite3_free(p);
3848 p = 0;
3849 if( objc>1 ){
3850 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3851 if( p==0 ) return TCL_ERROR;
3852 for(i=0; i<objc-1; i++){
3853 if( Tcl_GetIntFromObj(interp, objv[1+i], &p[i]) ){
3854 sqlite3_free(p);
3855 p = 0;
3856 return TCL_ERROR;
3860 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3861 return TCL_OK;
3864 ** Usage: intarray_addr INT ...
3866 ** Return the address of a C-language array of 32-bit integers.
3868 ** Space to hold the array is obtained from malloc(). Call this procedure once
3869 ** with no arguments in order to release memory. Each call to this procedure
3870 ** overwrites the previous array.
3872 static int SQLITE_TCLAPI test_int64array_addr(
3873 void * clientData,
3874 Tcl_Interp *interp,
3875 int objc,
3876 Tcl_Obj *CONST objv[]
3878 int i;
3879 static sqlite3_int64 *p = 0;
3881 sqlite3_free(p);
3882 p = 0;
3883 if( objc>1 ){
3884 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3885 if( p==0 ) return TCL_ERROR;
3886 for(i=0; i<objc-1; i++){
3887 Tcl_WideInt v;
3888 if( Tcl_GetWideIntFromObj(interp, objv[1+i], &v) ){
3889 sqlite3_free(p);
3890 p = 0;
3891 return TCL_ERROR;
3893 p[i] = v;
3896 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3897 return TCL_OK;
3900 ** Usage: doublearray_addr INT ...
3902 ** Return the address of a C-language array of doubles.
3904 ** Space to hold the array is obtained from malloc(). Call this procedure once
3905 ** with no arguments in order to release memory. Each call to this procedure
3906 ** overwrites the previous array.
3908 static int SQLITE_TCLAPI test_doublearray_addr(
3909 void * clientData,
3910 Tcl_Interp *interp,
3911 int objc,
3912 Tcl_Obj *CONST objv[]
3914 int i;
3915 static double *p = 0;
3917 sqlite3_free(p);
3918 p = 0;
3919 if( objc>1 ){
3920 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3921 if( p==0 ) return TCL_ERROR;
3922 for(i=0; i<objc-1; i++){
3923 if( Tcl_GetDoubleFromObj(interp, objv[1+i], &p[i]) ){
3924 sqlite3_free(p);
3925 p = 0;
3926 return TCL_ERROR;
3930 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3931 return TCL_OK;
3934 ** Usage: textarray_addr TEXT ...
3936 ** Return the address of a C-language array of strings.
3938 ** Space to hold the array is obtained from malloc(). Call this procedure once
3939 ** with no arguments in order to release memory. Each call to this procedure
3940 ** overwrites the previous array.
3942 static int SQLITE_TCLAPI test_textarray_addr(
3943 void * clientData,
3944 Tcl_Interp *interp,
3945 int objc,
3946 Tcl_Obj *CONST objv[]
3948 int i;
3949 static int n = 0;
3950 static char **p = 0;
3952 for(i=0; i<n; i++) sqlite3_free(p[i]);
3953 sqlite3_free(p);
3954 p = 0;
3955 if( objc>1 ){
3956 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3957 if( p==0 ) return TCL_ERROR;
3958 for(i=0; i<objc-1; i++){
3959 p[i] = sqlite3_mprintf("%s", Tcl_GetString(objv[1+i]));
3962 n = objc-1;
3963 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3964 return TCL_OK;
3969 ** Usage: sqlite3_bind_int64 STMT N VALUE
3971 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
3972 ** N is the index of a wildcard in the prepared statement. This command
3973 ** binds a 64-bit integer VALUE to that wildcard.
3975 static int SQLITE_TCLAPI test_bind_int64(
3976 void * clientData,
3977 Tcl_Interp *interp,
3978 int objc,
3979 Tcl_Obj *CONST objv[]
3981 sqlite3_stmt *pStmt;
3982 int idx;
3983 Tcl_WideInt value;
3984 int rc;
3986 if( objc!=4 ){
3987 Tcl_AppendResult(interp, "wrong # args: should be \"",
3988 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3989 return TCL_ERROR;
3992 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3993 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3994 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3996 rc = sqlite3_bind_int64(pStmt, idx, value);
3997 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3998 if( rc!=SQLITE_OK ){
3999 return TCL_ERROR;
4002 return TCL_OK;
4007 ** Usage: sqlite3_bind_double STMT N VALUE
4009 ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
4010 ** N is the index of a wildcard in the prepared statement. This command
4011 ** binds a 64-bit integer VALUE to that wildcard.
4013 static int SQLITE_TCLAPI test_bind_double(
4014 void * clientData,
4015 Tcl_Interp *interp,
4016 int objc,
4017 Tcl_Obj *CONST objv[]
4019 sqlite3_stmt *pStmt;
4020 int idx;
4021 double value = 0;
4022 int rc;
4023 const char *zVal;
4024 int i;
4025 static const struct {
4026 const char *zName; /* Name of the special floating point value */
4027 unsigned int iUpper; /* Upper 32 bits */
4028 unsigned int iLower; /* Lower 32 bits */
4029 } aSpecialFp[] = {
4030 { "NaN", 0x7fffffff, 0xffffffff },
4031 { "SNaN", 0x7ff7ffff, 0xffffffff },
4032 { "-NaN", 0xffffffff, 0xffffffff },
4033 { "-SNaN", 0xfff7ffff, 0xffffffff },
4034 { "+Inf", 0x7ff00000, 0x00000000 },
4035 { "-Inf", 0xfff00000, 0x00000000 },
4036 { "Epsilon", 0x00000000, 0x00000001 },
4037 { "-Epsilon", 0x80000000, 0x00000001 },
4038 { "NaN0", 0x7ff80000, 0x00000000 },
4039 { "-NaN0", 0xfff80000, 0x00000000 },
4042 if( objc!=4 ){
4043 Tcl_AppendResult(interp, "wrong # args: should be \"",
4044 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
4045 return TCL_ERROR;
4048 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4049 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4051 /* Intercept the string "NaN" and generate a NaN value for it.
4052 ** All other strings are passed through to Tcl_GetDoubleFromObj().
4053 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
4054 ** contain a bug.
4056 zVal = Tcl_GetString(objv[3]);
4057 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
4058 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
4059 sqlite3_uint64 x;
4060 x = aSpecialFp[i].iUpper;
4061 x <<= 32;
4062 x |= aSpecialFp[i].iLower;
4063 assert( sizeof(value)==8 );
4064 assert( sizeof(x)==8 );
4065 memcpy(&value, &x, 8);
4066 break;
4069 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
4070 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
4071 return TCL_ERROR;
4073 rc = sqlite3_bind_double(pStmt, idx, value);
4074 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4075 if( rc!=SQLITE_OK ){
4076 return TCL_ERROR;
4079 return TCL_OK;
4083 ** Usage: sqlite3_bind_null STMT N
4085 ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
4086 ** N is the index of a wildcard in the prepared statement. This command
4087 ** binds a NULL to the wildcard.
4089 static int SQLITE_TCLAPI test_bind_null(
4090 void * clientData,
4091 Tcl_Interp *interp,
4092 int objc,
4093 Tcl_Obj *CONST objv[]
4095 sqlite3_stmt *pStmt;
4096 int idx;
4097 int rc;
4099 if( objc!=3 ){
4100 Tcl_AppendResult(interp, "wrong # args: should be \"",
4101 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
4102 return TCL_ERROR;
4105 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4106 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4108 rc = sqlite3_bind_null(pStmt, idx);
4109 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4110 if( rc!=SQLITE_OK ){
4111 return TCL_ERROR;
4114 return TCL_OK;
4118 ** Usage: sqlite3_bind_text STMT N STRING BYTES
4120 ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
4121 ** N is the index of a wildcard in the prepared statement. This command
4122 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
4123 ** long.
4125 static int SQLITE_TCLAPI test_bind_text(
4126 void * clientData,
4127 Tcl_Interp *interp,
4128 int objc,
4129 Tcl_Obj *CONST objv[]
4131 sqlite3_stmt *pStmt;
4132 int idx;
4133 int trueLength = 0;
4134 int bytes;
4135 char *value;
4136 int rc;
4137 char *toFree = 0;
4139 if( objc!=5 ){
4140 Tcl_AppendResult(interp, "wrong # args: should be \"",
4141 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
4142 return TCL_ERROR;
4145 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4146 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4147 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &trueLength);
4148 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
4149 if( bytes<0 ){
4150 toFree = malloc( trueLength + 1 );
4151 if( toFree==0 ){
4152 Tcl_AppendResult(interp, "out of memory", (void*)0);
4153 return TCL_ERROR;
4155 memcpy(toFree, value, trueLength);
4156 toFree[trueLength] = 0;
4157 value = toFree;
4159 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
4160 free(toFree);
4161 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4162 if( rc!=SQLITE_OK ){
4163 Tcl_AppendResult(interp, sqlite3ErrName(rc), (void*)0);
4164 return TCL_ERROR;
4167 return TCL_OK;
4171 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
4173 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
4174 ** N is the index of a wildcard in the prepared statement. This command
4175 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
4176 ** long.
4178 static int SQLITE_TCLAPI test_bind_text16(
4179 void * clientData,
4180 Tcl_Interp *interp,
4181 int objc,
4182 Tcl_Obj *CONST objv[]
4184 #ifndef SQLITE_OMIT_UTF16
4185 sqlite3_stmt *pStmt;
4186 int idx;
4187 int bytes;
4188 char *value;
4189 char *toFree = 0;
4190 int rc;
4191 int trueLength = 0;
4193 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
4194 Tcl_Obj *oStmt = objv[objc-4];
4195 Tcl_Obj *oN = objv[objc-3];
4196 Tcl_Obj *oString = objv[objc-2];
4197 Tcl_Obj *oBytes = objv[objc-1];
4199 if( objc!=5 && objc!=6){
4200 Tcl_AppendResult(interp, "wrong # args: should be \"",
4201 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
4202 return TCL_ERROR;
4205 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
4206 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
4207 value = (char*)Tcl_GetByteArrayFromObj(oString, &trueLength);
4208 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
4209 if( bytes<0 && xDel==SQLITE_TRANSIENT ){
4210 toFree = malloc( trueLength + 3 );
4211 if( toFree==0 ){
4212 Tcl_AppendResult(interp, "out of memory", (void*)0);
4213 return TCL_ERROR;
4215 memcpy(toFree, value, trueLength);
4216 memset(toFree+trueLength, 0, 3);
4217 value = toFree;
4219 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
4220 free(toFree);
4221 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4222 if( rc!=SQLITE_OK ){
4223 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
4224 return TCL_ERROR;
4227 #endif /* SQLITE_OMIT_UTF16 */
4228 return TCL_OK;
4232 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
4234 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
4235 ** N is the index of a wildcard in the prepared statement. This command
4236 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
4238 static int SQLITE_TCLAPI test_bind_blob(
4239 void * clientData,
4240 Tcl_Interp *interp,
4241 int objc,
4242 Tcl_Obj *CONST objv[]
4244 sqlite3_stmt *pStmt;
4245 int len, idx;
4246 int bytes;
4247 char *value;
4248 int rc;
4249 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
4251 if( objc!=5 && objc!=6 ){
4252 Tcl_AppendResult(interp, "wrong # args: should be \"",
4253 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
4254 return TCL_ERROR;
4257 if( objc==6 ){
4258 xDestructor = SQLITE_STATIC;
4259 objv++;
4262 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4263 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4265 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &len);
4266 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
4268 if( bytes>len ){
4269 char zBuf[200];
4270 sqlite3_snprintf(sizeof(zBuf), zBuf,
4271 "cannot use %d blob bytes, have %d", bytes, len);
4272 Tcl_AppendResult(interp, zBuf, (char*)0);
4273 return TCL_ERROR;
4276 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
4277 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4278 if( rc!=SQLITE_OK ){
4279 return TCL_ERROR;
4282 return TCL_OK;
4286 ** Usage: sqlite3_bind_value_from_preupdate STMT N NEW|OLD IDX
4288 ** Test the sqlite3_bind_value interface using sqlite3_value objects
4289 ** obtained from either sqlite3_preupdate_new() (if arg[3]=="new") or
4290 ** sqlite3_preupdate_old() if (arg[3]=="old"). IDX is the index to
4291 ** pass to the sqlite3_preupdate_xxx() function.
4293 static int SQLITE_TCLAPI test_bind_value_from_preupdate(
4294 void * clientData,
4295 Tcl_Interp *interp,
4296 int objc,
4297 Tcl_Obj *CONST objv[]
4299 sqlite3_stmt *pStmt;
4300 int idx;
4301 int bidx;
4302 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4303 const char *z3 = 0;
4304 sqlite3 *db = 0;
4305 sqlite3_value *pVal = 0;
4306 #endif
4308 if( objc!=5 ){
4309 Tcl_WrongNumArgs(interp, 1, objv, "STMT N NEW|OLD IDX");
4310 return TCL_ERROR;
4313 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4314 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4315 if( Tcl_GetIntFromObj(interp, objv[4], &bidx) ) return TCL_ERROR;
4317 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4318 z3 = Tcl_GetString(objv[3]);
4319 db = sqlite3_db_handle(pStmt);
4320 if( z3[0]=='n' ){
4321 sqlite3_preupdate_new(db, bidx, &pVal);
4322 }else if( z3[0]=='o' ){
4323 sqlite3_preupdate_old(db, bidx, &pVal);
4324 }else{
4325 Tcl_AppendResult(interp, "expected new or old, got: ", z3, (char*)0);
4326 return TCL_ERROR;
4328 sqlite3_bind_value(pStmt, idx, pVal);
4329 #endif
4331 return TCL_OK;
4335 ** Usage: sqlite3_bind_value_from_select STMT N SELECT
4337 ** Test the sqlite3_bind_value interface. STMT is a prepared statement.
4338 ** N is the index of a wildcard in the prepared statement.
4340 static int SQLITE_TCLAPI test_bind_value_from_select(
4341 void * clientData,
4342 Tcl_Interp *interp,
4343 int objc,
4344 Tcl_Obj *CONST objv[]
4346 sqlite3_stmt *pStmt;
4347 sqlite3_stmt *pStmt2;
4348 int idx;
4349 const char *zSql = 0;
4350 sqlite3 *db = 0;
4351 int rc = SQLITE_OK;
4353 if( objc!=4 ){
4354 Tcl_WrongNumArgs(interp, 1, objv, "STMT N SELECT");
4355 return TCL_ERROR;
4358 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4359 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4360 zSql = Tcl_GetString(objv[3]);
4361 db = sqlite3_db_handle(pStmt);
4363 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt2, 0);
4364 if( rc!=SQLITE_OK ){
4365 Tcl_AppendResult(interp, "error in SQL: ", sqlite3_errmsg(db), (char*)0);
4366 return TCL_ERROR;
4368 if( sqlite3_step(pStmt2)==SQLITE_ROW ){
4369 sqlite3_value *pVal = sqlite3_column_value(pStmt2, 0);
4370 sqlite3_bind_value(pStmt, idx, pVal);
4372 rc = sqlite3_finalize(pStmt2);
4373 if( rc!=SQLITE_OK ){
4374 Tcl_AppendResult(interp,
4375 "error runnning SQL: ", sqlite3_errmsg(db), (char*)0
4377 return TCL_ERROR;
4380 return TCL_OK;
4383 #ifdef _WIN32
4384 struct iovec {
4385 void *iov_base;
4386 size_t iov_len;
4388 #else
4389 # include <sys/uio.h>
4390 #endif
4392 #ifndef SQLITE_OMIT_VIRTUALTABLE
4394 ** sqlite3_carray_bind [options...] STMT NAME VALUE ...
4396 ** Options:
4397 ** -transient
4398 ** -static
4399 ** -int32
4400 ** -int64
4401 ** -double
4402 ** -text
4403 ** -blob
4405 ** Each call clears static data. Called with no options does nothing
4406 ** but clear static data.
4408 static int SQLITE_TCLAPI test_carray_bind(
4409 void * clientData,
4410 Tcl_Interp *interp,
4411 int objc,
4412 Tcl_Obj *CONST objv[]
4414 sqlite3_stmt *pStmt;
4415 int eType = 0; /* CARRAY_INT32 */
4416 int nData = 0;
4417 void *aData = 0;
4418 int isTransient = 0;
4419 int isStatic = 0;
4420 int idx;
4421 int i, j;
4422 int rc;
4423 void (*xDel)(void*) = sqlite3_free;
4424 static void *aStaticData = 0;
4425 static int nStaticData = 0;
4426 static int eStaticType = 0;
4427 extern int sqlite3_carray_bind(
4428 sqlite3_stmt *pStmt,
4429 int i,
4430 void *aData,
4431 int nData,
4432 int mFlags,
4433 void (*xDestroy)(void*)
4436 if( aStaticData ){
4437 /* Always clear preexisting static data on every call */
4438 if( eStaticType==3 ){
4439 for(i=0; i<nStaticData; i++){
4440 sqlite3_free(((char**)aStaticData)[i]);
4443 if( eStaticType==4 ){
4444 for(i=0; i<nStaticData; i++){
4445 sqlite3_free(((struct iovec*)aStaticData)[i].iov_base);
4448 sqlite3_free(aStaticData);
4449 aStaticData = 0;
4450 nStaticData = 0;
4451 eStaticType = 0;
4453 if( objc==1 ) return TCL_OK;
4455 for(i=1; i<objc && Tcl_GetString(objv[i])[0]=='-'; i++){
4456 const char *z = Tcl_GetString(objv[i]);
4457 if( strcmp(z, "-transient")==0 ){
4458 isTransient = 1;
4459 xDel = SQLITE_TRANSIENT;
4460 }else
4461 if( strcmp(z, "-static")==0 ){
4462 isStatic = 1;
4463 xDel = SQLITE_STATIC;
4464 }else
4465 if( strcmp(z, "-int32")==0 ){
4466 eType = 0; /* CARRAY_INT32 */
4467 }else
4468 if( strcmp(z, "-int64")==0 ){
4469 eType = 1; /* CARRAY_INT64 */
4470 }else
4471 if( strcmp(z, "-double")==0 ){
4472 eType = 2; /* CARRAY_DOUBLE */
4473 }else
4474 if( strcmp(z, "-text")==0 ){
4475 eType = 3; /* CARRAY_TEXT */
4476 }else
4477 if( strcmp(z, "-blob")==0 ){
4478 eType = 4; /* CARRAY_BLOB */
4479 }else
4480 if( strcmp(z, "--")==0 ){
4481 break;
4482 }else
4484 Tcl_AppendResult(interp, "unknown option: ", z, (char*)0);
4485 return TCL_ERROR;
4488 if( eType==3 && !isStatic && !isTransient ){
4489 Tcl_AppendResult(interp, "text data must be either -static or -transient",
4490 (char*)0);
4491 return TCL_ERROR;
4493 if( eType==4 && !isStatic && !isTransient ){
4494 Tcl_AppendResult(interp, "blob data must be either -static or -transient",
4495 (char*)0);
4496 return TCL_ERROR;
4498 if( isStatic && isTransient ){
4499 Tcl_AppendResult(interp, "cannot be both -static and -transient",
4500 (char*)0);
4501 return TCL_ERROR;
4503 if( objc-i < 2 ){
4504 Tcl_WrongNumArgs(interp, 1, objv, "[OPTIONS] STMT IDX VALUE ...");
4505 return TCL_ERROR;
4507 if( getStmtPointer(interp, Tcl_GetString(objv[i]), &pStmt) ) return TCL_ERROR;
4508 i++;
4509 if( Tcl_GetIntFromObj(interp, objv[i], &idx) ) return TCL_ERROR;
4510 i++;
4511 nData = objc - i;
4512 switch( eType + 5*(nData<=0) ){
4513 case 0: { /* INT32 */
4514 int *a = sqlite3_malloc( sizeof(int)*nData );
4515 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4516 for(j=0; j<nData; j++){
4517 int v;
4518 if( Tcl_GetIntFromObj(interp, objv[i+j], &v) ){
4519 sqlite3_free(a);
4520 return TCL_ERROR;
4522 a[j] = v;
4524 aData = a;
4525 break;
4527 case 1: { /* INT64 */
4528 sqlite3_int64 *a = sqlite3_malloc( sizeof(sqlite3_int64)*nData );
4529 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4530 for(j=0; j<nData; j++){
4531 Tcl_WideInt v;
4532 if( Tcl_GetWideIntFromObj(interp, objv[i+j], &v) ){
4533 sqlite3_free(a);
4534 return TCL_ERROR;
4536 a[j] = v;
4538 aData = a;
4539 break;
4541 case 2: { /* DOUBLE */
4542 double *a = sqlite3_malloc( sizeof(double)*nData );
4543 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4544 for(j=0; j<nData; j++){
4545 double v;
4546 if( Tcl_GetDoubleFromObj(interp, objv[i+j], &v) ){
4547 sqlite3_free(a);
4548 return TCL_ERROR;
4550 a[j] = v;
4552 aData = a;
4553 break;
4555 case 3: { /* TEXT */
4556 char **a = sqlite3_malloc( sizeof(char*)*nData );
4557 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4558 for(j=0; j<nData; j++){
4559 const char *v = Tcl_GetString(objv[i+j]);
4560 a[j] = sqlite3_mprintf("%s", v);
4562 aData = a;
4563 break;
4565 case 4: { /* BLOB */
4566 struct iovec *a = sqlite3_malloc( sizeof(struct iovec)*nData );
4567 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4568 for(j=0; j<nData; j++){
4569 int n = 0;
4570 unsigned char *v = Tcl_GetByteArrayFromObj(objv[i+i], &n);
4571 a[j].iov_len = n;
4572 a[j].iov_base = sqlite3_malloc64( n );
4573 if( a[j].iov_base==0 ){
4574 a[j].iov_len = 0;
4575 }else{
4576 memcpy(a[j].iov_base, v, n);
4579 aData = a;
4580 break;
4582 case 5: { /* nData==0 */
4583 aData = "";
4584 xDel = SQLITE_STATIC;
4585 isTransient = 0;
4586 isStatic = 0;
4587 break;
4590 if( isStatic ){
4591 aStaticData = aData;
4592 nStaticData = nData;
4593 eStaticType = eType;
4595 rc = sqlite3_carray_bind(pStmt, idx, aData, nData, eType, xDel);
4596 if( isTransient ){
4597 if( eType==3 ){
4598 for(i=0; i<nData; i++) sqlite3_free(((char**)aData)[i]);
4600 if( eType==4 ){
4601 for(i=0; i<nData; i++) sqlite3_free(((struct iovec*)aData)[i].iov_base);
4603 sqlite3_free(aData);
4605 carray_bind_done:
4606 if( rc ){
4607 Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0);
4608 return TCL_ERROR;
4610 return TCL_OK;
4612 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4615 ** Usage: sqlite3_bind_parameter_count STMT
4617 ** Return the number of wildcards in the given statement.
4619 static int SQLITE_TCLAPI test_bind_parameter_count(
4620 void * clientData,
4621 Tcl_Interp *interp,
4622 int objc,
4623 Tcl_Obj *CONST objv[]
4625 sqlite3_stmt *pStmt;
4627 if( objc!=2 ){
4628 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4629 return TCL_ERROR;
4631 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4632 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
4633 return TCL_OK;
4637 ** Usage: sqlite3_bind_parameter_name STMT N
4639 ** Return the name of the Nth wildcard. The first wildcard is 1.
4640 ** An empty string is returned if N is out of range or if the wildcard
4641 ** is nameless.
4643 static int SQLITE_TCLAPI test_bind_parameter_name(
4644 void * clientData,
4645 Tcl_Interp *interp,
4646 int objc,
4647 Tcl_Obj *CONST objv[]
4649 sqlite3_stmt *pStmt;
4650 int i;
4652 if( objc!=3 ){
4653 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
4654 return TCL_ERROR;
4656 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4657 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
4658 Tcl_SetObjResult(interp,
4659 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
4661 return TCL_OK;
4665 ** Usage: sqlite3_bind_parameter_index STMT NAME
4667 ** Return the index of the wildcard called NAME. Return 0 if there is
4668 ** no such wildcard.
4670 static int SQLITE_TCLAPI test_bind_parameter_index(
4671 void * clientData,
4672 Tcl_Interp *interp,
4673 int objc,
4674 Tcl_Obj *CONST objv[]
4676 sqlite3_stmt *pStmt;
4678 if( objc!=3 ){
4679 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
4680 return TCL_ERROR;
4682 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4683 Tcl_SetObjResult(interp,
4684 Tcl_NewIntObj(
4685 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
4688 return TCL_OK;
4692 ** Usage: sqlite3_clear_bindings STMT
4695 static int SQLITE_TCLAPI test_clear_bindings(
4696 void * clientData,
4697 Tcl_Interp *interp,
4698 int objc,
4699 Tcl_Obj *CONST objv[]
4701 sqlite3_stmt *pStmt;
4703 if( objc!=2 ){
4704 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4705 return TCL_ERROR;
4707 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4708 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
4709 return TCL_OK;
4713 ** Usage: sqlite3_sleep MILLISECONDS
4715 static int SQLITE_TCLAPI test_sleep(
4716 void * clientData,
4717 Tcl_Interp *interp,
4718 int objc,
4719 Tcl_Obj *CONST objv[]
4721 int ms;
4723 if( objc!=2 ){
4724 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
4725 return TCL_ERROR;
4727 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
4728 return TCL_ERROR;
4730 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
4731 return TCL_OK;
4735 ** Usage: sqlite3_extended_errcode DB
4737 ** Return the string representation of the most recent sqlite3_* API
4738 ** error code. e.g. "SQLITE_ERROR".
4740 static int SQLITE_TCLAPI test_ex_errcode(
4741 void * clientData,
4742 Tcl_Interp *interp,
4743 int objc,
4744 Tcl_Obj *CONST objv[]
4746 sqlite3 *db;
4747 int rc;
4749 if( objc!=2 ){
4750 Tcl_AppendResult(interp, "wrong # args: should be \"",
4751 Tcl_GetString(objv[0]), " DB", 0);
4752 return TCL_ERROR;
4754 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4755 rc = sqlite3_extended_errcode(db);
4756 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4757 return TCL_OK;
4762 ** Usage: sqlite3_errcode DB
4764 ** Return the string representation of the most recent sqlite3_* API
4765 ** error code. e.g. "SQLITE_ERROR".
4767 static int SQLITE_TCLAPI test_errcode(
4768 void * clientData,
4769 Tcl_Interp *interp,
4770 int objc,
4771 Tcl_Obj *CONST objv[]
4773 sqlite3 *db;
4774 int rc;
4776 if( objc!=2 ){
4777 Tcl_AppendResult(interp, "wrong # args: should be \"",
4778 Tcl_GetString(objv[0]), " DB", 0);
4779 return TCL_ERROR;
4781 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4782 rc = sqlite3_errcode(db);
4783 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4784 return TCL_OK;
4788 ** Usage: sqlite3_errmsg DB
4790 ** Returns the UTF-8 representation of the error message string for the
4791 ** most recent sqlite3_* API call.
4793 static int SQLITE_TCLAPI test_errmsg(
4794 void * clientData,
4795 Tcl_Interp *interp,
4796 int objc,
4797 Tcl_Obj *CONST objv[]
4799 sqlite3 *db;
4800 const char *zErr;
4802 if( objc!=2 ){
4803 Tcl_AppendResult(interp, "wrong # args: should be \"",
4804 Tcl_GetString(objv[0]), " DB", 0);
4805 return TCL_ERROR;
4807 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4809 zErr = sqlite3_errmsg(db);
4810 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
4811 return TCL_OK;
4816 ** Usage: sqlite3_error_offset DB
4818 ** Return the byte offset into the input UTF8 SQL for the most recent
4819 ** error, or -1 of the error does not refer to a specific token.
4821 static int SQLITE_TCLAPI test_error_offset(
4822 void * clientData,
4823 Tcl_Interp *interp,
4824 int objc,
4825 Tcl_Obj *CONST objv[]
4827 sqlite3 *db;
4828 int iByteOffset;
4830 if( objc!=2 ){
4831 Tcl_AppendResult(interp, "wrong # args: should be \"",
4832 Tcl_GetString(objv[0]), " DB", 0);
4833 return TCL_ERROR;
4835 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4837 iByteOffset = sqlite3_error_offset(db);
4838 Tcl_SetObjResult(interp, Tcl_NewIntObj(iByteOffset));
4839 return TCL_OK;
4843 ** Usage: test_errmsg16 DB
4845 ** Returns the UTF-16 representation of the error message string for the
4846 ** most recent sqlite3_* API call. This is a byte array object at the TCL
4847 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
4848 ** UTF-16 string.
4850 static int SQLITE_TCLAPI test_errmsg16(
4851 void * clientData,
4852 Tcl_Interp *interp,
4853 int objc,
4854 Tcl_Obj *CONST objv[]
4856 #ifndef SQLITE_OMIT_UTF16
4857 sqlite3 *db;
4858 const void *zErr;
4859 const char *z;
4860 int bytes = 0;
4862 if( objc!=2 ){
4863 Tcl_AppendResult(interp, "wrong # args: should be \"",
4864 Tcl_GetString(objv[0]), " DB", 0);
4865 return TCL_ERROR;
4867 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4869 zErr = sqlite3_errmsg16(db);
4870 if( zErr ){
4871 z = zErr;
4872 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
4874 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
4875 #endif /* SQLITE_OMIT_UTF16 */
4876 return TCL_OK;
4880 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
4882 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4883 ** database handle <DB>. The parameter <tailval> is the name of a global
4884 ** variable that is set to the unused portion of <sql> (if any). A
4885 ** STMT handle is returned.
4887 static int SQLITE_TCLAPI test_prepare(
4888 void * clientData,
4889 Tcl_Interp *interp,
4890 int objc,
4891 Tcl_Obj *CONST objv[]
4893 sqlite3 *db;
4894 const char *zSql;
4895 int bytes;
4896 const char *zTail = 0;
4897 sqlite3_stmt *pStmt = 0;
4898 char zBuf[50];
4899 int rc;
4901 if( objc!=5 && objc!=4 ){
4902 Tcl_AppendResult(interp, "wrong # args: should be \"",
4903 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4904 return TCL_ERROR;
4906 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4907 zSql = Tcl_GetString(objv[2]);
4908 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4910 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4911 Tcl_ResetResult(interp);
4912 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4913 if( zTail && objc>=5 ){
4914 if( bytes>=0 ){
4915 bytes = bytes - (int)(zTail-zSql);
4917 if( (int)strlen(zTail)<bytes ){
4918 bytes = (int)strlen(zTail);
4920 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4922 if( rc!=SQLITE_OK ){
4923 assert( pStmt==0 );
4924 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4925 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4926 return TCL_ERROR;
4929 if( pStmt ){
4930 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4931 Tcl_AppendResult(interp, zBuf, 0);
4933 return TCL_OK;
4937 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
4939 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4940 ** database handle <DB>. The parameter <tailval> is the name of a global
4941 ** variable that is set to the unused portion of <sql> (if any). A
4942 ** STMT handle is returned.
4944 static int SQLITE_TCLAPI test_prepare_v2(
4945 void * clientData,
4946 Tcl_Interp *interp,
4947 int objc,
4948 Tcl_Obj *CONST objv[]
4950 sqlite3 *db;
4951 const char *zSql;
4952 char *zCopy = 0; /* malloc() copy of zSql */
4953 int bytes;
4954 const char *zTail = 0;
4955 const char **pzTail;
4956 sqlite3_stmt *pStmt = 0;
4957 char zBuf[50];
4958 int rc;
4960 if( objc!=5 && objc!=4 ){
4961 Tcl_AppendResult(interp, "wrong # args: should be \"",
4962 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
4963 return TCL_ERROR;
4965 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4966 zSql = Tcl_GetString(objv[2]);
4967 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4969 /* Instead of using zSql directly, make a copy into a buffer obtained
4970 ** directly from malloc(). The idea is to make it easier for valgrind
4971 ** to spot buffer overreads. */
4972 if( bytes>=0 ){
4973 zCopy = malloc(bytes);
4974 memcpy(zCopy, zSql, bytes);
4975 }else{
4976 int n = (int)strlen(zSql) + 1;
4977 zCopy = malloc(n);
4978 memcpy(zCopy, zSql, n);
4980 pzTail = objc>=5 ? &zTail : 0;
4981 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail);
4982 if( objc>=5 ){
4983 zTail = &zSql[(zTail - zCopy)];
4985 free(zCopy);
4987 assert(rc==SQLITE_OK || pStmt==0);
4988 Tcl_ResetResult(interp);
4989 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4990 if( rc==SQLITE_OK && objc>=5 && zTail ){
4991 if( bytes>=0 ){
4992 bytes = bytes - (int)(zTail-zSql);
4994 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4996 if( rc!=SQLITE_OK ){
4997 assert( pStmt==0 );
4998 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4999 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
5000 return TCL_ERROR;
5003 if( pStmt ){
5004 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5005 Tcl_AppendResult(interp, zBuf, 0);
5007 return TCL_OK;
5011 ** Usage: sqlite3_prepare_v3 DB sql bytes flags ?tailvar?
5013 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
5014 ** database handle <DB> and flags <flags>. The parameter <tailval> is
5015 ** the name of a global variable that is set to the unused portion of
5016 ** <sql> (if any). A STMT handle is returned.
5018 static int SQLITE_TCLAPI test_prepare_v3(
5019 void * clientData,
5020 Tcl_Interp *interp,
5021 int objc,
5022 Tcl_Obj *CONST objv[]
5024 sqlite3 *db;
5025 const char *zSql;
5026 char *zCopy = 0; /* malloc() copy of zSql */
5027 int bytes, flags;
5028 const char *zTail = 0;
5029 const char **pzTail;
5030 sqlite3_stmt *pStmt = 0;
5031 char zBuf[50];
5032 int rc;
5034 if( objc!=6 && objc!=5 ){
5035 Tcl_AppendResult(interp, "wrong # args: should be \"",
5036 Tcl_GetString(objv[0]), " DB sql bytes flags tailvar", 0);
5037 return TCL_ERROR;
5039 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5040 zSql = Tcl_GetString(objv[2]);
5041 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5042 if( Tcl_GetIntFromObj(interp, objv[4], &flags) ) return TCL_ERROR;
5044 /* Instead of using zSql directly, make a copy into a buffer obtained
5045 ** directly from malloc(). The idea is to make it easier for valgrind
5046 ** to spot buffer overreads. */
5047 if( bytes>=0 ){
5048 zCopy = malloc(bytes);
5049 memcpy(zCopy, zSql, bytes);
5050 }else{
5051 int n = (int)strlen(zSql) + 1;
5052 zCopy = malloc(n);
5053 memcpy(zCopy, zSql, n);
5055 pzTail = objc>=6 ? &zTail : 0;
5056 rc = sqlite3_prepare_v3(db, zCopy, bytes, (unsigned int)flags,&pStmt,pzTail);
5057 free(zCopy);
5058 zTail = &zSql[(zTail - zCopy)];
5060 assert(rc==SQLITE_OK || pStmt==0);
5061 Tcl_ResetResult(interp);
5062 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5063 if( rc==SQLITE_OK && zTail && objc>=6 ){
5064 if( bytes>=0 ){
5065 bytes = bytes - (int)(zTail-zSql);
5067 Tcl_ObjSetVar2(interp, objv[5], 0, Tcl_NewStringObj(zTail, bytes), 0);
5069 if( rc!=SQLITE_OK ){
5070 assert( pStmt==0 );
5071 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
5072 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
5073 return TCL_ERROR;
5076 if( pStmt ){
5077 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5078 Tcl_AppendResult(interp, zBuf, 0);
5080 return TCL_OK;
5084 ** Usage: sqlite3_prepare_tkt3134 DB
5086 ** Generate a prepared statement for a zero-byte string as a test
5087 ** for ticket #3134. The string should be preceded by a zero byte.
5089 static int SQLITE_TCLAPI test_prepare_tkt3134(
5090 void * clientData,
5091 Tcl_Interp *interp,
5092 int objc,
5093 Tcl_Obj *CONST objv[]
5095 sqlite3 *db;
5096 static const char zSql[] = "\000SELECT 1";
5097 sqlite3_stmt *pStmt = 0;
5098 char zBuf[50];
5099 int rc;
5101 if( objc!=2 ){
5102 Tcl_AppendResult(interp, "wrong # args: should be \"",
5103 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
5104 return TCL_ERROR;
5106 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5107 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
5108 assert(rc==SQLITE_OK || pStmt==0);
5109 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5110 if( rc!=SQLITE_OK ){
5111 assert( pStmt==0 );
5112 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
5113 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
5114 return TCL_ERROR;
5117 if( pStmt ){
5118 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5119 Tcl_AppendResult(interp, zBuf, 0);
5121 return TCL_OK;
5125 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
5127 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
5128 ** database handle <DB>. The parameter <tailval> is the name of a global
5129 ** variable that is set to the unused portion of <sql> (if any). A
5130 ** STMT handle is returned.
5132 static int SQLITE_TCLAPI test_prepare16(
5133 void * clientData,
5134 Tcl_Interp *interp,
5135 int objc,
5136 Tcl_Obj *CONST objv[]
5138 #ifndef SQLITE_OMIT_UTF16
5139 sqlite3 *db;
5140 const void *zSql;
5141 const void *zTail = 0;
5142 Tcl_Obj *pTail = 0;
5143 sqlite3_stmt *pStmt = 0;
5144 char zBuf[50];
5145 int rc;
5146 int bytes; /* The integer specified as arg 3 */
5147 int objlen; /* The byte-array length of arg 2 */
5149 if( objc!=5 && objc!=4 ){
5150 Tcl_AppendResult(interp, "wrong # args: should be \"",
5151 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
5152 return TCL_ERROR;
5154 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5155 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
5156 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5158 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
5159 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5160 if( rc ){
5161 return TCL_ERROR;
5164 if( objc>=5 ){
5165 if( zTail ){
5166 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
5167 }else{
5168 objlen = 0;
5170 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
5171 Tcl_IncrRefCount(pTail);
5172 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
5173 Tcl_DecrRefCount(pTail);
5176 if( pStmt ){
5177 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5179 Tcl_AppendResult(interp, zBuf, 0);
5180 #endif /* SQLITE_OMIT_UTF16 */
5181 return TCL_OK;
5185 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
5187 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
5188 ** database handle <DB>. The parameter <tailval> is the name of a global
5189 ** variable that is set to the unused portion of <sql> (if any). A
5190 ** STMT handle is returned.
5192 static int SQLITE_TCLAPI test_prepare16_v2(
5193 void * clientData,
5194 Tcl_Interp *interp,
5195 int objc,
5196 Tcl_Obj *CONST objv[]
5198 #ifndef SQLITE_OMIT_UTF16
5199 sqlite3 *db;
5200 const void *zSql;
5201 const void *zTail = 0;
5202 Tcl_Obj *pTail = 0;
5203 sqlite3_stmt *pStmt = 0;
5204 char zBuf[50];
5205 int rc;
5206 int bytes; /* The integer specified as arg 3 */
5207 int objlen; /* The byte-array length of arg 2 */
5209 if( objc!=5 && objc!=4 ){
5210 Tcl_AppendResult(interp, "wrong # args: should be \"",
5211 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
5212 return TCL_ERROR;
5214 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5215 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
5216 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5218 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
5219 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5220 if( rc ){
5221 return TCL_ERROR;
5224 if( objc>=5 ){
5225 if( zTail ){
5226 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
5227 }else{
5228 objlen = 0;
5230 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
5231 Tcl_IncrRefCount(pTail);
5232 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
5233 Tcl_DecrRefCount(pTail);
5236 if( pStmt ){
5237 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5239 Tcl_AppendResult(interp, zBuf, 0);
5240 #endif /* SQLITE_OMIT_UTF16 */
5241 return TCL_OK;
5245 ** Usage: sqlite3_open filename ?options-list?
5247 static int SQLITE_TCLAPI test_open(
5248 void * clientData,
5249 Tcl_Interp *interp,
5250 int objc,
5251 Tcl_Obj *CONST objv[]
5253 const char *zFilename;
5254 sqlite3 *db;
5255 char zBuf[100];
5257 if( objc!=3 && objc!=2 && objc!=1 ){
5258 Tcl_AppendResult(interp, "wrong # args: should be \"",
5259 Tcl_GetString(objv[0]), " filename options-list", 0);
5260 return TCL_ERROR;
5263 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
5264 sqlite3_open(zFilename, &db);
5266 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5267 Tcl_AppendResult(interp, zBuf, 0);
5268 return TCL_OK;
5272 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
5274 static int SQLITE_TCLAPI test_open_v2(
5275 void * clientData,
5276 Tcl_Interp *interp,
5277 int objc,
5278 Tcl_Obj *CONST objv[]
5280 const char *zFilename;
5281 const char *zVfs;
5282 int flags = 0;
5283 sqlite3 *db;
5284 int rc;
5285 char zBuf[100];
5287 int nFlag;
5288 Tcl_Obj **apFlag;
5289 int i;
5291 if( objc!=4 ){
5292 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
5293 return TCL_ERROR;
5295 zFilename = Tcl_GetString(objv[1]);
5296 zVfs = Tcl_GetString(objv[3]);
5297 if( zVfs[0]==0x00 ) zVfs = 0;
5299 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
5300 if( rc!=TCL_OK ) return rc;
5301 for(i=0; i<nFlag; i++){
5302 int iFlag;
5303 struct OpenFlag {
5304 const char *zFlag;
5305 int flag;
5306 } aFlag[] = {
5307 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
5308 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
5309 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
5310 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
5311 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
5312 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
5313 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
5314 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
5315 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
5316 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
5317 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
5318 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
5319 { "SQLITE_OPEN_SUPER_JOURNAL", SQLITE_OPEN_SUPER_JOURNAL },
5320 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
5321 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
5322 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
5323 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
5324 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
5325 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
5326 { "SQLITE_OPEN_EXRESCODE", SQLITE_OPEN_EXRESCODE },
5327 { 0, 0 }
5329 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
5330 "flag", 0, &iFlag
5332 if( rc!=TCL_OK ) return rc;
5333 flags |= aFlag[iFlag].flag;
5336 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
5337 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5338 Tcl_AppendResult(interp, zBuf, 0);
5339 return TCL_OK;
5343 ** Usage: sqlite3_open16 filename options
5345 static int SQLITE_TCLAPI test_open16(
5346 void * clientData,
5347 Tcl_Interp *interp,
5348 int objc,
5349 Tcl_Obj *CONST objv[]
5351 #ifndef SQLITE_OMIT_UTF16
5352 const void *zFilename;
5353 sqlite3 *db;
5354 char zBuf[100];
5356 if( objc!=3 ){
5357 Tcl_AppendResult(interp, "wrong # args: should be \"",
5358 Tcl_GetString(objv[0]), " filename options-list", 0);
5359 return TCL_ERROR;
5362 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
5363 sqlite3_open16(zFilename, &db);
5365 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5366 Tcl_AppendResult(interp, zBuf, 0);
5367 #endif /* SQLITE_OMIT_UTF16 */
5368 return TCL_OK;
5372 ** Usage: sqlite3_complete16 <UTF-16 string>
5374 ** Return 1 if the supplied argument is a complete SQL statement, or zero
5375 ** otherwise.
5377 static int SQLITE_TCLAPI test_complete16(
5378 void * clientData,
5379 Tcl_Interp *interp,
5380 int objc,
5381 Tcl_Obj *CONST objv[]
5383 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
5384 char *zBuf;
5386 if( objc!=2 ){
5387 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
5388 return TCL_ERROR;
5391 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
5392 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
5393 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
5394 return TCL_OK;
5398 ** Usage: sqlite3_normalize SQL
5400 ** Return the normalized value for an SQL statement.
5402 static int SQLITE_TCLAPI test_normalize(
5403 void * clientData,
5404 Tcl_Interp *interp,
5405 int objc,
5406 Tcl_Obj *CONST objv[]
5408 char *zSql;
5409 char *zNorm;
5410 extern char *sqlite3_normalize(const char*);
5412 if( objc!=2 ){
5413 Tcl_WrongNumArgs(interp, 1, objv, "SQL");
5414 return TCL_ERROR;
5417 zSql = (char*)Tcl_GetString(objv[1]);
5418 zNorm = sqlite3_normalize(zSql);
5419 if( zNorm ){
5420 Tcl_SetObjResult(interp, Tcl_NewStringObj(zNorm, -1));
5421 sqlite3_free(zNorm);
5423 return TCL_OK;
5427 ** Usage: sqlite3_step STMT
5429 ** Advance the statement to the next row.
5431 static int SQLITE_TCLAPI test_step(
5432 void * clientData,
5433 Tcl_Interp *interp,
5434 int objc,
5435 Tcl_Obj *CONST objv[]
5437 sqlite3_stmt *pStmt;
5438 int rc;
5440 if( objc!=2 ){
5441 Tcl_AppendResult(interp, "wrong # args: should be \"",
5442 Tcl_GetString(objv[0]), " STMT", 0);
5443 return TCL_ERROR;
5446 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5447 rc = sqlite3_step(pStmt);
5449 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
5450 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
5451 return TCL_OK;
5454 static int SQLITE_TCLAPI test_sql(
5455 void * clientData,
5456 Tcl_Interp *interp,
5457 int objc,
5458 Tcl_Obj *CONST objv[]
5460 sqlite3_stmt *pStmt;
5462 if( objc!=2 ){
5463 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5464 return TCL_ERROR;
5467 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5468 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
5469 return TCL_OK;
5471 static int SQLITE_TCLAPI test_ex_sql(
5472 void * clientData,
5473 Tcl_Interp *interp,
5474 int objc,
5475 Tcl_Obj *CONST objv[]
5477 sqlite3_stmt *pStmt;
5478 char *z;
5480 if( objc!=2 ){
5481 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5482 return TCL_ERROR;
5485 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5486 z = sqlite3_expanded_sql(pStmt);
5487 Tcl_SetResult(interp, z, TCL_VOLATILE);
5488 sqlite3_free(z);
5489 return TCL_OK;
5491 #ifdef SQLITE_ENABLE_NORMALIZE
5492 static int SQLITE_TCLAPI test_norm_sql(
5493 void * clientData,
5494 Tcl_Interp *interp,
5495 int objc,
5496 Tcl_Obj *CONST objv[]
5498 sqlite3_stmt *pStmt;
5500 if( objc!=2 ){
5501 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5502 return TCL_ERROR;
5505 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5506 Tcl_SetResult(interp, (char *)sqlite3_normalized_sql(pStmt), TCL_VOLATILE);
5507 return TCL_OK;
5509 #endif /* SQLITE_ENABLE_NORMALIZE */
5512 ** Usage: sqlite3_column_count STMT
5514 ** Return the number of columns returned by the sql statement STMT.
5516 static int SQLITE_TCLAPI test_column_count(
5517 void * clientData,
5518 Tcl_Interp *interp,
5519 int objc,
5520 Tcl_Obj *CONST objv[]
5522 sqlite3_stmt *pStmt;
5524 if( objc!=2 ){
5525 Tcl_AppendResult(interp, "wrong # args: should be \"",
5526 Tcl_GetString(objv[0]), " STMT column", 0);
5527 return TCL_ERROR;
5530 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5532 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
5533 return TCL_OK;
5537 ** Usage: sqlite3_column_type STMT column
5539 ** Return the type of the data in column 'column' of the current row.
5541 static int SQLITE_TCLAPI test_column_type(
5542 void * clientData,
5543 Tcl_Interp *interp,
5544 int objc,
5545 Tcl_Obj *CONST objv[]
5547 sqlite3_stmt *pStmt;
5548 int col;
5549 int tp;
5551 if( objc!=3 ){
5552 Tcl_AppendResult(interp, "wrong # args: should be \"",
5553 Tcl_GetString(objv[0]), " STMT column", 0);
5554 return TCL_ERROR;
5557 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5558 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5560 tp = sqlite3_column_type(pStmt, col);
5561 switch( tp ){
5562 case SQLITE_INTEGER:
5563 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
5564 break;
5565 case SQLITE_NULL:
5566 Tcl_SetResult(interp, "NULL", TCL_STATIC);
5567 break;
5568 case SQLITE_FLOAT:
5569 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
5570 break;
5571 case SQLITE_TEXT:
5572 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
5573 break;
5574 case SQLITE_BLOB:
5575 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
5576 break;
5577 default:
5578 assert(0);
5581 return TCL_OK;
5585 ** Usage: sqlite3_column_int64 STMT column
5587 ** Return the data in column 'column' of the current row cast as an
5588 ** wide (64-bit) integer.
5590 static int SQLITE_TCLAPI test_column_int64(
5591 void * clientData,
5592 Tcl_Interp *interp,
5593 int objc,
5594 Tcl_Obj *CONST objv[]
5596 sqlite3_stmt *pStmt;
5597 int col;
5598 i64 iVal;
5600 if( objc!=3 ){
5601 Tcl_AppendResult(interp, "wrong # args: should be \"",
5602 Tcl_GetString(objv[0]), " STMT column", 0);
5603 return TCL_ERROR;
5606 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5607 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5609 iVal = sqlite3_column_int64(pStmt, col);
5610 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
5611 return TCL_OK;
5615 ** Usage: sqlite3_column_blob STMT column
5617 static int SQLITE_TCLAPI test_column_blob(
5618 void * clientData,
5619 Tcl_Interp *interp,
5620 int objc,
5621 Tcl_Obj *CONST objv[]
5623 sqlite3_stmt *pStmt;
5624 int col;
5626 int len;
5627 const void *pBlob;
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 len = sqlite3_column_bytes(pStmt, col);
5639 pBlob = sqlite3_column_blob(pStmt, col);
5640 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
5641 return TCL_OK;
5645 ** Usage: sqlite3_column_double STMT column
5647 ** Return the data in column 'column' of the current row cast as a double.
5649 static int SQLITE_TCLAPI test_column_double(
5650 void * clientData,
5651 Tcl_Interp *interp,
5652 int objc,
5653 Tcl_Obj *CONST objv[]
5655 sqlite3_stmt *pStmt;
5656 int col;
5657 double rVal;
5659 if( objc!=3 ){
5660 Tcl_AppendResult(interp, "wrong # args: should be \"",
5661 Tcl_GetString(objv[0]), " STMT column", 0);
5662 return TCL_ERROR;
5665 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5666 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5668 rVal = sqlite3_column_double(pStmt, col);
5669 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
5670 return TCL_OK;
5674 ** Usage: sqlite3_data_count STMT
5676 ** Return the number of columns returned by the sql statement STMT.
5678 static int SQLITE_TCLAPI test_data_count(
5679 void * clientData,
5680 Tcl_Interp *interp,
5681 int objc,
5682 Tcl_Obj *CONST objv[]
5684 sqlite3_stmt *pStmt;
5686 if( objc!=2 ){
5687 Tcl_AppendResult(interp, "wrong # args: should be \"",
5688 Tcl_GetString(objv[0]), " STMT column", 0);
5689 return TCL_ERROR;
5692 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5694 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
5695 return TCL_OK;
5699 ** Usage: sqlite3_column_text STMT column
5701 ** Usage: sqlite3_column_decltype STMT column
5703 ** Usage: sqlite3_column_name STMT column
5705 static int SQLITE_TCLAPI test_stmt_utf8(
5706 void * clientData, /* Pointer to SQLite API function to be invoke */
5707 Tcl_Interp *interp,
5708 int objc,
5709 Tcl_Obj *CONST objv[]
5711 sqlite3_stmt *pStmt;
5712 int col;
5713 const char *(*xFunc)(sqlite3_stmt*, int);
5714 const char *zRet;
5716 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
5717 if( objc!=3 ){
5718 Tcl_AppendResult(interp, "wrong # args: should be \"",
5719 Tcl_GetString(objv[0]), " STMT column", 0);
5720 return TCL_ERROR;
5723 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5724 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5725 zRet = xFunc(pStmt, col);
5726 if( zRet ){
5727 Tcl_SetResult(interp, (char *)zRet, 0);
5729 return TCL_OK;
5732 static int SQLITE_TCLAPI test_global_recover(
5733 void * clientData,
5734 Tcl_Interp *interp,
5735 int objc,
5736 Tcl_Obj *CONST objv[]
5738 #ifndef SQLITE_OMIT_DEPRECATED
5739 int rc;
5740 if( objc!=1 ){
5741 Tcl_WrongNumArgs(interp, 1, objv, "");
5742 return TCL_ERROR;
5744 rc = sqlite3_global_recover();
5745 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5746 #endif
5747 return TCL_OK;
5751 ** Usage: sqlite3_column_text STMT column
5753 ** Usage: sqlite3_column_decltype STMT column
5755 ** Usage: sqlite3_column_name STMT column
5757 static int SQLITE_TCLAPI test_stmt_utf16(
5758 void * clientData, /* Pointer to SQLite API function to be invoked */
5759 Tcl_Interp *interp,
5760 int objc,
5761 Tcl_Obj *CONST objv[]
5763 #ifndef SQLITE_OMIT_UTF16
5764 sqlite3_stmt *pStmt;
5765 int col;
5766 Tcl_Obj *pRet;
5767 const void *zName16;
5768 const void *(*xFunc)(sqlite3_stmt*, int);
5770 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
5771 if( objc!=3 ){
5772 Tcl_AppendResult(interp, "wrong # args: should be \"",
5773 Tcl_GetString(objv[0]), " STMT column", 0);
5774 return TCL_ERROR;
5777 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5778 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5780 zName16 = xFunc(pStmt, col);
5781 if( zName16 ){
5782 int n;
5783 const char *z = zName16;
5784 for(n=0; z[n] || z[n+1]; n+=2){}
5785 pRet = Tcl_NewByteArrayObj(zName16, n+2);
5786 Tcl_SetObjResult(interp, pRet);
5788 #endif /* SQLITE_OMIT_UTF16 */
5790 return TCL_OK;
5794 ** Usage: sqlite3_column_int STMT column
5796 ** Usage: sqlite3_column_bytes STMT column
5798 ** Usage: sqlite3_column_bytes16 STMT column
5801 static int SQLITE_TCLAPI test_stmt_int(
5802 void * clientData, /* Pointer to SQLite API function to be invoked */
5803 Tcl_Interp *interp,
5804 int objc,
5805 Tcl_Obj *CONST objv[]
5807 sqlite3_stmt *pStmt;
5808 int col;
5809 int (*xFunc)(sqlite3_stmt*, int);
5811 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
5812 if( objc!=3 ){
5813 Tcl_AppendResult(interp, "wrong # args: should be \"",
5814 Tcl_GetString(objv[0]), " STMT column", 0);
5815 return TCL_ERROR;
5818 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5819 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5821 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
5822 return TCL_OK;
5826 ** Usage: sqlite3_interrupt DB
5828 ** Trigger an interrupt on DB
5830 static int SQLITE_TCLAPI test_interrupt(
5831 void * clientData,
5832 Tcl_Interp *interp,
5833 int argc,
5834 char **argv
5836 sqlite3 *db;
5837 if( argc!=2 ){
5838 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5839 return TCL_ERROR;
5841 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5842 sqlite3_interrupt(db);
5843 return TCL_OK;
5847 ** Usage: sqlite3_is_interrupted DB
5849 ** return true if an interrupt is current in effect on DB
5851 static int SQLITE_TCLAPI test_is_interrupted(
5852 void * clientData,
5853 Tcl_Interp *interp,
5854 int argc,
5855 char **argv
5857 sqlite3 *db;
5858 int rc;
5859 if( argc!=2 ){
5860 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5861 return TCL_ERROR;
5863 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5864 rc = sqlite3_is_interrupted(db);
5865 Tcl_AppendResult(interp, rc ? "1" : "0", (void*)0);
5866 return TCL_OK;
5870 ** Usage: sqlite_delete_function DB function-name
5872 ** Delete the user function 'function-name' from database handle DB. It
5873 ** is assumed that the user function was created as UTF8, any number of
5874 ** arguments (the way the TCL interface does it).
5876 static int SQLITE_TCLAPI delete_function(
5877 void * clientData,
5878 Tcl_Interp *interp,
5879 int argc,
5880 char **argv
5882 int rc;
5883 sqlite3 *db;
5884 if( argc!=3 ){
5885 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5886 " DB function-name", 0);
5887 return TCL_ERROR;
5889 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5890 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
5891 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5892 return TCL_OK;
5896 ** Usage: sqlite_delete_collation DB collation-name
5898 ** Delete the collation sequence 'collation-name' from database handle
5899 ** DB. It is assumed that the collation sequence was created as UTF8 (the
5900 ** way the TCL interface does it).
5902 static int SQLITE_TCLAPI delete_collation(
5903 void * clientData,
5904 Tcl_Interp *interp,
5905 int argc,
5906 char **argv
5908 int rc;
5909 sqlite3 *db;
5910 if( argc!=3 ){
5911 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5912 " DB function-name", 0);
5913 return TCL_ERROR;
5915 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5916 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
5917 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5918 return TCL_OK;
5922 ** Usage: sqlite3_get_autocommit DB
5924 ** Return true if the database DB is currently in auto-commit mode.
5925 ** Return false if not.
5927 static int SQLITE_TCLAPI get_autocommit(
5928 void * clientData,
5929 Tcl_Interp *interp,
5930 int argc,
5931 char **argv
5933 char zBuf[30];
5934 sqlite3 *db;
5935 if( argc!=2 ){
5936 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5937 " DB", 0);
5938 return TCL_ERROR;
5940 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5941 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db));
5942 Tcl_AppendResult(interp, zBuf, 0);
5943 return TCL_OK;
5947 ** Usage: sqlite3_busy_timeout DB MS
5949 ** Set the busy timeout. This is more easily done using the timeout
5950 ** method of the TCL interface. But we need a way to test the case
5951 ** where it returns SQLITE_MISUSE.
5953 static int SQLITE_TCLAPI test_busy_timeout(
5954 void * clientData,
5955 Tcl_Interp *interp,
5956 int argc,
5957 char **argv
5959 int rc, ms;
5960 sqlite3 *db;
5961 if( argc!=3 ){
5962 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5963 " DB", 0);
5964 return TCL_ERROR;
5966 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5967 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
5968 rc = sqlite3_busy_timeout(db, ms);
5969 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
5970 return TCL_OK;
5974 ** Usage: tcl_variable_type VARIABLENAME
5976 ** Return the name of the internal representation for the
5977 ** value of the given variable.
5979 static int SQLITE_TCLAPI tcl_variable_type(
5980 void * clientData,
5981 Tcl_Interp *interp,
5982 int objc,
5983 Tcl_Obj *CONST objv[]
5985 Tcl_Obj *pVar;
5986 if( objc!=2 ){
5987 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
5988 return TCL_ERROR;
5990 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
5991 if( pVar==0 ) return TCL_ERROR;
5992 if( pVar->typePtr ){
5993 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
5995 return TCL_OK;
5999 ** Usage: sqlite3_release_memory ?N?
6001 ** Attempt to release memory currently held but not actually required.
6002 ** The integer N is the number of bytes we are trying to release. The
6003 ** return value is the amount of memory actually released.
6005 static int SQLITE_TCLAPI test_release_memory(
6006 void * clientData,
6007 Tcl_Interp *interp,
6008 int objc,
6009 Tcl_Obj *CONST objv[]
6011 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
6012 int N;
6013 int amt;
6014 if( objc!=1 && objc!=2 ){
6015 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
6016 return TCL_ERROR;
6018 if( objc==2 ){
6019 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
6020 }else{
6021 N = -1;
6023 amt = sqlite3_release_memory(N);
6024 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
6025 #endif
6026 return TCL_OK;
6031 ** Usage: sqlite3_db_release_memory DB
6033 ** Attempt to release memory currently held by database DB. Return the
6034 ** result code (which in the current implementation is always zero).
6036 static int SQLITE_TCLAPI test_db_release_memory(
6037 void * clientData,
6038 Tcl_Interp *interp,
6039 int objc,
6040 Tcl_Obj *CONST objv[]
6042 sqlite3 *db;
6043 int rc;
6044 if( objc!=2 ){
6045 Tcl_WrongNumArgs(interp, 1, objv, "DB");
6046 return TCL_ERROR;
6048 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6049 rc = sqlite3_db_release_memory(db);
6050 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6051 return TCL_OK;
6055 ** Usage: sqlite3_db_cacheflush DB
6057 ** Attempt to flush any dirty pages to disk.
6059 static int SQLITE_TCLAPI test_db_cacheflush(
6060 void * clientData,
6061 Tcl_Interp *interp,
6062 int objc,
6063 Tcl_Obj *CONST objv[]
6065 sqlite3 *db;
6066 int rc;
6067 if( objc!=2 ){
6068 Tcl_WrongNumArgs(interp, 1, objv, "DB");
6069 return TCL_ERROR;
6071 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6072 rc = sqlite3_db_cacheflush(db);
6073 if( rc ){
6074 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
6075 return TCL_ERROR;
6078 Tcl_ResetResult(interp);
6079 return TCL_OK;
6083 ** Usage: sqlite3_system_errno DB
6085 ** Return the low-level system errno value.
6087 static int SQLITE_TCLAPI test_system_errno(
6088 void * clientData,
6089 Tcl_Interp *interp,
6090 int objc,
6091 Tcl_Obj *CONST objv[]
6093 sqlite3 *db;
6094 int iErrno;
6095 if( objc!=2 ){
6096 Tcl_WrongNumArgs(interp, 1, objv, "DB");
6097 return TCL_ERROR;
6099 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6100 iErrno = sqlite3_system_errno(db);
6101 Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno));
6102 return TCL_OK;
6106 ** Usage: sqlite3_db_filename DB DBNAME
6108 ** Return the name of a file associated with a database.
6110 static int SQLITE_TCLAPI test_db_filename(
6111 void * clientData,
6112 Tcl_Interp *interp,
6113 int objc,
6114 Tcl_Obj *CONST objv[]
6116 sqlite3 *db;
6117 const char *zDbName;
6118 if( objc!=3 ){
6119 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
6120 return TCL_ERROR;
6122 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6123 zDbName = Tcl_GetString(objv[2]);
6124 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
6125 return TCL_OK;
6129 ** Usage: sqlite3_db_readonly DB DBNAME
6131 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does
6132 ** not exist.
6134 static int SQLITE_TCLAPI test_db_readonly(
6135 void * clientData,
6136 Tcl_Interp *interp,
6137 int objc,
6138 Tcl_Obj *CONST objv[]
6140 sqlite3 *db;
6141 const char *zDbName;
6142 if( objc!=3 ){
6143 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
6144 return TCL_ERROR;
6146 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6147 zDbName = Tcl_GetString(objv[2]);
6148 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
6149 return TCL_OK;
6153 ** Usage: sqlite3_soft_heap_limit ?N?
6155 ** Query or set the soft heap limit for the current thread. The
6156 ** limit is only changed if the N is present. The previous limit
6157 ** is returned.
6159 static int SQLITE_TCLAPI test_soft_heap_limit(
6160 void * clientData,
6161 Tcl_Interp *interp,
6162 int objc,
6163 Tcl_Obj *CONST objv[]
6165 sqlite3_int64 amt;
6166 Tcl_WideInt N = -1;
6167 if( objc!=1 && objc!=2 ){
6168 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
6169 return TCL_ERROR;
6171 if( objc==2 ){
6172 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
6174 amt = sqlite3_soft_heap_limit64(N);
6175 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
6176 return TCL_OK;
6180 ** Usage: sqlite3_hard_heap_limit ?N?
6182 ** Query or set the hard heap limit for the current thread. The
6183 ** limit is only changed if the N is present. The previous limit
6184 ** is returned.
6186 static int SQLITE_TCLAPI test_hard_heap_limit(
6187 void * clientData,
6188 Tcl_Interp *interp,
6189 int objc,
6190 Tcl_Obj *CONST objv[]
6192 sqlite3_int64 amt;
6193 Tcl_WideInt N = -1;
6194 if( objc!=1 && objc!=2 ){
6195 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
6196 return TCL_ERROR;
6198 if( objc==2 ){
6199 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
6201 amt = sqlite3_hard_heap_limit64(N);
6202 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
6203 return TCL_OK;
6207 ** Usage: sqlite3_thread_cleanup
6209 ** Call the sqlite3_thread_cleanup API.
6211 static int SQLITE_TCLAPI test_thread_cleanup(
6212 void * clientData,
6213 Tcl_Interp *interp,
6214 int objc,
6215 Tcl_Obj *CONST objv[]
6217 #ifndef SQLITE_OMIT_DEPRECATED
6218 sqlite3_thread_cleanup();
6219 #endif
6220 return TCL_OK;
6224 ** Usage: sqlite3_pager_refcounts DB
6226 ** Return a list of numbers which are the PagerRefcount for all
6227 ** pagers on each database connection.
6229 static int SQLITE_TCLAPI test_pager_refcounts(
6230 void * clientData,
6231 Tcl_Interp *interp,
6232 int objc,
6233 Tcl_Obj *CONST objv[]
6235 sqlite3 *db;
6236 int i;
6237 int v, *a;
6238 Tcl_Obj *pResult;
6240 if( objc!=2 ){
6241 Tcl_AppendResult(interp, "wrong # args: should be \"",
6242 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6243 return TCL_ERROR;
6245 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6246 pResult = Tcl_NewObj();
6247 for(i=0; i<db->nDb; i++){
6248 if( db->aDb[i].pBt==0 ){
6249 v = -1;
6250 }else{
6251 sqlite3_mutex_enter(db->mutex);
6252 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
6253 v = a[0];
6254 sqlite3_mutex_leave(db->mutex);
6256 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
6258 Tcl_SetObjResult(interp, pResult);
6259 return TCL_OK;
6264 ** tclcmd: working_64bit_int
6266 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
6267 ** leads to a number of test failures. The present command checks the
6268 ** TCL build to see whether or not it supports 64-bit integers. It
6269 ** returns TRUE if it does and FALSE if not.
6271 ** This command is used to warn users that their TCL build is defective
6272 ** and that the errors they are seeing in the test scripts might be
6273 ** a result of their defective TCL rather than problems in SQLite.
6275 static int SQLITE_TCLAPI working_64bit_int(
6276 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6277 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6278 int objc, /* Number of arguments */
6279 Tcl_Obj *CONST objv[] /* Command arguments */
6281 Tcl_Obj *pTestObj;
6282 int working = 0;
6284 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
6285 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
6286 Tcl_DecrRefCount(pTestObj);
6287 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
6288 return TCL_OK;
6293 ** tclcmd: vfs_unlink_test
6295 ** This TCL command unregisters the primary VFS and then registers
6296 ** it back again. This is used to test the ability to register a
6297 ** VFS when none are previously registered, and the ability to
6298 ** unregister the only available VFS. Ticket #2738
6300 static int SQLITE_TCLAPI vfs_unlink_test(
6301 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6303 int objc, /* Number of arguments */
6304 Tcl_Obj *CONST objv[] /* Command arguments */
6306 int i;
6307 sqlite3_vfs *pMain;
6308 sqlite3_vfs *apVfs[20];
6309 sqlite3_vfs one, two;
6311 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
6312 one.zName = "__one";
6313 two.zName = "__two";
6315 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
6316 ** change the default VFS
6318 pMain = sqlite3_vfs_find(0);
6319 sqlite3_vfs_register(&one, 0);
6320 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
6321 sqlite3_vfs_register(&two, 0);
6322 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
6324 /* We can find a VFS by its name */
6325 assert( sqlite3_vfs_find("__one")==&one );
6326 assert( sqlite3_vfs_find("__two")==&two );
6328 /* Calling sqlite_vfs_register with non-zero second parameter changes the
6329 ** default VFS, even if the 1st parameter is an existing VFS that is
6330 ** previously registered as the non-default.
6332 sqlite3_vfs_register(&one, 1);
6333 assert( sqlite3_vfs_find("__one")==&one );
6334 assert( sqlite3_vfs_find("__two")==&two );
6335 assert( sqlite3_vfs_find(0)==&one );
6336 sqlite3_vfs_register(&two, 1);
6337 assert( sqlite3_vfs_find("__one")==&one );
6338 assert( sqlite3_vfs_find("__two")==&two );
6339 assert( sqlite3_vfs_find(0)==&two );
6340 if( pMain ){
6341 sqlite3_vfs_register(pMain, 1);
6342 assert( sqlite3_vfs_find("__one")==&one );
6343 assert( sqlite3_vfs_find("__two")==&two );
6344 assert( sqlite3_vfs_find(0)==pMain );
6347 /* Unlink the default VFS. Repeat until there are no more VFSes
6348 ** registered.
6350 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
6351 apVfs[i] = sqlite3_vfs_find(0);
6352 if( apVfs[i] ){
6353 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
6354 sqlite3_vfs_unregister(apVfs[i]);
6355 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
6358 assert( 0==sqlite3_vfs_find(0) );
6360 /* Register the main VFS as non-default (will be made default, since
6361 ** it'll be the only one in existence).
6363 sqlite3_vfs_register(pMain, 0);
6364 assert( sqlite3_vfs_find(0)==pMain );
6366 /* Un-register the main VFS again to restore an empty VFS list */
6367 sqlite3_vfs_unregister(pMain);
6368 assert( 0==sqlite3_vfs_find(0) );
6370 /* Relink all VFSes in reverse order. */
6371 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
6372 if( apVfs[i] ){
6373 sqlite3_vfs_register(apVfs[i], 1);
6374 assert( apVfs[i]==sqlite3_vfs_find(0) );
6375 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
6379 /* Unregister out sample VFSes. */
6380 sqlite3_vfs_unregister(&one);
6381 sqlite3_vfs_unregister(&two);
6383 /* Unregistering a VFS that is not currently registered is harmless */
6384 sqlite3_vfs_unregister(&one);
6385 sqlite3_vfs_unregister(&two);
6386 assert( sqlite3_vfs_find("__one")==0 );
6387 assert( sqlite3_vfs_find("__two")==0 );
6389 /* We should be left with the original default VFS back as the
6390 ** original */
6391 assert( sqlite3_vfs_find(0)==pMain );
6393 return TCL_OK;
6397 ** tclcmd: vfs_initfail_test
6399 ** This TCL command attempts to vfs_find and vfs_register when the
6400 ** sqlite3_initialize() interface is failing. All calls should fail.
6402 static int SQLITE_TCLAPI vfs_initfail_test(
6403 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6404 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6405 int objc, /* Number of arguments */
6406 Tcl_Obj *CONST objv[] /* Command arguments */
6408 sqlite3_vfs one;
6409 one.zName = "__one";
6411 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6412 sqlite3_vfs_register(&one, 0);
6413 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6414 sqlite3_vfs_register(&one, 1);
6415 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6416 return TCL_OK;
6420 ** Saved VFSes
6422 static sqlite3_vfs *apVfs[20];
6423 static int nVfs = 0;
6426 ** tclcmd: vfs_unregister_all
6428 ** Unregister all VFSes.
6430 static int SQLITE_TCLAPI vfs_unregister_all(
6431 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6432 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6433 int objc, /* Number of arguments */
6434 Tcl_Obj *CONST objv[] /* Command arguments */
6436 int i;
6437 for(i=0; i<ArraySize(apVfs); i++){
6438 apVfs[i] = sqlite3_vfs_find(0);
6439 if( apVfs[i]==0 ) break;
6440 sqlite3_vfs_unregister(apVfs[i]);
6442 nVfs = i;
6443 return TCL_OK;
6446 ** tclcmd: vfs_reregister_all
6448 ** Restore all VFSes that were removed using vfs_unregister_all. Taking
6449 ** care to put the linked list back together in the same order as it was
6450 ** in before vfs_unregister_all was invoked.
6452 static int SQLITE_TCLAPI vfs_reregister_all(
6453 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6454 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6455 int objc, /* Number of arguments */
6456 Tcl_Obj *CONST objv[] /* Command arguments */
6458 int i;
6459 for(i=nVfs-1; i>=0; i--){
6460 sqlite3_vfs_register(apVfs[i], 1);
6462 return TCL_OK;
6467 ** tclcmd: file_control_test DB
6469 ** This TCL command runs the sqlite3_file_control interface and
6470 ** verifies correct operation of the same.
6472 static int SQLITE_TCLAPI file_control_test(
6473 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6474 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6475 int objc, /* Number of arguments */
6476 Tcl_Obj *CONST objv[] /* Command arguments */
6478 int iArg = 0;
6479 sqlite3 *db;
6480 int rc;
6482 if( objc!=2 ){
6483 Tcl_AppendResult(interp, "wrong # args: should be \"",
6484 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6485 return TCL_ERROR;
6487 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6488 rc = sqlite3_file_control(db, 0, 0, &iArg);
6489 assert( rc==SQLITE_NOTFOUND );
6490 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
6491 assert( rc==SQLITE_ERROR );
6492 rc = sqlite3_file_control(db, "main", -1, &iArg);
6493 assert( rc==SQLITE_NOTFOUND );
6494 rc = sqlite3_file_control(db, "temp", -1, &iArg);
6495 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
6497 return TCL_OK;
6502 ** tclcmd: file_control_lasterrno_test DB
6504 ** This TCL command runs the sqlite3_file_control interface and
6505 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
6507 static int SQLITE_TCLAPI file_control_lasterrno_test(
6508 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6509 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6510 int objc, /* Number of arguments */
6511 Tcl_Obj *CONST objv[] /* Command arguments */
6513 int iArg = 0;
6514 sqlite3 *db;
6515 int rc;
6517 if( objc!=2 ){
6518 Tcl_AppendResult(interp, "wrong # args: should be \"",
6519 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6520 return TCL_ERROR;
6522 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6523 return TCL_ERROR;
6525 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
6526 if( rc ){
6527 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6528 return TCL_ERROR;
6530 if( iArg!=0 ) {
6531 Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
6532 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
6533 return TCL_ERROR;
6535 return TCL_OK;
6539 ** tclcmd: file_control_data_version DB DBNAME
6541 ** This TCL command runs the sqlite3_file_control with the
6542 ** SQLITE_FCNTL_DATA_VERSION opcode, returning the result.
6544 static int SQLITE_TCLAPI file_control_data_version(
6545 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6546 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6547 int objc, /* Number of arguments */
6548 Tcl_Obj *CONST objv[] /* Command arguments */
6550 unsigned int iVers; /* data version */
6551 char *zDb; /* Db name ("main", "temp" etc.) */
6552 sqlite3 *db; /* Database handle */
6553 int rc; /* file_control() return code */
6554 char zBuf[100];
6556 if( objc!=3 && objc!=2 ){
6557 Tcl_WrongNumArgs(interp, 1, objv, "DB [DBNAME]");
6558 return TCL_ERROR;
6560 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6561 return TCL_ERROR;
6563 zDb = objc==3 ? Tcl_GetString(objv[2]) : NULL;
6565 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_DATA_VERSION, (void *)&iVers);
6566 if( rc ){
6567 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6568 return TCL_ERROR;
6569 }else{
6570 sqlite3_snprintf(sizeof(zBuf),zBuf,"%u",iVers);
6571 Tcl_SetResult(interp, (char *)zBuf, TCL_VOLATILE);
6572 return TCL_OK;
6577 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
6579 ** This TCL command runs the sqlite3_file_control interface and
6580 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6581 ** SQLITE_SET_LOCKPROXYFILE verbs.
6583 static int SQLITE_TCLAPI file_control_chunksize_test(
6584 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6585 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6586 int objc, /* Number of arguments */
6587 Tcl_Obj *CONST objv[] /* Command arguments */
6589 int nSize; /* New chunk size */
6590 char *zDb; /* Db name ("main", "temp" etc.) */
6591 sqlite3 *db; /* Database handle */
6592 int rc; /* file_control() return code */
6594 if( objc!=4 ){
6595 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
6596 return TCL_ERROR;
6598 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6599 || Tcl_GetIntFromObj(interp, objv[3], &nSize)
6601 return TCL_ERROR;
6603 zDb = Tcl_GetString(objv[2]);
6604 if( zDb[0]=='\0' ) zDb = NULL;
6606 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
6607 if( rc ){
6608 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6609 return TCL_ERROR;
6611 return TCL_OK;
6615 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
6617 ** This TCL command runs the sqlite3_file_control interface
6618 ** with SQLITE_FCNTL_SIZE_HINT
6620 static int SQLITE_TCLAPI file_control_sizehint_test(
6621 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6622 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6623 int objc, /* Number of arguments */
6624 Tcl_Obj *CONST objv[] /* Command arguments */
6626 Tcl_WideInt nSize; /* Hinted size */
6627 char *zDb; /* Db name ("main", "temp" etc.) */
6628 sqlite3 *db; /* Database handle */
6629 int rc; /* file_control() return code */
6631 if( objc!=4 ){
6632 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
6633 return TCL_ERROR;
6635 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6636 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
6638 return TCL_ERROR;
6640 zDb = Tcl_GetString(objv[2]);
6641 if( zDb[0]=='\0' ) zDb = NULL;
6643 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
6644 if( rc ){
6645 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6646 return TCL_ERROR;
6648 return TCL_OK;
6652 ** tclcmd: file_control_lockproxy_test DB PWD
6654 ** This TCL command runs the sqlite3_file_control interface and
6655 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6656 ** SQLITE_SET_LOCKPROXYFILE verbs.
6658 static int SQLITE_TCLAPI file_control_lockproxy_test(
6659 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6660 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6661 int objc, /* Number of arguments */
6662 Tcl_Obj *CONST objv[] /* Command arguments */
6664 sqlite3 *db;
6666 if( objc!=3 ){
6667 Tcl_AppendResult(interp, "wrong # args: should be \"",
6668 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
6669 return TCL_ERROR;
6671 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6672 return TCL_ERROR;
6675 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
6676 # if defined(__APPLE__)
6677 # define SQLITE_ENABLE_LOCKING_STYLE 1
6678 # else
6679 # define SQLITE_ENABLE_LOCKING_STYLE 0
6680 # endif
6681 #endif
6682 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
6684 char *testPath;
6685 int rc;
6686 int nPwd;
6687 const char *zPwd;
6688 char proxyPath[400];
6690 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
6691 if( sizeof(proxyPath)<nPwd+20 ){
6692 Tcl_AppendResult(interp, "PWD too big", (void*)0);
6693 return TCL_ERROR;
6695 sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd);
6696 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
6697 if( rc ){
6698 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6699 return TCL_ERROR;
6701 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
6702 if( strncmp(proxyPath,testPath,11) ){
6703 Tcl_AppendResult(interp, "Lock proxy file did not match the "
6704 "previously assigned value", 0);
6705 return TCL_ERROR;
6707 if( rc ){
6708 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6709 return TCL_ERROR;
6711 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
6712 if( rc ){
6713 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6714 return TCL_ERROR;
6717 #endif
6718 return TCL_OK;
6721 #if SQLITE_OS_WIN
6723 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY
6725 ** This TCL command runs the sqlite3_file_control interface with
6726 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
6728 static int SQLITE_TCLAPI file_control_win32_av_retry(
6729 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6730 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6731 int objc, /* Number of arguments */
6732 Tcl_Obj *CONST objv[] /* Command arguments */
6734 sqlite3 *db;
6735 int rc;
6736 int a[2];
6737 char z[100];
6739 if( objc!=4 ){
6740 Tcl_AppendResult(interp, "wrong # args: should be \"",
6741 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
6742 return TCL_ERROR;
6744 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6745 return TCL_ERROR;
6747 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
6748 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
6749 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
6750 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
6751 Tcl_AppendResult(interp, z, (char*)0);
6752 return TCL_OK;
6756 ** tclcmd: file_control_win32_get_handle DB
6758 ** This TCL command runs the sqlite3_file_control interface with
6759 ** the SQLITE_FCNTL_WIN32_GET_HANDLE opcode.
6761 static int file_control_win32_get_handle(
6762 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6763 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6764 int objc, /* Number of arguments */
6765 Tcl_Obj *CONST objv[] /* Command arguments */
6767 sqlite3 *db;
6768 int rc;
6769 HANDLE hFile = NULL;
6770 char z[100];
6772 if( objc!=2 ){
6773 Tcl_AppendResult(interp, "wrong # args: should be \"",
6774 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6775 return TCL_ERROR;
6777 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6778 return TCL_ERROR;
6780 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_GET_HANDLE,
6781 (void*)&hFile);
6782 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
6783 Tcl_AppendResult(interp, z, (char*)0);
6784 return TCL_OK;
6788 ** tclcmd: file_control_win32_set_handle DB HANDLE
6790 ** This TCL command runs the sqlite3_file_control interface with
6791 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
6793 static int SQLITE_TCLAPI file_control_win32_set_handle(
6794 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6795 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6796 int objc, /* Number of arguments */
6797 Tcl_Obj *CONST objv[] /* Command arguments */
6799 sqlite3 *db;
6800 int rc;
6801 HANDLE hFile = NULL;
6802 char z[100];
6804 if( objc!=3 ){
6805 Tcl_AppendResult(interp, "wrong # args: should be \"",
6806 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0);
6807 return TCL_ERROR;
6809 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6810 return TCL_ERROR;
6812 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){
6813 return TCL_ERROR;
6815 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE,
6816 (void*)&hFile);
6817 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
6818 Tcl_AppendResult(interp, z, (char*)0);
6819 return TCL_OK;
6821 #endif
6824 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG
6826 ** This TCL command runs the sqlite3_file_control interface with
6827 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
6829 static int SQLITE_TCLAPI file_control_persist_wal(
6830 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6831 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6832 int objc, /* Number of arguments */
6833 Tcl_Obj *CONST objv[] /* Command arguments */
6835 sqlite3 *db;
6836 int rc;
6837 int bPersist;
6838 char z[100];
6840 if( objc!=3 ){
6841 Tcl_AppendResult(interp, "wrong # args: should be \"",
6842 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
6843 return TCL_ERROR;
6845 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6846 return TCL_ERROR;
6848 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
6849 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
6850 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
6851 Tcl_AppendResult(interp, z, (char*)0);
6852 return TCL_OK;
6856 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG
6858 ** This TCL command runs the sqlite3_file_control interface with
6859 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
6861 static int SQLITE_TCLAPI file_control_powersafe_overwrite(
6862 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6863 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6864 int objc, /* Number of arguments */
6865 Tcl_Obj *CONST objv[] /* Command arguments */
6867 sqlite3 *db;
6868 int rc;
6869 int b;
6870 char z[100];
6872 if( objc!=3 ){
6873 Tcl_AppendResult(interp, "wrong # args: should be \"",
6874 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
6875 return TCL_ERROR;
6877 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6878 return TCL_ERROR;
6880 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
6881 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
6882 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
6883 Tcl_AppendResult(interp, z, (char*)0);
6884 return TCL_OK;
6889 ** tclcmd: file_control_vfsname DB ?AUXDB?
6891 ** Return a string that describes the stack of VFSes.
6893 static int SQLITE_TCLAPI file_control_vfsname(
6894 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6895 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6896 int objc, /* Number of arguments */
6897 Tcl_Obj *CONST objv[] /* Command arguments */
6899 sqlite3 *db;
6900 const char *zDbName = "main";
6901 char *zVfsName = 0;
6903 if( objc!=2 && objc!=3 ){
6904 Tcl_AppendResult(interp, "wrong # args: should be \"",
6905 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6906 return TCL_ERROR;
6908 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6909 return TCL_ERROR;
6911 if( objc==3 ){
6912 zDbName = Tcl_GetString(objv[2]);
6914 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
6915 Tcl_AppendResult(interp, zVfsName, (char*)0);
6916 sqlite3_free(zVfsName);
6917 return TCL_OK;
6921 ** tclcmd: file_control_reservebytes DB N
6923 static int SQLITE_TCLAPI file_control_reservebytes(
6924 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6925 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6926 int objc, /* Number of arguments */
6927 Tcl_Obj *CONST objv[] /* Command arguments */
6929 sqlite3 *db;
6930 const char *zDbName = "main";
6931 int n = 0;
6932 int rc;
6934 if( objc!=3 ){
6935 Tcl_WrongNumArgs(interp, 1, objv, "DB N");
6936 return TCL_ERROR;
6938 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6939 || Tcl_GetIntFromObj(interp, objv[2], &n)
6941 return TCL_ERROR;
6944 rc = sqlite3_file_control(db, zDbName, SQLITE_FCNTL_RESERVE_BYTES, (void*)&n);
6945 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
6946 return TCL_OK;
6951 ** tclcmd: file_control_tempfilename DB ?AUXDB?
6953 ** Return a string that is a temporary filename
6955 static int SQLITE_TCLAPI file_control_tempfilename(
6956 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6957 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6958 int objc, /* Number of arguments */
6959 Tcl_Obj *CONST objv[] /* Command arguments */
6961 sqlite3 *db;
6962 const char *zDbName = "main";
6963 char *zTName = 0;
6965 if( objc!=2 && objc!=3 ){
6966 Tcl_AppendResult(interp, "wrong # args: should be \"",
6967 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6968 return TCL_ERROR;
6970 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6971 return TCL_ERROR;
6973 if( objc==3 ){
6974 zDbName = Tcl_GetString(objv[2]);
6976 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
6977 Tcl_AppendResult(interp, zTName, (char*)0);
6978 sqlite3_free(zTName);
6979 return TCL_OK;
6983 ** tclcmd: file_control_external_reader DB ?AUXDB?
6985 ** Return a string that is a temporary filename
6987 static int SQLITE_TCLAPI file_control_external_reader(
6988 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6989 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6990 int objc, /* Number of arguments */
6991 Tcl_Obj *CONST objv[] /* Command arguments */
6993 sqlite3 *db;
6994 const char *zName = "main";
6995 int iRes = 0;
6996 int rc = SQLITE_OK;
6998 if( objc!=2 && objc!=3 ){
6999 Tcl_AppendResult(interp, "wrong # args: should be \"",
7000 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
7001 return TCL_ERROR;
7003 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7004 return TCL_ERROR;
7006 if( objc==3 ){
7007 zName = Tcl_GetString(objv[2]);
7009 rc = sqlite3_file_control(db, zName, SQLITE_FCNTL_EXTERNAL_READER, &iRes);
7010 if( rc!=SQLITE_OK ){
7011 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7012 return TCL_ERROR;
7014 Tcl_SetObjResult(interp, Tcl_NewIntObj(iRes));
7015 return TCL_OK;
7020 ** tclcmd: sqlite3_vfs_list
7022 ** Return a tcl list containing the names of all registered vfs's.
7024 static int SQLITE_TCLAPI vfs_list(
7025 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7026 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7027 int objc, /* Number of arguments */
7028 Tcl_Obj *CONST objv[] /* Command arguments */
7030 sqlite3_vfs *pVfs;
7031 Tcl_Obj *pRet = Tcl_NewObj();
7032 if( objc!=1 ){
7033 Tcl_WrongNumArgs(interp, 1, objv, "");
7034 return TCL_ERROR;
7036 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
7037 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
7039 Tcl_SetObjResult(interp, pRet);
7040 return TCL_OK;
7044 ** tclcmd: sqlite3_limit DB ID VALUE
7046 ** This TCL command runs the sqlite3_limit interface and
7047 ** verifies correct operation of the same.
7049 static int SQLITE_TCLAPI test_limit(
7050 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7051 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7052 int objc, /* Number of arguments */
7053 Tcl_Obj *CONST objv[] /* Command arguments */
7055 sqlite3 *db;
7056 int rc;
7057 static const struct {
7058 char *zName;
7059 int id;
7060 } aId[] = {
7061 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
7062 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
7063 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
7064 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
7065 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
7066 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
7067 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
7068 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
7069 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
7070 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
7071 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
7072 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS },
7074 /* Out of range test cases */
7075 { "SQLITE_LIMIT_TOOSMALL", -1, },
7076 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 },
7078 int i, id = 0;
7079 int val;
7080 const char *zId;
7082 if( objc!=4 ){
7083 Tcl_AppendResult(interp, "wrong # args: should be \"",
7084 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
7085 return TCL_ERROR;
7087 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7088 zId = Tcl_GetString(objv[2]);
7089 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
7090 if( strcmp(zId, aId[i].zName)==0 ){
7091 id = aId[i].id;
7092 break;
7095 if( i>=sizeof(aId)/sizeof(aId[0]) ){
7096 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
7097 return TCL_ERROR;
7099 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7100 rc = sqlite3_limit(db, id, val);
7101 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
7102 return TCL_OK;
7106 ** tclcmd: save_prng_state
7108 ** Save the state of the pseudo-random number generator.
7109 ** At the same time, verify that sqlite3_test_control works even when
7110 ** called with an out-of-range opcode.
7112 static int SQLITE_TCLAPI save_prng_state(
7113 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7114 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7115 int objc, /* Number of arguments */
7116 Tcl_Obj *CONST objv[] /* Command arguments */
7118 int rc = sqlite3_test_control(9999);
7119 assert( rc==0 );
7120 rc = sqlite3_test_control(-1);
7121 assert( rc==0 );
7122 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
7123 return TCL_OK;
7126 ** tclcmd: restore_prng_state
7128 static int SQLITE_TCLAPI restore_prng_state(
7129 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
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 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
7135 return TCL_OK;
7138 ** tclcmd: reset_prng_state
7140 static int SQLITE_TCLAPI reset_prng_state(
7141 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7142 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7143 int objc, /* Number of arguments */
7144 Tcl_Obj *CONST objv[] /* Command arguments */
7146 sqlite3_randomness(0,0);
7147 return TCL_OK;
7150 ** tclcmd: prng_seed INT ?DB?
7152 ** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB.
7153 ** INT is an integer. DB is a database connection, or a NULL pointer if
7154 ** omitted.
7156 ** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema
7157 ** cookie for DB, or to INT if the schema cookie happens to be zero.
7159 ** When INT!=0 and DB==0, set the PRNG seed to just INT.
7161 ** If INT==0 and DB==0 then use the default procedure of calling the
7162 ** xRandomness method on the default VFS to get the PRNG seed.
7164 static int SQLITE_TCLAPI prng_seed(
7165 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7166 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7167 int objc, /* Number of arguments */
7168 Tcl_Obj *CONST objv[] /* Command arguments */
7170 int i = 0;
7171 sqlite3 *db = 0;
7172 if( objc!=2 && objc!=3 ){
7173 Tcl_WrongNumArgs(interp, 1, objv, "SEED ?DB?");
7174 return TCL_ERROR;
7176 if( Tcl_GetIntFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7177 if( objc==3 && getDbPointer(interp, Tcl_GetString(objv[2]), &db) ){
7178 return TCL_ERROR;
7180 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, i, db);
7181 return TCL_OK;
7185 ** tclcmd: extra_schema_checks BOOLEAN
7187 ** Enable or disable schema checks when parsing the sqlite_schema file.
7188 ** This is always enabled in production, but it is sometimes useful to
7189 ** disable the checks in order to make some internal error states reachable
7190 ** for testing.
7192 static int SQLITE_TCLAPI extra_schema_checks(
7193 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7194 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7195 int objc, /* Number of arguments */
7196 Tcl_Obj *CONST objv[] /* Command arguments */
7198 int i = 0;
7199 if( objc!=2 ){
7200 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
7201 return TCL_ERROR;
7203 if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7204 sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, i);
7205 return TCL_OK;
7209 ** tclcmd: use_long_double BOOLEAN|"default"
7211 ** If no argument, report the current value of the use-long-double flag.
7213 ** If argument is "default", set the use-long-double flag to the default
7214 ** value for this build, based on the size of LONGDOUBLE_TYPE.
7216 ** If argument is a boolean, set the use-long-double flag accordingly.
7218 ** Return the new setting.
7220 static int SQLITE_TCLAPI use_long_double(
7221 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7222 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7223 int objc, /* Number of arguments */
7224 Tcl_Obj *CONST objv[] /* Command arguments */
7226 int i = -1;
7227 if( objc==2 ){
7228 if( strcmp(Tcl_GetString(objv[1]),"default")==0 ){
7229 i = 2;
7230 }else{
7231 if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7234 i = sqlite3_test_control(SQLITE_TESTCTRL_USELONGDOUBLE, i);
7235 Tcl_SetObjResult(interp, Tcl_NewIntObj(i));
7236 return TCL_OK;
7240 ** tclcmd: database_may_be_corrupt
7242 ** Indicate that database files might be corrupt. In other words, set the normal
7243 ** state of operation.
7245 static int SQLITE_TCLAPI database_may_be_corrupt(
7246 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7247 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7248 int objc, /* Number of arguments */
7249 Tcl_Obj *CONST objv[] /* Command arguments */
7251 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0);
7252 return TCL_OK;
7255 ** tclcmd: database_never_corrupt
7257 ** Indicate that database files are always well-formed. This enables
7258 ** extra assert() statements that test conditions that are always true
7259 ** for well-formed databases.
7261 static int SQLITE_TCLAPI database_never_corrupt(
7262 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7263 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7264 int objc, /* Number of arguments */
7265 Tcl_Obj *CONST objv[] /* Command arguments */
7267 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1);
7268 return TCL_OK;
7272 ** tclcmd: pcache_stats
7274 static int SQLITE_TCLAPI test_pcache_stats(
7275 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7276 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7277 int objc, /* Number of arguments */
7278 Tcl_Obj *CONST objv[] /* Command arguments */
7280 int nMin;
7281 int nMax;
7282 int nCurrent;
7283 int nRecyclable;
7284 Tcl_Obj *pRet;
7286 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
7288 pRet = Tcl_NewObj();
7289 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
7290 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
7291 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
7292 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
7293 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
7294 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
7295 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
7296 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
7298 Tcl_SetObjResult(interp, pRet);
7300 return TCL_OK;
7303 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7304 static void test_unlock_notify_cb(void **aArg, int nArg){
7305 int ii;
7306 for(ii=0; ii<nArg; ii++){
7307 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
7310 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
7313 ** tclcmd: sqlite3_unlock_notify db
7315 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7316 static int SQLITE_TCLAPI test_unlock_notify(
7317 ClientData clientData, /* Unused */
7318 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7319 int objc, /* Number of arguments */
7320 Tcl_Obj *CONST objv[] /* Command arguments */
7322 sqlite3 *db;
7323 int rc;
7325 if( objc!=2 ){
7326 Tcl_WrongNumArgs(interp, 1, objv, "DB");
7327 return TCL_ERROR;
7330 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7331 return TCL_ERROR;
7333 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
7334 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7335 return TCL_OK;
7337 #endif
7340 ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
7342 static int SQLITE_TCLAPI test_wal_checkpoint(
7343 ClientData clientData, /* Unused */
7344 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7345 int objc, /* Number of arguments */
7346 Tcl_Obj *CONST objv[] /* Command arguments */
7348 char *zDb = 0;
7349 sqlite3 *db;
7350 int rc;
7352 if( objc!=3 && objc!=2 ){
7353 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
7354 return TCL_ERROR;
7357 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7358 return TCL_ERROR;
7360 if( objc==3 ){
7361 zDb = Tcl_GetString(objv[2]);
7363 rc = sqlite3_wal_checkpoint(db, zDb);
7364 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7365 return TCL_OK;
7369 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
7371 ** This command calls the wal_checkpoint_v2() function with the specified
7372 ** mode argument (passive, full or restart). If present, the database name
7373 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
7374 ** NAME argument is not present, a NULL pointer is passed instead.
7376 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
7377 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
7378 ** to the error message obtained from sqlite3_errmsg().
7380 ** Otherwise, this command returns a list of three integers. The first integer
7381 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
7382 ** are the values returned via the output parameters by wal_checkpoint_v2() -
7383 ** the number of frames in the log and the number of frames in the log
7384 ** that have been checkpointed.
7386 static int SQLITE_TCLAPI test_wal_checkpoint_v2(
7387 ClientData clientData, /* Unused */
7388 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7389 int objc, /* Number of arguments */
7390 Tcl_Obj *CONST objv[] /* Command arguments */
7392 char *zDb = 0;
7393 sqlite3 *db;
7394 int rc;
7396 int eMode;
7397 int nLog = -555;
7398 int nCkpt = -555;
7399 Tcl_Obj *pRet;
7401 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
7402 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
7403 assert( SQLITE_CHECKPOINT_FULL==1 );
7404 assert( SQLITE_CHECKPOINT_RESTART==2 );
7405 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
7407 if( objc!=3 && objc!=4 ){
7408 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
7409 return TCL_ERROR;
7412 if( objc==4 ){
7413 zDb = Tcl_GetString(objv[3]);
7415 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
7416 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
7417 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
7419 return TCL_ERROR;
7422 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
7423 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
7424 const char *zErrCode = sqlite3ErrName(rc);
7425 Tcl_ResetResult(interp);
7426 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
7427 return TCL_ERROR;
7430 pRet = Tcl_NewObj();
7431 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
7432 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
7433 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
7434 Tcl_SetObjResult(interp, pRet);
7436 return TCL_OK;
7440 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE
7442 static int SQLITE_TCLAPI test_wal_autocheckpoint(
7443 ClientData clientData, /* Unused */
7444 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7445 int objc, /* Number of arguments */
7446 Tcl_Obj *CONST objv[] /* Command arguments */
7448 sqlite3 *db;
7449 int rc;
7450 int iVal;
7453 if( objc!=3 ){
7454 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE");
7455 return TCL_ERROR;
7458 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
7459 || Tcl_GetIntFromObj(0, objv[2], &iVal)
7461 return TCL_ERROR;
7464 rc = sqlite3_wal_autocheckpoint(db, iVal);
7465 Tcl_ResetResult(interp);
7466 if( rc!=SQLITE_OK ){
7467 const char *zErrCode = sqlite3ErrName(rc);
7468 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1));
7469 return TCL_ERROR;
7472 return TCL_OK;
7477 ** tclcmd: test_sqlite3_log ?SCRIPT?
7479 static struct LogCallback {
7480 Tcl_Interp *pInterp;
7481 Tcl_Obj *pObj;
7482 } logcallback = {0, 0};
7483 static void xLogcallback(void *unused, int err, char *zMsg){
7484 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
7485 Tcl_IncrRefCount(pNew);
7486 Tcl_ListObjAppendElement(
7487 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
7489 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
7490 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
7491 Tcl_DecrRefCount(pNew);
7493 static int SQLITE_TCLAPI test_sqlite3_log(
7494 ClientData clientData,
7495 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7496 int objc, /* Number of arguments */
7497 Tcl_Obj *CONST objv[] /* Command arguments */
7499 if( objc>2 ){
7500 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
7501 return TCL_ERROR;
7503 if( logcallback.pObj ){
7504 Tcl_DecrRefCount(logcallback.pObj);
7505 logcallback.pObj = 0;
7506 logcallback.pInterp = 0;
7507 sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0);
7509 if( objc>1 ){
7510 logcallback.pObj = objv[1];
7511 Tcl_IncrRefCount(logcallback.pObj);
7512 logcallback.pInterp = interp;
7513 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0);
7515 return TCL_OK;
7519 ** tcl_objproc COMMANDNAME ARGS...
7521 ** Run a TCL command using its objProc interface. Throw an error if
7522 ** the command has no objProc interface.
7524 static int SQLITE_TCLAPI runAsObjProc(
7525 void * clientData,
7526 Tcl_Interp *interp,
7527 int objc,
7528 Tcl_Obj *CONST objv[]
7530 Tcl_CmdInfo cmdInfo;
7531 if( objc<2 ){
7532 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
7533 return TCL_ERROR;
7535 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
7536 Tcl_AppendResult(interp, "command not found: ",
7537 Tcl_GetString(objv[1]), (char*)0);
7538 return TCL_ERROR;
7540 if( cmdInfo.objProc==0 ){
7541 Tcl_AppendResult(interp, "command has no objProc: ",
7542 Tcl_GetString(objv[1]), (char*)0);
7543 return TCL_ERROR;
7545 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
7548 #ifndef SQLITE_OMIT_EXPLAIN
7550 ** WARNING: The following function, printExplainQueryPlan() is an exact
7551 ** copy of example code from eqp.in (eqp.html). If this code is modified,
7552 ** then the documentation copy needs to be modified as well.
7555 ** Argument pStmt is a prepared SQL statement. This function compiles
7556 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
7557 ** and prints the report to stdout using printf().
7559 int printExplainQueryPlan(sqlite3_stmt *pStmt){
7560 const char *zSql; /* Input SQL */
7561 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */
7562 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
7563 int rc; /* Return code from sqlite3_prepare_v2() */
7565 zSql = sqlite3_sql(pStmt);
7566 if( zSql==0 ) return SQLITE_ERROR;
7568 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
7569 if( zExplain==0 ) return SQLITE_NOMEM;
7571 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
7572 sqlite3_free(zExplain);
7573 if( rc!=SQLITE_OK ) return rc;
7575 while( SQLITE_ROW==sqlite3_step(pExplain) ){
7576 int iSelectid = sqlite3_column_int(pExplain, 0);
7577 int iOrder = sqlite3_column_int(pExplain, 1);
7578 int iFrom = sqlite3_column_int(pExplain, 2);
7579 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
7581 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
7584 return sqlite3_finalize(pExplain);
7587 static int SQLITE_TCLAPI test_print_eqp(
7588 void * clientData,
7589 Tcl_Interp *interp,
7590 int objc,
7591 Tcl_Obj *CONST objv[]
7593 int rc;
7594 sqlite3_stmt *pStmt;
7596 if( objc!=2 ){
7597 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
7598 return TCL_ERROR;
7600 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
7601 rc = printExplainQueryPlan(pStmt);
7602 /* This is needed on Windows so that a test case using this
7603 ** function can open a read pipe and get the output of
7604 ** printExplainQueryPlan() immediately.
7606 fflush(stdout);
7607 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
7608 return TCL_OK;
7610 #endif /* SQLITE_OMIT_EXPLAIN */
7612 #include <time.h>
7614 ** This is an alternative localtime_r() implementation used for testing
7615 ** the 'localtime' and 'utc' modifiers of date-time functions. Because
7616 ** the OS-supplied localtime_r() is locale-dependent, this alternative is
7617 ** provided as a stable test platform.
7619 ** Operation:
7621 ** (1) Localtime is 30 minutes earlier than (west of) UTC on
7622 ** even days (counting from 1970-01-01)
7624 ** (2) Localtime is 30 minutes later than (east of) UTC on odd days.
7626 ** (3) The function fails for the specific date/time value
7627 ** of 2000-05-29 14:16:00 in order to test the ability of
7628 ** SQLite to deal with localtime_r() failures.
7630 static int testLocaltime(const void *aliasT, void *aliasTM){
7631 const time_t t = *(const time_t*)aliasT;
7632 struct tm *pTm = (struct tm *)aliasTM;
7633 time_t altT;
7634 sqlite3_int64 iJD;
7635 int Z, A, B, C, D, E, X1, S;
7637 if( (t/86400) & 1 ){
7638 altT = t + 1800; /* 30 minutes later on odd days */
7639 }else{
7640 altT = t - 1800; /* 30 minutes earlier on even days */
7642 iJD = (sqlite3_int64)(altT + 210866760000);
7643 Z = (int)((iJD + 43200)/86400);
7644 A = (int)((Z - 1867216.25)/36524.25);
7645 A = Z + 1 + A - (A/4);
7646 B = A + 1524;
7647 C = (int)((B - 122.1)/365.25);
7648 D = (36525*(C&32767))/100;
7649 E = (int)((B-D)/30.6001);
7650 X1 = (int)(30.6001*E);
7651 pTm->tm_mday = B - D - X1;
7652 pTm->tm_mon = E<14 ? E-2 : E-14;
7653 pTm->tm_year = (pTm->tm_mon>1 ? C - 4716 : C - 4715) - 1900;
7654 S = (int)((iJD + 43200)%86400);
7655 pTm->tm_hour = S/3600;
7656 pTm->tm_min = (S/60)%60;
7657 pTm->tm_sec = S % 60;
7658 return t==959609760; /* Special case: 2000-05-29 14:16:00 fails */
7662 ** TCLCMD: strftime FORMAT UNIXTIMESTAMP
7664 ** Access to the C-library strftime() routine, so that its results
7665 ** can be compared against SQLite's internal strftime() SQL function
7666 ** implementation.
7668 static int SQLITE_TCLAPI strftime_cmd(
7669 void * clientData,
7670 Tcl_Interp *interp,
7671 int objc,
7672 Tcl_Obj *CONST objv[]
7674 Tcl_WideInt ts;
7675 time_t t;
7676 struct tm *pTm;
7677 const char *zFmt;
7678 size_t n;
7679 char zBuf[1000];
7680 if( objc!=3 ){
7681 Tcl_WrongNumArgs(interp, 1, objv, "FORMAT UNIXTIMESTAMP");
7682 return TCL_ERROR;
7684 if( Tcl_GetWideIntFromObj(interp, objv[2], &ts) ) return TCL_ERROR;
7685 zFmt = Tcl_GetString(objv[1]);
7686 t = (time_t)ts;
7687 pTm = gmtime(&t);
7688 n = strftime(zBuf, sizeof(zBuf)-1, zFmt, pTm);
7689 if( n>=0 && n<sizeof(zBuf) ){
7690 zBuf[n] = 0;
7691 Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
7693 return TCL_OK;
7697 ** .treetrace N
7699 static int SQLITE_TCLAPI test_treetrace(
7700 void * clientData,
7701 Tcl_Interp *interp,
7702 int objc,
7703 Tcl_Obj *CONST objv[]
7705 unsigned int v = 0;
7706 if( objc>=2 ){
7707 if( Tcl_GetIntFromObj(interp, objv[1], (int*)&v)==TCL_OK ){
7708 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &v);
7711 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &v);
7712 Tcl_SetObjResult(interp, Tcl_NewIntObj((int)v));
7713 return TCL_OK;
7717 ** sqlite3_test_control VERB ARGS...
7719 static int SQLITE_TCLAPI test_test_control(
7720 void * clientData,
7721 Tcl_Interp *interp,
7722 int objc,
7723 Tcl_Obj *CONST objv[]
7725 struct Verb {
7726 const char *zName;
7727 int i;
7728 } aVerb[] = {
7729 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
7730 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP },
7731 { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER },
7732 { "SQLITE_TESTCTRL_INTERNAL_FUNCTIONS", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS},
7733 { "SQLITE_TESTCTRL_FK_NO_ACTION", SQLITE_TESTCTRL_FK_NO_ACTION},
7734 { 0, 0 }
7736 int iVerb;
7737 int iFlag;
7738 int rc;
7740 if( objc<2 ){
7741 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
7742 return TCL_ERROR;
7745 rc = Tcl_GetIndexFromObjStruct(
7746 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
7748 if( rc!=TCL_OK ) return rc;
7750 iFlag = aVerb[iVerb].i;
7751 switch( iFlag ){
7752 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
7753 sqlite3 *db = 0;
7754 if( objc!=3 ){
7755 Tcl_WrongNumArgs(interp, 2, objv, "DB");
7756 return TCL_ERROR;
7758 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7759 sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db);
7760 break;
7762 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
7763 int val;
7764 if( objc!=3 ){
7765 Tcl_WrongNumArgs(interp, 2, objv, "0|1|2");
7766 return TCL_ERROR;
7768 if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR;
7769 sqlite3_test_control(iFlag, val, testLocaltime);
7770 break;
7773 case SQLITE_TESTCTRL_FK_NO_ACTION: {
7774 int val = 0;
7775 sqlite3 *db = 0;
7776 if( objc!=4 ){
7777 Tcl_WrongNumArgs(interp, 2, objv, "DB BOOLEAN");
7778 return TCL_ERROR;
7780 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7781 if( Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7783 sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, db, val);
7784 break;
7787 case SQLITE_TESTCTRL_SORTER_MMAP: {
7788 int val;
7789 sqlite3 *db;
7790 if( objc!=4 ){
7791 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT");
7792 return TCL_ERROR;
7794 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7795 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7796 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val);
7797 break;
7800 case SQLITE_TESTCTRL_IMPOSTER: {
7801 int onOff, tnum;
7802 const char *zDbName;
7803 sqlite3 *db;
7804 if( objc!=6 ){
7805 Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum");
7806 return TCL_ERROR;
7808 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7809 zDbName = Tcl_GetString(objv[3]);
7810 if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR;
7811 if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR;
7812 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum);
7813 break;
7817 Tcl_ResetResult(interp);
7818 return TCL_OK;
7821 #if SQLITE_OS_UNIX
7822 #include <sys/time.h>
7823 #include <sys/resource.h>
7825 static int SQLITE_TCLAPI test_getrusage(
7826 void * clientData,
7827 Tcl_Interp *interp,
7828 int objc,
7829 Tcl_Obj *CONST objv[]
7831 char buf[1024];
7832 struct rusage r;
7833 memset(&r, 0, sizeof(r));
7834 getrusage(RUSAGE_SELF, &r);
7836 sqlite3_snprintf(sizeof(buf), buf,
7837 "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
7838 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
7839 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
7840 (int)r.ru_minflt, (int)r.ru_majflt
7842 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
7843 return TCL_OK;
7845 #endif
7847 #if SQLITE_OS_WIN
7849 ** Information passed from the main thread into the windows file locker
7850 ** background thread.
7852 struct win32FileLocker {
7853 char *evName; /* Name of event to signal thread startup */
7854 HANDLE h; /* Handle of the file to be locked */
7855 int delay1; /* Delay before locking */
7856 int delay2; /* Delay before unlocking */
7857 int ok; /* Finished ok */
7858 int err; /* True if an error occurs */
7860 #endif
7863 #if SQLITE_OS_WIN
7864 #include <process.h>
7866 ** The background thread that does file locking.
7868 static void SQLITE_CDECL win32_file_locker(void *pAppData){
7869 struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
7870 if( p->evName ){
7871 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
7872 if ( ev ){
7873 SetEvent(ev);
7874 CloseHandle(ev);
7877 if( p->delay1 ) Sleep(p->delay1);
7878 if( LockFile(p->h, 0, 0, 100000000, 0) ){
7879 Sleep(p->delay2);
7880 UnlockFile(p->h, 0, 0, 100000000, 0);
7881 p->ok = 1;
7882 }else{
7883 p->err = 1;
7885 CloseHandle(p->h);
7886 p->h = 0;
7887 p->delay1 = 0;
7888 p->delay2 = 0;
7890 #endif
7892 #if SQLITE_OS_WIN
7894 ** lock_win32_file FILENAME DELAY1 DELAY2
7896 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
7897 ** Wait DELAY1 milliseconds before acquiring the lock.
7899 static int SQLITE_TCLAPI win32_file_lock(
7900 void * clientData,
7901 Tcl_Interp *interp,
7902 int objc,
7903 Tcl_Obj *CONST objv[]
7905 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
7906 const char *zFilename;
7907 char zBuf[200];
7908 int retry = 0;
7909 HANDLE ev;
7910 DWORD wResult;
7912 if( objc!=4 && objc!=1 ){
7913 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
7914 return TCL_ERROR;
7916 if( objc==1 ){
7917 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
7918 x.ok, x.err, x.delay1, x.delay2, x.h);
7919 Tcl_AppendResult(interp, zBuf, (char*)0);
7920 return TCL_OK;
7922 while( x.h && retry<30 ){
7923 retry++;
7924 Sleep(100);
7926 if( x.h ){
7927 Tcl_AppendResult(interp, "busy", (char*)0);
7928 return TCL_ERROR;
7930 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
7931 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
7932 zFilename = Tcl_GetString(objv[1]);
7933 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
7934 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
7935 FILE_ATTRIBUTE_NORMAL, 0);
7936 if( !x.h ){
7937 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
7938 return TCL_ERROR;
7940 ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
7941 if ( !ev ){
7942 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
7943 return TCL_ERROR;
7945 _beginthread(win32_file_locker, 0, (void*)&x);
7946 Sleep(0);
7947 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
7948 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
7949 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
7950 CloseHandle(ev);
7951 return TCL_ERROR;
7953 CloseHandle(ev);
7954 return TCL_OK;
7958 ** exists_win32_path PATH
7960 ** Returns non-zero if the specified path exists, whose fully qualified name
7961 ** may exceed 260 characters if it is prefixed with "\\?\".
7963 static int SQLITE_TCLAPI win32_exists_path(
7964 void *clientData,
7965 Tcl_Interp *interp,
7966 int objc,
7967 Tcl_Obj *CONST objv[]
7969 if( objc!=2 ){
7970 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
7971 return TCL_ERROR;
7973 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
7974 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
7975 return TCL_OK;
7979 ** find_win32_file PATTERN
7981 ** Returns a list of entries in a directory that match the specified pattern,
7982 ** whose fully qualified name may exceed 248 characters if it is prefixed with
7983 ** "\\?\".
7985 static int SQLITE_TCLAPI win32_find_file(
7986 void *clientData,
7987 Tcl_Interp *interp,
7988 int objc,
7989 Tcl_Obj *CONST objv[]
7991 HANDLE hFindFile = INVALID_HANDLE_VALUE;
7992 WIN32_FIND_DATAW findData;
7993 Tcl_Obj *listObj;
7994 DWORD lastErrno;
7995 if( objc!=2 ){
7996 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
7997 return TCL_ERROR;
7999 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
8000 if( hFindFile==INVALID_HANDLE_VALUE ){
8001 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8002 return TCL_ERROR;
8004 listObj = Tcl_NewObj();
8005 Tcl_IncrRefCount(listObj);
8006 do {
8007 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
8008 findData.cFileName, -1));
8009 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
8010 findData.dwFileAttributes));
8011 } while( FindNextFileW(hFindFile, &findData) );
8012 lastErrno = GetLastError();
8013 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
8014 FindClose(hFindFile);
8015 Tcl_DecrRefCount(listObj);
8016 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8017 return TCL_ERROR;
8019 FindClose(hFindFile);
8020 Tcl_SetObjResult(interp, listObj);
8021 return TCL_OK;
8025 ** delete_win32_file FILENAME
8027 ** Deletes the specified file, whose fully qualified name may exceed 260
8028 ** characters if it is prefixed with "\\?\".
8030 static int SQLITE_TCLAPI win32_delete_file(
8031 void *clientData,
8032 Tcl_Interp *interp,
8033 int objc,
8034 Tcl_Obj *CONST objv[]
8036 if( objc!=2 ){
8037 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
8038 return TCL_ERROR;
8040 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
8041 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8042 return TCL_ERROR;
8044 Tcl_ResetResult(interp);
8045 return TCL_OK;
8049 ** make_win32_dir DIRECTORY
8051 ** Creates the specified directory, whose fully qualified name may exceed 248
8052 ** characters if it is prefixed with "\\?\".
8054 static int SQLITE_TCLAPI win32_mkdir(
8055 void *clientData,
8056 Tcl_Interp *interp,
8057 int objc,
8058 Tcl_Obj *CONST objv[]
8060 if( objc!=2 ){
8061 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
8062 return TCL_ERROR;
8064 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
8065 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8066 return TCL_ERROR;
8068 Tcl_ResetResult(interp);
8069 return TCL_OK;
8073 ** remove_win32_dir DIRECTORY
8075 ** Removes the specified directory, whose fully qualified name may exceed 248
8076 ** characters if it is prefixed with "\\?\".
8078 static int SQLITE_TCLAPI win32_rmdir(
8079 void *clientData,
8080 Tcl_Interp *interp,
8081 int objc,
8082 Tcl_Obj *CONST objv[]
8084 if( objc!=2 ){
8085 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
8086 return TCL_ERROR;
8088 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
8089 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8090 return TCL_ERROR;
8092 Tcl_ResetResult(interp);
8093 return TCL_OK;
8095 #endif
8099 ** optimization_control DB OPT BOOLEAN
8101 ** Enable or disable query optimizations using the sqlite3_test_control()
8102 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
8103 ** OPT is the name of the optimization to be disabled. OPT can also be a
8104 ** list or optimizations names, in which case all optimizations named are
8105 ** enabled or disabled.
8107 ** Each invocation of this control overrides all prior invocations. The
8108 ** changes are not cumulative.
8110 static int SQLITE_TCLAPI optimization_control(
8111 void * clientData,
8112 Tcl_Interp *interp,
8113 int objc,
8114 Tcl_Obj *CONST objv[]
8116 int i;
8117 sqlite3 *db;
8118 const char *zOpt;
8119 int onoff;
8120 int mask = 0;
8121 int cnt = 0;
8122 static const struct {
8123 const char *zOptName;
8124 int mask;
8125 } aOpt[] = {
8126 { "all", SQLITE_AllOpts },
8127 { "none", 0 },
8128 { "query-flattener", SQLITE_QueryFlattener },
8129 { "groupby-order", SQLITE_GroupByOrder },
8130 { "factor-constants", SQLITE_FactorOutConst },
8131 { "distinct-opt", SQLITE_DistinctOpt },
8132 { "cover-idx-scan", SQLITE_CoverIdxScan },
8133 { "order-by-idx-join", SQLITE_OrderByIdxJoin },
8134 { "transitive", SQLITE_Transitive },
8135 { "omit-noop-join", SQLITE_OmitNoopJoin },
8136 { "stat4", SQLITE_Stat4 },
8137 { "skip-scan", SQLITE_SkipScan },
8138 { "push-down", SQLITE_PushDown },
8139 { "balanced-merge", SQLITE_BalancedMerge },
8140 { "propagate-const", SQLITE_PropagateConst },
8141 { "one-pass", SQLITE_OnePass },
8144 if( objc!=4 ){
8145 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
8146 return TCL_ERROR;
8148 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8149 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
8150 zOpt = Tcl_GetString(objv[2]);
8151 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
8152 if( strstr(zOpt, aOpt[i].zOptName)!=0 ){
8153 mask |= aOpt[i].mask;
8154 cnt++;
8157 if( onoff ) mask = ~mask;
8158 if( cnt==0 ){
8159 Tcl_AppendResult(interp, "unknown optimization - should be one of:",
8160 (char*)0);
8161 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
8162 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
8164 return TCL_ERROR;
8166 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
8167 Tcl_SetObjResult(interp, Tcl_NewIntObj(mask));
8168 return TCL_OK;
8172 ** load_static_extension DB NAME ...
8174 ** Load one or more statically linked extensions.
8176 static int SQLITE_TCLAPI tclLoadStaticExtensionCmd(
8177 void * clientData,
8178 Tcl_Interp *interp,
8179 int objc,
8180 Tcl_Obj *CONST objv[]
8182 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
8183 extern int sqlite3_appendvfs_init(sqlite3*,char**,const sqlite3_api_routines*);
8184 extern int sqlite3_basexx_init(sqlite3*,char**,const sqlite3_api_routines*);
8185 extern int sqlite3_carray_init(sqlite3*,char**,const sqlite3_api_routines*);
8186 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
8187 extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*);
8188 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
8189 extern int sqlite3_explain_init(sqlite3*,char**,const sqlite3_api_routines*);
8190 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
8191 extern int sqlite3_decimal_init(sqlite3*,char**,const sqlite3_api_routines*);
8192 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
8193 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
8194 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
8195 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
8196 #ifndef SQLITE_OMIT_VIRTUALTABLE
8197 extern int sqlite3_prefixes_init(sqlite3*,char**,const sqlite3_api_routines*);
8198 #endif
8199 extern int sqlite3_qpvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
8200 extern int sqlite3_randomjson_init(sqlite3*,char**,const sqlite3_api_routines*);
8201 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
8202 extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*);
8203 extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
8204 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
8205 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
8206 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
8207 extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
8208 #ifdef SQLITE_HAVE_ZLIB
8209 extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*);
8210 #endif
8211 static const struct {
8212 const char *zExtName;
8213 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
8214 } aExtension[] = {
8215 { "amatch", sqlite3_amatch_init },
8216 { "appendvfs", sqlite3_appendvfs_init },
8217 { "basexx", sqlite3_basexx_init },
8218 { "carray", sqlite3_carray_init },
8219 { "closure", sqlite3_closure_init },
8220 { "csv", sqlite3_csv_init },
8221 { "decimal", sqlite3_decimal_init },
8222 { "eval", sqlite3_eval_init },
8223 { "explain", sqlite3_explain_init },
8224 { "fileio", sqlite3_fileio_init },
8225 { "fuzzer", sqlite3_fuzzer_init },
8226 { "ieee754", sqlite3_ieee_init },
8227 { "nextchar", sqlite3_nextchar_init },
8228 { "percentile", sqlite3_percentile_init },
8229 #ifndef SQLITE_OMIT_VIRTUALTABLE
8230 { "prefixes", sqlite3_prefixes_init },
8231 #endif
8232 { "qpvtab", sqlite3_qpvtab_init },
8233 { "randomjson", sqlite3_randomjson_init },
8234 { "regexp", sqlite3_regexp_init },
8235 { "remember", sqlite3_remember_init },
8236 { "series", sqlite3_series_init },
8237 { "spellfix", sqlite3_spellfix_init },
8238 { "totype", sqlite3_totype_init },
8239 { "unionvtab", sqlite3_unionvtab_init },
8240 { "wholenumber", sqlite3_wholenumber_init },
8241 #ifdef SQLITE_HAVE_ZLIB
8242 { "zipfile", sqlite3_zipfile_init },
8243 #endif
8245 sqlite3 *db;
8246 const char *zName;
8247 int i, j, rc;
8248 char *zErrMsg = 0;
8249 if( objc<3 ){
8250 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
8251 return TCL_ERROR;
8253 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8254 for(j=2; j<objc; j++){
8255 zName = Tcl_GetString(objv[j]);
8256 for(i=0; i<ArraySize(aExtension); i++){
8257 if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
8259 if( i>=ArraySize(aExtension) ){
8260 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
8261 return TCL_ERROR;
8263 if( aExtension[i].pInit ){
8264 rc = aExtension[i].pInit(db, &zErrMsg, 0);
8265 }else{
8266 rc = SQLITE_OK;
8268 if( (rc!=SQLITE_OK && rc!=SQLITE_OK_LOAD_PERMANENTLY) || zErrMsg ){
8269 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
8270 (char*)0);
8271 sqlite3_free(zErrMsg);
8272 return TCL_ERROR;
8275 return TCL_OK;
8279 ** sorter_test_fakeheap BOOL
8282 static int SQLITE_TCLAPI sorter_test_fakeheap(
8283 void * clientData,
8284 Tcl_Interp *interp,
8285 int objc,
8286 Tcl_Obj *CONST objv[]
8288 int bArg;
8289 if( objc!=2 ){
8290 Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
8291 return TCL_ERROR;
8294 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){
8295 return TCL_ERROR;
8298 if( bArg ){
8299 if( sqlite3GlobalConfig.pHeap==0 ){
8300 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1);
8302 }else{
8303 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){
8304 sqlite3GlobalConfig.pHeap = 0;
8308 Tcl_ResetResult(interp);
8309 return TCL_OK;
8313 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2
8315 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
8316 ** check that the leftmost and rightmost columns returned are both integers,
8317 ** and that both contain the same value.
8319 ** Then execute statement $SQL2. Check that the statement returns the same
8320 ** set of integers in the same order as in the previous step (using $SQL1).
8322 static int SQLITE_TCLAPI sorter_test_sort4_helper(
8323 void * clientData,
8324 Tcl_Interp *interp,
8325 int objc,
8326 Tcl_Obj *CONST objv[]
8328 const char *zSql1;
8329 const char *zSql2;
8330 int nStep;
8331 int iStep;
8332 unsigned int iCksum1 = 0;
8333 unsigned int iCksum2 = 0;
8334 int rc;
8335 int iB;
8336 sqlite3 *db;
8337 sqlite3_stmt *pStmt;
8339 if( objc!=5 ){
8340 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2");
8341 return TCL_ERROR;
8344 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8345 zSql1 = Tcl_GetString(objv[2]);
8346 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR;
8347 zSql2 = Tcl_GetString(objv[4]);
8349 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0);
8350 if( rc!=SQLITE_OK ) goto sql_error;
8352 iB = sqlite3_column_count(pStmt)-1;
8353 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){
8354 int a = sqlite3_column_int(pStmt, 0);
8355 if( a!=sqlite3_column_int(pStmt, iB) ){
8356 Tcl_AppendResult(interp, "data error: (a!=b)", 0);
8357 return TCL_ERROR;
8360 iCksum1 += (iCksum1 << 3) + (unsigned int)a;
8362 rc = sqlite3_finalize(pStmt);
8363 if( rc!=SQLITE_OK ) goto sql_error;
8365 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0);
8366 if( rc!=SQLITE_OK ) goto sql_error;
8367 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){
8368 int a = sqlite3_column_int(pStmt, 0);
8369 iCksum2 += (iCksum2 << 3) + (unsigned int)a;
8371 rc = sqlite3_finalize(pStmt);
8372 if( rc!=SQLITE_OK ) goto sql_error;
8374 if( iCksum1!=iCksum2 ){
8375 Tcl_AppendResult(interp, "checksum mismatch", 0);
8376 return TCL_ERROR;
8379 return TCL_OK;
8380 sql_error:
8381 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
8382 return TCL_ERROR;
8386 #ifdef SQLITE_USER_AUTHENTICATION
8387 #include "sqlite3userauth.h"
8389 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD
8391 static int SQLITE_TCLAPI test_user_authenticate(
8392 ClientData clientData, /* Unused */
8393 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8394 int objc, /* Number of arguments */
8395 Tcl_Obj *CONST objv[] /* Command arguments */
8397 char *zUser = 0;
8398 char *zPasswd = 0;
8399 int nPasswd = 0;
8400 sqlite3 *db;
8401 int rc;
8403 if( objc!=4 ){
8404 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
8405 return TCL_ERROR;
8407 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8408 return TCL_ERROR;
8410 zUser = Tcl_GetString(objv[2]);
8411 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8412 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
8413 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8414 return TCL_OK;
8416 #endif /* SQLITE_USER_AUTHENTICATION */
8418 #ifdef SQLITE_USER_AUTHENTICATION
8420 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN
8422 static int SQLITE_TCLAPI test_user_add(
8423 ClientData clientData, /* Unused */
8424 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8425 int objc, /* Number of arguments */
8426 Tcl_Obj *CONST objv[] /* Command arguments */
8428 char *zUser = 0;
8429 char *zPasswd = 0;
8430 int nPasswd = 0;
8431 int isAdmin = 0;
8432 sqlite3 *db;
8433 int rc;
8435 if( objc!=5 ){
8436 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
8437 return TCL_ERROR;
8439 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8440 return TCL_ERROR;
8442 zUser = Tcl_GetString(objv[2]);
8443 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8444 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
8445 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
8446 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8447 return TCL_OK;
8449 #endif /* SQLITE_USER_AUTHENTICATION */
8451 #ifdef SQLITE_USER_AUTHENTICATION
8453 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN
8455 static int SQLITE_TCLAPI test_user_change(
8456 ClientData clientData, /* Unused */
8457 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8458 int objc, /* Number of arguments */
8459 Tcl_Obj *CONST objv[] /* Command arguments */
8461 char *zUser = 0;
8462 char *zPasswd = 0;
8463 int nPasswd = 0;
8464 int isAdmin = 0;
8465 sqlite3 *db;
8466 int rc;
8468 if( objc!=5 ){
8469 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
8470 return TCL_ERROR;
8472 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8473 return TCL_ERROR;
8475 zUser = Tcl_GetString(objv[2]);
8476 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8477 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
8478 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
8479 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8480 return TCL_OK;
8482 #endif /* SQLITE_USER_AUTHENTICATION */
8484 #ifdef SQLITE_USER_AUTHENTICATION
8486 ** tclcmd: sqlite3_user_delete DB USERNAME
8488 static int SQLITE_TCLAPI test_user_delete(
8489 ClientData clientData, /* Unused */
8490 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8491 int objc, /* Number of arguments */
8492 Tcl_Obj *CONST objv[] /* Command arguments */
8494 char *zUser = 0;
8495 sqlite3 *db;
8496 int rc;
8498 if( objc!=3 ){
8499 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
8500 return TCL_ERROR;
8502 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8503 return TCL_ERROR;
8505 zUser = Tcl_GetString(objv[2]);
8506 rc = sqlite3_user_delete(db, zUser);
8507 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8508 return TCL_OK;
8510 #endif /* SQLITE_USER_AUTHENTICATION */
8513 ** tclcmd: bad_behavior TYPE
8515 ** Do some things that should trigger a valgrind or -fsanitize=undefined
8516 ** warning. This is used to verify that errors and warnings output by those
8517 ** tools are detected by the test scripts.
8519 ** TYPE BEHAVIOR
8520 ** 1 Overflow a signed integer
8521 ** 2 Jump based on an uninitialized variable
8522 ** 3 Read after free
8523 ** 4 Panic
8525 static int SQLITE_TCLAPI test_bad_behavior(
8526 ClientData clientData, /* Pointer to an integer containing zero */
8527 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8528 int objc, /* Number of arguments */
8529 Tcl_Obj *CONST objv[] /* Command arguments */
8531 int iType;
8532 int xyz;
8533 int i = *(int*)clientData;
8534 int j;
8535 int w[10];
8536 int *a;
8537 if( objc!=2 ){
8538 Tcl_WrongNumArgs(interp, 1, objv, "TYPE");
8539 return TCL_ERROR;
8541 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR;
8542 switch( iType ){
8543 case 1: {
8544 xyz = 0x7fffff00 - i;
8545 xyz += 0x100;
8546 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz));
8547 break;
8549 case 2: {
8550 w[1] = 5;
8551 if( w[i]>0 ) w[1]++;
8552 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1]));
8553 break;
8555 case 3: {
8556 a = malloc( sizeof(int)*10 );
8557 for(j=0; j<10; j++) a[j] = j;
8558 free(a);
8559 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i]));
8560 break;
8562 case 4: {
8563 Tcl_Panic("Deliberate panic");
8564 break;
8567 return TCL_OK;
8571 ** tclcmd: register_dbstat_vtab DB
8573 ** Cause the dbstat virtual table to be available on the connection DB
8575 static int SQLITE_TCLAPI test_register_dbstat_vtab(
8576 void *clientData,
8577 Tcl_Interp *interp,
8578 int objc,
8579 Tcl_Obj *CONST objv[]
8581 #ifdef SQLITE_OMIT_VIRTUALTABLE
8582 Tcl_AppendResult(interp, "dbstat not available because of "
8583 "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
8584 return TCL_ERROR;
8585 #else
8586 struct SqliteDb { sqlite3 *db; };
8587 char *zDb;
8588 Tcl_CmdInfo cmdInfo;
8590 if( objc!=2 ){
8591 Tcl_WrongNumArgs(interp, 1, objv, "DB");
8592 return TCL_ERROR;
8595 zDb = Tcl_GetString(objv[1]);
8596 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
8597 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
8598 sqlite3DbstatRegister(db);
8600 return TCL_OK;
8601 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8605 ** tclcmd: sqlite3_db_config DB SETTING VALUE
8607 ** Invoke sqlite3_db_config() for one of the setting values.
8609 static int SQLITE_TCLAPI test_sqlite3_db_config(
8610 void *clientData,
8611 Tcl_Interp *interp,
8612 int objc,
8613 Tcl_Obj *CONST objv[]
8615 static const struct {
8616 const char *zName;
8617 int eVal;
8618 } aSetting[] = {
8619 { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY },
8620 { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER },
8621 { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8622 { "LOAD_EXTENSION", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8623 { "NO_CKPT_ON_CLOSE", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
8624 { "QPSG", SQLITE_DBCONFIG_ENABLE_QPSG },
8625 { "TRIGGER_EQP", SQLITE_DBCONFIG_TRIGGER_EQP },
8626 { "RESET_DB", SQLITE_DBCONFIG_RESET_DATABASE },
8627 { "DEFENSIVE", SQLITE_DBCONFIG_DEFENSIVE },
8628 { "WRITABLE_SCHEMA", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
8629 { "LEGACY_ALTER_TABLE", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
8630 { "DQS_DML", SQLITE_DBCONFIG_DQS_DML },
8631 { "DQS_DDL", SQLITE_DBCONFIG_DQS_DDL },
8632 { "LEGACY_FILE_FORMAT", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
8633 { "STMT_SCANSTATUS", SQLITE_DBCONFIG_STMT_SCANSTATUS },
8635 int i;
8636 int v = 0;
8637 const char *zSetting;
8638 sqlite3 *db;
8640 if( objc!=4 && objc!=3 ){
8641 Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING [VALUE]");
8642 return TCL_ERROR;
8644 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8645 zSetting = Tcl_GetString(objv[2]);
8646 if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7;
8647 if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9;
8648 if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7;
8649 for(i=0; i<ArraySize(aSetting); i++){
8650 if( strcmp(zSetting, aSetting[i].zName)==0 ) break;
8652 if( i>=ArraySize(aSetting) ){
8653 Tcl_SetObjResult(interp,
8654 Tcl_NewStringObj("unknown sqlite3_db_config setting", -1));
8655 return TCL_ERROR;
8657 if( objc==4 ){
8658 if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR;
8659 }else{
8660 v = -1;
8662 sqlite3_db_config(db, aSetting[i].eVal, v, &v);
8663 Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
8664 return TCL_OK;
8667 ** tclcmd: sqlite3_txn_state DB ?SCHEMA?
8669 ** Invoke sqlite3_txn_state(DB,SCHEMA) and return the
8670 ** numeric value that results. Use NULL for SCHEMA if the 3 argument
8671 ** is omitted.
8673 static int SQLITE_TCLAPI test_sqlite3_txn_state(
8674 void *clientData,
8675 Tcl_Interp *interp,
8676 int objc,
8677 Tcl_Obj *CONST objv[]
8679 sqlite3 *db;
8680 const char *zSchema;
8681 int iTxn;
8683 if( objc!=2 && objc!=3 ){
8684 Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCHEMA?");
8685 return TCL_ERROR;
8687 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8688 zSchema = objc==3 ? Tcl_GetString(objv[2]) : 0;
8689 iTxn = sqlite3_txn_state(db, zSchema);
8690 Tcl_SetObjResult(interp, Tcl_NewIntObj(iTxn));
8691 return TCL_OK;
8695 ** Change the name of the main database schema from "main" to "icecube".
8697 static int SQLITE_TCLAPI test_dbconfig_maindbname_icecube(
8698 void * clientData,
8699 Tcl_Interp *interp,
8700 int objc,
8701 Tcl_Obj *CONST objv[]
8703 int rc;
8704 sqlite3 *db;
8705 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
8706 if( objc!=2 ){
8707 Tcl_WrongNumArgs(interp, 1, objv, "DB");
8708 return TCL_ERROR;
8709 }else{
8710 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8711 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "icecube");
8712 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
8713 return TCL_OK;
8718 ** Usage: sqlite3_mmap_warm DB DBNAME
8720 static int SQLITE_TCLAPI test_mmap_warm(
8721 void * clientData,
8722 Tcl_Interp *interp,
8723 int objc,
8724 Tcl_Obj *CONST objv[]
8726 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
8727 extern int sqlite3_mmap_warm(sqlite3 *db, const char *);
8729 if( objc!=2 && objc!=3 ){
8730 Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?");
8731 return TCL_ERROR;
8732 }else{
8733 int rc;
8734 sqlite3 *db;
8735 const char *zDb = 0;
8736 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8737 if( objc==3 ){
8738 zDb = Tcl_GetString(objv[2]);
8740 rc = sqlite3_mmap_warm(db, zDb);
8741 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
8742 return TCL_OK;
8747 ** Usage: test_write_db DB OFFSET DATA
8749 ** Obtain the sqlite3_file* object for the database file for the "main" db
8750 ** of handle DB. Then invoke its xWrite method to write data DATA to offset
8751 ** OFFSET.
8753 static int SQLITE_TCLAPI test_write_db(
8754 void * clientData,
8755 Tcl_Interp *interp,
8756 int objc,
8757 Tcl_Obj *CONST objv[]
8759 sqlite3 *db = 0;
8760 Tcl_WideInt iOff = 0;
8761 const unsigned char *aData = 0;
8762 int nData = 0;
8763 sqlite3_file *pFile = 0;
8764 int rc;
8766 if( objc!=4 ){
8767 Tcl_WrongNumArgs(interp, 1, objv, "DB OFFSET DATA");
8768 return TCL_ERROR;
8770 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8771 if( Tcl_GetWideIntFromObj(interp, objv[2], &iOff) ) return TCL_ERROR;
8772 aData = Tcl_GetByteArrayFromObj(objv[3], &nData);
8774 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFile);
8775 rc = pFile->pMethods->xWrite(pFile, aData, nData, iOff);
8777 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8778 return TCL_OK;
8782 ** Usage: sqlite3_register_cksumvfs
8785 static int SQLITE_TCLAPI test_register_cksumvfs(
8786 void * clientData,
8787 Tcl_Interp *interp,
8788 int objc,
8789 Tcl_Obj *CONST objv[]
8791 if( objc!=1 ){
8792 Tcl_WrongNumArgs(interp, 1, objv, "");
8793 return TCL_ERROR;
8794 }else{
8795 extern int sqlite3_register_cksumvfs(const char*);
8796 int rc = sqlite3_register_cksumvfs(0);
8797 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8799 return TCL_OK;
8803 ** Usage: sqlite3_unregister_cksumvfs
8806 static int SQLITE_TCLAPI test_unregister_cksumvfs(
8807 void * clientData,
8808 Tcl_Interp *interp,
8809 int objc,
8810 Tcl_Obj *CONST objv[]
8812 if( objc!=1 ){
8813 Tcl_WrongNumArgs(interp, 1, objv, "");
8814 return TCL_ERROR;
8815 }else{
8816 extern int sqlite3_unregister_cksumvfs(void);
8817 int rc = sqlite3_unregister_cksumvfs();
8818 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8820 return TCL_OK;
8824 ** Usage: decode_hexdb TEXT
8826 ** Example: db deserialize [decode_hexdb $output_of_dbtotxt]
8828 ** This routine returns a byte-array for an SQLite database file that
8829 ** is constructed from a text input which is the output of the "dbtotxt"
8830 ** utility.
8832 static int SQLITE_TCLAPI test_decode_hexdb(
8833 void * clientData,
8834 Tcl_Interp *interp,
8835 int objc,
8836 Tcl_Obj *CONST objv[]
8838 const char *zIn = 0;
8839 unsigned char *a = 0;
8840 int n = 0;
8841 int lineno = 0;
8842 int i, iNext;
8843 int iOffset = 0;
8844 int j, k;
8845 int rc;
8846 unsigned int x[16];
8847 if( objc!=2 ){
8848 Tcl_WrongNumArgs(interp, 1, objv, "HEXDB");
8849 return TCL_ERROR;
8851 zIn = Tcl_GetString(objv[1]);
8852 for(i=0; zIn[i]; i=iNext){
8853 lineno++;
8854 for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){}
8855 if( zIn[iNext]=='\n' ) iNext++;
8856 while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; }
8857 if( a==0 ){
8858 int pgsz;
8859 rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz);
8860 if( rc!=2 ) continue;
8861 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){
8862 Tcl_AppendResult(interp, "bad 'pagesize' field", (void*)0);
8863 return TCL_ERROR;
8865 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
8866 if( n<512 ){
8867 Tcl_AppendResult(interp, "bad 'size' field", (void*)0);
8868 return TCL_ERROR;
8870 a = malloc( n );
8871 if( a==0 ){
8872 Tcl_AppendResult(interp, "out of memory", (void*)0);
8873 return TCL_ERROR;
8875 memset(a, 0, n);
8876 continue;
8878 rc = sscanf(zIn+i, "| page %d offset %d", &j, &k);
8879 if( rc==2 ){
8880 iOffset = k;
8881 continue;
8883 rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
8884 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
8885 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
8886 if( rc==17 ){
8887 k = iOffset+j;
8888 if( k+16<=n ){
8889 int ii;
8890 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
8892 continue;
8895 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n));
8896 free(a);
8897 return TCL_OK;
8901 ** Client data for the autovacuum_pages callback.
8903 struct AutovacPageData {
8904 Tcl_Interp *interp;
8905 char *zScript;
8907 typedef struct AutovacPageData AutovacPageData;
8910 ** Callback functions for sqlite3_autovacuum_pages
8912 static unsigned int test_autovacuum_pages_callback(
8913 void *pClientData,
8914 const char *zSchema,
8915 unsigned int nFilePages,
8916 unsigned int nFreePages,
8917 unsigned int nBytePerPage
8919 AutovacPageData *pData = (AutovacPageData*)pClientData;
8920 Tcl_DString str;
8921 unsigned int x;
8922 char zBuf[100];
8923 Tcl_DStringInit(&str);
8924 Tcl_DStringAppend(&str, pData->zScript, -1);
8925 Tcl_DStringAppendElement(&str, zSchema);
8926 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFilePages);
8927 Tcl_DStringAppendElement(&str, zBuf);
8928 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFreePages);
8929 Tcl_DStringAppendElement(&str, zBuf);
8930 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nBytePerPage);
8931 Tcl_DStringAppendElement(&str, zBuf);
8932 Tcl_ResetResult(pData->interp);
8933 Tcl_Eval(pData->interp, Tcl_DStringValue(&str));
8934 Tcl_DStringFree(&str);
8935 x = nFreePages;
8936 (void)Tcl_GetIntFromObj(0, Tcl_GetObjResult(pData->interp), (int*)&x);
8937 return x;
8941 ** Usage: sqlite3_autovacuum_pages DB SCRIPT
8943 ** Add an autovacuum-pages callback to database connection DB. The callback
8944 ** will invoke SCRIPT, after appending parameters.
8946 ** If SCRIPT is an empty string or is omitted, then the callback is
8947 ** cancelled.
8949 static int SQLITE_TCLAPI test_autovacuum_pages(
8950 void * clientData,
8951 Tcl_Interp *interp,
8952 int objc,
8953 Tcl_Obj *CONST objv[]
8955 AutovacPageData *pData;
8956 sqlite3 *db;
8957 int rc;
8958 const char *zScript;
8959 if( objc!=2 && objc!=3 ){
8960 Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCRIPT?");
8961 return TCL_ERROR;
8963 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8964 zScript = objc==3 ? Tcl_GetString(objv[2]) : 0;
8965 if( zScript ){
8966 size_t nScript = strlen(zScript);
8967 pData = sqlite3_malloc64( sizeof(*pData) + nScript + 1 );
8968 if( pData==0 ){
8969 Tcl_AppendResult(interp, "out of memory", (void*)0);
8970 return TCL_ERROR;
8972 pData->interp = interp;
8973 pData->zScript = (char*)&pData[1];
8974 memcpy(pData->zScript, zScript, nScript+1);
8975 rc = sqlite3_autovacuum_pages(db,test_autovacuum_pages_callback,
8976 pData, sqlite3_free);
8977 }else{
8978 rc = sqlite3_autovacuum_pages(db, 0, 0, 0);
8980 if( rc ){
8981 char zBuf[1000];
8982 sqlite3_snprintf(sizeof(zBuf), zBuf,
8983 "sqlite3_autovacuum_pages() returns %d", rc);
8984 Tcl_AppendResult(interp, zBuf, (void*)0);
8985 return TCL_ERROR;
8987 return TCL_OK;
8991 ** Usage: number_of_cores
8993 ** Return a guess at the number of available cores available on the
8994 ** processor on which this process is running.
8996 static int SQLITE_TCLAPI guess_number_of_cores(
8997 void * clientData,
8998 Tcl_Interp *interp,
8999 int objc,
9000 Tcl_Obj *CONST objv[]
9002 unsigned int nCore = 1;
9003 #if SQLITE_OS_WIN
9004 SYSTEM_INFO sysinfo;
9005 GetSystemInfo(&sysinfo);
9006 nCore = (unsigned int)sysinfo.dwNumberOfProcessors;
9007 #elif defined(__APPLE__)
9008 int nm[2];
9009 size_t len = 4;
9010 nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
9011 sysctl(nm, 2, &nCore, &len, NULL, 0);
9012 if( nCore<1 ){
9013 nm[1] = HW_NCPU;
9014 sysctl(nm, 2, &nCore, &len, NULL, 0);
9016 #else
9017 nCore = sysconf(_SC_NPROCESSORS_ONLN);
9018 #endif
9019 if( nCore<=0 ) nCore = 1;
9020 Tcl_SetObjResult(interp, Tcl_NewIntObj((int)nCore));
9021 return SQLITE_OK;
9026 ** Register commands with the TCL interpreter.
9028 int Sqlitetest1_Init(Tcl_Interp *interp){
9029 extern int sqlite3_search_count;
9030 extern int sqlite3_found_count;
9031 extern int sqlite3_interrupt_count;
9032 extern int sqlite3_open_file_count;
9033 extern int sqlite3_sort_count;
9034 extern int sqlite3_current_time;
9035 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
9036 extern int sqlite3_hostid_num;
9037 #endif
9038 extern int sqlite3_max_blobsize;
9039 extern int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport(void*,
9040 Tcl_Interp*,int,Tcl_Obj*CONST*);
9041 static int iZero = 0;
9042 static struct {
9043 char *zName;
9044 Tcl_CmdProc *xProc;
9045 } aCmd[] = {
9046 { "db_enter", (Tcl_CmdProc*)db_enter },
9047 { "db_leave", (Tcl_CmdProc*)db_leave },
9048 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
9049 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
9050 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
9051 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
9052 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
9053 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
9054 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
9055 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
9056 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
9057 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
9058 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
9059 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
9060 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
9061 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
9062 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
9063 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
9064 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
9065 #ifndef SQLITE_OMIT_GET_TABLE
9066 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
9067 #endif
9068 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
9069 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 },
9070 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
9071 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
9072 { "sqlite3_drop_modules", (Tcl_CmdProc*)test_drop_modules },
9073 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
9074 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
9075 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
9076 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
9077 { "sqlite3_key", (Tcl_CmdProc*)test_key },
9078 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
9079 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
9080 { "sqlite3_is_interrupted", (Tcl_CmdProc*)test_is_interrupted },
9081 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
9082 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
9083 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
9084 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
9085 { "printf", (Tcl_CmdProc*)test_printf },
9086 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
9087 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address },
9089 static struct {
9090 char *zName;
9091 Tcl_ObjCmdProc *xProc;
9092 void *clientData;
9093 } aObjCmd[] = {
9094 { "sqlite3_db_config", test_sqlite3_db_config, 0 },
9095 { "sqlite3_txn_state", test_sqlite3_txn_state, 0 },
9096 { "bad_behavior", test_bad_behavior, (void*)&iZero },
9097 { "register_dbstat_vtab", test_register_dbstat_vtab },
9098 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
9099 { "intarray_addr", test_intarray_addr, 0 },
9100 { "int64array_addr", test_int64array_addr, 0 },
9101 { "doublearray_addr", test_doublearray_addr, 0 },
9102 { "textarray_addr", test_textarray_addr, 0 },
9103 { "sqlite3_bind_int", test_bind_int, 0 },
9104 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
9105 { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 },
9106 { "sqlite3_bind_int64", test_bind_int64, 0 },
9107 { "sqlite3_bind_double", test_bind_double, 0 },
9108 { "sqlite3_bind_null", test_bind_null ,0 },
9109 { "sqlite3_bind_text", test_bind_text ,0 },
9110 { "sqlite3_bind_text16", test_bind_text16 ,0 },
9111 { "sqlite3_bind_blob", test_bind_blob ,0 },
9112 { "sqlite3_bind_value_from_select",test_bind_value_from_select ,0 },
9113 { "sqlite3_bind_value_from_preupdate",test_bind_value_from_preupdate ,0 },
9114 #ifndef SQLITE_OMIT_VIRTUALTABLE
9115 { "sqlite3_carray_bind", test_carray_bind ,0 },
9116 #endif
9117 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
9118 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
9119 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
9120 { "sqlite3_clear_bindings", test_clear_bindings, 0},
9121 { "sqlite3_sleep", test_sleep, 0},
9122 { "sqlite3_errcode", test_errcode ,0 },
9123 { "sqlite3_extended_errcode", test_ex_errcode ,0 },
9124 { "sqlite3_errmsg", test_errmsg ,0 },
9125 { "sqlite3_error_offset", test_error_offset ,0 },
9126 { "sqlite3_errmsg16", test_errmsg16 ,0 },
9127 { "sqlite3_open", test_open ,0 },
9128 { "sqlite3_open16", test_open16 ,0 },
9129 { "sqlite3_open_v2", test_open_v2 ,0 },
9130 { "sqlite3_complete16", test_complete16 ,0 },
9131 { "sqlite3_normalize", test_normalize ,0 },
9133 { "sqlite3_prepare", test_prepare ,0 },
9134 { "sqlite3_prepare16", test_prepare16 ,0 },
9135 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
9136 { "sqlite3_prepare_v3", test_prepare_v3 ,0 },
9137 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
9138 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
9139 { "sqlite3_finalize", test_finalize ,0 },
9140 { "sqlite3_stmt_status", test_stmt_status ,0 },
9141 { "sqlite3_reset", test_reset ,0 },
9142 { "sqlite3_expired", test_expired ,0 },
9143 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
9144 { "sqlite3_changes", test_changes ,0 },
9145 { "sqlite3_step", test_step ,0 },
9146 { "sqlite3_sql", test_sql ,0 },
9147 { "sqlite3_expanded_sql", test_ex_sql ,0 },
9148 #ifdef SQLITE_ENABLE_NORMALIZE
9149 { "sqlite3_normalized_sql", test_norm_sql ,0 },
9150 #endif
9151 { "sqlite3_next_stmt", test_next_stmt ,0 },
9152 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 },
9153 { "sqlite3_stmt_isexplain", test_stmt_isexplain,0 },
9154 { "sqlite3_stmt_explain", test_stmt_explain ,0 },
9155 { "sqlite3_stmt_busy", test_stmt_busy ,0 },
9156 { "uses_stmt_journal", uses_stmt_journal ,0 },
9158 { "sqlite3_release_memory", test_release_memory, 0},
9159 { "sqlite3_db_release_memory", test_db_release_memory, 0},
9160 { "sqlite3_db_cacheflush", test_db_cacheflush, 0},
9161 { "sqlite3_system_errno", test_system_errno, 0},
9162 { "sqlite3_db_filename", test_db_filename, 0},
9163 { "sqlite3_db_readonly", test_db_readonly, 0},
9164 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
9165 { "sqlite3_soft_heap_limit64", test_soft_heap_limit, 0},
9166 { "sqlite3_hard_heap_limit64", test_hard_heap_limit, 0},
9167 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
9168 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
9170 { "sqlite3_load_extension", test_load_extension, 0},
9171 { "sqlite3_enable_load_extension", test_enable_load, 0},
9172 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
9173 { "sqlite3_limit", test_limit, 0},
9174 { "dbconfig_maindbname_icecube", test_dbconfig_maindbname_icecube },
9176 { "save_prng_state", save_prng_state, 0 },
9177 { "restore_prng_state", restore_prng_state, 0 },
9178 { "reset_prng_state", reset_prng_state, 0 },
9179 { "prng_seed", prng_seed, 0 },
9180 { "extra_schema_checks", extra_schema_checks, 0},
9181 { "use_long_double", use_long_double, 0},
9182 { "database_never_corrupt", database_never_corrupt, 0},
9183 { "database_may_be_corrupt", database_may_be_corrupt, 0},
9184 { "optimization_control", optimization_control,0},
9185 #if SQLITE_OS_WIN
9186 { "lock_win32_file", win32_file_lock, 0 },
9187 { "exists_win32_path", win32_exists_path, 0 },
9188 { "find_win32_file", win32_find_file, 0 },
9189 { "delete_win32_file", win32_delete_file, 0 },
9190 { "make_win32_dir", win32_mkdir, 0 },
9191 { "remove_win32_dir", win32_rmdir, 0 },
9192 #endif
9193 { "tcl_objproc", runAsObjProc, 0 },
9195 /* sqlite3_column_*() API */
9196 { "sqlite3_column_count", test_column_count ,0 },
9197 { "sqlite3_data_count", test_data_count ,0 },
9198 { "sqlite3_column_type", test_column_type ,0 },
9199 { "sqlite3_column_blob", test_column_blob ,0 },
9200 { "sqlite3_column_double", test_column_double ,0 },
9201 { "sqlite3_column_int64", test_column_int64 ,0 },
9202 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
9203 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
9204 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
9205 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
9206 #ifndef SQLITE_OMIT_DECLTYPE
9207 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
9208 #endif
9209 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9210 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
9211 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
9212 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
9213 #endif
9215 #ifndef SQLITE_OMIT_UTF16
9216 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
9217 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
9218 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
9219 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
9220 #ifndef SQLITE_OMIT_DECLTYPE
9221 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
9222 #endif
9223 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9224 {"sqlite3_column_database_name16",
9225 test_stmt_utf16, (void*)sqlite3_column_database_name16},
9226 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
9227 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
9228 #endif
9229 #endif
9230 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
9231 { "sqlite3_global_recover", test_global_recover, 0 },
9232 { "working_64bit_int", working_64bit_int, 0 },
9233 { "vfs_unlink_test", vfs_unlink_test, 0 },
9234 { "vfs_initfail_test", vfs_initfail_test, 0 },
9235 { "vfs_unregister_all", vfs_unregister_all, 0 },
9236 { "vfs_reregister_all", vfs_reregister_all, 0 },
9237 { "file_control_test", file_control_test, 0 },
9238 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
9239 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
9240 { "file_control_chunksize_test", file_control_chunksize_test, 0 },
9241 { "file_control_sizehint_test", file_control_sizehint_test, 0 },
9242 { "file_control_data_version", file_control_data_version, 0 },
9243 #if SQLITE_OS_WIN
9244 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 },
9245 { "file_control_win32_get_handle", file_control_win32_get_handle, 0 },
9246 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 },
9247 #endif
9248 { "file_control_persist_wal", file_control_persist_wal, 0 },
9249 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
9250 { "file_control_vfsname", file_control_vfsname, 0 },
9251 { "file_control_reservebytes", file_control_reservebytes, 0 },
9252 { "file_control_tempfilename", file_control_tempfilename, 0 },
9253 { "file_control_external_reader", file_control_external_reader, 0 },
9254 { "sqlite3_vfs_list", vfs_list, 0 },
9255 { "sqlite3_create_function_v2", test_create_function_v2, 0 },
9257 /* Functions from os.h */
9258 #ifndef SQLITE_OMIT_UTF16
9259 { "add_test_collate", test_collate, 0 },
9260 { "add_test_collate_needed", test_collate_needed, 0 },
9261 { "add_test_function", test_function, 0 },
9262 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 },
9263 #endif
9264 { "sqlite3_test_errstr", test_errstr, 0 },
9265 { "tcl_variable_type", tcl_variable_type, 0 },
9266 #ifndef SQLITE_OMIT_SHARED_CACHE
9267 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
9268 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
9269 #endif
9270 { "sqlite3_libversion_number", test_libversion_number, 0 },
9271 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
9272 #ifndef SQLITE_OMIT_INCRBLOB
9273 { "sqlite3_blob_reopen", test_blob_reopen, 0 },
9274 #endif
9275 { "pcache_stats", test_pcache_stats, 0 },
9276 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
9277 { "sqlite3_unlock_notify", test_unlock_notify, 0 },
9278 #endif
9279 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
9280 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 },
9281 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 },
9282 { "test_sqlite3_log", test_sqlite3_log, 0 },
9283 #ifndef SQLITE_OMIT_EXPLAIN
9284 { "print_explain_query_plan", test_print_eqp, 0 },
9285 #endif
9286 { "strftime", strftime_cmd },
9287 { "sqlite3_test_control", test_test_control },
9288 { ".treetrace", test_treetrace },
9289 #if SQLITE_OS_UNIX
9290 { "getrusage", test_getrusage },
9291 #endif
9292 { "load_static_extension", tclLoadStaticExtensionCmd },
9293 { "sorter_test_fakeheap", sorter_test_fakeheap },
9294 { "sorter_test_sort4_helper", sorter_test_sort4_helper },
9295 #ifdef SQLITE_USER_AUTHENTICATION
9296 { "sqlite3_user_authenticate", test_user_authenticate, 0 },
9297 { "sqlite3_user_add", test_user_add, 0 },
9298 { "sqlite3_user_change", test_user_change, 0 },
9299 { "sqlite3_user_delete", test_user_delete, 0 },
9300 #endif
9301 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
9302 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 },
9303 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 },
9304 #endif
9305 #ifdef SQLITE_ENABLE_SQLLOG
9306 { "sqlite3_config_sqllog", test_config_sqllog, 0 },
9307 #endif
9308 { "vfs_current_time_int64", vfsCurrentTimeInt64, 0 },
9309 #ifdef SQLITE_ENABLE_SNAPSHOT
9310 { "sqlite3_snapshot_get", test_snapshot_get, 0 },
9311 { "sqlite3_snapshot_open", test_snapshot_open, 0 },
9312 { "sqlite3_snapshot_free", test_snapshot_free, 0 },
9313 { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 },
9314 { "sqlite3_snapshot_recover", test_snapshot_recover, 0 },
9315 { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 },
9316 { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 },
9317 { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 },
9318 #endif
9319 { "sqlite3_delete_database", test_delete_database, 0 },
9320 { "atomic_batch_write", test_atomic_batch_write, 0 },
9321 { "sqlite3_mmap_warm", test_mmap_warm, 0 },
9322 { "sqlite3_config_sorterref", test_config_sorterref, 0 },
9323 { "sqlite3_autovacuum_pages", test_autovacuum_pages, 0 },
9324 { "decode_hexdb", test_decode_hexdb, 0 },
9325 { "test_write_db", test_write_db, 0 },
9326 { "sqlite3_register_cksumvfs", test_register_cksumvfs, 0 },
9327 { "sqlite3_unregister_cksumvfs", test_unregister_cksumvfs, 0 },
9328 { "number_of_cores", guess_number_of_cores, 0 },
9329 #ifndef SQLITE_OMIT_VIRTUALTABLE
9330 { "create_null_module", test_create_null_module, 0 },
9331 #endif
9333 static int bitmask_size = sizeof(Bitmask)*8;
9334 static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
9335 int i;
9336 extern int sqlite3_sync_count, sqlite3_fullsync_count;
9337 extern int sqlite3_opentemp_count;
9338 extern int sqlite3_like_count;
9339 extern int sqlite3_xferopt_count;
9340 extern int sqlite3_pager_readdb_count;
9341 extern int sqlite3_pager_writedb_count;
9342 extern int sqlite3_pager_writej_count;
9343 #if SQLITE_OS_WIN
9344 extern LONG volatile sqlite3_os_type;
9345 #endif
9346 #ifdef SQLITE_DEBUG
9347 extern u32 sqlite3WhereTrace;
9348 extern int sqlite3OSTrace;
9349 extern int sqlite3WalTrace;
9350 #endif
9351 #ifdef SQLITE_TEST
9352 #ifdef SQLITE_ENABLE_FTS3
9353 extern int sqlite3_fts3_enable_parentheses;
9354 #endif
9355 #endif
9357 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
9358 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
9360 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
9361 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
9362 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
9364 Tcl_LinkVar(interp, "sqlite_search_count",
9365 (char*)&sqlite3_search_count, TCL_LINK_INT);
9366 Tcl_LinkVar(interp, "sqlite_found_count",
9367 (char*)&sqlite3_found_count, TCL_LINK_INT);
9368 Tcl_LinkVar(interp, "sqlite_sort_count",
9369 (char*)&sqlite3_sort_count, TCL_LINK_INT);
9370 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
9371 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
9372 Tcl_LinkVar(interp, "sqlite_like_count",
9373 (char*)&sqlite3_like_count, TCL_LINK_INT);
9374 Tcl_LinkVar(interp, "sqlite_interrupt_count",
9375 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
9376 Tcl_LinkVar(interp, "sqlite_open_file_count",
9377 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
9378 Tcl_LinkVar(interp, "sqlite_current_time",
9379 (char*)&sqlite3_current_time, TCL_LINK_INT);
9380 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
9381 Tcl_LinkVar(interp, "sqlite_hostid_num",
9382 (char*)&sqlite3_hostid_num, TCL_LINK_INT);
9383 #endif
9384 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
9385 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
9386 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
9387 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
9388 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
9389 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
9390 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
9391 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
9392 #ifndef SQLITE_OMIT_UTF16
9393 Tcl_LinkVar(interp, "unaligned_string_counter",
9394 (char*)&unaligned_string_counter, TCL_LINK_INT);
9395 #endif
9396 #ifndef SQLITE_OMIT_UTF16
9397 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
9398 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
9399 #endif
9400 #if SQLITE_OS_WIN
9401 Tcl_LinkVar(interp, "sqlite_os_type",
9402 (char*)&sqlite3_os_type, TCL_LINK_LONG);
9403 #endif
9404 #ifdef SQLITE_TEST
9406 static const char *query_plan = "*** OBSOLETE VARIABLE ***";
9407 Tcl_LinkVar(interp, "sqlite_query_plan",
9408 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
9410 #endif
9411 #ifdef SQLITE_DEBUG
9412 Tcl_LinkVar(interp, "sqlite_where_trace",
9413 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
9414 Tcl_LinkVar(interp, "sqlite_os_trace",
9415 (char*)&sqlite3OSTrace, TCL_LINK_INT);
9416 #ifndef SQLITE_OMIT_WAL
9417 Tcl_LinkVar(interp, "sqlite_wal_trace",
9418 (char*)&sqlite3WalTrace, TCL_LINK_INT);
9419 #endif
9420 #endif
9421 #ifndef SQLITE_OMIT_DISKIO
9422 Tcl_LinkVar(interp, "sqlite_opentemp_count",
9423 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
9424 #endif
9425 Tcl_LinkVar(interp, "sqlite_static_bind_value",
9426 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
9427 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
9428 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
9429 Tcl_LinkVar(interp, "sqlite_temp_directory",
9430 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
9431 Tcl_LinkVar(interp, "sqlite_data_directory",
9432 (char*)&sqlite3_data_directory, TCL_LINK_STRING);
9433 Tcl_LinkVar(interp, "bitmask_size",
9434 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
9435 Tcl_LinkVar(interp, "longdouble_size",
9436 (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
9437 Tcl_LinkVar(interp, "sqlite_sync_count",
9438 (char*)&sqlite3_sync_count, TCL_LINK_INT);
9439 Tcl_LinkVar(interp, "sqlite_fullsync_count",
9440 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
9441 #if defined(SQLITE_ENABLE_TREETRACE)
9442 Tcl_LinkVar(interp, "sqlite3_unsupported_treetrace",
9443 (char*)&sqlite3TreeTrace, TCL_LINK_INT);
9444 #endif
9445 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
9446 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
9447 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
9448 #endif
9449 return TCL_OK;