Add tests for the new code on this branch.
[sqlite.git] / src / test1.c
blob8faf5a397ba5b2127c45482e1a1c706a19d557ee
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 return TCL_OK;
669 ** Usage: sqlite3_rekey DB KEY
671 ** Change the codec key.
673 static int SQLITE_TCLAPI test_rekey(
674 void *NotUsed,
675 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
676 int argc, /* Number of arguments */
677 char **argv /* Text of each argument */
679 return TCL_OK;
683 ** Usage: sqlite3_close DB
685 ** Closes the database opened by sqlite3_open.
687 static int SQLITE_TCLAPI sqlite_test_close(
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 sqlite3 *db;
694 int rc;
695 if( argc!=2 ){
696 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
697 " FILENAME\"", 0);
698 return TCL_ERROR;
700 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
701 rc = sqlite3_close(db);
702 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
703 return TCL_OK;
707 ** Usage: sqlite3_close_v2 DB
709 ** Closes the database opened by sqlite3_open.
711 static int SQLITE_TCLAPI sqlite_test_close_v2(
712 void *NotUsed,
713 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
714 int argc, /* Number of arguments */
715 char **argv /* Text of each argument */
717 sqlite3 *db;
718 int rc;
719 if( argc!=2 ){
720 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
721 " FILENAME\"", 0);
722 return TCL_ERROR;
724 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
725 rc = sqlite3_close_v2(db);
726 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
727 return TCL_OK;
731 ** Implementation of the x_coalesce() function.
732 ** Return the first argument non-NULL argument.
734 static void t1_ifnullFunc(
735 sqlite3_context *context,
736 int argc,
737 sqlite3_value **argv
739 int i;
740 for(i=0; i<argc; i++){
741 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
742 int n = sqlite3_value_bytes(argv[i]);
743 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
744 n, SQLITE_TRANSIENT);
745 break;
751 ** These are test functions. hex8() interprets its argument as
752 ** UTF8 and returns a hex encoding. hex16le() interprets its argument
753 ** as UTF16le and returns a hex encoding.
755 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
756 const unsigned char *z;
757 int i;
758 char zBuf[200];
759 z = sqlite3_value_text(argv[0]);
760 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
761 sqlite3_snprintf(sizeof(zBuf)-i*2, &zBuf[i*2], "%02x", z[i]);
763 zBuf[i*2] = 0;
764 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
766 #ifndef SQLITE_OMIT_UTF16
767 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
768 const unsigned short int *z;
769 int i;
770 char zBuf[400];
771 z = sqlite3_value_text16(argv[0]);
772 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
773 sqlite3_snprintf(sizeof(zBuf)-i*4, &zBuf[i*4],"%04x", z[i]&0xff);
775 zBuf[i*4] = 0;
776 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
778 #endif
781 ** A structure into which to accumulate text.
783 struct dstr {
784 int nAlloc; /* Space allocated */
785 int nUsed; /* Space used */
786 char *z; /* The space */
790 ** Append text to a dstr
792 static void dstrAppend(struct dstr *p, const char *z, int divider){
793 int n = (int)strlen(z);
794 if( p->nUsed + n + 2 > p->nAlloc ){
795 char *zNew;
796 p->nAlloc = p->nAlloc*2 + n + 200;
797 zNew = sqlite3_realloc(p->z, p->nAlloc);
798 if( zNew==0 ){
799 sqlite3_free(p->z);
800 memset(p, 0, sizeof(*p));
801 return;
803 p->z = zNew;
805 if( divider && p->nUsed>0 ){
806 p->z[p->nUsed++] = divider;
808 memcpy(&p->z[p->nUsed], z, n+1);
809 p->nUsed += n;
813 ** Invoked for each callback from sqlite3ExecFunc
815 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
816 struct dstr *p = (struct dstr*)pData;
817 int i;
818 for(i=0; i<argc; i++){
819 if( argv[i]==0 ){
820 dstrAppend(p, "NULL", ' ');
821 }else{
822 dstrAppend(p, argv[i], ' ');
825 return 0;
829 ** Implementation of the x_sqlite_exec() function. This function takes
830 ** a single argument and attempts to execute that argument as SQL code.
831 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
833 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
834 ** from within a function call.
836 ** This routine simulates the effect of having two threads attempt to
837 ** use the same database at the same time.
839 static void sqlite3ExecFunc(
840 sqlite3_context *context,
841 int argc,
842 sqlite3_value **argv
844 struct dstr x;
845 memset(&x, 0, sizeof(x));
846 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
847 (char*)sqlite3_value_text(argv[0]),
848 execFuncCallback, &x, 0);
849 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
850 sqlite3_free(x.z);
854 ** Implementation of tkt2213func(), a scalar function that takes exactly
855 ** one argument. It has two interesting features:
857 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
858 ** If the three pointers returned are not the same an SQL error is raised.
860 ** * Otherwise it returns a copy of the text representation of its
861 ** argument in such a way as the VDBE representation is a Mem* cell
862 ** with the MEM_Term flag clear.
864 ** Ticket #2213 can therefore be tested by evaluating the following
865 ** SQL expression:
867 ** tkt2213func(tkt2213func('a string'));
869 static void tkt2213Function(
870 sqlite3_context *context,
871 int argc,
872 sqlite3_value **argv
874 int nText;
875 unsigned char const *zText1;
876 unsigned char const *zText2;
877 unsigned char const *zText3;
879 nText = sqlite3_value_bytes(argv[0]);
880 zText1 = sqlite3_value_text(argv[0]);
881 zText2 = sqlite3_value_text(argv[0]);
882 zText3 = sqlite3_value_text(argv[0]);
884 if( zText1!=zText2 || zText2!=zText3 ){
885 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
886 }else{
887 char *zCopy = (char *)sqlite3_malloc(nText);
888 memcpy(zCopy, zText1, nText);
889 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
894 ** The following SQL function takes 4 arguments. The 2nd and
895 ** 4th argument must be one of these strings: 'text', 'text16',
896 ** or 'blob' corresponding to API functions
898 ** sqlite3_value_text()
899 ** sqlite3_value_text16()
900 ** sqlite3_value_blob()
902 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
903 ** corresponding to APIs:
905 ** sqlite3_value_bytes()
906 ** sqlite3_value_bytes16()
907 ** noop
909 ** The APIs designated by the 2nd through 4th arguments are applied
910 ** to the first argument in order. If the pointers returned by the
911 ** second and fourth are different, this routine returns 1. Otherwise,
912 ** this routine returns 0.
914 ** This function is used to test to see when returned pointers from
915 ** the _text(), _text16() and _blob() APIs become invalidated.
917 static void ptrChngFunction(
918 sqlite3_context *context,
919 int argc,
920 sqlite3_value **argv
922 const void *p1, *p2;
923 const char *zCmd;
924 if( argc!=4 ) return;
925 zCmd = (const char*)sqlite3_value_text(argv[1]);
926 if( zCmd==0 ) return;
927 if( strcmp(zCmd,"text")==0 ){
928 p1 = (const void*)sqlite3_value_text(argv[0]);
929 #ifndef SQLITE_OMIT_UTF16
930 }else if( strcmp(zCmd, "text16")==0 ){
931 p1 = (const void*)sqlite3_value_text16(argv[0]);
932 #endif
933 }else if( strcmp(zCmd, "blob")==0 ){
934 p1 = (const void*)sqlite3_value_blob(argv[0]);
935 }else{
936 return;
938 zCmd = (const char*)sqlite3_value_text(argv[2]);
939 if( zCmd==0 ) return;
940 if( strcmp(zCmd,"bytes")==0 ){
941 sqlite3_value_bytes(argv[0]);
942 #ifndef SQLITE_OMIT_UTF16
943 }else if( strcmp(zCmd, "bytes16")==0 ){
944 sqlite3_value_bytes16(argv[0]);
945 #endif
946 }else if( strcmp(zCmd, "noop")==0 ){
947 /* do nothing */
948 }else{
949 return;
951 zCmd = (const char*)sqlite3_value_text(argv[3]);
952 if( zCmd==0 ) return;
953 if( strcmp(zCmd,"text")==0 ){
954 p2 = (const void*)sqlite3_value_text(argv[0]);
955 #ifndef SQLITE_OMIT_UTF16
956 }else if( strcmp(zCmd, "text16")==0 ){
957 p2 = (const void*)sqlite3_value_text16(argv[0]);
958 #endif
959 }else if( strcmp(zCmd, "blob")==0 ){
960 p2 = (const void*)sqlite3_value_blob(argv[0]);
961 }else{
962 return;
964 sqlite3_result_int(context, p1!=p2);
968 ** This SQL function returns a different answer each time it is called, even if
969 ** the arguments are the same.
971 static void nondeterministicFunction(
972 sqlite3_context *context,
973 int argc,
974 sqlite3_value **argv
976 static int cnt = 0;
977 sqlite3_result_int(context, cnt++);
981 ** This SQL function returns the integer value of its argument as a MEM_IntReal
982 ** value.
984 static void intrealFunction(
985 sqlite3_context *context,
986 int argc,
987 sqlite3_value **argv
989 sqlite3_int64 v = sqlite3_value_int64(argv[0]);
990 sqlite3_result_int64(context, v);
991 sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, context);
995 ** These SQL functions attempt to return a value (their first argument)
996 ** that has been modified to have multiple datatypes. For example both
997 ** TEXT and INTEGER.
999 static void addTextTypeFunction(
1000 sqlite3_context *context,
1001 int argc,
1002 sqlite3_value **argv
1004 (void)sqlite3_value_text(argv[0]);
1005 (void)argc;
1006 sqlite3_result_value(context, argv[0]);
1008 static void addIntTypeFunction(
1009 sqlite3_context *context,
1010 int argc,
1011 sqlite3_value **argv
1013 (void)sqlite3_value_int64(argv[0]);
1014 (void)argc;
1015 sqlite3_result_value(context, argv[0]);
1017 static void addRealTypeFunction(
1018 sqlite3_context *context,
1019 int argc,
1020 sqlite3_value **argv
1022 (void)sqlite3_value_double(argv[0]);
1023 (void)argc;
1024 sqlite3_result_value(context, argv[0]);
1028 ** SQL function: strtod(X)
1030 ** Use the C-library strtod() function to convert string X into a double.
1031 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1032 ** routines against the C-library.
1034 static void shellStrtod(
1035 sqlite3_context *pCtx,
1036 int nVal,
1037 sqlite3_value **apVal
1039 char *z = (char*)sqlite3_value_text(apVal[0]);
1040 UNUSED_PARAMETER(nVal);
1041 if( z==0 ) return;
1042 sqlite3_result_double(pCtx, strtod(z,0));
1046 ** SQL function: dtostr(X)
1048 ** Use the C-library printf() function to convert real value X into a string.
1049 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1050 ** routines against the C-library.
1052 static void shellDtostr(
1053 sqlite3_context *pCtx,
1054 int nVal,
1055 sqlite3_value **apVal
1057 double r = sqlite3_value_double(apVal[0]);
1058 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1059 char z[400];
1060 if( n<1 ) n = 1;
1061 if( n>350 ) n = 350;
1062 sprintf(z, "%#+.*e", n, r);
1063 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1068 ** Usage: sqlite3_create_function DB
1070 ** Call the sqlite3_create_function API on the given database in order
1071 ** to create a function named "x_coalesce". This function does the same thing
1072 ** as the "coalesce" function. This function also registers an SQL function
1073 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
1074 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
1075 ** The effect is similar to trying to use the same database connection from
1076 ** two threads at the same time.
1078 ** The original motivation for this routine was to be able to call the
1079 ** sqlite3_create_function function while a query is in progress in order
1080 ** to test the SQLITE_MISUSE detection logic.
1082 static int SQLITE_TCLAPI test_create_function(
1083 void *NotUsed,
1084 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1085 int argc, /* Number of arguments */
1086 char **argv /* Text of each argument */
1088 int rc;
1089 sqlite3 *db;
1091 if( argc!=2 ){
1092 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1093 " DB\"", 0);
1094 return TCL_ERROR;
1096 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1097 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0,
1098 t1_ifnullFunc, 0, 0);
1099 if( rc==SQLITE_OK ){
1100 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1101 0, hex8Func, 0, 0);
1103 #ifndef SQLITE_OMIT_UTF16
1104 if( rc==SQLITE_OK ){
1105 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
1106 0, hex16Func, 0, 0);
1108 #endif
1109 if( rc==SQLITE_OK ){
1110 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
1111 tkt2213Function, 0, 0);
1113 if( rc==SQLITE_OK ){
1114 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
1115 ptrChngFunction, 0, 0);
1118 /* Functions counter1() and counter2() have the same implementation - they
1119 ** both return an ascending integer with each call. But counter1() is marked
1120 ** as non-deterministic and counter2() is marked as deterministic.
1122 if( rc==SQLITE_OK ){
1123 rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
1124 0, nondeterministicFunction, 0, 0);
1126 if( rc==SQLITE_OK ){
1127 rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
1128 0, nondeterministicFunction, 0, 0);
1131 /* The intreal() function converts its argument to an integer and returns
1132 ** it as a MEM_IntReal.
1134 if( rc==SQLITE_OK ){
1135 rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8,
1136 0, intrealFunction, 0, 0);
1139 /* The add_text_type(), add_int_type(), and add_real_type() functions
1140 ** attempt to return a value that has multiple datatypes.
1142 if( rc==SQLITE_OK ){
1143 rc = sqlite3_create_function(db, "add_text_type", 1, SQLITE_UTF8,
1144 0, addTextTypeFunction, 0, 0);
1146 if( rc==SQLITE_OK ){
1147 rc = sqlite3_create_function(db, "add_int_type", 1, SQLITE_UTF8,
1148 0, addIntTypeFunction, 0, 0);
1150 if( rc==SQLITE_OK ){
1151 rc = sqlite3_create_function(db, "add_real_type", 1, SQLITE_UTF8,
1152 0, addRealTypeFunction, 0, 0);
1155 /* Functions strtod() and dtostr() work as in the shell. These routines
1156 ** use the standard C library to convert between floating point and
1157 ** text. This is used to compare SQLite's internal conversion routines
1158 ** against the standard library conversion routines.
1160 ** Both routines copy/pasted from the shell.c.in implementation
1161 ** on 2023-07-03.
1163 if( rc==SQLITE_OK ){
1164 rc = sqlite3_create_function(db, "strtod", 1, SQLITE_UTF8, 0,
1165 shellStrtod, 0, 0);
1167 if( rc==SQLITE_OK ){
1168 rc = sqlite3_create_function(db, "dtostr", 1, SQLITE_UTF8, 0,
1169 shellDtostr, 0, 0);
1171 if( rc==SQLITE_OK ){
1172 rc = sqlite3_create_function(db, "dtostr", 2, SQLITE_UTF8, 0,
1173 shellDtostr, 0, 0);
1176 #ifndef SQLITE_OMIT_UTF16
1177 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1178 ** because it is not tested anywhere else. */
1179 if( rc==SQLITE_OK ){
1180 const void *zUtf16;
1181 sqlite3_value *pVal;
1182 sqlite3_mutex_enter(db->mutex);
1183 pVal = sqlite3ValueNew(db);
1184 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
1185 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1186 if( db->mallocFailed ){
1187 rc = SQLITE_NOMEM;
1188 }else{
1189 rc = sqlite3_create_function16(db, zUtf16,
1190 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
1192 sqlite3ValueFree(pVal);
1193 sqlite3_mutex_leave(db->mutex);
1195 #endif
1197 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1198 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1199 return TCL_OK;
1203 ** Usage: sqlite3_drop_modules DB ?NAME ...?
1205 ** Invoke the sqlite3_drop_modules(D,L) interface on database
1206 ** connection DB, in order to drop all modules except those named in
1207 ** the argument.
1209 static int SQLITE_TCLAPI test_drop_modules(
1210 void *NotUsed,
1211 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1212 int argc, /* Number of arguments */
1213 char **argv /* Text of each argument */
1215 sqlite3 *db;
1217 if( argc<2 ){
1218 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1219 " DB\"", 0);
1220 return TCL_ERROR;
1222 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1223 #ifndef SQLITE_OMIT_VIRTUALTABLE
1224 sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0);
1225 #endif
1226 return TCL_OK;
1230 ** Routines to implement the x_count() aggregate function.
1232 ** x_count() counts the number of non-null arguments. But there are
1233 ** some twists for testing purposes.
1235 ** If the argument to x_count() is 40 then a UTF-8 error is reported
1236 ** on the step function. If x_count(41) is seen, then a UTF-16 error
1237 ** is reported on the step function. If the total count is 42, then
1238 ** a UTF-8 error is reported on the finalize function.
1240 typedef struct t1CountCtx t1CountCtx;
1241 struct t1CountCtx {
1242 int n;
1244 static void t1CountStep(
1245 sqlite3_context *context,
1246 int argc,
1247 sqlite3_value **argv
1249 t1CountCtx *p;
1250 p = sqlite3_aggregate_context(context, sizeof(*p));
1251 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
1252 p->n++;
1254 if( argc>0 ){
1255 int v = sqlite3_value_int(argv[0]);
1256 if( v==40 ){
1257 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
1258 #ifndef SQLITE_OMIT_UTF16
1259 }else if( v==41 ){
1260 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1261 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
1262 #endif
1266 static void t1CountFinalize(sqlite3_context *context){
1267 t1CountCtx *p;
1268 p = sqlite3_aggregate_context(context, sizeof(*p));
1269 if( p ){
1270 if( p->n==42 ){
1271 sqlite3_result_error(context, "x_count totals to 42", -1);
1272 }else{
1273 sqlite3_result_int(context, p ? p->n : 0);
1278 #ifndef SQLITE_OMIT_DEPRECATED
1279 static void legacyCountStep(
1280 sqlite3_context *context,
1281 int argc,
1282 sqlite3_value **argv
1284 /* no-op */
1287 static void legacyCountFinalize(sqlite3_context *context){
1288 sqlite3_result_int(context, sqlite3_aggregate_count(context));
1290 #endif
1293 ** Usage: sqlite3_create_aggregate DB
1295 ** Call the sqlite3_create_function API on the given database in order
1296 ** to create a function named "x_count". This function is similar
1297 ** to the built-in count() function, with a few special quirks
1298 ** for testing the sqlite3_result_error() APIs.
1300 ** The original motivation for this routine was to be able to call the
1301 ** sqlite3_create_aggregate function while a query is in progress in order
1302 ** to test the SQLITE_MISUSE detection logic. See misuse.test.
1304 ** This routine was later extended to test the use of sqlite3_result_error()
1305 ** within aggregate functions.
1307 ** Later: It is now also extended to register the aggregate function
1308 ** "legacy_count()" with the supplied database handle. This is used
1309 ** to test the deprecated sqlite3_aggregate_count() API.
1311 static int SQLITE_TCLAPI test_create_aggregate(
1312 void *NotUsed,
1313 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1314 int argc, /* Number of arguments */
1315 char **argv /* Text of each argument */
1317 sqlite3 *db;
1318 int rc;
1319 if( argc!=2 ){
1320 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1321 " FILENAME\"", 0);
1322 return TCL_ERROR;
1324 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1325 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
1326 t1CountStep,t1CountFinalize);
1327 if( rc==SQLITE_OK ){
1328 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
1329 t1CountStep,t1CountFinalize);
1331 #ifndef SQLITE_OMIT_DEPRECATED
1332 if( rc==SQLITE_OK ){
1333 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1334 legacyCountStep, legacyCountFinalize
1337 #endif
1338 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1339 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1340 return TCL_OK;
1345 ** Usage: printf TEXT
1347 ** Send output to printf. Use this rather than puts to merge the output
1348 ** in the correct sequence with debugging printfs inserted into C code.
1349 ** Puts uses a separate buffer and debugging statements will be out of
1350 ** sequence if it is used.
1352 static int SQLITE_TCLAPI test_printf(
1353 void *NotUsed,
1354 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1355 int argc, /* Number of arguments */
1356 char **argv /* Text of each argument */
1358 if( argc!=2 ){
1359 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1360 " TEXT\"", 0);
1361 return TCL_ERROR;
1363 printf("%s\n", argv[1]);
1364 return TCL_OK;
1370 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1372 ** Call mprintf with three integer arguments
1374 static int SQLITE_TCLAPI sqlite3_mprintf_int(
1375 void *NotUsed,
1376 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1377 int argc, /* Number of arguments */
1378 char **argv /* Text of each argument */
1380 int a[3], i;
1381 char *z;
1382 if( argc!=5 ){
1383 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1384 " FORMAT INT INT INT\"", 0);
1385 return TCL_ERROR;
1387 for(i=2; i<5; i++){
1388 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1390 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1391 Tcl_AppendResult(interp, z, 0);
1392 sqlite3_free(z);
1393 return TCL_OK;
1397 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1399 ** Call mprintf with three 64-bit integer arguments
1401 static int SQLITE_TCLAPI sqlite3_mprintf_int64(
1402 void *NotUsed,
1403 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1404 int argc, /* Number of arguments */
1405 char **argv /* Text of each argument */
1407 int i;
1408 sqlite_int64 a[3];
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( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){
1417 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1418 return TCL_ERROR;
1421 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1422 Tcl_AppendResult(interp, z, 0);
1423 sqlite3_free(z);
1424 return TCL_OK;
1428 ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1430 ** Call mprintf with three long integer arguments. This might be the
1431 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1432 ** platform.
1434 static int SQLITE_TCLAPI sqlite3_mprintf_long(
1435 void *NotUsed,
1436 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1437 int argc, /* Number of arguments */
1438 char **argv /* Text of each argument */
1440 int i;
1441 long int a[3];
1442 int b[3];
1443 char *z;
1444 if( argc!=5 ){
1445 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1446 " FORMAT INT INT INT\"", 0);
1447 return TCL_ERROR;
1449 for(i=2; i<5; i++){
1450 if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
1451 a[i-2] = (long int)b[i-2];
1452 a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
1454 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1455 Tcl_AppendResult(interp, z, 0);
1456 sqlite3_free(z);
1457 return TCL_OK;
1461 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1463 ** Call mprintf with two integer arguments and one string argument
1465 static int SQLITE_TCLAPI sqlite3_mprintf_str(
1466 void *NotUsed,
1467 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1468 int argc, /* Number of arguments */
1469 char **argv /* Text of each argument */
1471 int a[3], i;
1472 char *z;
1473 if( argc<4 || argc>5 ){
1474 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1475 " FORMAT INT INT ?STRING?\"", 0);
1476 return TCL_ERROR;
1478 for(i=2; i<4; i++){
1479 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1481 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1482 Tcl_AppendResult(interp, z, 0);
1483 sqlite3_free(z);
1484 return TCL_OK;
1488 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1490 ** Call mprintf with two integer arguments and one string argument
1492 static int SQLITE_TCLAPI sqlite3_snprintf_str(
1493 void *NotUsed,
1494 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1495 int argc, /* Number of arguments */
1496 char **argv /* Text of each argument */
1498 int a[3], i;
1499 int n;
1500 char *z;
1501 if( argc<5 || argc>6 ){
1502 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1503 " INT FORMAT INT INT ?STRING?\"", 0);
1504 return TCL_ERROR;
1506 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1507 if( n<0 ){
1508 Tcl_AppendResult(interp, "N must be non-negative", 0);
1509 return TCL_ERROR;
1511 for(i=3; i<5; i++){
1512 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1514 z = sqlite3_malloc( n+1 );
1515 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1516 Tcl_AppendResult(interp, z, 0);
1517 sqlite3_free(z);
1518 return TCL_OK;
1522 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1524 ** Call mprintf with two integer arguments and one double argument
1526 static int SQLITE_TCLAPI sqlite3_mprintf_double(
1527 void *NotUsed,
1528 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1529 int argc, /* Number of arguments */
1530 char **argv /* Text of each argument */
1532 int a[3], i;
1533 double r;
1534 char *z;
1535 if( argc!=5 ){
1536 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1537 " FORMAT INT INT DOUBLE\"", 0);
1538 return TCL_ERROR;
1540 for(i=2; i<4; i++){
1541 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1543 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1544 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1545 Tcl_AppendResult(interp, z, 0);
1546 sqlite3_free(z);
1547 return TCL_OK;
1551 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1553 ** Call mprintf with a single double argument which is the product of the
1554 ** two arguments given above. This is used to generate overflow and underflow
1555 ** doubles to test that they are converted properly.
1557 static int SQLITE_TCLAPI sqlite3_mprintf_scaled(
1558 void *NotUsed,
1559 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1560 int argc, /* Number of arguments */
1561 char **argv /* Text of each argument */
1563 int i;
1564 double r[2];
1565 char *z;
1566 if( argc!=4 ){
1567 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1568 " FORMAT DOUBLE DOUBLE\"", 0);
1569 return TCL_ERROR;
1571 for(i=2; i<4; i++){
1572 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1574 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1575 Tcl_AppendResult(interp, z, 0);
1576 sqlite3_free(z);
1577 return TCL_OK;
1581 ** Usage: sqlite3_mprintf_stronly FORMAT STRING
1583 ** Call mprintf with a single double argument which is the product of the
1584 ** two arguments given above. This is used to generate overflow and underflow
1585 ** doubles to test that they are converted properly.
1587 static int SQLITE_TCLAPI sqlite3_mprintf_stronly(
1588 void *NotUsed,
1589 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1590 int argc, /* Number of arguments */
1591 char **argv /* Text of each argument */
1593 char *z;
1594 if( argc!=3 ){
1595 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1596 " FORMAT STRING\"", 0);
1597 return TCL_ERROR;
1599 z = sqlite3_mprintf(argv[1], argv[2]);
1600 Tcl_AppendResult(interp, z, 0);
1601 sqlite3_free(z);
1602 return TCL_OK;
1606 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1608 ** Call mprintf with a single double argument which is derived from the
1609 ** hexadecimal encoding of an IEEE double.
1611 static int SQLITE_TCLAPI sqlite3_mprintf_hexdouble(
1612 void *NotUsed,
1613 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1614 int argc, /* Number of arguments */
1615 char **argv /* Text of each argument */
1617 char *z;
1618 double r;
1619 unsigned int x1, x2;
1620 sqlite_uint64 d;
1621 if( argc!=3 ){
1622 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1623 " FORMAT STRING\"", 0);
1624 return TCL_ERROR;
1626 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1627 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1628 return TCL_ERROR;
1630 d = x2;
1631 d = (d<<32) + x1;
1632 memcpy(&r, &d, sizeof(r));
1633 z = sqlite3_mprintf(argv[1], r);
1634 Tcl_AppendResult(interp, z, 0);
1635 sqlite3_free(z);
1636 return TCL_OK;
1640 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1643 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1644 static int SQLITE_TCLAPI test_enable_shared(
1645 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1646 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1647 int objc, /* Number of arguments */
1648 Tcl_Obj *CONST objv[] /* Command arguments */
1650 int rc;
1651 int enable;
1652 int ret = 0;
1654 if( objc!=2 && objc!=1 ){
1655 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
1656 return TCL_ERROR;
1658 ret = sqlite3GlobalConfig.sharedCacheEnabled;
1660 if( objc==2 ){
1661 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1662 return TCL_ERROR;
1664 rc = sqlite3_enable_shared_cache(enable);
1665 if( rc!=SQLITE_OK ){
1666 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1667 return TCL_ERROR;
1670 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1671 return TCL_OK;
1673 #endif
1678 ** Usage: sqlite3_extended_result_codes DB BOOLEAN
1681 static int SQLITE_TCLAPI test_extended_result_codes(
1682 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1683 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1684 int objc, /* Number of arguments */
1685 Tcl_Obj *CONST objv[] /* Command arguments */
1687 int enable;
1688 sqlite3 *db;
1690 if( objc!=3 ){
1691 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1692 return TCL_ERROR;
1694 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1695 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1696 sqlite3_extended_result_codes(db, enable);
1697 return TCL_OK;
1701 ** Usage: sqlite3_libversion_number
1704 static int SQLITE_TCLAPI test_libversion_number(
1705 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1706 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1707 int objc, /* Number of arguments */
1708 Tcl_Obj *CONST objv[] /* Command arguments */
1710 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1711 return TCL_OK;
1715 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1718 static int SQLITE_TCLAPI test_table_column_metadata(
1719 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1720 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1721 int objc, /* Number of arguments */
1722 Tcl_Obj *CONST objv[] /* Command arguments */
1724 sqlite3 *db;
1725 const char *zDb;
1726 const char *zTbl;
1727 const char *zCol;
1728 int rc;
1729 Tcl_Obj *pRet;
1731 const char *zDatatype;
1732 const char *zCollseq;
1733 int notnull;
1734 int primarykey;
1735 int autoincrement;
1737 if( objc!=5 && objc!=4 ){
1738 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1739 return TCL_ERROR;
1741 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1742 zDb = Tcl_GetString(objv[2]);
1743 zTbl = Tcl_GetString(objv[3]);
1744 zCol = objc==5 ? Tcl_GetString(objv[4]) : 0;
1746 if( strlen(zDb)==0 ) zDb = 0;
1748 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1749 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1751 if( rc!=SQLITE_OK ){
1752 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1753 return TCL_ERROR;
1756 pRet = Tcl_NewObj();
1757 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1758 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1759 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1760 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1761 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1762 Tcl_SetObjResult(interp, pRet);
1764 return TCL_OK;
1767 #ifndef SQLITE_OMIT_INCRBLOB
1769 static int SQLITE_TCLAPI blobHandleFromObj(
1770 Tcl_Interp *interp,
1771 Tcl_Obj *pObj,
1772 sqlite3_blob **ppBlob
1774 char *z;
1775 int n;
1777 z = Tcl_GetStringFromObj(pObj, &n);
1778 if( n==0 ){
1779 *ppBlob = 0;
1780 }else{
1781 int notUsed;
1782 Tcl_Channel channel;
1783 ClientData instanceData;
1785 channel = Tcl_GetChannel(interp, z, &notUsed);
1786 if( !channel ) return TCL_ERROR;
1788 Tcl_Flush(channel);
1789 Tcl_Seek(channel, 0, SEEK_SET);
1791 instanceData = Tcl_GetChannelInstanceData(channel);
1792 *ppBlob = *((sqlite3_blob **)instanceData);
1795 return TCL_OK;
1798 static int SQLITE_TCLAPI test_blob_reopen(
1799 ClientData clientData, /* Not used */
1800 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1801 int objc, /* Number of arguments */
1802 Tcl_Obj *CONST objv[] /* Command arguments */
1804 Tcl_WideInt iRowid;
1805 sqlite3_blob *pBlob;
1806 int rc;
1808 if( objc!=3 ){
1809 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
1810 return TCL_ERROR;
1813 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
1814 if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
1816 rc = sqlite3_blob_reopen(pBlob, iRowid);
1817 if( rc!=SQLITE_OK ){
1818 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
1821 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1824 #endif
1827 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1829 ** This Tcl proc is used for testing the experimental
1830 ** sqlite3_create_collation_v2() interface.
1832 struct TestCollationX {
1833 Tcl_Interp *interp;
1834 Tcl_Obj *pCmp;
1835 Tcl_Obj *pDel;
1837 typedef struct TestCollationX TestCollationX;
1838 static void testCreateCollationDel(void *pCtx){
1839 TestCollationX *p = (TestCollationX *)pCtx;
1841 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1842 if( rc!=TCL_OK ){
1843 Tcl_BackgroundError(p->interp);
1846 Tcl_DecrRefCount(p->pCmp);
1847 Tcl_DecrRefCount(p->pDel);
1848 sqlite3_free((void *)p);
1850 static int testCreateCollationCmp(
1851 void *pCtx,
1852 int nLeft,
1853 const void *zLeft,
1854 int nRight,
1855 const void *zRight
1857 TestCollationX *p = (TestCollationX *)pCtx;
1858 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1859 int iRes = 0;
1861 Tcl_IncrRefCount(pScript);
1862 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1863 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1865 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1866 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1868 Tcl_BackgroundError(p->interp);
1870 Tcl_DecrRefCount(pScript);
1872 return iRes;
1874 static int SQLITE_TCLAPI test_create_collation_v2(
1875 ClientData clientData, /* Not used */
1876 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1877 int objc, /* Number of arguments */
1878 Tcl_Obj *CONST objv[] /* Command arguments */
1880 TestCollationX *p;
1881 sqlite3 *db;
1882 int rc;
1884 if( objc!=5 ){
1885 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1886 return TCL_ERROR;
1888 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1890 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1891 p->pCmp = objv[3];
1892 p->pDel = objv[4];
1893 p->interp = interp;
1894 Tcl_IncrRefCount(p->pCmp);
1895 Tcl_IncrRefCount(p->pDel);
1897 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1898 (void *)p, testCreateCollationCmp, testCreateCollationDel
1900 if( rc!=SQLITE_MISUSE ){
1901 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1902 "an invalid encoding", (char*)0);
1903 return TCL_ERROR;
1905 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1906 (void *)p, testCreateCollationCmp, testCreateCollationDel
1908 return TCL_OK;
1912 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
1914 ** Available switches are:
1916 ** -func SCRIPT
1917 ** -step SCRIPT
1918 ** -final SCRIPT
1919 ** -destroy SCRIPT
1921 typedef struct CreateFunctionV2 CreateFunctionV2;
1922 struct CreateFunctionV2 {
1923 Tcl_Interp *interp;
1924 Tcl_Obj *pFunc; /* Script for function invocation */
1925 Tcl_Obj *pStep; /* Script for agg. step invocation */
1926 Tcl_Obj *pFinal; /* Script for agg. finalization invocation */
1927 Tcl_Obj *pDestroy; /* Destructor script */
1929 static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1931 static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1933 static void cf2Final(sqlite3_context *ctx){
1935 static void cf2Destroy(void *pUser){
1936 CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
1938 if( p->interp && p->pDestroy ){
1939 int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
1940 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
1943 if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
1944 if( p->pStep ) Tcl_DecrRefCount(p->pStep);
1945 if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
1946 if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
1947 sqlite3_free(p);
1949 static int SQLITE_TCLAPI test_create_function_v2(
1950 ClientData clientData, /* Not used */
1951 Tcl_Interp *interp, /* The invoking TCL interpreter */
1952 int objc, /* Number of arguments */
1953 Tcl_Obj *CONST objv[] /* Command arguments */
1955 sqlite3 *db;
1956 const char *zFunc;
1957 int nArg;
1958 int enc;
1959 CreateFunctionV2 *p;
1960 int i;
1961 int rc;
1963 struct EncTable {
1964 const char *zEnc;
1965 int enc;
1966 } aEnc[] = {
1967 {"utf8", SQLITE_UTF8 },
1968 {"utf16", SQLITE_UTF16 },
1969 {"utf16le", SQLITE_UTF16LE },
1970 {"utf16be", SQLITE_UTF16BE },
1971 {"any", SQLITE_ANY },
1972 {"0", 0 },
1973 {0, 0 }
1976 if( objc<5 || (objc%2)==0 ){
1977 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
1978 return TCL_ERROR;
1981 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1982 zFunc = Tcl_GetString(objv[2]);
1983 if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
1984 if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
1985 "encoding", 0, &enc)
1987 return TCL_ERROR;
1989 enc = aEnc[enc].enc;
1991 p = sqlite3_malloc(sizeof(CreateFunctionV2));
1992 assert( p );
1993 memset(p, 0, sizeof(CreateFunctionV2));
1994 p->interp = interp;
1996 for(i=5; i<objc; i+=2){
1997 int iSwitch;
1998 const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
1999 if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
2000 sqlite3_free(p);
2001 return TCL_ERROR;
2004 switch( iSwitch ){
2005 case 0: p->pFunc = objv[i+1]; break;
2006 case 1: p->pStep = objv[i+1]; break;
2007 case 2: p->pFinal = objv[i+1]; break;
2008 case 3: p->pDestroy = objv[i+1]; break;
2011 if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
2012 if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
2013 if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
2014 if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
2016 if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
2017 if( p->pStep ) Tcl_IncrRefCount(p->pStep);
2018 if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
2019 if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
2021 rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
2022 (p->pFunc ? cf2Func : 0),
2023 (p->pStep ? cf2Step : 0),
2024 (p->pFinal ? cf2Final : 0),
2025 cf2Destroy
2027 if( rc!=SQLITE_OK ){
2028 Tcl_ResetResult(interp);
2029 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
2030 return TCL_ERROR;
2032 return TCL_OK;
2036 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
2038 static int SQLITE_TCLAPI test_load_extension(
2039 ClientData clientData, /* Not used */
2040 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2041 int objc, /* Number of arguments */
2042 Tcl_Obj *CONST objv[] /* Command arguments */
2044 Tcl_CmdInfo cmdInfo;
2045 sqlite3 *db;
2046 int rc;
2047 char *zDb;
2048 char *zFile;
2049 char *zProc = 0;
2050 char *zErr = 0;
2052 if( objc!=4 && objc!=3 ){
2053 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
2054 return TCL_ERROR;
2056 zDb = Tcl_GetString(objv[1]);
2057 zFile = Tcl_GetString(objv[2]);
2058 if( objc==4 ){
2059 zProc = Tcl_GetString(objv[3]);
2062 /* Extract the C database handle from the Tcl command name */
2063 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
2064 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
2065 return TCL_ERROR;
2067 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
2068 assert(db);
2070 /* Call the underlying C function. If an error occurs, set rc to
2071 ** TCL_ERROR and load any error string into the interpreter. If no
2072 ** error occurs, set rc to TCL_OK.
2074 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2075 rc = SQLITE_ERROR;
2076 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
2077 (void)zProc;
2078 (void)zFile;
2079 #else
2080 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
2081 #endif
2082 if( rc!=SQLITE_OK ){
2083 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
2084 rc = TCL_ERROR;
2085 }else{
2086 rc = TCL_OK;
2088 sqlite3_free(zErr);
2090 return rc;
2094 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
2096 static int SQLITE_TCLAPI test_enable_load(
2097 ClientData clientData, /* Not used */
2098 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2099 int objc, /* Number of arguments */
2100 Tcl_Obj *CONST objv[] /* Command arguments */
2102 Tcl_CmdInfo cmdInfo;
2103 sqlite3 *db;
2104 char *zDb;
2105 int onoff;
2107 if( objc!=3 ){
2108 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
2109 return TCL_ERROR;
2111 zDb = Tcl_GetString(objv[1]);
2113 /* Extract the C database handle from the Tcl command name */
2114 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
2115 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
2116 return TCL_ERROR;
2118 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
2119 assert(db);
2121 /* Get the onoff parameter */
2122 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
2123 return TCL_ERROR;
2126 #ifdef SQLITE_OMIT_LOAD_EXTENSION
2127 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
2128 return TCL_ERROR;
2129 #else
2130 sqlite3_enable_load_extension(db, onoff);
2131 return TCL_OK;
2132 #endif
2136 ** Usage: sqlite_abort
2138 ** Shutdown the process immediately. This is not a clean shutdown.
2139 ** This command is used to test the recoverability of a database in
2140 ** the event of a program crash.
2142 static int SQLITE_TCLAPI sqlite_abort(
2143 void *NotUsed,
2144 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2145 int argc, /* Number of arguments */
2146 char **argv /* Text of each argument */
2148 #if defined(_MSC_VER)
2149 /* We do this, otherwise the test will halt with a popup message
2150 * that we have to click away before the test will continue.
2152 _set_abort_behavior( 0, _CALL_REPORTFAULT );
2153 #endif
2154 exit(255);
2155 assert( interp==0 ); /* This will always fail */
2156 return TCL_OK;
2160 ** The following routine is a user-defined SQL function whose purpose
2161 ** is to test the sqlite_set_result() API.
2163 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2164 while( argc>=2 ){
2165 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
2166 if( zArg0 ){
2167 if( 0==sqlite3StrICmp(zArg0, "int") ){
2168 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
2169 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
2170 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
2171 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
2172 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
2173 SQLITE_TRANSIENT);
2174 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
2175 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
2176 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
2177 sqlite3_result_null(context);
2178 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
2179 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
2180 }else{
2181 goto error_out;
2183 }else{
2184 goto error_out;
2186 argc -= 2;
2187 argv += 2;
2189 return;
2191 error_out:
2192 sqlite3_result_error(context,"first argument should be one of: "
2193 "int int64 string double null value", -1);
2197 ** Usage: sqlite_register_test_function DB NAME
2199 ** Register the test SQL function on the database DB under the name NAME.
2201 static int SQLITE_TCLAPI test_register_func(
2202 void *NotUsed,
2203 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2204 int argc, /* Number of arguments */
2205 char **argv /* Text of each argument */
2207 sqlite3 *db;
2208 int rc;
2209 if( argc!=3 ){
2210 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2211 " DB FUNCTION-NAME", 0);
2212 return TCL_ERROR;
2214 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2215 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
2216 testFunc, 0, 0);
2217 if( rc!=0 ){
2218 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2219 return TCL_ERROR;
2221 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2222 return TCL_OK;
2226 ** Usage: sqlite3_finalize STMT
2228 ** Finalize a statement handle.
2230 static int SQLITE_TCLAPI test_finalize(
2231 void * clientData,
2232 Tcl_Interp *interp,
2233 int objc,
2234 Tcl_Obj *CONST objv[]
2236 sqlite3_stmt *pStmt;
2237 int rc;
2238 sqlite3 *db = 0;
2240 if( objc!=2 ){
2241 Tcl_AppendResult(interp, "wrong # args: should be \"",
2242 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2243 return TCL_ERROR;
2246 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2248 if( pStmt ){
2249 db = StmtToDb(pStmt);
2251 rc = sqlite3_finalize(pStmt);
2252 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2253 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2254 return TCL_OK;
2258 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2260 ** Get the value of a status counter from a statement.
2262 static int SQLITE_TCLAPI test_stmt_status(
2263 void * clientData,
2264 Tcl_Interp *interp,
2265 int objc,
2266 Tcl_Obj *CONST objv[]
2268 int iValue;
2269 int i, op = 0, resetFlag;
2270 const char *zOpName;
2271 sqlite3_stmt *pStmt;
2273 static const struct {
2274 const char *zName;
2275 int op;
2276 } aOp[] = {
2277 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
2278 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
2279 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
2280 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP },
2281 { "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE },
2282 { "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN },
2283 { "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED },
2285 if( objc!=4 ){
2286 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2287 return TCL_ERROR;
2289 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2290 zOpName = Tcl_GetString(objv[2]);
2291 for(i=0; i<ArraySize(aOp); i++){
2292 if( strcmp(aOp[i].zName, zOpName)==0 ){
2293 op = aOp[i].op;
2294 break;
2297 if( i>=ArraySize(aOp) ){
2298 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2300 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2301 iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2302 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2303 return TCL_OK;
2306 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2308 ** Usage: sqlite3_stmt_scanstatus ?-flags FLAGS? STMT IDX
2310 static int SQLITE_TCLAPI test_stmt_scanstatus(
2311 void * clientData,
2312 Tcl_Interp *interp,
2313 int objc,
2314 Tcl_Obj *CONST objv[]
2316 sqlite3_stmt *pStmt; /* First argument */
2317 int idx; /* Second argument */
2319 const char *zName;
2320 const char *zExplain;
2321 sqlite3_int64 nLoop;
2322 sqlite3_int64 nVisit;
2323 sqlite3_int64 nCycle;
2324 double rEst;
2325 int res;
2326 int flags = 0;
2327 int iSelectId = 0;
2328 int iParentId = 0;
2329 int bDebug = 0;
2331 if( objc==5 ){
2332 struct Flag {
2333 const char *zFlag;
2334 int flag;
2335 } aTbl[] = {
2336 {"complex", SQLITE_SCANSTAT_COMPLEX},
2337 {"debug", -1},
2338 {0, 0}
2341 Tcl_Obj **aFlag = 0;
2342 int nFlag = 0;
2343 int ii;
2345 if( Tcl_ListObjGetElements(interp, objv[2], &nFlag, &aFlag) ){
2346 return TCL_ERROR;
2348 for(ii=0; ii<nFlag; ii++){
2349 int iVal = 0;
2350 int res = Tcl_GetIndexFromObjStruct(
2351 interp, aFlag[ii], aTbl, sizeof(aTbl[0]), "flag", 0, &iVal
2353 if( res ) return TCL_ERROR;
2354 if( aTbl[iVal].flag==-1 ){
2355 bDebug = 1;
2356 }else{
2357 flags |= aTbl[iVal].flag;
2362 if( objc!=3 && objc!=5 ){
2363 Tcl_WrongNumArgs(interp, 1, objv, "-flags FLAGS STMT IDX");
2364 return TCL_ERROR;
2366 if( getStmtPointer(interp, Tcl_GetString(objv[objc-2]), &pStmt)
2367 || Tcl_GetIntFromObj(interp, objv[objc-1], &idx)
2369 return TCL_ERROR;
2372 if( bDebug && 0==(flags & SQLITE_SCANSTAT_COMPLEX) ){
2373 Tcl_SetObjResult(interp,
2374 Tcl_NewStringObj("may not specify debug without complex", -1)
2376 return TCL_ERROR;
2379 if( idx<0 ){
2380 Tcl_Obj *pRet = Tcl_NewObj();
2381 res = sqlite3_stmt_scanstatus_v2(
2382 pStmt, -1, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle
2384 sqlite3_stmt_scanstatus_v2(
2385 pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle);
2386 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1));
2387 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle));
2388 Tcl_SetObjResult(interp, pRet);
2389 }else{
2390 res = sqlite3_stmt_scanstatus_v2(
2391 pStmt, idx, SQLITE_SCANSTAT_NLOOP, flags, (void*)&nLoop
2393 if( res==0 ){
2394 Tcl_Obj *pRet = Tcl_NewObj();
2395 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
2396 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
2397 sqlite3_stmt_scanstatus_v2(
2398 pStmt, idx, SQLITE_SCANSTAT_NVISIT, flags, (void*)&nVisit);
2399 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
2400 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
2401 sqlite3_stmt_scanstatus_v2(
2402 pStmt, idx, SQLITE_SCANSTAT_EST, flags, (void*)&rEst);
2403 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
2404 Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
2405 sqlite3_stmt_scanstatus_v2(
2406 pStmt, idx, SQLITE_SCANSTAT_NAME, flags, (void*)&zName);
2407 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
2408 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
2409 sqlite3_stmt_scanstatus_v2(
2410 pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, flags, (void*)&zExplain);
2411 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
2412 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
2413 sqlite3_stmt_scanstatus_v2(
2414 pStmt, idx, SQLITE_SCANSTAT_SELECTID, flags, (void*)&iSelectId);
2415 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iSelectId", -1));
2416 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iSelectId));
2417 sqlite3_stmt_scanstatus_v2(
2418 pStmt, idx, SQLITE_SCANSTAT_PARENTID, flags, (void*)&iParentId);
2419 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iParentId", -1));
2420 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iParentId));
2421 sqlite3_stmt_scanstatus_v2(
2422 pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle);
2423 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1));
2424 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle));
2426 if( bDebug ){
2427 int ii;
2428 ScanStatus *pScan = &((Vdbe*)pStmt)->aScan[idx];
2429 Tcl_Obj *pRange = Tcl_NewObj();
2430 Tcl_Obj *pCsr = Tcl_NewObj();
2432 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_loop", -1));
2433 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrLoop));
2434 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_visit", -1));
2435 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrVisit));
2436 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_explain",-1));
2437 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrExplain));
2438 for(ii=0; ii<ArraySize(pScan->aAddrRange)/2; ii++){
2439 int iStart = pScan->aAddrRange[ii*2];
2440 int iEnd = pScan->aAddrRange[ii*2+1];
2441 if( iStart>0 ){
2442 Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iStart));
2443 Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iEnd));
2444 }else if( iStart<0 ){
2445 Tcl_ListObjAppendElement(0, pCsr, Tcl_NewIntObj(iEnd));
2449 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_range", -1));
2450 Tcl_ListObjAppendElement(0, pRet, pRange);
2451 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_csr", -1));
2452 Tcl_ListObjAppendElement(0, pRet, pCsr);
2455 Tcl_SetObjResult(interp, pRet);
2456 }else{
2457 Tcl_ResetResult(interp);
2460 return TCL_OK;
2464 ** Usage: sqlite3_stmt_scanstatus_reset STMT
2466 static int SQLITE_TCLAPI test_stmt_scanstatus_reset(
2467 void * clientData,
2468 Tcl_Interp *interp,
2469 int objc,
2470 Tcl_Obj *CONST objv[]
2472 sqlite3_stmt *pStmt; /* First argument */
2473 if( objc!=2 ){
2474 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2475 return TCL_ERROR;
2477 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2478 sqlite3_stmt_scanstatus_reset(pStmt);
2479 return TCL_OK;
2481 #endif
2483 #ifdef SQLITE_ENABLE_SQLLOG
2485 ** Usage: sqlite3_config_sqllog
2487 ** Zero the SQLITE_CONFIG_SQLLOG configuration
2489 static int SQLITE_TCLAPI test_config_sqllog(
2490 void * clientData,
2491 Tcl_Interp *interp,
2492 int objc,
2493 Tcl_Obj *CONST objv[]
2495 if( objc!=1 ){
2496 Tcl_WrongNumArgs(interp, 1, objv, "");
2497 return TCL_ERROR;
2499 sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
2500 return TCL_OK;
2502 #endif
2505 ** Usage: sqlite3_config_sorterref
2507 ** Set the SQLITE_CONFIG_SORTERREF_SIZE configuration option
2509 static int SQLITE_TCLAPI test_config_sorterref(
2510 void * clientData,
2511 Tcl_Interp *interp,
2512 int objc,
2513 Tcl_Obj *CONST objv[]
2515 int iVal;
2516 if( objc!=2 ){
2517 Tcl_WrongNumArgs(interp, 1, objv, "NBYTE");
2518 return TCL_ERROR;
2520 if( Tcl_GetIntFromObj(interp, objv[1], &iVal) ) return TCL_ERROR;
2521 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, iVal);
2522 return TCL_OK;
2526 ** Usage: vfs_current_time_int64
2528 ** Return the value returned by the default VFS's xCurrentTimeInt64 method.
2530 static int SQLITE_TCLAPI vfsCurrentTimeInt64(
2531 void * clientData,
2532 Tcl_Interp *interp,
2533 int objc,
2534 Tcl_Obj *CONST objv[]
2536 i64 t;
2537 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
2538 if( objc!=1 ){
2539 Tcl_WrongNumArgs(interp, 1, objv, "");
2540 return TCL_ERROR;
2542 pVfs->xCurrentTimeInt64(pVfs, &t);
2543 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
2544 return TCL_OK;
2547 #ifndef SQLITE_OMIT_VIRTUALTABLE
2549 ** Usage: create_null_module DB NAME
2551 static int SQLITE_TCLAPI test_create_null_module(
2552 void * clientData,
2553 Tcl_Interp *interp,
2554 int objc,
2555 Tcl_Obj *CONST objv[]
2557 sqlite3 *db;
2558 char *zName;
2560 if( objc!=3 ){
2561 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2562 return TCL_ERROR;
2564 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2565 zName = Tcl_GetString(objv[2]);
2567 sqlite3_create_module(db, zName, 0, 0);
2568 return TCL_OK;
2570 #endif /* SQLITE_OMIT_VIRTUALTABLE */
2572 #ifdef SQLITE_ENABLE_SNAPSHOT
2574 ** Usage: sqlite3_snapshot_get DB DBNAME
2576 static int SQLITE_TCLAPI test_snapshot_get(
2577 void * clientData,
2578 Tcl_Interp *interp,
2579 int objc,
2580 Tcl_Obj *CONST objv[]
2582 int rc;
2583 sqlite3 *db;
2584 char *zName;
2585 sqlite3_snapshot *pSnapshot = 0;
2587 if( objc!=3 ){
2588 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2589 return TCL_ERROR;
2591 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2592 zName = Tcl_GetString(objv[2]);
2594 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2595 if( rc!=SQLITE_OK ){
2596 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2597 return TCL_ERROR;
2598 }else{
2599 char zBuf[100];
2600 if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
2601 Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
2603 return TCL_OK;
2605 #endif /* SQLITE_ENABLE_SNAPSHOT */
2607 #ifdef SQLITE_ENABLE_SNAPSHOT
2609 ** Usage: sqlite3_snapshot_recover DB DBNAME
2611 static int SQLITE_TCLAPI test_snapshot_recover(
2612 void * clientData,
2613 Tcl_Interp *interp,
2614 int objc,
2615 Tcl_Obj *CONST objv[]
2617 int rc;
2618 sqlite3 *db;
2619 char *zName;
2621 if( objc!=3 ){
2622 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2623 return TCL_ERROR;
2625 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2626 zName = Tcl_GetString(objv[2]);
2628 rc = sqlite3_snapshot_recover(db, zName);
2629 if( rc!=SQLITE_OK ){
2630 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2631 return TCL_ERROR;
2632 }else{
2633 Tcl_ResetResult(interp);
2635 return TCL_OK;
2637 #endif /* SQLITE_ENABLE_SNAPSHOT */
2639 #ifdef SQLITE_ENABLE_SNAPSHOT
2641 ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
2643 static int SQLITE_TCLAPI test_snapshot_open(
2644 void * clientData,
2645 Tcl_Interp *interp,
2646 int objc,
2647 Tcl_Obj *CONST objv[]
2649 int rc;
2650 sqlite3 *db;
2651 char *zName;
2652 sqlite3_snapshot *pSnapshot;
2654 if( objc!=4 ){
2655 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2656 return TCL_ERROR;
2658 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2659 zName = Tcl_GetString(objv[2]);
2660 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
2662 rc = sqlite3_snapshot_open(db, zName, pSnapshot);
2663 if( rc!=SQLITE_OK ){
2664 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2665 return TCL_ERROR;
2666 }else{
2667 Tcl_ResetResult(interp);
2669 return TCL_OK;
2671 #endif /* SQLITE_ENABLE_SNAPSHOT */
2673 #ifdef SQLITE_ENABLE_SNAPSHOT
2675 ** Usage: sqlite3_snapshot_free SNAPSHOT
2677 static int SQLITE_TCLAPI test_snapshot_free(
2678 void * clientData,
2679 Tcl_Interp *interp,
2680 int objc,
2681 Tcl_Obj *CONST objv[]
2683 sqlite3_snapshot *pSnapshot;
2684 if( objc!=2 ){
2685 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
2686 return TCL_ERROR;
2688 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2689 sqlite3_snapshot_free(pSnapshot);
2690 return TCL_OK;
2692 #endif /* SQLITE_ENABLE_SNAPSHOT */
2694 #ifdef SQLITE_ENABLE_SNAPSHOT
2696 ** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2
2698 static int SQLITE_TCLAPI test_snapshot_cmp(
2699 void * clientData,
2700 Tcl_Interp *interp,
2701 int objc,
2702 Tcl_Obj *CONST objv[]
2704 int res;
2705 sqlite3_snapshot *p1;
2706 sqlite3_snapshot *p2;
2707 if( objc!=3 ){
2708 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2709 return TCL_ERROR;
2711 p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2712 p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2]));
2713 res = sqlite3_snapshot_cmp(p1, p2);
2714 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2715 return TCL_OK;
2717 #endif /* SQLITE_ENABLE_SNAPSHOT */
2719 #ifdef SQLITE_ENABLE_SNAPSHOT
2721 ** Usage: sqlite3_snapshot_get_blob DB DBNAME
2723 static int SQLITE_TCLAPI test_snapshot_get_blob(
2724 void * clientData,
2725 Tcl_Interp *interp,
2726 int objc,
2727 Tcl_Obj *CONST objv[]
2729 int rc;
2730 sqlite3 *db;
2731 char *zName;
2732 sqlite3_snapshot *pSnapshot = 0;
2734 if( objc!=3 ){
2735 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2736 return TCL_ERROR;
2738 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2739 zName = Tcl_GetString(objv[2]);
2741 rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2742 if( rc!=SQLITE_OK ){
2743 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2744 return TCL_ERROR;
2745 }else{
2746 Tcl_SetObjResult(interp,
2747 Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot))
2749 sqlite3_snapshot_free(pSnapshot);
2751 return TCL_OK;
2753 #endif /* SQLITE_ENABLE_SNAPSHOT */
2755 #ifdef SQLITE_ENABLE_SNAPSHOT
2757 ** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT
2759 static int SQLITE_TCLAPI test_snapshot_open_blob(
2760 void * clientData,
2761 Tcl_Interp *interp,
2762 int objc,
2763 Tcl_Obj *CONST objv[]
2765 int rc;
2766 sqlite3 *db;
2767 char *zName;
2768 unsigned char *pBlob;
2769 int nBlob;
2771 if( objc!=4 ){
2772 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2773 return TCL_ERROR;
2775 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2776 zName = Tcl_GetString(objv[2]);
2777 pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob);
2778 if( nBlob!=sizeof(sqlite3_snapshot) ){
2779 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2780 return TCL_ERROR;
2782 rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob);
2783 if( rc!=SQLITE_OK ){
2784 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2785 return TCL_ERROR;
2787 return TCL_OK;
2789 #endif /* SQLITE_ENABLE_SNAPSHOT */
2791 #ifdef SQLITE_ENABLE_SNAPSHOT
2793 ** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2
2795 static int SQLITE_TCLAPI test_snapshot_cmp_blob(
2796 void * clientData,
2797 Tcl_Interp *interp,
2798 int objc,
2799 Tcl_Obj *CONST objv[]
2801 int res;
2802 unsigned char *p1;
2803 unsigned char *p2;
2804 int n1;
2805 int n2;
2807 if( objc!=3 ){
2808 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2809 return TCL_ERROR;
2812 p1 = Tcl_GetByteArrayFromObj(objv[1], &n1);
2813 p2 = Tcl_GetByteArrayFromObj(objv[2], &n2);
2815 if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){
2816 Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
2817 return TCL_ERROR;
2820 res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2);
2821 Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2822 return TCL_OK;
2824 #endif /* SQLITE_ENABLE_SNAPSHOT */
2827 ** Usage: sqlite3_delete_database FILENAME
2829 int sqlite3_delete_database(const char*); /* in test_delete.c */
2830 static int SQLITE_TCLAPI test_delete_database(
2831 void * clientData,
2832 Tcl_Interp *interp,
2833 int objc,
2834 Tcl_Obj *CONST objv[]
2836 int rc;
2837 const char *zFile;
2838 if( objc!=2 ){
2839 Tcl_WrongNumArgs(interp, 1, objv, "FILE");
2840 return TCL_ERROR;
2842 zFile = (const char*)Tcl_GetString(objv[1]);
2843 rc = sqlite3_delete_database(zFile);
2845 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2846 return TCL_OK;
2850 ** Usage: atomic_batch_write PATH
2852 static int SQLITE_TCLAPI test_atomic_batch_write(
2853 void * clientData,
2854 Tcl_Interp *interp,
2855 int objc,
2856 Tcl_Obj *CONST objv[]
2858 char *zFile = 0; /* Path to file to test */
2859 sqlite3 *db = 0; /* Database handle */
2860 sqlite3_file *pFd = 0; /* SQLite fd open on zFile */
2861 int bRes = 0; /* Integer result of this command */
2862 int dc = 0; /* Device-characteristics mask */
2863 int rc; /* sqlite3_open() return code */
2865 if( objc!=2 ){
2866 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
2867 return TCL_ERROR;
2869 zFile = Tcl_GetString(objv[1]);
2871 rc = sqlite3_open(zFile, &db);
2872 if( rc!=SQLITE_OK ){
2873 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
2874 sqlite3_close(db);
2875 return TCL_ERROR;
2878 rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
2879 dc = pFd->pMethods->xDeviceCharacteristics(pFd);
2880 if( dc & SQLITE_IOCAP_BATCH_ATOMIC ){
2881 bRes = 1;
2884 Tcl_SetObjResult(interp, Tcl_NewIntObj(bRes));
2885 sqlite3_close(db);
2886 return TCL_OK;
2890 ** Usage: sqlite3_next_stmt DB STMT
2892 ** Return the next statement in sequence after STMT.
2894 static int SQLITE_TCLAPI test_next_stmt(
2895 void * clientData,
2896 Tcl_Interp *interp,
2897 int objc,
2898 Tcl_Obj *CONST objv[]
2900 sqlite3_stmt *pStmt;
2901 sqlite3 *db = 0;
2902 char zBuf[50];
2904 if( objc!=3 ){
2905 Tcl_AppendResult(interp, "wrong # args: should be \"",
2906 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2907 return TCL_ERROR;
2910 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2911 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2912 pStmt = sqlite3_next_stmt(db, pStmt);
2913 if( pStmt ){
2914 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2915 Tcl_AppendResult(interp, zBuf, 0);
2917 return TCL_OK;
2921 ** Usage: sqlite3_stmt_readonly STMT
2923 ** Return true if STMT is a NULL pointer or a pointer to a statement
2924 ** that is guaranteed to leave the database unmodified.
2926 static int SQLITE_TCLAPI test_stmt_readonly(
2927 void * clientData,
2928 Tcl_Interp *interp,
2929 int objc,
2930 Tcl_Obj *CONST objv[]
2932 sqlite3_stmt *pStmt;
2933 int rc;
2935 if( objc!=2 ){
2936 Tcl_AppendResult(interp, "wrong # args: should be \"",
2937 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2938 return TCL_ERROR;
2941 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2942 rc = sqlite3_stmt_readonly(pStmt);
2943 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2944 return TCL_OK;
2948 ** Usage: sqlite3_stmt_isexplain STMT
2950 ** Return 1, 2, or 0 respectively if STMT is an EXPLAIN statement, an
2951 ** EXPLAIN QUERY PLAN statement or an ordinary statement or NULL pointer.
2953 static int SQLITE_TCLAPI test_stmt_isexplain(
2954 void * clientData,
2955 Tcl_Interp *interp,
2956 int objc,
2957 Tcl_Obj *CONST objv[]
2959 sqlite3_stmt *pStmt;
2960 int rc;
2962 if( objc!=2 ){
2963 Tcl_AppendResult(interp, "wrong # args: should be \"",
2964 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2965 return TCL_ERROR;
2968 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2969 rc = sqlite3_stmt_isexplain(pStmt);
2970 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
2971 return TCL_OK;
2975 ** Usage: sqlite3_stmt_explain STMT INT
2977 ** Set the explain to normal (0), EXPLAIN (1) or EXPLAIN QUERY PLAN (2).
2979 static int SQLITE_TCLAPI test_stmt_explain(
2980 void * clientData,
2981 Tcl_Interp *interp,
2982 int objc,
2983 Tcl_Obj *CONST objv[]
2985 sqlite3_stmt *pStmt;
2986 int eMode = 0;
2987 int rc;
2989 if( objc!=3 ){
2990 Tcl_AppendResult(interp, "wrong # args: should be \"",
2991 Tcl_GetStringFromObj(objv[0], 0), " STMT INT", 0);
2992 return TCL_ERROR;
2995 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2996 if( Tcl_GetIntFromObj(interp, objv[2], &eMode) ) return TCL_ERROR;
2997 rc = sqlite3_stmt_explain(pStmt, eMode);
2998 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
2999 return TCL_OK;
3003 ** Usage: sqlite3_stmt_busy STMT
3005 ** Return true if STMT is a non-NULL pointer to a statement
3006 ** that has been stepped but not to completion.
3008 static int SQLITE_TCLAPI test_stmt_busy(
3009 void * clientData,
3010 Tcl_Interp *interp,
3011 int objc,
3012 Tcl_Obj *CONST objv[]
3014 sqlite3_stmt *pStmt;
3015 int rc;
3017 if( objc!=2 ){
3018 Tcl_AppendResult(interp, "wrong # args: should be \"",
3019 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
3020 return TCL_ERROR;
3023 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3024 rc = sqlite3_stmt_busy(pStmt);
3025 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
3026 return TCL_OK;
3030 ** Usage: uses_stmt_journal STMT
3032 ** Return true if STMT uses a statement journal.
3034 static int SQLITE_TCLAPI uses_stmt_journal(
3035 void * clientData,
3036 Tcl_Interp *interp,
3037 int objc,
3038 Tcl_Obj *CONST objv[]
3040 sqlite3_stmt *pStmt;
3042 if( objc!=2 ){
3043 Tcl_AppendResult(interp, "wrong # args: should be \"",
3044 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
3045 return TCL_ERROR;
3048 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3049 sqlite3_stmt_readonly(pStmt);
3050 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
3051 return TCL_OK;
3056 ** Usage: sqlite3_reset STMT
3058 ** Reset a statement handle.
3060 static int SQLITE_TCLAPI test_reset(
3061 void * clientData,
3062 Tcl_Interp *interp,
3063 int objc,
3064 Tcl_Obj *CONST objv[]
3066 sqlite3_stmt *pStmt;
3067 int rc;
3069 if( objc!=2 ){
3070 Tcl_AppendResult(interp, "wrong # args: should be \"",
3071 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
3072 return TCL_ERROR;
3075 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3077 rc = sqlite3_reset(pStmt);
3078 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
3079 return TCL_ERROR;
3081 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3083 if( rc ){
3084 return TCL_ERROR;
3087 return TCL_OK;
3091 ** Usage: sqlite3_expired STMT
3093 ** Return TRUE if a recompilation of the statement is recommended.
3095 static int SQLITE_TCLAPI test_expired(
3096 void * clientData,
3097 Tcl_Interp *interp,
3098 int objc,
3099 Tcl_Obj *CONST objv[]
3101 #ifndef SQLITE_OMIT_DEPRECATED
3102 sqlite3_stmt *pStmt;
3103 if( objc!=2 ){
3104 Tcl_AppendResult(interp, "wrong # args: should be \"",
3105 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
3106 return TCL_ERROR;
3108 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3109 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
3110 #endif
3111 return TCL_OK;
3115 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
3117 ** Transfer all bindings from FROMSTMT over to TOSTMT
3119 static int SQLITE_TCLAPI test_transfer_bind(
3120 void * clientData,
3121 Tcl_Interp *interp,
3122 int objc,
3123 Tcl_Obj *CONST objv[]
3125 #ifndef SQLITE_OMIT_DEPRECATED
3126 sqlite3_stmt *pStmt1, *pStmt2;
3127 if( objc!=3 ){
3128 Tcl_AppendResult(interp, "wrong # args: should be \"",
3129 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
3130 return TCL_ERROR;
3132 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
3133 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
3134 Tcl_SetObjResult(interp,
3135 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
3136 #endif
3137 return TCL_OK;
3141 ** Usage: sqlite3_changes DB
3143 ** Return the number of changes made to the database by the last SQL
3144 ** execution.
3146 static int SQLITE_TCLAPI test_changes(
3147 void * clientData,
3148 Tcl_Interp *interp,
3149 int objc,
3150 Tcl_Obj *CONST objv[]
3152 sqlite3 *db;
3153 if( objc!=2 ){
3154 Tcl_AppendResult(interp, "wrong # args: should be \"",
3155 Tcl_GetString(objv[0]), " DB", 0);
3156 return TCL_ERROR;
3158 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3159 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
3160 return TCL_OK;
3164 ** This is the "static_bind_value" that variables are bound to when
3165 ** the FLAG option of sqlite3_bind is "static"
3167 static char *sqlite_static_bind_value = 0;
3168 static int sqlite_static_bind_nbyte = 0;
3171 ** Usage: sqlite3_bind VM IDX VALUE FLAGS
3173 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
3174 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
3175 ** ignored and the value is set to NULL. If FLAGS=="static" then
3176 ** the value is set to the value of a static variable named
3177 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
3178 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
3179 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
3181 static int SQLITE_TCLAPI test_bind(
3182 void *NotUsed,
3183 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3184 int argc, /* Number of arguments */
3185 char **argv /* Text of each argument */
3187 sqlite3_stmt *pStmt;
3188 int rc;
3189 int idx;
3190 if( argc!=5 ){
3191 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3192 " VM IDX VALUE (null|static|normal)\"", 0);
3193 return TCL_ERROR;
3195 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
3196 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
3197 if( strcmp(argv[4],"null")==0 ){
3198 rc = sqlite3_bind_null(pStmt, idx);
3199 }else if( strcmp(argv[4],"static")==0 ){
3200 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
3201 }else if( strcmp(argv[4],"static-nbytes")==0 ){
3202 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
3203 sqlite_static_bind_nbyte, 0);
3204 }else if( strcmp(argv[4],"normal")==0 ){
3205 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
3206 }else if( strcmp(argv[4],"blob10")==0 ){
3207 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
3208 }else{
3209 Tcl_AppendResult(interp, "4th argument should be "
3210 "\"null\" or \"static\" or \"normal\"", 0);
3211 return TCL_ERROR;
3213 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3214 if( rc ){
3215 char zBuf[50];
3216 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3217 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
3218 return TCL_ERROR;
3220 return TCL_OK;
3223 #ifndef SQLITE_OMIT_UTF16
3225 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
3227 ** This function is used to test that SQLite selects the correct collation
3228 ** sequence callback when multiple versions (for different text encodings)
3229 ** are available.
3231 ** Calling this routine registers the collation sequence "test_collate"
3232 ** with database handle <db>. The second argument must be a list of three
3233 ** boolean values. If the first is true, then a version of test_collate is
3234 ** registered for UTF-8, if the second is true, a version is registered for
3235 ** UTF-16le, if the third is true, a UTF-16be version is available.
3236 ** Previous versions of test_collate are deleted.
3238 ** The collation sequence test_collate is implemented by calling the
3239 ** following TCL script:
3241 ** "test_collate <enc> <lhs> <rhs>"
3243 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
3244 ** The <enc> parameter is the encoding of the collation function that
3245 ** SQLite selected to call. The TCL test script implements the
3246 ** "test_collate" proc.
3248 ** Note that this will only work with one interpreter at a time, as the
3249 ** interp pointer to use when evaluating the TCL script is stored in
3250 ** pTestCollateInterp.
3252 static Tcl_Interp* pTestCollateInterp;
3253 static int test_collate_func(
3254 void *pCtx,
3255 int nA, const void *zA,
3256 int nB, const void *zB
3258 Tcl_Interp *i = pTestCollateInterp;
3259 int encin = SQLITE_PTR_TO_INT(pCtx);
3260 int res;
3261 int n;
3263 sqlite3_value *pVal;
3264 Tcl_Obj *pX;
3266 pX = Tcl_NewStringObj("test_collate", -1);
3267 Tcl_IncrRefCount(pX);
3269 switch( encin ){
3270 case SQLITE_UTF8:
3271 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
3272 break;
3273 case SQLITE_UTF16LE:
3274 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
3275 break;
3276 case SQLITE_UTF16BE:
3277 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
3278 break;
3279 default:
3280 assert(0);
3283 sqlite3BeginBenignMalloc();
3284 pVal = sqlite3ValueNew(0);
3285 if( pVal ){
3286 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
3287 n = sqlite3_value_bytes(pVal);
3288 Tcl_ListObjAppendElement(i,pX,
3289 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
3290 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
3291 n = sqlite3_value_bytes(pVal);
3292 Tcl_ListObjAppendElement(i,pX,
3293 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
3294 sqlite3ValueFree(pVal);
3296 sqlite3EndBenignMalloc();
3298 Tcl_EvalObjEx(i, pX, 0);
3299 Tcl_DecrRefCount(pX);
3300 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
3301 return res;
3303 static int SQLITE_TCLAPI test_collate(
3304 void * clientData,
3305 Tcl_Interp *interp,
3306 int objc,
3307 Tcl_Obj *CONST objv[]
3309 sqlite3 *db;
3310 int val;
3311 sqlite3_value *pVal;
3312 int rc;
3314 if( objc!=5 ) goto bad_args;
3315 pTestCollateInterp = interp;
3316 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3318 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3319 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
3320 (void *)SQLITE_UTF8, val?test_collate_func:0);
3321 if( rc==SQLITE_OK ){
3322 const void *zUtf16;
3323 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3324 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
3325 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
3326 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3328 #if 0
3329 if( sqlite3_iMallocFail>0 ){
3330 sqlite3_iMallocFail++;
3332 #endif
3333 sqlite3_mutex_enter(db->mutex);
3334 pVal = sqlite3ValueNew(db);
3335 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
3336 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
3337 if( db->mallocFailed ){
3338 rc = SQLITE_NOMEM;
3339 }else{
3340 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
3341 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
3343 sqlite3ValueFree(pVal);
3344 sqlite3_mutex_leave(db->mutex);
3346 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3348 if( rc!=SQLITE_OK ){
3349 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3350 return TCL_ERROR;
3352 return TCL_OK;
3354 bad_args:
3355 Tcl_AppendResult(interp, "wrong # args: should be \"",
3356 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3357 return TCL_ERROR;
3361 ** Usage: add_test_utf16bin_collate <db ptr>
3363 ** Add a utf-16 collation sequence named "utf16bin" to the database
3364 ** handle. This collation sequence compares arguments in the same way as the
3365 ** built-in collation "binary".
3367 static int test_utf16bin_collate_func(
3368 void *pCtx,
3369 int nA, const void *zA,
3370 int nB, const void *zB
3372 int nCmp = (nA>nB ? nB : nA);
3373 int res = memcmp(zA, zB, nCmp);
3374 if( res==0 ) res = nA - nB;
3375 return res;
3377 static int SQLITE_TCLAPI test_utf16bin_collate(
3378 void * clientData,
3379 Tcl_Interp *interp,
3380 int objc,
3381 Tcl_Obj *CONST objv[]
3383 sqlite3 *db;
3384 int rc;
3386 if( objc!=2 ) goto bad_args;
3387 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3389 rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0,
3390 test_utf16bin_collate_func
3392 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3393 return TCL_OK;
3395 bad_args:
3396 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3397 return TCL_ERROR;
3401 ** When the collation needed callback is invoked, record the name of
3402 ** the requested collating function here. The recorded name is linked
3403 ** to a TCL variable and used to make sure that the requested collation
3404 ** name is correct.
3406 static char zNeededCollation[200];
3407 static char *pzNeededCollation = zNeededCollation;
3411 ** Called when a collating sequence is needed. Registered using
3412 ** sqlite3_collation_needed16().
3414 static void test_collate_needed_cb(
3415 void *pCtx,
3416 sqlite3 *db,
3417 int eTextRep,
3418 const void *pName
3420 int enc = ENC(db);
3421 int i;
3422 char *z;
3423 for(z = (char*)pName, i=0; *z || z[1]; z++){
3424 if( *z ) zNeededCollation[i++] = *z;
3426 zNeededCollation[i] = 0;
3427 sqlite3_create_collation(
3428 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
3432 ** Usage: add_test_collate_needed DB
3434 static int SQLITE_TCLAPI test_collate_needed(
3435 void * clientData,
3436 Tcl_Interp *interp,
3437 int objc,
3438 Tcl_Obj *CONST objv[]
3440 sqlite3 *db;
3441 int rc;
3443 if( objc!=2 ) goto bad_args;
3444 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3445 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
3446 zNeededCollation[0] = 0;
3447 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3448 return TCL_OK;
3450 bad_args:
3451 Tcl_WrongNumArgs(interp, 1, objv, "DB");
3452 return TCL_ERROR;
3456 ** tclcmd: add_alignment_test_collations DB
3458 ** Add two new collating sequences to the database DB
3460 ** utf16_aligned
3461 ** utf16_unaligned
3463 ** Both collating sequences use the same sort order as BINARY.
3464 ** The only difference is that the utf16_aligned collating
3465 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
3466 ** Both collating functions increment the unaligned utf16 counter
3467 ** whenever they see a string that begins on an odd byte boundary.
3469 static int unaligned_string_counter = 0;
3470 static int alignmentCollFunc(
3471 void *NotUsed,
3472 int nKey1, const void *pKey1,
3473 int nKey2, const void *pKey2
3475 int rc, n;
3476 n = nKey1<nKey2 ? nKey1 : nKey2;
3477 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
3478 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
3479 rc = memcmp(pKey1, pKey2, n);
3480 if( rc==0 ){
3481 rc = nKey1 - nKey2;
3483 return rc;
3485 static int SQLITE_TCLAPI add_alignment_test_collations(
3486 void * clientData,
3487 Tcl_Interp *interp,
3488 int objc,
3489 Tcl_Obj *CONST objv[]
3491 sqlite3 *db;
3492 if( objc>=2 ){
3493 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3494 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
3495 0, alignmentCollFunc);
3496 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
3497 0, alignmentCollFunc);
3499 return SQLITE_OK;
3501 #endif /* !defined(SQLITE_OMIT_UTF16) */
3504 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
3506 ** This function is used to test that SQLite selects the correct user
3507 ** function callback when multiple versions (for different text encodings)
3508 ** are available.
3510 ** Calling this routine registers up to three versions of the user function
3511 ** "test_function" with database handle <db>. If the second argument is
3512 ** true, then a version of test_function is registered for UTF-8, if the
3513 ** third is true, a version is registered for UTF-16le, if the fourth is
3514 ** true, a UTF-16be version is available. Previous versions of
3515 ** test_function are deleted.
3517 ** The user function is implemented by calling the following TCL script:
3519 ** "test_function <enc> <arg>"
3521 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
3522 ** single argument passed to the SQL function. The value returned by
3523 ** the TCL script is used as the return value of the SQL function. It
3524 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
3525 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
3526 ** prefers UTF-16BE.
3528 #ifndef SQLITE_OMIT_UTF16
3529 static void test_function_utf8(
3530 sqlite3_context *pCtx,
3531 int nArg,
3532 sqlite3_value **argv
3534 Tcl_Interp *interp;
3535 Tcl_Obj *pX;
3536 sqlite3_value *pVal;
3537 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3538 pX = Tcl_NewStringObj("test_function", -1);
3539 Tcl_IncrRefCount(pX);
3540 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
3541 Tcl_ListObjAppendElement(interp, pX,
3542 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3543 Tcl_EvalObjEx(interp, pX, 0);
3544 Tcl_DecrRefCount(pX);
3545 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
3546 pVal = sqlite3ValueNew(0);
3547 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3548 SQLITE_UTF8, SQLITE_STATIC);
3549 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
3550 -1, SQLITE_TRANSIENT);
3551 sqlite3ValueFree(pVal);
3553 static void test_function_utf16le(
3554 sqlite3_context *pCtx,
3555 int nArg,
3556 sqlite3_value **argv
3558 Tcl_Interp *interp;
3559 Tcl_Obj *pX;
3560 sqlite3_value *pVal;
3561 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3562 pX = Tcl_NewStringObj("test_function", -1);
3563 Tcl_IncrRefCount(pX);
3564 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
3565 Tcl_ListObjAppendElement(interp, pX,
3566 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3567 Tcl_EvalObjEx(interp, pX, 0);
3568 Tcl_DecrRefCount(pX);
3569 pVal = sqlite3ValueNew(0);
3570 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3571 SQLITE_UTF8, SQLITE_STATIC);
3572 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
3573 sqlite3ValueFree(pVal);
3575 static void test_function_utf16be(
3576 sqlite3_context *pCtx,
3577 int nArg,
3578 sqlite3_value **argv
3580 Tcl_Interp *interp;
3581 Tcl_Obj *pX;
3582 sqlite3_value *pVal;
3583 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3584 pX = Tcl_NewStringObj("test_function", -1);
3585 Tcl_IncrRefCount(pX);
3586 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
3587 Tcl_ListObjAppendElement(interp, pX,
3588 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3589 Tcl_EvalObjEx(interp, pX, 0);
3590 Tcl_DecrRefCount(pX);
3591 pVal = sqlite3ValueNew(0);
3592 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3593 SQLITE_UTF8, SQLITE_STATIC);
3594 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
3595 -1, SQLITE_TRANSIENT);
3596 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
3597 -1, SQLITE_TRANSIENT);
3598 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
3599 -1, SQLITE_TRANSIENT);
3600 sqlite3ValueFree(pVal);
3602 #endif /* SQLITE_OMIT_UTF16 */
3603 static int SQLITE_TCLAPI test_function(
3604 void * clientData,
3605 Tcl_Interp *interp,
3606 int objc,
3607 Tcl_Obj *CONST objv[]
3609 #ifndef SQLITE_OMIT_UTF16
3610 sqlite3 *db;
3611 int val;
3613 if( objc!=5 ) goto bad_args;
3614 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3616 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3617 if( val ){
3618 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
3619 interp, test_function_utf8, 0, 0);
3621 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3622 if( val ){
3623 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
3624 interp, test_function_utf16le, 0, 0);
3626 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3627 if( val ){
3628 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
3629 interp, test_function_utf16be, 0, 0);
3632 return TCL_OK;
3633 bad_args:
3634 Tcl_AppendResult(interp, "wrong # args: should be \"",
3635 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3636 #endif /* SQLITE_OMIT_UTF16 */
3637 return TCL_ERROR;
3641 ** Usage: sqlite3_test_errstr <err code>
3643 ** Test that the English language string equivalents for sqlite error codes
3644 ** are sane. The parameter is an integer representing an sqlite error code.
3645 ** The result is a list of two elements, the string representation of the
3646 ** error code and the English language explanation.
3648 static int SQLITE_TCLAPI test_errstr(
3649 void * clientData,
3650 Tcl_Interp *interp,
3651 int objc,
3652 Tcl_Obj *CONST objv[]
3654 char *zCode;
3655 int i;
3656 if( objc!=1 ){
3657 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
3660 zCode = Tcl_GetString(objv[1]);
3661 for(i=0; i<200; i++){
3662 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
3664 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
3665 return TCL_OK;
3669 ** Usage: breakpoint
3671 ** This routine exists for one purpose - to provide a place to put a
3672 ** breakpoint with GDB that can be triggered using TCL code. The use
3673 ** for this is when a particular test fails on (say) the 1485th iteration.
3674 ** In the TCL test script, we can add code like this:
3676 ** if {$i==1485} breakpoint
3678 ** Then run testfixture in the debugger and wait for the breakpoint to
3679 ** fire. Then additional breakpoints can be set to trace down the bug.
3681 static int SQLITE_TCLAPI test_breakpoint(
3682 void *NotUsed,
3683 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
3684 int argc, /* Number of arguments */
3685 char **argv /* Text of each argument */
3687 return TCL_OK; /* Do nothing */
3691 ** Usage: sqlite3_bind_zeroblob STMT IDX N
3693 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
3694 ** IDX is the index of a wildcard in the prepared statement. This command
3695 ** binds a N-byte zero-filled BLOB to the wildcard.
3697 static int SQLITE_TCLAPI test_bind_zeroblob(
3698 void * clientData,
3699 Tcl_Interp *interp,
3700 int objc,
3701 Tcl_Obj *CONST objv[]
3703 sqlite3_stmt *pStmt;
3704 int idx;
3705 int n;
3706 int rc;
3708 if( objc!=4 ){
3709 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3710 return TCL_ERROR;
3713 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3714 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3715 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3717 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
3718 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3719 if( rc!=SQLITE_OK ){
3720 return TCL_ERROR;
3723 return TCL_OK;
3727 ** Usage: sqlite3_bind_zeroblob64 STMT IDX N
3729 ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement.
3730 ** IDX is the index of a wildcard in the prepared statement. This command
3731 ** binds a N-byte zero-filled BLOB to the wildcard.
3733 static int SQLITE_TCLAPI test_bind_zeroblob64(
3734 void * clientData,
3735 Tcl_Interp *interp,
3736 int objc,
3737 Tcl_Obj *CONST objv[]
3739 sqlite3_stmt *pStmt;
3740 int idx;
3741 Tcl_WideInt n;
3742 int rc;
3744 if( objc!=4 ){
3745 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3746 return TCL_ERROR;
3749 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3750 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3751 if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3753 rc = sqlite3_bind_zeroblob64(pStmt, idx, n);
3754 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3755 if( rc!=SQLITE_OK ){
3756 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3757 return TCL_ERROR;
3760 return TCL_OK;
3764 ** Usage: sqlite3_bind_int STMT N VALUE
3766 ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
3767 ** N is the index of a wildcard in the prepared statement. This command
3768 ** binds a 32-bit integer VALUE to that wildcard.
3770 static int SQLITE_TCLAPI test_bind_int(
3771 void * clientData,
3772 Tcl_Interp *interp,
3773 int objc,
3774 Tcl_Obj *CONST objv[]
3776 sqlite3_stmt *pStmt;
3777 int idx;
3778 int value;
3779 int rc;
3781 if( objc!=4 ){
3782 Tcl_AppendResult(interp, "wrong # args: should be \"",
3783 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3784 return TCL_ERROR;
3787 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3788 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3789 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3791 rc = sqlite3_bind_int(pStmt, idx, value);
3792 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3793 if( rc!=SQLITE_OK ){
3794 return TCL_ERROR;
3797 return TCL_OK;
3802 ** Usage: intarray_addr INT ...
3804 ** Return the address of a C-language array of 32-bit integers.
3806 ** Space to hold the array is obtained from malloc(). Call this procedure once
3807 ** with no arguments in order to release memory. Each call to this procedure
3808 ** overwrites the previous array.
3810 static int SQLITE_TCLAPI test_intarray_addr(
3811 void * clientData,
3812 Tcl_Interp *interp,
3813 int objc,
3814 Tcl_Obj *CONST objv[]
3816 int i;
3817 static int *p = 0;
3819 sqlite3_free(p);
3820 p = 0;
3821 if( objc>1 ){
3822 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3823 if( p==0 ) return TCL_ERROR;
3824 for(i=0; i<objc-1; i++){
3825 if( Tcl_GetIntFromObj(interp, objv[1+i], &p[i]) ){
3826 sqlite3_free(p);
3827 p = 0;
3828 return TCL_ERROR;
3832 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3833 return TCL_OK;
3836 ** Usage: intarray_addr INT ...
3838 ** Return the address of a C-language array of 32-bit integers.
3840 ** Space to hold the array is obtained from malloc(). Call this procedure once
3841 ** with no arguments in order to release memory. Each call to this procedure
3842 ** overwrites the previous array.
3844 static int SQLITE_TCLAPI test_int64array_addr(
3845 void * clientData,
3846 Tcl_Interp *interp,
3847 int objc,
3848 Tcl_Obj *CONST objv[]
3850 int i;
3851 static sqlite3_int64 *p = 0;
3853 sqlite3_free(p);
3854 p = 0;
3855 if( objc>1 ){
3856 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3857 if( p==0 ) return TCL_ERROR;
3858 for(i=0; i<objc-1; i++){
3859 Tcl_WideInt v;
3860 if( Tcl_GetWideIntFromObj(interp, objv[1+i], &v) ){
3861 sqlite3_free(p);
3862 p = 0;
3863 return TCL_ERROR;
3865 p[i] = v;
3868 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3869 return TCL_OK;
3872 ** Usage: doublearray_addr INT ...
3874 ** Return the address of a C-language array of doubles.
3876 ** Space to hold the array is obtained from malloc(). Call this procedure once
3877 ** with no arguments in order to release memory. Each call to this procedure
3878 ** overwrites the previous array.
3880 static int SQLITE_TCLAPI test_doublearray_addr(
3881 void * clientData,
3882 Tcl_Interp *interp,
3883 int objc,
3884 Tcl_Obj *CONST objv[]
3886 int i;
3887 static double *p = 0;
3889 sqlite3_free(p);
3890 p = 0;
3891 if( objc>1 ){
3892 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3893 if( p==0 ) return TCL_ERROR;
3894 for(i=0; i<objc-1; i++){
3895 if( Tcl_GetDoubleFromObj(interp, objv[1+i], &p[i]) ){
3896 sqlite3_free(p);
3897 p = 0;
3898 return TCL_ERROR;
3902 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3903 return TCL_OK;
3906 ** Usage: textarray_addr TEXT ...
3908 ** Return the address of a C-language array of strings.
3910 ** Space to hold the array is obtained from malloc(). Call this procedure once
3911 ** with no arguments in order to release memory. Each call to this procedure
3912 ** overwrites the previous array.
3914 static int SQLITE_TCLAPI test_textarray_addr(
3915 void * clientData,
3916 Tcl_Interp *interp,
3917 int objc,
3918 Tcl_Obj *CONST objv[]
3920 int i;
3921 static int n = 0;
3922 static char **p = 0;
3924 for(i=0; i<n; i++) sqlite3_free(p[i]);
3925 sqlite3_free(p);
3926 p = 0;
3927 if( objc>1 ){
3928 p = sqlite3_malloc( sizeof(p[0])*(objc-1) );
3929 if( p==0 ) return TCL_ERROR;
3930 for(i=0; i<objc-1; i++){
3931 p[i] = sqlite3_mprintf("%s", Tcl_GetString(objv[1+i]));
3934 n = objc-1;
3935 Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p));
3936 return TCL_OK;
3941 ** Usage: sqlite3_bind_int64 STMT N VALUE
3943 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
3944 ** N is the index of a wildcard in the prepared statement. This command
3945 ** binds a 64-bit integer VALUE to that wildcard.
3947 static int SQLITE_TCLAPI test_bind_int64(
3948 void * clientData,
3949 Tcl_Interp *interp,
3950 int objc,
3951 Tcl_Obj *CONST objv[]
3953 sqlite3_stmt *pStmt;
3954 int idx;
3955 Tcl_WideInt value;
3956 int rc;
3958 if( objc!=4 ){
3959 Tcl_AppendResult(interp, "wrong # args: should be \"",
3960 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3961 return TCL_ERROR;
3964 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3965 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3966 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3968 rc = sqlite3_bind_int64(pStmt, idx, value);
3969 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3970 if( rc!=SQLITE_OK ){
3971 return TCL_ERROR;
3974 return TCL_OK;
3979 ** Usage: sqlite3_bind_double STMT N VALUE
3981 ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
3982 ** N is the index of a wildcard in the prepared statement. This command
3983 ** binds a 64-bit integer VALUE to that wildcard.
3985 static int SQLITE_TCLAPI test_bind_double(
3986 void * clientData,
3987 Tcl_Interp *interp,
3988 int objc,
3989 Tcl_Obj *CONST objv[]
3991 sqlite3_stmt *pStmt;
3992 int idx;
3993 double value = 0;
3994 int rc;
3995 const char *zVal;
3996 int i;
3997 static const struct {
3998 const char *zName; /* Name of the special floating point value */
3999 unsigned int iUpper; /* Upper 32 bits */
4000 unsigned int iLower; /* Lower 32 bits */
4001 } aSpecialFp[] = {
4002 { "NaN", 0x7fffffff, 0xffffffff },
4003 { "SNaN", 0x7ff7ffff, 0xffffffff },
4004 { "-NaN", 0xffffffff, 0xffffffff },
4005 { "-SNaN", 0xfff7ffff, 0xffffffff },
4006 { "+Inf", 0x7ff00000, 0x00000000 },
4007 { "-Inf", 0xfff00000, 0x00000000 },
4008 { "Epsilon", 0x00000000, 0x00000001 },
4009 { "-Epsilon", 0x80000000, 0x00000001 },
4010 { "NaN0", 0x7ff80000, 0x00000000 },
4011 { "-NaN0", 0xfff80000, 0x00000000 },
4014 if( objc!=4 ){
4015 Tcl_AppendResult(interp, "wrong # args: should be \"",
4016 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
4017 return TCL_ERROR;
4020 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4021 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4023 /* Intercept the string "NaN" and generate a NaN value for it.
4024 ** All other strings are passed through to Tcl_GetDoubleFromObj().
4025 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
4026 ** contain a bug.
4028 zVal = Tcl_GetString(objv[3]);
4029 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
4030 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
4031 sqlite3_uint64 x;
4032 x = aSpecialFp[i].iUpper;
4033 x <<= 32;
4034 x |= aSpecialFp[i].iLower;
4035 assert( sizeof(value)==8 );
4036 assert( sizeof(x)==8 );
4037 memcpy(&value, &x, 8);
4038 break;
4041 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
4042 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
4043 return TCL_ERROR;
4045 rc = sqlite3_bind_double(pStmt, idx, value);
4046 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4047 if( rc!=SQLITE_OK ){
4048 return TCL_ERROR;
4051 return TCL_OK;
4055 ** Usage: sqlite3_bind_null STMT N
4057 ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
4058 ** N is the index of a wildcard in the prepared statement. This command
4059 ** binds a NULL to the wildcard.
4061 static int SQLITE_TCLAPI test_bind_null(
4062 void * clientData,
4063 Tcl_Interp *interp,
4064 int objc,
4065 Tcl_Obj *CONST objv[]
4067 sqlite3_stmt *pStmt;
4068 int idx;
4069 int rc;
4071 if( objc!=3 ){
4072 Tcl_AppendResult(interp, "wrong # args: should be \"",
4073 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
4074 return TCL_ERROR;
4077 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4078 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4080 rc = sqlite3_bind_null(pStmt, idx);
4081 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4082 if( rc!=SQLITE_OK ){
4083 return TCL_ERROR;
4086 return TCL_OK;
4090 ** Usage: sqlite3_bind_text STMT N STRING BYTES
4092 ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
4093 ** N is the index of a wildcard in the prepared statement. This command
4094 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
4095 ** long.
4097 static int SQLITE_TCLAPI test_bind_text(
4098 void * clientData,
4099 Tcl_Interp *interp,
4100 int objc,
4101 Tcl_Obj *CONST objv[]
4103 sqlite3_stmt *pStmt;
4104 int idx;
4105 int trueLength = 0;
4106 int bytes;
4107 char *value;
4108 int rc;
4109 char *toFree = 0;
4111 if( objc!=5 ){
4112 Tcl_AppendResult(interp, "wrong # args: should be \"",
4113 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
4114 return TCL_ERROR;
4117 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4118 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4119 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &trueLength);
4120 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
4121 if( bytes<0 ){
4122 toFree = malloc( trueLength + 1 );
4123 if( toFree==0 ){
4124 Tcl_AppendResult(interp, "out of memory", (void*)0);
4125 return TCL_ERROR;
4127 memcpy(toFree, value, trueLength);
4128 toFree[trueLength] = 0;
4129 value = toFree;
4131 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
4132 free(toFree);
4133 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4134 if( rc!=SQLITE_OK ){
4135 Tcl_AppendResult(interp, sqlite3ErrName(rc), (void*)0);
4136 return TCL_ERROR;
4139 return TCL_OK;
4143 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
4145 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
4146 ** N is the index of a wildcard in the prepared statement. This command
4147 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
4148 ** long.
4150 static int SQLITE_TCLAPI test_bind_text16(
4151 void * clientData,
4152 Tcl_Interp *interp,
4153 int objc,
4154 Tcl_Obj *CONST objv[]
4156 #ifndef SQLITE_OMIT_UTF16
4157 sqlite3_stmt *pStmt;
4158 int idx;
4159 int bytes;
4160 char *value;
4161 char *toFree = 0;
4162 int rc;
4163 int trueLength = 0;
4165 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
4166 Tcl_Obj *oStmt = objv[objc-4];
4167 Tcl_Obj *oN = objv[objc-3];
4168 Tcl_Obj *oString = objv[objc-2];
4169 Tcl_Obj *oBytes = objv[objc-1];
4171 if( objc!=5 && objc!=6){
4172 Tcl_AppendResult(interp, "wrong # args: should be \"",
4173 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
4174 return TCL_ERROR;
4177 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
4178 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
4179 value = (char*)Tcl_GetByteArrayFromObj(oString, &trueLength);
4180 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
4181 if( bytes<0 && xDel==SQLITE_TRANSIENT ){
4182 toFree = malloc( trueLength + 3 );
4183 if( toFree==0 ){
4184 Tcl_AppendResult(interp, "out of memory", (void*)0);
4185 return TCL_ERROR;
4187 memcpy(toFree, value, trueLength);
4188 memset(toFree+trueLength, 0, 3);
4189 value = toFree;
4191 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
4192 free(toFree);
4193 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4194 if( rc!=SQLITE_OK ){
4195 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
4196 return TCL_ERROR;
4199 #endif /* SQLITE_OMIT_UTF16 */
4200 return TCL_OK;
4204 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
4206 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
4207 ** N is the index of a wildcard in the prepared statement. This command
4208 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
4210 static int SQLITE_TCLAPI test_bind_blob(
4211 void * clientData,
4212 Tcl_Interp *interp,
4213 int objc,
4214 Tcl_Obj *CONST objv[]
4216 sqlite3_stmt *pStmt;
4217 int len, idx;
4218 int bytes;
4219 char *value;
4220 int rc;
4221 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
4223 if( objc!=5 && objc!=6 ){
4224 Tcl_AppendResult(interp, "wrong # args: should be \"",
4225 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
4226 return TCL_ERROR;
4229 if( objc==6 ){
4230 xDestructor = SQLITE_STATIC;
4231 objv++;
4234 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4235 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4237 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &len);
4238 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
4240 if( bytes>len ){
4241 char zBuf[200];
4242 sqlite3_snprintf(sizeof(zBuf), zBuf,
4243 "cannot use %d blob bytes, have %d", bytes, len);
4244 Tcl_AppendResult(interp, zBuf, (char*)0);
4245 return TCL_ERROR;
4248 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
4249 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
4250 if( rc!=SQLITE_OK ){
4251 return TCL_ERROR;
4254 return TCL_OK;
4258 ** Usage: sqlite3_bind_value_from_preupdate STMT N NEW|OLD IDX
4260 ** Test the sqlite3_bind_value interface using sqlite3_value objects
4261 ** obtained from either sqlite3_preupdate_new() (if arg[3]=="new") or
4262 ** sqlite3_preupdate_old() if (arg[3]=="old"). IDX is the index to
4263 ** pass to the sqlite3_preupdate_xxx() function.
4265 static int SQLITE_TCLAPI test_bind_value_from_preupdate(
4266 void * clientData,
4267 Tcl_Interp *interp,
4268 int objc,
4269 Tcl_Obj *CONST objv[]
4271 sqlite3_stmt *pStmt;
4272 int idx;
4273 int bidx;
4274 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4275 const char *z3 = 0;
4276 sqlite3 *db = 0;
4277 sqlite3_value *pVal = 0;
4278 #endif
4280 if( objc!=5 ){
4281 Tcl_WrongNumArgs(interp, 1, objv, "STMT N NEW|OLD IDX");
4282 return TCL_ERROR;
4285 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4286 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4287 if( Tcl_GetIntFromObj(interp, objv[4], &bidx) ) return TCL_ERROR;
4289 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4290 z3 = Tcl_GetString(objv[3]);
4291 db = sqlite3_db_handle(pStmt);
4292 if( z3[0]=='n' ){
4293 sqlite3_preupdate_new(db, bidx, &pVal);
4294 }else if( z3[0]=='o' ){
4295 sqlite3_preupdate_old(db, bidx, &pVal);
4296 }else{
4297 Tcl_AppendResult(interp, "expected new or old, got: ", z3, (char*)0);
4298 return TCL_ERROR;
4300 sqlite3_bind_value(pStmt, idx, pVal);
4301 #endif
4303 return TCL_OK;
4307 ** Usage: sqlite3_bind_value_from_select STMT N SELECT
4309 ** Test the sqlite3_bind_value interface. STMT is a prepared statement.
4310 ** N is the index of a wildcard in the prepared statement.
4312 static int SQLITE_TCLAPI test_bind_value_from_select(
4313 void * clientData,
4314 Tcl_Interp *interp,
4315 int objc,
4316 Tcl_Obj *CONST objv[]
4318 sqlite3_stmt *pStmt;
4319 sqlite3_stmt *pStmt2;
4320 int idx;
4321 const char *zSql = 0;
4322 sqlite3 *db = 0;
4323 int rc = SQLITE_OK;
4325 if( objc!=4 ){
4326 Tcl_WrongNumArgs(interp, 1, objv, "STMT N SELECT");
4327 return TCL_ERROR;
4330 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4331 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
4332 zSql = Tcl_GetString(objv[3]);
4333 db = sqlite3_db_handle(pStmt);
4335 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt2, 0);
4336 if( rc!=SQLITE_OK ){
4337 Tcl_AppendResult(interp, "error in SQL: ", sqlite3_errmsg(db), (char*)0);
4338 return TCL_ERROR;
4340 if( sqlite3_step(pStmt2)==SQLITE_ROW ){
4341 sqlite3_value *pVal = sqlite3_column_value(pStmt2, 0);
4342 sqlite3_bind_value(pStmt, idx, pVal);
4344 rc = sqlite3_finalize(pStmt2);
4345 if( rc!=SQLITE_OK ){
4346 Tcl_AppendResult(interp,
4347 "error runnning SQL: ", sqlite3_errmsg(db), (char*)0
4349 return TCL_ERROR;
4352 return TCL_OK;
4355 #ifdef _WIN32
4356 struct iovec {
4357 void *iov_base;
4358 size_t iov_len;
4360 #else
4361 # include <sys/uio.h>
4362 #endif
4364 #ifndef SQLITE_OMIT_VIRTUALTABLE
4366 ** sqlite3_carray_bind [options...] STMT NAME VALUE ...
4368 ** Options:
4369 ** -transient
4370 ** -static
4371 ** -int32
4372 ** -int64
4373 ** -double
4374 ** -text
4375 ** -blob
4377 ** Each call clears static data. Called with no options does nothing
4378 ** but clear static data.
4380 static int SQLITE_TCLAPI test_carray_bind(
4381 void * clientData,
4382 Tcl_Interp *interp,
4383 int objc,
4384 Tcl_Obj *CONST objv[]
4386 sqlite3_stmt *pStmt;
4387 int eType = 0; /* CARRAY_INT32 */
4388 int nData = 0;
4389 void *aData = 0;
4390 int isTransient = 0;
4391 int isStatic = 0;
4392 int idx;
4393 int i, j;
4394 int rc;
4395 void (*xDel)(void*) = sqlite3_free;
4396 static void *aStaticData = 0;
4397 static int nStaticData = 0;
4398 static int eStaticType = 0;
4399 extern int sqlite3_carray_bind(
4400 sqlite3_stmt *pStmt,
4401 int i,
4402 void *aData,
4403 int nData,
4404 int mFlags,
4405 void (*xDestroy)(void*)
4408 if( aStaticData ){
4409 /* Always clear preexisting static data on every call */
4410 if( eStaticType==3 ){
4411 for(i=0; i<nStaticData; i++){
4412 sqlite3_free(((char**)aStaticData)[i]);
4415 if( eStaticType==4 ){
4416 for(i=0; i<nStaticData; i++){
4417 sqlite3_free(((struct iovec*)aStaticData)[i].iov_base);
4420 sqlite3_free(aStaticData);
4421 aStaticData = 0;
4422 nStaticData = 0;
4423 eStaticType = 0;
4425 if( objc==1 ) return TCL_OK;
4427 for(i=1; i<objc && Tcl_GetString(objv[i])[0]=='-'; i++){
4428 const char *z = Tcl_GetString(objv[i]);
4429 if( strcmp(z, "-transient")==0 ){
4430 isTransient = 1;
4431 xDel = SQLITE_TRANSIENT;
4432 }else
4433 if( strcmp(z, "-static")==0 ){
4434 isStatic = 1;
4435 xDel = SQLITE_STATIC;
4436 }else
4437 if( strcmp(z, "-int32")==0 ){
4438 eType = 0; /* CARRAY_INT32 */
4439 }else
4440 if( strcmp(z, "-int64")==0 ){
4441 eType = 1; /* CARRAY_INT64 */
4442 }else
4443 if( strcmp(z, "-double")==0 ){
4444 eType = 2; /* CARRAY_DOUBLE */
4445 }else
4446 if( strcmp(z, "-text")==0 ){
4447 eType = 3; /* CARRAY_TEXT */
4448 }else
4449 if( strcmp(z, "-blob")==0 ){
4450 eType = 4; /* CARRAY_BLOB */
4451 }else
4452 if( strcmp(z, "--")==0 ){
4453 break;
4454 }else
4456 Tcl_AppendResult(interp, "unknown option: ", z, (char*)0);
4457 return TCL_ERROR;
4460 if( eType==3 && !isStatic && !isTransient ){
4461 Tcl_AppendResult(interp, "text data must be either -static or -transient",
4462 (char*)0);
4463 return TCL_ERROR;
4465 if( eType==4 && !isStatic && !isTransient ){
4466 Tcl_AppendResult(interp, "blob data must be either -static or -transient",
4467 (char*)0);
4468 return TCL_ERROR;
4470 if( isStatic && isTransient ){
4471 Tcl_AppendResult(interp, "cannot be both -static and -transient",
4472 (char*)0);
4473 return TCL_ERROR;
4475 if( objc-i < 2 ){
4476 Tcl_WrongNumArgs(interp, 1, objv, "[OPTIONS] STMT IDX VALUE ...");
4477 return TCL_ERROR;
4479 if( getStmtPointer(interp, Tcl_GetString(objv[i]), &pStmt) ) return TCL_ERROR;
4480 i++;
4481 if( Tcl_GetIntFromObj(interp, objv[i], &idx) ) return TCL_ERROR;
4482 i++;
4483 nData = objc - i;
4484 switch( eType + 5*(nData<=0) ){
4485 case 0: { /* INT32 */
4486 int *a = sqlite3_malloc( sizeof(int)*nData );
4487 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4488 for(j=0; j<nData; j++){
4489 int v;
4490 if( Tcl_GetIntFromObj(interp, objv[i+j], &v) ){
4491 sqlite3_free(a);
4492 return TCL_ERROR;
4494 a[j] = v;
4496 aData = a;
4497 break;
4499 case 1: { /* INT64 */
4500 sqlite3_int64 *a = sqlite3_malloc( sizeof(sqlite3_int64)*nData );
4501 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4502 for(j=0; j<nData; j++){
4503 Tcl_WideInt v;
4504 if( Tcl_GetWideIntFromObj(interp, objv[i+j], &v) ){
4505 sqlite3_free(a);
4506 return TCL_ERROR;
4508 a[j] = v;
4510 aData = a;
4511 break;
4513 case 2: { /* DOUBLE */
4514 double *a = sqlite3_malloc( sizeof(double)*nData );
4515 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4516 for(j=0; j<nData; j++){
4517 double v;
4518 if( Tcl_GetDoubleFromObj(interp, objv[i+j], &v) ){
4519 sqlite3_free(a);
4520 return TCL_ERROR;
4522 a[j] = v;
4524 aData = a;
4525 break;
4527 case 3: { /* TEXT */
4528 char **a = sqlite3_malloc( sizeof(char*)*nData );
4529 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4530 for(j=0; j<nData; j++){
4531 const char *v = Tcl_GetString(objv[i+j]);
4532 a[j] = sqlite3_mprintf("%s", v);
4534 aData = a;
4535 break;
4537 case 4: { /* BLOB */
4538 struct iovec *a = sqlite3_malloc( sizeof(struct iovec)*nData );
4539 if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; }
4540 for(j=0; j<nData; j++){
4541 int n = 0;
4542 unsigned char *v = Tcl_GetByteArrayFromObj(objv[i+i], &n);
4543 a[j].iov_len = n;
4544 a[j].iov_base = sqlite3_malloc64( n );
4545 if( a[j].iov_base==0 ){
4546 a[j].iov_len = 0;
4547 }else{
4548 memcpy(a[j].iov_base, v, n);
4551 aData = a;
4552 break;
4554 case 5: { /* nData==0 */
4555 aData = "";
4556 xDel = SQLITE_STATIC;
4557 isTransient = 0;
4558 isStatic = 0;
4559 break;
4562 if( isStatic ){
4563 aStaticData = aData;
4564 nStaticData = nData;
4565 eStaticType = eType;
4567 rc = sqlite3_carray_bind(pStmt, idx, aData, nData, eType, xDel);
4568 if( isTransient ){
4569 if( eType==3 ){
4570 for(i=0; i<nData; i++) sqlite3_free(((char**)aData)[i]);
4572 if( eType==4 ){
4573 for(i=0; i<nData; i++) sqlite3_free(((struct iovec*)aData)[i].iov_base);
4575 sqlite3_free(aData);
4577 carray_bind_done:
4578 if( rc ){
4579 Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0);
4580 return TCL_ERROR;
4582 return TCL_OK;
4584 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4587 ** Usage: sqlite3_bind_parameter_count STMT
4589 ** Return the number of wildcards in the given statement.
4591 static int SQLITE_TCLAPI test_bind_parameter_count(
4592 void * clientData,
4593 Tcl_Interp *interp,
4594 int objc,
4595 Tcl_Obj *CONST objv[]
4597 sqlite3_stmt *pStmt;
4599 if( objc!=2 ){
4600 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4601 return TCL_ERROR;
4603 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4604 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
4605 return TCL_OK;
4609 ** Usage: sqlite3_bind_parameter_name STMT N
4611 ** Return the name of the Nth wildcard. The first wildcard is 1.
4612 ** An empty string is returned if N is out of range or if the wildcard
4613 ** is nameless.
4615 static int SQLITE_TCLAPI test_bind_parameter_name(
4616 void * clientData,
4617 Tcl_Interp *interp,
4618 int objc,
4619 Tcl_Obj *CONST objv[]
4621 sqlite3_stmt *pStmt;
4622 int i;
4624 if( objc!=3 ){
4625 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
4626 return TCL_ERROR;
4628 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4629 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
4630 Tcl_SetObjResult(interp,
4631 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
4633 return TCL_OK;
4637 ** Usage: sqlite3_bind_parameter_index STMT NAME
4639 ** Return the index of the wildcard called NAME. Return 0 if there is
4640 ** no such wildcard.
4642 static int SQLITE_TCLAPI test_bind_parameter_index(
4643 void * clientData,
4644 Tcl_Interp *interp,
4645 int objc,
4646 Tcl_Obj *CONST objv[]
4648 sqlite3_stmt *pStmt;
4650 if( objc!=3 ){
4651 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
4652 return TCL_ERROR;
4654 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4655 Tcl_SetObjResult(interp,
4656 Tcl_NewIntObj(
4657 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
4660 return TCL_OK;
4664 ** Usage: sqlite3_clear_bindings STMT
4667 static int SQLITE_TCLAPI test_clear_bindings(
4668 void * clientData,
4669 Tcl_Interp *interp,
4670 int objc,
4671 Tcl_Obj *CONST objv[]
4673 sqlite3_stmt *pStmt;
4675 if( objc!=2 ){
4676 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4677 return TCL_ERROR;
4679 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4680 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
4681 return TCL_OK;
4685 ** Usage: sqlite3_sleep MILLISECONDS
4687 static int SQLITE_TCLAPI test_sleep(
4688 void * clientData,
4689 Tcl_Interp *interp,
4690 int objc,
4691 Tcl_Obj *CONST objv[]
4693 int ms;
4695 if( objc!=2 ){
4696 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
4697 return TCL_ERROR;
4699 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
4700 return TCL_ERROR;
4702 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
4703 return TCL_OK;
4707 ** Usage: sqlite3_extended_errcode DB
4709 ** Return the string representation of the most recent sqlite3_* API
4710 ** error code. e.g. "SQLITE_ERROR".
4712 static int SQLITE_TCLAPI test_ex_errcode(
4713 void * clientData,
4714 Tcl_Interp *interp,
4715 int objc,
4716 Tcl_Obj *CONST objv[]
4718 sqlite3 *db;
4719 int rc;
4721 if( objc!=2 ){
4722 Tcl_AppendResult(interp, "wrong # args: should be \"",
4723 Tcl_GetString(objv[0]), " DB", 0);
4724 return TCL_ERROR;
4726 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4727 rc = sqlite3_extended_errcode(db);
4728 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4729 return TCL_OK;
4734 ** Usage: sqlite3_errcode DB
4736 ** Return the string representation of the most recent sqlite3_* API
4737 ** error code. e.g. "SQLITE_ERROR".
4739 static int SQLITE_TCLAPI test_errcode(
4740 void * clientData,
4741 Tcl_Interp *interp,
4742 int objc,
4743 Tcl_Obj *CONST objv[]
4745 sqlite3 *db;
4746 int rc;
4748 if( objc!=2 ){
4749 Tcl_AppendResult(interp, "wrong # args: should be \"",
4750 Tcl_GetString(objv[0]), " DB", 0);
4751 return TCL_ERROR;
4753 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4754 rc = sqlite3_errcode(db);
4755 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
4756 return TCL_OK;
4760 ** Usage: sqlite3_errmsg DB
4762 ** Returns the UTF-8 representation of the error message string for the
4763 ** most recent sqlite3_* API call.
4765 static int SQLITE_TCLAPI test_errmsg(
4766 void * clientData,
4767 Tcl_Interp *interp,
4768 int objc,
4769 Tcl_Obj *CONST objv[]
4771 sqlite3 *db;
4772 const char *zErr;
4774 if( objc!=2 ){
4775 Tcl_AppendResult(interp, "wrong # args: should be \"",
4776 Tcl_GetString(objv[0]), " DB", 0);
4777 return TCL_ERROR;
4779 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4781 zErr = sqlite3_errmsg(db);
4782 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
4783 return TCL_OK;
4788 ** Usage: sqlite3_error_offset DB
4790 ** Return the byte offset into the input UTF8 SQL for the most recent
4791 ** error, or -1 of the error does not refer to a specific token.
4793 static int SQLITE_TCLAPI test_error_offset(
4794 void * clientData,
4795 Tcl_Interp *interp,
4796 int objc,
4797 Tcl_Obj *CONST objv[]
4799 sqlite3 *db;
4800 int iByteOffset;
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 iByteOffset = sqlite3_error_offset(db);
4810 Tcl_SetObjResult(interp, Tcl_NewIntObj(iByteOffset));
4811 return TCL_OK;
4815 ** Usage: test_errmsg16 DB
4817 ** Returns the UTF-16 representation of the error message string for the
4818 ** most recent sqlite3_* API call. This is a byte array object at the TCL
4819 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
4820 ** UTF-16 string.
4822 static int SQLITE_TCLAPI test_errmsg16(
4823 void * clientData,
4824 Tcl_Interp *interp,
4825 int objc,
4826 Tcl_Obj *CONST objv[]
4828 #ifndef SQLITE_OMIT_UTF16
4829 sqlite3 *db;
4830 const void *zErr;
4831 const char *z;
4832 int bytes = 0;
4834 if( objc!=2 ){
4835 Tcl_AppendResult(interp, "wrong # args: should be \"",
4836 Tcl_GetString(objv[0]), " DB", 0);
4837 return TCL_ERROR;
4839 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4841 zErr = sqlite3_errmsg16(db);
4842 if( zErr ){
4843 z = zErr;
4844 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
4846 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
4847 #endif /* SQLITE_OMIT_UTF16 */
4848 return TCL_OK;
4852 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
4854 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4855 ** database handle <DB>. The parameter <tailval> is the name of a global
4856 ** variable that is set to the unused portion of <sql> (if any). A
4857 ** STMT handle is returned.
4859 static int SQLITE_TCLAPI test_prepare(
4860 void * clientData,
4861 Tcl_Interp *interp,
4862 int objc,
4863 Tcl_Obj *CONST objv[]
4865 sqlite3 *db;
4866 const char *zSql;
4867 int bytes;
4868 const char *zTail = 0;
4869 sqlite3_stmt *pStmt = 0;
4870 char zBuf[50];
4871 int rc;
4873 if( objc!=5 && objc!=4 ){
4874 Tcl_AppendResult(interp, "wrong # args: should be \"",
4875 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4876 return TCL_ERROR;
4878 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4879 zSql = Tcl_GetString(objv[2]);
4880 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4882 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4883 Tcl_ResetResult(interp);
4884 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4885 if( zTail && objc>=5 ){
4886 if( bytes>=0 ){
4887 bytes = bytes - (int)(zTail-zSql);
4889 if( (int)strlen(zTail)<bytes ){
4890 bytes = (int)strlen(zTail);
4892 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4894 if( rc!=SQLITE_OK ){
4895 assert( pStmt==0 );
4896 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4897 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4898 return TCL_ERROR;
4901 if( pStmt ){
4902 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4903 Tcl_AppendResult(interp, zBuf, 0);
4905 return TCL_OK;
4909 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
4911 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4912 ** database handle <DB>. The parameter <tailval> is the name of a global
4913 ** variable that is set to the unused portion of <sql> (if any). A
4914 ** STMT handle is returned.
4916 static int SQLITE_TCLAPI test_prepare_v2(
4917 void * clientData,
4918 Tcl_Interp *interp,
4919 int objc,
4920 Tcl_Obj *CONST objv[]
4922 sqlite3 *db;
4923 const char *zSql;
4924 char *zCopy = 0; /* malloc() copy of zSql */
4925 int bytes;
4926 const char *zTail = 0;
4927 const char **pzTail;
4928 sqlite3_stmt *pStmt = 0;
4929 char zBuf[50];
4930 int rc;
4932 if( objc!=5 && objc!=4 ){
4933 Tcl_AppendResult(interp, "wrong # args: should be \"",
4934 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
4935 return TCL_ERROR;
4937 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4938 zSql = Tcl_GetString(objv[2]);
4939 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4941 /* Instead of using zSql directly, make a copy into a buffer obtained
4942 ** directly from malloc(). The idea is to make it easier for valgrind
4943 ** to spot buffer overreads. */
4944 if( bytes>=0 ){
4945 zCopy = malloc(bytes);
4946 memcpy(zCopy, zSql, bytes);
4947 }else{
4948 int n = (int)strlen(zSql) + 1;
4949 zCopy = malloc(n);
4950 memcpy(zCopy, zSql, n);
4952 pzTail = objc>=5 ? &zTail : 0;
4953 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail);
4954 if( objc>=5 ){
4955 zTail = &zSql[(zTail - zCopy)];
4957 free(zCopy);
4959 assert(rc==SQLITE_OK || pStmt==0);
4960 Tcl_ResetResult(interp);
4961 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4962 if( rc==SQLITE_OK && objc>=5 && zTail ){
4963 if( bytes>=0 ){
4964 bytes = bytes - (int)(zTail-zSql);
4966 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
4968 if( rc!=SQLITE_OK ){
4969 assert( pStmt==0 );
4970 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
4971 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
4972 return TCL_ERROR;
4975 if( pStmt ){
4976 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4977 Tcl_AppendResult(interp, zBuf, 0);
4979 return TCL_OK;
4983 ** Usage: sqlite3_prepare_v3 DB sql bytes flags ?tailvar?
4985 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
4986 ** database handle <DB> and flags <flags>. The parameter <tailval> is
4987 ** the name of a global variable that is set to the unused portion of
4988 ** <sql> (if any). A STMT handle is returned.
4990 static int SQLITE_TCLAPI test_prepare_v3(
4991 void * clientData,
4992 Tcl_Interp *interp,
4993 int objc,
4994 Tcl_Obj *CONST objv[]
4996 sqlite3 *db;
4997 const char *zSql;
4998 char *zCopy = 0; /* malloc() copy of zSql */
4999 int bytes, flags;
5000 const char *zTail = 0;
5001 const char **pzTail;
5002 sqlite3_stmt *pStmt = 0;
5003 char zBuf[50];
5004 int rc;
5006 if( objc!=6 && objc!=5 ){
5007 Tcl_AppendResult(interp, "wrong # args: should be \"",
5008 Tcl_GetString(objv[0]), " DB sql bytes flags tailvar", 0);
5009 return TCL_ERROR;
5011 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5012 zSql = Tcl_GetString(objv[2]);
5013 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5014 if( Tcl_GetIntFromObj(interp, objv[4], &flags) ) return TCL_ERROR;
5016 /* Instead of using zSql directly, make a copy into a buffer obtained
5017 ** directly from malloc(). The idea is to make it easier for valgrind
5018 ** to spot buffer overreads. */
5019 if( bytes>=0 ){
5020 zCopy = malloc(bytes);
5021 memcpy(zCopy, zSql, bytes);
5022 }else{
5023 int n = (int)strlen(zSql) + 1;
5024 zCopy = malloc(n);
5025 memcpy(zCopy, zSql, n);
5027 pzTail = objc>=6 ? &zTail : 0;
5028 rc = sqlite3_prepare_v3(db, zCopy, bytes, (unsigned int)flags,&pStmt,pzTail);
5029 free(zCopy);
5030 zTail = &zSql[(zTail - zCopy)];
5032 assert(rc==SQLITE_OK || pStmt==0);
5033 Tcl_ResetResult(interp);
5034 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5035 if( rc==SQLITE_OK && zTail && objc>=6 ){
5036 if( bytes>=0 ){
5037 bytes = bytes - (int)(zTail-zSql);
5039 Tcl_ObjSetVar2(interp, objv[5], 0, Tcl_NewStringObj(zTail, bytes), 0);
5041 if( rc!=SQLITE_OK ){
5042 assert( pStmt==0 );
5043 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
5044 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
5045 return TCL_ERROR;
5048 if( pStmt ){
5049 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5050 Tcl_AppendResult(interp, zBuf, 0);
5052 return TCL_OK;
5056 ** Usage: sqlite3_prepare_tkt3134 DB
5058 ** Generate a prepared statement for a zero-byte string as a test
5059 ** for ticket #3134. The string should be preceded by a zero byte.
5061 static int SQLITE_TCLAPI test_prepare_tkt3134(
5062 void * clientData,
5063 Tcl_Interp *interp,
5064 int objc,
5065 Tcl_Obj *CONST objv[]
5067 sqlite3 *db;
5068 static const char zSql[] = "\000SELECT 1";
5069 sqlite3_stmt *pStmt = 0;
5070 char zBuf[50];
5071 int rc;
5073 if( objc!=2 ){
5074 Tcl_AppendResult(interp, "wrong # args: should be \"",
5075 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
5076 return TCL_ERROR;
5078 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5079 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
5080 assert(rc==SQLITE_OK || pStmt==0);
5081 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5082 if( rc!=SQLITE_OK ){
5083 assert( pStmt==0 );
5084 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
5085 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
5086 return TCL_ERROR;
5089 if( pStmt ){
5090 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5091 Tcl_AppendResult(interp, zBuf, 0);
5093 return TCL_OK;
5097 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
5099 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
5100 ** database handle <DB>. The parameter <tailval> is the name of a global
5101 ** variable that is set to the unused portion of <sql> (if any). A
5102 ** STMT handle is returned.
5104 static int SQLITE_TCLAPI test_prepare16(
5105 void * clientData,
5106 Tcl_Interp *interp,
5107 int objc,
5108 Tcl_Obj *CONST objv[]
5110 #ifndef SQLITE_OMIT_UTF16
5111 sqlite3 *db;
5112 const void *zSql;
5113 const void *zTail = 0;
5114 Tcl_Obj *pTail = 0;
5115 sqlite3_stmt *pStmt = 0;
5116 char zBuf[50];
5117 int rc;
5118 int bytes; /* The integer specified as arg 3 */
5119 int objlen; /* The byte-array length of arg 2 */
5121 if( objc!=5 && objc!=4 ){
5122 Tcl_AppendResult(interp, "wrong # args: should be \"",
5123 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
5124 return TCL_ERROR;
5126 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5127 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
5128 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5130 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
5131 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5132 if( rc ){
5133 return TCL_ERROR;
5136 if( objc>=5 ){
5137 if( zTail ){
5138 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
5139 }else{
5140 objlen = 0;
5142 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
5143 Tcl_IncrRefCount(pTail);
5144 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
5145 Tcl_DecrRefCount(pTail);
5148 if( pStmt ){
5149 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5151 Tcl_AppendResult(interp, zBuf, 0);
5152 #endif /* SQLITE_OMIT_UTF16 */
5153 return TCL_OK;
5157 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
5159 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
5160 ** database handle <DB>. The parameter <tailval> is the name of a global
5161 ** variable that is set to the unused portion of <sql> (if any). A
5162 ** STMT handle is returned.
5164 static int SQLITE_TCLAPI test_prepare16_v2(
5165 void * clientData,
5166 Tcl_Interp *interp,
5167 int objc,
5168 Tcl_Obj *CONST objv[]
5170 #ifndef SQLITE_OMIT_UTF16
5171 sqlite3 *db;
5172 const void *zSql;
5173 const void *zTail = 0;
5174 Tcl_Obj *pTail = 0;
5175 sqlite3_stmt *pStmt = 0;
5176 char zBuf[50];
5177 int rc;
5178 int bytes; /* The integer specified as arg 3 */
5179 int objlen; /* The byte-array length of arg 2 */
5181 if( objc!=5 && objc!=4 ){
5182 Tcl_AppendResult(interp, "wrong # args: should be \"",
5183 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
5184 return TCL_ERROR;
5186 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5187 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
5188 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
5190 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
5191 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
5192 if( rc ){
5193 return TCL_ERROR;
5196 if( objc>=5 ){
5197 if( zTail ){
5198 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
5199 }else{
5200 objlen = 0;
5202 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
5203 Tcl_IncrRefCount(pTail);
5204 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
5205 Tcl_DecrRefCount(pTail);
5208 if( pStmt ){
5209 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
5211 Tcl_AppendResult(interp, zBuf, 0);
5212 #endif /* SQLITE_OMIT_UTF16 */
5213 return TCL_OK;
5217 ** Usage: sqlite3_open filename ?options-list?
5219 static int SQLITE_TCLAPI test_open(
5220 void * clientData,
5221 Tcl_Interp *interp,
5222 int objc,
5223 Tcl_Obj *CONST objv[]
5225 const char *zFilename;
5226 sqlite3 *db;
5227 char zBuf[100];
5229 if( objc!=3 && objc!=2 && objc!=1 ){
5230 Tcl_AppendResult(interp, "wrong # args: should be \"",
5231 Tcl_GetString(objv[0]), " filename options-list", 0);
5232 return TCL_ERROR;
5235 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
5236 sqlite3_open(zFilename, &db);
5238 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5239 Tcl_AppendResult(interp, zBuf, 0);
5240 return TCL_OK;
5244 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
5246 static int SQLITE_TCLAPI test_open_v2(
5247 void * clientData,
5248 Tcl_Interp *interp,
5249 int objc,
5250 Tcl_Obj *CONST objv[]
5252 const char *zFilename;
5253 const char *zVfs;
5254 int flags = 0;
5255 sqlite3 *db;
5256 int rc;
5257 char zBuf[100];
5259 int nFlag;
5260 Tcl_Obj **apFlag;
5261 int i;
5263 if( objc!=4 ){
5264 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
5265 return TCL_ERROR;
5267 zFilename = Tcl_GetString(objv[1]);
5268 zVfs = Tcl_GetString(objv[3]);
5269 if( zVfs[0]==0x00 ) zVfs = 0;
5271 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
5272 if( rc!=TCL_OK ) return rc;
5273 for(i=0; i<nFlag; i++){
5274 int iFlag;
5275 struct OpenFlag {
5276 const char *zFlag;
5277 int flag;
5278 } aFlag[] = {
5279 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
5280 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
5281 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
5282 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
5283 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
5284 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
5285 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
5286 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
5287 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
5288 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
5289 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
5290 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
5291 { "SQLITE_OPEN_SUPER_JOURNAL", SQLITE_OPEN_SUPER_JOURNAL },
5292 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
5293 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
5294 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
5295 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
5296 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
5297 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
5298 { "SQLITE_OPEN_EXRESCODE", SQLITE_OPEN_EXRESCODE },
5299 { 0, 0 }
5301 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
5302 "flag", 0, &iFlag
5304 if( rc!=TCL_OK ) return rc;
5305 flags |= aFlag[iFlag].flag;
5308 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
5309 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5310 Tcl_AppendResult(interp, zBuf, 0);
5311 return TCL_OK;
5315 ** Usage: sqlite3_open16 filename options
5317 static int SQLITE_TCLAPI test_open16(
5318 void * clientData,
5319 Tcl_Interp *interp,
5320 int objc,
5321 Tcl_Obj *CONST objv[]
5323 #ifndef SQLITE_OMIT_UTF16
5324 const void *zFilename;
5325 sqlite3 *db;
5326 char zBuf[100];
5328 if( objc!=3 ){
5329 Tcl_AppendResult(interp, "wrong # args: should be \"",
5330 Tcl_GetString(objv[0]), " filename options-list", 0);
5331 return TCL_ERROR;
5334 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
5335 sqlite3_open16(zFilename, &db);
5337 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
5338 Tcl_AppendResult(interp, zBuf, 0);
5339 #endif /* SQLITE_OMIT_UTF16 */
5340 return TCL_OK;
5344 ** Usage: sqlite3_complete16 <UTF-16 string>
5346 ** Return 1 if the supplied argument is a complete SQL statement, or zero
5347 ** otherwise.
5349 static int SQLITE_TCLAPI test_complete16(
5350 void * clientData,
5351 Tcl_Interp *interp,
5352 int objc,
5353 Tcl_Obj *CONST objv[]
5355 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
5356 char *zBuf;
5358 if( objc!=2 ){
5359 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
5360 return TCL_ERROR;
5363 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
5364 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
5365 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
5366 return TCL_OK;
5370 ** Usage: sqlite3_normalize SQL
5372 ** Return the normalized value for an SQL statement.
5374 static int SQLITE_TCLAPI test_normalize(
5375 void * clientData,
5376 Tcl_Interp *interp,
5377 int objc,
5378 Tcl_Obj *CONST objv[]
5380 char *zSql;
5381 char *zNorm;
5382 extern char *sqlite3_normalize(const char*);
5384 if( objc!=2 ){
5385 Tcl_WrongNumArgs(interp, 1, objv, "SQL");
5386 return TCL_ERROR;
5389 zSql = (char*)Tcl_GetString(objv[1]);
5390 zNorm = sqlite3_normalize(zSql);
5391 if( zNorm ){
5392 Tcl_SetObjResult(interp, Tcl_NewStringObj(zNorm, -1));
5393 sqlite3_free(zNorm);
5395 return TCL_OK;
5399 ** Usage: sqlite3_step STMT
5401 ** Advance the statement to the next row.
5403 static int SQLITE_TCLAPI test_step(
5404 void * clientData,
5405 Tcl_Interp *interp,
5406 int objc,
5407 Tcl_Obj *CONST objv[]
5409 sqlite3_stmt *pStmt;
5410 int rc;
5412 if( objc!=2 ){
5413 Tcl_AppendResult(interp, "wrong # args: should be \"",
5414 Tcl_GetString(objv[0]), " STMT", 0);
5415 return TCL_ERROR;
5418 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5419 rc = sqlite3_step(pStmt);
5421 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
5422 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
5423 return TCL_OK;
5426 static int SQLITE_TCLAPI test_sql(
5427 void * clientData,
5428 Tcl_Interp *interp,
5429 int objc,
5430 Tcl_Obj *CONST objv[]
5432 sqlite3_stmt *pStmt;
5434 if( objc!=2 ){
5435 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5436 return TCL_ERROR;
5439 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5440 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
5441 return TCL_OK;
5443 static int SQLITE_TCLAPI test_ex_sql(
5444 void * clientData,
5445 Tcl_Interp *interp,
5446 int objc,
5447 Tcl_Obj *CONST objv[]
5449 sqlite3_stmt *pStmt;
5450 char *z;
5452 if( objc!=2 ){
5453 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5454 return TCL_ERROR;
5457 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5458 z = sqlite3_expanded_sql(pStmt);
5459 Tcl_SetResult(interp, z, TCL_VOLATILE);
5460 sqlite3_free(z);
5461 return TCL_OK;
5463 #ifdef SQLITE_ENABLE_NORMALIZE
5464 static int SQLITE_TCLAPI test_norm_sql(
5465 void * clientData,
5466 Tcl_Interp *interp,
5467 int objc,
5468 Tcl_Obj *CONST objv[]
5470 sqlite3_stmt *pStmt;
5472 if( objc!=2 ){
5473 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5474 return TCL_ERROR;
5477 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5478 Tcl_SetResult(interp, (char *)sqlite3_normalized_sql(pStmt), TCL_VOLATILE);
5479 return TCL_OK;
5481 #endif /* SQLITE_ENABLE_NORMALIZE */
5484 ** Usage: sqlite3_column_count STMT
5486 ** Return the number of columns returned by the sql statement STMT.
5488 static int SQLITE_TCLAPI test_column_count(
5489 void * clientData,
5490 Tcl_Interp *interp,
5491 int objc,
5492 Tcl_Obj *CONST objv[]
5494 sqlite3_stmt *pStmt;
5496 if( objc!=2 ){
5497 Tcl_AppendResult(interp, "wrong # args: should be \"",
5498 Tcl_GetString(objv[0]), " STMT column", 0);
5499 return TCL_ERROR;
5502 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5504 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
5505 return TCL_OK;
5509 ** Usage: sqlite3_column_type STMT column
5511 ** Return the type of the data in column 'column' of the current row.
5513 static int SQLITE_TCLAPI test_column_type(
5514 void * clientData,
5515 Tcl_Interp *interp,
5516 int objc,
5517 Tcl_Obj *CONST objv[]
5519 sqlite3_stmt *pStmt;
5520 int col;
5521 int tp;
5523 if( objc!=3 ){
5524 Tcl_AppendResult(interp, "wrong # args: should be \"",
5525 Tcl_GetString(objv[0]), " STMT column", 0);
5526 return TCL_ERROR;
5529 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5530 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5532 tp = sqlite3_column_type(pStmt, col);
5533 switch( tp ){
5534 case SQLITE_INTEGER:
5535 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
5536 break;
5537 case SQLITE_NULL:
5538 Tcl_SetResult(interp, "NULL", TCL_STATIC);
5539 break;
5540 case SQLITE_FLOAT:
5541 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
5542 break;
5543 case SQLITE_TEXT:
5544 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
5545 break;
5546 case SQLITE_BLOB:
5547 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
5548 break;
5549 default:
5550 assert(0);
5553 return TCL_OK;
5557 ** Usage: sqlite3_column_int64 STMT column
5559 ** Return the data in column 'column' of the current row cast as an
5560 ** wide (64-bit) integer.
5562 static int SQLITE_TCLAPI test_column_int64(
5563 void * clientData,
5564 Tcl_Interp *interp,
5565 int objc,
5566 Tcl_Obj *CONST objv[]
5568 sqlite3_stmt *pStmt;
5569 int col;
5570 i64 iVal;
5572 if( objc!=3 ){
5573 Tcl_AppendResult(interp, "wrong # args: should be \"",
5574 Tcl_GetString(objv[0]), " STMT column", 0);
5575 return TCL_ERROR;
5578 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5579 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5581 iVal = sqlite3_column_int64(pStmt, col);
5582 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
5583 return TCL_OK;
5587 ** Usage: sqlite3_column_blob STMT column
5589 static int SQLITE_TCLAPI test_column_blob(
5590 void * clientData,
5591 Tcl_Interp *interp,
5592 int objc,
5593 Tcl_Obj *CONST objv[]
5595 sqlite3_stmt *pStmt;
5596 int col;
5598 int len;
5599 const void *pBlob;
5601 if( objc!=3 ){
5602 Tcl_AppendResult(interp, "wrong # args: should be \"",
5603 Tcl_GetString(objv[0]), " STMT column", 0);
5604 return TCL_ERROR;
5607 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5608 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5610 len = sqlite3_column_bytes(pStmt, col);
5611 pBlob = sqlite3_column_blob(pStmt, col);
5612 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
5613 return TCL_OK;
5617 ** Usage: sqlite3_column_double STMT column
5619 ** Return the data in column 'column' of the current row cast as a double.
5621 static int SQLITE_TCLAPI test_column_double(
5622 void * clientData,
5623 Tcl_Interp *interp,
5624 int objc,
5625 Tcl_Obj *CONST objv[]
5627 sqlite3_stmt *pStmt;
5628 int col;
5629 double rVal;
5631 if( objc!=3 ){
5632 Tcl_AppendResult(interp, "wrong # args: should be \"",
5633 Tcl_GetString(objv[0]), " STMT column", 0);
5634 return TCL_ERROR;
5637 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5638 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5640 rVal = sqlite3_column_double(pStmt, col);
5641 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
5642 return TCL_OK;
5646 ** Usage: sqlite3_data_count STMT
5648 ** Return the number of columns returned by the sql statement STMT.
5650 static int SQLITE_TCLAPI test_data_count(
5651 void * clientData,
5652 Tcl_Interp *interp,
5653 int objc,
5654 Tcl_Obj *CONST objv[]
5656 sqlite3_stmt *pStmt;
5658 if( objc!=2 ){
5659 Tcl_AppendResult(interp, "wrong # args: should be \"",
5660 Tcl_GetString(objv[0]), " STMT column", 0);
5661 return TCL_ERROR;
5664 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5666 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
5667 return TCL_OK;
5671 ** Usage: sqlite3_column_text STMT column
5673 ** Usage: sqlite3_column_decltype STMT column
5675 ** Usage: sqlite3_column_name STMT column
5677 static int SQLITE_TCLAPI test_stmt_utf8(
5678 void * clientData, /* Pointer to SQLite API function to be invoke */
5679 Tcl_Interp *interp,
5680 int objc,
5681 Tcl_Obj *CONST objv[]
5683 sqlite3_stmt *pStmt;
5684 int col;
5685 const char *(*xFunc)(sqlite3_stmt*, int);
5686 const char *zRet;
5688 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
5689 if( objc!=3 ){
5690 Tcl_AppendResult(interp, "wrong # args: should be \"",
5691 Tcl_GetString(objv[0]), " STMT column", 0);
5692 return TCL_ERROR;
5695 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5696 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5697 zRet = xFunc(pStmt, col);
5698 if( zRet ){
5699 Tcl_SetResult(interp, (char *)zRet, 0);
5701 return TCL_OK;
5704 static int SQLITE_TCLAPI test_global_recover(
5705 void * clientData,
5706 Tcl_Interp *interp,
5707 int objc,
5708 Tcl_Obj *CONST objv[]
5710 #ifndef SQLITE_OMIT_DEPRECATED
5711 int rc;
5712 if( objc!=1 ){
5713 Tcl_WrongNumArgs(interp, 1, objv, "");
5714 return TCL_ERROR;
5716 rc = sqlite3_global_recover();
5717 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5718 #endif
5719 return TCL_OK;
5723 ** Usage: sqlite3_column_text STMT column
5725 ** Usage: sqlite3_column_decltype STMT column
5727 ** Usage: sqlite3_column_name STMT column
5729 static int SQLITE_TCLAPI test_stmt_utf16(
5730 void * clientData, /* Pointer to SQLite API function to be invoked */
5731 Tcl_Interp *interp,
5732 int objc,
5733 Tcl_Obj *CONST objv[]
5735 #ifndef SQLITE_OMIT_UTF16
5736 sqlite3_stmt *pStmt;
5737 int col;
5738 Tcl_Obj *pRet;
5739 const void *zName16;
5740 const void *(*xFunc)(sqlite3_stmt*, int);
5742 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
5743 if( objc!=3 ){
5744 Tcl_AppendResult(interp, "wrong # args: should be \"",
5745 Tcl_GetString(objv[0]), " STMT column", 0);
5746 return TCL_ERROR;
5749 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5750 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5752 zName16 = xFunc(pStmt, col);
5753 if( zName16 ){
5754 int n;
5755 const char *z = zName16;
5756 for(n=0; z[n] || z[n+1]; n+=2){}
5757 pRet = Tcl_NewByteArrayObj(zName16, n+2);
5758 Tcl_SetObjResult(interp, pRet);
5760 #endif /* SQLITE_OMIT_UTF16 */
5762 return TCL_OK;
5766 ** Usage: sqlite3_column_int STMT column
5768 ** Usage: sqlite3_column_bytes STMT column
5770 ** Usage: sqlite3_column_bytes16 STMT column
5773 static int SQLITE_TCLAPI test_stmt_int(
5774 void * clientData, /* Pointer to SQLite API function to be invoked */
5775 Tcl_Interp *interp,
5776 int objc,
5777 Tcl_Obj *CONST objv[]
5779 sqlite3_stmt *pStmt;
5780 int col;
5781 int (*xFunc)(sqlite3_stmt*, int);
5783 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
5784 if( objc!=3 ){
5785 Tcl_AppendResult(interp, "wrong # args: should be \"",
5786 Tcl_GetString(objv[0]), " STMT column", 0);
5787 return TCL_ERROR;
5790 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5791 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
5793 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
5794 return TCL_OK;
5798 ** Usage: sqlite3_interrupt DB
5800 ** Trigger an interrupt on DB
5802 static int SQLITE_TCLAPI test_interrupt(
5803 void * clientData,
5804 Tcl_Interp *interp,
5805 int argc,
5806 char **argv
5808 sqlite3 *db;
5809 if( argc!=2 ){
5810 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5811 return TCL_ERROR;
5813 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5814 sqlite3_interrupt(db);
5815 return TCL_OK;
5819 ** Usage: sqlite3_is_interrupted DB
5821 ** return true if an interrupt is current in effect on DB
5823 static int SQLITE_TCLAPI test_is_interrupted(
5824 void * clientData,
5825 Tcl_Interp *interp,
5826 int argc,
5827 char **argv
5829 sqlite3 *db;
5830 int rc;
5831 if( argc!=2 ){
5832 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
5833 return TCL_ERROR;
5835 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5836 rc = sqlite3_is_interrupted(db);
5837 Tcl_AppendResult(interp, rc ? "1" : "0", (void*)0);
5838 return TCL_OK;
5842 ** Usage: sqlite_delete_function DB function-name
5844 ** Delete the user function 'function-name' from database handle DB. It
5845 ** is assumed that the user function was created as UTF8, any number of
5846 ** arguments (the way the TCL interface does it).
5848 static int SQLITE_TCLAPI delete_function(
5849 void * clientData,
5850 Tcl_Interp *interp,
5851 int argc,
5852 char **argv
5854 int rc;
5855 sqlite3 *db;
5856 if( argc!=3 ){
5857 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5858 " DB function-name", 0);
5859 return TCL_ERROR;
5861 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5862 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
5863 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5864 return TCL_OK;
5868 ** Usage: sqlite_delete_collation DB collation-name
5870 ** Delete the collation sequence 'collation-name' from database handle
5871 ** DB. It is assumed that the collation sequence was created as UTF8 (the
5872 ** way the TCL interface does it).
5874 static int SQLITE_TCLAPI delete_collation(
5875 void * clientData,
5876 Tcl_Interp *interp,
5877 int argc,
5878 char **argv
5880 int rc;
5881 sqlite3 *db;
5882 if( argc!=3 ){
5883 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5884 " DB function-name", 0);
5885 return TCL_ERROR;
5887 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5888 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
5889 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5890 return TCL_OK;
5894 ** Usage: sqlite3_get_autocommit DB
5896 ** Return true if the database DB is currently in auto-commit mode.
5897 ** Return false if not.
5899 static int SQLITE_TCLAPI get_autocommit(
5900 void * clientData,
5901 Tcl_Interp *interp,
5902 int argc,
5903 char **argv
5905 char zBuf[30];
5906 sqlite3 *db;
5907 if( argc!=2 ){
5908 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5909 " DB", 0);
5910 return TCL_ERROR;
5912 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5913 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db));
5914 Tcl_AppendResult(interp, zBuf, 0);
5915 return TCL_OK;
5919 ** Usage: sqlite3_busy_timeout DB MS
5921 ** Set the busy timeout. This is more easily done using the timeout
5922 ** method of the TCL interface. But we need a way to test the case
5923 ** where it returns SQLITE_MISUSE.
5925 static int SQLITE_TCLAPI test_busy_timeout(
5926 void * clientData,
5927 Tcl_Interp *interp,
5928 int argc,
5929 char **argv
5931 int rc, ms;
5932 sqlite3 *db;
5933 if( argc!=3 ){
5934 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5935 " DB", 0);
5936 return TCL_ERROR;
5938 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5939 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
5940 rc = sqlite3_busy_timeout(db, ms);
5941 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
5942 return TCL_OK;
5946 ** Usage: tcl_variable_type VARIABLENAME
5948 ** Return the name of the internal representation for the
5949 ** value of the given variable.
5951 static int SQLITE_TCLAPI tcl_variable_type(
5952 void * clientData,
5953 Tcl_Interp *interp,
5954 int objc,
5955 Tcl_Obj *CONST objv[]
5957 Tcl_Obj *pVar;
5958 if( objc!=2 ){
5959 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
5960 return TCL_ERROR;
5962 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
5963 if( pVar==0 ) return TCL_ERROR;
5964 if( pVar->typePtr ){
5965 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
5967 return TCL_OK;
5971 ** Usage: sqlite3_release_memory ?N?
5973 ** Attempt to release memory currently held but not actually required.
5974 ** The integer N is the number of bytes we are trying to release. The
5975 ** return value is the amount of memory actually released.
5977 static int SQLITE_TCLAPI test_release_memory(
5978 void * clientData,
5979 Tcl_Interp *interp,
5980 int objc,
5981 Tcl_Obj *CONST objv[]
5983 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
5984 int N;
5985 int amt;
5986 if( objc!=1 && objc!=2 ){
5987 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
5988 return TCL_ERROR;
5990 if( objc==2 ){
5991 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
5992 }else{
5993 N = -1;
5995 amt = sqlite3_release_memory(N);
5996 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
5997 #endif
5998 return TCL_OK;
6003 ** Usage: sqlite3_db_release_memory DB
6005 ** Attempt to release memory currently held by database DB. Return the
6006 ** result code (which in the current implementation is always zero).
6008 static int SQLITE_TCLAPI test_db_release_memory(
6009 void * clientData,
6010 Tcl_Interp *interp,
6011 int objc,
6012 Tcl_Obj *CONST objv[]
6014 sqlite3 *db;
6015 int rc;
6016 if( objc!=2 ){
6017 Tcl_WrongNumArgs(interp, 1, objv, "DB");
6018 return TCL_ERROR;
6020 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6021 rc = sqlite3_db_release_memory(db);
6022 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6023 return TCL_OK;
6027 ** Usage: sqlite3_db_cacheflush DB
6029 ** Attempt to flush any dirty pages to disk.
6031 static int SQLITE_TCLAPI test_db_cacheflush(
6032 void * clientData,
6033 Tcl_Interp *interp,
6034 int objc,
6035 Tcl_Obj *CONST objv[]
6037 sqlite3 *db;
6038 int rc;
6039 if( objc!=2 ){
6040 Tcl_WrongNumArgs(interp, 1, objv, "DB");
6041 return TCL_ERROR;
6043 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6044 rc = sqlite3_db_cacheflush(db);
6045 if( rc ){
6046 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
6047 return TCL_ERROR;
6050 Tcl_ResetResult(interp);
6051 return TCL_OK;
6055 ** Usage: sqlite3_system_errno DB
6057 ** Return the low-level system errno value.
6059 static int SQLITE_TCLAPI test_system_errno(
6060 void * clientData,
6061 Tcl_Interp *interp,
6062 int objc,
6063 Tcl_Obj *CONST objv[]
6065 sqlite3 *db;
6066 int iErrno;
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 iErrno = sqlite3_system_errno(db);
6073 Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno));
6074 return TCL_OK;
6078 ** Usage: sqlite3_db_filename DB DBNAME
6080 ** Return the name of a file associated with a database.
6082 static int SQLITE_TCLAPI test_db_filename(
6083 void * clientData,
6084 Tcl_Interp *interp,
6085 int objc,
6086 Tcl_Obj *CONST objv[]
6088 sqlite3 *db;
6089 const char *zDbName;
6090 if( objc!=3 ){
6091 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
6092 return TCL_ERROR;
6094 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6095 zDbName = Tcl_GetString(objv[2]);
6096 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
6097 return TCL_OK;
6101 ** Usage: sqlite3_db_readonly DB DBNAME
6103 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does
6104 ** not exist.
6106 static int SQLITE_TCLAPI test_db_readonly(
6107 void * clientData,
6108 Tcl_Interp *interp,
6109 int objc,
6110 Tcl_Obj *CONST objv[]
6112 sqlite3 *db;
6113 const char *zDbName;
6114 if( objc!=3 ){
6115 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
6116 return TCL_ERROR;
6118 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6119 zDbName = Tcl_GetString(objv[2]);
6120 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
6121 return TCL_OK;
6125 ** Usage: sqlite3_soft_heap_limit ?N?
6127 ** Query or set the soft heap limit for the current thread. The
6128 ** limit is only changed if the N is present. The previous limit
6129 ** is returned.
6131 static int SQLITE_TCLAPI test_soft_heap_limit(
6132 void * clientData,
6133 Tcl_Interp *interp,
6134 int objc,
6135 Tcl_Obj *CONST objv[]
6137 sqlite3_int64 amt;
6138 Tcl_WideInt N = -1;
6139 if( objc!=1 && objc!=2 ){
6140 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
6141 return TCL_ERROR;
6143 if( objc==2 ){
6144 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
6146 amt = sqlite3_soft_heap_limit64(N);
6147 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
6148 return TCL_OK;
6152 ** Usage: sqlite3_hard_heap_limit ?N?
6154 ** Query or set the hard heap limit for the current thread. The
6155 ** limit is only changed if the N is present. The previous limit
6156 ** is returned.
6158 static int SQLITE_TCLAPI test_hard_heap_limit(
6159 void * clientData,
6160 Tcl_Interp *interp,
6161 int objc,
6162 Tcl_Obj *CONST objv[]
6164 sqlite3_int64 amt;
6165 Tcl_WideInt N = -1;
6166 if( objc!=1 && objc!=2 ){
6167 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
6168 return TCL_ERROR;
6170 if( objc==2 ){
6171 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
6173 amt = sqlite3_hard_heap_limit64(N);
6174 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
6175 return TCL_OK;
6179 ** Usage: sqlite3_thread_cleanup
6181 ** Call the sqlite3_thread_cleanup API.
6183 static int SQLITE_TCLAPI test_thread_cleanup(
6184 void * clientData,
6185 Tcl_Interp *interp,
6186 int objc,
6187 Tcl_Obj *CONST objv[]
6189 #ifndef SQLITE_OMIT_DEPRECATED
6190 sqlite3_thread_cleanup();
6191 #endif
6192 return TCL_OK;
6196 ** Usage: sqlite3_pager_refcounts DB
6198 ** Return a list of numbers which are the PagerRefcount for all
6199 ** pagers on each database connection.
6201 static int SQLITE_TCLAPI test_pager_refcounts(
6202 void * clientData,
6203 Tcl_Interp *interp,
6204 int objc,
6205 Tcl_Obj *CONST objv[]
6207 sqlite3 *db;
6208 int i;
6209 int v, *a;
6210 Tcl_Obj *pResult;
6212 if( objc!=2 ){
6213 Tcl_AppendResult(interp, "wrong # args: should be \"",
6214 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6215 return TCL_ERROR;
6217 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6218 pResult = Tcl_NewObj();
6219 for(i=0; i<db->nDb; i++){
6220 if( db->aDb[i].pBt==0 ){
6221 v = -1;
6222 }else{
6223 sqlite3_mutex_enter(db->mutex);
6224 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
6225 v = a[0];
6226 sqlite3_mutex_leave(db->mutex);
6228 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
6230 Tcl_SetObjResult(interp, pResult);
6231 return TCL_OK;
6236 ** tclcmd: working_64bit_int
6238 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
6239 ** leads to a number of test failures. The present command checks the
6240 ** TCL build to see whether or not it supports 64-bit integers. It
6241 ** returns TRUE if it does and FALSE if not.
6243 ** This command is used to warn users that their TCL build is defective
6244 ** and that the errors they are seeing in the test scripts might be
6245 ** a result of their defective TCL rather than problems in SQLite.
6247 static int SQLITE_TCLAPI working_64bit_int(
6248 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6249 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6250 int objc, /* Number of arguments */
6251 Tcl_Obj *CONST objv[] /* Command arguments */
6253 Tcl_Obj *pTestObj;
6254 int working = 0;
6256 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
6257 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
6258 Tcl_DecrRefCount(pTestObj);
6259 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
6260 return TCL_OK;
6265 ** tclcmd: vfs_unlink_test
6267 ** This TCL command unregisters the primary VFS and then registers
6268 ** it back again. This is used to test the ability to register a
6269 ** VFS when none are previously registered, and the ability to
6270 ** unregister the only available VFS. Ticket #2738
6272 static int SQLITE_TCLAPI vfs_unlink_test(
6273 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6274 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6275 int objc, /* Number of arguments */
6276 Tcl_Obj *CONST objv[] /* Command arguments */
6278 int i;
6279 sqlite3_vfs *pMain;
6280 sqlite3_vfs *apVfs[20];
6281 sqlite3_vfs one, two;
6283 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
6284 one.zName = "__one";
6285 two.zName = "__two";
6287 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
6288 ** change the default VFS
6290 pMain = sqlite3_vfs_find(0);
6291 sqlite3_vfs_register(&one, 0);
6292 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
6293 sqlite3_vfs_register(&two, 0);
6294 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
6296 /* We can find a VFS by its name */
6297 assert( sqlite3_vfs_find("__one")==&one );
6298 assert( sqlite3_vfs_find("__two")==&two );
6300 /* Calling sqlite_vfs_register with non-zero second parameter changes the
6301 ** default VFS, even if the 1st parameter is an existing VFS that is
6302 ** previously registered as the non-default.
6304 sqlite3_vfs_register(&one, 1);
6305 assert( sqlite3_vfs_find("__one")==&one );
6306 assert( sqlite3_vfs_find("__two")==&two );
6307 assert( sqlite3_vfs_find(0)==&one );
6308 sqlite3_vfs_register(&two, 1);
6309 assert( sqlite3_vfs_find("__one")==&one );
6310 assert( sqlite3_vfs_find("__two")==&two );
6311 assert( sqlite3_vfs_find(0)==&two );
6312 if( pMain ){
6313 sqlite3_vfs_register(pMain, 1);
6314 assert( sqlite3_vfs_find("__one")==&one );
6315 assert( sqlite3_vfs_find("__two")==&two );
6316 assert( sqlite3_vfs_find(0)==pMain );
6319 /* Unlink the default VFS. Repeat until there are no more VFSes
6320 ** registered.
6322 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
6323 apVfs[i] = sqlite3_vfs_find(0);
6324 if( apVfs[i] ){
6325 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
6326 sqlite3_vfs_unregister(apVfs[i]);
6327 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
6330 assert( 0==sqlite3_vfs_find(0) );
6332 /* Register the main VFS as non-default (will be made default, since
6333 ** it'll be the only one in existence).
6335 sqlite3_vfs_register(pMain, 0);
6336 assert( sqlite3_vfs_find(0)==pMain );
6338 /* Un-register the main VFS again to restore an empty VFS list */
6339 sqlite3_vfs_unregister(pMain);
6340 assert( 0==sqlite3_vfs_find(0) );
6342 /* Relink all VFSes in reverse order. */
6343 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
6344 if( apVfs[i] ){
6345 sqlite3_vfs_register(apVfs[i], 1);
6346 assert( apVfs[i]==sqlite3_vfs_find(0) );
6347 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
6351 /* Unregister out sample VFSes. */
6352 sqlite3_vfs_unregister(&one);
6353 sqlite3_vfs_unregister(&two);
6355 /* Unregistering a VFS that is not currently registered is harmless */
6356 sqlite3_vfs_unregister(&one);
6357 sqlite3_vfs_unregister(&two);
6358 assert( sqlite3_vfs_find("__one")==0 );
6359 assert( sqlite3_vfs_find("__two")==0 );
6361 /* We should be left with the original default VFS back as the
6362 ** original */
6363 assert( sqlite3_vfs_find(0)==pMain );
6365 return TCL_OK;
6369 ** tclcmd: vfs_initfail_test
6371 ** This TCL command attempts to vfs_find and vfs_register when the
6372 ** sqlite3_initialize() interface is failing. All calls should fail.
6374 static int SQLITE_TCLAPI vfs_initfail_test(
6375 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6376 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6377 int objc, /* Number of arguments */
6378 Tcl_Obj *CONST objv[] /* Command arguments */
6380 sqlite3_vfs one;
6381 one.zName = "__one";
6383 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6384 sqlite3_vfs_register(&one, 0);
6385 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6386 sqlite3_vfs_register(&one, 1);
6387 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
6388 return TCL_OK;
6392 ** Saved VFSes
6394 static sqlite3_vfs *apVfs[20];
6395 static int nVfs = 0;
6398 ** tclcmd: vfs_unregister_all
6400 ** Unregister all VFSes.
6402 static int SQLITE_TCLAPI vfs_unregister_all(
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 int i;
6409 for(i=0; i<ArraySize(apVfs); i++){
6410 apVfs[i] = sqlite3_vfs_find(0);
6411 if( apVfs[i]==0 ) break;
6412 sqlite3_vfs_unregister(apVfs[i]);
6414 nVfs = i;
6415 return TCL_OK;
6418 ** tclcmd: vfs_reregister_all
6420 ** Restore all VFSes that were removed using vfs_unregister_all. Taking
6421 ** care to put the linked list back together in the same order as it was
6422 ** in before vfs_unregister_all was invoked.
6424 static int SQLITE_TCLAPI vfs_reregister_all(
6425 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6426 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6427 int objc, /* Number of arguments */
6428 Tcl_Obj *CONST objv[] /* Command arguments */
6430 int i;
6431 for(i=nVfs-1; i>=0; i--){
6432 sqlite3_vfs_register(apVfs[i], 1);
6434 return TCL_OK;
6439 ** tclcmd: file_control_test DB
6441 ** This TCL command runs the sqlite3_file_control interface and
6442 ** verifies correct operation of the same.
6444 static int SQLITE_TCLAPI file_control_test(
6445 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6446 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6447 int objc, /* Number of arguments */
6448 Tcl_Obj *CONST objv[] /* Command arguments */
6450 int iArg = 0;
6451 sqlite3 *db;
6452 int rc;
6454 if( objc!=2 ){
6455 Tcl_AppendResult(interp, "wrong # args: should be \"",
6456 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6457 return TCL_ERROR;
6459 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6460 rc = sqlite3_file_control(db, 0, 0, &iArg);
6461 assert( rc==SQLITE_NOTFOUND );
6462 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
6463 assert( rc==SQLITE_ERROR );
6464 rc = sqlite3_file_control(db, "main", -1, &iArg);
6465 assert( rc==SQLITE_NOTFOUND );
6466 rc = sqlite3_file_control(db, "temp", -1, &iArg);
6467 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
6469 return TCL_OK;
6474 ** tclcmd: file_control_lasterrno_test DB
6476 ** This TCL command runs the sqlite3_file_control interface and
6477 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
6479 static int SQLITE_TCLAPI file_control_lasterrno_test(
6480 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6481 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6482 int objc, /* Number of arguments */
6483 Tcl_Obj *CONST objv[] /* Command arguments */
6485 int iArg = 0;
6486 sqlite3 *db;
6487 int rc;
6489 if( objc!=2 ){
6490 Tcl_AppendResult(interp, "wrong # args: should be \"",
6491 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6492 return TCL_ERROR;
6494 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6495 return TCL_ERROR;
6497 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
6498 if( rc ){
6499 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6500 return TCL_ERROR;
6502 if( iArg!=0 ) {
6503 Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
6504 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
6505 return TCL_ERROR;
6507 return TCL_OK;
6511 ** tclcmd: file_control_data_version DB DBNAME
6513 ** This TCL command runs the sqlite3_file_control with the
6514 ** SQLITE_FCNTL_DATA_VERSION opcode, returning the result.
6516 static int SQLITE_TCLAPI file_control_data_version(
6517 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6518 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6519 int objc, /* Number of arguments */
6520 Tcl_Obj *CONST objv[] /* Command arguments */
6522 unsigned int iVers; /* data version */
6523 char *zDb; /* Db name ("main", "temp" etc.) */
6524 sqlite3 *db; /* Database handle */
6525 int rc; /* file_control() return code */
6526 char zBuf[100];
6528 if( objc!=3 && objc!=2 ){
6529 Tcl_WrongNumArgs(interp, 1, objv, "DB [DBNAME]");
6530 return TCL_ERROR;
6532 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6533 return TCL_ERROR;
6535 zDb = objc==3 ? Tcl_GetString(objv[2]) : NULL;
6537 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_DATA_VERSION, (void *)&iVers);
6538 if( rc ){
6539 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6540 return TCL_ERROR;
6541 }else{
6542 sqlite3_snprintf(sizeof(zBuf),zBuf,"%u",iVers);
6543 Tcl_SetResult(interp, (char *)zBuf, TCL_VOLATILE);
6544 return TCL_OK;
6549 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
6551 ** This TCL command runs the sqlite3_file_control interface and
6552 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6553 ** SQLITE_SET_LOCKPROXYFILE verbs.
6555 static int SQLITE_TCLAPI file_control_chunksize_test(
6556 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6557 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6558 int objc, /* Number of arguments */
6559 Tcl_Obj *CONST objv[] /* Command arguments */
6561 int nSize; /* New chunk size */
6562 char *zDb; /* Db name ("main", "temp" etc.) */
6563 sqlite3 *db; /* Database handle */
6564 int rc; /* file_control() return code */
6566 if( objc!=4 ){
6567 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
6568 return TCL_ERROR;
6570 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6571 || Tcl_GetIntFromObj(interp, objv[3], &nSize)
6573 return TCL_ERROR;
6575 zDb = Tcl_GetString(objv[2]);
6576 if( zDb[0]=='\0' ) zDb = NULL;
6578 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
6579 if( rc ){
6580 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6581 return TCL_ERROR;
6583 return TCL_OK;
6587 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
6589 ** This TCL command runs the sqlite3_file_control interface
6590 ** with SQLITE_FCNTL_SIZE_HINT
6592 static int SQLITE_TCLAPI file_control_sizehint_test(
6593 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6594 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6595 int objc, /* Number of arguments */
6596 Tcl_Obj *CONST objv[] /* Command arguments */
6598 Tcl_WideInt nSize; /* Hinted size */
6599 char *zDb; /* Db name ("main", "temp" etc.) */
6600 sqlite3 *db; /* Database handle */
6601 int rc; /* file_control() return code */
6603 if( objc!=4 ){
6604 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
6605 return TCL_ERROR;
6607 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6608 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
6610 return TCL_ERROR;
6612 zDb = Tcl_GetString(objv[2]);
6613 if( zDb[0]=='\0' ) zDb = NULL;
6615 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
6616 if( rc ){
6617 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
6618 return TCL_ERROR;
6620 return TCL_OK;
6624 ** tclcmd: file_control_lockproxy_test DB PWD
6626 ** This TCL command runs the sqlite3_file_control interface and
6627 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6628 ** SQLITE_SET_LOCKPROXYFILE verbs.
6630 static int SQLITE_TCLAPI file_control_lockproxy_test(
6631 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6632 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6633 int objc, /* Number of arguments */
6634 Tcl_Obj *CONST objv[] /* Command arguments */
6636 sqlite3 *db;
6638 if( objc!=3 ){
6639 Tcl_AppendResult(interp, "wrong # args: should be \"",
6640 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
6641 return TCL_ERROR;
6643 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6644 return TCL_ERROR;
6647 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
6648 # if defined(__APPLE__)
6649 # define SQLITE_ENABLE_LOCKING_STYLE 1
6650 # else
6651 # define SQLITE_ENABLE_LOCKING_STYLE 0
6652 # endif
6653 #endif
6654 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
6656 char *testPath;
6657 int rc;
6658 int nPwd;
6659 const char *zPwd;
6660 char proxyPath[400];
6662 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
6663 if( sizeof(proxyPath)<nPwd+20 ){
6664 Tcl_AppendResult(interp, "PWD too big", (void*)0);
6665 return TCL_ERROR;
6667 sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd);
6668 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
6669 if( rc ){
6670 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6671 return TCL_ERROR;
6673 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
6674 if( strncmp(proxyPath,testPath,11) ){
6675 Tcl_AppendResult(interp, "Lock proxy file did not match the "
6676 "previously assigned value", 0);
6677 return TCL_ERROR;
6679 if( rc ){
6680 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6681 return TCL_ERROR;
6683 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
6684 if( rc ){
6685 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
6686 return TCL_ERROR;
6689 #endif
6690 return TCL_OK;
6693 #if SQLITE_OS_WIN
6695 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY
6697 ** This TCL command runs the sqlite3_file_control interface with
6698 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
6700 static int SQLITE_TCLAPI file_control_win32_av_retry(
6701 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6702 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6703 int objc, /* Number of arguments */
6704 Tcl_Obj *CONST objv[] /* Command arguments */
6706 sqlite3 *db;
6707 int rc;
6708 int a[2];
6709 char z[100];
6711 if( objc!=4 ){
6712 Tcl_AppendResult(interp, "wrong # args: should be \"",
6713 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
6714 return TCL_ERROR;
6716 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6717 return TCL_ERROR;
6719 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
6720 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
6721 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
6722 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
6723 Tcl_AppendResult(interp, z, (char*)0);
6724 return TCL_OK;
6728 ** tclcmd: file_control_win32_get_handle DB
6730 ** This TCL command runs the sqlite3_file_control interface with
6731 ** the SQLITE_FCNTL_WIN32_GET_HANDLE opcode.
6733 static int file_control_win32_get_handle(
6734 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6735 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6736 int objc, /* Number of arguments */
6737 Tcl_Obj *CONST objv[] /* Command arguments */
6739 sqlite3 *db;
6740 int rc;
6741 HANDLE hFile = NULL;
6742 char z[100];
6744 if( objc!=2 ){
6745 Tcl_AppendResult(interp, "wrong # args: should be \"",
6746 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
6747 return TCL_ERROR;
6749 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6750 return TCL_ERROR;
6752 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_GET_HANDLE,
6753 (void*)&hFile);
6754 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
6755 Tcl_AppendResult(interp, z, (char*)0);
6756 return TCL_OK;
6760 ** tclcmd: file_control_win32_set_handle DB HANDLE
6762 ** This TCL command runs the sqlite3_file_control interface with
6763 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
6765 static int SQLITE_TCLAPI file_control_win32_set_handle(
6766 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6767 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6768 int objc, /* Number of arguments */
6769 Tcl_Obj *CONST objv[] /* Command arguments */
6771 sqlite3 *db;
6772 int rc;
6773 HANDLE hFile = NULL;
6774 char z[100];
6776 if( objc!=3 ){
6777 Tcl_AppendResult(interp, "wrong # args: should be \"",
6778 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0);
6779 return TCL_ERROR;
6781 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6782 return TCL_ERROR;
6784 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){
6785 return TCL_ERROR;
6787 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE,
6788 (void*)&hFile);
6789 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
6790 Tcl_AppendResult(interp, z, (char*)0);
6791 return TCL_OK;
6793 #endif
6796 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG
6798 ** This TCL command runs the sqlite3_file_control interface with
6799 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
6801 static int SQLITE_TCLAPI file_control_persist_wal(
6802 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6803 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6804 int objc, /* Number of arguments */
6805 Tcl_Obj *CONST objv[] /* Command arguments */
6807 sqlite3 *db;
6808 int rc;
6809 int bPersist;
6810 char z[100];
6812 if( objc!=3 ){
6813 Tcl_AppendResult(interp, "wrong # args: should be \"",
6814 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
6815 return TCL_ERROR;
6817 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6818 return TCL_ERROR;
6820 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
6821 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
6822 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
6823 Tcl_AppendResult(interp, z, (char*)0);
6824 return TCL_OK;
6828 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG
6830 ** This TCL command runs the sqlite3_file_control interface with
6831 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
6833 static int SQLITE_TCLAPI file_control_powersafe_overwrite(
6834 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6835 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6836 int objc, /* Number of arguments */
6837 Tcl_Obj *CONST objv[] /* Command arguments */
6839 sqlite3 *db;
6840 int rc;
6841 int b;
6842 char z[100];
6844 if( objc!=3 ){
6845 Tcl_AppendResult(interp, "wrong # args: should be \"",
6846 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
6847 return TCL_ERROR;
6849 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6850 return TCL_ERROR;
6852 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
6853 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
6854 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
6855 Tcl_AppendResult(interp, z, (char*)0);
6856 return TCL_OK;
6861 ** tclcmd: file_control_vfsname DB ?AUXDB?
6863 ** Return a string that describes the stack of VFSes.
6865 static int SQLITE_TCLAPI file_control_vfsname(
6866 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6867 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6868 int objc, /* Number of arguments */
6869 Tcl_Obj *CONST objv[] /* Command arguments */
6871 sqlite3 *db;
6872 const char *zDbName = "main";
6873 char *zVfsName = 0;
6875 if( objc!=2 && objc!=3 ){
6876 Tcl_AppendResult(interp, "wrong # args: should be \"",
6877 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6878 return TCL_ERROR;
6880 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6881 return TCL_ERROR;
6883 if( objc==3 ){
6884 zDbName = Tcl_GetString(objv[2]);
6886 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
6887 Tcl_AppendResult(interp, zVfsName, (char*)0);
6888 sqlite3_free(zVfsName);
6889 return TCL_OK;
6893 ** tclcmd: file_control_reservebytes DB N
6895 static int SQLITE_TCLAPI file_control_reservebytes(
6896 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6897 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6898 int objc, /* Number of arguments */
6899 Tcl_Obj *CONST objv[] /* Command arguments */
6901 sqlite3 *db;
6902 const char *zDbName = "main";
6903 int n = 0;
6904 int rc;
6906 if( objc!=3 ){
6907 Tcl_WrongNumArgs(interp, 1, objv, "DB N");
6908 return TCL_ERROR;
6910 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
6911 || Tcl_GetIntFromObj(interp, objv[2], &n)
6913 return TCL_ERROR;
6916 rc = sqlite3_file_control(db, zDbName, SQLITE_FCNTL_RESERVE_BYTES, (void*)&n);
6917 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
6918 return TCL_OK;
6923 ** tclcmd: file_control_tempfilename DB ?AUXDB?
6925 ** Return a string that is a temporary filename
6927 static int SQLITE_TCLAPI file_control_tempfilename(
6928 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6929 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6930 int objc, /* Number of arguments */
6931 Tcl_Obj *CONST objv[] /* Command arguments */
6933 sqlite3 *db;
6934 const char *zDbName = "main";
6935 char *zTName = 0;
6937 if( objc!=2 && objc!=3 ){
6938 Tcl_AppendResult(interp, "wrong # args: should be \"",
6939 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6940 return TCL_ERROR;
6942 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6943 return TCL_ERROR;
6945 if( objc==3 ){
6946 zDbName = Tcl_GetString(objv[2]);
6948 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
6949 Tcl_AppendResult(interp, zTName, (char*)0);
6950 sqlite3_free(zTName);
6951 return TCL_OK;
6955 ** tclcmd: file_control_external_reader DB ?AUXDB?
6957 ** Return a string that is a temporary filename
6959 static int SQLITE_TCLAPI file_control_external_reader(
6960 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6961 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6962 int objc, /* Number of arguments */
6963 Tcl_Obj *CONST objv[] /* Command arguments */
6965 sqlite3 *db;
6966 const char *zName = "main";
6967 int iRes = 0;
6968 int rc = SQLITE_OK;
6970 if( objc!=2 && objc!=3 ){
6971 Tcl_AppendResult(interp, "wrong # args: should be \"",
6972 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
6973 return TCL_ERROR;
6975 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6976 return TCL_ERROR;
6978 if( objc==3 ){
6979 zName = Tcl_GetString(objv[2]);
6981 rc = sqlite3_file_control(db, zName, SQLITE_FCNTL_EXTERNAL_READER, &iRes);
6982 if( rc!=SQLITE_OK ){
6983 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6984 return TCL_ERROR;
6986 Tcl_SetObjResult(interp, Tcl_NewIntObj(iRes));
6987 return TCL_OK;
6992 ** tclcmd: sqlite3_vfs_list
6994 ** Return a tcl list containing the names of all registered vfs's.
6996 static int SQLITE_TCLAPI vfs_list(
6997 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
6998 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
6999 int objc, /* Number of arguments */
7000 Tcl_Obj *CONST objv[] /* Command arguments */
7002 sqlite3_vfs *pVfs;
7003 Tcl_Obj *pRet = Tcl_NewObj();
7004 if( objc!=1 ){
7005 Tcl_WrongNumArgs(interp, 1, objv, "");
7006 return TCL_ERROR;
7008 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
7009 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
7011 Tcl_SetObjResult(interp, pRet);
7012 return TCL_OK;
7016 ** tclcmd: sqlite3_limit DB ID VALUE
7018 ** This TCL command runs the sqlite3_limit interface and
7019 ** verifies correct operation of the same.
7021 static int SQLITE_TCLAPI test_limit(
7022 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7023 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7024 int objc, /* Number of arguments */
7025 Tcl_Obj *CONST objv[] /* Command arguments */
7027 sqlite3 *db;
7028 int rc;
7029 static const struct {
7030 char *zName;
7031 int id;
7032 } aId[] = {
7033 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
7034 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
7035 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
7036 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
7037 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
7038 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
7039 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
7040 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
7041 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
7042 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
7043 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
7044 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS },
7046 /* Out of range test cases */
7047 { "SQLITE_LIMIT_TOOSMALL", -1, },
7048 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 },
7050 int i, id = 0;
7051 int val;
7052 const char *zId;
7054 if( objc!=4 ){
7055 Tcl_AppendResult(interp, "wrong # args: should be \"",
7056 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
7057 return TCL_ERROR;
7059 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7060 zId = Tcl_GetString(objv[2]);
7061 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
7062 if( strcmp(zId, aId[i].zName)==0 ){
7063 id = aId[i].id;
7064 break;
7067 if( i>=sizeof(aId)/sizeof(aId[0]) ){
7068 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
7069 return TCL_ERROR;
7071 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7072 rc = sqlite3_limit(db, id, val);
7073 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
7074 return TCL_OK;
7078 ** tclcmd: save_prng_state
7080 ** Save the state of the pseudo-random number generator.
7081 ** At the same time, verify that sqlite3_test_control works even when
7082 ** called with an out-of-range opcode.
7084 static int SQLITE_TCLAPI save_prng_state(
7085 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7086 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7087 int objc, /* Number of arguments */
7088 Tcl_Obj *CONST objv[] /* Command arguments */
7090 int rc = sqlite3_test_control(9999);
7091 assert( rc==0 );
7092 rc = sqlite3_test_control(-1);
7093 assert( rc==0 );
7094 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
7095 return TCL_OK;
7098 ** tclcmd: restore_prng_state
7100 static int SQLITE_TCLAPI restore_prng_state(
7101 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7102 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7103 int objc, /* Number of arguments */
7104 Tcl_Obj *CONST objv[] /* Command arguments */
7106 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
7107 return TCL_OK;
7110 ** tclcmd: reset_prng_state
7112 static int SQLITE_TCLAPI reset_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 sqlite3_randomness(0,0);
7119 return TCL_OK;
7122 ** tclcmd: prng_seed INT ?DB?
7124 ** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB.
7125 ** INT is an integer. DB is a database connection, or a NULL pointer if
7126 ** omitted.
7128 ** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema
7129 ** cookie for DB, or to INT if the schema cookie happens to be zero.
7131 ** When INT!=0 and DB==0, set the PRNG seed to just INT.
7133 ** If INT==0 and DB==0 then use the default procedure of calling the
7134 ** xRandomness method on the default VFS to get the PRNG seed.
7136 static int SQLITE_TCLAPI prng_seed(
7137 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7138 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7139 int objc, /* Number of arguments */
7140 Tcl_Obj *CONST objv[] /* Command arguments */
7142 int i = 0;
7143 sqlite3 *db = 0;
7144 if( objc!=2 && objc!=3 ){
7145 Tcl_WrongNumArgs(interp, 1, objv, "SEED ?DB?");
7146 return TCL_ERROR;
7148 if( Tcl_GetIntFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7149 if( objc==3 && getDbPointer(interp, Tcl_GetString(objv[2]), &db) ){
7150 return TCL_ERROR;
7152 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, i, db);
7153 return TCL_OK;
7157 ** tclcmd: extra_schema_checks BOOLEAN
7159 ** Enable or disable schema checks when parsing the sqlite_schema file.
7160 ** This is always enabled in production, but it is sometimes useful to
7161 ** disable the checks in order to make some internal error states reachable
7162 ** for testing.
7164 static int SQLITE_TCLAPI extra_schema_checks(
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 if( objc!=2 ){
7172 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
7173 return TCL_ERROR;
7175 if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7176 sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, i);
7177 return TCL_OK;
7181 ** tclcmd: use_long_double BOOLEAN|"default"
7183 ** If no argument, report the current value of the use-long-double flag.
7185 ** If argument is "default", set the use-long-double flag to the default
7186 ** value for this build, based on the size of LONGDOUBLE_TYPE.
7188 ** If argument is a boolean, set the use-long-double flag accordingly.
7190 ** Return the new setting.
7192 static int SQLITE_TCLAPI use_long_double(
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 = -1;
7199 if( objc==2 ){
7200 if( strcmp(Tcl_GetString(objv[1]),"default")==0 ){
7201 i = 2;
7202 }else{
7203 if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR;
7206 i = sqlite3_test_control(SQLITE_TESTCTRL_USELONGDOUBLE, i);
7207 Tcl_SetObjResult(interp, Tcl_NewIntObj(i));
7208 return TCL_OK;
7212 ** tclcmd: database_may_be_corrupt
7214 ** Indicate that database files might be corrupt. In other words, set the normal
7215 ** state of operation.
7217 static int SQLITE_TCLAPI database_may_be_corrupt(
7218 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7219 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7220 int objc, /* Number of arguments */
7221 Tcl_Obj *CONST objv[] /* Command arguments */
7223 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0);
7224 return TCL_OK;
7227 ** tclcmd: database_never_corrupt
7229 ** Indicate that database files are always well-formed. This enables
7230 ** extra assert() statements that test conditions that are always true
7231 ** for well-formed databases.
7233 static int SQLITE_TCLAPI database_never_corrupt(
7234 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7235 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7236 int objc, /* Number of arguments */
7237 Tcl_Obj *CONST objv[] /* Command arguments */
7239 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1);
7240 return TCL_OK;
7244 ** tclcmd: pcache_stats
7246 static int SQLITE_TCLAPI test_pcache_stats(
7247 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
7248 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7249 int objc, /* Number of arguments */
7250 Tcl_Obj *CONST objv[] /* Command arguments */
7252 int nMin;
7253 int nMax;
7254 int nCurrent;
7255 int nRecyclable;
7256 Tcl_Obj *pRet;
7258 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
7260 pRet = Tcl_NewObj();
7261 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
7262 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
7263 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
7264 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
7265 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
7266 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
7267 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
7268 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
7270 Tcl_SetObjResult(interp, pRet);
7272 return TCL_OK;
7275 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7276 static void test_unlock_notify_cb(void **aArg, int nArg){
7277 int ii;
7278 for(ii=0; ii<nArg; ii++){
7279 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
7282 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
7285 ** tclcmd: sqlite3_unlock_notify db
7287 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7288 static int SQLITE_TCLAPI test_unlock_notify(
7289 ClientData clientData, /* Unused */
7290 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7291 int objc, /* Number of arguments */
7292 Tcl_Obj *CONST objv[] /* Command arguments */
7294 sqlite3 *db;
7295 int rc;
7297 if( objc!=2 ){
7298 Tcl_WrongNumArgs(interp, 1, objv, "DB");
7299 return TCL_ERROR;
7302 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7303 return TCL_ERROR;
7305 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
7306 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7307 return TCL_OK;
7309 #endif
7312 ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
7314 static int SQLITE_TCLAPI test_wal_checkpoint(
7315 ClientData clientData, /* Unused */
7316 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7317 int objc, /* Number of arguments */
7318 Tcl_Obj *CONST objv[] /* Command arguments */
7320 char *zDb = 0;
7321 sqlite3 *db;
7322 int rc;
7324 if( objc!=3 && objc!=2 ){
7325 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
7326 return TCL_ERROR;
7329 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
7330 return TCL_ERROR;
7332 if( objc==3 ){
7333 zDb = Tcl_GetString(objv[2]);
7335 rc = sqlite3_wal_checkpoint(db, zDb);
7336 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
7337 return TCL_OK;
7341 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
7343 ** This command calls the wal_checkpoint_v2() function with the specified
7344 ** mode argument (passive, full or restart). If present, the database name
7345 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
7346 ** NAME argument is not present, a NULL pointer is passed instead.
7348 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
7349 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
7350 ** to the error message obtained from sqlite3_errmsg().
7352 ** Otherwise, this command returns a list of three integers. The first integer
7353 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
7354 ** are the values returned via the output parameters by wal_checkpoint_v2() -
7355 ** the number of frames in the log and the number of frames in the log
7356 ** that have been checkpointed.
7358 static int SQLITE_TCLAPI test_wal_checkpoint_v2(
7359 ClientData clientData, /* Unused */
7360 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7361 int objc, /* Number of arguments */
7362 Tcl_Obj *CONST objv[] /* Command arguments */
7364 char *zDb = 0;
7365 sqlite3 *db;
7366 int rc;
7368 int eMode;
7369 int nLog = -555;
7370 int nCkpt = -555;
7371 Tcl_Obj *pRet;
7373 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
7374 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
7375 assert( SQLITE_CHECKPOINT_FULL==1 );
7376 assert( SQLITE_CHECKPOINT_RESTART==2 );
7377 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
7379 if( objc!=3 && objc!=4 ){
7380 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
7381 return TCL_ERROR;
7384 if( objc==4 ){
7385 zDb = Tcl_GetString(objv[3]);
7387 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
7388 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
7389 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
7391 return TCL_ERROR;
7394 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
7395 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
7396 const char *zErrCode = sqlite3ErrName(rc);
7397 Tcl_ResetResult(interp);
7398 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
7399 return TCL_ERROR;
7402 pRet = Tcl_NewObj();
7403 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
7404 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
7405 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
7406 Tcl_SetObjResult(interp, pRet);
7408 return TCL_OK;
7412 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE
7414 static int SQLITE_TCLAPI test_wal_autocheckpoint(
7415 ClientData clientData, /* Unused */
7416 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7417 int objc, /* Number of arguments */
7418 Tcl_Obj *CONST objv[] /* Command arguments */
7420 sqlite3 *db;
7421 int rc;
7422 int iVal;
7425 if( objc!=3 ){
7426 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE");
7427 return TCL_ERROR;
7430 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
7431 || Tcl_GetIntFromObj(0, objv[2], &iVal)
7433 return TCL_ERROR;
7436 rc = sqlite3_wal_autocheckpoint(db, iVal);
7437 Tcl_ResetResult(interp);
7438 if( rc!=SQLITE_OK ){
7439 const char *zErrCode = sqlite3ErrName(rc);
7440 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1));
7441 return TCL_ERROR;
7444 return TCL_OK;
7449 ** tclcmd: test_sqlite3_log ?SCRIPT?
7451 static struct LogCallback {
7452 Tcl_Interp *pInterp;
7453 Tcl_Obj *pObj;
7454 } logcallback = {0, 0};
7455 static void xLogcallback(void *unused, int err, char *zMsg){
7456 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
7457 Tcl_IncrRefCount(pNew);
7458 Tcl_ListObjAppendElement(
7459 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
7461 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
7462 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
7463 Tcl_DecrRefCount(pNew);
7465 static int SQLITE_TCLAPI test_sqlite3_log(
7466 ClientData clientData,
7467 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
7468 int objc, /* Number of arguments */
7469 Tcl_Obj *CONST objv[] /* Command arguments */
7471 if( objc>2 ){
7472 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
7473 return TCL_ERROR;
7475 if( logcallback.pObj ){
7476 Tcl_DecrRefCount(logcallback.pObj);
7477 logcallback.pObj = 0;
7478 logcallback.pInterp = 0;
7479 sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0);
7481 if( objc>1 ){
7482 logcallback.pObj = objv[1];
7483 Tcl_IncrRefCount(logcallback.pObj);
7484 logcallback.pInterp = interp;
7485 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0);
7487 return TCL_OK;
7491 ** tcl_objproc COMMANDNAME ARGS...
7493 ** Run a TCL command using its objProc interface. Throw an error if
7494 ** the command has no objProc interface.
7496 static int SQLITE_TCLAPI runAsObjProc(
7497 void * clientData,
7498 Tcl_Interp *interp,
7499 int objc,
7500 Tcl_Obj *CONST objv[]
7502 Tcl_CmdInfo cmdInfo;
7503 if( objc<2 ){
7504 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
7505 return TCL_ERROR;
7507 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
7508 Tcl_AppendResult(interp, "command not found: ",
7509 Tcl_GetString(objv[1]), (char*)0);
7510 return TCL_ERROR;
7512 if( cmdInfo.objProc==0 ){
7513 Tcl_AppendResult(interp, "command has no objProc: ",
7514 Tcl_GetString(objv[1]), (char*)0);
7515 return TCL_ERROR;
7517 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
7520 #ifndef SQLITE_OMIT_EXPLAIN
7522 ** WARNING: The following function, printExplainQueryPlan() is an exact
7523 ** copy of example code from eqp.in (eqp.html). If this code is modified,
7524 ** then the documentation copy needs to be modified as well.
7527 ** Argument pStmt is a prepared SQL statement. This function compiles
7528 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
7529 ** and prints the report to stdout using printf().
7531 int printExplainQueryPlan(sqlite3_stmt *pStmt){
7532 const char *zSql; /* Input SQL */
7533 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */
7534 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
7535 int rc; /* Return code from sqlite3_prepare_v2() */
7537 zSql = sqlite3_sql(pStmt);
7538 if( zSql==0 ) return SQLITE_ERROR;
7540 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
7541 if( zExplain==0 ) return SQLITE_NOMEM;
7543 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
7544 sqlite3_free(zExplain);
7545 if( rc!=SQLITE_OK ) return rc;
7547 while( SQLITE_ROW==sqlite3_step(pExplain) ){
7548 int iSelectid = sqlite3_column_int(pExplain, 0);
7549 int iOrder = sqlite3_column_int(pExplain, 1);
7550 int iFrom = sqlite3_column_int(pExplain, 2);
7551 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
7553 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
7556 return sqlite3_finalize(pExplain);
7559 static int SQLITE_TCLAPI test_print_eqp(
7560 void * clientData,
7561 Tcl_Interp *interp,
7562 int objc,
7563 Tcl_Obj *CONST objv[]
7565 int rc;
7566 sqlite3_stmt *pStmt;
7568 if( objc!=2 ){
7569 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
7570 return TCL_ERROR;
7572 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
7573 rc = printExplainQueryPlan(pStmt);
7574 /* This is needed on Windows so that a test case using this
7575 ** function can open a read pipe and get the output of
7576 ** printExplainQueryPlan() immediately.
7578 fflush(stdout);
7579 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
7580 return TCL_OK;
7582 #endif /* SQLITE_OMIT_EXPLAIN */
7584 #include <time.h>
7586 ** This is an alternative localtime_r() implementation used for testing
7587 ** the 'localtime' and 'utc' modifiers of date-time functions. Because
7588 ** the OS-supplied localtime_r() is locale-dependent, this alternative is
7589 ** provided as a stable test platform.
7591 ** Operation:
7593 ** (1) Localtime is 30 minutes earlier than (west of) UTC on
7594 ** even days (counting from 1970-01-01)
7596 ** (2) Localtime is 30 minutes later than (east of) UTC on odd days.
7598 ** (3) The function fails for the specific date/time value
7599 ** of 2000-05-29 14:16:00 in order to test the ability of
7600 ** SQLite to deal with localtime_r() failures.
7602 static int testLocaltime(const void *aliasT, void *aliasTM){
7603 const time_t t = *(const time_t*)aliasT;
7604 struct tm *pTm = (struct tm *)aliasTM;
7605 time_t altT;
7606 sqlite3_int64 iJD;
7607 int Z, A, B, C, D, E, X1, S;
7609 if( (t/86400) & 1 ){
7610 altT = t + 1800; /* 30 minutes later on odd days */
7611 }else{
7612 altT = t - 1800; /* 30 minutes earlier on even days */
7614 iJD = (sqlite3_int64)(altT + 210866760000);
7615 Z = (int)((iJD + 43200)/86400);
7616 A = (int)((Z - 1867216.25)/36524.25);
7617 A = Z + 1 + A - (A/4);
7618 B = A + 1524;
7619 C = (int)((B - 122.1)/365.25);
7620 D = (36525*(C&32767))/100;
7621 E = (int)((B-D)/30.6001);
7622 X1 = (int)(30.6001*E);
7623 pTm->tm_mday = B - D - X1;
7624 pTm->tm_mon = E<14 ? E-2 : E-14;
7625 pTm->tm_year = (pTm->tm_mon>1 ? C - 4716 : C - 4715) - 1900;
7626 S = (int)((iJD + 43200)%86400);
7627 pTm->tm_hour = S/3600;
7628 pTm->tm_min = (S/60)%60;
7629 pTm->tm_sec = S % 60;
7630 return t==959609760; /* Special case: 2000-05-29 14:16:00 fails */
7634 ** TCLCMD: strftime FORMAT UNIXTIMESTAMP
7636 ** Access to the C-library strftime() routine, so that its results
7637 ** can be compared against SQLite's internal strftime() SQL function
7638 ** implementation.
7640 static int SQLITE_TCLAPI strftime_cmd(
7641 void * clientData,
7642 Tcl_Interp *interp,
7643 int objc,
7644 Tcl_Obj *CONST objv[]
7646 Tcl_WideInt ts;
7647 time_t t;
7648 struct tm *pTm;
7649 const char *zFmt;
7650 size_t n;
7651 char zBuf[1000];
7652 if( objc!=3 ){
7653 Tcl_WrongNumArgs(interp, 1, objv, "FORMAT UNIXTIMESTAMP");
7654 return TCL_ERROR;
7656 if( Tcl_GetWideIntFromObj(interp, objv[2], &ts) ) return TCL_ERROR;
7657 zFmt = Tcl_GetString(objv[1]);
7658 t = (time_t)ts;
7659 pTm = gmtime(&t);
7660 n = strftime(zBuf, sizeof(zBuf)-1, zFmt, pTm);
7661 if( n>=0 && n<sizeof(zBuf) ){
7662 zBuf[n] = 0;
7663 Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
7665 return TCL_OK;
7669 ** .treetrace N
7671 static int SQLITE_TCLAPI test_treetrace(
7672 void * clientData,
7673 Tcl_Interp *interp,
7674 int objc,
7675 Tcl_Obj *CONST objv[]
7677 unsigned int v = 0;
7678 if( objc>=2 ){
7679 if( Tcl_GetIntFromObj(interp, objv[1], (int*)&v)==TCL_OK ){
7680 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &v);
7683 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &v);
7684 Tcl_SetObjResult(interp, Tcl_NewIntObj((int)v));
7685 return TCL_OK;
7689 ** sqlite3_test_control VERB ARGS...
7691 static int SQLITE_TCLAPI test_test_control(
7692 void * clientData,
7693 Tcl_Interp *interp,
7694 int objc,
7695 Tcl_Obj *CONST objv[]
7697 struct Verb {
7698 const char *zName;
7699 int i;
7700 } aVerb[] = {
7701 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
7702 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP },
7703 { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER },
7704 { "SQLITE_TESTCTRL_INTERNAL_FUNCTIONS", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS},
7705 { "SQLITE_TESTCTRL_FK_NO_ACTION", SQLITE_TESTCTRL_FK_NO_ACTION},
7706 { 0, 0 }
7708 int iVerb;
7709 int iFlag;
7710 int rc;
7712 if( objc<2 ){
7713 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
7714 return TCL_ERROR;
7717 rc = Tcl_GetIndexFromObjStruct(
7718 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
7720 if( rc!=TCL_OK ) return rc;
7722 iFlag = aVerb[iVerb].i;
7723 switch( iFlag ){
7724 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
7725 sqlite3 *db = 0;
7726 if( objc!=3 ){
7727 Tcl_WrongNumArgs(interp, 2, objv, "DB");
7728 return TCL_ERROR;
7730 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7731 sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db);
7732 break;
7734 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
7735 int val;
7736 if( objc!=3 ){
7737 Tcl_WrongNumArgs(interp, 2, objv, "0|1|2");
7738 return TCL_ERROR;
7740 if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR;
7741 sqlite3_test_control(iFlag, val, testLocaltime);
7742 break;
7745 case SQLITE_TESTCTRL_FK_NO_ACTION: {
7746 int val = 0;
7747 sqlite3 *db = 0;
7748 if( objc!=4 ){
7749 Tcl_WrongNumArgs(interp, 2, objv, "DB BOOLEAN");
7750 return TCL_ERROR;
7752 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7753 if( Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7755 sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, db, val);
7756 break;
7759 case SQLITE_TESTCTRL_SORTER_MMAP: {
7760 int val;
7761 sqlite3 *db;
7762 if( objc!=4 ){
7763 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT");
7764 return TCL_ERROR;
7766 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7767 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
7768 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val);
7769 break;
7772 case SQLITE_TESTCTRL_IMPOSTER: {
7773 int onOff, tnum;
7774 const char *zDbName;
7775 sqlite3 *db;
7776 if( objc!=6 ){
7777 Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum");
7778 return TCL_ERROR;
7780 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
7781 zDbName = Tcl_GetString(objv[3]);
7782 if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR;
7783 if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR;
7784 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum);
7785 break;
7789 Tcl_ResetResult(interp);
7790 return TCL_OK;
7793 #if SQLITE_OS_UNIX
7794 #include <sys/time.h>
7795 #include <sys/resource.h>
7797 static int SQLITE_TCLAPI test_getrusage(
7798 void * clientData,
7799 Tcl_Interp *interp,
7800 int objc,
7801 Tcl_Obj *CONST objv[]
7803 char buf[1024];
7804 struct rusage r;
7805 memset(&r, 0, sizeof(r));
7806 getrusage(RUSAGE_SELF, &r);
7808 sqlite3_snprintf(sizeof(buf), buf,
7809 "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
7810 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
7811 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
7812 (int)r.ru_minflt, (int)r.ru_majflt
7814 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
7815 return TCL_OK;
7817 #endif
7819 #if SQLITE_OS_WIN
7821 ** Information passed from the main thread into the windows file locker
7822 ** background thread.
7824 struct win32FileLocker {
7825 char *evName; /* Name of event to signal thread startup */
7826 HANDLE h; /* Handle of the file to be locked */
7827 int delay1; /* Delay before locking */
7828 int delay2; /* Delay before unlocking */
7829 int ok; /* Finished ok */
7830 int err; /* True if an error occurs */
7832 #endif
7835 #if SQLITE_OS_WIN
7836 #include <process.h>
7838 ** The background thread that does file locking.
7840 static void SQLITE_CDECL win32_file_locker(void *pAppData){
7841 struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
7842 if( p->evName ){
7843 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
7844 if ( ev ){
7845 SetEvent(ev);
7846 CloseHandle(ev);
7849 if( p->delay1 ) Sleep(p->delay1);
7850 if( LockFile(p->h, 0, 0, 100000000, 0) ){
7851 Sleep(p->delay2);
7852 UnlockFile(p->h, 0, 0, 100000000, 0);
7853 p->ok = 1;
7854 }else{
7855 p->err = 1;
7857 CloseHandle(p->h);
7858 p->h = 0;
7859 p->delay1 = 0;
7860 p->delay2 = 0;
7862 #endif
7864 #if SQLITE_OS_WIN
7866 ** lock_win32_file FILENAME DELAY1 DELAY2
7868 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
7869 ** Wait DELAY1 milliseconds before acquiring the lock.
7871 static int SQLITE_TCLAPI win32_file_lock(
7872 void * clientData,
7873 Tcl_Interp *interp,
7874 int objc,
7875 Tcl_Obj *CONST objv[]
7877 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
7878 const char *zFilename;
7879 char zBuf[200];
7880 int retry = 0;
7881 HANDLE ev;
7882 DWORD wResult;
7884 if( objc!=4 && objc!=1 ){
7885 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
7886 return TCL_ERROR;
7888 if( objc==1 ){
7889 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
7890 x.ok, x.err, x.delay1, x.delay2, x.h);
7891 Tcl_AppendResult(interp, zBuf, (char*)0);
7892 return TCL_OK;
7894 while( x.h && retry<30 ){
7895 retry++;
7896 Sleep(100);
7898 if( x.h ){
7899 Tcl_AppendResult(interp, "busy", (char*)0);
7900 return TCL_ERROR;
7902 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
7903 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
7904 zFilename = Tcl_GetString(objv[1]);
7905 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
7906 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
7907 FILE_ATTRIBUTE_NORMAL, 0);
7908 if( !x.h ){
7909 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
7910 return TCL_ERROR;
7912 ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
7913 if ( !ev ){
7914 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
7915 return TCL_ERROR;
7917 _beginthread(win32_file_locker, 0, (void*)&x);
7918 Sleep(0);
7919 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
7920 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
7921 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
7922 CloseHandle(ev);
7923 return TCL_ERROR;
7925 CloseHandle(ev);
7926 return TCL_OK;
7930 ** exists_win32_path PATH
7932 ** Returns non-zero if the specified path exists, whose fully qualified name
7933 ** may exceed 260 characters if it is prefixed with "\\?\".
7935 static int SQLITE_TCLAPI win32_exists_path(
7936 void *clientData,
7937 Tcl_Interp *interp,
7938 int objc,
7939 Tcl_Obj *CONST objv[]
7941 if( objc!=2 ){
7942 Tcl_WrongNumArgs(interp, 1, objv, "PATH");
7943 return TCL_ERROR;
7945 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
7946 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
7947 return TCL_OK;
7951 ** find_win32_file PATTERN
7953 ** Returns a list of entries in a directory that match the specified pattern,
7954 ** whose fully qualified name may exceed 248 characters if it is prefixed with
7955 ** "\\?\".
7957 static int SQLITE_TCLAPI win32_find_file(
7958 void *clientData,
7959 Tcl_Interp *interp,
7960 int objc,
7961 Tcl_Obj *CONST objv[]
7963 HANDLE hFindFile = INVALID_HANDLE_VALUE;
7964 WIN32_FIND_DATAW findData;
7965 Tcl_Obj *listObj;
7966 DWORD lastErrno;
7967 if( objc!=2 ){
7968 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
7969 return TCL_ERROR;
7971 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
7972 if( hFindFile==INVALID_HANDLE_VALUE ){
7973 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7974 return TCL_ERROR;
7976 listObj = Tcl_NewObj();
7977 Tcl_IncrRefCount(listObj);
7978 do {
7979 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
7980 findData.cFileName, -1));
7981 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
7982 findData.dwFileAttributes));
7983 } while( FindNextFileW(hFindFile, &findData) );
7984 lastErrno = GetLastError();
7985 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
7986 FindClose(hFindFile);
7987 Tcl_DecrRefCount(listObj);
7988 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
7989 return TCL_ERROR;
7991 FindClose(hFindFile);
7992 Tcl_SetObjResult(interp, listObj);
7993 return TCL_OK;
7997 ** delete_win32_file FILENAME
7999 ** Deletes the specified file, whose fully qualified name may exceed 260
8000 ** characters if it is prefixed with "\\?\".
8002 static int SQLITE_TCLAPI win32_delete_file(
8003 void *clientData,
8004 Tcl_Interp *interp,
8005 int objc,
8006 Tcl_Obj *CONST objv[]
8008 if( objc!=2 ){
8009 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
8010 return TCL_ERROR;
8012 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
8013 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8014 return TCL_ERROR;
8016 Tcl_ResetResult(interp);
8017 return TCL_OK;
8021 ** make_win32_dir DIRECTORY
8023 ** Creates the specified directory, whose fully qualified name may exceed 248
8024 ** characters if it is prefixed with "\\?\".
8026 static int SQLITE_TCLAPI win32_mkdir(
8027 void *clientData,
8028 Tcl_Interp *interp,
8029 int objc,
8030 Tcl_Obj *CONST objv[]
8032 if( objc!=2 ){
8033 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
8034 return TCL_ERROR;
8036 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
8037 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8038 return TCL_ERROR;
8040 Tcl_ResetResult(interp);
8041 return TCL_OK;
8045 ** remove_win32_dir DIRECTORY
8047 ** Removes the specified directory, whose fully qualified name may exceed 248
8048 ** characters if it is prefixed with "\\?\".
8050 static int SQLITE_TCLAPI win32_rmdir(
8051 void *clientData,
8052 Tcl_Interp *interp,
8053 int objc,
8054 Tcl_Obj *CONST objv[]
8056 if( objc!=2 ){
8057 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
8058 return TCL_ERROR;
8060 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
8061 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
8062 return TCL_ERROR;
8064 Tcl_ResetResult(interp);
8065 return TCL_OK;
8067 #endif
8071 ** optimization_control DB OPT BOOLEAN
8073 ** Enable or disable query optimizations using the sqlite3_test_control()
8074 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
8075 ** OPT is the name of the optimization to be disabled. OPT can also be a
8076 ** list or optimizations names, in which case all optimizations named are
8077 ** enabled or disabled.
8079 ** Each invocation of this control overrides all prior invocations. The
8080 ** changes are not cumulative.
8082 static int SQLITE_TCLAPI optimization_control(
8083 void * clientData,
8084 Tcl_Interp *interp,
8085 int objc,
8086 Tcl_Obj *CONST objv[]
8088 int i;
8089 sqlite3 *db;
8090 const char *zOpt;
8091 int onoff;
8092 int mask = 0;
8093 int cnt = 0;
8094 static const struct {
8095 const char *zOptName;
8096 int mask;
8097 } aOpt[] = {
8098 { "all", SQLITE_AllOpts },
8099 { "none", 0 },
8100 { "query-flattener", SQLITE_QueryFlattener },
8101 { "groupby-order", SQLITE_GroupByOrder },
8102 { "factor-constants", SQLITE_FactorOutConst },
8103 { "distinct-opt", SQLITE_DistinctOpt },
8104 { "cover-idx-scan", SQLITE_CoverIdxScan },
8105 { "order-by-idx-join", SQLITE_OrderByIdxJoin },
8106 { "transitive", SQLITE_Transitive },
8107 { "omit-noop-join", SQLITE_OmitNoopJoin },
8108 { "stat4", SQLITE_Stat4 },
8109 { "skip-scan", SQLITE_SkipScan },
8110 { "push-down", SQLITE_PushDown },
8111 { "balanced-merge", SQLITE_BalancedMerge },
8112 { "propagate-const", SQLITE_PropagateConst },
8113 { "one-pass", SQLITE_OnePass },
8116 if( objc!=4 ){
8117 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
8118 return TCL_ERROR;
8120 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8121 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
8122 zOpt = Tcl_GetString(objv[2]);
8123 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
8124 if( strstr(zOpt, aOpt[i].zOptName)!=0 ){
8125 mask |= aOpt[i].mask;
8126 cnt++;
8129 if( onoff ) mask = ~mask;
8130 if( cnt==0 ){
8131 Tcl_AppendResult(interp, "unknown optimization - should be one of:",
8132 (char*)0);
8133 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
8134 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
8136 return TCL_ERROR;
8138 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
8139 Tcl_SetObjResult(interp, Tcl_NewIntObj(mask));
8140 return TCL_OK;
8144 ** load_static_extension DB NAME ...
8146 ** Load one or more statically linked extensions.
8148 static int SQLITE_TCLAPI tclLoadStaticExtensionCmd(
8149 void * clientData,
8150 Tcl_Interp *interp,
8151 int objc,
8152 Tcl_Obj *CONST objv[]
8154 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
8155 extern int sqlite3_appendvfs_init(sqlite3*,char**,const sqlite3_api_routines*);
8156 extern int sqlite3_basexx_init(sqlite3*,char**,const sqlite3_api_routines*);
8157 extern int sqlite3_carray_init(sqlite3*,char**,const sqlite3_api_routines*);
8158 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
8159 extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*);
8160 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
8161 extern int sqlite3_explain_init(sqlite3*,char**,const sqlite3_api_routines*);
8162 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
8163 extern int sqlite3_decimal_init(sqlite3*,char**,const sqlite3_api_routines*);
8164 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
8165 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
8166 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
8167 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
8168 #ifndef SQLITE_OMIT_VIRTUALTABLE
8169 extern int sqlite3_prefixes_init(sqlite3*,char**,const sqlite3_api_routines*);
8170 #endif
8171 extern int sqlite3_qpvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
8172 extern int sqlite3_randomjson_init(sqlite3*,char**,const sqlite3_api_routines*);
8173 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
8174 extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*);
8175 extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
8176 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
8177 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
8178 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
8179 extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*);
8180 #ifdef SQLITE_HAVE_ZLIB
8181 extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*);
8182 #endif
8183 static const struct {
8184 const char *zExtName;
8185 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
8186 } aExtension[] = {
8187 { "amatch", sqlite3_amatch_init },
8188 { "appendvfs", sqlite3_appendvfs_init },
8189 { "basexx", sqlite3_basexx_init },
8190 { "carray", sqlite3_carray_init },
8191 { "closure", sqlite3_closure_init },
8192 { "csv", sqlite3_csv_init },
8193 { "decimal", sqlite3_decimal_init },
8194 { "eval", sqlite3_eval_init },
8195 { "explain", sqlite3_explain_init },
8196 { "fileio", sqlite3_fileio_init },
8197 { "fuzzer", sqlite3_fuzzer_init },
8198 { "ieee754", sqlite3_ieee_init },
8199 { "nextchar", sqlite3_nextchar_init },
8200 { "percentile", sqlite3_percentile_init },
8201 #ifndef SQLITE_OMIT_VIRTUALTABLE
8202 { "prefixes", sqlite3_prefixes_init },
8203 #endif
8204 { "qpvtab", sqlite3_qpvtab_init },
8205 { "randomjson", sqlite3_randomjson_init },
8206 { "regexp", sqlite3_regexp_init },
8207 { "remember", sqlite3_remember_init },
8208 { "series", sqlite3_series_init },
8209 { "spellfix", sqlite3_spellfix_init },
8210 { "totype", sqlite3_totype_init },
8211 { "unionvtab", sqlite3_unionvtab_init },
8212 { "wholenumber", sqlite3_wholenumber_init },
8213 #ifdef SQLITE_HAVE_ZLIB
8214 { "zipfile", sqlite3_zipfile_init },
8215 #endif
8217 sqlite3 *db;
8218 const char *zName;
8219 int i, j, rc;
8220 char *zErrMsg = 0;
8221 if( objc<3 ){
8222 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
8223 return TCL_ERROR;
8225 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8226 for(j=2; j<objc; j++){
8227 zName = Tcl_GetString(objv[j]);
8228 for(i=0; i<ArraySize(aExtension); i++){
8229 if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
8231 if( i>=ArraySize(aExtension) ){
8232 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
8233 return TCL_ERROR;
8235 if( aExtension[i].pInit ){
8236 rc = aExtension[i].pInit(db, &zErrMsg, 0);
8237 }else{
8238 rc = SQLITE_OK;
8240 if( (rc!=SQLITE_OK && rc!=SQLITE_OK_LOAD_PERMANENTLY) || zErrMsg ){
8241 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
8242 (char*)0);
8243 sqlite3_free(zErrMsg);
8244 return TCL_ERROR;
8247 return TCL_OK;
8251 ** sorter_test_fakeheap BOOL
8254 static int SQLITE_TCLAPI sorter_test_fakeheap(
8255 void * clientData,
8256 Tcl_Interp *interp,
8257 int objc,
8258 Tcl_Obj *CONST objv[]
8260 int bArg;
8261 if( objc!=2 ){
8262 Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
8263 return TCL_ERROR;
8266 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){
8267 return TCL_ERROR;
8270 if( bArg ){
8271 if( sqlite3GlobalConfig.pHeap==0 ){
8272 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1);
8274 }else{
8275 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){
8276 sqlite3GlobalConfig.pHeap = 0;
8280 Tcl_ResetResult(interp);
8281 return TCL_OK;
8285 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2
8287 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
8288 ** check that the leftmost and rightmost columns returned are both integers,
8289 ** and that both contain the same value.
8291 ** Then execute statement $SQL2. Check that the statement returns the same
8292 ** set of integers in the same order as in the previous step (using $SQL1).
8294 static int SQLITE_TCLAPI sorter_test_sort4_helper(
8295 void * clientData,
8296 Tcl_Interp *interp,
8297 int objc,
8298 Tcl_Obj *CONST objv[]
8300 const char *zSql1;
8301 const char *zSql2;
8302 int nStep;
8303 int iStep;
8304 unsigned int iCksum1 = 0;
8305 unsigned int iCksum2 = 0;
8306 int rc;
8307 int iB;
8308 sqlite3 *db;
8309 sqlite3_stmt *pStmt;
8311 if( objc!=5 ){
8312 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2");
8313 return TCL_ERROR;
8316 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8317 zSql1 = Tcl_GetString(objv[2]);
8318 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR;
8319 zSql2 = Tcl_GetString(objv[4]);
8321 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0);
8322 if( rc!=SQLITE_OK ) goto sql_error;
8324 iB = sqlite3_column_count(pStmt)-1;
8325 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){
8326 int a = sqlite3_column_int(pStmt, 0);
8327 if( a!=sqlite3_column_int(pStmt, iB) ){
8328 Tcl_AppendResult(interp, "data error: (a!=b)", 0);
8329 return TCL_ERROR;
8332 iCksum1 += (iCksum1 << 3) + (unsigned int)a;
8334 rc = sqlite3_finalize(pStmt);
8335 if( rc!=SQLITE_OK ) goto sql_error;
8337 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0);
8338 if( rc!=SQLITE_OK ) goto sql_error;
8339 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){
8340 int a = sqlite3_column_int(pStmt, 0);
8341 iCksum2 += (iCksum2 << 3) + (unsigned int)a;
8343 rc = sqlite3_finalize(pStmt);
8344 if( rc!=SQLITE_OK ) goto sql_error;
8346 if( iCksum1!=iCksum2 ){
8347 Tcl_AppendResult(interp, "checksum mismatch", 0);
8348 return TCL_ERROR;
8351 return TCL_OK;
8352 sql_error:
8353 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
8354 return TCL_ERROR;
8358 #ifdef SQLITE_USER_AUTHENTICATION
8359 #include "sqlite3userauth.h"
8361 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD
8363 static int SQLITE_TCLAPI test_user_authenticate(
8364 ClientData clientData, /* Unused */
8365 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8366 int objc, /* Number of arguments */
8367 Tcl_Obj *CONST objv[] /* Command arguments */
8369 char *zUser = 0;
8370 char *zPasswd = 0;
8371 int nPasswd = 0;
8372 sqlite3 *db;
8373 int rc;
8375 if( objc!=4 ){
8376 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
8377 return TCL_ERROR;
8379 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8380 return TCL_ERROR;
8382 zUser = Tcl_GetString(objv[2]);
8383 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8384 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
8385 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8386 return TCL_OK;
8388 #endif /* SQLITE_USER_AUTHENTICATION */
8390 #ifdef SQLITE_USER_AUTHENTICATION
8392 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN
8394 static int SQLITE_TCLAPI test_user_add(
8395 ClientData clientData, /* Unused */
8396 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8397 int objc, /* Number of arguments */
8398 Tcl_Obj *CONST objv[] /* Command arguments */
8400 char *zUser = 0;
8401 char *zPasswd = 0;
8402 int nPasswd = 0;
8403 int isAdmin = 0;
8404 sqlite3 *db;
8405 int rc;
8407 if( objc!=5 ){
8408 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
8409 return TCL_ERROR;
8411 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8412 return TCL_ERROR;
8414 zUser = Tcl_GetString(objv[2]);
8415 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8416 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
8417 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
8418 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8419 return TCL_OK;
8421 #endif /* SQLITE_USER_AUTHENTICATION */
8423 #ifdef SQLITE_USER_AUTHENTICATION
8425 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN
8427 static int SQLITE_TCLAPI test_user_change(
8428 ClientData clientData, /* Unused */
8429 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8430 int objc, /* Number of arguments */
8431 Tcl_Obj *CONST objv[] /* Command arguments */
8433 char *zUser = 0;
8434 char *zPasswd = 0;
8435 int nPasswd = 0;
8436 int isAdmin = 0;
8437 sqlite3 *db;
8438 int rc;
8440 if( objc!=5 ){
8441 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
8442 return TCL_ERROR;
8444 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8445 return TCL_ERROR;
8447 zUser = Tcl_GetString(objv[2]);
8448 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
8449 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
8450 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
8451 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8452 return TCL_OK;
8454 #endif /* SQLITE_USER_AUTHENTICATION */
8456 #ifdef SQLITE_USER_AUTHENTICATION
8458 ** tclcmd: sqlite3_user_delete DB USERNAME
8460 static int SQLITE_TCLAPI test_user_delete(
8461 ClientData clientData, /* Unused */
8462 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8463 int objc, /* Number of arguments */
8464 Tcl_Obj *CONST objv[] /* Command arguments */
8466 char *zUser = 0;
8467 sqlite3 *db;
8468 int rc;
8470 if( objc!=3 ){
8471 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
8472 return TCL_ERROR;
8474 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
8475 return TCL_ERROR;
8477 zUser = Tcl_GetString(objv[2]);
8478 rc = sqlite3_user_delete(db, zUser);
8479 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
8480 return TCL_OK;
8482 #endif /* SQLITE_USER_AUTHENTICATION */
8485 ** tclcmd: bad_behavior TYPE
8487 ** Do some things that should trigger a valgrind or -fsanitize=undefined
8488 ** warning. This is used to verify that errors and warnings output by those
8489 ** tools are detected by the test scripts.
8491 ** TYPE BEHAVIOR
8492 ** 1 Overflow a signed integer
8493 ** 2 Jump based on an uninitialized variable
8494 ** 3 Read after free
8495 ** 4 Panic
8497 static int SQLITE_TCLAPI test_bad_behavior(
8498 ClientData clientData, /* Pointer to an integer containing zero */
8499 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
8500 int objc, /* Number of arguments */
8501 Tcl_Obj *CONST objv[] /* Command arguments */
8503 int iType;
8504 int xyz;
8505 int i = *(int*)clientData;
8506 int j;
8507 int w[10];
8508 int *a;
8509 if( objc!=2 ){
8510 Tcl_WrongNumArgs(interp, 1, objv, "TYPE");
8511 return TCL_ERROR;
8513 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR;
8514 switch( iType ){
8515 case 1: {
8516 xyz = 0x7fffff00 - i;
8517 xyz += 0x100;
8518 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz));
8519 break;
8521 case 2: {
8522 w[1] = 5;
8523 if( w[i]>0 ) w[1]++;
8524 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1]));
8525 break;
8527 case 3: {
8528 a = malloc( sizeof(int)*10 );
8529 for(j=0; j<10; j++) a[j] = j;
8530 free(a);
8531 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i]));
8532 break;
8534 case 4: {
8535 Tcl_Panic("Deliberate panic");
8536 break;
8539 return TCL_OK;
8543 ** tclcmd: register_dbstat_vtab DB
8545 ** Cause the dbstat virtual table to be available on the connection DB
8547 static int SQLITE_TCLAPI test_register_dbstat_vtab(
8548 void *clientData,
8549 Tcl_Interp *interp,
8550 int objc,
8551 Tcl_Obj *CONST objv[]
8553 #ifdef SQLITE_OMIT_VIRTUALTABLE
8554 Tcl_AppendResult(interp, "dbstat not available because of "
8555 "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
8556 return TCL_ERROR;
8557 #else
8558 struct SqliteDb { sqlite3 *db; };
8559 char *zDb;
8560 Tcl_CmdInfo cmdInfo;
8562 if( objc!=2 ){
8563 Tcl_WrongNumArgs(interp, 1, objv, "DB");
8564 return TCL_ERROR;
8567 zDb = Tcl_GetString(objv[1]);
8568 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
8569 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
8570 sqlite3DbstatRegister(db);
8572 return TCL_OK;
8573 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8577 ** tclcmd: sqlite3_db_config DB SETTING VALUE
8579 ** Invoke sqlite3_db_config() for one of the setting values.
8581 static int SQLITE_TCLAPI test_sqlite3_db_config(
8582 void *clientData,
8583 Tcl_Interp *interp,
8584 int objc,
8585 Tcl_Obj *CONST objv[]
8587 static const struct {
8588 const char *zName;
8589 int eVal;
8590 } aSetting[] = {
8591 { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY },
8592 { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER },
8593 { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8594 { "LOAD_EXTENSION", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8595 { "NO_CKPT_ON_CLOSE", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
8596 { "QPSG", SQLITE_DBCONFIG_ENABLE_QPSG },
8597 { "TRIGGER_EQP", SQLITE_DBCONFIG_TRIGGER_EQP },
8598 { "RESET_DB", SQLITE_DBCONFIG_RESET_DATABASE },
8599 { "DEFENSIVE", SQLITE_DBCONFIG_DEFENSIVE },
8600 { "WRITABLE_SCHEMA", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
8601 { "LEGACY_ALTER_TABLE", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
8602 { "DQS_DML", SQLITE_DBCONFIG_DQS_DML },
8603 { "DQS_DDL", SQLITE_DBCONFIG_DQS_DDL },
8604 { "LEGACY_FILE_FORMAT", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
8605 { "STMT_SCANSTATUS", SQLITE_DBCONFIG_STMT_SCANSTATUS },
8607 int i;
8608 int v = 0;
8609 const char *zSetting;
8610 sqlite3 *db;
8612 if( objc!=4 && objc!=3 ){
8613 Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING [VALUE]");
8614 return TCL_ERROR;
8616 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8617 zSetting = Tcl_GetString(objv[2]);
8618 if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7;
8619 if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9;
8620 if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7;
8621 for(i=0; i<ArraySize(aSetting); i++){
8622 if( strcmp(zSetting, aSetting[i].zName)==0 ) break;
8624 if( i>=ArraySize(aSetting) ){
8625 Tcl_SetObjResult(interp,
8626 Tcl_NewStringObj("unknown sqlite3_db_config setting", -1));
8627 return TCL_ERROR;
8629 if( objc==4 ){
8630 if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR;
8631 }else{
8632 v = -1;
8634 sqlite3_db_config(db, aSetting[i].eVal, v, &v);
8635 Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
8636 return TCL_OK;
8639 ** tclcmd: sqlite3_txn_state DB ?SCHEMA?
8641 ** Invoke sqlite3_txn_state(DB,SCHEMA) and return the
8642 ** numeric value that results. Use NULL for SCHEMA if the 3 argument
8643 ** is omitted.
8645 static int SQLITE_TCLAPI test_sqlite3_txn_state(
8646 void *clientData,
8647 Tcl_Interp *interp,
8648 int objc,
8649 Tcl_Obj *CONST objv[]
8651 sqlite3 *db;
8652 const char *zSchema;
8653 int iTxn;
8655 if( objc!=2 && objc!=3 ){
8656 Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCHEMA?");
8657 return TCL_ERROR;
8659 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8660 zSchema = objc==3 ? Tcl_GetString(objv[2]) : 0;
8661 iTxn = sqlite3_txn_state(db, zSchema);
8662 Tcl_SetObjResult(interp, Tcl_NewIntObj(iTxn));
8663 return TCL_OK;
8667 ** Change the name of the main database schema from "main" to "icecube".
8669 static int SQLITE_TCLAPI test_dbconfig_maindbname_icecube(
8670 void * clientData,
8671 Tcl_Interp *interp,
8672 int objc,
8673 Tcl_Obj *CONST objv[]
8675 int rc;
8676 sqlite3 *db;
8677 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
8678 if( objc!=2 ){
8679 Tcl_WrongNumArgs(interp, 1, objv, "DB");
8680 return TCL_ERROR;
8681 }else{
8682 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8683 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "icecube");
8684 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
8685 return TCL_OK;
8690 ** Usage: sqlite3_mmap_warm DB DBNAME
8692 static int SQLITE_TCLAPI test_mmap_warm(
8693 void * clientData,
8694 Tcl_Interp *interp,
8695 int objc,
8696 Tcl_Obj *CONST objv[]
8698 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
8699 extern int sqlite3_mmap_warm(sqlite3 *db, const char *);
8701 if( objc!=2 && objc!=3 ){
8702 Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?");
8703 return TCL_ERROR;
8704 }else{
8705 int rc;
8706 sqlite3 *db;
8707 const char *zDb = 0;
8708 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8709 if( objc==3 ){
8710 zDb = Tcl_GetString(objv[2]);
8712 rc = sqlite3_mmap_warm(db, zDb);
8713 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
8714 return TCL_OK;
8719 ** Usage: test_write_db DB OFFSET DATA
8721 ** Obtain the sqlite3_file* object for the database file for the "main" db
8722 ** of handle DB. Then invoke its xWrite method to write data DATA to offset
8723 ** OFFSET.
8725 static int SQLITE_TCLAPI test_write_db(
8726 void * clientData,
8727 Tcl_Interp *interp,
8728 int objc,
8729 Tcl_Obj *CONST objv[]
8731 sqlite3 *db = 0;
8732 Tcl_WideInt iOff = 0;
8733 const unsigned char *aData = 0;
8734 int nData = 0;
8735 sqlite3_file *pFile = 0;
8736 int rc;
8738 if( objc!=4 ){
8739 Tcl_WrongNumArgs(interp, 1, objv, "DB OFFSET DATA");
8740 return TCL_ERROR;
8742 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8743 if( Tcl_GetWideIntFromObj(interp, objv[2], &iOff) ) return TCL_ERROR;
8744 aData = Tcl_GetByteArrayFromObj(objv[3], &nData);
8746 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFile);
8747 rc = pFile->pMethods->xWrite(pFile, aData, nData, iOff);
8749 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8750 return TCL_OK;
8754 ** Usage: sqlite3_register_cksumvfs
8757 static int SQLITE_TCLAPI test_register_cksumvfs(
8758 void * clientData,
8759 Tcl_Interp *interp,
8760 int objc,
8761 Tcl_Obj *CONST objv[]
8763 if( objc!=1 ){
8764 Tcl_WrongNumArgs(interp, 1, objv, "");
8765 return TCL_ERROR;
8766 }else{
8767 extern int sqlite3_register_cksumvfs(const char*);
8768 int rc = sqlite3_register_cksumvfs(0);
8769 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8771 return TCL_OK;
8775 ** Usage: sqlite3_unregister_cksumvfs
8778 static int SQLITE_TCLAPI test_unregister_cksumvfs(
8779 void * clientData,
8780 Tcl_Interp *interp,
8781 int objc,
8782 Tcl_Obj *CONST objv[]
8784 if( objc!=1 ){
8785 Tcl_WrongNumArgs(interp, 1, objv, "");
8786 return TCL_ERROR;
8787 }else{
8788 extern int sqlite3_unregister_cksumvfs(void);
8789 int rc = sqlite3_unregister_cksumvfs();
8790 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
8792 return TCL_OK;
8796 ** Usage: decode_hexdb TEXT
8798 ** Example: db deserialize [decode_hexdb $output_of_dbtotxt]
8800 ** This routine returns a byte-array for an SQLite database file that
8801 ** is constructed from a text input which is the output of the "dbtotxt"
8802 ** utility.
8804 static int SQLITE_TCLAPI test_decode_hexdb(
8805 void * clientData,
8806 Tcl_Interp *interp,
8807 int objc,
8808 Tcl_Obj *CONST objv[]
8810 const char *zIn = 0;
8811 unsigned char *a = 0;
8812 int n = 0;
8813 int lineno = 0;
8814 int i, iNext;
8815 int iOffset = 0;
8816 int j, k;
8817 int rc;
8818 unsigned int x[16];
8819 if( objc!=2 ){
8820 Tcl_WrongNumArgs(interp, 1, objv, "HEXDB");
8821 return TCL_ERROR;
8823 zIn = Tcl_GetString(objv[1]);
8824 for(i=0; zIn[i]; i=iNext){
8825 lineno++;
8826 for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){}
8827 if( zIn[iNext]=='\n' ) iNext++;
8828 while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; }
8829 if( a==0 ){
8830 int pgsz;
8831 rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz);
8832 if( rc!=2 ) continue;
8833 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){
8834 Tcl_AppendResult(interp, "bad 'pagesize' field", (void*)0);
8835 return TCL_ERROR;
8837 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
8838 if( n<512 ){
8839 Tcl_AppendResult(interp, "bad 'size' field", (void*)0);
8840 return TCL_ERROR;
8842 a = malloc( n );
8843 if( a==0 ){
8844 Tcl_AppendResult(interp, "out of memory", (void*)0);
8845 return TCL_ERROR;
8847 memset(a, 0, n);
8848 continue;
8850 rc = sscanf(zIn+i, "| page %d offset %d", &j, &k);
8851 if( rc==2 ){
8852 iOffset = k;
8853 continue;
8855 rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
8856 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
8857 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
8858 if( rc==17 ){
8859 k = iOffset+j;
8860 if( k+16<=n ){
8861 int ii;
8862 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
8864 continue;
8867 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n));
8868 free(a);
8869 return TCL_OK;
8873 ** Client data for the autovacuum_pages callback.
8875 struct AutovacPageData {
8876 Tcl_Interp *interp;
8877 char *zScript;
8879 typedef struct AutovacPageData AutovacPageData;
8882 ** Callback functions for sqlite3_autovacuum_pages
8884 static unsigned int test_autovacuum_pages_callback(
8885 void *pClientData,
8886 const char *zSchema,
8887 unsigned int nFilePages,
8888 unsigned int nFreePages,
8889 unsigned int nBytePerPage
8891 AutovacPageData *pData = (AutovacPageData*)pClientData;
8892 Tcl_DString str;
8893 unsigned int x;
8894 char zBuf[100];
8895 Tcl_DStringInit(&str);
8896 Tcl_DStringAppend(&str, pData->zScript, -1);
8897 Tcl_DStringAppendElement(&str, zSchema);
8898 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFilePages);
8899 Tcl_DStringAppendElement(&str, zBuf);
8900 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFreePages);
8901 Tcl_DStringAppendElement(&str, zBuf);
8902 sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nBytePerPage);
8903 Tcl_DStringAppendElement(&str, zBuf);
8904 Tcl_ResetResult(pData->interp);
8905 Tcl_Eval(pData->interp, Tcl_DStringValue(&str));
8906 Tcl_DStringFree(&str);
8907 x = nFreePages;
8908 (void)Tcl_GetIntFromObj(0, Tcl_GetObjResult(pData->interp), (int*)&x);
8909 return x;
8913 ** Usage: sqlite3_autovacuum_pages DB SCRIPT
8915 ** Add an autovacuum-pages callback to database connection DB. The callback
8916 ** will invoke SCRIPT, after appending parameters.
8918 ** If SCRIPT is an empty string or is omitted, then the callback is
8919 ** cancelled.
8921 static int SQLITE_TCLAPI test_autovacuum_pages(
8922 void * clientData,
8923 Tcl_Interp *interp,
8924 int objc,
8925 Tcl_Obj *CONST objv[]
8927 AutovacPageData *pData;
8928 sqlite3 *db;
8929 int rc;
8930 const char *zScript;
8931 if( objc!=2 && objc!=3 ){
8932 Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCRIPT?");
8933 return TCL_ERROR;
8935 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
8936 zScript = objc==3 ? Tcl_GetString(objv[2]) : 0;
8937 if( zScript ){
8938 size_t nScript = strlen(zScript);
8939 pData = sqlite3_malloc64( sizeof(*pData) + nScript + 1 );
8940 if( pData==0 ){
8941 Tcl_AppendResult(interp, "out of memory", (void*)0);
8942 return TCL_ERROR;
8944 pData->interp = interp;
8945 pData->zScript = (char*)&pData[1];
8946 memcpy(pData->zScript, zScript, nScript+1);
8947 rc = sqlite3_autovacuum_pages(db,test_autovacuum_pages_callback,
8948 pData, sqlite3_free);
8949 }else{
8950 rc = sqlite3_autovacuum_pages(db, 0, 0, 0);
8952 if( rc ){
8953 char zBuf[1000];
8954 sqlite3_snprintf(sizeof(zBuf), zBuf,
8955 "sqlite3_autovacuum_pages() returns %d", rc);
8956 Tcl_AppendResult(interp, zBuf, (void*)0);
8957 return TCL_ERROR;
8959 return TCL_OK;
8963 ** Usage: number_of_cores
8965 ** Return a guess at the number of available cores available on the
8966 ** processor on which this process is running.
8968 static int SQLITE_TCLAPI guess_number_of_cores(
8969 void * clientData,
8970 Tcl_Interp *interp,
8971 int objc,
8972 Tcl_Obj *CONST objv[]
8974 unsigned int nCore = 1;
8975 #if SQLITE_OS_WIN
8976 SYSTEM_INFO sysinfo;
8977 GetSystemInfo(&sysinfo);
8978 nCore = (unsigned int)sysinfo.dwNumberOfProcessors;
8979 #elif defined(__APPLE__)
8980 int nm[2];
8981 size_t len = 4;
8982 nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
8983 sysctl(nm, 2, &nCore, &len, NULL, 0);
8984 if( nCore<1 ){
8985 nm[1] = HW_NCPU;
8986 sysctl(nm, 2, &nCore, &len, NULL, 0);
8988 #else
8989 nCore = sysconf(_SC_NPROCESSORS_ONLN);
8990 #endif
8991 if( nCore<=0 ) nCore = 1;
8992 Tcl_SetObjResult(interp, Tcl_NewIntObj((int)nCore));
8993 return SQLITE_OK;
8998 ** Register commands with the TCL interpreter.
9000 int Sqlitetest1_Init(Tcl_Interp *interp){
9001 extern int sqlite3_search_count;
9002 extern int sqlite3_found_count;
9003 extern int sqlite3_interrupt_count;
9004 extern int sqlite3_open_file_count;
9005 extern int sqlite3_sort_count;
9006 extern int sqlite3_current_time;
9007 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
9008 extern int sqlite3_hostid_num;
9009 #endif
9010 extern int sqlite3_max_blobsize;
9011 extern int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport(void*,
9012 Tcl_Interp*,int,Tcl_Obj*CONST*);
9013 static int iZero = 0;
9014 static struct {
9015 char *zName;
9016 Tcl_CmdProc *xProc;
9017 } aCmd[] = {
9018 { "db_enter", (Tcl_CmdProc*)db_enter },
9019 { "db_leave", (Tcl_CmdProc*)db_leave },
9020 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
9021 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
9022 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
9023 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
9024 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
9025 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
9026 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
9027 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
9028 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
9029 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
9030 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
9031 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
9032 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
9033 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
9034 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
9035 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
9036 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
9037 #ifndef SQLITE_OMIT_GET_TABLE
9038 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
9039 #endif
9040 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
9041 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 },
9042 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
9043 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
9044 { "sqlite3_drop_modules", (Tcl_CmdProc*)test_drop_modules },
9045 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
9046 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
9047 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
9048 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
9049 { "sqlite3_key", (Tcl_CmdProc*)test_key },
9050 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
9051 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
9052 { "sqlite3_is_interrupted", (Tcl_CmdProc*)test_is_interrupted },
9053 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
9054 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
9055 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
9056 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
9057 { "printf", (Tcl_CmdProc*)test_printf },
9058 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
9059 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address },
9061 static struct {
9062 char *zName;
9063 Tcl_ObjCmdProc *xProc;
9064 void *clientData;
9065 } aObjCmd[] = {
9066 { "sqlite3_db_config", test_sqlite3_db_config, 0 },
9067 { "sqlite3_txn_state", test_sqlite3_txn_state, 0 },
9068 { "bad_behavior", test_bad_behavior, (void*)&iZero },
9069 { "register_dbstat_vtab", test_register_dbstat_vtab },
9070 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
9071 { "intarray_addr", test_intarray_addr, 0 },
9072 { "int64array_addr", test_int64array_addr, 0 },
9073 { "doublearray_addr", test_doublearray_addr, 0 },
9074 { "textarray_addr", test_textarray_addr, 0 },
9075 { "sqlite3_bind_int", test_bind_int, 0 },
9076 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
9077 { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 },
9078 { "sqlite3_bind_int64", test_bind_int64, 0 },
9079 { "sqlite3_bind_double", test_bind_double, 0 },
9080 { "sqlite3_bind_null", test_bind_null ,0 },
9081 { "sqlite3_bind_text", test_bind_text ,0 },
9082 { "sqlite3_bind_text16", test_bind_text16 ,0 },
9083 { "sqlite3_bind_blob", test_bind_blob ,0 },
9084 { "sqlite3_bind_value_from_select",test_bind_value_from_select ,0 },
9085 { "sqlite3_bind_value_from_preupdate",test_bind_value_from_preupdate ,0 },
9086 #ifndef SQLITE_OMIT_VIRTUALTABLE
9087 { "sqlite3_carray_bind", test_carray_bind ,0 },
9088 #endif
9089 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
9090 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
9091 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
9092 { "sqlite3_clear_bindings", test_clear_bindings, 0},
9093 { "sqlite3_sleep", test_sleep, 0},
9094 { "sqlite3_errcode", test_errcode ,0 },
9095 { "sqlite3_extended_errcode", test_ex_errcode ,0 },
9096 { "sqlite3_errmsg", test_errmsg ,0 },
9097 { "sqlite3_error_offset", test_error_offset ,0 },
9098 { "sqlite3_errmsg16", test_errmsg16 ,0 },
9099 { "sqlite3_open", test_open ,0 },
9100 { "sqlite3_open16", test_open16 ,0 },
9101 { "sqlite3_open_v2", test_open_v2 ,0 },
9102 { "sqlite3_complete16", test_complete16 ,0 },
9103 { "sqlite3_normalize", test_normalize ,0 },
9105 { "sqlite3_prepare", test_prepare ,0 },
9106 { "sqlite3_prepare16", test_prepare16 ,0 },
9107 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
9108 { "sqlite3_prepare_v3", test_prepare_v3 ,0 },
9109 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
9110 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
9111 { "sqlite3_finalize", test_finalize ,0 },
9112 { "sqlite3_stmt_status", test_stmt_status ,0 },
9113 { "sqlite3_reset", test_reset ,0 },
9114 { "sqlite3_expired", test_expired ,0 },
9115 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
9116 { "sqlite3_changes", test_changes ,0 },
9117 { "sqlite3_step", test_step ,0 },
9118 { "sqlite3_sql", test_sql ,0 },
9119 { "sqlite3_expanded_sql", test_ex_sql ,0 },
9120 #ifdef SQLITE_ENABLE_NORMALIZE
9121 { "sqlite3_normalized_sql", test_norm_sql ,0 },
9122 #endif
9123 { "sqlite3_next_stmt", test_next_stmt ,0 },
9124 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 },
9125 { "sqlite3_stmt_isexplain", test_stmt_isexplain,0 },
9126 { "sqlite3_stmt_explain", test_stmt_explain ,0 },
9127 { "sqlite3_stmt_busy", test_stmt_busy ,0 },
9128 { "uses_stmt_journal", uses_stmt_journal ,0 },
9130 { "sqlite3_release_memory", test_release_memory, 0},
9131 { "sqlite3_db_release_memory", test_db_release_memory, 0},
9132 { "sqlite3_db_cacheflush", test_db_cacheflush, 0},
9133 { "sqlite3_system_errno", test_system_errno, 0},
9134 { "sqlite3_db_filename", test_db_filename, 0},
9135 { "sqlite3_db_readonly", test_db_readonly, 0},
9136 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
9137 { "sqlite3_soft_heap_limit64", test_soft_heap_limit, 0},
9138 { "sqlite3_hard_heap_limit64", test_hard_heap_limit, 0},
9139 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
9140 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
9142 { "sqlite3_load_extension", test_load_extension, 0},
9143 { "sqlite3_enable_load_extension", test_enable_load, 0},
9144 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
9145 { "sqlite3_limit", test_limit, 0},
9146 { "dbconfig_maindbname_icecube", test_dbconfig_maindbname_icecube },
9148 { "save_prng_state", save_prng_state, 0 },
9149 { "restore_prng_state", restore_prng_state, 0 },
9150 { "reset_prng_state", reset_prng_state, 0 },
9151 { "prng_seed", prng_seed, 0 },
9152 { "extra_schema_checks", extra_schema_checks, 0},
9153 { "use_long_double", use_long_double, 0},
9154 { "database_never_corrupt", database_never_corrupt, 0},
9155 { "database_may_be_corrupt", database_may_be_corrupt, 0},
9156 { "optimization_control", optimization_control,0},
9157 #if SQLITE_OS_WIN
9158 { "lock_win32_file", win32_file_lock, 0 },
9159 { "exists_win32_path", win32_exists_path, 0 },
9160 { "find_win32_file", win32_find_file, 0 },
9161 { "delete_win32_file", win32_delete_file, 0 },
9162 { "make_win32_dir", win32_mkdir, 0 },
9163 { "remove_win32_dir", win32_rmdir, 0 },
9164 #endif
9165 { "tcl_objproc", runAsObjProc, 0 },
9167 /* sqlite3_column_*() API */
9168 { "sqlite3_column_count", test_column_count ,0 },
9169 { "sqlite3_data_count", test_data_count ,0 },
9170 { "sqlite3_column_type", test_column_type ,0 },
9171 { "sqlite3_column_blob", test_column_blob ,0 },
9172 { "sqlite3_column_double", test_column_double ,0 },
9173 { "sqlite3_column_int64", test_column_int64 ,0 },
9174 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
9175 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
9176 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
9177 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
9178 #ifndef SQLITE_OMIT_DECLTYPE
9179 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
9180 #endif
9181 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9182 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
9183 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
9184 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
9185 #endif
9187 #ifndef SQLITE_OMIT_UTF16
9188 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
9189 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
9190 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
9191 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
9192 #ifndef SQLITE_OMIT_DECLTYPE
9193 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
9194 #endif
9195 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9196 {"sqlite3_column_database_name16",
9197 test_stmt_utf16, (void*)sqlite3_column_database_name16},
9198 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
9199 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
9200 #endif
9201 #endif
9202 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
9203 { "sqlite3_global_recover", test_global_recover, 0 },
9204 { "working_64bit_int", working_64bit_int, 0 },
9205 { "vfs_unlink_test", vfs_unlink_test, 0 },
9206 { "vfs_initfail_test", vfs_initfail_test, 0 },
9207 { "vfs_unregister_all", vfs_unregister_all, 0 },
9208 { "vfs_reregister_all", vfs_reregister_all, 0 },
9209 { "file_control_test", file_control_test, 0 },
9210 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
9211 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
9212 { "file_control_chunksize_test", file_control_chunksize_test, 0 },
9213 { "file_control_sizehint_test", file_control_sizehint_test, 0 },
9214 { "file_control_data_version", file_control_data_version, 0 },
9215 #if SQLITE_OS_WIN
9216 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 },
9217 { "file_control_win32_get_handle", file_control_win32_get_handle, 0 },
9218 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 },
9219 #endif
9220 { "file_control_persist_wal", file_control_persist_wal, 0 },
9221 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
9222 { "file_control_vfsname", file_control_vfsname, 0 },
9223 { "file_control_reservebytes", file_control_reservebytes, 0 },
9224 { "file_control_tempfilename", file_control_tempfilename, 0 },
9225 { "file_control_external_reader", file_control_external_reader, 0 },
9226 { "sqlite3_vfs_list", vfs_list, 0 },
9227 { "sqlite3_create_function_v2", test_create_function_v2, 0 },
9229 /* Functions from os.h */
9230 #ifndef SQLITE_OMIT_UTF16
9231 { "add_test_collate", test_collate, 0 },
9232 { "add_test_collate_needed", test_collate_needed, 0 },
9233 { "add_test_function", test_function, 0 },
9234 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 },
9235 #endif
9236 { "sqlite3_test_errstr", test_errstr, 0 },
9237 { "tcl_variable_type", tcl_variable_type, 0 },
9238 #ifndef SQLITE_OMIT_SHARED_CACHE
9239 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
9240 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
9241 #endif
9242 { "sqlite3_libversion_number", test_libversion_number, 0 },
9243 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
9244 #ifndef SQLITE_OMIT_INCRBLOB
9245 { "sqlite3_blob_reopen", test_blob_reopen, 0 },
9246 #endif
9247 { "pcache_stats", test_pcache_stats, 0 },
9248 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
9249 { "sqlite3_unlock_notify", test_unlock_notify, 0 },
9250 #endif
9251 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
9252 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 },
9253 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 },
9254 { "test_sqlite3_log", test_sqlite3_log, 0 },
9255 #ifndef SQLITE_OMIT_EXPLAIN
9256 { "print_explain_query_plan", test_print_eqp, 0 },
9257 #endif
9258 { "strftime", strftime_cmd },
9259 { "sqlite3_test_control", test_test_control },
9260 { ".treetrace", test_treetrace },
9261 #if SQLITE_OS_UNIX
9262 { "getrusage", test_getrusage },
9263 #endif
9264 { "load_static_extension", tclLoadStaticExtensionCmd },
9265 { "sorter_test_fakeheap", sorter_test_fakeheap },
9266 { "sorter_test_sort4_helper", sorter_test_sort4_helper },
9267 #ifdef SQLITE_USER_AUTHENTICATION
9268 { "sqlite3_user_authenticate", test_user_authenticate, 0 },
9269 { "sqlite3_user_add", test_user_add, 0 },
9270 { "sqlite3_user_change", test_user_change, 0 },
9271 { "sqlite3_user_delete", test_user_delete, 0 },
9272 #endif
9273 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
9274 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 },
9275 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 },
9276 #endif
9277 #ifdef SQLITE_ENABLE_SQLLOG
9278 { "sqlite3_config_sqllog", test_config_sqllog, 0 },
9279 #endif
9280 { "vfs_current_time_int64", vfsCurrentTimeInt64, 0 },
9281 #ifdef SQLITE_ENABLE_SNAPSHOT
9282 { "sqlite3_snapshot_get", test_snapshot_get, 0 },
9283 { "sqlite3_snapshot_open", test_snapshot_open, 0 },
9284 { "sqlite3_snapshot_free", test_snapshot_free, 0 },
9285 { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 },
9286 { "sqlite3_snapshot_recover", test_snapshot_recover, 0 },
9287 { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 },
9288 { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 },
9289 { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 },
9290 #endif
9291 { "sqlite3_delete_database", test_delete_database, 0 },
9292 { "atomic_batch_write", test_atomic_batch_write, 0 },
9293 { "sqlite3_mmap_warm", test_mmap_warm, 0 },
9294 { "sqlite3_config_sorterref", test_config_sorterref, 0 },
9295 { "sqlite3_autovacuum_pages", test_autovacuum_pages, 0 },
9296 { "decode_hexdb", test_decode_hexdb, 0 },
9297 { "test_write_db", test_write_db, 0 },
9298 { "sqlite3_register_cksumvfs", test_register_cksumvfs, 0 },
9299 { "sqlite3_unregister_cksumvfs", test_unregister_cksumvfs, 0 },
9300 { "number_of_cores", guess_number_of_cores, 0 },
9301 #ifndef SQLITE_OMIT_VIRTUALTABLE
9302 { "create_null_module", test_create_null_module, 0 },
9303 #endif
9305 static int bitmask_size = sizeof(Bitmask)*8;
9306 static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
9307 int i;
9308 extern int sqlite3_sync_count, sqlite3_fullsync_count;
9309 extern int sqlite3_opentemp_count;
9310 extern int sqlite3_like_count;
9311 extern int sqlite3_xferopt_count;
9312 extern int sqlite3_pager_readdb_count;
9313 extern int sqlite3_pager_writedb_count;
9314 extern int sqlite3_pager_writej_count;
9315 #if SQLITE_OS_WIN
9316 extern LONG volatile sqlite3_os_type;
9317 #endif
9318 #ifdef SQLITE_DEBUG
9319 extern u32 sqlite3WhereTrace;
9320 extern int sqlite3OSTrace;
9321 extern int sqlite3WalTrace;
9322 #endif
9323 #ifdef SQLITE_TEST
9324 #ifdef SQLITE_ENABLE_FTS3
9325 extern int sqlite3_fts3_enable_parentheses;
9326 #endif
9327 #endif
9329 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
9330 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
9332 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
9333 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
9334 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
9336 Tcl_LinkVar(interp, "sqlite_search_count",
9337 (char*)&sqlite3_search_count, TCL_LINK_INT);
9338 Tcl_LinkVar(interp, "sqlite_found_count",
9339 (char*)&sqlite3_found_count, TCL_LINK_INT);
9340 Tcl_LinkVar(interp, "sqlite_sort_count",
9341 (char*)&sqlite3_sort_count, TCL_LINK_INT);
9342 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
9343 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
9344 Tcl_LinkVar(interp, "sqlite_like_count",
9345 (char*)&sqlite3_like_count, TCL_LINK_INT);
9346 Tcl_LinkVar(interp, "sqlite_interrupt_count",
9347 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
9348 Tcl_LinkVar(interp, "sqlite_open_file_count",
9349 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
9350 Tcl_LinkVar(interp, "sqlite_current_time",
9351 (char*)&sqlite3_current_time, TCL_LINK_INT);
9352 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
9353 Tcl_LinkVar(interp, "sqlite_hostid_num",
9354 (char*)&sqlite3_hostid_num, TCL_LINK_INT);
9355 #endif
9356 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
9357 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
9358 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
9359 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
9360 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
9361 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
9362 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
9363 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
9364 #ifndef SQLITE_OMIT_UTF16
9365 Tcl_LinkVar(interp, "unaligned_string_counter",
9366 (char*)&unaligned_string_counter, TCL_LINK_INT);
9367 #endif
9368 #ifndef SQLITE_OMIT_UTF16
9369 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
9370 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
9371 #endif
9372 #if SQLITE_OS_WIN
9373 Tcl_LinkVar(interp, "sqlite_os_type",
9374 (char*)&sqlite3_os_type, TCL_LINK_LONG);
9375 #endif
9376 #ifdef SQLITE_TEST
9378 static const char *query_plan = "*** OBSOLETE VARIABLE ***";
9379 Tcl_LinkVar(interp, "sqlite_query_plan",
9380 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
9382 #endif
9383 #ifdef SQLITE_DEBUG
9384 Tcl_LinkVar(interp, "sqlite_where_trace",
9385 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
9386 Tcl_LinkVar(interp, "sqlite_os_trace",
9387 (char*)&sqlite3OSTrace, TCL_LINK_INT);
9388 #ifndef SQLITE_OMIT_WAL
9389 Tcl_LinkVar(interp, "sqlite_wal_trace",
9390 (char*)&sqlite3WalTrace, TCL_LINK_INT);
9391 #endif
9392 #endif
9393 #ifndef SQLITE_OMIT_DISKIO
9394 Tcl_LinkVar(interp, "sqlite_opentemp_count",
9395 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
9396 #endif
9397 Tcl_LinkVar(interp, "sqlite_static_bind_value",
9398 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
9399 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
9400 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
9401 Tcl_LinkVar(interp, "sqlite_temp_directory",
9402 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
9403 Tcl_LinkVar(interp, "sqlite_data_directory",
9404 (char*)&sqlite3_data_directory, TCL_LINK_STRING);
9405 Tcl_LinkVar(interp, "bitmask_size",
9406 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
9407 Tcl_LinkVar(interp, "longdouble_size",
9408 (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
9409 Tcl_LinkVar(interp, "sqlite_sync_count",
9410 (char*)&sqlite3_sync_count, TCL_LINK_INT);
9411 Tcl_LinkVar(interp, "sqlite_fullsync_count",
9412 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
9413 #if defined(SQLITE_ENABLE_TREETRACE)
9414 Tcl_LinkVar(interp, "sqlite3_unsupported_treetrace",
9415 (char*)&sqlite3TreeTrace, TCL_LINK_INT);
9416 #endif
9417 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
9418 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
9419 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
9420 #endif
9421 return TCL_OK;