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