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. 10 ** 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. 15 */ 16 #include "sqliteInt.h" 17 #if SQLITE_OS_WIN 18 # include "os_win.h" 19 #endif 20 21 #include "vdbeInt.h" 22 #include "tcl.h" 23 #include <stdlib.h> 24 #include <string.h> 25 26 /* 27 ** This is a copy of the first part of the SqliteDb structure in 28 ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine 29 ** can extract the sqlite3* pointer from an existing Tcl SQLite 30 ** connection. 31 */ 32 struct SqliteDb { 33 sqlite3 *db; 34 }; 35 36 /* 37 ** Convert text generated by the "%p" conversion format back into 38 ** a pointer. 39 */ 40 static int testHexToInt(int h){ 41 if( h>='0' && h<='9' ){ 42 return h - '0'; 43 }else if( h>='a' && h<='f' ){ 44 return h - 'a' + 10; 45 }else{ 46 assert( h>='A' && h<='F' ); 47 return h - 'A' + 10; 48 } 49 } 50 void *sqlite3TestTextToPtr(const char *z){ 51 void *p; 52 u64 v; 53 u32 v2; 54 if( z[0]=='0' && z[1]=='x' ){ 55 z += 2; 56 } 57 v = 0; 58 while( *z ){ 59 v = (v<<4) + testHexToInt(*z); 60 z++; 61 } 62 if( sizeof(p)==sizeof(v) ){ 63 memcpy(&p, &v, sizeof(p)); 64 }else{ 65 assert( sizeof(p)==sizeof(v2) ); 66 v2 = (u32)v; 67 memcpy(&p, &v2, sizeof(p)); 68 } 69 return p; 70 } 71 72 73 /* 74 ** A TCL command that returns the address of the sqlite* pointer 75 ** for an sqlite connection instance. Bad things happen if the 76 ** input is not an sqlite connection. 77 */ 78 static int get_sqlite_pointer( 79 void * clientData, 80 Tcl_Interp *interp, 81 int objc, 82 Tcl_Obj *CONST objv[] 83 ){ 84 struct SqliteDb *p; 85 Tcl_CmdInfo cmdInfo; 86 char zBuf[100]; 87 if( objc!=2 ){ 88 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION"); 89 return TCL_ERROR; 90 } 91 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 92 Tcl_AppendResult(interp, "command not found: ", 93 Tcl_GetString(objv[1]), (char*)0); 94 return TCL_ERROR; 95 } 96 p = (struct SqliteDb*)cmdInfo.objClientData; 97 sprintf(zBuf, "%p", p->db); 98 if( strncmp(zBuf,"0x",2) ){ 99 sprintf(zBuf, "0x%p", p->db); 100 } 101 Tcl_AppendResult(interp, zBuf, 0); 102 return TCL_OK; 103 } 104 105 /* 106 ** Decode a pointer to an sqlite3 object. 107 */ 108 int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ 109 struct SqliteDb *p; 110 Tcl_CmdInfo cmdInfo; 111 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){ 112 p = (struct SqliteDb*)cmdInfo.objClientData; 113 *ppDb = p->db; 114 }else{ 115 *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA); 116 } 117 return TCL_OK; 118 } 119 120 #if SQLITE_OS_WIN 121 /* 122 ** Decode a Win32 HANDLE object. 123 */ 124 int getWin32Handle(Tcl_Interp *interp, const char *zA, LPHANDLE phFile){ 125 *phFile = (HANDLE)sqlite3TestTextToPtr(zA); 126 return TCL_OK; 127 } 128 #endif 129 130 extern const char *sqlite3ErrName(int); 131 #define t1ErrorName sqlite3ErrName 132 133 /* 134 ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the 135 ** fact that the sqlite3* is the first field in the Vdbe structure. 136 */ 137 #define StmtToDb(X) sqlite3_db_handle(X) 138 139 /* 140 ** Check a return value to make sure it agrees with the results 141 ** from sqlite3_errcode. 142 */ 143 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ 144 if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK 145 && sqlite3_errcode(db)!=rc ){ 146 char zBuf[200]; 147 int r2 = sqlite3_errcode(db); 148 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)", 149 t1ErrorName(rc), rc, t1ErrorName(r2), r2); 150 Tcl_ResetResult(interp); 151 Tcl_AppendResult(interp, zBuf, 0); 152 return 1; 153 } 154 return 0; 155 } 156 157 /* 158 ** Decode a pointer to an sqlite3_stmt object. 159 */ 160 static int getStmtPointer( 161 Tcl_Interp *interp, 162 const char *zArg, 163 sqlite3_stmt **ppStmt 164 ){ 165 *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg); 166 return TCL_OK; 167 } 168 169 /* 170 ** Generate a text representation of a pointer that can be understood 171 ** by the getDbPointer and getVmPointer routines above. 172 ** 173 ** The problem is, on some machines (Solaris) if you do a printf with 174 ** "%p" you cannot turn around and do a scanf with the same "%p" and 175 ** get your pointer back. You have to prepend a "0x" before it will 176 ** work. Or at least that is what is reported to me (drh). But this 177 ** behavior varies from machine to machine. The solution used her is 178 ** to test the string right after it is generated to see if it can be 179 ** understood by scanf, and if not, try prepending an "0x" to see if 180 ** that helps. If nothing works, a fatal error is generated. 181 */ 182 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){ 183 sqlite3_snprintf(100, zPtr, "%p", p); 184 return TCL_OK; 185 } 186 187 /* 188 ** The callback routine for sqlite3_exec_printf(). 189 */ 190 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ 191 Tcl_DString *str = (Tcl_DString*)pArg; 192 int i; 193 194 if( Tcl_DStringLength(str)==0 ){ 195 for(i=0; i<argc; i++){ 196 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); 197 } 198 } 199 for(i=0; i<argc; i++){ 200 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); 201 } 202 return 0; 203 } 204 205 /* 206 ** The I/O tracing callback. 207 */ 208 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 209 static FILE *iotrace_file = 0; 210 static void io_trace_callback(const char *zFormat, ...){ 211 va_list ap; 212 va_start(ap, zFormat); 213 vfprintf(iotrace_file, zFormat, ap); 214 va_end(ap); 215 fflush(iotrace_file); 216 } 217 #endif 218 219 /* 220 ** Usage: io_trace FILENAME 221 ** 222 ** Turn I/O tracing on or off. If FILENAME is not an empty string, 223 ** I/O tracing begins going into FILENAME. If FILENAME is an empty 224 ** string, I/O tracing is turned off. 225 */ 226 static int test_io_trace( 227 void *NotUsed, 228 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 229 int argc, /* Number of arguments */ 230 char **argv /* Text of each argument */ 231 ){ 232 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 233 if( argc!=2 ){ 234 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 235 " FILENAME\"", 0); 236 return TCL_ERROR; 237 } 238 if( iotrace_file ){ 239 if( iotrace_file!=stdout && iotrace_file!=stderr ){ 240 fclose(iotrace_file); 241 } 242 iotrace_file = 0; 243 sqlite3IoTrace = 0; 244 } 245 if( argv[1][0] ){ 246 if( strcmp(argv[1],"stdout")==0 ){ 247 iotrace_file = stdout; 248 }else if( strcmp(argv[1],"stderr")==0 ){ 249 iotrace_file = stderr; 250 }else{ 251 iotrace_file = fopen(argv[1], "w"); 252 } 253 sqlite3IoTrace = io_trace_callback; 254 } 255 #endif 256 return TCL_OK; 257 } 258 259 /* 260 ** Usage: clang_sanitize_address 261 ** 262 ** Returns true if the program was compiled using clang with the 263 ** -fsanitize=address switch on the command line. False otherwise. 264 ** 265 ** Also return true if the OMIT_MISUSE environment variable exists. 266 */ 267 static int clang_sanitize_address( 268 void *NotUsed, 269 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 270 int argc, /* Number of arguments */ 271 char **argv /* Text of each argument */ 272 ){ 273 int res = 0; 274 #if defined(__has_feature) 275 # if __has_feature(address_sanitizer) 276 res = 1; 277 # endif 278 #endif 279 if( res==0 && getenv("OMIT_MISUSE")!=0 ) res = 1; 280 Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); 281 return TCL_OK; 282 } 283 284 /* 285 ** Usage: sqlite3_exec_printf DB FORMAT STRING 286 ** 287 ** Invoke the sqlite3_exec_printf() interface using the open database 288 ** DB. The SQL is the string FORMAT. The format string should contain 289 ** one %s or %q. STRING is the value inserted into %s or %q. 290 */ 291 static int test_exec_printf( 292 void *NotUsed, 293 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 294 int argc, /* Number of arguments */ 295 char **argv /* Text of each argument */ 296 ){ 297 sqlite3 *db; 298 Tcl_DString str; 299 int rc; 300 char *zErr = 0; 301 char *zSql; 302 char zBuf[30]; 303 if( argc!=4 ){ 304 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 305 " DB FORMAT STRING", 0); 306 return TCL_ERROR; 307 } 308 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 309 Tcl_DStringInit(&str); 310 zSql = sqlite3_mprintf(argv[2], argv[3]); 311 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); 312 sqlite3_free(zSql); 313 sprintf(zBuf, "%d", rc); 314 Tcl_AppendElement(interp, zBuf); 315 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); 316 Tcl_DStringFree(&str); 317 if( zErr ) sqlite3_free(zErr); 318 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 319 return TCL_OK; 320 } 321 322 /* 323 ** Usage: sqlite3_exec_hex DB HEX 324 ** 325 ** Invoke the sqlite3_exec() on a string that is obtained by translating 326 ** HEX into ASCII. Most characters are translated as is. %HH becomes 327 ** a hex character. 328 */ 329 static int test_exec_hex( 330 void *NotUsed, 331 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 332 int argc, /* Number of arguments */ 333 char **argv /* Text of each argument */ 334 ){ 335 sqlite3 *db; 336 Tcl_DString str; 337 int rc, i, j; 338 char *zErr = 0; 339 char *zHex; 340 char zSql[500]; 341 char zBuf[30]; 342 if( argc!=3 ){ 343 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 344 " DB HEX", 0); 345 return TCL_ERROR; 346 } 347 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 348 zHex = argv[2]; 349 for(i=j=0; i<sizeof(zSql) && zHex[j]; i++, j++){ 350 if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){ 351 zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]); 352 j += 2; 353 }else{ 354 zSql[i] = zHex[j]; 355 } 356 } 357 zSql[i] = 0; 358 Tcl_DStringInit(&str); 359 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); 360 sprintf(zBuf, "%d", rc); 361 Tcl_AppendElement(interp, zBuf); 362 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); 363 Tcl_DStringFree(&str); 364 if( zErr ) sqlite3_free(zErr); 365 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 366 return TCL_OK; 367 } 368 369 /* 370 ** Usage: db_enter DB 371 ** db_leave DB 372 ** 373 ** Enter or leave the mutex on a database connection. 374 */ 375 static int db_enter( 376 void *NotUsed, 377 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 378 int argc, /* Number of arguments */ 379 char **argv /* Text of each argument */ 380 ){ 381 sqlite3 *db; 382 if( argc!=2 ){ 383 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 384 " DB", 0); 385 return TCL_ERROR; 386 } 387 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 388 sqlite3_mutex_enter(db->mutex); 389 return TCL_OK; 390 } 391 static int db_leave( 392 void *NotUsed, 393 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 394 int argc, /* Number of arguments */ 395 char **argv /* Text of each argument */ 396 ){ 397 sqlite3 *db; 398 if( argc!=2 ){ 399 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 400 " DB", 0); 401 return TCL_ERROR; 402 } 403 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 404 sqlite3_mutex_leave(db->mutex); 405 return TCL_OK; 406 } 407 408 /* 409 ** Usage: sqlite3_exec DB SQL 410 ** 411 ** Invoke the sqlite3_exec interface using the open database DB 412 */ 413 static int test_exec( 414 void *NotUsed, 415 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 416 int argc, /* Number of arguments */ 417 char **argv /* Text of each argument */ 418 ){ 419 sqlite3 *db; 420 Tcl_DString str; 421 int rc; 422 char *zErr = 0; 423 char *zSql; 424 int i, j; 425 char zBuf[30]; 426 if( argc!=3 ){ 427 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 428 " DB SQL", 0); 429 return TCL_ERROR; 430 } 431 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 432 Tcl_DStringInit(&str); 433 zSql = sqlite3_mprintf("%s", argv[2]); 434 for(i=j=0; zSql[i];){ 435 if( zSql[i]=='%' ){ 436 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]); 437 i += 3; 438 }else{ 439 zSql[j++] = zSql[i++]; 440 } 441 } 442 zSql[j] = 0; 443 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); 444 sqlite3_free(zSql); 445 sprintf(zBuf, "%d", rc); 446 Tcl_AppendElement(interp, zBuf); 447 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); 448 Tcl_DStringFree(&str); 449 if( zErr ) sqlite3_free(zErr); 450 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 451 return TCL_OK; 452 } 453 454 /* 455 ** Usage: sqlite3_exec_nr DB SQL 456 ** 457 ** Invoke the sqlite3_exec interface using the open database DB. Discard 458 ** all results 459 */ 460 static int test_exec_nr( 461 void *NotUsed, 462 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 463 int argc, /* Number of arguments */ 464 char **argv /* Text of each argument */ 465 ){ 466 sqlite3 *db; 467 int rc; 468 char *zErr = 0; 469 if( argc!=3 ){ 470 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 471 " DB SQL", 0); 472 return TCL_ERROR; 473 } 474 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 475 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); 476 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 477 return TCL_OK; 478 } 479 480 /* 481 ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ... 482 ** 483 ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to 484 ** concatenate arg0 through argn using separator as the separator. 485 ** Return the result. 486 */ 487 static int test_mprintf_z( 488 void *NotUsed, 489 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 490 int argc, /* Number of arguments */ 491 char **argv /* Text of each argument */ 492 ){ 493 char *zResult = 0; 494 int i; 495 496 for(i=2; i<argc && (i==2 || zResult); i++){ 497 zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]); 498 } 499 Tcl_AppendResult(interp, zResult, 0); 500 sqlite3_free(zResult); 501 return TCL_OK; 502 } 503 504 /* 505 ** Usage: sqlite3_mprintf_n_test STRING 506 ** 507 ** Test the %n format of sqlite_mprintf(). Return the length of the 508 ** input string. 509 */ 510 static int test_mprintf_n( 511 void *NotUsed, 512 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 513 int argc, /* Number of arguments */ 514 char **argv /* Text of each argument */ 515 ){ 516 char *zStr; 517 int n = 0; 518 zStr = sqlite3_mprintf("%s%n", argv[1], &n); 519 sqlite3_free(zStr); 520 Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); 521 return TCL_OK; 522 } 523 524 /* 525 ** Usage: sqlite3_snprintf_int SIZE FORMAT INT 526 ** 527 ** Test the of sqlite3_snprintf() routine. SIZE is the size of the 528 ** output buffer in bytes. The maximum size is 100. FORMAT is the 529 ** format string. INT is a single integer argument. The FORMAT 530 ** string must require no more than this one integer argument. If 531 ** You pass in a format string that requires more than one argument, 532 ** bad things will happen. 533 */ 534 static int test_snprintf_int( 535 void *NotUsed, 536 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 537 int argc, /* Number of arguments */ 538 char **argv /* Text of each argument */ 539 ){ 540 char zStr[100]; 541 int n = atoi(argv[1]); 542 const char *zFormat = argv[2]; 543 int a1 = atoi(argv[3]); 544 if( n>sizeof(zStr) ) n = sizeof(zStr); 545 sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz"); 546 sqlite3_snprintf(n, zStr, zFormat, a1); 547 Tcl_AppendResult(interp, zStr, 0); 548 return TCL_OK; 549 } 550 551 #ifndef SQLITE_OMIT_GET_TABLE 552 553 /* 554 ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts? 555 ** 556 ** Invoke the sqlite3_get_table_printf() interface using the open database 557 ** DB. The SQL is the string FORMAT. The format string should contain 558 ** one %s or %q. STRING is the value inserted into %s or %q. 559 */ 560 static int test_get_table_printf( 561 void *NotUsed, 562 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 563 int argc, /* Number of arguments */ 564 char **argv /* Text of each argument */ 565 ){ 566 sqlite3 *db; 567 Tcl_DString str; 568 int rc; 569 char *zErr = 0; 570 int nRow = 0, nCol = 0; 571 char **aResult; 572 int i; 573 char zBuf[30]; 574 char *zSql; 575 int resCount = -1; 576 if( argc==5 ){ 577 if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR; 578 } 579 if( argc!=4 && argc!=5 ){ 580 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 581 " DB FORMAT STRING ?COUNT?", 0); 582 return TCL_ERROR; 583 } 584 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 585 Tcl_DStringInit(&str); 586 zSql = sqlite3_mprintf(argv[2],argv[3]); 587 if( argc==5 ){ 588 rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr); 589 }else{ 590 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr); 591 resCount = (nRow+1)*nCol; 592 } 593 sqlite3_free(zSql); 594 sprintf(zBuf, "%d", rc); 595 Tcl_AppendElement(interp, zBuf); 596 if( rc==SQLITE_OK ){ 597 if( argc==4 ){ 598 sprintf(zBuf, "%d", nRow); 599 Tcl_AppendElement(interp, zBuf); 600 sprintf(zBuf, "%d", nCol); 601 Tcl_AppendElement(interp, zBuf); 602 } 603 for(i=0; i<resCount; i++){ 604 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); 605 } 606 }else{ 607 Tcl_AppendElement(interp, zErr); 608 } 609 sqlite3_free_table(aResult); 610 if( zErr ) sqlite3_free(zErr); 611 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 612 return TCL_OK; 613 } 614 615 #endif /* SQLITE_OMIT_GET_TABLE */ 616 617 618 /* 619 ** Usage: sqlite3_last_insert_rowid DB 620 ** 621 ** Returns the integer ROWID of the most recent insert. 622 */ 623 static int test_last_rowid( 624 void *NotUsed, 625 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 626 int argc, /* Number of arguments */ 627 char **argv /* Text of each argument */ 628 ){ 629 sqlite3 *db; 630 char zBuf[30]; 631 632 if( argc!=2 ){ 633 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); 634 return TCL_ERROR; 635 } 636 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 637 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db)); 638 Tcl_AppendResult(interp, zBuf, 0); 639 return SQLITE_OK; 640 } 641 642 /* 643 ** Usage: sqlite3_key DB KEY 644 ** 645 ** Set the codec key. 646 */ 647 static int test_key( 648 void *NotUsed, 649 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 650 int argc, /* Number of arguments */ 651 char **argv /* Text of each argument */ 652 ){ 653 #ifdef SQLITE_HAS_CODEC 654 sqlite3 *db; 655 const char *zKey; 656 int nKey; 657 if( argc!=3 ){ 658 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 659 " FILENAME\"", 0); 660 return TCL_ERROR; 661 } 662 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 663 zKey = argv[2]; 664 nKey = strlen(zKey); 665 sqlite3_key(db, zKey, nKey); 666 #endif 667 return TCL_OK; 668 } 669 670 /* 671 ** Usage: sqlite3_rekey DB KEY 672 ** 673 ** Change the codec key. 674 */ 675 static int test_rekey( 676 void *NotUsed, 677 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 678 int argc, /* Number of arguments */ 679 char **argv /* Text of each argument */ 680 ){ 681 #ifdef SQLITE_HAS_CODEC 682 sqlite3 *db; 683 const char *zKey; 684 int nKey; 685 if( argc!=3 ){ 686 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 687 " FILENAME\"", 0); 688 return TCL_ERROR; 689 } 690 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 691 zKey = argv[2]; 692 nKey = strlen(zKey); 693 sqlite3_rekey(db, zKey, nKey); 694 #endif 695 return TCL_OK; 696 } 697 698 /* 699 ** Usage: sqlite3_close DB 700 ** 701 ** Closes the database opened by sqlite3_open. 702 */ 703 static int sqlite_test_close( 704 void *NotUsed, 705 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 706 int argc, /* Number of arguments */ 707 char **argv /* Text of each argument */ 708 ){ 709 sqlite3 *db; 710 int rc; 711 if( argc!=2 ){ 712 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 713 " FILENAME\"", 0); 714 return TCL_ERROR; 715 } 716 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 717 rc = sqlite3_close(db); 718 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 719 return TCL_OK; 720 } 721 722 /* 723 ** Usage: sqlite3_close_v2 DB 724 ** 725 ** Closes the database opened by sqlite3_open. 726 */ 727 static int sqlite_test_close_v2( 728 void *NotUsed, 729 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 730 int argc, /* Number of arguments */ 731 char **argv /* Text of each argument */ 732 ){ 733 sqlite3 *db; 734 int rc; 735 if( argc!=2 ){ 736 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 737 " FILENAME\"", 0); 738 return TCL_ERROR; 739 } 740 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 741 rc = sqlite3_close_v2(db); 742 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 743 return TCL_OK; 744 } 745 746 /* 747 ** Implementation of the x_coalesce() function. 748 ** Return the first argument non-NULL argument. 749 */ 750 static void t1_ifnullFunc( 751 sqlite3_context *context, 752 int argc, 753 sqlite3_value **argv 754 ){ 755 int i; 756 for(i=0; i<argc; i++){ 757 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 758 int n = sqlite3_value_bytes(argv[i]); 759 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]), 760 n, SQLITE_TRANSIENT); 761 break; 762 } 763 } 764 } 765 766 /* 767 ** These are test functions. hex8() interprets its argument as 768 ** UTF8 and returns a hex encoding. hex16le() interprets its argument 769 ** as UTF16le and returns a hex encoding. 770 */ 771 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ 772 const unsigned char *z; 773 int i; 774 char zBuf[200]; 775 z = sqlite3_value_text(argv[0]); 776 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ 777 sprintf(&zBuf[i*2], "%02x", z[i]&0xff); 778 } 779 zBuf[i*2] = 0; 780 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); 781 } 782 #ifndef SQLITE_OMIT_UTF16 783 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ 784 const unsigned short int *z; 785 int i; 786 char zBuf[400]; 787 z = sqlite3_value_text16(argv[0]); 788 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ 789 sprintf(&zBuf[i*4], "%04x", z[i]&0xff); 790 } 791 zBuf[i*4] = 0; 792 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); 793 } 794 #endif 795 796 /* 797 ** A structure into which to accumulate text. 798 */ 799 struct dstr { 800 int nAlloc; /* Space allocated */ 801 int nUsed; /* Space used */ 802 char *z; /* The space */ 803 }; 804 805 /* 806 ** Append text to a dstr 807 */ 808 static void dstrAppend(struct dstr *p, const char *z, int divider){ 809 int n = (int)strlen(z); 810 if( p->nUsed + n + 2 > p->nAlloc ){ 811 char *zNew; 812 p->nAlloc = p->nAlloc*2 + n + 200; 813 zNew = sqlite3_realloc(p->z, p->nAlloc); 814 if( zNew==0 ){ 815 sqlite3_free(p->z); 816 memset(p, 0, sizeof(*p)); 817 return; 818 } 819 p->z = zNew; 820 } 821 if( divider && p->nUsed>0 ){ 822 p->z[p->nUsed++] = divider; 823 } 824 memcpy(&p->z[p->nUsed], z, n+1); 825 p->nUsed += n; 826 } 827 828 /* 829 ** Invoked for each callback from sqlite3ExecFunc 830 */ 831 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ 832 struct dstr *p = (struct dstr*)pData; 833 int i; 834 for(i=0; i<argc; i++){ 835 if( argv[i]==0 ){ 836 dstrAppend(p, "NULL", ' '); 837 }else{ 838 dstrAppend(p, argv[i], ' '); 839 } 840 } 841 return 0; 842 } 843 844 /* 845 ** Implementation of the x_sqlite_exec() function. This function takes 846 ** a single argument and attempts to execute that argument as SQL code. 847 ** This is illegal and should set the SQLITE_MISUSE flag on the database. 848 ** 849 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec() 850 ** from within a function call. 851 ** 852 ** This routine simulates the effect of having two threads attempt to 853 ** use the same database at the same time. 854 */ 855 static void sqlite3ExecFunc( 856 sqlite3_context *context, 857 int argc, 858 sqlite3_value **argv 859 ){ 860 struct dstr x; 861 memset(&x, 0, sizeof(x)); 862 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), 863 (char*)sqlite3_value_text(argv[0]), 864 execFuncCallback, &x, 0); 865 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); 866 sqlite3_free(x.z); 867 } 868 869 /* 870 ** Implementation of tkt2213func(), a scalar function that takes exactly 871 ** one argument. It has two interesting features: 872 ** 873 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*. 874 ** If the three pointers returned are not the same an SQL error is raised. 875 ** 876 ** * Otherwise it returns a copy of the text representation of its 877 ** argument in such a way as the VDBE representation is a Mem* cell 878 ** with the MEM_Term flag clear. 879 ** 880 ** Ticket #2213 can therefore be tested by evaluating the following 881 ** SQL expression: 882 ** 883 ** tkt2213func(tkt2213func('a string')); 884 */ 885 static void tkt2213Function( 886 sqlite3_context *context, 887 int argc, 888 sqlite3_value **argv 889 ){ 890 int nText; 891 unsigned char const *zText1; 892 unsigned char const *zText2; 893 unsigned char const *zText3; 894 895 nText = sqlite3_value_bytes(argv[0]); 896 zText1 = sqlite3_value_text(argv[0]); 897 zText2 = sqlite3_value_text(argv[0]); 898 zText3 = sqlite3_value_text(argv[0]); 899 900 if( zText1!=zText2 || zText2!=zText3 ){ 901 sqlite3_result_error(context, "tkt2213 is not fixed", -1); 902 }else{ 903 char *zCopy = (char *)sqlite3_malloc(nText); 904 memcpy(zCopy, zText1, nText); 905 sqlite3_result_text(context, zCopy, nText, sqlite3_free); 906 } 907 } 908 909 /* 910 ** The following SQL function takes 4 arguments. The 2nd and 911 ** 4th argument must be one of these strings: 'text', 'text16', 912 ** or 'blob' corresponding to API functions 913 ** 914 ** sqlite3_value_text() 915 ** sqlite3_value_text16() 916 ** sqlite3_value_blob() 917 ** 918 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop', 919 ** corresponding to APIs: 920 ** 921 ** sqlite3_value_bytes() 922 ** sqlite3_value_bytes16() 923 ** noop 924 ** 925 ** The APIs designated by the 2nd through 4th arguments are applied 926 ** to the first argument in order. If the pointers returned by the 927 ** second and fourth are different, this routine returns 1. Otherwise, 928 ** this routine returns 0. 929 ** 930 ** This function is used to test to see when returned pointers from 931 ** the _text(), _text16() and _blob() APIs become invalidated. 932 */ 933 static void ptrChngFunction( 934 sqlite3_context *context, 935 int argc, 936 sqlite3_value **argv 937 ){ 938 const void *p1, *p2; 939 const char *zCmd; 940 if( argc!=4 ) return; 941 zCmd = (const char*)sqlite3_value_text(argv[1]); 942 if( zCmd==0 ) return; 943 if( strcmp(zCmd,"text")==0 ){ 944 p1 = (const void*)sqlite3_value_text(argv[0]); 945 #ifndef SQLITE_OMIT_UTF16 946 }else if( strcmp(zCmd, "text16")==0 ){ 947 p1 = (const void*)sqlite3_value_text16(argv[0]); 948 #endif 949 }else if( strcmp(zCmd, "blob")==0 ){ 950 p1 = (const void*)sqlite3_value_blob(argv[0]); 951 }else{ 952 return; 953 } 954 zCmd = (const char*)sqlite3_value_text(argv[2]); 955 if( zCmd==0 ) return; 956 if( strcmp(zCmd,"bytes")==0 ){ 957 sqlite3_value_bytes(argv[0]); 958 #ifndef SQLITE_OMIT_UTF16 959 }else if( strcmp(zCmd, "bytes16")==0 ){ 960 sqlite3_value_bytes16(argv[0]); 961 #endif 962 }else if( strcmp(zCmd, "noop")==0 ){ 963 /* do nothing */ 964 }else{ 965 return; 966 } 967 zCmd = (const char*)sqlite3_value_text(argv[3]); 968 if( zCmd==0 ) return; 969 if( strcmp(zCmd,"text")==0 ){ 970 p2 = (const void*)sqlite3_value_text(argv[0]); 971 #ifndef SQLITE_OMIT_UTF16 972 }else if( strcmp(zCmd, "text16")==0 ){ 973 p2 = (const void*)sqlite3_value_text16(argv[0]); 974 #endif 975 }else if( strcmp(zCmd, "blob")==0 ){ 976 p2 = (const void*)sqlite3_value_blob(argv[0]); 977 }else{ 978 return; 979 } 980 sqlite3_result_int(context, p1!=p2); 981 } 982 983 /* 984 ** This SQL function returns a different answer each time it is called, even if 985 ** the arguments are the same. 986 */ 987 static void nondeterministicFunction( 988 sqlite3_context *context, 989 int argc, 990 sqlite3_value **argv 991 ){ 992 static int cnt = 0; 993 sqlite3_result_int(context, cnt++); 994 } 995 996 /* 997 ** Usage: sqlite3_create_function DB 998 ** 999 ** Call the sqlite3_create_function API on the given database in order 1000 ** to create a function named "x_coalesce". This function does the same thing 1001 ** as the "coalesce" function. This function also registers an SQL function 1002 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec() 1003 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error. 1004 ** The effect is similar to trying to use the same database connection from 1005 ** two threads at the same time. 1006 ** 1007 ** The original motivation for this routine was to be able to call the 1008 ** sqlite3_create_function function while a query is in progress in order 1009 ** to test the SQLITE_MISUSE detection logic. 1010 */ 1011 static int test_create_function( 1012 void *NotUsed, 1013 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1014 int argc, /* Number of arguments */ 1015 char **argv /* Text of each argument */ 1016 ){ 1017 int rc; 1018 sqlite3 *db; 1019 1020 if( argc!=2 ){ 1021 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1022 " DB\"", 0); 1023 return TCL_ERROR; 1024 } 1025 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 1026 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0, 1027 t1_ifnullFunc, 0, 0); 1028 if( rc==SQLITE_OK ){ 1029 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, 1030 0, hex8Func, 0, 0); 1031 } 1032 #ifndef SQLITE_OMIT_UTF16 1033 if( rc==SQLITE_OK ){ 1034 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC, 1035 0, hex16Func, 0, 0); 1036 } 1037 #endif 1038 if( rc==SQLITE_OK ){ 1039 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, 1040 tkt2213Function, 0, 0); 1041 } 1042 if( rc==SQLITE_OK ){ 1043 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, 1044 ptrChngFunction, 0, 0); 1045 } 1046 1047 /* Functions counter1() and counter2() have the same implementation - they 1048 ** both return an ascending integer with each call. But counter1() is marked 1049 ** as non-deterministic and counter2() is marked as deterministic. 1050 */ 1051 if( rc==SQLITE_OK ){ 1052 rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8, 1053 0, nondeterministicFunction, 0, 0); 1054 } 1055 if( rc==SQLITE_OK ){ 1056 rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1057 0, nondeterministicFunction, 0, 0); 1058 } 1059 1060 #ifndef SQLITE_OMIT_UTF16 1061 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also 1062 ** because it is not tested anywhere else. */ 1063 if( rc==SQLITE_OK ){ 1064 const void *zUtf16; 1065 sqlite3_value *pVal; 1066 sqlite3_mutex_enter(db->mutex); 1067 pVal = sqlite3ValueNew(db); 1068 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); 1069 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 1070 if( db->mallocFailed ){ 1071 rc = SQLITE_NOMEM; 1072 }else{ 1073 rc = sqlite3_create_function16(db, zUtf16, 1074 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); 1075 } 1076 sqlite3ValueFree(pVal); 1077 sqlite3_mutex_leave(db->mutex); 1078 } 1079 #endif 1080 1081 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 1082 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 1083 return TCL_OK; 1084 } 1085 1086 /* 1087 ** Routines to implement the x_count() aggregate function. 1088 ** 1089 ** x_count() counts the number of non-null arguments. But there are 1090 ** some twists for testing purposes. 1091 ** 1092 ** If the argument to x_count() is 40 then a UTF-8 error is reported 1093 ** on the step function. If x_count(41) is seen, then a UTF-16 error 1094 ** is reported on the step function. If the total count is 42, then 1095 ** a UTF-8 error is reported on the finalize function. 1096 */ 1097 typedef struct t1CountCtx t1CountCtx; 1098 struct t1CountCtx { 1099 int n; 1100 }; 1101 static void t1CountStep( 1102 sqlite3_context *context, 1103 int argc, 1104 sqlite3_value **argv 1105 ){ 1106 t1CountCtx *p; 1107 p = sqlite3_aggregate_context(context, sizeof(*p)); 1108 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ 1109 p->n++; 1110 } 1111 if( argc>0 ){ 1112 int v = sqlite3_value_int(argv[0]); 1113 if( v==40 ){ 1114 sqlite3_result_error(context, "value of 40 handed to x_count", -1); 1115 #ifndef SQLITE_OMIT_UTF16 1116 }else if( v==41 ){ 1117 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; 1118 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1); 1119 #endif 1120 } 1121 } 1122 } 1123 static void t1CountFinalize(sqlite3_context *context){ 1124 t1CountCtx *p; 1125 p = sqlite3_aggregate_context(context, sizeof(*p)); 1126 if( p ){ 1127 if( p->n==42 ){ 1128 sqlite3_result_error(context, "x_count totals to 42", -1); 1129 }else{ 1130 sqlite3_result_int(context, p ? p->n : 0); 1131 } 1132 } 1133 } 1134 1135 #ifndef SQLITE_OMIT_DEPRECATED 1136 static void legacyCountStep( 1137 sqlite3_context *context, 1138 int argc, 1139 sqlite3_value **argv 1140 ){ 1141 /* no-op */ 1142 } 1143 1144 static void legacyCountFinalize(sqlite3_context *context){ 1145 sqlite3_result_int(context, sqlite3_aggregate_count(context)); 1146 } 1147 #endif 1148 1149 /* 1150 ** Usage: sqlite3_create_aggregate DB 1151 ** 1152 ** Call the sqlite3_create_function API on the given database in order 1153 ** to create a function named "x_count". This function is similar 1154 ** to the built-in count() function, with a few special quirks 1155 ** for testing the sqlite3_result_error() APIs. 1156 ** 1157 ** The original motivation for this routine was to be able to call the 1158 ** sqlite3_create_aggregate function while a query is in progress in order 1159 ** to test the SQLITE_MISUSE detection logic. See misuse.test. 1160 ** 1161 ** This routine was later extended to test the use of sqlite3_result_error() 1162 ** within aggregate functions. 1163 ** 1164 ** Later: It is now also extended to register the aggregate function 1165 ** "legacy_count()" with the supplied database handle. This is used 1166 ** to test the deprecated sqlite3_aggregate_count() API. 1167 */ 1168 static int test_create_aggregate( 1169 void *NotUsed, 1170 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1171 int argc, /* Number of arguments */ 1172 char **argv /* Text of each argument */ 1173 ){ 1174 sqlite3 *db; 1175 int rc; 1176 if( argc!=2 ){ 1177 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1178 " FILENAME\"", 0); 1179 return TCL_ERROR; 1180 } 1181 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 1182 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, 1183 t1CountStep,t1CountFinalize); 1184 if( rc==SQLITE_OK ){ 1185 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, 1186 t1CountStep,t1CountFinalize); 1187 } 1188 #ifndef SQLITE_OMIT_DEPRECATED 1189 if( rc==SQLITE_OK ){ 1190 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0, 1191 legacyCountStep, legacyCountFinalize 1192 ); 1193 } 1194 #endif 1195 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 1196 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 1197 return TCL_OK; 1198 } 1199 1200 1201 /* 1202 ** Usage: printf TEXT 1203 ** 1204 ** Send output to printf. Use this rather than puts to merge the output 1205 ** in the correct sequence with debugging printfs inserted into C code. 1206 ** Puts uses a separate buffer and debugging statements will be out of 1207 ** sequence if it is used. 1208 */ 1209 static int test_printf( 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 */ 1214 ){ 1215 if( argc!=2 ){ 1216 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1217 " TEXT\"", 0); 1218 return TCL_ERROR; 1219 } 1220 printf("%s\n", argv[1]); 1221 return TCL_OK; 1222 } 1223 1224 1225 1226 /* 1227 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER 1228 ** 1229 ** Call mprintf with three integer arguments 1230 */ 1231 static int sqlite3_mprintf_int( 1232 void *NotUsed, 1233 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1234 int argc, /* Number of arguments */ 1235 char **argv /* Text of each argument */ 1236 ){ 1237 int a[3], i; 1238 char *z; 1239 if( argc!=5 ){ 1240 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1241 " FORMAT INT INT INT\"", 0); 1242 return TCL_ERROR; 1243 } 1244 for(i=2; i<5; i++){ 1245 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; 1246 } 1247 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); 1248 Tcl_AppendResult(interp, z, 0); 1249 sqlite3_free(z); 1250 return TCL_OK; 1251 } 1252 1253 /* 1254 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER 1255 ** 1256 ** Call mprintf with three 64-bit integer arguments 1257 */ 1258 static int sqlite3_mprintf_int64( 1259 void *NotUsed, 1260 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1261 int argc, /* Number of arguments */ 1262 char **argv /* Text of each argument */ 1263 ){ 1264 int i; 1265 sqlite_int64 a[3]; 1266 char *z; 1267 if( argc!=5 ){ 1268 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1269 " FORMAT INT INT INT\"", 0); 1270 return TCL_ERROR; 1271 } 1272 for(i=2; i<5; i++){ 1273 if( sqlite3Atoi64(argv[i], &a[i-2], 1000000, SQLITE_UTF8) ){ 1274 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); 1275 return TCL_ERROR; 1276 } 1277 } 1278 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); 1279 Tcl_AppendResult(interp, z, 0); 1280 sqlite3_free(z); 1281 return TCL_OK; 1282 } 1283 1284 /* 1285 ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER 1286 ** 1287 ** Call mprintf with three long integer arguments. This might be the 1288 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on 1289 ** platform. 1290 */ 1291 static int sqlite3_mprintf_long( 1292 void *NotUsed, 1293 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1294 int argc, /* Number of arguments */ 1295 char **argv /* Text of each argument */ 1296 ){ 1297 int i; 1298 long int a[3]; 1299 int b[3]; 1300 char *z; 1301 if( argc!=5 ){ 1302 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1303 " FORMAT INT INT INT\"", 0); 1304 return TCL_ERROR; 1305 } 1306 for(i=2; i<5; i++){ 1307 if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR; 1308 a[i-2] = (long int)b[i-2]; 1309 a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1; 1310 } 1311 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); 1312 Tcl_AppendResult(interp, z, 0); 1313 sqlite3_free(z); 1314 return TCL_OK; 1315 } 1316 1317 /* 1318 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING 1319 ** 1320 ** Call mprintf with two integer arguments and one string argument 1321 */ 1322 static int sqlite3_mprintf_str( 1323 void *NotUsed, 1324 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1325 int argc, /* Number of arguments */ 1326 char **argv /* Text of each argument */ 1327 ){ 1328 int a[3], i; 1329 char *z; 1330 if( argc<4 || argc>5 ){ 1331 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1332 " FORMAT INT INT ?STRING?\"", 0); 1333 return TCL_ERROR; 1334 } 1335 for(i=2; i<4; i++){ 1336 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; 1337 } 1338 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL); 1339 Tcl_AppendResult(interp, z, 0); 1340 sqlite3_free(z); 1341 return TCL_OK; 1342 } 1343 1344 /* 1345 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING 1346 ** 1347 ** Call mprintf with two integer arguments and one string argument 1348 */ 1349 static int sqlite3_snprintf_str( 1350 void *NotUsed, 1351 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1352 int argc, /* Number of arguments */ 1353 char **argv /* Text of each argument */ 1354 ){ 1355 int a[3], i; 1356 int n; 1357 char *z; 1358 if( argc<5 || argc>6 ){ 1359 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1360 " INT FORMAT INT INT ?STRING?\"", 0); 1361 return TCL_ERROR; 1362 } 1363 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; 1364 if( n<0 ){ 1365 Tcl_AppendResult(interp, "N must be non-negative", 0); 1366 return TCL_ERROR; 1367 } 1368 for(i=3; i<5; i++){ 1369 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR; 1370 } 1371 z = sqlite3_malloc( n+1 ); 1372 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); 1373 Tcl_AppendResult(interp, z, 0); 1374 sqlite3_free(z); 1375 return TCL_OK; 1376 } 1377 1378 /* 1379 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE 1380 ** 1381 ** Call mprintf with two integer arguments and one double argument 1382 */ 1383 static int sqlite3_mprintf_double( 1384 void *NotUsed, 1385 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1386 int argc, /* Number of arguments */ 1387 char **argv /* Text of each argument */ 1388 ){ 1389 int a[3], i; 1390 double r; 1391 char *z; 1392 if( argc!=5 ){ 1393 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1394 " FORMAT INT INT DOUBLE\"", 0); 1395 return TCL_ERROR; 1396 } 1397 for(i=2; i<4; i++){ 1398 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; 1399 } 1400 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; 1401 z = sqlite3_mprintf(argv[1], a[0], a[1], r); 1402 Tcl_AppendResult(interp, z, 0); 1403 sqlite3_free(z); 1404 return TCL_OK; 1405 } 1406 1407 /* 1408 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE 1409 ** 1410 ** Call mprintf with a single double argument which is the product of the 1411 ** two arguments given above. This is used to generate overflow and underflow 1412 ** doubles to test that they are converted properly. 1413 */ 1414 static int sqlite3_mprintf_scaled( 1415 void *NotUsed, 1416 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1417 int argc, /* Number of arguments */ 1418 char **argv /* Text of each argument */ 1419 ){ 1420 int i; 1421 double r[2]; 1422 char *z; 1423 if( argc!=4 ){ 1424 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1425 " FORMAT DOUBLE DOUBLE\"", 0); 1426 return TCL_ERROR; 1427 } 1428 for(i=2; i<4; i++){ 1429 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; 1430 } 1431 z = sqlite3_mprintf(argv[1], r[0]*r[1]); 1432 Tcl_AppendResult(interp, z, 0); 1433 sqlite3_free(z); 1434 return TCL_OK; 1435 } 1436 1437 /* 1438 ** Usage: sqlite3_mprintf_stronly FORMAT STRING 1439 ** 1440 ** Call mprintf with a single double argument which is the product of the 1441 ** two arguments given above. This is used to generate overflow and underflow 1442 ** doubles to test that they are converted properly. 1443 */ 1444 static int sqlite3_mprintf_stronly( 1445 void *NotUsed, 1446 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1447 int argc, /* Number of arguments */ 1448 char **argv /* Text of each argument */ 1449 ){ 1450 char *z; 1451 if( argc!=3 ){ 1452 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1453 " FORMAT STRING\"", 0); 1454 return TCL_ERROR; 1455 } 1456 z = sqlite3_mprintf(argv[1], argv[2]); 1457 Tcl_AppendResult(interp, z, 0); 1458 sqlite3_free(z); 1459 return TCL_OK; 1460 } 1461 1462 /* 1463 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX 1464 ** 1465 ** Call mprintf with a single double argument which is derived from the 1466 ** hexadecimal encoding of an IEEE double. 1467 */ 1468 static int sqlite3_mprintf_hexdouble( 1469 void *NotUsed, 1470 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1471 int argc, /* Number of arguments */ 1472 char **argv /* Text of each argument */ 1473 ){ 1474 char *z; 1475 double r; 1476 unsigned int x1, x2; 1477 sqlite_uint64 d; 1478 if( argc!=3 ){ 1479 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1480 " FORMAT STRING\"", 0); 1481 return TCL_ERROR; 1482 } 1483 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ 1484 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0); 1485 return TCL_ERROR; 1486 } 1487 d = x2; 1488 d = (d<<32) + x1; 1489 memcpy(&r, &d, sizeof(r)); 1490 z = sqlite3_mprintf(argv[1], r); 1491 Tcl_AppendResult(interp, z, 0); 1492 sqlite3_free(z); 1493 return TCL_OK; 1494 } 1495 1496 /* 1497 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN? 1498 ** 1499 */ 1500 #if !defined(SQLITE_OMIT_SHARED_CACHE) 1501 static int test_enable_shared( 1502 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1503 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1504 int objc, /* Number of arguments */ 1505 Tcl_Obj *CONST objv[] /* Command arguments */ 1506 ){ 1507 int rc; 1508 int enable; 1509 int ret = 0; 1510 1511 if( objc!=2 && objc!=1 ){ 1512 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?"); 1513 return TCL_ERROR; 1514 } 1515 ret = sqlite3GlobalConfig.sharedCacheEnabled; 1516 1517 if( objc==2 ){ 1518 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){ 1519 return TCL_ERROR; 1520 } 1521 rc = sqlite3_enable_shared_cache(enable); 1522 if( rc!=SQLITE_OK ){ 1523 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); 1524 return TCL_ERROR; 1525 } 1526 } 1527 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret)); 1528 return TCL_OK; 1529 } 1530 #endif 1531 1532 1533 1534 /* 1535 ** Usage: sqlite3_extended_result_codes DB BOOLEAN 1536 ** 1537 */ 1538 static int test_extended_result_codes( 1539 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1540 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1541 int objc, /* Number of arguments */ 1542 Tcl_Obj *CONST objv[] /* Command arguments */ 1543 ){ 1544 int enable; 1545 sqlite3 *db; 1546 1547 if( objc!=3 ){ 1548 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); 1549 return TCL_ERROR; 1550 } 1551 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1552 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR; 1553 sqlite3_extended_result_codes(db, enable); 1554 return TCL_OK; 1555 } 1556 1557 /* 1558 ** Usage: sqlite3_libversion_number 1559 ** 1560 */ 1561 static int test_libversion_number( 1562 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1563 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1564 int objc, /* Number of arguments */ 1565 Tcl_Obj *CONST objv[] /* Command arguments */ 1566 ){ 1567 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number())); 1568 return TCL_OK; 1569 } 1570 1571 /* 1572 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname 1573 ** 1574 */ 1575 static int test_table_column_metadata( 1576 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1577 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1578 int objc, /* Number of arguments */ 1579 Tcl_Obj *CONST objv[] /* Command arguments */ 1580 ){ 1581 sqlite3 *db; 1582 const char *zDb; 1583 const char *zTbl; 1584 const char *zCol; 1585 int rc; 1586 Tcl_Obj *pRet; 1587 1588 const char *zDatatype; 1589 const char *zCollseq; 1590 int notnull; 1591 int primarykey; 1592 int autoincrement; 1593 1594 if( objc!=5 && objc!=4 ){ 1595 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname"); 1596 return TCL_ERROR; 1597 } 1598 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1599 zDb = Tcl_GetString(objv[2]); 1600 zTbl = Tcl_GetString(objv[3]); 1601 zCol = objc==5 ? Tcl_GetString(objv[4]) : 0; 1602 1603 if( strlen(zDb)==0 ) zDb = 0; 1604 1605 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, 1606 &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement); 1607 1608 if( rc!=SQLITE_OK ){ 1609 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0); 1610 return TCL_ERROR; 1611 } 1612 1613 pRet = Tcl_NewObj(); 1614 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1)); 1615 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1)); 1616 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull)); 1617 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey)); 1618 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement)); 1619 Tcl_SetObjResult(interp, pRet); 1620 1621 return TCL_OK; 1622 } 1623 1624 #ifndef SQLITE_OMIT_INCRBLOB 1625 1626 static int blobHandleFromObj( 1627 Tcl_Interp *interp, 1628 Tcl_Obj *pObj, 1629 sqlite3_blob **ppBlob 1630 ){ 1631 char *z; 1632 int n; 1633 1634 z = Tcl_GetStringFromObj(pObj, &n); 1635 if( n==0 ){ 1636 *ppBlob = 0; 1637 }else{ 1638 int notUsed; 1639 Tcl_Channel channel; 1640 ClientData instanceData; 1641 1642 channel = Tcl_GetChannel(interp, z, ¬Used); 1643 if( !channel ) return TCL_ERROR; 1644 1645 Tcl_Flush(channel); 1646 Tcl_Seek(channel, 0, SEEK_SET); 1647 1648 instanceData = Tcl_GetChannelInstanceData(channel); 1649 *ppBlob = *((sqlite3_blob **)instanceData); 1650 } 1651 1652 return TCL_OK; 1653 } 1654 1655 static int test_blob_reopen( 1656 ClientData clientData, /* Not used */ 1657 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1658 int objc, /* Number of arguments */ 1659 Tcl_Obj *CONST objv[] /* Command arguments */ 1660 ){ 1661 Tcl_WideInt iRowid; 1662 sqlite3_blob *pBlob; 1663 int rc; 1664 1665 if( objc!=3 ){ 1666 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID"); 1667 return TCL_ERROR; 1668 } 1669 1670 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; 1671 if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR; 1672 1673 rc = sqlite3_blob_reopen(pBlob, iRowid); 1674 if( rc!=SQLITE_OK ){ 1675 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); 1676 } 1677 1678 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); 1679 } 1680 1681 #endif 1682 1683 /* 1684 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC 1685 ** 1686 ** This Tcl proc is used for testing the experimental 1687 ** sqlite3_create_collation_v2() interface. 1688 */ 1689 struct TestCollationX { 1690 Tcl_Interp *interp; 1691 Tcl_Obj *pCmp; 1692 Tcl_Obj *pDel; 1693 }; 1694 typedef struct TestCollationX TestCollationX; 1695 static void testCreateCollationDel(void *pCtx){ 1696 TestCollationX *p = (TestCollationX *)pCtx; 1697 1698 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL); 1699 if( rc!=TCL_OK ){ 1700 Tcl_BackgroundError(p->interp); 1701 } 1702 1703 Tcl_DecrRefCount(p->pCmp); 1704 Tcl_DecrRefCount(p->pDel); 1705 sqlite3_free((void *)p); 1706 } 1707 static int testCreateCollationCmp( 1708 void *pCtx, 1709 int nLeft, 1710 const void *zLeft, 1711 int nRight, 1712 const void *zRight 1713 ){ 1714 TestCollationX *p = (TestCollationX *)pCtx; 1715 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp); 1716 int iRes = 0; 1717 1718 Tcl_IncrRefCount(pScript); 1719 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft)); 1720 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight)); 1721 1722 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL) 1723 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes) 1724 ){ 1725 Tcl_BackgroundError(p->interp); 1726 } 1727 Tcl_DecrRefCount(pScript); 1728 1729 return iRes; 1730 } 1731 static int test_create_collation_v2( 1732 ClientData clientData, /* Not used */ 1733 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1734 int objc, /* Number of arguments */ 1735 Tcl_Obj *CONST objv[] /* Command arguments */ 1736 ){ 1737 TestCollationX *p; 1738 sqlite3 *db; 1739 int rc; 1740 1741 if( objc!=5 ){ 1742 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC"); 1743 return TCL_ERROR; 1744 } 1745 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1746 1747 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX)); 1748 p->pCmp = objv[3]; 1749 p->pDel = objv[4]; 1750 p->interp = interp; 1751 Tcl_IncrRefCount(p->pCmp); 1752 Tcl_IncrRefCount(p->pDel); 1753 1754 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16, 1755 (void *)p, testCreateCollationCmp, testCreateCollationDel 1756 ); 1757 if( rc!=SQLITE_MISUSE ){ 1758 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect " 1759 "an invalid encoding", (char*)0); 1760 return TCL_ERROR; 1761 } 1762 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, 1763 (void *)p, testCreateCollationCmp, testCreateCollationDel 1764 ); 1765 return TCL_OK; 1766 } 1767 1768 /* 1769 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES? 1770 ** 1771 ** Available switches are: 1772 ** 1773 ** -func SCRIPT 1774 ** -step SCRIPT 1775 ** -final SCRIPT 1776 ** -destroy SCRIPT 1777 */ 1778 typedef struct CreateFunctionV2 CreateFunctionV2; 1779 struct CreateFunctionV2 { 1780 Tcl_Interp *interp; 1781 Tcl_Obj *pFunc; /* Script for function invocation */ 1782 Tcl_Obj *pStep; /* Script for agg. step invocation */ 1783 Tcl_Obj *pFinal; /* Script for agg. finalization invocation */ 1784 Tcl_Obj *pDestroy; /* Destructor script */ 1785 }; 1786 static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ 1787 } 1788 static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ 1789 } 1790 static void cf2Final(sqlite3_context *ctx){ 1791 } 1792 static void cf2Destroy(void *pUser){ 1793 CreateFunctionV2 *p = (CreateFunctionV2 *)pUser; 1794 1795 if( p->interp && p->pDestroy ){ 1796 int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0); 1797 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); 1798 } 1799 1800 if( p->pFunc ) Tcl_DecrRefCount(p->pFunc); 1801 if( p->pStep ) Tcl_DecrRefCount(p->pStep); 1802 if( p->pFinal ) Tcl_DecrRefCount(p->pFinal); 1803 if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy); 1804 sqlite3_free(p); 1805 } 1806 static int test_create_function_v2( 1807 ClientData clientData, /* Not used */ 1808 Tcl_Interp *interp, /* The invoking TCL interpreter */ 1809 int objc, /* Number of arguments */ 1810 Tcl_Obj *CONST objv[] /* Command arguments */ 1811 ){ 1812 sqlite3 *db; 1813 const char *zFunc; 1814 int nArg; 1815 int enc; 1816 CreateFunctionV2 *p; 1817 int i; 1818 int rc; 1819 1820 struct EncTable { 1821 const char *zEnc; 1822 int enc; 1823 } aEnc[] = { 1824 {"utf8", SQLITE_UTF8 }, 1825 {"utf16", SQLITE_UTF16 }, 1826 {"utf16le", SQLITE_UTF16LE }, 1827 {"utf16be", SQLITE_UTF16BE }, 1828 {"any", SQLITE_ANY }, 1829 {"0", 0 } 1830 }; 1831 1832 if( objc<5 || (objc%2)==0 ){ 1833 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES..."); 1834 return TCL_ERROR; 1835 } 1836 1837 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1838 zFunc = Tcl_GetString(objv[2]); 1839 if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR; 1840 if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]), 1841 "encoding", 0, &enc) 1842 ){ 1843 return TCL_ERROR; 1844 } 1845 enc = aEnc[enc].enc; 1846 1847 p = sqlite3_malloc(sizeof(CreateFunctionV2)); 1848 assert( p ); 1849 memset(p, 0, sizeof(CreateFunctionV2)); 1850 p->interp = interp; 1851 1852 for(i=5; i<objc; i+=2){ 1853 int iSwitch; 1854 const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0}; 1855 if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){ 1856 sqlite3_free(p); 1857 return TCL_ERROR; 1858 } 1859 1860 switch( iSwitch ){ 1861 case 0: p->pFunc = objv[i+1]; break; 1862 case 1: p->pStep = objv[i+1]; break; 1863 case 2: p->pFinal = objv[i+1]; break; 1864 case 3: p->pDestroy = objv[i+1]; break; 1865 } 1866 } 1867 if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc); 1868 if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep); 1869 if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal); 1870 if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy); 1871 1872 if( p->pFunc ) Tcl_IncrRefCount(p->pFunc); 1873 if( p->pStep ) Tcl_IncrRefCount(p->pStep); 1874 if( p->pFinal ) Tcl_IncrRefCount(p->pFinal); 1875 if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy); 1876 1877 rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p, 1878 (p->pFunc ? cf2Func : 0), 1879 (p->pStep ? cf2Step : 0), 1880 (p->pFinal ? cf2Final : 0), 1881 cf2Destroy 1882 ); 1883 if( rc!=SQLITE_OK ){ 1884 Tcl_ResetResult(interp); 1885 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 1886 return TCL_ERROR; 1887 } 1888 return TCL_OK; 1889 } 1890 1891 /* 1892 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? 1893 */ 1894 static int test_load_extension( 1895 ClientData clientData, /* Not used */ 1896 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1897 int objc, /* Number of arguments */ 1898 Tcl_Obj *CONST objv[] /* Command arguments */ 1899 ){ 1900 Tcl_CmdInfo cmdInfo; 1901 sqlite3 *db; 1902 int rc; 1903 char *zDb; 1904 char *zFile; 1905 char *zProc = 0; 1906 char *zErr = 0; 1907 1908 if( objc!=4 && objc!=3 ){ 1909 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?"); 1910 return TCL_ERROR; 1911 } 1912 zDb = Tcl_GetString(objv[1]); 1913 zFile = Tcl_GetString(objv[2]); 1914 if( objc==4 ){ 1915 zProc = Tcl_GetString(objv[3]); 1916 } 1917 1918 /* Extract the C database handle from the Tcl command name */ 1919 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 1920 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); 1921 return TCL_ERROR; 1922 } 1923 db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 1924 assert(db); 1925 1926 /* Call the underlying C function. If an error occurs, set rc to 1927 ** TCL_ERROR and load any error string into the interpreter. If no 1928 ** error occurs, set rc to TCL_OK. 1929 */ 1930 #ifdef SQLITE_OMIT_LOAD_EXTENSION 1931 rc = SQLITE_ERROR; 1932 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); 1933 #else 1934 rc = sqlite3_load_extension(db, zFile, zProc, &zErr); 1935 #endif 1936 if( rc!=SQLITE_OK ){ 1937 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); 1938 rc = TCL_ERROR; 1939 }else{ 1940 rc = TCL_OK; 1941 } 1942 sqlite3_free(zErr); 1943 1944 return rc; 1945 } 1946 1947 /* 1948 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF 1949 */ 1950 static int test_enable_load( 1951 ClientData clientData, /* Not used */ 1952 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1953 int objc, /* Number of arguments */ 1954 Tcl_Obj *CONST objv[] /* Command arguments */ 1955 ){ 1956 Tcl_CmdInfo cmdInfo; 1957 sqlite3 *db; 1958 char *zDb; 1959 int onoff; 1960 1961 if( objc!=3 ){ 1962 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); 1963 return TCL_ERROR; 1964 } 1965 zDb = Tcl_GetString(objv[1]); 1966 1967 /* Extract the C database handle from the Tcl command name */ 1968 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 1969 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); 1970 return TCL_ERROR; 1971 } 1972 db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 1973 assert(db); 1974 1975 /* Get the onoff parameter */ 1976 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 1977 return TCL_ERROR; 1978 } 1979 1980 #ifdef SQLITE_OMIT_LOAD_EXTENSION 1981 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); 1982 return TCL_ERROR; 1983 #else 1984 sqlite3_enable_load_extension(db, onoff); 1985 return TCL_OK; 1986 #endif 1987 } 1988 1989 /* 1990 ** Usage: sqlite_abort 1991 ** 1992 ** Shutdown the process immediately. This is not a clean shutdown. 1993 ** This command is used to test the recoverability of a database in 1994 ** the event of a program crash. 1995 */ 1996 static int sqlite_abort( 1997 void *NotUsed, 1998 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1999 int argc, /* Number of arguments */ 2000 char **argv /* Text of each argument */ 2001 ){ 2002 #if defined(_MSC_VER) 2003 /* We do this, otherwise the test will halt with a popup message 2004 * that we have to click away before the test will continue. 2005 */ 2006 _set_abort_behavior( 0, _CALL_REPORTFAULT ); 2007 #endif 2008 exit(255); 2009 assert( interp==0 ); /* This will always fail */ 2010 return TCL_OK; 2011 } 2012 2013 /* 2014 ** The following routine is a user-defined SQL function whose purpose 2015 ** is to test the sqlite_set_result() API. 2016 */ 2017 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2018 while( argc>=2 ){ 2019 const char *zArg0 = (char*)sqlite3_value_text(argv[0]); 2020 if( zArg0 ){ 2021 if( 0==sqlite3StrICmp(zArg0, "int") ){ 2022 sqlite3_result_int(context, sqlite3_value_int(argv[1])); 2023 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ 2024 sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); 2025 }else if( sqlite3StrICmp(zArg0,"string")==0 ){ 2026 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, 2027 SQLITE_TRANSIENT); 2028 }else if( sqlite3StrICmp(zArg0,"double")==0 ){ 2029 sqlite3_result_double(context, sqlite3_value_double(argv[1])); 2030 }else if( sqlite3StrICmp(zArg0,"null")==0 ){ 2031 sqlite3_result_null(context); 2032 }else if( sqlite3StrICmp(zArg0,"value")==0 ){ 2033 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); 2034 }else{ 2035 goto error_out; 2036 } 2037 }else{ 2038 goto error_out; 2039 } 2040 argc -= 2; 2041 argv += 2; 2042 } 2043 return; 2044 2045 error_out: 2046 sqlite3_result_error(context,"first argument should be one of: " 2047 "int int64 string double null value", -1); 2048 } 2049 2050 /* 2051 ** Usage: sqlite_register_test_function DB NAME 2052 ** 2053 ** Register the test SQL function on the database DB under the name NAME. 2054 */ 2055 static int test_register_func( 2056 void *NotUsed, 2057 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2058 int argc, /* Number of arguments */ 2059 char **argv /* Text of each argument */ 2060 ){ 2061 sqlite3 *db; 2062 int rc; 2063 if( argc!=3 ){ 2064 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 2065 " DB FUNCTION-NAME", 0); 2066 return TCL_ERROR; 2067 } 2068 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 2069 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 2070 testFunc, 0, 0); 2071 if( rc!=0 ){ 2072 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); 2073 return TCL_ERROR; 2074 } 2075 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2076 return TCL_OK; 2077 } 2078 2079 /* 2080 ** Usage: sqlite3_finalize STMT 2081 ** 2082 ** Finalize a statement handle. 2083 */ 2084 static int test_finalize( 2085 void * clientData, 2086 Tcl_Interp *interp, 2087 int objc, 2088 Tcl_Obj *CONST objv[] 2089 ){ 2090 sqlite3_stmt *pStmt; 2091 int rc; 2092 sqlite3 *db = 0; 2093 2094 if( objc!=2 ){ 2095 Tcl_AppendResult(interp, "wrong # args: should be \"", 2096 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2097 return TCL_ERROR; 2098 } 2099 2100 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2101 2102 if( pStmt ){ 2103 db = StmtToDb(pStmt); 2104 } 2105 rc = sqlite3_finalize(pStmt); 2106 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 2107 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2108 return TCL_OK; 2109 } 2110 2111 /* 2112 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG 2113 ** 2114 ** Get the value of a status counter from a statement. 2115 */ 2116 static int test_stmt_status( 2117 void * clientData, 2118 Tcl_Interp *interp, 2119 int objc, 2120 Tcl_Obj *CONST objv[] 2121 ){ 2122 int iValue; 2123 int i, op = 0, resetFlag; 2124 const char *zOpName; 2125 sqlite3_stmt *pStmt; 2126 2127 static const struct { 2128 const char *zName; 2129 int op; 2130 } aOp[] = { 2131 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP }, 2132 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT }, 2133 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX }, 2134 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP }, 2135 }; 2136 if( objc!=4 ){ 2137 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); 2138 return TCL_ERROR; 2139 } 2140 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2141 zOpName = Tcl_GetString(objv[2]); 2142 for(i=0; i<ArraySize(aOp); i++){ 2143 if( strcmp(aOp[i].zName, zOpName)==0 ){ 2144 op = aOp[i].op; 2145 break; 2146 } 2147 } 2148 if( i>=ArraySize(aOp) ){ 2149 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; 2150 } 2151 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; 2152 iValue = sqlite3_stmt_status(pStmt, op, resetFlag); 2153 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue)); 2154 return TCL_OK; 2155 } 2156 2157 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 2158 /* 2159 ** Usage: sqlite3_stmt_scanstatus STMT IDX 2160 */ 2161 static int test_stmt_scanstatus( 2162 void * clientData, 2163 Tcl_Interp *interp, 2164 int objc, 2165 Tcl_Obj *CONST objv[] 2166 ){ 2167 sqlite3_stmt *pStmt; /* First argument */ 2168 int idx; /* Second argument */ 2169 2170 const char *zName; 2171 const char *zExplain; 2172 sqlite3_int64 nLoop; 2173 sqlite3_int64 nVisit; 2174 double rEst; 2175 int res; 2176 2177 if( objc!=3 ){ 2178 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX"); 2179 return TCL_ERROR; 2180 } 2181 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2182 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2183 2184 res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop); 2185 if( res==0 ){ 2186 Tcl_Obj *pRet = Tcl_NewObj(); 2187 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1)); 2188 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop)); 2189 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); 2190 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1)); 2191 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit)); 2192 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst); 2193 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1)); 2194 Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst)); 2195 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName); 2196 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1)); 2197 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1)); 2198 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); 2199 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1)); 2200 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1)); 2201 Tcl_SetObjResult(interp, pRet); 2202 }else{ 2203 Tcl_ResetResult(interp); 2204 } 2205 return TCL_OK; 2206 } 2207 2208 /* 2209 ** Usage: sqlite3_stmt_scanstatus_reset STMT 2210 */ 2211 static int test_stmt_scanstatus_reset( 2212 void * clientData, 2213 Tcl_Interp *interp, 2214 int objc, 2215 Tcl_Obj *CONST objv[] 2216 ){ 2217 sqlite3_stmt *pStmt; /* First argument */ 2218 if( objc!=2 ){ 2219 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 2220 return TCL_ERROR; 2221 } 2222 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2223 sqlite3_stmt_scanstatus_reset(pStmt); 2224 return TCL_OK; 2225 } 2226 #endif 2227 2228 /* 2229 ** Usage: sqlite3_next_stmt DB STMT 2230 ** 2231 ** Return the next statment in sequence after STMT. 2232 */ 2233 static int test_next_stmt( 2234 void * clientData, 2235 Tcl_Interp *interp, 2236 int objc, 2237 Tcl_Obj *CONST objv[] 2238 ){ 2239 sqlite3_stmt *pStmt; 2240 sqlite3 *db = 0; 2241 char zBuf[50]; 2242 2243 if( objc!=3 ){ 2244 Tcl_AppendResult(interp, "wrong # args: should be \"", 2245 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); 2246 return TCL_ERROR; 2247 } 2248 2249 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2250 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; 2251 pStmt = sqlite3_next_stmt(db, pStmt); 2252 if( pStmt ){ 2253 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 2254 Tcl_AppendResult(interp, zBuf, 0); 2255 } 2256 return TCL_OK; 2257 } 2258 2259 /* 2260 ** Usage: sqlite3_stmt_readonly STMT 2261 ** 2262 ** Return true if STMT is a NULL pointer or a pointer to a statement 2263 ** that is guaranteed to leave the database unmodified. 2264 */ 2265 static int test_stmt_readonly( 2266 void * clientData, 2267 Tcl_Interp *interp, 2268 int objc, 2269 Tcl_Obj *CONST objv[] 2270 ){ 2271 sqlite3_stmt *pStmt; 2272 int rc; 2273 2274 if( objc!=2 ){ 2275 Tcl_AppendResult(interp, "wrong # args: should be \"", 2276 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2277 return TCL_ERROR; 2278 } 2279 2280 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2281 rc = sqlite3_stmt_readonly(pStmt); 2282 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); 2283 return TCL_OK; 2284 } 2285 2286 /* 2287 ** Usage: sqlite3_stmt_busy STMT 2288 ** 2289 ** Return true if STMT is a non-NULL pointer to a statement 2290 ** that has been stepped but not to completion. 2291 */ 2292 static int test_stmt_busy( 2293 void * clientData, 2294 Tcl_Interp *interp, 2295 int objc, 2296 Tcl_Obj *CONST objv[] 2297 ){ 2298 sqlite3_stmt *pStmt; 2299 int rc; 2300 2301 if( objc!=2 ){ 2302 Tcl_AppendResult(interp, "wrong # args: should be \"", 2303 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2304 return TCL_ERROR; 2305 } 2306 2307 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2308 rc = sqlite3_stmt_busy(pStmt); 2309 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); 2310 return TCL_OK; 2311 } 2312 2313 /* 2314 ** Usage: uses_stmt_journal STMT 2315 ** 2316 ** Return true if STMT uses a statement journal. 2317 */ 2318 static int uses_stmt_journal( 2319 void * clientData, 2320 Tcl_Interp *interp, 2321 int objc, 2322 Tcl_Obj *CONST objv[] 2323 ){ 2324 sqlite3_stmt *pStmt; 2325 2326 if( objc!=2 ){ 2327 Tcl_AppendResult(interp, "wrong # args: should be \"", 2328 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2329 return TCL_ERROR; 2330 } 2331 2332 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2333 sqlite3_stmt_readonly(pStmt); 2334 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal)); 2335 return TCL_OK; 2336 } 2337 2338 2339 /* 2340 ** Usage: sqlite3_reset STMT 2341 ** 2342 ** Reset a statement handle. 2343 */ 2344 static int test_reset( 2345 void * clientData, 2346 Tcl_Interp *interp, 2347 int objc, 2348 Tcl_Obj *CONST objv[] 2349 ){ 2350 sqlite3_stmt *pStmt; 2351 int rc; 2352 2353 if( objc!=2 ){ 2354 Tcl_AppendResult(interp, "wrong # args: should be \"", 2355 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2356 return TCL_ERROR; 2357 } 2358 2359 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2360 2361 rc = sqlite3_reset(pStmt); 2362 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ 2363 return TCL_ERROR; 2364 } 2365 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 2366 /* 2367 if( rc ){ 2368 return TCL_ERROR; 2369 } 2370 */ 2371 return TCL_OK; 2372 } 2373 2374 /* 2375 ** Usage: sqlite3_expired STMT 2376 ** 2377 ** Return TRUE if a recompilation of the statement is recommended. 2378 */ 2379 static int test_expired( 2380 void * clientData, 2381 Tcl_Interp *interp, 2382 int objc, 2383 Tcl_Obj *CONST objv[] 2384 ){ 2385 #ifndef SQLITE_OMIT_DEPRECATED 2386 sqlite3_stmt *pStmt; 2387 if( objc!=2 ){ 2388 Tcl_AppendResult(interp, "wrong # args: should be \"", 2389 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2390 return TCL_ERROR; 2391 } 2392 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2393 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); 2394 #endif 2395 return TCL_OK; 2396 } 2397 2398 /* 2399 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT 2400 ** 2401 ** Transfer all bindings from FROMSTMT over to TOSTMT 2402 */ 2403 static int test_transfer_bind( 2404 void * clientData, 2405 Tcl_Interp *interp, 2406 int objc, 2407 Tcl_Obj *CONST objv[] 2408 ){ 2409 #ifndef SQLITE_OMIT_DEPRECATED 2410 sqlite3_stmt *pStmt1, *pStmt2; 2411 if( objc!=3 ){ 2412 Tcl_AppendResult(interp, "wrong # args: should be \"", 2413 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); 2414 return TCL_ERROR; 2415 } 2416 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; 2417 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; 2418 Tcl_SetObjResult(interp, 2419 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); 2420 #endif 2421 return TCL_OK; 2422 } 2423 2424 /* 2425 ** Usage: sqlite3_changes DB 2426 ** 2427 ** Return the number of changes made to the database by the last SQL 2428 ** execution. 2429 */ 2430 static int test_changes( 2431 void * clientData, 2432 Tcl_Interp *interp, 2433 int objc, 2434 Tcl_Obj *CONST objv[] 2435 ){ 2436 sqlite3 *db; 2437 if( objc!=2 ){ 2438 Tcl_AppendResult(interp, "wrong # args: should be \"", 2439 Tcl_GetString(objv[0]), " DB", 0); 2440 return TCL_ERROR; 2441 } 2442 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2443 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); 2444 return TCL_OK; 2445 } 2446 2447 /* 2448 ** This is the "static_bind_value" that variables are bound to when 2449 ** the FLAG option of sqlite3_bind is "static" 2450 */ 2451 static char *sqlite_static_bind_value = 0; 2452 static int sqlite_static_bind_nbyte = 0; 2453 2454 /* 2455 ** Usage: sqlite3_bind VM IDX VALUE FLAGS 2456 ** 2457 ** Sets the value of the IDX-th occurrence of "?" in the original SQL 2458 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is 2459 ** ignored and the value is set to NULL. If FLAGS=="static" then 2460 ** the value is set to the value of a static variable named 2461 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy 2462 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored 2463 ** an a 10-byte blob "abc\000xyz\000pq" is inserted. 2464 */ 2465 static int test_bind( 2466 void *NotUsed, 2467 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2468 int argc, /* Number of arguments */ 2469 char **argv /* Text of each argument */ 2470 ){ 2471 sqlite3_stmt *pStmt; 2472 int rc; 2473 int idx; 2474 if( argc!=5 ){ 2475 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 2476 " VM IDX VALUE (null|static|normal)\"", 0); 2477 return TCL_ERROR; 2478 } 2479 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; 2480 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; 2481 if( strcmp(argv[4],"null")==0 ){ 2482 rc = sqlite3_bind_null(pStmt, idx); 2483 }else if( strcmp(argv[4],"static")==0 ){ 2484 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); 2485 }else if( strcmp(argv[4],"static-nbytes")==0 ){ 2486 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, 2487 sqlite_static_bind_nbyte, 0); 2488 }else if( strcmp(argv[4],"normal")==0 ){ 2489 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); 2490 }else if( strcmp(argv[4],"blob10")==0 ){ 2491 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); 2492 }else{ 2493 Tcl_AppendResult(interp, "4th argument should be " 2494 "\"null\" or \"static\" or \"normal\"", 0); 2495 return TCL_ERROR; 2496 } 2497 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2498 if( rc ){ 2499 char zBuf[50]; 2500 sprintf(zBuf, "(%d) ", rc); 2501 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); 2502 return TCL_ERROR; 2503 } 2504 return TCL_OK; 2505 } 2506 2507 #ifndef SQLITE_OMIT_UTF16 2508 /* 2509 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> 2510 ** 2511 ** This function is used to test that SQLite selects the correct collation 2512 ** sequence callback when multiple versions (for different text encodings) 2513 ** are available. 2514 ** 2515 ** Calling this routine registers the collation sequence "test_collate" 2516 ** with database handle <db>. The second argument must be a list of three 2517 ** boolean values. If the first is true, then a version of test_collate is 2518 ** registered for UTF-8, if the second is true, a version is registered for 2519 ** UTF-16le, if the third is true, a UTF-16be version is available. 2520 ** Previous versions of test_collate are deleted. 2521 ** 2522 ** The collation sequence test_collate is implemented by calling the 2523 ** following TCL script: 2524 ** 2525 ** "test_collate <enc> <lhs> <rhs>" 2526 ** 2527 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. 2528 ** The <enc> parameter is the encoding of the collation function that 2529 ** SQLite selected to call. The TCL test script implements the 2530 ** "test_collate" proc. 2531 ** 2532 ** Note that this will only work with one interpreter at a time, as the 2533 ** interp pointer to use when evaluating the TCL script is stored in 2534 ** pTestCollateInterp. 2535 */ 2536 static Tcl_Interp* pTestCollateInterp; 2537 static int test_collate_func( 2538 void *pCtx, 2539 int nA, const void *zA, 2540 int nB, const void *zB 2541 ){ 2542 Tcl_Interp *i = pTestCollateInterp; 2543 int encin = SQLITE_PTR_TO_INT(pCtx); 2544 int res; 2545 int n; 2546 2547 sqlite3_value *pVal; 2548 Tcl_Obj *pX; 2549 2550 pX = Tcl_NewStringObj("test_collate", -1); 2551 Tcl_IncrRefCount(pX); 2552 2553 switch( encin ){ 2554 case SQLITE_UTF8: 2555 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); 2556 break; 2557 case SQLITE_UTF16LE: 2558 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); 2559 break; 2560 case SQLITE_UTF16BE: 2561 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); 2562 break; 2563 default: 2564 assert(0); 2565 } 2566 2567 sqlite3BeginBenignMalloc(); 2568 pVal = sqlite3ValueNew(0); 2569 if( pVal ){ 2570 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); 2571 n = sqlite3_value_bytes(pVal); 2572 Tcl_ListObjAppendElement(i,pX, 2573 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2574 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); 2575 n = sqlite3_value_bytes(pVal); 2576 Tcl_ListObjAppendElement(i,pX, 2577 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2578 sqlite3ValueFree(pVal); 2579 } 2580 sqlite3EndBenignMalloc(); 2581 2582 Tcl_EvalObjEx(i, pX, 0); 2583 Tcl_DecrRefCount(pX); 2584 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); 2585 return res; 2586 } 2587 static int test_collate( 2588 void * clientData, 2589 Tcl_Interp *interp, 2590 int objc, 2591 Tcl_Obj *CONST objv[] 2592 ){ 2593 sqlite3 *db; 2594 int val; 2595 sqlite3_value *pVal; 2596 int rc; 2597 2598 if( objc!=5 ) goto bad_args; 2599 pTestCollateInterp = interp; 2600 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2601 2602 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2603 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, 2604 (void *)SQLITE_UTF8, val?test_collate_func:0); 2605 if( rc==SQLITE_OK ){ 2606 const void *zUtf16; 2607 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2608 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, 2609 (void *)SQLITE_UTF16LE, val?test_collate_func:0); 2610 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2611 2612 #if 0 2613 if( sqlite3_iMallocFail>0 ){ 2614 sqlite3_iMallocFail++; 2615 } 2616 #endif 2617 sqlite3_mutex_enter(db->mutex); 2618 pVal = sqlite3ValueNew(db); 2619 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); 2620 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 2621 if( db->mallocFailed ){ 2622 rc = SQLITE_NOMEM; 2623 }else{ 2624 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, 2625 (void *)SQLITE_UTF16BE, val?test_collate_func:0); 2626 } 2627 sqlite3ValueFree(pVal); 2628 sqlite3_mutex_leave(db->mutex); 2629 } 2630 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2631 2632 if( rc!=SQLITE_OK ){ 2633 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 2634 return TCL_ERROR; 2635 } 2636 return TCL_OK; 2637 2638 bad_args: 2639 Tcl_AppendResult(interp, "wrong # args: should be \"", 2640 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2641 return TCL_ERROR; 2642 } 2643 2644 /* 2645 ** Usage: add_test_utf16bin_collate <db ptr> 2646 ** 2647 ** Add a utf-16 collation sequence named "utf16bin" to the database 2648 ** handle. This collation sequence compares arguments in the same way as the 2649 ** built-in collation "binary". 2650 */ 2651 static int test_utf16bin_collate_func( 2652 void *pCtx, 2653 int nA, const void *zA, 2654 int nB, const void *zB 2655 ){ 2656 int nCmp = (nA>nB ? nB : nA); 2657 int res = memcmp(zA, zB, nCmp); 2658 if( res==0 ) res = nA - nB; 2659 return res; 2660 } 2661 static int test_utf16bin_collate( 2662 void * clientData, 2663 Tcl_Interp *interp, 2664 int objc, 2665 Tcl_Obj *CONST objv[] 2666 ){ 2667 sqlite3 *db; 2668 int rc; 2669 2670 if( objc!=2 ) goto bad_args; 2671 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2672 2673 rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0, 2674 test_utf16bin_collate_func 2675 ); 2676 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2677 return TCL_OK; 2678 2679 bad_args: 2680 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 2681 return TCL_ERROR; 2682 } 2683 2684 /* 2685 ** When the collation needed callback is invoked, record the name of 2686 ** the requested collating function here. The recorded name is linked 2687 ** to a TCL variable and used to make sure that the requested collation 2688 ** name is correct. 2689 */ 2690 static char zNeededCollation[200]; 2691 static char *pzNeededCollation = zNeededCollation; 2692 2693 2694 /* 2695 ** Called when a collating sequence is needed. Registered using 2696 ** sqlite3_collation_needed16(). 2697 */ 2698 static void test_collate_needed_cb( 2699 void *pCtx, 2700 sqlite3 *db, 2701 int eTextRep, 2702 const void *pName 2703 ){ 2704 int enc = ENC(db); 2705 int i; 2706 char *z; 2707 for(z = (char*)pName, i=0; *z || z[1]; z++){ 2708 if( *z ) zNeededCollation[i++] = *z; 2709 } 2710 zNeededCollation[i] = 0; 2711 sqlite3_create_collation( 2712 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func); 2713 } 2714 2715 /* 2716 ** Usage: add_test_collate_needed DB 2717 */ 2718 static int test_collate_needed( 2719 void * clientData, 2720 Tcl_Interp *interp, 2721 int objc, 2722 Tcl_Obj *CONST objv[] 2723 ){ 2724 sqlite3 *db; 2725 int rc; 2726 2727 if( objc!=2 ) goto bad_args; 2728 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2729 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); 2730 zNeededCollation[0] = 0; 2731 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2732 return TCL_OK; 2733 2734 bad_args: 2735 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 2736 return TCL_ERROR; 2737 } 2738 2739 /* 2740 ** tclcmd: add_alignment_test_collations DB 2741 ** 2742 ** Add two new collating sequences to the database DB 2743 ** 2744 ** utf16_aligned 2745 ** utf16_unaligned 2746 ** 2747 ** Both collating sequences use the same sort order as BINARY. 2748 ** The only difference is that the utf16_aligned collating 2749 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. 2750 ** Both collating functions increment the unaligned utf16 counter 2751 ** whenever they see a string that begins on an odd byte boundary. 2752 */ 2753 static int unaligned_string_counter = 0; 2754 static int alignmentCollFunc( 2755 void *NotUsed, 2756 int nKey1, const void *pKey1, 2757 int nKey2, const void *pKey2 2758 ){ 2759 int rc, n; 2760 n = nKey1<nKey2 ? nKey1 : nKey2; 2761 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++; 2762 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++; 2763 rc = memcmp(pKey1, pKey2, n); 2764 if( rc==0 ){ 2765 rc = nKey1 - nKey2; 2766 } 2767 return rc; 2768 } 2769 static int add_alignment_test_collations( 2770 void * clientData, 2771 Tcl_Interp *interp, 2772 int objc, 2773 Tcl_Obj *CONST objv[] 2774 ){ 2775 sqlite3 *db; 2776 if( objc>=2 ){ 2777 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2778 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, 2779 0, alignmentCollFunc); 2780 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED, 2781 0, alignmentCollFunc); 2782 } 2783 return SQLITE_OK; 2784 } 2785 #endif /* !defined(SQLITE_OMIT_UTF16) */ 2786 2787 /* 2788 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> 2789 ** 2790 ** This function is used to test that SQLite selects the correct user 2791 ** function callback when multiple versions (for different text encodings) 2792 ** are available. 2793 ** 2794 ** Calling this routine registers up to three versions of the user function 2795 ** "test_function" with database handle <db>. If the second argument is 2796 ** true, then a version of test_function is registered for UTF-8, if the 2797 ** third is true, a version is registered for UTF-16le, if the fourth is 2798 ** true, a UTF-16be version is available. Previous versions of 2799 ** test_function are deleted. 2800 ** 2801 ** The user function is implemented by calling the following TCL script: 2802 ** 2803 ** "test_function <enc> <arg>" 2804 ** 2805 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the 2806 ** single argument passed to the SQL function. The value returned by 2807 ** the TCL script is used as the return value of the SQL function. It 2808 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 2809 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that 2810 ** prefers UTF-16BE. 2811 */ 2812 #ifndef SQLITE_OMIT_UTF16 2813 static void test_function_utf8( 2814 sqlite3_context *pCtx, 2815 int nArg, 2816 sqlite3_value **argv 2817 ){ 2818 Tcl_Interp *interp; 2819 Tcl_Obj *pX; 2820 sqlite3_value *pVal; 2821 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2822 pX = Tcl_NewStringObj("test_function", -1); 2823 Tcl_IncrRefCount(pX); 2824 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); 2825 Tcl_ListObjAppendElement(interp, pX, 2826 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2827 Tcl_EvalObjEx(interp, pX, 0); 2828 Tcl_DecrRefCount(pX); 2829 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); 2830 pVal = sqlite3ValueNew(0); 2831 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2832 SQLITE_UTF8, SQLITE_STATIC); 2833 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), 2834 -1, SQLITE_TRANSIENT); 2835 sqlite3ValueFree(pVal); 2836 } 2837 static void test_function_utf16le( 2838 sqlite3_context *pCtx, 2839 int nArg, 2840 sqlite3_value **argv 2841 ){ 2842 Tcl_Interp *interp; 2843 Tcl_Obj *pX; 2844 sqlite3_value *pVal; 2845 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2846 pX = Tcl_NewStringObj("test_function", -1); 2847 Tcl_IncrRefCount(pX); 2848 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); 2849 Tcl_ListObjAppendElement(interp, pX, 2850 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2851 Tcl_EvalObjEx(interp, pX, 0); 2852 Tcl_DecrRefCount(pX); 2853 pVal = sqlite3ValueNew(0); 2854 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2855 SQLITE_UTF8, SQLITE_STATIC); 2856 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); 2857 sqlite3ValueFree(pVal); 2858 } 2859 static void test_function_utf16be( 2860 sqlite3_context *pCtx, 2861 int nArg, 2862 sqlite3_value **argv 2863 ){ 2864 Tcl_Interp *interp; 2865 Tcl_Obj *pX; 2866 sqlite3_value *pVal; 2867 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2868 pX = Tcl_NewStringObj("test_function", -1); 2869 Tcl_IncrRefCount(pX); 2870 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); 2871 Tcl_ListObjAppendElement(interp, pX, 2872 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2873 Tcl_EvalObjEx(interp, pX, 0); 2874 Tcl_DecrRefCount(pX); 2875 pVal = sqlite3ValueNew(0); 2876 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2877 SQLITE_UTF8, SQLITE_STATIC); 2878 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), 2879 -1, SQLITE_TRANSIENT); 2880 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), 2881 -1, SQLITE_TRANSIENT); 2882 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), 2883 -1, SQLITE_TRANSIENT); 2884 sqlite3ValueFree(pVal); 2885 } 2886 #endif /* SQLITE_OMIT_UTF16 */ 2887 static int test_function( 2888 void * clientData, 2889 Tcl_Interp *interp, 2890 int objc, 2891 Tcl_Obj *CONST objv[] 2892 ){ 2893 #ifndef SQLITE_OMIT_UTF16 2894 sqlite3 *db; 2895 int val; 2896 2897 if( objc!=5 ) goto bad_args; 2898 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2899 2900 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2901 if( val ){ 2902 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, 2903 interp, test_function_utf8, 0, 0); 2904 } 2905 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2906 if( val ){ 2907 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, 2908 interp, test_function_utf16le, 0, 0); 2909 } 2910 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2911 if( val ){ 2912 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, 2913 interp, test_function_utf16be, 0, 0); 2914 } 2915 2916 return TCL_OK; 2917 bad_args: 2918 Tcl_AppendResult(interp, "wrong # args: should be \"", 2919 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2920 #endif /* SQLITE_OMIT_UTF16 */ 2921 return TCL_ERROR; 2922 } 2923 2924 /* 2925 ** Usage: sqlite3_test_errstr <err code> 2926 ** 2927 ** Test that the english language string equivalents for sqlite error codes 2928 ** are sane. The parameter is an integer representing an sqlite error code. 2929 ** The result is a list of two elements, the string representation of the 2930 ** error code and the english language explanation. 2931 */ 2932 static int test_errstr( 2933 void * clientData, 2934 Tcl_Interp *interp, 2935 int objc, 2936 Tcl_Obj *CONST objv[] 2937 ){ 2938 char *zCode; 2939 int i; 2940 if( objc!=1 ){ 2941 Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); 2942 } 2943 2944 zCode = Tcl_GetString(objv[1]); 2945 for(i=0; i<200; i++){ 2946 if( 0==strcmp(t1ErrorName(i), zCode) ) break; 2947 } 2948 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); 2949 return TCL_OK; 2950 } 2951 2952 /* 2953 ** Usage: breakpoint 2954 ** 2955 ** This routine exists for one purpose - to provide a place to put a 2956 ** breakpoint with GDB that can be triggered using TCL code. The use 2957 ** for this is when a particular test fails on (say) the 1485th iteration. 2958 ** In the TCL test script, we can add code like this: 2959 ** 2960 ** if {$i==1485} breakpoint 2961 ** 2962 ** Then run testfixture in the debugger and wait for the breakpoint to 2963 ** fire. Then additional breakpoints can be set to trace down the bug. 2964 */ 2965 static int test_breakpoint( 2966 void *NotUsed, 2967 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2968 int argc, /* Number of arguments */ 2969 char **argv /* Text of each argument */ 2970 ){ 2971 return TCL_OK; /* Do nothing */ 2972 } 2973 2974 /* 2975 ** Usage: sqlite3_bind_zeroblob STMT IDX N 2976 ** 2977 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. 2978 ** IDX is the index of a wildcard in the prepared statement. This command 2979 ** binds a N-byte zero-filled BLOB to the wildcard. 2980 */ 2981 static int test_bind_zeroblob( 2982 void * clientData, 2983 Tcl_Interp *interp, 2984 int objc, 2985 Tcl_Obj *CONST objv[] 2986 ){ 2987 sqlite3_stmt *pStmt; 2988 int idx; 2989 int n; 2990 int rc; 2991 2992 if( objc!=4 ){ 2993 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); 2994 return TCL_ERROR; 2995 } 2996 2997 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2998 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2999 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; 3000 3001 rc = sqlite3_bind_zeroblob(pStmt, idx, n); 3002 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3003 if( rc!=SQLITE_OK ){ 3004 return TCL_ERROR; 3005 } 3006 3007 return TCL_OK; 3008 } 3009 3010 /* 3011 ** Usage: sqlite3_bind_int STMT N VALUE 3012 ** 3013 ** Test the sqlite3_bind_int interface. STMT is a prepared statement. 3014 ** N is the index of a wildcard in the prepared statement. This command 3015 ** binds a 32-bit integer VALUE to that wildcard. 3016 */ 3017 static int test_bind_int( 3018 void * clientData, 3019 Tcl_Interp *interp, 3020 int objc, 3021 Tcl_Obj *CONST objv[] 3022 ){ 3023 sqlite3_stmt *pStmt; 3024 int idx; 3025 int value; 3026 int rc; 3027 3028 if( objc!=4 ){ 3029 Tcl_AppendResult(interp, "wrong # args: should be \"", 3030 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3031 return TCL_ERROR; 3032 } 3033 3034 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3035 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3036 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3037 3038 rc = sqlite3_bind_int(pStmt, idx, value); 3039 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3040 if( rc!=SQLITE_OK ){ 3041 return TCL_ERROR; 3042 } 3043 3044 return TCL_OK; 3045 } 3046 3047 3048 /* 3049 ** Usage: sqlite3_bind_int64 STMT N VALUE 3050 ** 3051 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. 3052 ** N is the index of a wildcard in the prepared statement. This command 3053 ** binds a 64-bit integer VALUE to that wildcard. 3054 */ 3055 static int test_bind_int64( 3056 void * clientData, 3057 Tcl_Interp *interp, 3058 int objc, 3059 Tcl_Obj *CONST objv[] 3060 ){ 3061 sqlite3_stmt *pStmt; 3062 int idx; 3063 Tcl_WideInt value; 3064 int rc; 3065 3066 if( objc!=4 ){ 3067 Tcl_AppendResult(interp, "wrong # args: should be \"", 3068 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3069 return TCL_ERROR; 3070 } 3071 3072 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3073 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3074 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3075 3076 rc = sqlite3_bind_int64(pStmt, idx, value); 3077 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3078 if( rc!=SQLITE_OK ){ 3079 return TCL_ERROR; 3080 } 3081 3082 return TCL_OK; 3083 } 3084 3085 3086 /* 3087 ** Usage: sqlite3_bind_double STMT N VALUE 3088 ** 3089 ** Test the sqlite3_bind_double interface. STMT is a prepared statement. 3090 ** N is the index of a wildcard in the prepared statement. This command 3091 ** binds a 64-bit integer VALUE to that wildcard. 3092 */ 3093 static int test_bind_double( 3094 void * clientData, 3095 Tcl_Interp *interp, 3096 int objc, 3097 Tcl_Obj *CONST objv[] 3098 ){ 3099 sqlite3_stmt *pStmt; 3100 int idx; 3101 double value = 0; 3102 int rc; 3103 const char *zVal; 3104 int i; 3105 static const struct { 3106 const char *zName; /* Name of the special floating point value */ 3107 unsigned int iUpper; /* Upper 32 bits */ 3108 unsigned int iLower; /* Lower 32 bits */ 3109 } aSpecialFp[] = { 3110 { "NaN", 0x7fffffff, 0xffffffff }, 3111 { "SNaN", 0x7ff7ffff, 0xffffffff }, 3112 { "-NaN", 0xffffffff, 0xffffffff }, 3113 { "-SNaN", 0xfff7ffff, 0xffffffff }, 3114 { "+Inf", 0x7ff00000, 0x00000000 }, 3115 { "-Inf", 0xfff00000, 0x00000000 }, 3116 { "Epsilon", 0x00000000, 0x00000001 }, 3117 { "-Epsilon", 0x80000000, 0x00000001 }, 3118 { "NaN0", 0x7ff80000, 0x00000000 }, 3119 { "-NaN0", 0xfff80000, 0x00000000 }, 3120 }; 3121 3122 if( objc!=4 ){ 3123 Tcl_AppendResult(interp, "wrong # args: should be \"", 3124 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3125 return TCL_ERROR; 3126 } 3127 3128 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3129 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3130 3131 /* Intercept the string "NaN" and generate a NaN value for it. 3132 ** All other strings are passed through to Tcl_GetDoubleFromObj(). 3133 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions 3134 ** contain a bug. 3135 */ 3136 zVal = Tcl_GetString(objv[3]); 3137 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ 3138 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ 3139 sqlite3_uint64 x; 3140 x = aSpecialFp[i].iUpper; 3141 x <<= 32; 3142 x |= aSpecialFp[i].iLower; 3143 assert( sizeof(value)==8 ); 3144 assert( sizeof(x)==8 ); 3145 memcpy(&value, &x, 8); 3146 break; 3147 } 3148 } 3149 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && 3150 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ 3151 return TCL_ERROR; 3152 } 3153 rc = sqlite3_bind_double(pStmt, idx, value); 3154 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3155 if( rc!=SQLITE_OK ){ 3156 return TCL_ERROR; 3157 } 3158 3159 return TCL_OK; 3160 } 3161 3162 /* 3163 ** Usage: sqlite3_bind_null STMT N 3164 ** 3165 ** Test the sqlite3_bind_null interface. STMT is a prepared statement. 3166 ** N is the index of a wildcard in the prepared statement. This command 3167 ** binds a NULL to the wildcard. 3168 */ 3169 static int test_bind_null( 3170 void * clientData, 3171 Tcl_Interp *interp, 3172 int objc, 3173 Tcl_Obj *CONST objv[] 3174 ){ 3175 sqlite3_stmt *pStmt; 3176 int idx; 3177 int rc; 3178 3179 if( objc!=3 ){ 3180 Tcl_AppendResult(interp, "wrong # args: should be \"", 3181 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); 3182 return TCL_ERROR; 3183 } 3184 3185 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3186 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3187 3188 rc = sqlite3_bind_null(pStmt, idx); 3189 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3190 if( rc!=SQLITE_OK ){ 3191 return TCL_ERROR; 3192 } 3193 3194 return TCL_OK; 3195 } 3196 3197 /* 3198 ** Usage: sqlite3_bind_text STMT N STRING BYTES 3199 ** 3200 ** Test the sqlite3_bind_text interface. STMT is a prepared statement. 3201 ** N is the index of a wildcard in the prepared statement. This command 3202 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes 3203 ** long. 3204 */ 3205 static int test_bind_text( 3206 void * clientData, 3207 Tcl_Interp *interp, 3208 int objc, 3209 Tcl_Obj *CONST objv[] 3210 ){ 3211 sqlite3_stmt *pStmt; 3212 int idx; 3213 int bytes; 3214 char *value; 3215 int rc; 3216 3217 if( objc!=5 ){ 3218 Tcl_AppendResult(interp, "wrong # args: should be \"", 3219 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3220 return TCL_ERROR; 3221 } 3222 3223 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3224 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3225 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); 3226 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3227 3228 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); 3229 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3230 if( rc!=SQLITE_OK ){ 3231 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3232 return TCL_ERROR; 3233 } 3234 3235 return TCL_OK; 3236 } 3237 3238 /* 3239 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES 3240 ** 3241 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. 3242 ** N is the index of a wildcard in the prepared statement. This command 3243 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes 3244 ** long. 3245 */ 3246 static int test_bind_text16( 3247 void * clientData, 3248 Tcl_Interp *interp, 3249 int objc, 3250 Tcl_Obj *CONST objv[] 3251 ){ 3252 #ifndef SQLITE_OMIT_UTF16 3253 sqlite3_stmt *pStmt; 3254 int idx; 3255 int bytes; 3256 char *value; 3257 int rc; 3258 3259 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); 3260 Tcl_Obj *oStmt = objv[objc-4]; 3261 Tcl_Obj *oN = objv[objc-3]; 3262 Tcl_Obj *oString = objv[objc-2]; 3263 Tcl_Obj *oBytes = objv[objc-1]; 3264 3265 if( objc!=5 && objc!=6){ 3266 Tcl_AppendResult(interp, "wrong # args: should be \"", 3267 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3268 return TCL_ERROR; 3269 } 3270 3271 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; 3272 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; 3273 value = (char*)Tcl_GetByteArrayFromObj(oString, 0); 3274 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; 3275 3276 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); 3277 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3278 if( rc!=SQLITE_OK ){ 3279 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3280 return TCL_ERROR; 3281 } 3282 3283 #endif /* SQLITE_OMIT_UTF16 */ 3284 return TCL_OK; 3285 } 3286 3287 /* 3288 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES 3289 ** 3290 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. 3291 ** N is the index of a wildcard in the prepared statement. This command 3292 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. 3293 */ 3294 static int test_bind_blob( 3295 void * clientData, 3296 Tcl_Interp *interp, 3297 int objc, 3298 Tcl_Obj *CONST objv[] 3299 ){ 3300 sqlite3_stmt *pStmt; 3301 int idx; 3302 int bytes; 3303 char *value; 3304 int rc; 3305 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; 3306 3307 if( objc!=5 && objc!=6 ){ 3308 Tcl_AppendResult(interp, "wrong # args: should be \"", 3309 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); 3310 return TCL_ERROR; 3311 } 3312 3313 if( objc==6 ){ 3314 xDestructor = SQLITE_STATIC; 3315 objv++; 3316 } 3317 3318 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3319 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3320 value = Tcl_GetString(objv[3]); 3321 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3322 3323 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); 3324 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3325 if( rc!=SQLITE_OK ){ 3326 return TCL_ERROR; 3327 } 3328 3329 return TCL_OK; 3330 } 3331 3332 /* 3333 ** Usage: sqlite3_bind_parameter_count STMT 3334 ** 3335 ** Return the number of wildcards in the given statement. 3336 */ 3337 static int test_bind_parameter_count( 3338 void * clientData, 3339 Tcl_Interp *interp, 3340 int objc, 3341 Tcl_Obj *CONST objv[] 3342 ){ 3343 sqlite3_stmt *pStmt; 3344 3345 if( objc!=2 ){ 3346 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3347 return TCL_ERROR; 3348 } 3349 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3350 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); 3351 return TCL_OK; 3352 } 3353 3354 /* 3355 ** Usage: sqlite3_bind_parameter_name STMT N 3356 ** 3357 ** Return the name of the Nth wildcard. The first wildcard is 1. 3358 ** An empty string is returned if N is out of range or if the wildcard 3359 ** is nameless. 3360 */ 3361 static int test_bind_parameter_name( 3362 void * clientData, 3363 Tcl_Interp *interp, 3364 int objc, 3365 Tcl_Obj *CONST objv[] 3366 ){ 3367 sqlite3_stmt *pStmt; 3368 int i; 3369 3370 if( objc!=3 ){ 3371 Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); 3372 return TCL_ERROR; 3373 } 3374 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3375 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; 3376 Tcl_SetObjResult(interp, 3377 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) 3378 ); 3379 return TCL_OK; 3380 } 3381 3382 /* 3383 ** Usage: sqlite3_bind_parameter_index STMT NAME 3384 ** 3385 ** Return the index of the wildcard called NAME. Return 0 if there is 3386 ** no such wildcard. 3387 */ 3388 static int test_bind_parameter_index( 3389 void * clientData, 3390 Tcl_Interp *interp, 3391 int objc, 3392 Tcl_Obj *CONST objv[] 3393 ){ 3394 sqlite3_stmt *pStmt; 3395 3396 if( objc!=3 ){ 3397 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); 3398 return TCL_ERROR; 3399 } 3400 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3401 Tcl_SetObjResult(interp, 3402 Tcl_NewIntObj( 3403 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) 3404 ) 3405 ); 3406 return TCL_OK; 3407 } 3408 3409 /* 3410 ** Usage: sqlite3_clear_bindings STMT 3411 ** 3412 */ 3413 static int test_clear_bindings( 3414 void * clientData, 3415 Tcl_Interp *interp, 3416 int objc, 3417 Tcl_Obj *CONST objv[] 3418 ){ 3419 sqlite3_stmt *pStmt; 3420 3421 if( objc!=2 ){ 3422 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3423 return TCL_ERROR; 3424 } 3425 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3426 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); 3427 return TCL_OK; 3428 } 3429 3430 /* 3431 ** Usage: sqlite3_sleep MILLISECONDS 3432 */ 3433 static int test_sleep( 3434 void * clientData, 3435 Tcl_Interp *interp, 3436 int objc, 3437 Tcl_Obj *CONST objv[] 3438 ){ 3439 int ms; 3440 3441 if( objc!=2 ){ 3442 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); 3443 return TCL_ERROR; 3444 } 3445 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ 3446 return TCL_ERROR; 3447 } 3448 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); 3449 return TCL_OK; 3450 } 3451 3452 /* 3453 ** Usage: sqlite3_extended_errcode DB 3454 ** 3455 ** Return the string representation of the most recent sqlite3_* API 3456 ** error code. e.g. "SQLITE_ERROR". 3457 */ 3458 static int test_ex_errcode( 3459 void * clientData, 3460 Tcl_Interp *interp, 3461 int objc, 3462 Tcl_Obj *CONST objv[] 3463 ){ 3464 sqlite3 *db; 3465 int rc; 3466 3467 if( objc!=2 ){ 3468 Tcl_AppendResult(interp, "wrong # args: should be \"", 3469 Tcl_GetString(objv[0]), " DB", 0); 3470 return TCL_ERROR; 3471 } 3472 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3473 rc = sqlite3_extended_errcode(db); 3474 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3475 return TCL_OK; 3476 } 3477 3478 3479 /* 3480 ** Usage: sqlite3_errcode DB 3481 ** 3482 ** Return the string representation of the most recent sqlite3_* API 3483 ** error code. e.g. "SQLITE_ERROR". 3484 */ 3485 static int test_errcode( 3486 void * clientData, 3487 Tcl_Interp *interp, 3488 int objc, 3489 Tcl_Obj *CONST objv[] 3490 ){ 3491 sqlite3 *db; 3492 int rc; 3493 3494 if( objc!=2 ){ 3495 Tcl_AppendResult(interp, "wrong # args: should be \"", 3496 Tcl_GetString(objv[0]), " DB", 0); 3497 return TCL_ERROR; 3498 } 3499 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3500 rc = sqlite3_errcode(db); 3501 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3502 return TCL_OK; 3503 } 3504 3505 /* 3506 ** Usage: sqlite3_errmsg DB 3507 ** 3508 ** Returns the UTF-8 representation of the error message string for the 3509 ** most recent sqlite3_* API call. 3510 */ 3511 static int test_errmsg( 3512 void * clientData, 3513 Tcl_Interp *interp, 3514 int objc, 3515 Tcl_Obj *CONST objv[] 3516 ){ 3517 sqlite3 *db; 3518 const char *zErr; 3519 3520 if( objc!=2 ){ 3521 Tcl_AppendResult(interp, "wrong # args: should be \"", 3522 Tcl_GetString(objv[0]), " DB", 0); 3523 return TCL_ERROR; 3524 } 3525 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3526 3527 zErr = sqlite3_errmsg(db); 3528 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 3529 return TCL_OK; 3530 } 3531 3532 /* 3533 ** Usage: test_errmsg16 DB 3534 ** 3535 ** Returns the UTF-16 representation of the error message string for the 3536 ** most recent sqlite3_* API call. This is a byte array object at the TCL 3537 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the 3538 ** UTF-16 string. 3539 */ 3540 static int test_errmsg16( 3541 void * clientData, 3542 Tcl_Interp *interp, 3543 int objc, 3544 Tcl_Obj *CONST objv[] 3545 ){ 3546 #ifndef SQLITE_OMIT_UTF16 3547 sqlite3 *db; 3548 const void *zErr; 3549 const char *z; 3550 int bytes = 0; 3551 3552 if( objc!=2 ){ 3553 Tcl_AppendResult(interp, "wrong # args: should be \"", 3554 Tcl_GetString(objv[0]), " DB", 0); 3555 return TCL_ERROR; 3556 } 3557 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3558 3559 zErr = sqlite3_errmsg16(db); 3560 if( zErr ){ 3561 z = zErr; 3562 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} 3563 } 3564 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); 3565 #endif /* SQLITE_OMIT_UTF16 */ 3566 return TCL_OK; 3567 } 3568 3569 /* 3570 ** Usage: sqlite3_prepare DB sql bytes ?tailvar? 3571 ** 3572 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3573 ** database handle <DB>. The parameter <tailval> is the name of a global 3574 ** variable that is set to the unused portion of <sql> (if any). A 3575 ** STMT handle is returned. 3576 */ 3577 static int test_prepare( 3578 void * clientData, 3579 Tcl_Interp *interp, 3580 int objc, 3581 Tcl_Obj *CONST objv[] 3582 ){ 3583 sqlite3 *db; 3584 const char *zSql; 3585 int bytes; 3586 const char *zTail = 0; 3587 sqlite3_stmt *pStmt = 0; 3588 char zBuf[50]; 3589 int rc; 3590 3591 if( objc!=5 && objc!=4 ){ 3592 Tcl_AppendResult(interp, "wrong # args: should be \"", 3593 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3594 return TCL_ERROR; 3595 } 3596 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3597 zSql = Tcl_GetString(objv[2]); 3598 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3599 3600 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3601 Tcl_ResetResult(interp); 3602 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3603 if( zTail && objc>=5 ){ 3604 if( bytes>=0 ){ 3605 bytes = bytes - (int)(zTail-zSql); 3606 } 3607 if( (int)strlen(zTail)<bytes ){ 3608 bytes = (int)strlen(zTail); 3609 } 3610 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3611 } 3612 if( rc!=SQLITE_OK ){ 3613 assert( pStmt==0 ); 3614 sprintf(zBuf, "(%d) ", rc); 3615 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3616 return TCL_ERROR; 3617 } 3618 3619 if( pStmt ){ 3620 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3621 Tcl_AppendResult(interp, zBuf, 0); 3622 } 3623 return TCL_OK; 3624 } 3625 3626 /* 3627 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? 3628 ** 3629 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3630 ** database handle <DB>. The parameter <tailval> is the name of a global 3631 ** variable that is set to the unused portion of <sql> (if any). A 3632 ** STMT handle is returned. 3633 */ 3634 static int test_prepare_v2( 3635 void * clientData, 3636 Tcl_Interp *interp, 3637 int objc, 3638 Tcl_Obj *CONST objv[] 3639 ){ 3640 sqlite3 *db; 3641 const char *zSql; 3642 char *zCopy = 0; /* malloc() copy of zSql */ 3643 int bytes; 3644 const char *zTail = 0; 3645 sqlite3_stmt *pStmt = 0; 3646 char zBuf[50]; 3647 int rc; 3648 3649 if( objc!=5 && objc!=4 ){ 3650 Tcl_AppendResult(interp, "wrong # args: should be \"", 3651 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3652 return TCL_ERROR; 3653 } 3654 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3655 zSql = Tcl_GetString(objv[2]); 3656 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3657 3658 /* Instead of using zSql directly, make a copy into a buffer obtained 3659 ** directly from malloc(). The idea is to make it easier for valgrind 3660 ** to spot buffer overreads. */ 3661 if( bytes>=0 ){ 3662 zCopy = malloc(bytes); 3663 memcpy(zCopy, zSql, bytes); 3664 }else{ 3665 int n = (int)strlen(zSql) + 1; 3666 zCopy = malloc(n); 3667 memcpy(zCopy, zSql, n); 3668 } 3669 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0); 3670 free(zCopy); 3671 zTail = &zSql[(zTail - zCopy)]; 3672 3673 assert(rc==SQLITE_OK || pStmt==0); 3674 Tcl_ResetResult(interp); 3675 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3676 if( rc==SQLITE_OK && zTail && objc>=5 ){ 3677 if( bytes>=0 ){ 3678 bytes = bytes - (int)(zTail-zSql); 3679 } 3680 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3681 } 3682 if( rc!=SQLITE_OK ){ 3683 assert( pStmt==0 ); 3684 sprintf(zBuf, "(%d) ", rc); 3685 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3686 return TCL_ERROR; 3687 } 3688 3689 if( pStmt ){ 3690 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3691 Tcl_AppendResult(interp, zBuf, 0); 3692 } 3693 return TCL_OK; 3694 } 3695 3696 /* 3697 ** Usage: sqlite3_prepare_tkt3134 DB 3698 ** 3699 ** Generate a prepared statement for a zero-byte string as a test 3700 ** for ticket #3134. The string should be preceded by a zero byte. 3701 */ 3702 static int test_prepare_tkt3134( 3703 void * clientData, 3704 Tcl_Interp *interp, 3705 int objc, 3706 Tcl_Obj *CONST objv[] 3707 ){ 3708 sqlite3 *db; 3709 static const char zSql[] = "\000SELECT 1"; 3710 sqlite3_stmt *pStmt = 0; 3711 char zBuf[50]; 3712 int rc; 3713 3714 if( objc!=2 ){ 3715 Tcl_AppendResult(interp, "wrong # args: should be \"", 3716 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3717 return TCL_ERROR; 3718 } 3719 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3720 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); 3721 assert(rc==SQLITE_OK || pStmt==0); 3722 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3723 if( rc!=SQLITE_OK ){ 3724 assert( pStmt==0 ); 3725 sprintf(zBuf, "(%d) ", rc); 3726 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3727 return TCL_ERROR; 3728 } 3729 3730 if( pStmt ){ 3731 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3732 Tcl_AppendResult(interp, zBuf, 0); 3733 } 3734 return TCL_OK; 3735 } 3736 3737 /* 3738 ** Usage: sqlite3_prepare16 DB sql bytes tailvar 3739 ** 3740 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3741 ** database handle <DB>. The parameter <tailval> is the name of a global 3742 ** variable that is set to the unused portion of <sql> (if any). A 3743 ** STMT handle is returned. 3744 */ 3745 static int test_prepare16( 3746 void * clientData, 3747 Tcl_Interp *interp, 3748 int objc, 3749 Tcl_Obj *CONST objv[] 3750 ){ 3751 #ifndef SQLITE_OMIT_UTF16 3752 sqlite3 *db; 3753 const void *zSql; 3754 const void *zTail = 0; 3755 Tcl_Obj *pTail = 0; 3756 sqlite3_stmt *pStmt = 0; 3757 char zBuf[50]; 3758 int rc; 3759 int bytes; /* The integer specified as arg 3 */ 3760 int objlen; /* The byte-array length of arg 2 */ 3761 3762 if( objc!=5 && objc!=4 ){ 3763 Tcl_AppendResult(interp, "wrong # args: should be \"", 3764 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3765 return TCL_ERROR; 3766 } 3767 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3768 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3769 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3770 3771 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3772 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3773 if( rc ){ 3774 return TCL_ERROR; 3775 } 3776 3777 if( objc>=5 ){ 3778 if( zTail ){ 3779 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3780 }else{ 3781 objlen = 0; 3782 } 3783 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3784 Tcl_IncrRefCount(pTail); 3785 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3786 Tcl_DecrRefCount(pTail); 3787 } 3788 3789 if( pStmt ){ 3790 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3791 } 3792 Tcl_AppendResult(interp, zBuf, 0); 3793 #endif /* SQLITE_OMIT_UTF16 */ 3794 return TCL_OK; 3795 } 3796 3797 /* 3798 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? 3799 ** 3800 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3801 ** database handle <DB>. The parameter <tailval> is the name of a global 3802 ** variable that is set to the unused portion of <sql> (if any). A 3803 ** STMT handle is returned. 3804 */ 3805 static int test_prepare16_v2( 3806 void * clientData, 3807 Tcl_Interp *interp, 3808 int objc, 3809 Tcl_Obj *CONST objv[] 3810 ){ 3811 #ifndef SQLITE_OMIT_UTF16 3812 sqlite3 *db; 3813 const void *zSql; 3814 const void *zTail = 0; 3815 Tcl_Obj *pTail = 0; 3816 sqlite3_stmt *pStmt = 0; 3817 char zBuf[50]; 3818 int rc; 3819 int bytes; /* The integer specified as arg 3 */ 3820 int objlen; /* The byte-array length of arg 2 */ 3821 3822 if( objc!=5 && objc!=4 ){ 3823 Tcl_AppendResult(interp, "wrong # args: should be \"", 3824 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3825 return TCL_ERROR; 3826 } 3827 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3828 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3829 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3830 3831 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3832 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3833 if( rc ){ 3834 return TCL_ERROR; 3835 } 3836 3837 if( objc>=5 ){ 3838 if( zTail ){ 3839 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3840 }else{ 3841 objlen = 0; 3842 } 3843 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3844 Tcl_IncrRefCount(pTail); 3845 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3846 Tcl_DecrRefCount(pTail); 3847 } 3848 3849 if( pStmt ){ 3850 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3851 } 3852 Tcl_AppendResult(interp, zBuf, 0); 3853 #endif /* SQLITE_OMIT_UTF16 */ 3854 return TCL_OK; 3855 } 3856 3857 /* 3858 ** Usage: sqlite3_open filename ?options-list? 3859 */ 3860 static int test_open( 3861 void * clientData, 3862 Tcl_Interp *interp, 3863 int objc, 3864 Tcl_Obj *CONST objv[] 3865 ){ 3866 const char *zFilename; 3867 sqlite3 *db; 3868 char zBuf[100]; 3869 3870 if( objc!=3 && objc!=2 && objc!=1 ){ 3871 Tcl_AppendResult(interp, "wrong # args: should be \"", 3872 Tcl_GetString(objv[0]), " filename options-list", 0); 3873 return TCL_ERROR; 3874 } 3875 3876 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; 3877 sqlite3_open(zFilename, &db); 3878 3879 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3880 Tcl_AppendResult(interp, zBuf, 0); 3881 return TCL_OK; 3882 } 3883 3884 /* 3885 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS 3886 */ 3887 static int test_open_v2( 3888 void * clientData, 3889 Tcl_Interp *interp, 3890 int objc, 3891 Tcl_Obj *CONST objv[] 3892 ){ 3893 const char *zFilename; 3894 const char *zVfs; 3895 int flags = 0; 3896 sqlite3 *db; 3897 int rc; 3898 char zBuf[100]; 3899 3900 int nFlag; 3901 Tcl_Obj **apFlag; 3902 int i; 3903 3904 if( objc!=4 ){ 3905 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); 3906 return TCL_ERROR; 3907 } 3908 zFilename = Tcl_GetString(objv[1]); 3909 zVfs = Tcl_GetString(objv[3]); 3910 if( zVfs[0]==0x00 ) zVfs = 0; 3911 3912 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); 3913 if( rc!=TCL_OK ) return rc; 3914 for(i=0; i<nFlag; i++){ 3915 int iFlag; 3916 struct OpenFlag { 3917 const char *zFlag; 3918 int flag; 3919 } aFlag[] = { 3920 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, 3921 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, 3922 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, 3923 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, 3924 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, 3925 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, 3926 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, 3927 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, 3928 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, 3929 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, 3930 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, 3931 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, 3932 { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, 3933 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, 3934 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, 3935 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, 3936 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, 3937 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, 3938 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, 3939 { 0, 0 } 3940 }; 3941 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), 3942 "flag", 0, &iFlag 3943 ); 3944 if( rc!=TCL_OK ) return rc; 3945 flags |= aFlag[iFlag].flag; 3946 } 3947 3948 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); 3949 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3950 Tcl_AppendResult(interp, zBuf, 0); 3951 return TCL_OK; 3952 } 3953 3954 /* 3955 ** Usage: sqlite3_open16 filename options 3956 */ 3957 static int test_open16( 3958 void * clientData, 3959 Tcl_Interp *interp, 3960 int objc, 3961 Tcl_Obj *CONST objv[] 3962 ){ 3963 #ifndef SQLITE_OMIT_UTF16 3964 const void *zFilename; 3965 sqlite3 *db; 3966 char zBuf[100]; 3967 3968 if( objc!=3 ){ 3969 Tcl_AppendResult(interp, "wrong # args: should be \"", 3970 Tcl_GetString(objv[0]), " filename options-list", 0); 3971 return TCL_ERROR; 3972 } 3973 3974 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); 3975 sqlite3_open16(zFilename, &db); 3976 3977 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3978 Tcl_AppendResult(interp, zBuf, 0); 3979 #endif /* SQLITE_OMIT_UTF16 */ 3980 return TCL_OK; 3981 } 3982 3983 /* 3984 ** Usage: sqlite3_complete16 <UTF-16 string> 3985 ** 3986 ** Return 1 if the supplied argument is a complete SQL statement, or zero 3987 ** otherwise. 3988 */ 3989 static int test_complete16( 3990 void * clientData, 3991 Tcl_Interp *interp, 3992 int objc, 3993 Tcl_Obj *CONST objv[] 3994 ){ 3995 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) 3996 char *zBuf; 3997 3998 if( objc!=2 ){ 3999 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); 4000 return TCL_ERROR; 4001 } 4002 4003 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); 4004 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); 4005 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ 4006 return TCL_OK; 4007 } 4008 4009 /* 4010 ** Usage: sqlite3_step STMT 4011 ** 4012 ** Advance the statement to the next row. 4013 */ 4014 static int test_step( 4015 void * clientData, 4016 Tcl_Interp *interp, 4017 int objc, 4018 Tcl_Obj *CONST objv[] 4019 ){ 4020 sqlite3_stmt *pStmt; 4021 int rc; 4022 4023 if( objc!=2 ){ 4024 Tcl_AppendResult(interp, "wrong # args: should be \"", 4025 Tcl_GetString(objv[0]), " STMT", 0); 4026 return TCL_ERROR; 4027 } 4028 4029 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4030 rc = sqlite3_step(pStmt); 4031 4032 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ 4033 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 4034 return TCL_OK; 4035 } 4036 4037 static int test_sql( 4038 void * clientData, 4039 Tcl_Interp *interp, 4040 int objc, 4041 Tcl_Obj *CONST objv[] 4042 ){ 4043 sqlite3_stmt *pStmt; 4044 4045 if( objc!=2 ){ 4046 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 4047 return TCL_ERROR; 4048 } 4049 4050 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4051 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); 4052 return TCL_OK; 4053 } 4054 4055 /* 4056 ** Usage: sqlite3_column_count STMT 4057 ** 4058 ** Return the number of columns returned by the sql statement STMT. 4059 */ 4060 static int test_column_count( 4061 void * clientData, 4062 Tcl_Interp *interp, 4063 int objc, 4064 Tcl_Obj *CONST objv[] 4065 ){ 4066 sqlite3_stmt *pStmt; 4067 4068 if( objc!=2 ){ 4069 Tcl_AppendResult(interp, "wrong # args: should be \"", 4070 Tcl_GetString(objv[0]), " STMT column", 0); 4071 return TCL_ERROR; 4072 } 4073 4074 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4075 4076 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); 4077 return TCL_OK; 4078 } 4079 4080 /* 4081 ** Usage: sqlite3_column_type STMT column 4082 ** 4083 ** Return the type of the data in column 'column' of the current row. 4084 */ 4085 static int test_column_type( 4086 void * clientData, 4087 Tcl_Interp *interp, 4088 int objc, 4089 Tcl_Obj *CONST objv[] 4090 ){ 4091 sqlite3_stmt *pStmt; 4092 int col; 4093 int tp; 4094 4095 if( objc!=3 ){ 4096 Tcl_AppendResult(interp, "wrong # args: should be \"", 4097 Tcl_GetString(objv[0]), " STMT column", 0); 4098 return TCL_ERROR; 4099 } 4100 4101 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4102 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4103 4104 tp = sqlite3_column_type(pStmt, col); 4105 switch( tp ){ 4106 case SQLITE_INTEGER: 4107 Tcl_SetResult(interp, "INTEGER", TCL_STATIC); 4108 break; 4109 case SQLITE_NULL: 4110 Tcl_SetResult(interp, "NULL", TCL_STATIC); 4111 break; 4112 case SQLITE_FLOAT: 4113 Tcl_SetResult(interp, "FLOAT", TCL_STATIC); 4114 break; 4115 case SQLITE_TEXT: 4116 Tcl_SetResult(interp, "TEXT", TCL_STATIC); 4117 break; 4118 case SQLITE_BLOB: 4119 Tcl_SetResult(interp, "BLOB", TCL_STATIC); 4120 break; 4121 default: 4122 assert(0); 4123 } 4124 4125 return TCL_OK; 4126 } 4127 4128 /* 4129 ** Usage: sqlite3_column_int64 STMT column 4130 ** 4131 ** Return the data in column 'column' of the current row cast as an 4132 ** wide (64-bit) integer. 4133 */ 4134 static int test_column_int64( 4135 void * clientData, 4136 Tcl_Interp *interp, 4137 int objc, 4138 Tcl_Obj *CONST objv[] 4139 ){ 4140 sqlite3_stmt *pStmt; 4141 int col; 4142 i64 iVal; 4143 4144 if( objc!=3 ){ 4145 Tcl_AppendResult(interp, "wrong # args: should be \"", 4146 Tcl_GetString(objv[0]), " STMT column", 0); 4147 return TCL_ERROR; 4148 } 4149 4150 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4151 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4152 4153 iVal = sqlite3_column_int64(pStmt, col); 4154 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); 4155 return TCL_OK; 4156 } 4157 4158 /* 4159 ** Usage: sqlite3_column_blob STMT column 4160 */ 4161 static int test_column_blob( 4162 void * clientData, 4163 Tcl_Interp *interp, 4164 int objc, 4165 Tcl_Obj *CONST objv[] 4166 ){ 4167 sqlite3_stmt *pStmt; 4168 int col; 4169 4170 int len; 4171 const void *pBlob; 4172 4173 if( objc!=3 ){ 4174 Tcl_AppendResult(interp, "wrong # args: should be \"", 4175 Tcl_GetString(objv[0]), " STMT column", 0); 4176 return TCL_ERROR; 4177 } 4178 4179 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4180 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4181 4182 len = sqlite3_column_bytes(pStmt, col); 4183 pBlob = sqlite3_column_blob(pStmt, col); 4184 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); 4185 return TCL_OK; 4186 } 4187 4188 /* 4189 ** Usage: sqlite3_column_double STMT column 4190 ** 4191 ** Return the data in column 'column' of the current row cast as a double. 4192 */ 4193 static int test_column_double( 4194 void * clientData, 4195 Tcl_Interp *interp, 4196 int objc, 4197 Tcl_Obj *CONST objv[] 4198 ){ 4199 sqlite3_stmt *pStmt; 4200 int col; 4201 double rVal; 4202 4203 if( objc!=3 ){ 4204 Tcl_AppendResult(interp, "wrong # args: should be \"", 4205 Tcl_GetString(objv[0]), " STMT column", 0); 4206 return TCL_ERROR; 4207 } 4208 4209 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4210 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4211 4212 rVal = sqlite3_column_double(pStmt, col); 4213 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); 4214 return TCL_OK; 4215 } 4216 4217 /* 4218 ** Usage: sqlite3_data_count STMT 4219 ** 4220 ** Return the number of columns returned by the sql statement STMT. 4221 */ 4222 static int test_data_count( 4223 void * clientData, 4224 Tcl_Interp *interp, 4225 int objc, 4226 Tcl_Obj *CONST objv[] 4227 ){ 4228 sqlite3_stmt *pStmt; 4229 4230 if( objc!=2 ){ 4231 Tcl_AppendResult(interp, "wrong # args: should be \"", 4232 Tcl_GetString(objv[0]), " STMT column", 0); 4233 return TCL_ERROR; 4234 } 4235 4236 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4237 4238 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); 4239 return TCL_OK; 4240 } 4241 4242 /* 4243 ** Usage: sqlite3_column_text STMT column 4244 ** 4245 ** Usage: sqlite3_column_decltype STMT column 4246 ** 4247 ** Usage: sqlite3_column_name STMT column 4248 */ 4249 static int test_stmt_utf8( 4250 void * clientData, /* Pointer to SQLite API function to be invoke */ 4251 Tcl_Interp *interp, 4252 int objc, 4253 Tcl_Obj *CONST objv[] 4254 ){ 4255 sqlite3_stmt *pStmt; 4256 int col; 4257 const char *(*xFunc)(sqlite3_stmt*, int); 4258 const char *zRet; 4259 4260 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; 4261 if( objc!=3 ){ 4262 Tcl_AppendResult(interp, "wrong # args: should be \"", 4263 Tcl_GetString(objv[0]), " STMT column", 0); 4264 return TCL_ERROR; 4265 } 4266 4267 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4268 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4269 zRet = xFunc(pStmt, col); 4270 if( zRet ){ 4271 Tcl_SetResult(interp, (char *)zRet, 0); 4272 } 4273 return TCL_OK; 4274 } 4275 4276 static int test_global_recover( 4277 void * clientData, 4278 Tcl_Interp *interp, 4279 int objc, 4280 Tcl_Obj *CONST objv[] 4281 ){ 4282 #ifndef SQLITE_OMIT_DEPRECATED 4283 int rc; 4284 if( objc!=1 ){ 4285 Tcl_WrongNumArgs(interp, 1, objv, ""); 4286 return TCL_ERROR; 4287 } 4288 rc = sqlite3_global_recover(); 4289 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4290 #endif 4291 return TCL_OK; 4292 } 4293 4294 /* 4295 ** Usage: sqlite3_column_text STMT column 4296 ** 4297 ** Usage: sqlite3_column_decltype STMT column 4298 ** 4299 ** Usage: sqlite3_column_name STMT column 4300 */ 4301 static int test_stmt_utf16( 4302 void * clientData, /* Pointer to SQLite API function to be invoked */ 4303 Tcl_Interp *interp, 4304 int objc, 4305 Tcl_Obj *CONST objv[] 4306 ){ 4307 #ifndef SQLITE_OMIT_UTF16 4308 sqlite3_stmt *pStmt; 4309 int col; 4310 Tcl_Obj *pRet; 4311 const void *zName16; 4312 const void *(*xFunc)(sqlite3_stmt*, int); 4313 4314 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; 4315 if( objc!=3 ){ 4316 Tcl_AppendResult(interp, "wrong # args: should be \"", 4317 Tcl_GetString(objv[0]), " STMT column", 0); 4318 return TCL_ERROR; 4319 } 4320 4321 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4322 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4323 4324 zName16 = xFunc(pStmt, col); 4325 if( zName16 ){ 4326 int n; 4327 const char *z = zName16; 4328 for(n=0; z[n] || z[n+1]; n+=2){} 4329 pRet = Tcl_NewByteArrayObj(zName16, n+2); 4330 Tcl_SetObjResult(interp, pRet); 4331 } 4332 #endif /* SQLITE_OMIT_UTF16 */ 4333 4334 return TCL_OK; 4335 } 4336 4337 /* 4338 ** Usage: sqlite3_column_int STMT column 4339 ** 4340 ** Usage: sqlite3_column_bytes STMT column 4341 ** 4342 ** Usage: sqlite3_column_bytes16 STMT column 4343 ** 4344 */ 4345 static int test_stmt_int( 4346 void * clientData, /* Pointer to SQLite API function to be invoked */ 4347 Tcl_Interp *interp, 4348 int objc, 4349 Tcl_Obj *CONST objv[] 4350 ){ 4351 sqlite3_stmt *pStmt; 4352 int col; 4353 int (*xFunc)(sqlite3_stmt*, int); 4354 4355 xFunc = (int (*)(sqlite3_stmt*, int))clientData; 4356 if( objc!=3 ){ 4357 Tcl_AppendResult(interp, "wrong # args: should be \"", 4358 Tcl_GetString(objv[0]), " STMT column", 0); 4359 return TCL_ERROR; 4360 } 4361 4362 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4363 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4364 4365 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); 4366 return TCL_OK; 4367 } 4368 4369 /* 4370 ** Usage: sqlite_set_magic DB MAGIC-NUMBER 4371 ** 4372 ** Set the db->magic value. This is used to test error recovery logic. 4373 */ 4374 static int sqlite_set_magic( 4375 void * clientData, 4376 Tcl_Interp *interp, 4377 int argc, 4378 char **argv 4379 ){ 4380 sqlite3 *db; 4381 if( argc!=3 ){ 4382 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4383 " DB MAGIC", 0); 4384 return TCL_ERROR; 4385 } 4386 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4387 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ 4388 db->magic = SQLITE_MAGIC_OPEN; 4389 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ 4390 db->magic = SQLITE_MAGIC_CLOSED; 4391 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ 4392 db->magic = SQLITE_MAGIC_BUSY; 4393 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ 4394 db->magic = SQLITE_MAGIC_ERROR; 4395 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ 4396 return TCL_ERROR; 4397 } 4398 return TCL_OK; 4399 } 4400 4401 /* 4402 ** Usage: sqlite3_interrupt DB 4403 ** 4404 ** Trigger an interrupt on DB 4405 */ 4406 static int test_interrupt( 4407 void * clientData, 4408 Tcl_Interp *interp, 4409 int argc, 4410 char **argv 4411 ){ 4412 sqlite3 *db; 4413 if( argc!=2 ){ 4414 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); 4415 return TCL_ERROR; 4416 } 4417 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4418 sqlite3_interrupt(db); 4419 return TCL_OK; 4420 } 4421 4422 static u8 *sqlite3_stack_baseline = 0; 4423 4424 /* 4425 ** Fill the stack with a known bitpattern. 4426 */ 4427 static void prepStack(void){ 4428 int i; 4429 u32 bigBuf[65536]; 4430 for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; 4431 sqlite3_stack_baseline = (u8*)&bigBuf[65536]; 4432 } 4433 4434 /* 4435 ** Get the current stack depth. Used for debugging only. 4436 */ 4437 u64 sqlite3StackDepth(void){ 4438 u8 x; 4439 return (u64)(sqlite3_stack_baseline - &x); 4440 } 4441 4442 /* 4443 ** Usage: sqlite3_stack_used DB SQL 4444 ** 4445 ** Try to measure the amount of stack space used by a call to sqlite3_exec 4446 */ 4447 static int test_stack_used( 4448 void * clientData, 4449 Tcl_Interp *interp, 4450 int argc, 4451 char **argv 4452 ){ 4453 sqlite3 *db; 4454 int i; 4455 if( argc!=3 ){ 4456 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4457 " DB SQL", 0); 4458 return TCL_ERROR; 4459 } 4460 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4461 prepStack(); 4462 (void)sqlite3_exec(db, argv[2], 0, 0, 0); 4463 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} 4464 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); 4465 return TCL_OK; 4466 } 4467 4468 /* 4469 ** Usage: sqlite_delete_function DB function-name 4470 ** 4471 ** Delete the user function 'function-name' from database handle DB. It 4472 ** is assumed that the user function was created as UTF8, any number of 4473 ** arguments (the way the TCL interface does it). 4474 */ 4475 static int delete_function( 4476 void * clientData, 4477 Tcl_Interp *interp, 4478 int argc, 4479 char **argv 4480 ){ 4481 int rc; 4482 sqlite3 *db; 4483 if( argc!=3 ){ 4484 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4485 " DB function-name", 0); 4486 return TCL_ERROR; 4487 } 4488 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4489 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); 4490 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4491 return TCL_OK; 4492 } 4493 4494 /* 4495 ** Usage: sqlite_delete_collation DB collation-name 4496 ** 4497 ** Delete the collation sequence 'collation-name' from database handle 4498 ** DB. It is assumed that the collation sequence was created as UTF8 (the 4499 ** way the TCL interface does it). 4500 */ 4501 static int delete_collation( 4502 void * clientData, 4503 Tcl_Interp *interp, 4504 int argc, 4505 char **argv 4506 ){ 4507 int rc; 4508 sqlite3 *db; 4509 if( argc!=3 ){ 4510 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4511 " DB function-name", 0); 4512 return TCL_ERROR; 4513 } 4514 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4515 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); 4516 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4517 return TCL_OK; 4518 } 4519 4520 /* 4521 ** Usage: sqlite3_get_autocommit DB 4522 ** 4523 ** Return true if the database DB is currently in auto-commit mode. 4524 ** Return false if not. 4525 */ 4526 static int get_autocommit( 4527 void * clientData, 4528 Tcl_Interp *interp, 4529 int argc, 4530 char **argv 4531 ){ 4532 char zBuf[30]; 4533 sqlite3 *db; 4534 if( argc!=2 ){ 4535 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4536 " DB", 0); 4537 return TCL_ERROR; 4538 } 4539 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4540 sprintf(zBuf, "%d", sqlite3_get_autocommit(db)); 4541 Tcl_AppendResult(interp, zBuf, 0); 4542 return TCL_OK; 4543 } 4544 4545 /* 4546 ** Usage: sqlite3_busy_timeout DB MS 4547 ** 4548 ** Set the busy timeout. This is more easily done using the timeout 4549 ** method of the TCL interface. But we need a way to test the case 4550 ** where it returns SQLITE_MISUSE. 4551 */ 4552 static int test_busy_timeout( 4553 void * clientData, 4554 Tcl_Interp *interp, 4555 int argc, 4556 char **argv 4557 ){ 4558 int rc, ms; 4559 sqlite3 *db; 4560 if( argc!=3 ){ 4561 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4562 " DB", 0); 4563 return TCL_ERROR; 4564 } 4565 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4566 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; 4567 rc = sqlite3_busy_timeout(db, ms); 4568 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 4569 return TCL_OK; 4570 } 4571 4572 /* 4573 ** Usage: tcl_variable_type VARIABLENAME 4574 ** 4575 ** Return the name of the internal representation for the 4576 ** value of the given variable. 4577 */ 4578 static int tcl_variable_type( 4579 void * clientData, 4580 Tcl_Interp *interp, 4581 int objc, 4582 Tcl_Obj *CONST objv[] 4583 ){ 4584 Tcl_Obj *pVar; 4585 if( objc!=2 ){ 4586 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); 4587 return TCL_ERROR; 4588 } 4589 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); 4590 if( pVar==0 ) return TCL_ERROR; 4591 if( pVar->typePtr ){ 4592 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); 4593 } 4594 return TCL_OK; 4595 } 4596 4597 /* 4598 ** Usage: sqlite3_release_memory ?N? 4599 ** 4600 ** Attempt to release memory currently held but not actually required. 4601 ** The integer N is the number of bytes we are trying to release. The 4602 ** return value is the amount of memory actually released. 4603 */ 4604 static int test_release_memory( 4605 void * clientData, 4606 Tcl_Interp *interp, 4607 int objc, 4608 Tcl_Obj *CONST objv[] 4609 ){ 4610 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) 4611 int N; 4612 int amt; 4613 if( objc!=1 && objc!=2 ){ 4614 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4615 return TCL_ERROR; 4616 } 4617 if( objc==2 ){ 4618 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4619 }else{ 4620 N = -1; 4621 } 4622 amt = sqlite3_release_memory(N); 4623 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); 4624 #endif 4625 return TCL_OK; 4626 } 4627 4628 4629 /* 4630 ** Usage: sqlite3_db_release_memory DB 4631 ** 4632 ** Attempt to release memory currently held by database DB. Return the 4633 ** result code (which in the current implementation is always zero). 4634 */ 4635 static int test_db_release_memory( 4636 void * clientData, 4637 Tcl_Interp *interp, 4638 int objc, 4639 Tcl_Obj *CONST objv[] 4640 ){ 4641 sqlite3 *db; 4642 int rc; 4643 if( objc!=2 ){ 4644 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 4645 return TCL_ERROR; 4646 } 4647 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4648 rc = sqlite3_db_release_memory(db); 4649 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 4650 return TCL_OK; 4651 } 4652 4653 /* 4654 ** Usage: sqlite3_db_filename DB DBNAME 4655 ** 4656 ** Return the name of a file associated with a database. 4657 */ 4658 static int test_db_filename( 4659 void * clientData, 4660 Tcl_Interp *interp, 4661 int objc, 4662 Tcl_Obj *CONST objv[] 4663 ){ 4664 sqlite3 *db; 4665 const char *zDbName; 4666 if( objc!=3 ){ 4667 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4668 return TCL_ERROR; 4669 } 4670 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4671 zDbName = Tcl_GetString(objv[2]); 4672 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); 4673 return TCL_OK; 4674 } 4675 4676 /* 4677 ** Usage: sqlite3_db_readonly DB DBNAME 4678 ** 4679 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does 4680 ** not exist. 4681 */ 4682 static int test_db_readonly( 4683 void * clientData, 4684 Tcl_Interp *interp, 4685 int objc, 4686 Tcl_Obj *CONST objv[] 4687 ){ 4688 sqlite3 *db; 4689 const char *zDbName; 4690 if( objc!=3 ){ 4691 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4692 return TCL_ERROR; 4693 } 4694 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4695 zDbName = Tcl_GetString(objv[2]); 4696 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName))); 4697 return TCL_OK; 4698 } 4699 4700 /* 4701 ** Usage: sqlite3_soft_heap_limit ?N? 4702 ** 4703 ** Query or set the soft heap limit for the current thread. The 4704 ** limit is only changed if the N is present. The previous limit 4705 ** is returned. 4706 */ 4707 static int test_soft_heap_limit( 4708 void * clientData, 4709 Tcl_Interp *interp, 4710 int objc, 4711 Tcl_Obj *CONST objv[] 4712 ){ 4713 sqlite3_int64 amt; 4714 Tcl_WideInt N = -1; 4715 if( objc!=1 && objc!=2 ){ 4716 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4717 return TCL_ERROR; 4718 } 4719 if( objc==2 ){ 4720 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4721 } 4722 amt = sqlite3_soft_heap_limit64(N); 4723 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); 4724 return TCL_OK; 4725 } 4726 4727 /* 4728 ** Usage: sqlite3_thread_cleanup 4729 ** 4730 ** Call the sqlite3_thread_cleanup API. 4731 */ 4732 static int test_thread_cleanup( 4733 void * clientData, 4734 Tcl_Interp *interp, 4735 int objc, 4736 Tcl_Obj *CONST objv[] 4737 ){ 4738 #ifndef SQLITE_OMIT_DEPRECATED 4739 sqlite3_thread_cleanup(); 4740 #endif 4741 return TCL_OK; 4742 } 4743 4744 /* 4745 ** Usage: sqlite3_pager_refcounts DB 4746 ** 4747 ** Return a list of numbers which are the PagerRefcount for all 4748 ** pagers on each database connection. 4749 */ 4750 static int test_pager_refcounts( 4751 void * clientData, 4752 Tcl_Interp *interp, 4753 int objc, 4754 Tcl_Obj *CONST objv[] 4755 ){ 4756 sqlite3 *db; 4757 int i; 4758 int v, *a; 4759 Tcl_Obj *pResult; 4760 4761 if( objc!=2 ){ 4762 Tcl_AppendResult(interp, "wrong # args: should be \"", 4763 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 4764 return TCL_ERROR; 4765 } 4766 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4767 pResult = Tcl_NewObj(); 4768 for(i=0; i<db->nDb; i++){ 4769 if( db->aDb[i].pBt==0 ){ 4770 v = -1; 4771 }else{ 4772 sqlite3_mutex_enter(db->mutex); 4773 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); 4774 v = a[0]; 4775 sqlite3_mutex_leave(db->mutex); 4776 } 4777 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); 4778 } 4779 Tcl_SetObjResult(interp, pResult); 4780 return TCL_OK; 4781 } 4782 4783 4784 /* 4785 ** tclcmd: working_64bit_int 4786 ** 4787 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This 4788 ** leads to a number of test failures. The present command checks the 4789 ** TCL build to see whether or not it supports 64-bit integers. It 4790 ** returns TRUE if it does and FALSE if not. 4791 ** 4792 ** This command is used to warn users that their TCL build is defective 4793 ** and that the errors they are seeing in the test scripts might be 4794 ** a result of their defective TCL rather than problems in SQLite. 4795 */ 4796 static int working_64bit_int( 4797 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4798 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4799 int objc, /* Number of arguments */ 4800 Tcl_Obj *CONST objv[] /* Command arguments */ 4801 ){ 4802 Tcl_Obj *pTestObj; 4803 int working = 0; 4804 4805 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); 4806 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; 4807 Tcl_DecrRefCount(pTestObj); 4808 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); 4809 return TCL_OK; 4810 } 4811 4812 4813 /* 4814 ** tclcmd: vfs_unlink_test 4815 ** 4816 ** This TCL command unregisters the primary VFS and then registers 4817 ** it back again. This is used to test the ability to register a 4818 ** VFS when none are previously registered, and the ability to 4819 ** unregister the only available VFS. Ticket #2738 4820 */ 4821 static int vfs_unlink_test( 4822 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4823 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4824 int objc, /* Number of arguments */ 4825 Tcl_Obj *CONST objv[] /* Command arguments */ 4826 ){ 4827 int i; 4828 sqlite3_vfs *pMain; 4829 sqlite3_vfs *apVfs[20]; 4830 sqlite3_vfs one, two; 4831 4832 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ 4833 one.zName = "__one"; 4834 two.zName = "__two"; 4835 4836 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not 4837 ** change the default VFS 4838 */ 4839 pMain = sqlite3_vfs_find(0); 4840 sqlite3_vfs_register(&one, 0); 4841 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 4842 sqlite3_vfs_register(&two, 0); 4843 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 4844 4845 /* We can find a VFS by its name */ 4846 assert( sqlite3_vfs_find("__one")==&one ); 4847 assert( sqlite3_vfs_find("__two")==&two ); 4848 4849 /* Calling sqlite_vfs_register with non-zero second parameter changes the 4850 ** default VFS, even if the 1st parameter is an existig VFS that is 4851 ** previously registered as the non-default. 4852 */ 4853 sqlite3_vfs_register(&one, 1); 4854 assert( sqlite3_vfs_find("__one")==&one ); 4855 assert( sqlite3_vfs_find("__two")==&two ); 4856 assert( sqlite3_vfs_find(0)==&one ); 4857 sqlite3_vfs_register(&two, 1); 4858 assert( sqlite3_vfs_find("__one")==&one ); 4859 assert( sqlite3_vfs_find("__two")==&two ); 4860 assert( sqlite3_vfs_find(0)==&two ); 4861 if( pMain ){ 4862 sqlite3_vfs_register(pMain, 1); 4863 assert( sqlite3_vfs_find("__one")==&one ); 4864 assert( sqlite3_vfs_find("__two")==&two ); 4865 assert( sqlite3_vfs_find(0)==pMain ); 4866 } 4867 4868 /* Unlink the default VFS. Repeat until there are no more VFSes 4869 ** registered. 4870 */ 4871 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ 4872 apVfs[i] = sqlite3_vfs_find(0); 4873 if( apVfs[i] ){ 4874 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 4875 sqlite3_vfs_unregister(apVfs[i]); 4876 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); 4877 } 4878 } 4879 assert( 0==sqlite3_vfs_find(0) ); 4880 4881 /* Register the main VFS as non-default (will be made default, since 4882 ** it'll be the only one in existence). 4883 */ 4884 sqlite3_vfs_register(pMain, 0); 4885 assert( sqlite3_vfs_find(0)==pMain ); 4886 4887 /* Un-register the main VFS again to restore an empty VFS list */ 4888 sqlite3_vfs_unregister(pMain); 4889 assert( 0==sqlite3_vfs_find(0) ); 4890 4891 /* Relink all VFSes in reverse order. */ 4892 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ 4893 if( apVfs[i] ){ 4894 sqlite3_vfs_register(apVfs[i], 1); 4895 assert( apVfs[i]==sqlite3_vfs_find(0) ); 4896 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 4897 } 4898 } 4899 4900 /* Unregister out sample VFSes. */ 4901 sqlite3_vfs_unregister(&one); 4902 sqlite3_vfs_unregister(&two); 4903 4904 /* Unregistering a VFS that is not currently registered is harmless */ 4905 sqlite3_vfs_unregister(&one); 4906 sqlite3_vfs_unregister(&two); 4907 assert( sqlite3_vfs_find("__one")==0 ); 4908 assert( sqlite3_vfs_find("__two")==0 ); 4909 4910 /* We should be left with the original default VFS back as the 4911 ** original */ 4912 assert( sqlite3_vfs_find(0)==pMain ); 4913 4914 return TCL_OK; 4915 } 4916 4917 /* 4918 ** tclcmd: vfs_initfail_test 4919 ** 4920 ** This TCL command attempts to vfs_find and vfs_register when the 4921 ** sqlite3_initialize() interface is failing. All calls should fail. 4922 */ 4923 static int vfs_initfail_test( 4924 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4925 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4926 int objc, /* Number of arguments */ 4927 Tcl_Obj *CONST objv[] /* Command arguments */ 4928 ){ 4929 sqlite3_vfs one; 4930 one.zName = "__one"; 4931 4932 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4933 sqlite3_vfs_register(&one, 0); 4934 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4935 sqlite3_vfs_register(&one, 1); 4936 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4937 return TCL_OK; 4938 } 4939 4940 /* 4941 ** Saved VFSes 4942 */ 4943 static sqlite3_vfs *apVfs[20]; 4944 static int nVfs = 0; 4945 4946 /* 4947 ** tclcmd: vfs_unregister_all 4948 ** 4949 ** Unregister all VFSes. 4950 */ 4951 static int vfs_unregister_all( 4952 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4953 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4954 int objc, /* Number of arguments */ 4955 Tcl_Obj *CONST objv[] /* Command arguments */ 4956 ){ 4957 int i; 4958 for(i=0; i<ArraySize(apVfs); i++){ 4959 apVfs[i] = sqlite3_vfs_find(0); 4960 if( apVfs[i]==0 ) break; 4961 sqlite3_vfs_unregister(apVfs[i]); 4962 } 4963 nVfs = i; 4964 return TCL_OK; 4965 } 4966 /* 4967 ** tclcmd: vfs_reregister_all 4968 ** 4969 ** Restore all VFSes that were removed using vfs_unregister_all 4970 */ 4971 static int vfs_reregister_all( 4972 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4973 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4974 int objc, /* Number of arguments */ 4975 Tcl_Obj *CONST objv[] /* Command arguments */ 4976 ){ 4977 int i; 4978 for(i=0; i<nVfs; i++){ 4979 sqlite3_vfs_register(apVfs[i], i==0); 4980 } 4981 return TCL_OK; 4982 } 4983 4984 4985 /* 4986 ** tclcmd: file_control_test DB 4987 ** 4988 ** This TCL command runs the sqlite3_file_control interface and 4989 ** verifies correct operation of the same. 4990 */ 4991 static int file_control_test( 4992 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4993 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4994 int objc, /* Number of arguments */ 4995 Tcl_Obj *CONST objv[] /* Command arguments */ 4996 ){ 4997 int iArg = 0; 4998 sqlite3 *db; 4999 int rc; 5000 5001 if( objc!=2 ){ 5002 Tcl_AppendResult(interp, "wrong # args: should be \"", 5003 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5004 return TCL_ERROR; 5005 } 5006 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5007 rc = sqlite3_file_control(db, 0, 0, &iArg); 5008 assert( rc==SQLITE_NOTFOUND ); 5009 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); 5010 assert( rc==SQLITE_ERROR ); 5011 rc = sqlite3_file_control(db, "main", -1, &iArg); 5012 assert( rc==SQLITE_NOTFOUND ); 5013 rc = sqlite3_file_control(db, "temp", -1, &iArg); 5014 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); 5015 5016 return TCL_OK; 5017 } 5018 5019 5020 /* 5021 ** tclcmd: file_control_lasterrno_test DB 5022 ** 5023 ** This TCL command runs the sqlite3_file_control interface and 5024 ** verifies correct operation of the SQLITE_LAST_ERRNO verb. 5025 */ 5026 static int file_control_lasterrno_test( 5027 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5028 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5029 int objc, /* Number of arguments */ 5030 Tcl_Obj *CONST objv[] /* Command arguments */ 5031 ){ 5032 int iArg = 0; 5033 sqlite3 *db; 5034 int rc; 5035 5036 if( objc!=2 ){ 5037 Tcl_AppendResult(interp, "wrong # args: should be \"", 5038 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5039 return TCL_ERROR; 5040 } 5041 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5042 return TCL_ERROR; 5043 } 5044 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); 5045 if( rc ){ 5046 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5047 return TCL_ERROR; 5048 } 5049 if( iArg!=0 ) { 5050 Tcl_AppendResult(interp, "Unexpected non-zero errno: ", 5051 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); 5052 return TCL_ERROR; 5053 } 5054 return TCL_OK; 5055 } 5056 5057 /* 5058 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE 5059 ** 5060 ** This TCL command runs the sqlite3_file_control interface and 5061 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5062 ** SQLITE_SET_LOCKPROXYFILE verbs. 5063 */ 5064 static int file_control_chunksize_test( 5065 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5066 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5067 int objc, /* Number of arguments */ 5068 Tcl_Obj *CONST objv[] /* Command arguments */ 5069 ){ 5070 int nSize; /* New chunk size */ 5071 char *zDb; /* Db name ("main", "temp" etc.) */ 5072 sqlite3 *db; /* Database handle */ 5073 int rc; /* file_control() return code */ 5074 5075 if( objc!=4 ){ 5076 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5077 return TCL_ERROR; 5078 } 5079 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5080 || Tcl_GetIntFromObj(interp, objv[3], &nSize) 5081 ){ 5082 return TCL_ERROR; 5083 } 5084 zDb = Tcl_GetString(objv[2]); 5085 if( zDb[0]=='\0' ) zDb = NULL; 5086 5087 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); 5088 if( rc ){ 5089 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5090 return TCL_ERROR; 5091 } 5092 return TCL_OK; 5093 } 5094 5095 /* 5096 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE 5097 ** 5098 ** This TCL command runs the sqlite3_file_control interface 5099 ** with SQLITE_FCNTL_SIZE_HINT 5100 */ 5101 static int file_control_sizehint_test( 5102 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5103 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5104 int objc, /* Number of arguments */ 5105 Tcl_Obj *CONST objv[] /* Command arguments */ 5106 ){ 5107 Tcl_WideInt nSize; /* Hinted size */ 5108 char *zDb; /* Db name ("main", "temp" etc.) */ 5109 sqlite3 *db; /* Database handle */ 5110 int rc; /* file_control() return code */ 5111 5112 if( objc!=4 ){ 5113 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5114 return TCL_ERROR; 5115 } 5116 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5117 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) 5118 ){ 5119 return TCL_ERROR; 5120 } 5121 zDb = Tcl_GetString(objv[2]); 5122 if( zDb[0]=='\0' ) zDb = NULL; 5123 5124 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); 5125 if( rc ){ 5126 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5127 return TCL_ERROR; 5128 } 5129 return TCL_OK; 5130 } 5131 5132 /* 5133 ** tclcmd: file_control_lockproxy_test DB PWD 5134 ** 5135 ** This TCL command runs the sqlite3_file_control interface and 5136 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5137 ** SQLITE_SET_LOCKPROXYFILE verbs. 5138 */ 5139 static int file_control_lockproxy_test( 5140 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5141 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5142 int objc, /* Number of arguments */ 5143 Tcl_Obj *CONST objv[] /* Command arguments */ 5144 ){ 5145 sqlite3 *db; 5146 5147 if( objc!=3 ){ 5148 Tcl_AppendResult(interp, "wrong # args: should be \"", 5149 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); 5150 return TCL_ERROR; 5151 } 5152 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5153 return TCL_ERROR; 5154 } 5155 5156 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 5157 # if defined(__APPLE__) 5158 # define SQLITE_ENABLE_LOCKING_STYLE 1 5159 # else 5160 # define SQLITE_ENABLE_LOCKING_STYLE 0 5161 # endif 5162 #endif 5163 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 5164 { 5165 char *testPath; 5166 int rc; 5167 int nPwd; 5168 const char *zPwd; 5169 char proxyPath[400]; 5170 5171 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); 5172 if( sizeof(proxyPath)<nPwd+20 ){ 5173 Tcl_AppendResult(interp, "PWD too big", (void*)0); 5174 return TCL_ERROR; 5175 } 5176 sprintf(proxyPath, "%s/test.proxy", zPwd); 5177 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5178 if( rc ){ 5179 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5180 return TCL_ERROR; 5181 } 5182 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); 5183 if( strncmp(proxyPath,testPath,11) ){ 5184 Tcl_AppendResult(interp, "Lock proxy file did not match the " 5185 "previously assigned value", 0); 5186 return TCL_ERROR; 5187 } 5188 if( rc ){ 5189 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5190 return TCL_ERROR; 5191 } 5192 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5193 if( rc ){ 5194 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5195 return TCL_ERROR; 5196 } 5197 } 5198 #endif 5199 return TCL_OK; 5200 } 5201 5202 #if SQLITE_OS_WIN 5203 /* 5204 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY 5205 ** 5206 ** This TCL command runs the sqlite3_file_control interface with 5207 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. 5208 */ 5209 static int file_control_win32_av_retry( 5210 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5211 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5212 int objc, /* Number of arguments */ 5213 Tcl_Obj *CONST objv[] /* Command arguments */ 5214 ){ 5215 sqlite3 *db; 5216 int rc; 5217 int a[2]; 5218 char z[100]; 5219 5220 if( objc!=4 ){ 5221 Tcl_AppendResult(interp, "wrong # args: should be \"", 5222 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); 5223 return TCL_ERROR; 5224 } 5225 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5226 return TCL_ERROR; 5227 } 5228 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; 5229 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; 5230 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); 5231 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); 5232 Tcl_AppendResult(interp, z, (char*)0); 5233 return TCL_OK; 5234 } 5235 5236 /* 5237 ** tclcmd: file_control_win32_set_handle DB HANDLE 5238 ** 5239 ** This TCL command runs the sqlite3_file_control interface with 5240 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode. 5241 */ 5242 static int file_control_win32_set_handle( 5243 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5244 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5245 int objc, /* Number of arguments */ 5246 Tcl_Obj *CONST objv[] /* Command arguments */ 5247 ){ 5248 sqlite3 *db; 5249 int rc; 5250 HANDLE hFile = NULL; 5251 char z[100]; 5252 5253 if( objc!=3 ){ 5254 Tcl_AppendResult(interp, "wrong # args: should be \"", 5255 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0); 5256 return TCL_ERROR; 5257 } 5258 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5259 return TCL_ERROR; 5260 } 5261 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){ 5262 return TCL_ERROR; 5263 } 5264 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE, 5265 (void*)&hFile); 5266 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile); 5267 Tcl_AppendResult(interp, z, (char*)0); 5268 return TCL_OK; 5269 } 5270 #endif 5271 5272 /* 5273 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG 5274 ** 5275 ** This TCL command runs the sqlite3_file_control interface with 5276 ** the SQLITE_FCNTL_PERSIST_WAL opcode. 5277 */ 5278 static int file_control_persist_wal( 5279 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5280 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5281 int objc, /* Number of arguments */ 5282 Tcl_Obj *CONST objv[] /* Command arguments */ 5283 ){ 5284 sqlite3 *db; 5285 int rc; 5286 int bPersist; 5287 char z[100]; 5288 5289 if( objc!=3 ){ 5290 Tcl_AppendResult(interp, "wrong # args: should be \"", 5291 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5292 return TCL_ERROR; 5293 } 5294 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5295 return TCL_ERROR; 5296 } 5297 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; 5298 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); 5299 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); 5300 Tcl_AppendResult(interp, z, (char*)0); 5301 return TCL_OK; 5302 } 5303 5304 /* 5305 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG 5306 ** 5307 ** This TCL command runs the sqlite3_file_control interface with 5308 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. 5309 */ 5310 static int file_control_powersafe_overwrite( 5311 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5312 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5313 int objc, /* Number of arguments */ 5314 Tcl_Obj *CONST objv[] /* Command arguments */ 5315 ){ 5316 sqlite3 *db; 5317 int rc; 5318 int b; 5319 char z[100]; 5320 5321 if( objc!=3 ){ 5322 Tcl_AppendResult(interp, "wrong # args: should be \"", 5323 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5324 return TCL_ERROR; 5325 } 5326 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5327 return TCL_ERROR; 5328 } 5329 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR; 5330 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b); 5331 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b); 5332 Tcl_AppendResult(interp, z, (char*)0); 5333 return TCL_OK; 5334 } 5335 5336 5337 /* 5338 ** tclcmd: file_control_vfsname DB ?AUXDB? 5339 ** 5340 ** Return a string that describes the stack of VFSes. 5341 */ 5342 static int file_control_vfsname( 5343 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5344 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5345 int objc, /* Number of arguments */ 5346 Tcl_Obj *CONST objv[] /* Command arguments */ 5347 ){ 5348 sqlite3 *db; 5349 const char *zDbName = "main"; 5350 char *zVfsName = 0; 5351 5352 if( objc!=2 && objc!=3 ){ 5353 Tcl_AppendResult(interp, "wrong # args: should be \"", 5354 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5355 return TCL_ERROR; 5356 } 5357 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5358 return TCL_ERROR; 5359 } 5360 if( objc==3 ){ 5361 zDbName = Tcl_GetString(objv[2]); 5362 } 5363 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); 5364 Tcl_AppendResult(interp, zVfsName, (char*)0); 5365 sqlite3_free(zVfsName); 5366 return TCL_OK; 5367 } 5368 5369 /* 5370 ** tclcmd: file_control_tempfilename DB ?AUXDB? 5371 ** 5372 ** Return a string that is a temporary filename 5373 */ 5374 static int file_control_tempfilename( 5375 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5376 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5377 int objc, /* Number of arguments */ 5378 Tcl_Obj *CONST objv[] /* Command arguments */ 5379 ){ 5380 sqlite3 *db; 5381 const char *zDbName = "main"; 5382 char *zTName = 0; 5383 5384 if( objc!=2 && objc!=3 ){ 5385 Tcl_AppendResult(interp, "wrong # args: should be \"", 5386 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5387 return TCL_ERROR; 5388 } 5389 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5390 return TCL_ERROR; 5391 } 5392 if( objc==3 ){ 5393 zDbName = Tcl_GetString(objv[2]); 5394 } 5395 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName); 5396 Tcl_AppendResult(interp, zTName, (char*)0); 5397 sqlite3_free(zTName); 5398 return TCL_OK; 5399 } 5400 5401 5402 /* 5403 ** tclcmd: sqlite3_vfs_list 5404 ** 5405 ** Return a tcl list containing the names of all registered vfs's. 5406 */ 5407 static int vfs_list( 5408 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5409 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5410 int objc, /* Number of arguments */ 5411 Tcl_Obj *CONST objv[] /* Command arguments */ 5412 ){ 5413 sqlite3_vfs *pVfs; 5414 Tcl_Obj *pRet = Tcl_NewObj(); 5415 if( objc!=1 ){ 5416 Tcl_WrongNumArgs(interp, 1, objv, ""); 5417 return TCL_ERROR; 5418 } 5419 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 5420 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); 5421 } 5422 Tcl_SetObjResult(interp, pRet); 5423 return TCL_OK; 5424 } 5425 5426 /* 5427 ** tclcmd: sqlite3_limit DB ID VALUE 5428 ** 5429 ** This TCL command runs the sqlite3_limit interface and 5430 ** verifies correct operation of the same. 5431 */ 5432 static int test_limit( 5433 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5434 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5435 int objc, /* Number of arguments */ 5436 Tcl_Obj *CONST objv[] /* Command arguments */ 5437 ){ 5438 sqlite3 *db; 5439 int rc; 5440 static const struct { 5441 char *zName; 5442 int id; 5443 } aId[] = { 5444 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, 5445 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, 5446 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, 5447 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, 5448 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, 5449 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, 5450 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, 5451 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, 5452 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 5453 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, 5454 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, 5455 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS }, 5456 5457 /* Out of range test cases */ 5458 { "SQLITE_LIMIT_TOOSMALL", -1, }, 5459 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 }, 5460 }; 5461 int i, id = 0; 5462 int val; 5463 const char *zId; 5464 5465 if( objc!=4 ){ 5466 Tcl_AppendResult(interp, "wrong # args: should be \"", 5467 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); 5468 return TCL_ERROR; 5469 } 5470 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5471 zId = Tcl_GetString(objv[2]); 5472 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ 5473 if( strcmp(zId, aId[i].zName)==0 ){ 5474 id = aId[i].id; 5475 break; 5476 } 5477 } 5478 if( i>=sizeof(aId)/sizeof(aId[0]) ){ 5479 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); 5480 return TCL_ERROR; 5481 } 5482 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 5483 rc = sqlite3_limit(db, id, val); 5484 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5485 return TCL_OK; 5486 } 5487 5488 /* 5489 ** tclcmd: save_prng_state 5490 ** 5491 ** Save the state of the pseudo-random number generator. 5492 ** At the same time, verify that sqlite3_test_control works even when 5493 ** called with an out-of-range opcode. 5494 */ 5495 static int save_prng_state( 5496 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5497 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5498 int objc, /* Number of arguments */ 5499 Tcl_Obj *CONST objv[] /* Command arguments */ 5500 ){ 5501 int rc = sqlite3_test_control(9999); 5502 assert( rc==0 ); 5503 rc = sqlite3_test_control(-1); 5504 assert( rc==0 ); 5505 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); 5506 return TCL_OK; 5507 } 5508 /* 5509 ** tclcmd: restore_prng_state 5510 */ 5511 static int restore_prng_state( 5512 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5513 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5514 int objc, /* Number of arguments */ 5515 Tcl_Obj *CONST objv[] /* Command arguments */ 5516 ){ 5517 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); 5518 return TCL_OK; 5519 } 5520 /* 5521 ** tclcmd: reset_prng_state 5522 */ 5523 static int reset_prng_state( 5524 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5525 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5526 int objc, /* Number of arguments */ 5527 Tcl_Obj *CONST objv[] /* Command arguments */ 5528 ){ 5529 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); 5530 return TCL_OK; 5531 } 5532 5533 /* 5534 ** tclcmd: database_may_be_corrupt 5535 ** 5536 ** Indicate that database files might be corrupt. In other words, set the normal 5537 ** state of operation. 5538 */ 5539 static int database_may_be_corrupt( 5540 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5541 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5542 int objc, /* Number of arguments */ 5543 Tcl_Obj *CONST objv[] /* Command arguments */ 5544 ){ 5545 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0); 5546 return TCL_OK; 5547 } 5548 /* 5549 ** tclcmd: database_never_corrupt 5550 ** 5551 ** Indicate that database files are always well-formed. This enables extra assert() 5552 ** statements that test conditions that are always true for well-formed databases. 5553 */ 5554 static int database_never_corrupt( 5555 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5556 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5557 int objc, /* Number of arguments */ 5558 Tcl_Obj *CONST objv[] /* Command arguments */ 5559 ){ 5560 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1); 5561 return TCL_OK; 5562 } 5563 5564 /* 5565 ** tclcmd: pcache_stats 5566 */ 5567 static int test_pcache_stats( 5568 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5569 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5570 int objc, /* Number of arguments */ 5571 Tcl_Obj *CONST objv[] /* Command arguments */ 5572 ){ 5573 int nMin; 5574 int nMax; 5575 int nCurrent; 5576 int nRecyclable; 5577 Tcl_Obj *pRet; 5578 5579 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); 5580 5581 pRet = Tcl_NewObj(); 5582 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); 5583 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); 5584 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); 5585 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); 5586 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); 5587 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); 5588 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); 5589 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); 5590 5591 Tcl_SetObjResult(interp, pRet); 5592 5593 return TCL_OK; 5594 } 5595 5596 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5597 static void test_unlock_notify_cb(void **aArg, int nArg){ 5598 int ii; 5599 for(ii=0; ii<nArg; ii++){ 5600 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); 5601 } 5602 } 5603 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ 5604 5605 /* 5606 ** tclcmd: sqlite3_unlock_notify db 5607 */ 5608 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5609 static int test_unlock_notify( 5610 ClientData clientData, /* Unused */ 5611 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5612 int objc, /* Number of arguments */ 5613 Tcl_Obj *CONST objv[] /* Command arguments */ 5614 ){ 5615 sqlite3 *db; 5616 int rc; 5617 5618 if( objc!=2 ){ 5619 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 5620 return TCL_ERROR; 5621 } 5622 5623 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5624 return TCL_ERROR; 5625 } 5626 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); 5627 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5628 return TCL_OK; 5629 } 5630 #endif 5631 5632 /* 5633 ** tclcmd: sqlite3_wal_checkpoint db ?NAME? 5634 */ 5635 static int test_wal_checkpoint( 5636 ClientData clientData, /* Unused */ 5637 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5638 int objc, /* Number of arguments */ 5639 Tcl_Obj *CONST objv[] /* Command arguments */ 5640 ){ 5641 char *zDb = 0; 5642 sqlite3 *db; 5643 int rc; 5644 5645 if( objc!=3 && objc!=2 ){ 5646 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); 5647 return TCL_ERROR; 5648 } 5649 5650 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5651 return TCL_ERROR; 5652 } 5653 if( objc==3 ){ 5654 zDb = Tcl_GetString(objv[2]); 5655 } 5656 rc = sqlite3_wal_checkpoint(db, zDb); 5657 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5658 return TCL_OK; 5659 } 5660 5661 /* 5662 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? 5663 ** 5664 ** This command calls the wal_checkpoint_v2() function with the specified 5665 ** mode argument (passive, full or restart). If present, the database name 5666 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the 5667 ** NAME argument is not present, a NULL pointer is passed instead. 5668 ** 5669 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or 5670 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set 5671 ** to the error message obtained from sqlite3_errmsg(). 5672 ** 5673 ** Otherwise, this command returns a list of three integers. The first integer 5674 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers 5675 ** are the values returned via the output parameters by wal_checkpoint_v2() - 5676 ** the number of frames in the log and the number of frames in the log 5677 ** that have been checkpointed. 5678 */ 5679 static int test_wal_checkpoint_v2( 5680 ClientData clientData, /* Unused */ 5681 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5682 int objc, /* Number of arguments */ 5683 Tcl_Obj *CONST objv[] /* Command arguments */ 5684 ){ 5685 char *zDb = 0; 5686 sqlite3 *db; 5687 int rc; 5688 5689 int eMode; 5690 int nLog = -555; 5691 int nCkpt = -555; 5692 Tcl_Obj *pRet; 5693 5694 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 }; 5695 assert( SQLITE_CHECKPOINT_PASSIVE==0 ); 5696 assert( SQLITE_CHECKPOINT_FULL==1 ); 5697 assert( SQLITE_CHECKPOINT_RESTART==2 ); 5698 assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); 5699 5700 if( objc!=3 && objc!=4 ){ 5701 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); 5702 return TCL_ERROR; 5703 } 5704 5705 if( objc==4 ){ 5706 zDb = Tcl_GetString(objv[3]); 5707 } 5708 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || ( 5709 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode) 5710 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) 5711 )){ 5712 return TCL_ERROR; 5713 } 5714 5715 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); 5716 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 5717 const char *zErrCode = sqlite3ErrName(rc); 5718 Tcl_ResetResult(interp); 5719 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0); 5720 return TCL_ERROR; 5721 } 5722 5723 pRet = Tcl_NewObj(); 5724 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); 5725 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); 5726 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); 5727 Tcl_SetObjResult(interp, pRet); 5728 5729 return TCL_OK; 5730 } 5731 5732 /* 5733 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE 5734 */ 5735 static int test_wal_autocheckpoint( 5736 ClientData clientData, /* Unused */ 5737 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5738 int objc, /* Number of arguments */ 5739 Tcl_Obj *CONST objv[] /* Command arguments */ 5740 ){ 5741 sqlite3 *db; 5742 int rc; 5743 int iVal; 5744 5745 5746 if( objc!=3 ){ 5747 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE"); 5748 return TCL_ERROR; 5749 } 5750 5751 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5752 || Tcl_GetIntFromObj(0, objv[2], &iVal) 5753 ){ 5754 return TCL_ERROR; 5755 } 5756 5757 rc = sqlite3_wal_autocheckpoint(db, iVal); 5758 Tcl_ResetResult(interp); 5759 if( rc!=SQLITE_OK ){ 5760 const char *zErrCode = sqlite3ErrName(rc); 5761 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1)); 5762 return TCL_ERROR; 5763 } 5764 5765 return TCL_OK; 5766 } 5767 5768 5769 /* 5770 ** tclcmd: test_sqlite3_log ?SCRIPT? 5771 */ 5772 static struct LogCallback { 5773 Tcl_Interp *pInterp; 5774 Tcl_Obj *pObj; 5775 } logcallback = {0, 0}; 5776 static void xLogcallback(void *unused, int err, char *zMsg){ 5777 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); 5778 Tcl_IncrRefCount(pNew); 5779 Tcl_ListObjAppendElement( 5780 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1) 5781 ); 5782 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); 5783 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 5784 Tcl_DecrRefCount(pNew); 5785 } 5786 static int test_sqlite3_log( 5787 ClientData clientData, 5788 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5789 int objc, /* Number of arguments */ 5790 Tcl_Obj *CONST objv[] /* Command arguments */ 5791 ){ 5792 if( objc>2 ){ 5793 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); 5794 return TCL_ERROR; 5795 } 5796 if( logcallback.pObj ){ 5797 Tcl_DecrRefCount(logcallback.pObj); 5798 logcallback.pObj = 0; 5799 logcallback.pInterp = 0; 5800 sqlite3_config(SQLITE_CONFIG_LOG, 0, 0); 5801 } 5802 if( objc>1 ){ 5803 logcallback.pObj = objv[1]; 5804 Tcl_IncrRefCount(logcallback.pObj); 5805 logcallback.pInterp = interp; 5806 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0); 5807 } 5808 return TCL_OK; 5809 } 5810 5811 /* 5812 ** tcl_objproc COMMANDNAME ARGS... 5813 ** 5814 ** Run a TCL command using its objProc interface. Throw an error if 5815 ** the command has no objProc interface. 5816 */ 5817 static int runAsObjProc( 5818 void * clientData, 5819 Tcl_Interp *interp, 5820 int objc, 5821 Tcl_Obj *CONST objv[] 5822 ){ 5823 Tcl_CmdInfo cmdInfo; 5824 if( objc<2 ){ 5825 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); 5826 return TCL_ERROR; 5827 } 5828 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 5829 Tcl_AppendResult(interp, "command not found: ", 5830 Tcl_GetString(objv[1]), (char*)0); 5831 return TCL_ERROR; 5832 } 5833 if( cmdInfo.objProc==0 ){ 5834 Tcl_AppendResult(interp, "command has no objProc: ", 5835 Tcl_GetString(objv[1]), (char*)0); 5836 return TCL_ERROR; 5837 } 5838 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); 5839 } 5840 5841 #ifndef SQLITE_OMIT_EXPLAIN 5842 /* 5843 ** WARNING: The following function, printExplainQueryPlan() is an exact 5844 ** copy of example code from eqp.in (eqp.html). If this code is modified, 5845 ** then the documentation copy needs to be modified as well. 5846 */ 5847 /* 5848 ** Argument pStmt is a prepared SQL statement. This function compiles 5849 ** an EXPLAIN QUERY PLAN command to report on the prepared statement, 5850 ** and prints the report to stdout using printf(). 5851 */ 5852 int printExplainQueryPlan(sqlite3_stmt *pStmt){ 5853 const char *zSql; /* Input SQL */ 5854 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ 5855 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ 5856 int rc; /* Return code from sqlite3_prepare_v2() */ 5857 5858 zSql = sqlite3_sql(pStmt); 5859 if( zSql==0 ) return SQLITE_ERROR; 5860 5861 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); 5862 if( zExplain==0 ) return SQLITE_NOMEM; 5863 5864 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); 5865 sqlite3_free(zExplain); 5866 if( rc!=SQLITE_OK ) return rc; 5867 5868 while( SQLITE_ROW==sqlite3_step(pExplain) ){ 5869 int iSelectid = sqlite3_column_int(pExplain, 0); 5870 int iOrder = sqlite3_column_int(pExplain, 1); 5871 int iFrom = sqlite3_column_int(pExplain, 2); 5872 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); 5873 5874 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); 5875 } 5876 5877 return sqlite3_finalize(pExplain); 5878 } 5879 5880 static int test_print_eqp( 5881 void * clientData, 5882 Tcl_Interp *interp, 5883 int objc, 5884 Tcl_Obj *CONST objv[] 5885 ){ 5886 int rc; 5887 sqlite3_stmt *pStmt; 5888 5889 if( objc!=2 ){ 5890 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 5891 return TCL_ERROR; 5892 } 5893 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 5894 rc = printExplainQueryPlan(pStmt); 5895 /* This is needed on Windows so that a test case using this 5896 ** function can open a read pipe and get the output of 5897 ** printExplainQueryPlan() immediately. 5898 */ 5899 fflush(stdout); 5900 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 5901 return TCL_OK; 5902 } 5903 #endif /* SQLITE_OMIT_EXPLAIN */ 5904 5905 /* 5906 ** sqlite3_test_control VERB ARGS... 5907 */ 5908 static int test_test_control( 5909 void * clientData, 5910 Tcl_Interp *interp, 5911 int objc, 5912 Tcl_Obj *CONST objv[] 5913 ){ 5914 struct Verb { 5915 const char *zName; 5916 int i; 5917 } aVerb[] = { 5918 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, 5919 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP }, 5920 }; 5921 int iVerb; 5922 int iFlag; 5923 int rc; 5924 5925 if( objc<2 ){ 5926 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); 5927 return TCL_ERROR; 5928 } 5929 5930 rc = Tcl_GetIndexFromObjStruct( 5931 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb 5932 ); 5933 if( rc!=TCL_OK ) return rc; 5934 5935 iFlag = aVerb[iVerb].i; 5936 switch( iFlag ){ 5937 case SQLITE_TESTCTRL_LOCALTIME_FAULT: { 5938 int val; 5939 if( objc!=3 ){ 5940 Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); 5941 return TCL_ERROR; 5942 } 5943 if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 5944 sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); 5945 break; 5946 } 5947 5948 case SQLITE_TESTCTRL_SORTER_MMAP: { 5949 int val; 5950 sqlite3 *db; 5951 if( objc!=4 ){ 5952 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT"); 5953 return TCL_ERROR; 5954 } 5955 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; 5956 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 5957 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val); 5958 break; 5959 } 5960 } 5961 5962 Tcl_ResetResult(interp); 5963 return TCL_OK; 5964 } 5965 5966 #if SQLITE_OS_UNIX 5967 #include <sys/time.h> 5968 #include <sys/resource.h> 5969 5970 static int test_getrusage( 5971 void * clientData, 5972 Tcl_Interp *interp, 5973 int objc, 5974 Tcl_Obj *CONST objv[] 5975 ){ 5976 char buf[1024]; 5977 struct rusage r; 5978 memset(&r, 0, sizeof(r)); 5979 getrusage(RUSAGE_SELF, &r); 5980 5981 sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", 5982 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, 5983 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, 5984 (int)r.ru_minflt, (int)r.ru_majflt 5985 ); 5986 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1)); 5987 return TCL_OK; 5988 } 5989 #endif 5990 5991 #if SQLITE_OS_WIN 5992 /* 5993 ** Information passed from the main thread into the windows file locker 5994 ** background thread. 5995 */ 5996 struct win32FileLocker { 5997 char *evName; /* Name of event to signal thread startup */ 5998 HANDLE h; /* Handle of the file to be locked */ 5999 int delay1; /* Delay before locking */ 6000 int delay2; /* Delay before unlocking */ 6001 int ok; /* Finished ok */ 6002 int err; /* True if an error occurs */ 6003 }; 6004 #endif 6005 6006 6007 #if SQLITE_OS_WIN 6008 #include <process.h> 6009 /* 6010 ** The background thread that does file locking. 6011 */ 6012 static void win32_file_locker(void *pAppData){ 6013 struct win32FileLocker *p = (struct win32FileLocker*)pAppData; 6014 if( p->evName ){ 6015 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); 6016 if ( ev ){ 6017 SetEvent(ev); 6018 CloseHandle(ev); 6019 } 6020 } 6021 if( p->delay1 ) Sleep(p->delay1); 6022 if( LockFile(p->h, 0, 0, 100000000, 0) ){ 6023 Sleep(p->delay2); 6024 UnlockFile(p->h, 0, 0, 100000000, 0); 6025 p->ok = 1; 6026 }else{ 6027 p->err = 1; 6028 } 6029 CloseHandle(p->h); 6030 p->h = 0; 6031 p->delay1 = 0; 6032 p->delay2 = 0; 6033 } 6034 #endif 6035 6036 #if SQLITE_OS_WIN 6037 /* 6038 ** lock_win32_file FILENAME DELAY1 DELAY2 6039 ** 6040 ** Get an exclusive manditory lock on file for DELAY2 milliseconds. 6041 ** Wait DELAY1 milliseconds before acquiring the lock. 6042 */ 6043 static int win32_file_lock( 6044 void * clientData, 6045 Tcl_Interp *interp, 6046 int objc, 6047 Tcl_Obj *CONST objv[] 6048 ){ 6049 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; 6050 const char *zFilename; 6051 char zBuf[200]; 6052 int retry = 0; 6053 HANDLE ev; 6054 DWORD wResult; 6055 6056 if( objc!=4 && objc!=1 ){ 6057 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); 6058 return TCL_ERROR; 6059 } 6060 if( objc==1 ){ 6061 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", 6062 x.ok, x.err, x.delay1, x.delay2, x.h); 6063 Tcl_AppendResult(interp, zBuf, (char*)0); 6064 return TCL_OK; 6065 } 6066 while( x.h && retry<30 ){ 6067 retry++; 6068 Sleep(100); 6069 } 6070 if( x.h ){ 6071 Tcl_AppendResult(interp, "busy", (char*)0); 6072 return TCL_ERROR; 6073 } 6074 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; 6075 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; 6076 zFilename = Tcl_GetString(objv[1]); 6077 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, 6078 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 6079 FILE_ATTRIBUTE_NORMAL, 0); 6080 if( !x.h ){ 6081 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); 6082 return TCL_ERROR; 6083 } 6084 ev = CreateEvent(NULL, TRUE, FALSE, x.evName); 6085 if ( !ev ){ 6086 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); 6087 return TCL_ERROR; 6088 } 6089 _beginthread(win32_file_locker, 0, (void*)&x); 6090 Sleep(0); 6091 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ 6092 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); 6093 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); 6094 CloseHandle(ev); 6095 return TCL_ERROR; 6096 } 6097 CloseHandle(ev); 6098 return TCL_OK; 6099 } 6100 6101 /* 6102 ** exists_win32_path PATH 6103 ** 6104 ** Returns non-zero if the specified path exists, whose fully qualified name 6105 ** may exceed 260 characters if it is prefixed with "\\?\". 6106 */ 6107 static int win32_exists_path( 6108 void *clientData, 6109 Tcl_Interp *interp, 6110 int objc, 6111 Tcl_Obj *CONST objv[] 6112 ){ 6113 if( objc!=2 ){ 6114 Tcl_WrongNumArgs(interp, 1, objv, "PATH"); 6115 return TCL_ERROR; 6116 } 6117 Tcl_SetObjResult(interp, Tcl_NewBooleanObj( 6118 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES )); 6119 return TCL_OK; 6120 } 6121 6122 /* 6123 ** find_win32_file PATTERN 6124 ** 6125 ** Returns a list of entries in a directory that match the specified pattern, 6126 ** whose fully qualified name may exceed 248 characters if it is prefixed with 6127 ** "\\?\". 6128 */ 6129 static int win32_find_file( 6130 void *clientData, 6131 Tcl_Interp *interp, 6132 int objc, 6133 Tcl_Obj *CONST objv[] 6134 ){ 6135 HANDLE hFindFile = INVALID_HANDLE_VALUE; 6136 WIN32_FIND_DATAW findData; 6137 Tcl_Obj *listObj; 6138 DWORD lastErrno; 6139 if( objc!=2 ){ 6140 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN"); 6141 return TCL_ERROR; 6142 } 6143 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData); 6144 if( hFindFile==INVALID_HANDLE_VALUE ){ 6145 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6146 return TCL_ERROR; 6147 } 6148 listObj = Tcl_NewObj(); 6149 Tcl_IncrRefCount(listObj); 6150 do { 6151 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj( 6152 findData.cFileName, -1)); 6153 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj( 6154 findData.dwFileAttributes)); 6155 } while( FindNextFileW(hFindFile, &findData) ); 6156 lastErrno = GetLastError(); 6157 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){ 6158 FindClose(hFindFile); 6159 Tcl_DecrRefCount(listObj); 6160 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6161 return TCL_ERROR; 6162 } 6163 FindClose(hFindFile); 6164 Tcl_SetObjResult(interp, listObj); 6165 return TCL_OK; 6166 } 6167 6168 /* 6169 ** delete_win32_file FILENAME 6170 ** 6171 ** Deletes the specified file, whose fully qualified name may exceed 260 6172 ** characters if it is prefixed with "\\?\". 6173 */ 6174 static int win32_delete_file( 6175 void *clientData, 6176 Tcl_Interp *interp, 6177 int objc, 6178 Tcl_Obj *CONST objv[] 6179 ){ 6180 if( objc!=2 ){ 6181 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 6182 return TCL_ERROR; 6183 } 6184 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){ 6185 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6186 return TCL_ERROR; 6187 } 6188 Tcl_ResetResult(interp); 6189 return TCL_OK; 6190 } 6191 6192 /* 6193 ** make_win32_dir DIRECTORY 6194 ** 6195 ** Creates the specified directory, whose fully qualified name may exceed 248 6196 ** characters if it is prefixed with "\\?\". 6197 */ 6198 static int win32_mkdir( 6199 void *clientData, 6200 Tcl_Interp *interp, 6201 int objc, 6202 Tcl_Obj *CONST objv[] 6203 ){ 6204 if( objc!=2 ){ 6205 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); 6206 return TCL_ERROR; 6207 } 6208 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){ 6209 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6210 return TCL_ERROR; 6211 } 6212 Tcl_ResetResult(interp); 6213 return TCL_OK; 6214 } 6215 6216 /* 6217 ** remove_win32_dir DIRECTORY 6218 ** 6219 ** Removes the specified directory, whose fully qualified name may exceed 248 6220 ** characters if it is prefixed with "\\?\". 6221 */ 6222 static int win32_rmdir( 6223 void *clientData, 6224 Tcl_Interp *interp, 6225 int objc, 6226 Tcl_Obj *CONST objv[] 6227 ){ 6228 if( objc!=2 ){ 6229 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); 6230 return TCL_ERROR; 6231 } 6232 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){ 6233 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6234 return TCL_ERROR; 6235 } 6236 Tcl_ResetResult(interp); 6237 return TCL_OK; 6238 } 6239 #endif 6240 6241 6242 /* 6243 ** optimization_control DB OPT BOOLEAN 6244 ** 6245 ** Enable or disable query optimizations using the sqlite3_test_control() 6246 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. 6247 ** OPT is the name of the optimization to be disabled. 6248 */ 6249 static int optimization_control( 6250 void * clientData, 6251 Tcl_Interp *interp, 6252 int objc, 6253 Tcl_Obj *CONST objv[] 6254 ){ 6255 int i; 6256 sqlite3 *db; 6257 const char *zOpt; 6258 int onoff; 6259 int mask = 0; 6260 static const struct { 6261 const char *zOptName; 6262 int mask; 6263 } aOpt[] = { 6264 { "all", SQLITE_AllOpts }, 6265 { "none", 0 }, 6266 { "query-flattener", SQLITE_QueryFlattener }, 6267 { "column-cache", SQLITE_ColumnCache }, 6268 { "groupby-order", SQLITE_GroupByOrder }, 6269 { "factor-constants", SQLITE_FactorOutConst }, 6270 { "distinct-opt", SQLITE_DistinctOpt }, 6271 { "cover-idx-scan", SQLITE_CoverIdxScan }, 6272 { "order-by-idx-join", SQLITE_OrderByIdxJoin }, 6273 { "transitive", SQLITE_Transitive }, 6274 { "subquery-coroutine", SQLITE_SubqCoroutine }, 6275 { "omit-noop-join", SQLITE_OmitNoopJoin }, 6276 { "stat3", SQLITE_Stat34 }, 6277 { "stat4", SQLITE_Stat34 }, 6278 }; 6279 6280 if( objc!=4 ){ 6281 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); 6282 return TCL_ERROR; 6283 } 6284 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6285 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; 6286 zOpt = Tcl_GetString(objv[2]); 6287 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 6288 if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ 6289 mask = aOpt[i].mask; 6290 break; 6291 } 6292 } 6293 if( onoff ) mask = ~mask; 6294 if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ 6295 Tcl_AppendResult(interp, "unknown optimization - should be one of:", 6296 (char*)0); 6297 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 6298 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0); 6299 } 6300 return TCL_ERROR; 6301 } 6302 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); 6303 return TCL_OK; 6304 } 6305 6306 typedef struct sqlite3_api_routines sqlite3_api_routines; 6307 /* 6308 ** load_static_extension DB NAME ... 6309 ** 6310 ** Load one or more statically linked extensions. 6311 */ 6312 static int tclLoadStaticExtensionCmd( 6313 void * clientData, 6314 Tcl_Interp *interp, 6315 int objc, 6316 Tcl_Obj *CONST objv[] 6317 ){ 6318 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); 6319 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); 6320 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*); 6321 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*); 6322 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); 6323 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); 6324 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); 6325 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*); 6326 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); 6327 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); 6328 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); 6329 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); 6330 static const struct { 6331 const char *zExtName; 6332 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); 6333 } aExtension[] = { 6334 { "amatch", sqlite3_amatch_init }, 6335 { "closure", sqlite3_closure_init }, 6336 { "eval", sqlite3_eval_init }, 6337 { "fileio", sqlite3_fileio_init }, 6338 { "fuzzer", sqlite3_fuzzer_init }, 6339 { "ieee754", sqlite3_ieee_init }, 6340 { "nextchar", sqlite3_nextchar_init }, 6341 { "percentile", sqlite3_percentile_init }, 6342 { "regexp", sqlite3_regexp_init }, 6343 { "spellfix", sqlite3_spellfix_init }, 6344 { "totype", sqlite3_totype_init }, 6345 { "wholenumber", sqlite3_wholenumber_init }, 6346 }; 6347 sqlite3 *db; 6348 const char *zName; 6349 int i, j, rc; 6350 char *zErrMsg = 0; 6351 if( objc<3 ){ 6352 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); 6353 return TCL_ERROR; 6354 } 6355 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6356 for(j=2; j<objc; j++){ 6357 zName = Tcl_GetString(objv[j]); 6358 for(i=0; i<ArraySize(aExtension); i++){ 6359 if( strcmp(zName, aExtension[i].zExtName)==0 ) break; 6360 } 6361 if( i>=ArraySize(aExtension) ){ 6362 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0); 6363 return TCL_ERROR; 6364 } 6365 rc = aExtension[i].pInit(db, &zErrMsg, 0); 6366 if( rc!=SQLITE_OK || zErrMsg ){ 6367 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, 6368 (char*)0); 6369 sqlite3_free(zErrMsg); 6370 return TCL_ERROR; 6371 } 6372 } 6373 return TCL_OK; 6374 } 6375 6376 /* 6377 ** sorter_test_fakeheap BOOL 6378 ** 6379 */ 6380 static int sorter_test_fakeheap( 6381 void * clientData, 6382 Tcl_Interp *interp, 6383 int objc, 6384 Tcl_Obj *CONST objv[] 6385 ){ 6386 int bArg; 6387 if( objc!=2 ){ 6388 Tcl_WrongNumArgs(interp, 1, objv, "BOOL"); 6389 return TCL_ERROR; 6390 } 6391 6392 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){ 6393 return TCL_ERROR; 6394 } 6395 6396 if( bArg ){ 6397 if( sqlite3GlobalConfig.pHeap==0 ){ 6398 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1); 6399 } 6400 }else{ 6401 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){ 6402 sqlite3GlobalConfig.pHeap = 0; 6403 } 6404 } 6405 6406 Tcl_ResetResult(interp); 6407 return TCL_OK; 6408 } 6409 6410 /* 6411 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2 6412 ** 6413 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row, 6414 ** check that the leftmost and rightmost columns returned are both integers, 6415 ** and that both contain the same value. 6416 ** 6417 ** Then execute statement $SQL2. Check that the statement returns the same 6418 ** set of integers in the same order as in the previous step (using $SQL1). 6419 */ 6420 static int sorter_test_sort4_helper( 6421 void * clientData, 6422 Tcl_Interp *interp, 6423 int objc, 6424 Tcl_Obj *CONST objv[] 6425 ){ 6426 const char *zSql1; 6427 const char *zSql2; 6428 int nStep; 6429 int iStep; 6430 int iCksum1 = 0; 6431 int iCksum2 = 0; 6432 int rc; 6433 int iB; 6434 sqlite3 *db; 6435 sqlite3_stmt *pStmt; 6436 6437 if( objc!=5 ){ 6438 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2"); 6439 return TCL_ERROR; 6440 } 6441 6442 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6443 zSql1 = Tcl_GetString(objv[2]); 6444 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR; 6445 zSql2 = Tcl_GetString(objv[4]); 6446 6447 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0); 6448 if( rc!=SQLITE_OK ) goto sql_error; 6449 6450 iB = sqlite3_column_count(pStmt)-1; 6451 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){ 6452 int a = sqlite3_column_int(pStmt, 0); 6453 if( a!=sqlite3_column_int(pStmt, iB) ){ 6454 Tcl_AppendResult(interp, "data error: (a!=b)", 0); 6455 return TCL_ERROR; 6456 } 6457 6458 iCksum1 += (iCksum1 << 3) + a; 6459 } 6460 rc = sqlite3_finalize(pStmt); 6461 if( rc!=SQLITE_OK ) goto sql_error; 6462 6463 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0); 6464 if( rc!=SQLITE_OK ) goto sql_error; 6465 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){ 6466 int a = sqlite3_column_int(pStmt, 0); 6467 iCksum2 += (iCksum2 << 3) + a; 6468 } 6469 rc = sqlite3_finalize(pStmt); 6470 if( rc!=SQLITE_OK ) goto sql_error; 6471 6472 if( iCksum1!=iCksum2 ){ 6473 Tcl_AppendResult(interp, "checksum mismatch", 0); 6474 return TCL_ERROR; 6475 } 6476 6477 return TCL_OK; 6478 sql_error: 6479 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0); 6480 return TCL_ERROR; 6481 } 6482 6483 6484 #ifdef SQLITE_USER_AUTHENTICATION 6485 #include "sqlite3userauth.h" 6486 /* 6487 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD 6488 */ 6489 static int test_user_authenticate( 6490 ClientData clientData, /* Unused */ 6491 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6492 int objc, /* Number of arguments */ 6493 Tcl_Obj *CONST objv[] /* Command arguments */ 6494 ){ 6495 char *zUser = 0; 6496 char *zPasswd = 0; 6497 int nPasswd = 0; 6498 sqlite3 *db; 6499 int rc; 6500 6501 if( objc!=4 ){ 6502 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD"); 6503 return TCL_ERROR; 6504 } 6505 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6506 return TCL_ERROR; 6507 } 6508 zUser = Tcl_GetString(objv[2]); 6509 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6510 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd); 6511 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6512 return TCL_OK; 6513 } 6514 #endif /* SQLITE_USER_AUTHENTICATION */ 6515 6516 #ifdef SQLITE_USER_AUTHENTICATION 6517 /* 6518 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN 6519 */ 6520 static int test_user_add( 6521 ClientData clientData, /* Unused */ 6522 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6523 int objc, /* Number of arguments */ 6524 Tcl_Obj *CONST objv[] /* Command arguments */ 6525 ){ 6526 char *zUser = 0; 6527 char *zPasswd = 0; 6528 int nPasswd = 0; 6529 int isAdmin = 0; 6530 sqlite3 *db; 6531 int rc; 6532 6533 if( objc!=5 ){ 6534 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); 6535 return TCL_ERROR; 6536 } 6537 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6538 return TCL_ERROR; 6539 } 6540 zUser = Tcl_GetString(objv[2]); 6541 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6542 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); 6543 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin); 6544 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6545 return TCL_OK; 6546 } 6547 #endif /* SQLITE_USER_AUTHENTICATION */ 6548 6549 #ifdef SQLITE_USER_AUTHENTICATION 6550 /* 6551 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN 6552 */ 6553 static int test_user_change( 6554 ClientData clientData, /* Unused */ 6555 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6556 int objc, /* Number of arguments */ 6557 Tcl_Obj *CONST objv[] /* Command arguments */ 6558 ){ 6559 char *zUser = 0; 6560 char *zPasswd = 0; 6561 int nPasswd = 0; 6562 int isAdmin = 0; 6563 sqlite3 *db; 6564 int rc; 6565 6566 if( objc!=5 ){ 6567 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); 6568 return TCL_ERROR; 6569 } 6570 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6571 return TCL_ERROR; 6572 } 6573 zUser = Tcl_GetString(objv[2]); 6574 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6575 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); 6576 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin); 6577 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6578 return TCL_OK; 6579 } 6580 #endif /* SQLITE_USER_AUTHENTICATION */ 6581 6582 #ifdef SQLITE_USER_AUTHENTICATION 6583 /* 6584 ** tclcmd: sqlite3_user_delete DB USERNAME 6585 */ 6586 static int test_user_delete( 6587 ClientData clientData, /* Unused */ 6588 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6589 int objc, /* Number of arguments */ 6590 Tcl_Obj *CONST objv[] /* Command arguments */ 6591 ){ 6592 char *zUser = 0; 6593 sqlite3 *db; 6594 int rc; 6595 6596 if( objc!=3 ){ 6597 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME"); 6598 return TCL_ERROR; 6599 } 6600 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6601 return TCL_ERROR; 6602 } 6603 zUser = Tcl_GetString(objv[2]); 6604 rc = sqlite3_user_delete(db, zUser); 6605 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6606 return TCL_OK; 6607 } 6608 #endif /* SQLITE_USER_AUTHENTICATION */ 6609 6610 /* 6611 ** tclcmd: bad_behavior TYPE 6612 ** 6613 ** Do some things that should trigger a valgrind or -fsanitize=undefined 6614 ** warning. This is used to verify that errors and warnings output by those 6615 ** tools are detected by the test scripts. 6616 ** 6617 ** TYPE BEHAVIOR 6618 ** 1 Overflow a signed integer 6619 ** 2 Jump based on an uninitialized variable 6620 ** 3 Read after free 6621 ** 4 Panic 6622 */ 6623 static int test_bad_behavior( 6624 ClientData clientData, /* Pointer to an integer containing zero */ 6625 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6626 int objc, /* Number of arguments */ 6627 Tcl_Obj *CONST objv[] /* Command arguments */ 6628 ){ 6629 int iType; 6630 int xyz; 6631 int i = *(int*)clientData; 6632 int j; 6633 int w[10]; 6634 int *a; 6635 if( objc!=2 ){ 6636 Tcl_WrongNumArgs(interp, 1, objv, "TYPE"); 6637 return TCL_ERROR; 6638 } 6639 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR; 6640 switch( iType ){ 6641 case 1: { 6642 xyz = 0x7fffff00 - i; 6643 xyz += 0x100; 6644 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz)); 6645 break; 6646 } 6647 case 2: { 6648 w[1] = 5; 6649 if( w[i]>0 ) w[1]++; 6650 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1])); 6651 break; 6652 } 6653 case 3: { 6654 a = malloc( sizeof(int)*10 ); 6655 for(j=0; j<10; j++) a[j] = j; 6656 free(a); 6657 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i])); 6658 break; 6659 } 6660 case 4: { 6661 Tcl_Panic("Deliberate panic"); 6662 break; 6663 } 6664 } 6665 return TCL_OK; 6666 } 6667 6668 6669 /* 6670 ** Register commands with the TCL interpreter. 6671 */ 6672 int Sqlitetest1_Init(Tcl_Interp *interp){ 6673 extern int sqlite3_search_count; 6674 extern int sqlite3_found_count; 6675 extern int sqlite3_interrupt_count; 6676 extern int sqlite3_open_file_count; 6677 extern int sqlite3_sort_count; 6678 extern int sqlite3_current_time; 6679 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6680 extern int sqlite3_hostid_num; 6681 #endif 6682 extern int sqlite3_max_blobsize; 6683 extern int sqlite3BtreeSharedCacheReport(void*, 6684 Tcl_Interp*,int,Tcl_Obj*CONST*); 6685 static int iZero = 0; 6686 static struct { 6687 char *zName; 6688 Tcl_CmdProc *xProc; 6689 } aCmd[] = { 6690 { "db_enter", (Tcl_CmdProc*)db_enter }, 6691 { "db_leave", (Tcl_CmdProc*)db_leave }, 6692 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, 6693 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, 6694 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, 6695 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, 6696 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, 6697 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, 6698 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, 6699 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, 6700 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, 6701 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, 6702 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, 6703 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, 6704 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, 6705 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, 6706 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, 6707 { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, 6708 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, 6709 #ifndef SQLITE_OMIT_GET_TABLE 6710 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, 6711 #endif 6712 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, 6713 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, 6714 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, 6715 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, 6716 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, 6717 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, 6718 { "sqlite_bind", (Tcl_CmdProc*)test_bind }, 6719 { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, 6720 { "sqlite3_key", (Tcl_CmdProc*)test_key }, 6721 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, 6722 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, 6723 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, 6724 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, 6725 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, 6726 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, 6727 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, 6728 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, 6729 { "printf", (Tcl_CmdProc*)test_printf }, 6730 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, 6731 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address }, 6732 }; 6733 static struct { 6734 char *zName; 6735 Tcl_ObjCmdProc *xProc; 6736 void *clientData; 6737 } aObjCmd[] = { 6738 { "bad_behavior", test_bad_behavior, (void*)&iZero }, 6739 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, 6740 { "sqlite3_bind_int", test_bind_int, 0 }, 6741 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, 6742 { "sqlite3_bind_int64", test_bind_int64, 0 }, 6743 { "sqlite3_bind_double", test_bind_double, 0 }, 6744 { "sqlite3_bind_null", test_bind_null ,0 }, 6745 { "sqlite3_bind_text", test_bind_text ,0 }, 6746 { "sqlite3_bind_text16", test_bind_text16 ,0 }, 6747 { "sqlite3_bind_blob", test_bind_blob ,0 }, 6748 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, 6749 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, 6750 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, 6751 { "sqlite3_clear_bindings", test_clear_bindings, 0}, 6752 { "sqlite3_sleep", test_sleep, 0}, 6753 { "sqlite3_errcode", test_errcode ,0 }, 6754 { "sqlite3_extended_errcode", test_ex_errcode ,0 }, 6755 { "sqlite3_errmsg", test_errmsg ,0 }, 6756 { "sqlite3_errmsg16", test_errmsg16 ,0 }, 6757 { "sqlite3_open", test_open ,0 }, 6758 { "sqlite3_open16", test_open16 ,0 }, 6759 { "sqlite3_open_v2", test_open_v2 ,0 }, 6760 { "sqlite3_complete16", test_complete16 ,0 }, 6761 6762 { "sqlite3_prepare", test_prepare ,0 }, 6763 { "sqlite3_prepare16", test_prepare16 ,0 }, 6764 { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, 6765 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, 6766 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, 6767 { "sqlite3_finalize", test_finalize ,0 }, 6768 { "sqlite3_stmt_status", test_stmt_status ,0 }, 6769 { "sqlite3_reset", test_reset ,0 }, 6770 { "sqlite3_expired", test_expired ,0 }, 6771 { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, 6772 { "sqlite3_changes", test_changes ,0 }, 6773 { "sqlite3_step", test_step ,0 }, 6774 { "sqlite3_sql", test_sql ,0 }, 6775 { "sqlite3_next_stmt", test_next_stmt ,0 }, 6776 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, 6777 { "sqlite3_stmt_busy", test_stmt_busy ,0 }, 6778 { "uses_stmt_journal", uses_stmt_journal ,0 }, 6779 6780 { "sqlite3_release_memory", test_release_memory, 0}, 6781 { "sqlite3_db_release_memory", test_db_release_memory, 0}, 6782 { "sqlite3_db_filename", test_db_filename, 0}, 6783 { "sqlite3_db_readonly", test_db_readonly, 0}, 6784 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, 6785 { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, 6786 { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, 6787 6788 { "sqlite3_load_extension", test_load_extension, 0}, 6789 { "sqlite3_enable_load_extension", test_enable_load, 0}, 6790 { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, 6791 { "sqlite3_limit", test_limit, 0}, 6792 6793 { "save_prng_state", save_prng_state, 0 }, 6794 { "restore_prng_state", restore_prng_state, 0 }, 6795 { "reset_prng_state", reset_prng_state, 0 }, 6796 { "database_never_corrupt", database_never_corrupt, 0}, 6797 { "database_may_be_corrupt", database_may_be_corrupt, 0}, 6798 { "optimization_control", optimization_control,0}, 6799 #if SQLITE_OS_WIN 6800 { "lock_win32_file", win32_file_lock, 0 }, 6801 { "exists_win32_path", win32_exists_path, 0 }, 6802 { "find_win32_file", win32_find_file, 0 }, 6803 { "delete_win32_file", win32_delete_file, 0 }, 6804 { "make_win32_dir", win32_mkdir, 0 }, 6805 { "remove_win32_dir", win32_rmdir, 0 }, 6806 #endif 6807 { "tcl_objproc", runAsObjProc, 0 }, 6808 6809 /* sqlite3_column_*() API */ 6810 { "sqlite3_column_count", test_column_count ,0 }, 6811 { "sqlite3_data_count", test_data_count ,0 }, 6812 { "sqlite3_column_type", test_column_type ,0 }, 6813 { "sqlite3_column_blob", test_column_blob ,0 }, 6814 { "sqlite3_column_double", test_column_double ,0 }, 6815 { "sqlite3_column_int64", test_column_int64 ,0 }, 6816 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, 6817 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, 6818 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, 6819 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, 6820 #ifndef SQLITE_OMIT_DECLTYPE 6821 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, 6822 #endif 6823 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6824 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, 6825 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, 6826 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, 6827 #endif 6828 6829 #ifndef SQLITE_OMIT_UTF16 6830 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, 6831 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, 6832 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, 6833 { "add_alignment_test_collations", add_alignment_test_collations, 0 }, 6834 #ifndef SQLITE_OMIT_DECLTYPE 6835 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, 6836 #endif 6837 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6838 {"sqlite3_column_database_name16", 6839 test_stmt_utf16, (void*)sqlite3_column_database_name16}, 6840 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, 6841 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, 6842 #endif 6843 #endif 6844 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, 6845 { "sqlite3_global_recover", test_global_recover, 0 }, 6846 { "working_64bit_int", working_64bit_int, 0 }, 6847 { "vfs_unlink_test", vfs_unlink_test, 0 }, 6848 { "vfs_initfail_test", vfs_initfail_test, 0 }, 6849 { "vfs_unregister_all", vfs_unregister_all, 0 }, 6850 { "vfs_reregister_all", vfs_reregister_all, 0 }, 6851 { "file_control_test", file_control_test, 0 }, 6852 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, 6853 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, 6854 { "file_control_chunksize_test", file_control_chunksize_test, 0 }, 6855 { "file_control_sizehint_test", file_control_sizehint_test, 0 }, 6856 #if SQLITE_OS_WIN 6857 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, 6858 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 }, 6859 #endif 6860 { "file_control_persist_wal", file_control_persist_wal, 0 }, 6861 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, 6862 { "file_control_vfsname", file_control_vfsname, 0 }, 6863 { "file_control_tempfilename", file_control_tempfilename, 0 }, 6864 { "sqlite3_vfs_list", vfs_list, 0 }, 6865 { "sqlite3_create_function_v2", test_create_function_v2, 0 }, 6866 6867 /* Functions from os.h */ 6868 #ifndef SQLITE_OMIT_UTF16 6869 { "add_test_collate", test_collate, 0 }, 6870 { "add_test_collate_needed", test_collate_needed, 0 }, 6871 { "add_test_function", test_function, 0 }, 6872 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 }, 6873 #endif 6874 { "sqlite3_test_errstr", test_errstr, 0 }, 6875 { "tcl_variable_type", tcl_variable_type, 0 }, 6876 #ifndef SQLITE_OMIT_SHARED_CACHE 6877 { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, 6878 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, 6879 #endif 6880 { "sqlite3_libversion_number", test_libversion_number, 0 }, 6881 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, 6882 #ifndef SQLITE_OMIT_INCRBLOB 6883 { "sqlite3_blob_reopen", test_blob_reopen, 0 }, 6884 #endif 6885 { "pcache_stats", test_pcache_stats, 0 }, 6886 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 6887 { "sqlite3_unlock_notify", test_unlock_notify, 0 }, 6888 #endif 6889 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, 6890 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, 6891 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 }, 6892 { "test_sqlite3_log", test_sqlite3_log, 0 }, 6893 #ifndef SQLITE_OMIT_EXPLAIN 6894 { "print_explain_query_plan", test_print_eqp, 0 }, 6895 #endif 6896 { "sqlite3_test_control", test_test_control }, 6897 #if SQLITE_OS_UNIX 6898 { "getrusage", test_getrusage }, 6899 #endif 6900 { "load_static_extension", tclLoadStaticExtensionCmd }, 6901 { "sorter_test_fakeheap", sorter_test_fakeheap }, 6902 { "sorter_test_sort4_helper", sorter_test_sort4_helper }, 6903 #ifdef SQLITE_USER_AUTHENTICATION 6904 { "sqlite3_user_authenticate", test_user_authenticate, 0 }, 6905 { "sqlite3_user_add", test_user_add, 0 }, 6906 { "sqlite3_user_change", test_user_change, 0 }, 6907 { "sqlite3_user_delete", test_user_delete, 0 }, 6908 #endif 6909 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 6910 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 }, 6911 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 }, 6912 #endif 6913 6914 }; 6915 static int bitmask_size = sizeof(Bitmask)*8; 6916 int i; 6917 extern int sqlite3_sync_count, sqlite3_fullsync_count; 6918 extern int sqlite3_opentemp_count; 6919 extern int sqlite3_like_count; 6920 extern int sqlite3_xferopt_count; 6921 extern int sqlite3_pager_readdb_count; 6922 extern int sqlite3_pager_writedb_count; 6923 extern int sqlite3_pager_writej_count; 6924 #if SQLITE_OS_WIN 6925 extern LONG volatile sqlite3_os_type; 6926 #endif 6927 #ifdef SQLITE_DEBUG 6928 extern int sqlite3WhereTrace; 6929 extern int sqlite3OSTrace; 6930 extern int sqlite3WalTrace; 6931 #endif 6932 #ifdef SQLITE_TEST 6933 #ifdef SQLITE_ENABLE_FTS3 6934 extern int sqlite3_fts3_enable_parentheses; 6935 #endif 6936 #endif 6937 6938 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 6939 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 6940 } 6941 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 6942 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 6943 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 6944 } 6945 Tcl_LinkVar(interp, "sqlite_search_count", 6946 (char*)&sqlite3_search_count, TCL_LINK_INT); 6947 Tcl_LinkVar(interp, "sqlite_found_count", 6948 (char*)&sqlite3_found_count, TCL_LINK_INT); 6949 Tcl_LinkVar(interp, "sqlite_sort_count", 6950 (char*)&sqlite3_sort_count, TCL_LINK_INT); 6951 Tcl_LinkVar(interp, "sqlite3_max_blobsize", 6952 (char*)&sqlite3_max_blobsize, TCL_LINK_INT); 6953 Tcl_LinkVar(interp, "sqlite_like_count", 6954 (char*)&sqlite3_like_count, TCL_LINK_INT); 6955 Tcl_LinkVar(interp, "sqlite_interrupt_count", 6956 (char*)&sqlite3_interrupt_count, TCL_LINK_INT); 6957 Tcl_LinkVar(interp, "sqlite_open_file_count", 6958 (char*)&sqlite3_open_file_count, TCL_LINK_INT); 6959 Tcl_LinkVar(interp, "sqlite_current_time", 6960 (char*)&sqlite3_current_time, TCL_LINK_INT); 6961 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6962 Tcl_LinkVar(interp, "sqlite_hostid_num", 6963 (char*)&sqlite3_hostid_num, TCL_LINK_INT); 6964 #endif 6965 Tcl_LinkVar(interp, "sqlite3_xferopt_count", 6966 (char*)&sqlite3_xferopt_count, TCL_LINK_INT); 6967 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", 6968 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); 6969 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", 6970 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); 6971 Tcl_LinkVar(interp, "sqlite3_pager_writej_count", 6972 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); 6973 #ifndef SQLITE_OMIT_UTF16 6974 Tcl_LinkVar(interp, "unaligned_string_counter", 6975 (char*)&unaligned_string_counter, TCL_LINK_INT); 6976 #endif 6977 #ifndef SQLITE_OMIT_UTF16 6978 Tcl_LinkVar(interp, "sqlite_last_needed_collation", 6979 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 6980 #endif 6981 #if SQLITE_OS_WIN 6982 Tcl_LinkVar(interp, "sqlite_os_type", 6983 (char*)&sqlite3_os_type, TCL_LINK_LONG); 6984 #endif 6985 #ifdef SQLITE_TEST 6986 { 6987 static const char *query_plan = "*** OBSOLETE VARIABLE ***"; 6988 Tcl_LinkVar(interp, "sqlite_query_plan", 6989 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 6990 } 6991 #endif 6992 #ifdef SQLITE_DEBUG 6993 Tcl_LinkVar(interp, "sqlite_where_trace", 6994 (char*)&sqlite3WhereTrace, TCL_LINK_INT); 6995 Tcl_LinkVar(interp, "sqlite_os_trace", 6996 (char*)&sqlite3OSTrace, TCL_LINK_INT); 6997 #ifndef SQLITE_OMIT_WAL 6998 Tcl_LinkVar(interp, "sqlite_wal_trace", 6999 (char*)&sqlite3WalTrace, TCL_LINK_INT); 7000 #endif 7001 #endif 7002 #ifndef SQLITE_OMIT_DISKIO 7003 Tcl_LinkVar(interp, "sqlite_opentemp_count", 7004 (char*)&sqlite3_opentemp_count, TCL_LINK_INT); 7005 #endif 7006 Tcl_LinkVar(interp, "sqlite_static_bind_value", 7007 (char*)&sqlite_static_bind_value, TCL_LINK_STRING); 7008 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", 7009 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); 7010 Tcl_LinkVar(interp, "sqlite_temp_directory", 7011 (char*)&sqlite3_temp_directory, TCL_LINK_STRING); 7012 Tcl_LinkVar(interp, "sqlite_data_directory", 7013 (char*)&sqlite3_data_directory, TCL_LINK_STRING); 7014 Tcl_LinkVar(interp, "bitmask_size", 7015 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); 7016 Tcl_LinkVar(interp, "sqlite_sync_count", 7017 (char*)&sqlite3_sync_count, TCL_LINK_INT); 7018 Tcl_LinkVar(interp, "sqlite_fullsync_count", 7019 (char*)&sqlite3_fullsync_count, TCL_LINK_INT); 7020 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) 7021 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", 7022 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); 7023 #endif 7024 return TCL_OK; 7025 } 7026