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[501]; 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)-1) && 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_zeroblob64 STMT IDX N 3013 ** 3014 ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement. 3015 ** IDX is the index of a wildcard in the prepared statement. This command 3016 ** binds a N-byte zero-filled BLOB to the wildcard. 3017 */ 3018 static int test_bind_zeroblob64( 3019 void * clientData, 3020 Tcl_Interp *interp, 3021 int objc, 3022 Tcl_Obj *CONST objv[] 3023 ){ 3024 sqlite3_stmt *pStmt; 3025 int idx; 3026 i64 n; 3027 int rc; 3028 3029 if( objc!=4 ){ 3030 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); 3031 return TCL_ERROR; 3032 } 3033 3034 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3035 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3036 if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; 3037 3038 rc = sqlite3_bind_zeroblob64(pStmt, idx, n); 3039 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3040 if( rc!=SQLITE_OK ){ 3041 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3042 return TCL_ERROR; 3043 } 3044 3045 return TCL_OK; 3046 } 3047 3048 /* 3049 ** Usage: sqlite3_bind_int STMT N VALUE 3050 ** 3051 ** Test the sqlite3_bind_int interface. STMT is a prepared statement. 3052 ** N is the index of a wildcard in the prepared statement. This command 3053 ** binds a 32-bit integer VALUE to that wildcard. 3054 */ 3055 static int test_bind_int( 3056 void * clientData, 3057 Tcl_Interp *interp, 3058 int objc, 3059 Tcl_Obj *CONST objv[] 3060 ){ 3061 sqlite3_stmt *pStmt; 3062 int idx; 3063 int value; 3064 int rc; 3065 3066 if( objc!=4 ){ 3067 Tcl_AppendResult(interp, "wrong # args: should be \"", 3068 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3069 return TCL_ERROR; 3070 } 3071 3072 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3073 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3074 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3075 3076 rc = sqlite3_bind_int(pStmt, idx, value); 3077 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3078 if( rc!=SQLITE_OK ){ 3079 return TCL_ERROR; 3080 } 3081 3082 return TCL_OK; 3083 } 3084 3085 3086 /* 3087 ** Usage: sqlite3_bind_int64 STMT N VALUE 3088 ** 3089 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. 3090 ** N is the index of a wildcard in the prepared statement. This command 3091 ** binds a 64-bit integer VALUE to that wildcard. 3092 */ 3093 static int test_bind_int64( 3094 void * clientData, 3095 Tcl_Interp *interp, 3096 int objc, 3097 Tcl_Obj *CONST objv[] 3098 ){ 3099 sqlite3_stmt *pStmt; 3100 int idx; 3101 Tcl_WideInt value; 3102 int rc; 3103 3104 if( objc!=4 ){ 3105 Tcl_AppendResult(interp, "wrong # args: should be \"", 3106 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3107 return TCL_ERROR; 3108 } 3109 3110 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3111 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3112 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3113 3114 rc = sqlite3_bind_int64(pStmt, idx, value); 3115 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3116 if( rc!=SQLITE_OK ){ 3117 return TCL_ERROR; 3118 } 3119 3120 return TCL_OK; 3121 } 3122 3123 3124 /* 3125 ** Usage: sqlite3_bind_double STMT N VALUE 3126 ** 3127 ** Test the sqlite3_bind_double interface. STMT is a prepared statement. 3128 ** N is the index of a wildcard in the prepared statement. This command 3129 ** binds a 64-bit integer VALUE to that wildcard. 3130 */ 3131 static int test_bind_double( 3132 void * clientData, 3133 Tcl_Interp *interp, 3134 int objc, 3135 Tcl_Obj *CONST objv[] 3136 ){ 3137 sqlite3_stmt *pStmt; 3138 int idx; 3139 double value = 0; 3140 int rc; 3141 const char *zVal; 3142 int i; 3143 static const struct { 3144 const char *zName; /* Name of the special floating point value */ 3145 unsigned int iUpper; /* Upper 32 bits */ 3146 unsigned int iLower; /* Lower 32 bits */ 3147 } aSpecialFp[] = { 3148 { "NaN", 0x7fffffff, 0xffffffff }, 3149 { "SNaN", 0x7ff7ffff, 0xffffffff }, 3150 { "-NaN", 0xffffffff, 0xffffffff }, 3151 { "-SNaN", 0xfff7ffff, 0xffffffff }, 3152 { "+Inf", 0x7ff00000, 0x00000000 }, 3153 { "-Inf", 0xfff00000, 0x00000000 }, 3154 { "Epsilon", 0x00000000, 0x00000001 }, 3155 { "-Epsilon", 0x80000000, 0x00000001 }, 3156 { "NaN0", 0x7ff80000, 0x00000000 }, 3157 { "-NaN0", 0xfff80000, 0x00000000 }, 3158 }; 3159 3160 if( objc!=4 ){ 3161 Tcl_AppendResult(interp, "wrong # args: should be \"", 3162 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3163 return TCL_ERROR; 3164 } 3165 3166 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3167 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3168 3169 /* Intercept the string "NaN" and generate a NaN value for it. 3170 ** All other strings are passed through to Tcl_GetDoubleFromObj(). 3171 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions 3172 ** contain a bug. 3173 */ 3174 zVal = Tcl_GetString(objv[3]); 3175 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ 3176 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ 3177 sqlite3_uint64 x; 3178 x = aSpecialFp[i].iUpper; 3179 x <<= 32; 3180 x |= aSpecialFp[i].iLower; 3181 assert( sizeof(value)==8 ); 3182 assert( sizeof(x)==8 ); 3183 memcpy(&value, &x, 8); 3184 break; 3185 } 3186 } 3187 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && 3188 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ 3189 return TCL_ERROR; 3190 } 3191 rc = sqlite3_bind_double(pStmt, idx, value); 3192 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3193 if( rc!=SQLITE_OK ){ 3194 return TCL_ERROR; 3195 } 3196 3197 return TCL_OK; 3198 } 3199 3200 /* 3201 ** Usage: sqlite3_bind_null STMT N 3202 ** 3203 ** Test the sqlite3_bind_null interface. STMT is a prepared statement. 3204 ** N is the index of a wildcard in the prepared statement. This command 3205 ** binds a NULL to the wildcard. 3206 */ 3207 static int test_bind_null( 3208 void * clientData, 3209 Tcl_Interp *interp, 3210 int objc, 3211 Tcl_Obj *CONST objv[] 3212 ){ 3213 sqlite3_stmt *pStmt; 3214 int idx; 3215 int rc; 3216 3217 if( objc!=3 ){ 3218 Tcl_AppendResult(interp, "wrong # args: should be \"", 3219 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); 3220 return TCL_ERROR; 3221 } 3222 3223 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3224 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3225 3226 rc = sqlite3_bind_null(pStmt, idx); 3227 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3228 if( rc!=SQLITE_OK ){ 3229 return TCL_ERROR; 3230 } 3231 3232 return TCL_OK; 3233 } 3234 3235 /* 3236 ** Usage: sqlite3_bind_text STMT N STRING BYTES 3237 ** 3238 ** Test the sqlite3_bind_text interface. STMT is a prepared statement. 3239 ** N is the index of a wildcard in the prepared statement. This command 3240 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes 3241 ** long. 3242 */ 3243 static int test_bind_text( 3244 void * clientData, 3245 Tcl_Interp *interp, 3246 int objc, 3247 Tcl_Obj *CONST objv[] 3248 ){ 3249 sqlite3_stmt *pStmt; 3250 int idx; 3251 int bytes; 3252 char *value; 3253 int rc; 3254 3255 if( objc!=5 ){ 3256 Tcl_AppendResult(interp, "wrong # args: should be \"", 3257 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3258 return TCL_ERROR; 3259 } 3260 3261 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3262 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3263 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); 3264 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3265 3266 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); 3267 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3268 if( rc!=SQLITE_OK ){ 3269 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3270 return TCL_ERROR; 3271 } 3272 3273 return TCL_OK; 3274 } 3275 3276 /* 3277 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES 3278 ** 3279 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. 3280 ** N is the index of a wildcard in the prepared statement. This command 3281 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes 3282 ** long. 3283 */ 3284 static int test_bind_text16( 3285 void * clientData, 3286 Tcl_Interp *interp, 3287 int objc, 3288 Tcl_Obj *CONST objv[] 3289 ){ 3290 #ifndef SQLITE_OMIT_UTF16 3291 sqlite3_stmt *pStmt; 3292 int idx; 3293 int bytes; 3294 char *value; 3295 int rc; 3296 3297 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); 3298 Tcl_Obj *oStmt = objv[objc-4]; 3299 Tcl_Obj *oN = objv[objc-3]; 3300 Tcl_Obj *oString = objv[objc-2]; 3301 Tcl_Obj *oBytes = objv[objc-1]; 3302 3303 if( objc!=5 && objc!=6){ 3304 Tcl_AppendResult(interp, "wrong # args: should be \"", 3305 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3306 return TCL_ERROR; 3307 } 3308 3309 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; 3310 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; 3311 value = (char*)Tcl_GetByteArrayFromObj(oString, 0); 3312 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; 3313 3314 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); 3315 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3316 if( rc!=SQLITE_OK ){ 3317 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3318 return TCL_ERROR; 3319 } 3320 3321 #endif /* SQLITE_OMIT_UTF16 */ 3322 return TCL_OK; 3323 } 3324 3325 /* 3326 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES 3327 ** 3328 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. 3329 ** N is the index of a wildcard in the prepared statement. This command 3330 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. 3331 */ 3332 static int test_bind_blob( 3333 void * clientData, 3334 Tcl_Interp *interp, 3335 int objc, 3336 Tcl_Obj *CONST objv[] 3337 ){ 3338 sqlite3_stmt *pStmt; 3339 int idx; 3340 int bytes; 3341 char *value; 3342 int rc; 3343 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; 3344 3345 if( objc!=5 && objc!=6 ){ 3346 Tcl_AppendResult(interp, "wrong # args: should be \"", 3347 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); 3348 return TCL_ERROR; 3349 } 3350 3351 if( objc==6 ){ 3352 xDestructor = SQLITE_STATIC; 3353 objv++; 3354 } 3355 3356 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3357 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3358 value = Tcl_GetString(objv[3]); 3359 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3360 3361 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); 3362 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3363 if( rc!=SQLITE_OK ){ 3364 return TCL_ERROR; 3365 } 3366 3367 return TCL_OK; 3368 } 3369 3370 /* 3371 ** Usage: sqlite3_bind_parameter_count STMT 3372 ** 3373 ** Return the number of wildcards in the given statement. 3374 */ 3375 static int test_bind_parameter_count( 3376 void * clientData, 3377 Tcl_Interp *interp, 3378 int objc, 3379 Tcl_Obj *CONST objv[] 3380 ){ 3381 sqlite3_stmt *pStmt; 3382 3383 if( objc!=2 ){ 3384 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3385 return TCL_ERROR; 3386 } 3387 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3388 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); 3389 return TCL_OK; 3390 } 3391 3392 /* 3393 ** Usage: sqlite3_bind_parameter_name STMT N 3394 ** 3395 ** Return the name of the Nth wildcard. The first wildcard is 1. 3396 ** An empty string is returned if N is out of range or if the wildcard 3397 ** is nameless. 3398 */ 3399 static int test_bind_parameter_name( 3400 void * clientData, 3401 Tcl_Interp *interp, 3402 int objc, 3403 Tcl_Obj *CONST objv[] 3404 ){ 3405 sqlite3_stmt *pStmt; 3406 int i; 3407 3408 if( objc!=3 ){ 3409 Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); 3410 return TCL_ERROR; 3411 } 3412 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3413 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; 3414 Tcl_SetObjResult(interp, 3415 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) 3416 ); 3417 return TCL_OK; 3418 } 3419 3420 /* 3421 ** Usage: sqlite3_bind_parameter_index STMT NAME 3422 ** 3423 ** Return the index of the wildcard called NAME. Return 0 if there is 3424 ** no such wildcard. 3425 */ 3426 static int test_bind_parameter_index( 3427 void * clientData, 3428 Tcl_Interp *interp, 3429 int objc, 3430 Tcl_Obj *CONST objv[] 3431 ){ 3432 sqlite3_stmt *pStmt; 3433 3434 if( objc!=3 ){ 3435 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); 3436 return TCL_ERROR; 3437 } 3438 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3439 Tcl_SetObjResult(interp, 3440 Tcl_NewIntObj( 3441 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) 3442 ) 3443 ); 3444 return TCL_OK; 3445 } 3446 3447 /* 3448 ** Usage: sqlite3_clear_bindings STMT 3449 ** 3450 */ 3451 static int test_clear_bindings( 3452 void * clientData, 3453 Tcl_Interp *interp, 3454 int objc, 3455 Tcl_Obj *CONST objv[] 3456 ){ 3457 sqlite3_stmt *pStmt; 3458 3459 if( objc!=2 ){ 3460 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3461 return TCL_ERROR; 3462 } 3463 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3464 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); 3465 return TCL_OK; 3466 } 3467 3468 /* 3469 ** Usage: sqlite3_sleep MILLISECONDS 3470 */ 3471 static int test_sleep( 3472 void * clientData, 3473 Tcl_Interp *interp, 3474 int objc, 3475 Tcl_Obj *CONST objv[] 3476 ){ 3477 int ms; 3478 3479 if( objc!=2 ){ 3480 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); 3481 return TCL_ERROR; 3482 } 3483 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ 3484 return TCL_ERROR; 3485 } 3486 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); 3487 return TCL_OK; 3488 } 3489 3490 /* 3491 ** Usage: sqlite3_extended_errcode DB 3492 ** 3493 ** Return the string representation of the most recent sqlite3_* API 3494 ** error code. e.g. "SQLITE_ERROR". 3495 */ 3496 static int test_ex_errcode( 3497 void * clientData, 3498 Tcl_Interp *interp, 3499 int objc, 3500 Tcl_Obj *CONST objv[] 3501 ){ 3502 sqlite3 *db; 3503 int rc; 3504 3505 if( objc!=2 ){ 3506 Tcl_AppendResult(interp, "wrong # args: should be \"", 3507 Tcl_GetString(objv[0]), " DB", 0); 3508 return TCL_ERROR; 3509 } 3510 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3511 rc = sqlite3_extended_errcode(db); 3512 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3513 return TCL_OK; 3514 } 3515 3516 3517 /* 3518 ** Usage: sqlite3_errcode DB 3519 ** 3520 ** Return the string representation of the most recent sqlite3_* API 3521 ** error code. e.g. "SQLITE_ERROR". 3522 */ 3523 static int test_errcode( 3524 void * clientData, 3525 Tcl_Interp *interp, 3526 int objc, 3527 Tcl_Obj *CONST objv[] 3528 ){ 3529 sqlite3 *db; 3530 int rc; 3531 3532 if( objc!=2 ){ 3533 Tcl_AppendResult(interp, "wrong # args: should be \"", 3534 Tcl_GetString(objv[0]), " DB", 0); 3535 return TCL_ERROR; 3536 } 3537 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3538 rc = sqlite3_errcode(db); 3539 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3540 return TCL_OK; 3541 } 3542 3543 /* 3544 ** Usage: sqlite3_errmsg DB 3545 ** 3546 ** Returns the UTF-8 representation of the error message string for the 3547 ** most recent sqlite3_* API call. 3548 */ 3549 static int test_errmsg( 3550 void * clientData, 3551 Tcl_Interp *interp, 3552 int objc, 3553 Tcl_Obj *CONST objv[] 3554 ){ 3555 sqlite3 *db; 3556 const char *zErr; 3557 3558 if( objc!=2 ){ 3559 Tcl_AppendResult(interp, "wrong # args: should be \"", 3560 Tcl_GetString(objv[0]), " DB", 0); 3561 return TCL_ERROR; 3562 } 3563 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3564 3565 zErr = sqlite3_errmsg(db); 3566 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 3567 return TCL_OK; 3568 } 3569 3570 /* 3571 ** Usage: test_errmsg16 DB 3572 ** 3573 ** Returns the UTF-16 representation of the error message string for the 3574 ** most recent sqlite3_* API call. This is a byte array object at the TCL 3575 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the 3576 ** UTF-16 string. 3577 */ 3578 static int test_errmsg16( 3579 void * clientData, 3580 Tcl_Interp *interp, 3581 int objc, 3582 Tcl_Obj *CONST objv[] 3583 ){ 3584 #ifndef SQLITE_OMIT_UTF16 3585 sqlite3 *db; 3586 const void *zErr; 3587 const char *z; 3588 int bytes = 0; 3589 3590 if( objc!=2 ){ 3591 Tcl_AppendResult(interp, "wrong # args: should be \"", 3592 Tcl_GetString(objv[0]), " DB", 0); 3593 return TCL_ERROR; 3594 } 3595 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3596 3597 zErr = sqlite3_errmsg16(db); 3598 if( zErr ){ 3599 z = zErr; 3600 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} 3601 } 3602 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); 3603 #endif /* SQLITE_OMIT_UTF16 */ 3604 return TCL_OK; 3605 } 3606 3607 /* 3608 ** Usage: sqlite3_prepare DB sql bytes ?tailvar? 3609 ** 3610 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3611 ** database handle <DB>. The parameter <tailval> is the name of a global 3612 ** variable that is set to the unused portion of <sql> (if any). A 3613 ** STMT handle is returned. 3614 */ 3615 static int test_prepare( 3616 void * clientData, 3617 Tcl_Interp *interp, 3618 int objc, 3619 Tcl_Obj *CONST objv[] 3620 ){ 3621 sqlite3 *db; 3622 const char *zSql; 3623 int bytes; 3624 const char *zTail = 0; 3625 sqlite3_stmt *pStmt = 0; 3626 char zBuf[50]; 3627 int rc; 3628 3629 if( objc!=5 && objc!=4 ){ 3630 Tcl_AppendResult(interp, "wrong # args: should be \"", 3631 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3632 return TCL_ERROR; 3633 } 3634 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3635 zSql = Tcl_GetString(objv[2]); 3636 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3637 3638 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3639 Tcl_ResetResult(interp); 3640 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3641 if( zTail && objc>=5 ){ 3642 if( bytes>=0 ){ 3643 bytes = bytes - (int)(zTail-zSql); 3644 } 3645 if( (int)strlen(zTail)<bytes ){ 3646 bytes = (int)strlen(zTail); 3647 } 3648 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3649 } 3650 if( rc!=SQLITE_OK ){ 3651 assert( pStmt==0 ); 3652 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 3653 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3654 return TCL_ERROR; 3655 } 3656 3657 if( pStmt ){ 3658 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3659 Tcl_AppendResult(interp, zBuf, 0); 3660 } 3661 return TCL_OK; 3662 } 3663 3664 /* 3665 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? 3666 ** 3667 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3668 ** database handle <DB>. The parameter <tailval> is the name of a global 3669 ** variable that is set to the unused portion of <sql> (if any). A 3670 ** STMT handle is returned. 3671 */ 3672 static int test_prepare_v2( 3673 void * clientData, 3674 Tcl_Interp *interp, 3675 int objc, 3676 Tcl_Obj *CONST objv[] 3677 ){ 3678 sqlite3 *db; 3679 const char *zSql; 3680 char *zCopy = 0; /* malloc() copy of zSql */ 3681 int bytes; 3682 const char *zTail = 0; 3683 sqlite3_stmt *pStmt = 0; 3684 char zBuf[50]; 3685 int rc; 3686 3687 if( objc!=5 && objc!=4 ){ 3688 Tcl_AppendResult(interp, "wrong # args: should be \"", 3689 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3690 return TCL_ERROR; 3691 } 3692 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3693 zSql = Tcl_GetString(objv[2]); 3694 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3695 3696 /* Instead of using zSql directly, make a copy into a buffer obtained 3697 ** directly from malloc(). The idea is to make it easier for valgrind 3698 ** to spot buffer overreads. */ 3699 if( bytes>=0 ){ 3700 zCopy = malloc(bytes); 3701 memcpy(zCopy, zSql, bytes); 3702 }else{ 3703 int n = (int)strlen(zSql) + 1; 3704 zCopy = malloc(n); 3705 memcpy(zCopy, zSql, n); 3706 } 3707 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0); 3708 free(zCopy); 3709 zTail = &zSql[(zTail - zCopy)]; 3710 3711 assert(rc==SQLITE_OK || pStmt==0); 3712 Tcl_ResetResult(interp); 3713 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3714 if( rc==SQLITE_OK && zTail && objc>=5 ){ 3715 if( bytes>=0 ){ 3716 bytes = bytes - (int)(zTail-zSql); 3717 } 3718 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3719 } 3720 if( rc!=SQLITE_OK ){ 3721 assert( pStmt==0 ); 3722 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 3723 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3724 return TCL_ERROR; 3725 } 3726 3727 if( pStmt ){ 3728 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3729 Tcl_AppendResult(interp, zBuf, 0); 3730 } 3731 return TCL_OK; 3732 } 3733 3734 /* 3735 ** Usage: sqlite3_prepare_tkt3134 DB 3736 ** 3737 ** Generate a prepared statement for a zero-byte string as a test 3738 ** for ticket #3134. The string should be preceded by a zero byte. 3739 */ 3740 static int test_prepare_tkt3134( 3741 void * clientData, 3742 Tcl_Interp *interp, 3743 int objc, 3744 Tcl_Obj *CONST objv[] 3745 ){ 3746 sqlite3 *db; 3747 static const char zSql[] = "\000SELECT 1"; 3748 sqlite3_stmt *pStmt = 0; 3749 char zBuf[50]; 3750 int rc; 3751 3752 if( objc!=2 ){ 3753 Tcl_AppendResult(interp, "wrong # args: should be \"", 3754 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3755 return TCL_ERROR; 3756 } 3757 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3758 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); 3759 assert(rc==SQLITE_OK || pStmt==0); 3760 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3761 if( rc!=SQLITE_OK ){ 3762 assert( pStmt==0 ); 3763 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 3764 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3765 return TCL_ERROR; 3766 } 3767 3768 if( pStmt ){ 3769 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3770 Tcl_AppendResult(interp, zBuf, 0); 3771 } 3772 return TCL_OK; 3773 } 3774 3775 /* 3776 ** Usage: sqlite3_prepare16 DB sql bytes tailvar 3777 ** 3778 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3779 ** database handle <DB>. The parameter <tailval> is the name of a global 3780 ** variable that is set to the unused portion of <sql> (if any). A 3781 ** STMT handle is returned. 3782 */ 3783 static int test_prepare16( 3784 void * clientData, 3785 Tcl_Interp *interp, 3786 int objc, 3787 Tcl_Obj *CONST objv[] 3788 ){ 3789 #ifndef SQLITE_OMIT_UTF16 3790 sqlite3 *db; 3791 const void *zSql; 3792 const void *zTail = 0; 3793 Tcl_Obj *pTail = 0; 3794 sqlite3_stmt *pStmt = 0; 3795 char zBuf[50]; 3796 int rc; 3797 int bytes; /* The integer specified as arg 3 */ 3798 int objlen; /* The byte-array length of arg 2 */ 3799 3800 if( objc!=5 && objc!=4 ){ 3801 Tcl_AppendResult(interp, "wrong # args: should be \"", 3802 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3803 return TCL_ERROR; 3804 } 3805 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3806 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3807 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3808 3809 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3810 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3811 if( rc ){ 3812 return TCL_ERROR; 3813 } 3814 3815 if( objc>=5 ){ 3816 if( zTail ){ 3817 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3818 }else{ 3819 objlen = 0; 3820 } 3821 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3822 Tcl_IncrRefCount(pTail); 3823 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3824 Tcl_DecrRefCount(pTail); 3825 } 3826 3827 if( pStmt ){ 3828 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3829 } 3830 Tcl_AppendResult(interp, zBuf, 0); 3831 #endif /* SQLITE_OMIT_UTF16 */ 3832 return TCL_OK; 3833 } 3834 3835 /* 3836 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? 3837 ** 3838 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3839 ** database handle <DB>. The parameter <tailval> is the name of a global 3840 ** variable that is set to the unused portion of <sql> (if any). A 3841 ** STMT handle is returned. 3842 */ 3843 static int test_prepare16_v2( 3844 void * clientData, 3845 Tcl_Interp *interp, 3846 int objc, 3847 Tcl_Obj *CONST objv[] 3848 ){ 3849 #ifndef SQLITE_OMIT_UTF16 3850 sqlite3 *db; 3851 const void *zSql; 3852 const void *zTail = 0; 3853 Tcl_Obj *pTail = 0; 3854 sqlite3_stmt *pStmt = 0; 3855 char zBuf[50]; 3856 int rc; 3857 int bytes; /* The integer specified as arg 3 */ 3858 int objlen; /* The byte-array length of arg 2 */ 3859 3860 if( objc!=5 && objc!=4 ){ 3861 Tcl_AppendResult(interp, "wrong # args: should be \"", 3862 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3863 return TCL_ERROR; 3864 } 3865 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3866 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3867 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3868 3869 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3870 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3871 if( rc ){ 3872 return TCL_ERROR; 3873 } 3874 3875 if( objc>=5 ){ 3876 if( zTail ){ 3877 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3878 }else{ 3879 objlen = 0; 3880 } 3881 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3882 Tcl_IncrRefCount(pTail); 3883 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3884 Tcl_DecrRefCount(pTail); 3885 } 3886 3887 if( pStmt ){ 3888 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3889 } 3890 Tcl_AppendResult(interp, zBuf, 0); 3891 #endif /* SQLITE_OMIT_UTF16 */ 3892 return TCL_OK; 3893 } 3894 3895 /* 3896 ** Usage: sqlite3_open filename ?options-list? 3897 */ 3898 static int test_open( 3899 void * clientData, 3900 Tcl_Interp *interp, 3901 int objc, 3902 Tcl_Obj *CONST objv[] 3903 ){ 3904 const char *zFilename; 3905 sqlite3 *db; 3906 char zBuf[100]; 3907 3908 if( objc!=3 && objc!=2 && objc!=1 ){ 3909 Tcl_AppendResult(interp, "wrong # args: should be \"", 3910 Tcl_GetString(objv[0]), " filename options-list", 0); 3911 return TCL_ERROR; 3912 } 3913 3914 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; 3915 sqlite3_open(zFilename, &db); 3916 3917 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3918 Tcl_AppendResult(interp, zBuf, 0); 3919 return TCL_OK; 3920 } 3921 3922 /* 3923 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS 3924 */ 3925 static int test_open_v2( 3926 void * clientData, 3927 Tcl_Interp *interp, 3928 int objc, 3929 Tcl_Obj *CONST objv[] 3930 ){ 3931 const char *zFilename; 3932 const char *zVfs; 3933 int flags = 0; 3934 sqlite3 *db; 3935 int rc; 3936 char zBuf[100]; 3937 3938 int nFlag; 3939 Tcl_Obj **apFlag; 3940 int i; 3941 3942 if( objc!=4 ){ 3943 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); 3944 return TCL_ERROR; 3945 } 3946 zFilename = Tcl_GetString(objv[1]); 3947 zVfs = Tcl_GetString(objv[3]); 3948 if( zVfs[0]==0x00 ) zVfs = 0; 3949 3950 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); 3951 if( rc!=TCL_OK ) return rc; 3952 for(i=0; i<nFlag; i++){ 3953 int iFlag; 3954 struct OpenFlag { 3955 const char *zFlag; 3956 int flag; 3957 } aFlag[] = { 3958 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, 3959 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, 3960 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, 3961 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, 3962 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, 3963 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, 3964 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, 3965 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, 3966 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, 3967 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, 3968 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, 3969 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, 3970 { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, 3971 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, 3972 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, 3973 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, 3974 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, 3975 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, 3976 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, 3977 { 0, 0 } 3978 }; 3979 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), 3980 "flag", 0, &iFlag 3981 ); 3982 if( rc!=TCL_OK ) return rc; 3983 flags |= aFlag[iFlag].flag; 3984 } 3985 3986 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); 3987 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3988 Tcl_AppendResult(interp, zBuf, 0); 3989 return TCL_OK; 3990 } 3991 3992 /* 3993 ** Usage: sqlite3_open16 filename options 3994 */ 3995 static int test_open16( 3996 void * clientData, 3997 Tcl_Interp *interp, 3998 int objc, 3999 Tcl_Obj *CONST objv[] 4000 ){ 4001 #ifndef SQLITE_OMIT_UTF16 4002 const void *zFilename; 4003 sqlite3 *db; 4004 char zBuf[100]; 4005 4006 if( objc!=3 ){ 4007 Tcl_AppendResult(interp, "wrong # args: should be \"", 4008 Tcl_GetString(objv[0]), " filename options-list", 0); 4009 return TCL_ERROR; 4010 } 4011 4012 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); 4013 sqlite3_open16(zFilename, &db); 4014 4015 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 4016 Tcl_AppendResult(interp, zBuf, 0); 4017 #endif /* SQLITE_OMIT_UTF16 */ 4018 return TCL_OK; 4019 } 4020 4021 /* 4022 ** Usage: sqlite3_complete16 <UTF-16 string> 4023 ** 4024 ** Return 1 if the supplied argument is a complete SQL statement, or zero 4025 ** otherwise. 4026 */ 4027 static int test_complete16( 4028 void * clientData, 4029 Tcl_Interp *interp, 4030 int objc, 4031 Tcl_Obj *CONST objv[] 4032 ){ 4033 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) 4034 char *zBuf; 4035 4036 if( objc!=2 ){ 4037 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); 4038 return TCL_ERROR; 4039 } 4040 4041 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); 4042 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); 4043 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ 4044 return TCL_OK; 4045 } 4046 4047 /* 4048 ** Usage: sqlite3_step STMT 4049 ** 4050 ** Advance the statement to the next row. 4051 */ 4052 static int test_step( 4053 void * clientData, 4054 Tcl_Interp *interp, 4055 int objc, 4056 Tcl_Obj *CONST objv[] 4057 ){ 4058 sqlite3_stmt *pStmt; 4059 int rc; 4060 4061 if( objc!=2 ){ 4062 Tcl_AppendResult(interp, "wrong # args: should be \"", 4063 Tcl_GetString(objv[0]), " STMT", 0); 4064 return TCL_ERROR; 4065 } 4066 4067 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4068 rc = sqlite3_step(pStmt); 4069 4070 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ 4071 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 4072 return TCL_OK; 4073 } 4074 4075 static int test_sql( 4076 void * clientData, 4077 Tcl_Interp *interp, 4078 int objc, 4079 Tcl_Obj *CONST objv[] 4080 ){ 4081 sqlite3_stmt *pStmt; 4082 4083 if( objc!=2 ){ 4084 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 4085 return TCL_ERROR; 4086 } 4087 4088 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4089 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); 4090 return TCL_OK; 4091 } 4092 4093 /* 4094 ** Usage: sqlite3_column_count STMT 4095 ** 4096 ** Return the number of columns returned by the sql statement STMT. 4097 */ 4098 static int test_column_count( 4099 void * clientData, 4100 Tcl_Interp *interp, 4101 int objc, 4102 Tcl_Obj *CONST objv[] 4103 ){ 4104 sqlite3_stmt *pStmt; 4105 4106 if( objc!=2 ){ 4107 Tcl_AppendResult(interp, "wrong # args: should be \"", 4108 Tcl_GetString(objv[0]), " STMT column", 0); 4109 return TCL_ERROR; 4110 } 4111 4112 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4113 4114 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); 4115 return TCL_OK; 4116 } 4117 4118 /* 4119 ** Usage: sqlite3_column_type STMT column 4120 ** 4121 ** Return the type of the data in column 'column' of the current row. 4122 */ 4123 static int test_column_type( 4124 void * clientData, 4125 Tcl_Interp *interp, 4126 int objc, 4127 Tcl_Obj *CONST objv[] 4128 ){ 4129 sqlite3_stmt *pStmt; 4130 int col; 4131 int tp; 4132 4133 if( objc!=3 ){ 4134 Tcl_AppendResult(interp, "wrong # args: should be \"", 4135 Tcl_GetString(objv[0]), " STMT column", 0); 4136 return TCL_ERROR; 4137 } 4138 4139 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4140 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4141 4142 tp = sqlite3_column_type(pStmt, col); 4143 switch( tp ){ 4144 case SQLITE_INTEGER: 4145 Tcl_SetResult(interp, "INTEGER", TCL_STATIC); 4146 break; 4147 case SQLITE_NULL: 4148 Tcl_SetResult(interp, "NULL", TCL_STATIC); 4149 break; 4150 case SQLITE_FLOAT: 4151 Tcl_SetResult(interp, "FLOAT", TCL_STATIC); 4152 break; 4153 case SQLITE_TEXT: 4154 Tcl_SetResult(interp, "TEXT", TCL_STATIC); 4155 break; 4156 case SQLITE_BLOB: 4157 Tcl_SetResult(interp, "BLOB", TCL_STATIC); 4158 break; 4159 default: 4160 assert(0); 4161 } 4162 4163 return TCL_OK; 4164 } 4165 4166 /* 4167 ** Usage: sqlite3_column_int64 STMT column 4168 ** 4169 ** Return the data in column 'column' of the current row cast as an 4170 ** wide (64-bit) integer. 4171 */ 4172 static int test_column_int64( 4173 void * clientData, 4174 Tcl_Interp *interp, 4175 int objc, 4176 Tcl_Obj *CONST objv[] 4177 ){ 4178 sqlite3_stmt *pStmt; 4179 int col; 4180 i64 iVal; 4181 4182 if( objc!=3 ){ 4183 Tcl_AppendResult(interp, "wrong # args: should be \"", 4184 Tcl_GetString(objv[0]), " STMT column", 0); 4185 return TCL_ERROR; 4186 } 4187 4188 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4189 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4190 4191 iVal = sqlite3_column_int64(pStmt, col); 4192 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); 4193 return TCL_OK; 4194 } 4195 4196 /* 4197 ** Usage: sqlite3_column_blob STMT column 4198 */ 4199 static int test_column_blob( 4200 void * clientData, 4201 Tcl_Interp *interp, 4202 int objc, 4203 Tcl_Obj *CONST objv[] 4204 ){ 4205 sqlite3_stmt *pStmt; 4206 int col; 4207 4208 int len; 4209 const void *pBlob; 4210 4211 if( objc!=3 ){ 4212 Tcl_AppendResult(interp, "wrong # args: should be \"", 4213 Tcl_GetString(objv[0]), " STMT column", 0); 4214 return TCL_ERROR; 4215 } 4216 4217 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4218 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4219 4220 len = sqlite3_column_bytes(pStmt, col); 4221 pBlob = sqlite3_column_blob(pStmt, col); 4222 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); 4223 return TCL_OK; 4224 } 4225 4226 /* 4227 ** Usage: sqlite3_column_double STMT column 4228 ** 4229 ** Return the data in column 'column' of the current row cast as a double. 4230 */ 4231 static int test_column_double( 4232 void * clientData, 4233 Tcl_Interp *interp, 4234 int objc, 4235 Tcl_Obj *CONST objv[] 4236 ){ 4237 sqlite3_stmt *pStmt; 4238 int col; 4239 double rVal; 4240 4241 if( objc!=3 ){ 4242 Tcl_AppendResult(interp, "wrong # args: should be \"", 4243 Tcl_GetString(objv[0]), " STMT column", 0); 4244 return TCL_ERROR; 4245 } 4246 4247 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4248 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4249 4250 rVal = sqlite3_column_double(pStmt, col); 4251 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); 4252 return TCL_OK; 4253 } 4254 4255 /* 4256 ** Usage: sqlite3_data_count STMT 4257 ** 4258 ** Return the number of columns returned by the sql statement STMT. 4259 */ 4260 static int test_data_count( 4261 void * clientData, 4262 Tcl_Interp *interp, 4263 int objc, 4264 Tcl_Obj *CONST objv[] 4265 ){ 4266 sqlite3_stmt *pStmt; 4267 4268 if( objc!=2 ){ 4269 Tcl_AppendResult(interp, "wrong # args: should be \"", 4270 Tcl_GetString(objv[0]), " STMT column", 0); 4271 return TCL_ERROR; 4272 } 4273 4274 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4275 4276 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); 4277 return TCL_OK; 4278 } 4279 4280 /* 4281 ** Usage: sqlite3_column_text STMT column 4282 ** 4283 ** Usage: sqlite3_column_decltype STMT column 4284 ** 4285 ** Usage: sqlite3_column_name STMT column 4286 */ 4287 static int test_stmt_utf8( 4288 void * clientData, /* Pointer to SQLite API function to be invoke */ 4289 Tcl_Interp *interp, 4290 int objc, 4291 Tcl_Obj *CONST objv[] 4292 ){ 4293 sqlite3_stmt *pStmt; 4294 int col; 4295 const char *(*xFunc)(sqlite3_stmt*, int); 4296 const char *zRet; 4297 4298 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; 4299 if( objc!=3 ){ 4300 Tcl_AppendResult(interp, "wrong # args: should be \"", 4301 Tcl_GetString(objv[0]), " STMT column", 0); 4302 return TCL_ERROR; 4303 } 4304 4305 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4306 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4307 zRet = xFunc(pStmt, col); 4308 if( zRet ){ 4309 Tcl_SetResult(interp, (char *)zRet, 0); 4310 } 4311 return TCL_OK; 4312 } 4313 4314 static int test_global_recover( 4315 void * clientData, 4316 Tcl_Interp *interp, 4317 int objc, 4318 Tcl_Obj *CONST objv[] 4319 ){ 4320 #ifndef SQLITE_OMIT_DEPRECATED 4321 int rc; 4322 if( objc!=1 ){ 4323 Tcl_WrongNumArgs(interp, 1, objv, ""); 4324 return TCL_ERROR; 4325 } 4326 rc = sqlite3_global_recover(); 4327 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4328 #endif 4329 return TCL_OK; 4330 } 4331 4332 /* 4333 ** Usage: sqlite3_column_text STMT column 4334 ** 4335 ** Usage: sqlite3_column_decltype STMT column 4336 ** 4337 ** Usage: sqlite3_column_name STMT column 4338 */ 4339 static int test_stmt_utf16( 4340 void * clientData, /* Pointer to SQLite API function to be invoked */ 4341 Tcl_Interp *interp, 4342 int objc, 4343 Tcl_Obj *CONST objv[] 4344 ){ 4345 #ifndef SQLITE_OMIT_UTF16 4346 sqlite3_stmt *pStmt; 4347 int col; 4348 Tcl_Obj *pRet; 4349 const void *zName16; 4350 const void *(*xFunc)(sqlite3_stmt*, int); 4351 4352 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; 4353 if( objc!=3 ){ 4354 Tcl_AppendResult(interp, "wrong # args: should be \"", 4355 Tcl_GetString(objv[0]), " STMT column", 0); 4356 return TCL_ERROR; 4357 } 4358 4359 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4360 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4361 4362 zName16 = xFunc(pStmt, col); 4363 if( zName16 ){ 4364 int n; 4365 const char *z = zName16; 4366 for(n=0; z[n] || z[n+1]; n+=2){} 4367 pRet = Tcl_NewByteArrayObj(zName16, n+2); 4368 Tcl_SetObjResult(interp, pRet); 4369 } 4370 #endif /* SQLITE_OMIT_UTF16 */ 4371 4372 return TCL_OK; 4373 } 4374 4375 /* 4376 ** Usage: sqlite3_column_int STMT column 4377 ** 4378 ** Usage: sqlite3_column_bytes STMT column 4379 ** 4380 ** Usage: sqlite3_column_bytes16 STMT column 4381 ** 4382 */ 4383 static int test_stmt_int( 4384 void * clientData, /* Pointer to SQLite API function to be invoked */ 4385 Tcl_Interp *interp, 4386 int objc, 4387 Tcl_Obj *CONST objv[] 4388 ){ 4389 sqlite3_stmt *pStmt; 4390 int col; 4391 int (*xFunc)(sqlite3_stmt*, int); 4392 4393 xFunc = (int (*)(sqlite3_stmt*, int))clientData; 4394 if( objc!=3 ){ 4395 Tcl_AppendResult(interp, "wrong # args: should be \"", 4396 Tcl_GetString(objv[0]), " STMT column", 0); 4397 return TCL_ERROR; 4398 } 4399 4400 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4401 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4402 4403 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); 4404 return TCL_OK; 4405 } 4406 4407 /* 4408 ** Usage: sqlite_set_magic DB MAGIC-NUMBER 4409 ** 4410 ** Set the db->magic value. This is used to test error recovery logic. 4411 */ 4412 static int sqlite_set_magic( 4413 void * clientData, 4414 Tcl_Interp *interp, 4415 int argc, 4416 char **argv 4417 ){ 4418 sqlite3 *db; 4419 if( argc!=3 ){ 4420 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4421 " DB MAGIC", 0); 4422 return TCL_ERROR; 4423 } 4424 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4425 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ 4426 db->magic = SQLITE_MAGIC_OPEN; 4427 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ 4428 db->magic = SQLITE_MAGIC_CLOSED; 4429 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ 4430 db->magic = SQLITE_MAGIC_BUSY; 4431 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ 4432 db->magic = SQLITE_MAGIC_ERROR; 4433 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ 4434 return TCL_ERROR; 4435 } 4436 return TCL_OK; 4437 } 4438 4439 /* 4440 ** Usage: sqlite3_interrupt DB 4441 ** 4442 ** Trigger an interrupt on DB 4443 */ 4444 static int test_interrupt( 4445 void * clientData, 4446 Tcl_Interp *interp, 4447 int argc, 4448 char **argv 4449 ){ 4450 sqlite3 *db; 4451 if( argc!=2 ){ 4452 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); 4453 return TCL_ERROR; 4454 } 4455 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4456 sqlite3_interrupt(db); 4457 return TCL_OK; 4458 } 4459 4460 static u8 *sqlite3_stack_baseline = 0; 4461 4462 /* 4463 ** Fill the stack with a known bitpattern. 4464 */ 4465 static void prepStack(void){ 4466 int i; 4467 u32 bigBuf[65536]; 4468 for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; 4469 sqlite3_stack_baseline = (u8*)&bigBuf[65536]; 4470 } 4471 4472 /* 4473 ** Get the current stack depth. Used for debugging only. 4474 */ 4475 u64 sqlite3StackDepth(void){ 4476 u8 x; 4477 return (u64)(sqlite3_stack_baseline - &x); 4478 } 4479 4480 /* 4481 ** Usage: sqlite3_stack_used DB SQL 4482 ** 4483 ** Try to measure the amount of stack space used by a call to sqlite3_exec 4484 */ 4485 static int test_stack_used( 4486 void * clientData, 4487 Tcl_Interp *interp, 4488 int argc, 4489 char **argv 4490 ){ 4491 sqlite3 *db; 4492 int i; 4493 if( argc!=3 ){ 4494 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4495 " DB SQL", 0); 4496 return TCL_ERROR; 4497 } 4498 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4499 prepStack(); 4500 (void)sqlite3_exec(db, argv[2], 0, 0, 0); 4501 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} 4502 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); 4503 return TCL_OK; 4504 } 4505 4506 /* 4507 ** Usage: sqlite_delete_function DB function-name 4508 ** 4509 ** Delete the user function 'function-name' from database handle DB. It 4510 ** is assumed that the user function was created as UTF8, any number of 4511 ** arguments (the way the TCL interface does it). 4512 */ 4513 static int delete_function( 4514 void * clientData, 4515 Tcl_Interp *interp, 4516 int argc, 4517 char **argv 4518 ){ 4519 int rc; 4520 sqlite3 *db; 4521 if( argc!=3 ){ 4522 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4523 " DB function-name", 0); 4524 return TCL_ERROR; 4525 } 4526 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4527 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); 4528 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4529 return TCL_OK; 4530 } 4531 4532 /* 4533 ** Usage: sqlite_delete_collation DB collation-name 4534 ** 4535 ** Delete the collation sequence 'collation-name' from database handle 4536 ** DB. It is assumed that the collation sequence was created as UTF8 (the 4537 ** way the TCL interface does it). 4538 */ 4539 static int delete_collation( 4540 void * clientData, 4541 Tcl_Interp *interp, 4542 int argc, 4543 char **argv 4544 ){ 4545 int rc; 4546 sqlite3 *db; 4547 if( argc!=3 ){ 4548 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4549 " DB function-name", 0); 4550 return TCL_ERROR; 4551 } 4552 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4553 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); 4554 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4555 return TCL_OK; 4556 } 4557 4558 /* 4559 ** Usage: sqlite3_get_autocommit DB 4560 ** 4561 ** Return true if the database DB is currently in auto-commit mode. 4562 ** Return false if not. 4563 */ 4564 static int get_autocommit( 4565 void * clientData, 4566 Tcl_Interp *interp, 4567 int argc, 4568 char **argv 4569 ){ 4570 char zBuf[30]; 4571 sqlite3 *db; 4572 if( argc!=2 ){ 4573 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4574 " DB", 0); 4575 return TCL_ERROR; 4576 } 4577 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4578 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db)); 4579 Tcl_AppendResult(interp, zBuf, 0); 4580 return TCL_OK; 4581 } 4582 4583 /* 4584 ** Usage: sqlite3_busy_timeout DB MS 4585 ** 4586 ** Set the busy timeout. This is more easily done using the timeout 4587 ** method of the TCL interface. But we need a way to test the case 4588 ** where it returns SQLITE_MISUSE. 4589 */ 4590 static int test_busy_timeout( 4591 void * clientData, 4592 Tcl_Interp *interp, 4593 int argc, 4594 char **argv 4595 ){ 4596 int rc, ms; 4597 sqlite3 *db; 4598 if( argc!=3 ){ 4599 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4600 " DB", 0); 4601 return TCL_ERROR; 4602 } 4603 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4604 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; 4605 rc = sqlite3_busy_timeout(db, ms); 4606 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 4607 return TCL_OK; 4608 } 4609 4610 /* 4611 ** Usage: tcl_variable_type VARIABLENAME 4612 ** 4613 ** Return the name of the internal representation for the 4614 ** value of the given variable. 4615 */ 4616 static int tcl_variable_type( 4617 void * clientData, 4618 Tcl_Interp *interp, 4619 int objc, 4620 Tcl_Obj *CONST objv[] 4621 ){ 4622 Tcl_Obj *pVar; 4623 if( objc!=2 ){ 4624 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); 4625 return TCL_ERROR; 4626 } 4627 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); 4628 if( pVar==0 ) return TCL_ERROR; 4629 if( pVar->typePtr ){ 4630 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); 4631 } 4632 return TCL_OK; 4633 } 4634 4635 /* 4636 ** Usage: sqlite3_release_memory ?N? 4637 ** 4638 ** Attempt to release memory currently held but not actually required. 4639 ** The integer N is the number of bytes we are trying to release. The 4640 ** return value is the amount of memory actually released. 4641 */ 4642 static int test_release_memory( 4643 void * clientData, 4644 Tcl_Interp *interp, 4645 int objc, 4646 Tcl_Obj *CONST objv[] 4647 ){ 4648 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) 4649 int N; 4650 int amt; 4651 if( objc!=1 && objc!=2 ){ 4652 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4653 return TCL_ERROR; 4654 } 4655 if( objc==2 ){ 4656 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4657 }else{ 4658 N = -1; 4659 } 4660 amt = sqlite3_release_memory(N); 4661 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); 4662 #endif 4663 return TCL_OK; 4664 } 4665 4666 4667 /* 4668 ** Usage: sqlite3_db_release_memory DB 4669 ** 4670 ** Attempt to release memory currently held by database DB. Return the 4671 ** result code (which in the current implementation is always zero). 4672 */ 4673 static int test_db_release_memory( 4674 void * clientData, 4675 Tcl_Interp *interp, 4676 int objc, 4677 Tcl_Obj *CONST objv[] 4678 ){ 4679 sqlite3 *db; 4680 int rc; 4681 if( objc!=2 ){ 4682 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 4683 return TCL_ERROR; 4684 } 4685 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4686 rc = sqlite3_db_release_memory(db); 4687 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 4688 return TCL_OK; 4689 } 4690 4691 /* 4692 ** Usage: sqlite3_db_cacheflush DB 4693 ** 4694 ** Attempt to flush any dirty pages to disk. 4695 */ 4696 static int test_db_cacheflush( 4697 void * clientData, 4698 Tcl_Interp *interp, 4699 int objc, 4700 Tcl_Obj *CONST objv[] 4701 ){ 4702 sqlite3 *db; 4703 int rc; 4704 if( objc!=2 ){ 4705 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 4706 return TCL_ERROR; 4707 } 4708 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4709 rc = sqlite3_db_cacheflush(db); 4710 if( rc ){ 4711 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); 4712 return TCL_ERROR; 4713 } 4714 4715 Tcl_ResetResult(interp); 4716 return TCL_OK; 4717 } 4718 4719 /* 4720 ** Usage: sqlite3_db_filename DB DBNAME 4721 ** 4722 ** Return the name of a file associated with a database. 4723 */ 4724 static int test_db_filename( 4725 void * clientData, 4726 Tcl_Interp *interp, 4727 int objc, 4728 Tcl_Obj *CONST objv[] 4729 ){ 4730 sqlite3 *db; 4731 const char *zDbName; 4732 if( objc!=3 ){ 4733 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4734 return TCL_ERROR; 4735 } 4736 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4737 zDbName = Tcl_GetString(objv[2]); 4738 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); 4739 return TCL_OK; 4740 } 4741 4742 /* 4743 ** Usage: sqlite3_db_readonly DB DBNAME 4744 ** 4745 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does 4746 ** not exist. 4747 */ 4748 static int test_db_readonly( 4749 void * clientData, 4750 Tcl_Interp *interp, 4751 int objc, 4752 Tcl_Obj *CONST objv[] 4753 ){ 4754 sqlite3 *db; 4755 const char *zDbName; 4756 if( objc!=3 ){ 4757 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4758 return TCL_ERROR; 4759 } 4760 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4761 zDbName = Tcl_GetString(objv[2]); 4762 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName))); 4763 return TCL_OK; 4764 } 4765 4766 /* 4767 ** Usage: sqlite3_soft_heap_limit ?N? 4768 ** 4769 ** Query or set the soft heap limit for the current thread. The 4770 ** limit is only changed if the N is present. The previous limit 4771 ** is returned. 4772 */ 4773 static int test_soft_heap_limit( 4774 void * clientData, 4775 Tcl_Interp *interp, 4776 int objc, 4777 Tcl_Obj *CONST objv[] 4778 ){ 4779 sqlite3_int64 amt; 4780 Tcl_WideInt N = -1; 4781 if( objc!=1 && objc!=2 ){ 4782 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4783 return TCL_ERROR; 4784 } 4785 if( objc==2 ){ 4786 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4787 } 4788 amt = sqlite3_soft_heap_limit64(N); 4789 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); 4790 return TCL_OK; 4791 } 4792 4793 /* 4794 ** Usage: sqlite3_thread_cleanup 4795 ** 4796 ** Call the sqlite3_thread_cleanup API. 4797 */ 4798 static int test_thread_cleanup( 4799 void * clientData, 4800 Tcl_Interp *interp, 4801 int objc, 4802 Tcl_Obj *CONST objv[] 4803 ){ 4804 #ifndef SQLITE_OMIT_DEPRECATED 4805 sqlite3_thread_cleanup(); 4806 #endif 4807 return TCL_OK; 4808 } 4809 4810 /* 4811 ** Usage: sqlite3_pager_refcounts DB 4812 ** 4813 ** Return a list of numbers which are the PagerRefcount for all 4814 ** pagers on each database connection. 4815 */ 4816 static int test_pager_refcounts( 4817 void * clientData, 4818 Tcl_Interp *interp, 4819 int objc, 4820 Tcl_Obj *CONST objv[] 4821 ){ 4822 sqlite3 *db; 4823 int i; 4824 int v, *a; 4825 Tcl_Obj *pResult; 4826 4827 if( objc!=2 ){ 4828 Tcl_AppendResult(interp, "wrong # args: should be \"", 4829 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 4830 return TCL_ERROR; 4831 } 4832 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4833 pResult = Tcl_NewObj(); 4834 for(i=0; i<db->nDb; i++){ 4835 if( db->aDb[i].pBt==0 ){ 4836 v = -1; 4837 }else{ 4838 sqlite3_mutex_enter(db->mutex); 4839 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); 4840 v = a[0]; 4841 sqlite3_mutex_leave(db->mutex); 4842 } 4843 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); 4844 } 4845 Tcl_SetObjResult(interp, pResult); 4846 return TCL_OK; 4847 } 4848 4849 4850 /* 4851 ** tclcmd: working_64bit_int 4852 ** 4853 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This 4854 ** leads to a number of test failures. The present command checks the 4855 ** TCL build to see whether or not it supports 64-bit integers. It 4856 ** returns TRUE if it does and FALSE if not. 4857 ** 4858 ** This command is used to warn users that their TCL build is defective 4859 ** and that the errors they are seeing in the test scripts might be 4860 ** a result of their defective TCL rather than problems in SQLite. 4861 */ 4862 static int working_64bit_int( 4863 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4864 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4865 int objc, /* Number of arguments */ 4866 Tcl_Obj *CONST objv[] /* Command arguments */ 4867 ){ 4868 Tcl_Obj *pTestObj; 4869 int working = 0; 4870 4871 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); 4872 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; 4873 Tcl_DecrRefCount(pTestObj); 4874 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); 4875 return TCL_OK; 4876 } 4877 4878 4879 /* 4880 ** tclcmd: vfs_unlink_test 4881 ** 4882 ** This TCL command unregisters the primary VFS and then registers 4883 ** it back again. This is used to test the ability to register a 4884 ** VFS when none are previously registered, and the ability to 4885 ** unregister the only available VFS. Ticket #2738 4886 */ 4887 static int vfs_unlink_test( 4888 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4889 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4890 int objc, /* Number of arguments */ 4891 Tcl_Obj *CONST objv[] /* Command arguments */ 4892 ){ 4893 int i; 4894 sqlite3_vfs *pMain; 4895 sqlite3_vfs *apVfs[20]; 4896 sqlite3_vfs one, two; 4897 4898 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ 4899 one.zName = "__one"; 4900 two.zName = "__two"; 4901 4902 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not 4903 ** change the default VFS 4904 */ 4905 pMain = sqlite3_vfs_find(0); 4906 sqlite3_vfs_register(&one, 0); 4907 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 4908 sqlite3_vfs_register(&two, 0); 4909 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 4910 4911 /* We can find a VFS by its name */ 4912 assert( sqlite3_vfs_find("__one")==&one ); 4913 assert( sqlite3_vfs_find("__two")==&two ); 4914 4915 /* Calling sqlite_vfs_register with non-zero second parameter changes the 4916 ** default VFS, even if the 1st parameter is an existig VFS that is 4917 ** previously registered as the non-default. 4918 */ 4919 sqlite3_vfs_register(&one, 1); 4920 assert( sqlite3_vfs_find("__one")==&one ); 4921 assert( sqlite3_vfs_find("__two")==&two ); 4922 assert( sqlite3_vfs_find(0)==&one ); 4923 sqlite3_vfs_register(&two, 1); 4924 assert( sqlite3_vfs_find("__one")==&one ); 4925 assert( sqlite3_vfs_find("__two")==&two ); 4926 assert( sqlite3_vfs_find(0)==&two ); 4927 if( pMain ){ 4928 sqlite3_vfs_register(pMain, 1); 4929 assert( sqlite3_vfs_find("__one")==&one ); 4930 assert( sqlite3_vfs_find("__two")==&two ); 4931 assert( sqlite3_vfs_find(0)==pMain ); 4932 } 4933 4934 /* Unlink the default VFS. Repeat until there are no more VFSes 4935 ** registered. 4936 */ 4937 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ 4938 apVfs[i] = sqlite3_vfs_find(0); 4939 if( apVfs[i] ){ 4940 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 4941 sqlite3_vfs_unregister(apVfs[i]); 4942 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); 4943 } 4944 } 4945 assert( 0==sqlite3_vfs_find(0) ); 4946 4947 /* Register the main VFS as non-default (will be made default, since 4948 ** it'll be the only one in existence). 4949 */ 4950 sqlite3_vfs_register(pMain, 0); 4951 assert( sqlite3_vfs_find(0)==pMain ); 4952 4953 /* Un-register the main VFS again to restore an empty VFS list */ 4954 sqlite3_vfs_unregister(pMain); 4955 assert( 0==sqlite3_vfs_find(0) ); 4956 4957 /* Relink all VFSes in reverse order. */ 4958 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ 4959 if( apVfs[i] ){ 4960 sqlite3_vfs_register(apVfs[i], 1); 4961 assert( apVfs[i]==sqlite3_vfs_find(0) ); 4962 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 4963 } 4964 } 4965 4966 /* Unregister out sample VFSes. */ 4967 sqlite3_vfs_unregister(&one); 4968 sqlite3_vfs_unregister(&two); 4969 4970 /* Unregistering a VFS that is not currently registered is harmless */ 4971 sqlite3_vfs_unregister(&one); 4972 sqlite3_vfs_unregister(&two); 4973 assert( sqlite3_vfs_find("__one")==0 ); 4974 assert( sqlite3_vfs_find("__two")==0 ); 4975 4976 /* We should be left with the original default VFS back as the 4977 ** original */ 4978 assert( sqlite3_vfs_find(0)==pMain ); 4979 4980 return TCL_OK; 4981 } 4982 4983 /* 4984 ** tclcmd: vfs_initfail_test 4985 ** 4986 ** This TCL command attempts to vfs_find and vfs_register when the 4987 ** sqlite3_initialize() interface is failing. All calls should fail. 4988 */ 4989 static int vfs_initfail_test( 4990 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4991 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4992 int objc, /* Number of arguments */ 4993 Tcl_Obj *CONST objv[] /* Command arguments */ 4994 ){ 4995 sqlite3_vfs one; 4996 one.zName = "__one"; 4997 4998 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4999 sqlite3_vfs_register(&one, 0); 5000 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 5001 sqlite3_vfs_register(&one, 1); 5002 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 5003 return TCL_OK; 5004 } 5005 5006 /* 5007 ** Saved VFSes 5008 */ 5009 static sqlite3_vfs *apVfs[20]; 5010 static int nVfs = 0; 5011 5012 /* 5013 ** tclcmd: vfs_unregister_all 5014 ** 5015 ** Unregister all VFSes. 5016 */ 5017 static int vfs_unregister_all( 5018 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5019 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5020 int objc, /* Number of arguments */ 5021 Tcl_Obj *CONST objv[] /* Command arguments */ 5022 ){ 5023 int i; 5024 for(i=0; i<ArraySize(apVfs); i++){ 5025 apVfs[i] = sqlite3_vfs_find(0); 5026 if( apVfs[i]==0 ) break; 5027 sqlite3_vfs_unregister(apVfs[i]); 5028 } 5029 nVfs = i; 5030 return TCL_OK; 5031 } 5032 /* 5033 ** tclcmd: vfs_reregister_all 5034 ** 5035 ** Restore all VFSes that were removed using vfs_unregister_all 5036 */ 5037 static int vfs_reregister_all( 5038 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5039 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5040 int objc, /* Number of arguments */ 5041 Tcl_Obj *CONST objv[] /* Command arguments */ 5042 ){ 5043 int i; 5044 for(i=0; i<nVfs; i++){ 5045 sqlite3_vfs_register(apVfs[i], i==0); 5046 } 5047 return TCL_OK; 5048 } 5049 5050 5051 /* 5052 ** tclcmd: file_control_test DB 5053 ** 5054 ** This TCL command runs the sqlite3_file_control interface and 5055 ** verifies correct operation of the same. 5056 */ 5057 static int file_control_test( 5058 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5059 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5060 int objc, /* Number of arguments */ 5061 Tcl_Obj *CONST objv[] /* Command arguments */ 5062 ){ 5063 int iArg = 0; 5064 sqlite3 *db; 5065 int rc; 5066 5067 if( objc!=2 ){ 5068 Tcl_AppendResult(interp, "wrong # args: should be \"", 5069 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5070 return TCL_ERROR; 5071 } 5072 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5073 rc = sqlite3_file_control(db, 0, 0, &iArg); 5074 assert( rc==SQLITE_NOTFOUND ); 5075 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); 5076 assert( rc==SQLITE_ERROR ); 5077 rc = sqlite3_file_control(db, "main", -1, &iArg); 5078 assert( rc==SQLITE_NOTFOUND ); 5079 rc = sqlite3_file_control(db, "temp", -1, &iArg); 5080 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); 5081 5082 return TCL_OK; 5083 } 5084 5085 5086 /* 5087 ** tclcmd: file_control_lasterrno_test DB 5088 ** 5089 ** This TCL command runs the sqlite3_file_control interface and 5090 ** verifies correct operation of the SQLITE_LAST_ERRNO verb. 5091 */ 5092 static int file_control_lasterrno_test( 5093 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5094 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5095 int objc, /* Number of arguments */ 5096 Tcl_Obj *CONST objv[] /* Command arguments */ 5097 ){ 5098 int iArg = 0; 5099 sqlite3 *db; 5100 int rc; 5101 5102 if( objc!=2 ){ 5103 Tcl_AppendResult(interp, "wrong # args: should be \"", 5104 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5105 return TCL_ERROR; 5106 } 5107 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5108 return TCL_ERROR; 5109 } 5110 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); 5111 if( rc ){ 5112 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5113 return TCL_ERROR; 5114 } 5115 if( iArg!=0 ) { 5116 Tcl_AppendResult(interp, "Unexpected non-zero errno: ", 5117 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); 5118 return TCL_ERROR; 5119 } 5120 return TCL_OK; 5121 } 5122 5123 /* 5124 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE 5125 ** 5126 ** This TCL command runs the sqlite3_file_control interface and 5127 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5128 ** SQLITE_SET_LOCKPROXYFILE verbs. 5129 */ 5130 static int file_control_chunksize_test( 5131 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5132 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5133 int objc, /* Number of arguments */ 5134 Tcl_Obj *CONST objv[] /* Command arguments */ 5135 ){ 5136 int nSize; /* New chunk size */ 5137 char *zDb; /* Db name ("main", "temp" etc.) */ 5138 sqlite3 *db; /* Database handle */ 5139 int rc; /* file_control() return code */ 5140 5141 if( objc!=4 ){ 5142 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5143 return TCL_ERROR; 5144 } 5145 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5146 || Tcl_GetIntFromObj(interp, objv[3], &nSize) 5147 ){ 5148 return TCL_ERROR; 5149 } 5150 zDb = Tcl_GetString(objv[2]); 5151 if( zDb[0]=='\0' ) zDb = NULL; 5152 5153 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); 5154 if( rc ){ 5155 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5156 return TCL_ERROR; 5157 } 5158 return TCL_OK; 5159 } 5160 5161 /* 5162 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE 5163 ** 5164 ** This TCL command runs the sqlite3_file_control interface 5165 ** with SQLITE_FCNTL_SIZE_HINT 5166 */ 5167 static int file_control_sizehint_test( 5168 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5169 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5170 int objc, /* Number of arguments */ 5171 Tcl_Obj *CONST objv[] /* Command arguments */ 5172 ){ 5173 Tcl_WideInt nSize; /* Hinted size */ 5174 char *zDb; /* Db name ("main", "temp" etc.) */ 5175 sqlite3 *db; /* Database handle */ 5176 int rc; /* file_control() return code */ 5177 5178 if( objc!=4 ){ 5179 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5180 return TCL_ERROR; 5181 } 5182 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5183 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) 5184 ){ 5185 return TCL_ERROR; 5186 } 5187 zDb = Tcl_GetString(objv[2]); 5188 if( zDb[0]=='\0' ) zDb = NULL; 5189 5190 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); 5191 if( rc ){ 5192 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5193 return TCL_ERROR; 5194 } 5195 return TCL_OK; 5196 } 5197 5198 /* 5199 ** tclcmd: file_control_lockproxy_test DB PWD 5200 ** 5201 ** This TCL command runs the sqlite3_file_control interface and 5202 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5203 ** SQLITE_SET_LOCKPROXYFILE verbs. 5204 */ 5205 static int file_control_lockproxy_test( 5206 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5207 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5208 int objc, /* Number of arguments */ 5209 Tcl_Obj *CONST objv[] /* Command arguments */ 5210 ){ 5211 sqlite3 *db; 5212 5213 if( objc!=3 ){ 5214 Tcl_AppendResult(interp, "wrong # args: should be \"", 5215 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); 5216 return TCL_ERROR; 5217 } 5218 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5219 return TCL_ERROR; 5220 } 5221 5222 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 5223 # if defined(__APPLE__) 5224 # define SQLITE_ENABLE_LOCKING_STYLE 1 5225 # else 5226 # define SQLITE_ENABLE_LOCKING_STYLE 0 5227 # endif 5228 #endif 5229 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 5230 { 5231 char *testPath; 5232 int rc; 5233 int nPwd; 5234 const char *zPwd; 5235 char proxyPath[400]; 5236 5237 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); 5238 if( sizeof(proxyPath)<nPwd+20 ){ 5239 Tcl_AppendResult(interp, "PWD too big", (void*)0); 5240 return TCL_ERROR; 5241 } 5242 sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd); 5243 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5244 if( rc ){ 5245 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5246 return TCL_ERROR; 5247 } 5248 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); 5249 if( strncmp(proxyPath,testPath,11) ){ 5250 Tcl_AppendResult(interp, "Lock proxy file did not match the " 5251 "previously assigned value", 0); 5252 return TCL_ERROR; 5253 } 5254 if( rc ){ 5255 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5256 return TCL_ERROR; 5257 } 5258 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5259 if( rc ){ 5260 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5261 return TCL_ERROR; 5262 } 5263 } 5264 #endif 5265 return TCL_OK; 5266 } 5267 5268 #if SQLITE_OS_WIN 5269 /* 5270 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY 5271 ** 5272 ** This TCL command runs the sqlite3_file_control interface with 5273 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. 5274 */ 5275 static int file_control_win32_av_retry( 5276 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5277 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5278 int objc, /* Number of arguments */ 5279 Tcl_Obj *CONST objv[] /* Command arguments */ 5280 ){ 5281 sqlite3 *db; 5282 int rc; 5283 int a[2]; 5284 char z[100]; 5285 5286 if( objc!=4 ){ 5287 Tcl_AppendResult(interp, "wrong # args: should be \"", 5288 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); 5289 return TCL_ERROR; 5290 } 5291 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5292 return TCL_ERROR; 5293 } 5294 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; 5295 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; 5296 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); 5297 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); 5298 Tcl_AppendResult(interp, z, (char*)0); 5299 return TCL_OK; 5300 } 5301 5302 /* 5303 ** tclcmd: file_control_win32_set_handle DB HANDLE 5304 ** 5305 ** This TCL command runs the sqlite3_file_control interface with 5306 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode. 5307 */ 5308 static int file_control_win32_set_handle( 5309 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5310 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5311 int objc, /* Number of arguments */ 5312 Tcl_Obj *CONST objv[] /* Command arguments */ 5313 ){ 5314 sqlite3 *db; 5315 int rc; 5316 HANDLE hFile = NULL; 5317 char z[100]; 5318 5319 if( objc!=3 ){ 5320 Tcl_AppendResult(interp, "wrong # args: should be \"", 5321 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0); 5322 return TCL_ERROR; 5323 } 5324 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5325 return TCL_ERROR; 5326 } 5327 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){ 5328 return TCL_ERROR; 5329 } 5330 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE, 5331 (void*)&hFile); 5332 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile); 5333 Tcl_AppendResult(interp, z, (char*)0); 5334 return TCL_OK; 5335 } 5336 #endif 5337 5338 /* 5339 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG 5340 ** 5341 ** This TCL command runs the sqlite3_file_control interface with 5342 ** the SQLITE_FCNTL_PERSIST_WAL opcode. 5343 */ 5344 static int file_control_persist_wal( 5345 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5346 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5347 int objc, /* Number of arguments */ 5348 Tcl_Obj *CONST objv[] /* Command arguments */ 5349 ){ 5350 sqlite3 *db; 5351 int rc; 5352 int bPersist; 5353 char z[100]; 5354 5355 if( objc!=3 ){ 5356 Tcl_AppendResult(interp, "wrong # args: should be \"", 5357 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5358 return TCL_ERROR; 5359 } 5360 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5361 return TCL_ERROR; 5362 } 5363 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; 5364 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); 5365 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); 5366 Tcl_AppendResult(interp, z, (char*)0); 5367 return TCL_OK; 5368 } 5369 5370 /* 5371 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG 5372 ** 5373 ** This TCL command runs the sqlite3_file_control interface with 5374 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. 5375 */ 5376 static int file_control_powersafe_overwrite( 5377 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5378 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5379 int objc, /* Number of arguments */ 5380 Tcl_Obj *CONST objv[] /* Command arguments */ 5381 ){ 5382 sqlite3 *db; 5383 int rc; 5384 int b; 5385 char z[100]; 5386 5387 if( objc!=3 ){ 5388 Tcl_AppendResult(interp, "wrong # args: should be \"", 5389 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5390 return TCL_ERROR; 5391 } 5392 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5393 return TCL_ERROR; 5394 } 5395 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR; 5396 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b); 5397 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b); 5398 Tcl_AppendResult(interp, z, (char*)0); 5399 return TCL_OK; 5400 } 5401 5402 5403 /* 5404 ** tclcmd: file_control_vfsname DB ?AUXDB? 5405 ** 5406 ** Return a string that describes the stack of VFSes. 5407 */ 5408 static int file_control_vfsname( 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 *db; 5415 const char *zDbName = "main"; 5416 char *zVfsName = 0; 5417 5418 if( objc!=2 && objc!=3 ){ 5419 Tcl_AppendResult(interp, "wrong # args: should be \"", 5420 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5421 return TCL_ERROR; 5422 } 5423 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5424 return TCL_ERROR; 5425 } 5426 if( objc==3 ){ 5427 zDbName = Tcl_GetString(objv[2]); 5428 } 5429 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); 5430 Tcl_AppendResult(interp, zVfsName, (char*)0); 5431 sqlite3_free(zVfsName); 5432 return TCL_OK; 5433 } 5434 5435 /* 5436 ** tclcmd: file_control_tempfilename DB ?AUXDB? 5437 ** 5438 ** Return a string that is a temporary filename 5439 */ 5440 static int file_control_tempfilename( 5441 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5442 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5443 int objc, /* Number of arguments */ 5444 Tcl_Obj *CONST objv[] /* Command arguments */ 5445 ){ 5446 sqlite3 *db; 5447 const char *zDbName = "main"; 5448 char *zTName = 0; 5449 5450 if( objc!=2 && objc!=3 ){ 5451 Tcl_AppendResult(interp, "wrong # args: should be \"", 5452 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5453 return TCL_ERROR; 5454 } 5455 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5456 return TCL_ERROR; 5457 } 5458 if( objc==3 ){ 5459 zDbName = Tcl_GetString(objv[2]); 5460 } 5461 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName); 5462 Tcl_AppendResult(interp, zTName, (char*)0); 5463 sqlite3_free(zTName); 5464 return TCL_OK; 5465 } 5466 5467 5468 /* 5469 ** tclcmd: sqlite3_vfs_list 5470 ** 5471 ** Return a tcl list containing the names of all registered vfs's. 5472 */ 5473 static int vfs_list( 5474 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5475 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5476 int objc, /* Number of arguments */ 5477 Tcl_Obj *CONST objv[] /* Command arguments */ 5478 ){ 5479 sqlite3_vfs *pVfs; 5480 Tcl_Obj *pRet = Tcl_NewObj(); 5481 if( objc!=1 ){ 5482 Tcl_WrongNumArgs(interp, 1, objv, ""); 5483 return TCL_ERROR; 5484 } 5485 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 5486 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); 5487 } 5488 Tcl_SetObjResult(interp, pRet); 5489 return TCL_OK; 5490 } 5491 5492 /* 5493 ** tclcmd: sqlite3_limit DB ID VALUE 5494 ** 5495 ** This TCL command runs the sqlite3_limit interface and 5496 ** verifies correct operation of the same. 5497 */ 5498 static int test_limit( 5499 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5500 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5501 int objc, /* Number of arguments */ 5502 Tcl_Obj *CONST objv[] /* Command arguments */ 5503 ){ 5504 sqlite3 *db; 5505 int rc; 5506 static const struct { 5507 char *zName; 5508 int id; 5509 } aId[] = { 5510 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, 5511 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, 5512 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, 5513 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, 5514 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, 5515 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, 5516 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, 5517 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, 5518 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 5519 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, 5520 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, 5521 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS }, 5522 5523 /* Out of range test cases */ 5524 { "SQLITE_LIMIT_TOOSMALL", -1, }, 5525 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 }, 5526 }; 5527 int i, id = 0; 5528 int val; 5529 const char *zId; 5530 5531 if( objc!=4 ){ 5532 Tcl_AppendResult(interp, "wrong # args: should be \"", 5533 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); 5534 return TCL_ERROR; 5535 } 5536 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5537 zId = Tcl_GetString(objv[2]); 5538 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ 5539 if( strcmp(zId, aId[i].zName)==0 ){ 5540 id = aId[i].id; 5541 break; 5542 } 5543 } 5544 if( i>=sizeof(aId)/sizeof(aId[0]) ){ 5545 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); 5546 return TCL_ERROR; 5547 } 5548 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 5549 rc = sqlite3_limit(db, id, val); 5550 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5551 return TCL_OK; 5552 } 5553 5554 /* 5555 ** tclcmd: save_prng_state 5556 ** 5557 ** Save the state of the pseudo-random number generator. 5558 ** At the same time, verify that sqlite3_test_control works even when 5559 ** called with an out-of-range opcode. 5560 */ 5561 static int save_prng_state( 5562 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5563 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5564 int objc, /* Number of arguments */ 5565 Tcl_Obj *CONST objv[] /* Command arguments */ 5566 ){ 5567 int rc = sqlite3_test_control(9999); 5568 assert( rc==0 ); 5569 rc = sqlite3_test_control(-1); 5570 assert( rc==0 ); 5571 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); 5572 return TCL_OK; 5573 } 5574 /* 5575 ** tclcmd: restore_prng_state 5576 */ 5577 static int restore_prng_state( 5578 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5579 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5580 int objc, /* Number of arguments */ 5581 Tcl_Obj *CONST objv[] /* Command arguments */ 5582 ){ 5583 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); 5584 return TCL_OK; 5585 } 5586 /* 5587 ** tclcmd: reset_prng_state 5588 */ 5589 static int reset_prng_state( 5590 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5591 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5592 int objc, /* Number of arguments */ 5593 Tcl_Obj *CONST objv[] /* Command arguments */ 5594 ){ 5595 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); 5596 return TCL_OK; 5597 } 5598 5599 /* 5600 ** tclcmd: database_may_be_corrupt 5601 ** 5602 ** Indicate that database files might be corrupt. In other words, set the normal 5603 ** state of operation. 5604 */ 5605 static int database_may_be_corrupt( 5606 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5607 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5608 int objc, /* Number of arguments */ 5609 Tcl_Obj *CONST objv[] /* Command arguments */ 5610 ){ 5611 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0); 5612 return TCL_OK; 5613 } 5614 /* 5615 ** tclcmd: database_never_corrupt 5616 ** 5617 ** Indicate that database files are always well-formed. This enables extra assert() 5618 ** statements that test conditions that are always true for well-formed databases. 5619 */ 5620 static int database_never_corrupt( 5621 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5622 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5623 int objc, /* Number of arguments */ 5624 Tcl_Obj *CONST objv[] /* Command arguments */ 5625 ){ 5626 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1); 5627 return TCL_OK; 5628 } 5629 5630 /* 5631 ** tclcmd: pcache_stats 5632 */ 5633 static int test_pcache_stats( 5634 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5635 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5636 int objc, /* Number of arguments */ 5637 Tcl_Obj *CONST objv[] /* Command arguments */ 5638 ){ 5639 int nMin; 5640 int nMax; 5641 int nCurrent; 5642 int nRecyclable; 5643 Tcl_Obj *pRet; 5644 5645 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); 5646 5647 pRet = Tcl_NewObj(); 5648 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); 5649 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); 5650 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); 5651 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); 5652 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); 5653 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); 5654 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); 5655 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); 5656 5657 Tcl_SetObjResult(interp, pRet); 5658 5659 return TCL_OK; 5660 } 5661 5662 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5663 static void test_unlock_notify_cb(void **aArg, int nArg){ 5664 int ii; 5665 for(ii=0; ii<nArg; ii++){ 5666 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); 5667 } 5668 } 5669 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ 5670 5671 /* 5672 ** tclcmd: sqlite3_unlock_notify db 5673 */ 5674 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5675 static int test_unlock_notify( 5676 ClientData clientData, /* Unused */ 5677 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5678 int objc, /* Number of arguments */ 5679 Tcl_Obj *CONST objv[] /* Command arguments */ 5680 ){ 5681 sqlite3 *db; 5682 int rc; 5683 5684 if( objc!=2 ){ 5685 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 5686 return TCL_ERROR; 5687 } 5688 5689 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5690 return TCL_ERROR; 5691 } 5692 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); 5693 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5694 return TCL_OK; 5695 } 5696 #endif 5697 5698 /* 5699 ** tclcmd: sqlite3_wal_checkpoint db ?NAME? 5700 */ 5701 static int test_wal_checkpoint( 5702 ClientData clientData, /* Unused */ 5703 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5704 int objc, /* Number of arguments */ 5705 Tcl_Obj *CONST objv[] /* Command arguments */ 5706 ){ 5707 char *zDb = 0; 5708 sqlite3 *db; 5709 int rc; 5710 5711 if( objc!=3 && objc!=2 ){ 5712 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); 5713 return TCL_ERROR; 5714 } 5715 5716 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5717 return TCL_ERROR; 5718 } 5719 if( objc==3 ){ 5720 zDb = Tcl_GetString(objv[2]); 5721 } 5722 rc = sqlite3_wal_checkpoint(db, zDb); 5723 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5724 return TCL_OK; 5725 } 5726 5727 /* 5728 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? 5729 ** 5730 ** This command calls the wal_checkpoint_v2() function with the specified 5731 ** mode argument (passive, full or restart). If present, the database name 5732 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the 5733 ** NAME argument is not present, a NULL pointer is passed instead. 5734 ** 5735 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or 5736 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set 5737 ** to the error message obtained from sqlite3_errmsg(). 5738 ** 5739 ** Otherwise, this command returns a list of three integers. The first integer 5740 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers 5741 ** are the values returned via the output parameters by wal_checkpoint_v2() - 5742 ** the number of frames in the log and the number of frames in the log 5743 ** that have been checkpointed. 5744 */ 5745 static int test_wal_checkpoint_v2( 5746 ClientData clientData, /* Unused */ 5747 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5748 int objc, /* Number of arguments */ 5749 Tcl_Obj *CONST objv[] /* Command arguments */ 5750 ){ 5751 char *zDb = 0; 5752 sqlite3 *db; 5753 int rc; 5754 5755 int eMode; 5756 int nLog = -555; 5757 int nCkpt = -555; 5758 Tcl_Obj *pRet; 5759 5760 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 }; 5761 assert( SQLITE_CHECKPOINT_PASSIVE==0 ); 5762 assert( SQLITE_CHECKPOINT_FULL==1 ); 5763 assert( SQLITE_CHECKPOINT_RESTART==2 ); 5764 assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); 5765 5766 if( objc!=3 && objc!=4 ){ 5767 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); 5768 return TCL_ERROR; 5769 } 5770 5771 if( objc==4 ){ 5772 zDb = Tcl_GetString(objv[3]); 5773 } 5774 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || ( 5775 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode) 5776 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) 5777 )){ 5778 return TCL_ERROR; 5779 } 5780 5781 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); 5782 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 5783 const char *zErrCode = sqlite3ErrName(rc); 5784 Tcl_ResetResult(interp); 5785 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0); 5786 return TCL_ERROR; 5787 } 5788 5789 pRet = Tcl_NewObj(); 5790 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); 5791 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); 5792 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); 5793 Tcl_SetObjResult(interp, pRet); 5794 5795 return TCL_OK; 5796 } 5797 5798 /* 5799 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE 5800 */ 5801 static int test_wal_autocheckpoint( 5802 ClientData clientData, /* Unused */ 5803 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5804 int objc, /* Number of arguments */ 5805 Tcl_Obj *CONST objv[] /* Command arguments */ 5806 ){ 5807 sqlite3 *db; 5808 int rc; 5809 int iVal; 5810 5811 5812 if( objc!=3 ){ 5813 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE"); 5814 return TCL_ERROR; 5815 } 5816 5817 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5818 || Tcl_GetIntFromObj(0, objv[2], &iVal) 5819 ){ 5820 return TCL_ERROR; 5821 } 5822 5823 rc = sqlite3_wal_autocheckpoint(db, iVal); 5824 Tcl_ResetResult(interp); 5825 if( rc!=SQLITE_OK ){ 5826 const char *zErrCode = sqlite3ErrName(rc); 5827 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1)); 5828 return TCL_ERROR; 5829 } 5830 5831 return TCL_OK; 5832 } 5833 5834 5835 /* 5836 ** tclcmd: test_sqlite3_log ?SCRIPT? 5837 */ 5838 static struct LogCallback { 5839 Tcl_Interp *pInterp; 5840 Tcl_Obj *pObj; 5841 } logcallback = {0, 0}; 5842 static void xLogcallback(void *unused, int err, char *zMsg){ 5843 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); 5844 Tcl_IncrRefCount(pNew); 5845 Tcl_ListObjAppendElement( 5846 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1) 5847 ); 5848 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); 5849 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 5850 Tcl_DecrRefCount(pNew); 5851 } 5852 static int test_sqlite3_log( 5853 ClientData clientData, 5854 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5855 int objc, /* Number of arguments */ 5856 Tcl_Obj *CONST objv[] /* Command arguments */ 5857 ){ 5858 if( objc>2 ){ 5859 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); 5860 return TCL_ERROR; 5861 } 5862 if( logcallback.pObj ){ 5863 Tcl_DecrRefCount(logcallback.pObj); 5864 logcallback.pObj = 0; 5865 logcallback.pInterp = 0; 5866 sqlite3_config(SQLITE_CONFIG_LOG, 0, 0); 5867 } 5868 if( objc>1 ){ 5869 logcallback.pObj = objv[1]; 5870 Tcl_IncrRefCount(logcallback.pObj); 5871 logcallback.pInterp = interp; 5872 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0); 5873 } 5874 return TCL_OK; 5875 } 5876 5877 /* 5878 ** tcl_objproc COMMANDNAME ARGS... 5879 ** 5880 ** Run a TCL command using its objProc interface. Throw an error if 5881 ** the command has no objProc interface. 5882 */ 5883 static int runAsObjProc( 5884 void * clientData, 5885 Tcl_Interp *interp, 5886 int objc, 5887 Tcl_Obj *CONST objv[] 5888 ){ 5889 Tcl_CmdInfo cmdInfo; 5890 if( objc<2 ){ 5891 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); 5892 return TCL_ERROR; 5893 } 5894 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 5895 Tcl_AppendResult(interp, "command not found: ", 5896 Tcl_GetString(objv[1]), (char*)0); 5897 return TCL_ERROR; 5898 } 5899 if( cmdInfo.objProc==0 ){ 5900 Tcl_AppendResult(interp, "command has no objProc: ", 5901 Tcl_GetString(objv[1]), (char*)0); 5902 return TCL_ERROR; 5903 } 5904 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); 5905 } 5906 5907 #ifndef SQLITE_OMIT_EXPLAIN 5908 /* 5909 ** WARNING: The following function, printExplainQueryPlan() is an exact 5910 ** copy of example code from eqp.in (eqp.html). If this code is modified, 5911 ** then the documentation copy needs to be modified as well. 5912 */ 5913 /* 5914 ** Argument pStmt is a prepared SQL statement. This function compiles 5915 ** an EXPLAIN QUERY PLAN command to report on the prepared statement, 5916 ** and prints the report to stdout using printf(). 5917 */ 5918 int printExplainQueryPlan(sqlite3_stmt *pStmt){ 5919 const char *zSql; /* Input SQL */ 5920 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ 5921 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ 5922 int rc; /* Return code from sqlite3_prepare_v2() */ 5923 5924 zSql = sqlite3_sql(pStmt); 5925 if( zSql==0 ) return SQLITE_ERROR; 5926 5927 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); 5928 if( zExplain==0 ) return SQLITE_NOMEM; 5929 5930 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); 5931 sqlite3_free(zExplain); 5932 if( rc!=SQLITE_OK ) return rc; 5933 5934 while( SQLITE_ROW==sqlite3_step(pExplain) ){ 5935 int iSelectid = sqlite3_column_int(pExplain, 0); 5936 int iOrder = sqlite3_column_int(pExplain, 1); 5937 int iFrom = sqlite3_column_int(pExplain, 2); 5938 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); 5939 5940 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); 5941 } 5942 5943 return sqlite3_finalize(pExplain); 5944 } 5945 5946 static int test_print_eqp( 5947 void * clientData, 5948 Tcl_Interp *interp, 5949 int objc, 5950 Tcl_Obj *CONST objv[] 5951 ){ 5952 int rc; 5953 sqlite3_stmt *pStmt; 5954 5955 if( objc!=2 ){ 5956 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 5957 return TCL_ERROR; 5958 } 5959 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 5960 rc = printExplainQueryPlan(pStmt); 5961 /* This is needed on Windows so that a test case using this 5962 ** function can open a read pipe and get the output of 5963 ** printExplainQueryPlan() immediately. 5964 */ 5965 fflush(stdout); 5966 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 5967 return TCL_OK; 5968 } 5969 #endif /* SQLITE_OMIT_EXPLAIN */ 5970 5971 /* 5972 ** sqlite3_test_control VERB ARGS... 5973 */ 5974 static int test_test_control( 5975 void * clientData, 5976 Tcl_Interp *interp, 5977 int objc, 5978 Tcl_Obj *CONST objv[] 5979 ){ 5980 struct Verb { 5981 const char *zName; 5982 int i; 5983 } aVerb[] = { 5984 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, 5985 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP }, 5986 { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER }, 5987 }; 5988 int iVerb; 5989 int iFlag; 5990 int rc; 5991 5992 if( objc<2 ){ 5993 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); 5994 return TCL_ERROR; 5995 } 5996 5997 rc = Tcl_GetIndexFromObjStruct( 5998 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb 5999 ); 6000 if( rc!=TCL_OK ) return rc; 6001 6002 iFlag = aVerb[iVerb].i; 6003 switch( iFlag ){ 6004 case SQLITE_TESTCTRL_LOCALTIME_FAULT: { 6005 int val; 6006 if( objc!=3 ){ 6007 Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); 6008 return TCL_ERROR; 6009 } 6010 if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 6011 sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); 6012 break; 6013 } 6014 6015 case SQLITE_TESTCTRL_SORTER_MMAP: { 6016 int val; 6017 sqlite3 *db; 6018 if( objc!=4 ){ 6019 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT"); 6020 return TCL_ERROR; 6021 } 6022 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; 6023 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 6024 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val); 6025 break; 6026 } 6027 6028 case SQLITE_TESTCTRL_IMPOSTER: { 6029 int onOff, tnum; 6030 const char *zDbName; 6031 sqlite3 *db; 6032 if( objc!=6 ){ 6033 Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum"); 6034 return TCL_ERROR; 6035 } 6036 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; 6037 zDbName = Tcl_GetString(objv[3]); 6038 if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR; 6039 if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR; 6040 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum); 6041 break; 6042 } 6043 } 6044 6045 Tcl_ResetResult(interp); 6046 return TCL_OK; 6047 } 6048 6049 #if SQLITE_OS_UNIX 6050 #include <sys/time.h> 6051 #include <sys/resource.h> 6052 6053 static int test_getrusage( 6054 void * clientData, 6055 Tcl_Interp *interp, 6056 int objc, 6057 Tcl_Obj *CONST objv[] 6058 ){ 6059 char buf[1024]; 6060 struct rusage r; 6061 memset(&r, 0, sizeof(r)); 6062 getrusage(RUSAGE_SELF, &r); 6063 6064 sqlite3_snprintf(sizeof(buf), buf, 6065 "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", 6066 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, 6067 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, 6068 (int)r.ru_minflt, (int)r.ru_majflt 6069 ); 6070 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1)); 6071 return TCL_OK; 6072 } 6073 #endif 6074 6075 #if SQLITE_OS_WIN 6076 /* 6077 ** Information passed from the main thread into the windows file locker 6078 ** background thread. 6079 */ 6080 struct win32FileLocker { 6081 char *evName; /* Name of event to signal thread startup */ 6082 HANDLE h; /* Handle of the file to be locked */ 6083 int delay1; /* Delay before locking */ 6084 int delay2; /* Delay before unlocking */ 6085 int ok; /* Finished ok */ 6086 int err; /* True if an error occurs */ 6087 }; 6088 #endif 6089 6090 6091 #if SQLITE_OS_WIN 6092 #include <process.h> 6093 /* 6094 ** The background thread that does file locking. 6095 */ 6096 static void win32_file_locker(void *pAppData){ 6097 struct win32FileLocker *p = (struct win32FileLocker*)pAppData; 6098 if( p->evName ){ 6099 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); 6100 if ( ev ){ 6101 SetEvent(ev); 6102 CloseHandle(ev); 6103 } 6104 } 6105 if( p->delay1 ) Sleep(p->delay1); 6106 if( LockFile(p->h, 0, 0, 100000000, 0) ){ 6107 Sleep(p->delay2); 6108 UnlockFile(p->h, 0, 0, 100000000, 0); 6109 p->ok = 1; 6110 }else{ 6111 p->err = 1; 6112 } 6113 CloseHandle(p->h); 6114 p->h = 0; 6115 p->delay1 = 0; 6116 p->delay2 = 0; 6117 } 6118 #endif 6119 6120 #if SQLITE_OS_WIN 6121 /* 6122 ** lock_win32_file FILENAME DELAY1 DELAY2 6123 ** 6124 ** Get an exclusive manditory lock on file for DELAY2 milliseconds. 6125 ** Wait DELAY1 milliseconds before acquiring the lock. 6126 */ 6127 static int win32_file_lock( 6128 void * clientData, 6129 Tcl_Interp *interp, 6130 int objc, 6131 Tcl_Obj *CONST objv[] 6132 ){ 6133 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; 6134 const char *zFilename; 6135 char zBuf[200]; 6136 int retry = 0; 6137 HANDLE ev; 6138 DWORD wResult; 6139 6140 if( objc!=4 && objc!=1 ){ 6141 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); 6142 return TCL_ERROR; 6143 } 6144 if( objc==1 ){ 6145 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", 6146 x.ok, x.err, x.delay1, x.delay2, x.h); 6147 Tcl_AppendResult(interp, zBuf, (char*)0); 6148 return TCL_OK; 6149 } 6150 while( x.h && retry<30 ){ 6151 retry++; 6152 Sleep(100); 6153 } 6154 if( x.h ){ 6155 Tcl_AppendResult(interp, "busy", (char*)0); 6156 return TCL_ERROR; 6157 } 6158 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; 6159 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; 6160 zFilename = Tcl_GetString(objv[1]); 6161 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, 6162 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 6163 FILE_ATTRIBUTE_NORMAL, 0); 6164 if( !x.h ){ 6165 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); 6166 return TCL_ERROR; 6167 } 6168 ev = CreateEvent(NULL, TRUE, FALSE, x.evName); 6169 if ( !ev ){ 6170 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); 6171 return TCL_ERROR; 6172 } 6173 _beginthread(win32_file_locker, 0, (void*)&x); 6174 Sleep(0); 6175 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ 6176 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); 6177 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); 6178 CloseHandle(ev); 6179 return TCL_ERROR; 6180 } 6181 CloseHandle(ev); 6182 return TCL_OK; 6183 } 6184 6185 /* 6186 ** exists_win32_path PATH 6187 ** 6188 ** Returns non-zero if the specified path exists, whose fully qualified name 6189 ** may exceed 260 characters if it is prefixed with "\\?\". 6190 */ 6191 static int win32_exists_path( 6192 void *clientData, 6193 Tcl_Interp *interp, 6194 int objc, 6195 Tcl_Obj *CONST objv[] 6196 ){ 6197 if( objc!=2 ){ 6198 Tcl_WrongNumArgs(interp, 1, objv, "PATH"); 6199 return TCL_ERROR; 6200 } 6201 Tcl_SetObjResult(interp, Tcl_NewBooleanObj( 6202 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES )); 6203 return TCL_OK; 6204 } 6205 6206 /* 6207 ** find_win32_file PATTERN 6208 ** 6209 ** Returns a list of entries in a directory that match the specified pattern, 6210 ** whose fully qualified name may exceed 248 characters if it is prefixed with 6211 ** "\\?\". 6212 */ 6213 static int win32_find_file( 6214 void *clientData, 6215 Tcl_Interp *interp, 6216 int objc, 6217 Tcl_Obj *CONST objv[] 6218 ){ 6219 HANDLE hFindFile = INVALID_HANDLE_VALUE; 6220 WIN32_FIND_DATAW findData; 6221 Tcl_Obj *listObj; 6222 DWORD lastErrno; 6223 if( objc!=2 ){ 6224 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN"); 6225 return TCL_ERROR; 6226 } 6227 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData); 6228 if( hFindFile==INVALID_HANDLE_VALUE ){ 6229 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6230 return TCL_ERROR; 6231 } 6232 listObj = Tcl_NewObj(); 6233 Tcl_IncrRefCount(listObj); 6234 do { 6235 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj( 6236 findData.cFileName, -1)); 6237 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj( 6238 findData.dwFileAttributes)); 6239 } while( FindNextFileW(hFindFile, &findData) ); 6240 lastErrno = GetLastError(); 6241 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){ 6242 FindClose(hFindFile); 6243 Tcl_DecrRefCount(listObj); 6244 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6245 return TCL_ERROR; 6246 } 6247 FindClose(hFindFile); 6248 Tcl_SetObjResult(interp, listObj); 6249 return TCL_OK; 6250 } 6251 6252 /* 6253 ** delete_win32_file FILENAME 6254 ** 6255 ** Deletes the specified file, whose fully qualified name may exceed 260 6256 ** characters if it is prefixed with "\\?\". 6257 */ 6258 static int win32_delete_file( 6259 void *clientData, 6260 Tcl_Interp *interp, 6261 int objc, 6262 Tcl_Obj *CONST objv[] 6263 ){ 6264 if( objc!=2 ){ 6265 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 6266 return TCL_ERROR; 6267 } 6268 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){ 6269 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6270 return TCL_ERROR; 6271 } 6272 Tcl_ResetResult(interp); 6273 return TCL_OK; 6274 } 6275 6276 /* 6277 ** make_win32_dir DIRECTORY 6278 ** 6279 ** Creates the specified directory, whose fully qualified name may exceed 248 6280 ** characters if it is prefixed with "\\?\". 6281 */ 6282 static int win32_mkdir( 6283 void *clientData, 6284 Tcl_Interp *interp, 6285 int objc, 6286 Tcl_Obj *CONST objv[] 6287 ){ 6288 if( objc!=2 ){ 6289 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); 6290 return TCL_ERROR; 6291 } 6292 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){ 6293 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6294 return TCL_ERROR; 6295 } 6296 Tcl_ResetResult(interp); 6297 return TCL_OK; 6298 } 6299 6300 /* 6301 ** remove_win32_dir DIRECTORY 6302 ** 6303 ** Removes the specified directory, whose fully qualified name may exceed 248 6304 ** characters if it is prefixed with "\\?\". 6305 */ 6306 static int win32_rmdir( 6307 void *clientData, 6308 Tcl_Interp *interp, 6309 int objc, 6310 Tcl_Obj *CONST objv[] 6311 ){ 6312 if( objc!=2 ){ 6313 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); 6314 return TCL_ERROR; 6315 } 6316 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){ 6317 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6318 return TCL_ERROR; 6319 } 6320 Tcl_ResetResult(interp); 6321 return TCL_OK; 6322 } 6323 #endif 6324 6325 6326 /* 6327 ** optimization_control DB OPT BOOLEAN 6328 ** 6329 ** Enable or disable query optimizations using the sqlite3_test_control() 6330 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. 6331 ** OPT is the name of the optimization to be disabled. 6332 */ 6333 static int optimization_control( 6334 void * clientData, 6335 Tcl_Interp *interp, 6336 int objc, 6337 Tcl_Obj *CONST objv[] 6338 ){ 6339 int i; 6340 sqlite3 *db; 6341 const char *zOpt; 6342 int onoff; 6343 int mask = 0; 6344 static const struct { 6345 const char *zOptName; 6346 int mask; 6347 } aOpt[] = { 6348 { "all", SQLITE_AllOpts }, 6349 { "none", 0 }, 6350 { "query-flattener", SQLITE_QueryFlattener }, 6351 { "column-cache", SQLITE_ColumnCache }, 6352 { "groupby-order", SQLITE_GroupByOrder }, 6353 { "factor-constants", SQLITE_FactorOutConst }, 6354 { "distinct-opt", SQLITE_DistinctOpt }, 6355 { "cover-idx-scan", SQLITE_CoverIdxScan }, 6356 { "order-by-idx-join", SQLITE_OrderByIdxJoin }, 6357 { "transitive", SQLITE_Transitive }, 6358 { "subquery-coroutine", SQLITE_SubqCoroutine }, 6359 { "omit-noop-join", SQLITE_OmitNoopJoin }, 6360 { "stat3", SQLITE_Stat34 }, 6361 { "stat4", SQLITE_Stat34 }, 6362 }; 6363 6364 if( objc!=4 ){ 6365 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); 6366 return TCL_ERROR; 6367 } 6368 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6369 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; 6370 zOpt = Tcl_GetString(objv[2]); 6371 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 6372 if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ 6373 mask = aOpt[i].mask; 6374 break; 6375 } 6376 } 6377 if( onoff ) mask = ~mask; 6378 if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ 6379 Tcl_AppendResult(interp, "unknown optimization - should be one of:", 6380 (char*)0); 6381 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 6382 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0); 6383 } 6384 return TCL_ERROR; 6385 } 6386 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); 6387 return TCL_OK; 6388 } 6389 6390 typedef struct sqlite3_api_routines sqlite3_api_routines; 6391 /* 6392 ** load_static_extension DB NAME ... 6393 ** 6394 ** Load one or more statically linked extensions. 6395 */ 6396 static int tclLoadStaticExtensionCmd( 6397 void * clientData, 6398 Tcl_Interp *interp, 6399 int objc, 6400 Tcl_Obj *CONST objv[] 6401 ){ 6402 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); 6403 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); 6404 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*); 6405 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*); 6406 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); 6407 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); 6408 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); 6409 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*); 6410 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); 6411 extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*); 6412 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); 6413 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); 6414 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); 6415 static const struct { 6416 const char *zExtName; 6417 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); 6418 } aExtension[] = { 6419 { "amatch", sqlite3_amatch_init }, 6420 { "closure", sqlite3_closure_init }, 6421 { "eval", sqlite3_eval_init }, 6422 { "fileio", sqlite3_fileio_init }, 6423 { "fuzzer", sqlite3_fuzzer_init }, 6424 { "ieee754", sqlite3_ieee_init }, 6425 { "nextchar", sqlite3_nextchar_init }, 6426 { "percentile", sqlite3_percentile_init }, 6427 { "regexp", sqlite3_regexp_init }, 6428 { "series", sqlite3_series_init }, 6429 { "spellfix", sqlite3_spellfix_init }, 6430 { "totype", sqlite3_totype_init }, 6431 { "wholenumber", sqlite3_wholenumber_init }, 6432 }; 6433 sqlite3 *db; 6434 const char *zName; 6435 int i, j, rc; 6436 char *zErrMsg = 0; 6437 if( objc<3 ){ 6438 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); 6439 return TCL_ERROR; 6440 } 6441 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6442 for(j=2; j<objc; j++){ 6443 zName = Tcl_GetString(objv[j]); 6444 for(i=0; i<ArraySize(aExtension); i++){ 6445 if( strcmp(zName, aExtension[i].zExtName)==0 ) break; 6446 } 6447 if( i>=ArraySize(aExtension) ){ 6448 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0); 6449 return TCL_ERROR; 6450 } 6451 if( aExtension[i].pInit ){ 6452 rc = aExtension[i].pInit(db, &zErrMsg, 0); 6453 }else{ 6454 rc = SQLITE_OK; 6455 } 6456 if( rc!=SQLITE_OK || zErrMsg ){ 6457 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, 6458 (char*)0); 6459 sqlite3_free(zErrMsg); 6460 return TCL_ERROR; 6461 } 6462 } 6463 return TCL_OK; 6464 } 6465 6466 /* 6467 ** sorter_test_fakeheap BOOL 6468 ** 6469 */ 6470 static int sorter_test_fakeheap( 6471 void * clientData, 6472 Tcl_Interp *interp, 6473 int objc, 6474 Tcl_Obj *CONST objv[] 6475 ){ 6476 int bArg; 6477 if( objc!=2 ){ 6478 Tcl_WrongNumArgs(interp, 1, objv, "BOOL"); 6479 return TCL_ERROR; 6480 } 6481 6482 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){ 6483 return TCL_ERROR; 6484 } 6485 6486 if( bArg ){ 6487 if( sqlite3GlobalConfig.pHeap==0 ){ 6488 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1); 6489 } 6490 }else{ 6491 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){ 6492 sqlite3GlobalConfig.pHeap = 0; 6493 } 6494 } 6495 6496 Tcl_ResetResult(interp); 6497 return TCL_OK; 6498 } 6499 6500 /* 6501 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2 6502 ** 6503 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row, 6504 ** check that the leftmost and rightmost columns returned are both integers, 6505 ** and that both contain the same value. 6506 ** 6507 ** Then execute statement $SQL2. Check that the statement returns the same 6508 ** set of integers in the same order as in the previous step (using $SQL1). 6509 */ 6510 static int sorter_test_sort4_helper( 6511 void * clientData, 6512 Tcl_Interp *interp, 6513 int objc, 6514 Tcl_Obj *CONST objv[] 6515 ){ 6516 const char *zSql1; 6517 const char *zSql2; 6518 int nStep; 6519 int iStep; 6520 int iCksum1 = 0; 6521 int iCksum2 = 0; 6522 int rc; 6523 int iB; 6524 sqlite3 *db; 6525 sqlite3_stmt *pStmt; 6526 6527 if( objc!=5 ){ 6528 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2"); 6529 return TCL_ERROR; 6530 } 6531 6532 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6533 zSql1 = Tcl_GetString(objv[2]); 6534 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR; 6535 zSql2 = Tcl_GetString(objv[4]); 6536 6537 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0); 6538 if( rc!=SQLITE_OK ) goto sql_error; 6539 6540 iB = sqlite3_column_count(pStmt)-1; 6541 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){ 6542 int a = sqlite3_column_int(pStmt, 0); 6543 if( a!=sqlite3_column_int(pStmt, iB) ){ 6544 Tcl_AppendResult(interp, "data error: (a!=b)", 0); 6545 return TCL_ERROR; 6546 } 6547 6548 iCksum1 += (iCksum1 << 3) + a; 6549 } 6550 rc = sqlite3_finalize(pStmt); 6551 if( rc!=SQLITE_OK ) goto sql_error; 6552 6553 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0); 6554 if( rc!=SQLITE_OK ) goto sql_error; 6555 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){ 6556 int a = sqlite3_column_int(pStmt, 0); 6557 iCksum2 += (iCksum2 << 3) + a; 6558 } 6559 rc = sqlite3_finalize(pStmt); 6560 if( rc!=SQLITE_OK ) goto sql_error; 6561 6562 if( iCksum1!=iCksum2 ){ 6563 Tcl_AppendResult(interp, "checksum mismatch", 0); 6564 return TCL_ERROR; 6565 } 6566 6567 return TCL_OK; 6568 sql_error: 6569 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0); 6570 return TCL_ERROR; 6571 } 6572 6573 6574 #ifdef SQLITE_USER_AUTHENTICATION 6575 #include "sqlite3userauth.h" 6576 /* 6577 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD 6578 */ 6579 static int test_user_authenticate( 6580 ClientData clientData, /* Unused */ 6581 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6582 int objc, /* Number of arguments */ 6583 Tcl_Obj *CONST objv[] /* Command arguments */ 6584 ){ 6585 char *zUser = 0; 6586 char *zPasswd = 0; 6587 int nPasswd = 0; 6588 sqlite3 *db; 6589 int rc; 6590 6591 if( objc!=4 ){ 6592 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD"); 6593 return TCL_ERROR; 6594 } 6595 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6596 return TCL_ERROR; 6597 } 6598 zUser = Tcl_GetString(objv[2]); 6599 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6600 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd); 6601 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6602 return TCL_OK; 6603 } 6604 #endif /* SQLITE_USER_AUTHENTICATION */ 6605 6606 #ifdef SQLITE_USER_AUTHENTICATION 6607 /* 6608 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN 6609 */ 6610 static int test_user_add( 6611 ClientData clientData, /* Unused */ 6612 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6613 int objc, /* Number of arguments */ 6614 Tcl_Obj *CONST objv[] /* Command arguments */ 6615 ){ 6616 char *zUser = 0; 6617 char *zPasswd = 0; 6618 int nPasswd = 0; 6619 int isAdmin = 0; 6620 sqlite3 *db; 6621 int rc; 6622 6623 if( objc!=5 ){ 6624 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); 6625 return TCL_ERROR; 6626 } 6627 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6628 return TCL_ERROR; 6629 } 6630 zUser = Tcl_GetString(objv[2]); 6631 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6632 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); 6633 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin); 6634 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6635 return TCL_OK; 6636 } 6637 #endif /* SQLITE_USER_AUTHENTICATION */ 6638 6639 #ifdef SQLITE_USER_AUTHENTICATION 6640 /* 6641 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN 6642 */ 6643 static int test_user_change( 6644 ClientData clientData, /* Unused */ 6645 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6646 int objc, /* Number of arguments */ 6647 Tcl_Obj *CONST objv[] /* Command arguments */ 6648 ){ 6649 char *zUser = 0; 6650 char *zPasswd = 0; 6651 int nPasswd = 0; 6652 int isAdmin = 0; 6653 sqlite3 *db; 6654 int rc; 6655 6656 if( objc!=5 ){ 6657 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); 6658 return TCL_ERROR; 6659 } 6660 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6661 return TCL_ERROR; 6662 } 6663 zUser = Tcl_GetString(objv[2]); 6664 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6665 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); 6666 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin); 6667 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6668 return TCL_OK; 6669 } 6670 #endif /* SQLITE_USER_AUTHENTICATION */ 6671 6672 #ifdef SQLITE_USER_AUTHENTICATION 6673 /* 6674 ** tclcmd: sqlite3_user_delete DB USERNAME 6675 */ 6676 static int test_user_delete( 6677 ClientData clientData, /* Unused */ 6678 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6679 int objc, /* Number of arguments */ 6680 Tcl_Obj *CONST objv[] /* Command arguments */ 6681 ){ 6682 char *zUser = 0; 6683 sqlite3 *db; 6684 int rc; 6685 6686 if( objc!=3 ){ 6687 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME"); 6688 return TCL_ERROR; 6689 } 6690 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6691 return TCL_ERROR; 6692 } 6693 zUser = Tcl_GetString(objv[2]); 6694 rc = sqlite3_user_delete(db, zUser); 6695 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6696 return TCL_OK; 6697 } 6698 #endif /* SQLITE_USER_AUTHENTICATION */ 6699 6700 /* 6701 ** tclcmd: bad_behavior TYPE 6702 ** 6703 ** Do some things that should trigger a valgrind or -fsanitize=undefined 6704 ** warning. This is used to verify that errors and warnings output by those 6705 ** tools are detected by the test scripts. 6706 ** 6707 ** TYPE BEHAVIOR 6708 ** 1 Overflow a signed integer 6709 ** 2 Jump based on an uninitialized variable 6710 ** 3 Read after free 6711 ** 4 Panic 6712 */ 6713 static int test_bad_behavior( 6714 ClientData clientData, /* Pointer to an integer containing zero */ 6715 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6716 int objc, /* Number of arguments */ 6717 Tcl_Obj *CONST objv[] /* Command arguments */ 6718 ){ 6719 int iType; 6720 int xyz; 6721 int i = *(int*)clientData; 6722 int j; 6723 int w[10]; 6724 int *a; 6725 if( objc!=2 ){ 6726 Tcl_WrongNumArgs(interp, 1, objv, "TYPE"); 6727 return TCL_ERROR; 6728 } 6729 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR; 6730 switch( iType ){ 6731 case 1: { 6732 xyz = 0x7fffff00 - i; 6733 xyz += 0x100; 6734 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz)); 6735 break; 6736 } 6737 case 2: { 6738 w[1] = 5; 6739 if( w[i]>0 ) w[1]++; 6740 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1])); 6741 break; 6742 } 6743 case 3: { 6744 a = malloc( sizeof(int)*10 ); 6745 for(j=0; j<10; j++) a[j] = j; 6746 free(a); 6747 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i])); 6748 break; 6749 } 6750 case 4: { 6751 Tcl_Panic("Deliberate panic"); 6752 break; 6753 } 6754 } 6755 return TCL_OK; 6756 } 6757 6758 /* 6759 ** tclcmd: register_dbstat_vtab DB 6760 ** 6761 ** Cause the dbstat virtual table to be available on the connection DB 6762 */ 6763 static int test_register_dbstat_vtab( 6764 void *clientData, 6765 Tcl_Interp *interp, 6766 int objc, 6767 Tcl_Obj *CONST objv[] 6768 ){ 6769 #ifdef SQLITE_OMIT_VIRTUALTABLE 6770 Tcl_AppendResult(interp, "dbstat not available because of " 6771 "SQLITE_OMIT_VIRTUALTABLE", (void*)0); 6772 return TCL_ERROR; 6773 #else 6774 struct SqliteDb { sqlite3 *db; }; 6775 char *zDb; 6776 Tcl_CmdInfo cmdInfo; 6777 6778 if( objc!=2 ){ 6779 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 6780 return TCL_ERROR; 6781 } 6782 6783 zDb = Tcl_GetString(objv[1]); 6784 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 6785 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 6786 sqlite3DbstatRegister(db); 6787 } 6788 return TCL_OK; 6789 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6790 } 6791 6792 /* 6793 ** Register commands with the TCL interpreter. 6794 */ 6795 int Sqlitetest1_Init(Tcl_Interp *interp){ 6796 extern int sqlite3_search_count; 6797 extern int sqlite3_found_count; 6798 extern int sqlite3_interrupt_count; 6799 extern int sqlite3_open_file_count; 6800 extern int sqlite3_sort_count; 6801 extern int sqlite3_current_time; 6802 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6803 extern int sqlite3_hostid_num; 6804 #endif 6805 extern int sqlite3_max_blobsize; 6806 extern int sqlite3BtreeSharedCacheReport(void*, 6807 Tcl_Interp*,int,Tcl_Obj*CONST*); 6808 static int iZero = 0; 6809 static struct { 6810 char *zName; 6811 Tcl_CmdProc *xProc; 6812 } aCmd[] = { 6813 { "db_enter", (Tcl_CmdProc*)db_enter }, 6814 { "db_leave", (Tcl_CmdProc*)db_leave }, 6815 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, 6816 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, 6817 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, 6818 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, 6819 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, 6820 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, 6821 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, 6822 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, 6823 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, 6824 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, 6825 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, 6826 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, 6827 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, 6828 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, 6829 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, 6830 { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, 6831 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, 6832 #ifndef SQLITE_OMIT_GET_TABLE 6833 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, 6834 #endif 6835 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, 6836 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, 6837 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, 6838 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, 6839 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, 6840 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, 6841 { "sqlite_bind", (Tcl_CmdProc*)test_bind }, 6842 { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, 6843 { "sqlite3_key", (Tcl_CmdProc*)test_key }, 6844 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, 6845 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, 6846 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, 6847 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, 6848 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, 6849 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, 6850 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, 6851 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, 6852 { "printf", (Tcl_CmdProc*)test_printf }, 6853 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, 6854 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address }, 6855 }; 6856 static struct { 6857 char *zName; 6858 Tcl_ObjCmdProc *xProc; 6859 void *clientData; 6860 } aObjCmd[] = { 6861 { "bad_behavior", test_bad_behavior, (void*)&iZero }, 6862 { "register_dbstat_vtab", test_register_dbstat_vtab }, 6863 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, 6864 { "sqlite3_bind_int", test_bind_int, 0 }, 6865 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, 6866 { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 }, 6867 { "sqlite3_bind_int64", test_bind_int64, 0 }, 6868 { "sqlite3_bind_double", test_bind_double, 0 }, 6869 { "sqlite3_bind_null", test_bind_null ,0 }, 6870 { "sqlite3_bind_text", test_bind_text ,0 }, 6871 { "sqlite3_bind_text16", test_bind_text16 ,0 }, 6872 { "sqlite3_bind_blob", test_bind_blob ,0 }, 6873 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, 6874 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, 6875 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, 6876 { "sqlite3_clear_bindings", test_clear_bindings, 0}, 6877 { "sqlite3_sleep", test_sleep, 0}, 6878 { "sqlite3_errcode", test_errcode ,0 }, 6879 { "sqlite3_extended_errcode", test_ex_errcode ,0 }, 6880 { "sqlite3_errmsg", test_errmsg ,0 }, 6881 { "sqlite3_errmsg16", test_errmsg16 ,0 }, 6882 { "sqlite3_open", test_open ,0 }, 6883 { "sqlite3_open16", test_open16 ,0 }, 6884 { "sqlite3_open_v2", test_open_v2 ,0 }, 6885 { "sqlite3_complete16", test_complete16 ,0 }, 6886 6887 { "sqlite3_prepare", test_prepare ,0 }, 6888 { "sqlite3_prepare16", test_prepare16 ,0 }, 6889 { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, 6890 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, 6891 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, 6892 { "sqlite3_finalize", test_finalize ,0 }, 6893 { "sqlite3_stmt_status", test_stmt_status ,0 }, 6894 { "sqlite3_reset", test_reset ,0 }, 6895 { "sqlite3_expired", test_expired ,0 }, 6896 { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, 6897 { "sqlite3_changes", test_changes ,0 }, 6898 { "sqlite3_step", test_step ,0 }, 6899 { "sqlite3_sql", test_sql ,0 }, 6900 { "sqlite3_next_stmt", test_next_stmt ,0 }, 6901 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, 6902 { "sqlite3_stmt_busy", test_stmt_busy ,0 }, 6903 { "uses_stmt_journal", uses_stmt_journal ,0 }, 6904 6905 { "sqlite3_release_memory", test_release_memory, 0}, 6906 { "sqlite3_db_release_memory", test_db_release_memory, 0}, 6907 { "sqlite3_db_cacheflush", test_db_cacheflush, 0}, 6908 { "sqlite3_db_filename", test_db_filename, 0}, 6909 { "sqlite3_db_readonly", test_db_readonly, 0}, 6910 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, 6911 { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, 6912 { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, 6913 6914 { "sqlite3_load_extension", test_load_extension, 0}, 6915 { "sqlite3_enable_load_extension", test_enable_load, 0}, 6916 { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, 6917 { "sqlite3_limit", test_limit, 0}, 6918 6919 { "save_prng_state", save_prng_state, 0 }, 6920 { "restore_prng_state", restore_prng_state, 0 }, 6921 { "reset_prng_state", reset_prng_state, 0 }, 6922 { "database_never_corrupt", database_never_corrupt, 0}, 6923 { "database_may_be_corrupt", database_may_be_corrupt, 0}, 6924 { "optimization_control", optimization_control,0}, 6925 #if SQLITE_OS_WIN 6926 { "lock_win32_file", win32_file_lock, 0 }, 6927 { "exists_win32_path", win32_exists_path, 0 }, 6928 { "find_win32_file", win32_find_file, 0 }, 6929 { "delete_win32_file", win32_delete_file, 0 }, 6930 { "make_win32_dir", win32_mkdir, 0 }, 6931 { "remove_win32_dir", win32_rmdir, 0 }, 6932 #endif 6933 { "tcl_objproc", runAsObjProc, 0 }, 6934 6935 /* sqlite3_column_*() API */ 6936 { "sqlite3_column_count", test_column_count ,0 }, 6937 { "sqlite3_data_count", test_data_count ,0 }, 6938 { "sqlite3_column_type", test_column_type ,0 }, 6939 { "sqlite3_column_blob", test_column_blob ,0 }, 6940 { "sqlite3_column_double", test_column_double ,0 }, 6941 { "sqlite3_column_int64", test_column_int64 ,0 }, 6942 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, 6943 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, 6944 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, 6945 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, 6946 #ifndef SQLITE_OMIT_DECLTYPE 6947 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, 6948 #endif 6949 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6950 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, 6951 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, 6952 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, 6953 #endif 6954 6955 #ifndef SQLITE_OMIT_UTF16 6956 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, 6957 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, 6958 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, 6959 { "add_alignment_test_collations", add_alignment_test_collations, 0 }, 6960 #ifndef SQLITE_OMIT_DECLTYPE 6961 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, 6962 #endif 6963 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6964 {"sqlite3_column_database_name16", 6965 test_stmt_utf16, (void*)sqlite3_column_database_name16}, 6966 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, 6967 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, 6968 #endif 6969 #endif 6970 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, 6971 { "sqlite3_global_recover", test_global_recover, 0 }, 6972 { "working_64bit_int", working_64bit_int, 0 }, 6973 { "vfs_unlink_test", vfs_unlink_test, 0 }, 6974 { "vfs_initfail_test", vfs_initfail_test, 0 }, 6975 { "vfs_unregister_all", vfs_unregister_all, 0 }, 6976 { "vfs_reregister_all", vfs_reregister_all, 0 }, 6977 { "file_control_test", file_control_test, 0 }, 6978 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, 6979 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, 6980 { "file_control_chunksize_test", file_control_chunksize_test, 0 }, 6981 { "file_control_sizehint_test", file_control_sizehint_test, 0 }, 6982 #if SQLITE_OS_WIN 6983 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, 6984 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 }, 6985 #endif 6986 { "file_control_persist_wal", file_control_persist_wal, 0 }, 6987 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, 6988 { "file_control_vfsname", file_control_vfsname, 0 }, 6989 { "file_control_tempfilename", file_control_tempfilename, 0 }, 6990 { "sqlite3_vfs_list", vfs_list, 0 }, 6991 { "sqlite3_create_function_v2", test_create_function_v2, 0 }, 6992 6993 /* Functions from os.h */ 6994 #ifndef SQLITE_OMIT_UTF16 6995 { "add_test_collate", test_collate, 0 }, 6996 { "add_test_collate_needed", test_collate_needed, 0 }, 6997 { "add_test_function", test_function, 0 }, 6998 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 }, 6999 #endif 7000 { "sqlite3_test_errstr", test_errstr, 0 }, 7001 { "tcl_variable_type", tcl_variable_type, 0 }, 7002 #ifndef SQLITE_OMIT_SHARED_CACHE 7003 { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, 7004 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, 7005 #endif 7006 { "sqlite3_libversion_number", test_libversion_number, 0 }, 7007 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, 7008 #ifndef SQLITE_OMIT_INCRBLOB 7009 { "sqlite3_blob_reopen", test_blob_reopen, 0 }, 7010 #endif 7011 { "pcache_stats", test_pcache_stats, 0 }, 7012 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 7013 { "sqlite3_unlock_notify", test_unlock_notify, 0 }, 7014 #endif 7015 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, 7016 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, 7017 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 }, 7018 { "test_sqlite3_log", test_sqlite3_log, 0 }, 7019 #ifndef SQLITE_OMIT_EXPLAIN 7020 { "print_explain_query_plan", test_print_eqp, 0 }, 7021 #endif 7022 { "sqlite3_test_control", test_test_control }, 7023 #if SQLITE_OS_UNIX 7024 { "getrusage", test_getrusage }, 7025 #endif 7026 { "load_static_extension", tclLoadStaticExtensionCmd }, 7027 { "sorter_test_fakeheap", sorter_test_fakeheap }, 7028 { "sorter_test_sort4_helper", sorter_test_sort4_helper }, 7029 #ifdef SQLITE_USER_AUTHENTICATION 7030 { "sqlite3_user_authenticate", test_user_authenticate, 0 }, 7031 { "sqlite3_user_add", test_user_add, 0 }, 7032 { "sqlite3_user_change", test_user_change, 0 }, 7033 { "sqlite3_user_delete", test_user_delete, 0 }, 7034 #endif 7035 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 7036 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 }, 7037 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 }, 7038 #endif 7039 7040 }; 7041 static int bitmask_size = sizeof(Bitmask)*8; 7042 int i; 7043 extern int sqlite3_sync_count, sqlite3_fullsync_count; 7044 extern int sqlite3_opentemp_count; 7045 extern int sqlite3_like_count; 7046 extern int sqlite3_xferopt_count; 7047 extern int sqlite3_pager_readdb_count; 7048 extern int sqlite3_pager_writedb_count; 7049 extern int sqlite3_pager_writej_count; 7050 #if SQLITE_OS_WIN 7051 extern LONG volatile sqlite3_os_type; 7052 #endif 7053 #ifdef SQLITE_DEBUG 7054 extern int sqlite3WhereTrace; 7055 extern int sqlite3OSTrace; 7056 extern int sqlite3WalTrace; 7057 #endif 7058 #ifdef SQLITE_TEST 7059 #ifdef SQLITE_ENABLE_FTS3 7060 extern int sqlite3_fts3_enable_parentheses; 7061 #endif 7062 #endif 7063 7064 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 7065 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 7066 } 7067 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 7068 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 7069 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 7070 } 7071 Tcl_LinkVar(interp, "sqlite_search_count", 7072 (char*)&sqlite3_search_count, TCL_LINK_INT); 7073 Tcl_LinkVar(interp, "sqlite_found_count", 7074 (char*)&sqlite3_found_count, TCL_LINK_INT); 7075 Tcl_LinkVar(interp, "sqlite_sort_count", 7076 (char*)&sqlite3_sort_count, TCL_LINK_INT); 7077 Tcl_LinkVar(interp, "sqlite3_max_blobsize", 7078 (char*)&sqlite3_max_blobsize, TCL_LINK_INT); 7079 Tcl_LinkVar(interp, "sqlite_like_count", 7080 (char*)&sqlite3_like_count, TCL_LINK_INT); 7081 Tcl_LinkVar(interp, "sqlite_interrupt_count", 7082 (char*)&sqlite3_interrupt_count, TCL_LINK_INT); 7083 Tcl_LinkVar(interp, "sqlite_open_file_count", 7084 (char*)&sqlite3_open_file_count, TCL_LINK_INT); 7085 Tcl_LinkVar(interp, "sqlite_current_time", 7086 (char*)&sqlite3_current_time, TCL_LINK_INT); 7087 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 7088 Tcl_LinkVar(interp, "sqlite_hostid_num", 7089 (char*)&sqlite3_hostid_num, TCL_LINK_INT); 7090 #endif 7091 Tcl_LinkVar(interp, "sqlite3_xferopt_count", 7092 (char*)&sqlite3_xferopt_count, TCL_LINK_INT); 7093 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", 7094 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); 7095 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", 7096 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); 7097 Tcl_LinkVar(interp, "sqlite3_pager_writej_count", 7098 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); 7099 #ifndef SQLITE_OMIT_UTF16 7100 Tcl_LinkVar(interp, "unaligned_string_counter", 7101 (char*)&unaligned_string_counter, TCL_LINK_INT); 7102 #endif 7103 #ifndef SQLITE_OMIT_UTF16 7104 Tcl_LinkVar(interp, "sqlite_last_needed_collation", 7105 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 7106 #endif 7107 #if SQLITE_OS_WIN 7108 Tcl_LinkVar(interp, "sqlite_os_type", 7109 (char*)&sqlite3_os_type, TCL_LINK_LONG); 7110 #endif 7111 #ifdef SQLITE_TEST 7112 { 7113 static const char *query_plan = "*** OBSOLETE VARIABLE ***"; 7114 Tcl_LinkVar(interp, "sqlite_query_plan", 7115 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 7116 } 7117 #endif 7118 #ifdef SQLITE_DEBUG 7119 Tcl_LinkVar(interp, "sqlite_where_trace", 7120 (char*)&sqlite3WhereTrace, TCL_LINK_INT); 7121 Tcl_LinkVar(interp, "sqlite_os_trace", 7122 (char*)&sqlite3OSTrace, TCL_LINK_INT); 7123 #ifndef SQLITE_OMIT_WAL 7124 Tcl_LinkVar(interp, "sqlite_wal_trace", 7125 (char*)&sqlite3WalTrace, TCL_LINK_INT); 7126 #endif 7127 #endif 7128 #ifndef SQLITE_OMIT_DISKIO 7129 Tcl_LinkVar(interp, "sqlite_opentemp_count", 7130 (char*)&sqlite3_opentemp_count, TCL_LINK_INT); 7131 #endif 7132 Tcl_LinkVar(interp, "sqlite_static_bind_value", 7133 (char*)&sqlite_static_bind_value, TCL_LINK_STRING); 7134 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", 7135 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); 7136 Tcl_LinkVar(interp, "sqlite_temp_directory", 7137 (char*)&sqlite3_temp_directory, TCL_LINK_STRING); 7138 Tcl_LinkVar(interp, "sqlite_data_directory", 7139 (char*)&sqlite3_data_directory, TCL_LINK_STRING); 7140 Tcl_LinkVar(interp, "bitmask_size", 7141 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); 7142 Tcl_LinkVar(interp, "sqlite_sync_count", 7143 (char*)&sqlite3_sync_count, TCL_LINK_INT); 7144 Tcl_LinkVar(interp, "sqlite_fullsync_count", 7145 (char*)&sqlite3_fullsync_count, TCL_LINK_INT); 7146 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) 7147 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", 7148 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); 7149 #endif 7150 return TCL_OK; 7151 } 7152