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 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 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 (void)zProc; 1935 (void)zFile; 1936 #else 1937 rc = sqlite3_load_extension(db, zFile, zProc, &zErr); 1938 #endif 1939 if( rc!=SQLITE_OK ){ 1940 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); 1941 rc = TCL_ERROR; 1942 }else{ 1943 rc = TCL_OK; 1944 } 1945 sqlite3_free(zErr); 1946 1947 return rc; 1948 } 1949 1950 /* 1951 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF 1952 */ 1953 static int test_enable_load( 1954 ClientData clientData, /* Not used */ 1955 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1956 int objc, /* Number of arguments */ 1957 Tcl_Obj *CONST objv[] /* Command arguments */ 1958 ){ 1959 Tcl_CmdInfo cmdInfo; 1960 sqlite3 *db; 1961 char *zDb; 1962 int onoff; 1963 1964 if( objc!=3 ){ 1965 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); 1966 return TCL_ERROR; 1967 } 1968 zDb = Tcl_GetString(objv[1]); 1969 1970 /* Extract the C database handle from the Tcl command name */ 1971 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 1972 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); 1973 return TCL_ERROR; 1974 } 1975 db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 1976 assert(db); 1977 1978 /* Get the onoff parameter */ 1979 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 1980 return TCL_ERROR; 1981 } 1982 1983 #ifdef SQLITE_OMIT_LOAD_EXTENSION 1984 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); 1985 return TCL_ERROR; 1986 #else 1987 sqlite3_enable_load_extension(db, onoff); 1988 return TCL_OK; 1989 #endif 1990 } 1991 1992 /* 1993 ** Usage: sqlite_abort 1994 ** 1995 ** Shutdown the process immediately. This is not a clean shutdown. 1996 ** This command is used to test the recoverability of a database in 1997 ** the event of a program crash. 1998 */ 1999 static int sqlite_abort( 2000 void *NotUsed, 2001 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2002 int argc, /* Number of arguments */ 2003 char **argv /* Text of each argument */ 2004 ){ 2005 #if defined(_MSC_VER) 2006 /* We do this, otherwise the test will halt with a popup message 2007 * that we have to click away before the test will continue. 2008 */ 2009 _set_abort_behavior( 0, _CALL_REPORTFAULT ); 2010 #endif 2011 exit(255); 2012 assert( interp==0 ); /* This will always fail */ 2013 return TCL_OK; 2014 } 2015 2016 /* 2017 ** The following routine is a user-defined SQL function whose purpose 2018 ** is to test the sqlite_set_result() API. 2019 */ 2020 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2021 while( argc>=2 ){ 2022 const char *zArg0 = (char*)sqlite3_value_text(argv[0]); 2023 if( zArg0 ){ 2024 if( 0==sqlite3StrICmp(zArg0, "int") ){ 2025 sqlite3_result_int(context, sqlite3_value_int(argv[1])); 2026 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ 2027 sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); 2028 }else if( sqlite3StrICmp(zArg0,"string")==0 ){ 2029 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, 2030 SQLITE_TRANSIENT); 2031 }else if( sqlite3StrICmp(zArg0,"double")==0 ){ 2032 sqlite3_result_double(context, sqlite3_value_double(argv[1])); 2033 }else if( sqlite3StrICmp(zArg0,"null")==0 ){ 2034 sqlite3_result_null(context); 2035 }else if( sqlite3StrICmp(zArg0,"value")==0 ){ 2036 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); 2037 }else{ 2038 goto error_out; 2039 } 2040 }else{ 2041 goto error_out; 2042 } 2043 argc -= 2; 2044 argv += 2; 2045 } 2046 return; 2047 2048 error_out: 2049 sqlite3_result_error(context,"first argument should be one of: " 2050 "int int64 string double null value", -1); 2051 } 2052 2053 /* 2054 ** Usage: sqlite_register_test_function DB NAME 2055 ** 2056 ** Register the test SQL function on the database DB under the name NAME. 2057 */ 2058 static int test_register_func( 2059 void *NotUsed, 2060 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2061 int argc, /* Number of arguments */ 2062 char **argv /* Text of each argument */ 2063 ){ 2064 sqlite3 *db; 2065 int rc; 2066 if( argc!=3 ){ 2067 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 2068 " DB FUNCTION-NAME", 0); 2069 return TCL_ERROR; 2070 } 2071 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 2072 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 2073 testFunc, 0, 0); 2074 if( rc!=0 ){ 2075 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); 2076 return TCL_ERROR; 2077 } 2078 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2079 return TCL_OK; 2080 } 2081 2082 /* 2083 ** Usage: sqlite3_finalize STMT 2084 ** 2085 ** Finalize a statement handle. 2086 */ 2087 static int test_finalize( 2088 void * clientData, 2089 Tcl_Interp *interp, 2090 int objc, 2091 Tcl_Obj *CONST objv[] 2092 ){ 2093 sqlite3_stmt *pStmt; 2094 int rc; 2095 sqlite3 *db = 0; 2096 2097 if( objc!=2 ){ 2098 Tcl_AppendResult(interp, "wrong # args: should be \"", 2099 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2100 return TCL_ERROR; 2101 } 2102 2103 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2104 2105 if( pStmt ){ 2106 db = StmtToDb(pStmt); 2107 } 2108 rc = sqlite3_finalize(pStmt); 2109 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 2110 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2111 return TCL_OK; 2112 } 2113 2114 /* 2115 ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG 2116 ** 2117 ** Get the value of a status counter from a statement. 2118 */ 2119 static int test_stmt_status( 2120 void * clientData, 2121 Tcl_Interp *interp, 2122 int objc, 2123 Tcl_Obj *CONST objv[] 2124 ){ 2125 int iValue; 2126 int i, op = 0, resetFlag; 2127 const char *zOpName; 2128 sqlite3_stmt *pStmt; 2129 2130 static const struct { 2131 const char *zName; 2132 int op; 2133 } aOp[] = { 2134 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP }, 2135 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT }, 2136 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX }, 2137 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP }, 2138 }; 2139 if( objc!=4 ){ 2140 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); 2141 return TCL_ERROR; 2142 } 2143 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2144 zOpName = Tcl_GetString(objv[2]); 2145 for(i=0; i<ArraySize(aOp); i++){ 2146 if( strcmp(aOp[i].zName, zOpName)==0 ){ 2147 op = aOp[i].op; 2148 break; 2149 } 2150 } 2151 if( i>=ArraySize(aOp) ){ 2152 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; 2153 } 2154 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; 2155 iValue = sqlite3_stmt_status(pStmt, op, resetFlag); 2156 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue)); 2157 return TCL_OK; 2158 } 2159 2160 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 2161 /* 2162 ** Usage: sqlite3_stmt_scanstatus STMT IDX 2163 */ 2164 static int test_stmt_scanstatus( 2165 void * clientData, 2166 Tcl_Interp *interp, 2167 int objc, 2168 Tcl_Obj *CONST objv[] 2169 ){ 2170 sqlite3_stmt *pStmt; /* First argument */ 2171 int idx; /* Second argument */ 2172 2173 const char *zName; 2174 const char *zExplain; 2175 sqlite3_int64 nLoop; 2176 sqlite3_int64 nVisit; 2177 double rEst; 2178 int res; 2179 2180 if( objc!=3 ){ 2181 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX"); 2182 return TCL_ERROR; 2183 } 2184 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2185 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2186 2187 res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop); 2188 if( res==0 ){ 2189 Tcl_Obj *pRet = Tcl_NewObj(); 2190 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1)); 2191 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop)); 2192 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); 2193 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1)); 2194 Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit)); 2195 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst); 2196 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1)); 2197 Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst)); 2198 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName); 2199 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1)); 2200 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1)); 2201 sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); 2202 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1)); 2203 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1)); 2204 Tcl_SetObjResult(interp, pRet); 2205 }else{ 2206 Tcl_ResetResult(interp); 2207 } 2208 return TCL_OK; 2209 } 2210 2211 /* 2212 ** Usage: sqlite3_stmt_scanstatus_reset STMT 2213 */ 2214 static int test_stmt_scanstatus_reset( 2215 void * clientData, 2216 Tcl_Interp *interp, 2217 int objc, 2218 Tcl_Obj *CONST objv[] 2219 ){ 2220 sqlite3_stmt *pStmt; /* First argument */ 2221 if( objc!=2 ){ 2222 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 2223 return TCL_ERROR; 2224 } 2225 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2226 sqlite3_stmt_scanstatus_reset(pStmt); 2227 return TCL_OK; 2228 } 2229 #endif 2230 2231 #ifdef SQLITE_ENABLE_SQLLOG 2232 /* 2233 ** Usage: sqlite3_config_sqllog 2234 ** 2235 ** Zero the SQLITE_CONFIG_SQLLOG configuration 2236 */ 2237 static int test_config_sqllog( 2238 void * clientData, 2239 Tcl_Interp *interp, 2240 int objc, 2241 Tcl_Obj *CONST objv[] 2242 ){ 2243 if( objc!=1 ){ 2244 Tcl_WrongNumArgs(interp, 1, objv, ""); 2245 return TCL_ERROR; 2246 } 2247 sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0); 2248 return TCL_OK; 2249 } 2250 #endif 2251 2252 /* 2253 ** Usage: vfs_current_time_int64 2254 ** 2255 ** Return the value returned by the default VFS's xCurrentTimeInt64 method. 2256 */ 2257 static int vfsCurrentTimeInt64( 2258 void * clientData, 2259 Tcl_Interp *interp, 2260 int objc, 2261 Tcl_Obj *CONST objv[] 2262 ){ 2263 i64 t; 2264 sqlite3_vfs *pVfs = sqlite3_vfs_find(0); 2265 if( objc!=1 ){ 2266 Tcl_WrongNumArgs(interp, 1, objv, ""); 2267 return TCL_ERROR; 2268 } 2269 pVfs->xCurrentTimeInt64(pVfs, &t); 2270 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t)); 2271 return TCL_OK; 2272 } 2273 2274 #ifdef SQLITE_ENABLE_SNAPSHOT 2275 /* 2276 ** Usage: sqlite3_snapshot_get DB DBNAME 2277 */ 2278 static int test_snapshot_get( 2279 void * clientData, 2280 Tcl_Interp *interp, 2281 int objc, 2282 Tcl_Obj *CONST objv[] 2283 ){ 2284 int rc; 2285 sqlite3 *db; 2286 char *zName; 2287 sqlite3_snapshot *pSnapshot = 0; 2288 2289 if( objc!=3 ){ 2290 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 2291 return TCL_ERROR; 2292 } 2293 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2294 zName = Tcl_GetString(objv[2]); 2295 2296 rc = sqlite3_snapshot_get(db, zName, &pSnapshot); 2297 if( rc!=SQLITE_OK ){ 2298 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); 2299 return TCL_ERROR; 2300 }else{ 2301 char zBuf[100]; 2302 if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR; 2303 Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1)); 2304 } 2305 return TCL_OK; 2306 } 2307 #endif /* SQLITE_ENABLE_SNAPSHOT */ 2308 2309 #ifdef SQLITE_ENABLE_SNAPSHOT 2310 /* 2311 ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT 2312 */ 2313 static int test_snapshot_open( 2314 void * clientData, 2315 Tcl_Interp *interp, 2316 int objc, 2317 Tcl_Obj *CONST objv[] 2318 ){ 2319 int rc; 2320 sqlite3 *db; 2321 char *zName; 2322 sqlite3_snapshot *pSnapshot; 2323 2324 if( objc!=4 ){ 2325 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT"); 2326 return TCL_ERROR; 2327 } 2328 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2329 zName = Tcl_GetString(objv[2]); 2330 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3])); 2331 2332 rc = sqlite3_snapshot_open(db, zName, pSnapshot); 2333 if( rc!=SQLITE_OK ){ 2334 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); 2335 return TCL_ERROR; 2336 } 2337 return TCL_OK; 2338 } 2339 #endif /* SQLITE_ENABLE_SNAPSHOT */ 2340 2341 #ifdef SQLITE_ENABLE_SNAPSHOT 2342 /* 2343 ** Usage: sqlite3_snapshot_free SNAPSHOT 2344 */ 2345 static int test_snapshot_free( 2346 void * clientData, 2347 Tcl_Interp *interp, 2348 int objc, 2349 Tcl_Obj *CONST objv[] 2350 ){ 2351 sqlite3_snapshot *pSnapshot; 2352 if( objc!=2 ){ 2353 Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT"); 2354 return TCL_ERROR; 2355 } 2356 pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 2357 sqlite3_snapshot_free(pSnapshot); 2358 return TCL_OK; 2359 } 2360 #endif /* SQLITE_ENABLE_SNAPSHOT */ 2361 2362 /* 2363 ** Usage: sqlite3_next_stmt DB STMT 2364 ** 2365 ** Return the next statment in sequence after STMT. 2366 */ 2367 static int test_next_stmt( 2368 void * clientData, 2369 Tcl_Interp *interp, 2370 int objc, 2371 Tcl_Obj *CONST objv[] 2372 ){ 2373 sqlite3_stmt *pStmt; 2374 sqlite3 *db = 0; 2375 char zBuf[50]; 2376 2377 if( objc!=3 ){ 2378 Tcl_AppendResult(interp, "wrong # args: should be \"", 2379 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); 2380 return TCL_ERROR; 2381 } 2382 2383 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2384 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; 2385 pStmt = sqlite3_next_stmt(db, pStmt); 2386 if( pStmt ){ 2387 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 2388 Tcl_AppendResult(interp, zBuf, 0); 2389 } 2390 return TCL_OK; 2391 } 2392 2393 /* 2394 ** Usage: sqlite3_stmt_readonly STMT 2395 ** 2396 ** Return true if STMT is a NULL pointer or a pointer to a statement 2397 ** that is guaranteed to leave the database unmodified. 2398 */ 2399 static int test_stmt_readonly( 2400 void * clientData, 2401 Tcl_Interp *interp, 2402 int objc, 2403 Tcl_Obj *CONST objv[] 2404 ){ 2405 sqlite3_stmt *pStmt; 2406 int rc; 2407 2408 if( objc!=2 ){ 2409 Tcl_AppendResult(interp, "wrong # args: should be \"", 2410 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2411 return TCL_ERROR; 2412 } 2413 2414 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2415 rc = sqlite3_stmt_readonly(pStmt); 2416 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); 2417 return TCL_OK; 2418 } 2419 2420 /* 2421 ** Usage: sqlite3_stmt_busy STMT 2422 ** 2423 ** Return true if STMT is a non-NULL pointer to a statement 2424 ** that has been stepped but not to completion. 2425 */ 2426 static int test_stmt_busy( 2427 void * clientData, 2428 Tcl_Interp *interp, 2429 int objc, 2430 Tcl_Obj *CONST objv[] 2431 ){ 2432 sqlite3_stmt *pStmt; 2433 int rc; 2434 2435 if( objc!=2 ){ 2436 Tcl_AppendResult(interp, "wrong # args: should be \"", 2437 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2438 return TCL_ERROR; 2439 } 2440 2441 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2442 rc = sqlite3_stmt_busy(pStmt); 2443 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); 2444 return TCL_OK; 2445 } 2446 2447 /* 2448 ** Usage: uses_stmt_journal STMT 2449 ** 2450 ** Return true if STMT uses a statement journal. 2451 */ 2452 static int uses_stmt_journal( 2453 void * clientData, 2454 Tcl_Interp *interp, 2455 int objc, 2456 Tcl_Obj *CONST objv[] 2457 ){ 2458 sqlite3_stmt *pStmt; 2459 2460 if( objc!=2 ){ 2461 Tcl_AppendResult(interp, "wrong # args: should be \"", 2462 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2463 return TCL_ERROR; 2464 } 2465 2466 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2467 sqlite3_stmt_readonly(pStmt); 2468 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal)); 2469 return TCL_OK; 2470 } 2471 2472 2473 /* 2474 ** Usage: sqlite3_reset STMT 2475 ** 2476 ** Reset a statement handle. 2477 */ 2478 static int test_reset( 2479 void * clientData, 2480 Tcl_Interp *interp, 2481 int objc, 2482 Tcl_Obj *CONST objv[] 2483 ){ 2484 sqlite3_stmt *pStmt; 2485 int rc; 2486 2487 if( objc!=2 ){ 2488 Tcl_AppendResult(interp, "wrong # args: should be \"", 2489 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2490 return TCL_ERROR; 2491 } 2492 2493 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2494 2495 rc = sqlite3_reset(pStmt); 2496 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ 2497 return TCL_ERROR; 2498 } 2499 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 2500 /* 2501 if( rc ){ 2502 return TCL_ERROR; 2503 } 2504 */ 2505 return TCL_OK; 2506 } 2507 2508 /* 2509 ** Usage: sqlite3_expired STMT 2510 ** 2511 ** Return TRUE if a recompilation of the statement is recommended. 2512 */ 2513 static int test_expired( 2514 void * clientData, 2515 Tcl_Interp *interp, 2516 int objc, 2517 Tcl_Obj *CONST objv[] 2518 ){ 2519 #ifndef SQLITE_OMIT_DEPRECATED 2520 sqlite3_stmt *pStmt; 2521 if( objc!=2 ){ 2522 Tcl_AppendResult(interp, "wrong # args: should be \"", 2523 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2524 return TCL_ERROR; 2525 } 2526 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2527 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); 2528 #endif 2529 return TCL_OK; 2530 } 2531 2532 /* 2533 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT 2534 ** 2535 ** Transfer all bindings from FROMSTMT over to TOSTMT 2536 */ 2537 static int test_transfer_bind( 2538 void * clientData, 2539 Tcl_Interp *interp, 2540 int objc, 2541 Tcl_Obj *CONST objv[] 2542 ){ 2543 #ifndef SQLITE_OMIT_DEPRECATED 2544 sqlite3_stmt *pStmt1, *pStmt2; 2545 if( objc!=3 ){ 2546 Tcl_AppendResult(interp, "wrong # args: should be \"", 2547 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); 2548 return TCL_ERROR; 2549 } 2550 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; 2551 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; 2552 Tcl_SetObjResult(interp, 2553 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); 2554 #endif 2555 return TCL_OK; 2556 } 2557 2558 /* 2559 ** Usage: sqlite3_changes DB 2560 ** 2561 ** Return the number of changes made to the database by the last SQL 2562 ** execution. 2563 */ 2564 static int test_changes( 2565 void * clientData, 2566 Tcl_Interp *interp, 2567 int objc, 2568 Tcl_Obj *CONST objv[] 2569 ){ 2570 sqlite3 *db; 2571 if( objc!=2 ){ 2572 Tcl_AppendResult(interp, "wrong # args: should be \"", 2573 Tcl_GetString(objv[0]), " DB", 0); 2574 return TCL_ERROR; 2575 } 2576 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2577 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); 2578 return TCL_OK; 2579 } 2580 2581 /* 2582 ** This is the "static_bind_value" that variables are bound to when 2583 ** the FLAG option of sqlite3_bind is "static" 2584 */ 2585 static char *sqlite_static_bind_value = 0; 2586 static int sqlite_static_bind_nbyte = 0; 2587 2588 /* 2589 ** Usage: sqlite3_bind VM IDX VALUE FLAGS 2590 ** 2591 ** Sets the value of the IDX-th occurrence of "?" in the original SQL 2592 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is 2593 ** ignored and the value is set to NULL. If FLAGS=="static" then 2594 ** the value is set to the value of a static variable named 2595 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy 2596 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored 2597 ** an a 10-byte blob "abc\000xyz\000pq" is inserted. 2598 */ 2599 static int test_bind( 2600 void *NotUsed, 2601 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2602 int argc, /* Number of arguments */ 2603 char **argv /* Text of each argument */ 2604 ){ 2605 sqlite3_stmt *pStmt; 2606 int rc; 2607 int idx; 2608 if( argc!=5 ){ 2609 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 2610 " VM IDX VALUE (null|static|normal)\"", 0); 2611 return TCL_ERROR; 2612 } 2613 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; 2614 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; 2615 if( strcmp(argv[4],"null")==0 ){ 2616 rc = sqlite3_bind_null(pStmt, idx); 2617 }else if( strcmp(argv[4],"static")==0 ){ 2618 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); 2619 }else if( strcmp(argv[4],"static-nbytes")==0 ){ 2620 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, 2621 sqlite_static_bind_nbyte, 0); 2622 }else if( strcmp(argv[4],"normal")==0 ){ 2623 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); 2624 }else if( strcmp(argv[4],"blob10")==0 ){ 2625 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); 2626 }else{ 2627 Tcl_AppendResult(interp, "4th argument should be " 2628 "\"null\" or \"static\" or \"normal\"", 0); 2629 return TCL_ERROR; 2630 } 2631 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2632 if( rc ){ 2633 char zBuf[50]; 2634 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 2635 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); 2636 return TCL_ERROR; 2637 } 2638 return TCL_OK; 2639 } 2640 2641 #ifndef SQLITE_OMIT_UTF16 2642 /* 2643 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> 2644 ** 2645 ** This function is used to test that SQLite selects the correct collation 2646 ** sequence callback when multiple versions (for different text encodings) 2647 ** are available. 2648 ** 2649 ** Calling this routine registers the collation sequence "test_collate" 2650 ** with database handle <db>. The second argument must be a list of three 2651 ** boolean values. If the first is true, then a version of test_collate is 2652 ** registered for UTF-8, if the second is true, a version is registered for 2653 ** UTF-16le, if the third is true, a UTF-16be version is available. 2654 ** Previous versions of test_collate are deleted. 2655 ** 2656 ** The collation sequence test_collate is implemented by calling the 2657 ** following TCL script: 2658 ** 2659 ** "test_collate <enc> <lhs> <rhs>" 2660 ** 2661 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. 2662 ** The <enc> parameter is the encoding of the collation function that 2663 ** SQLite selected to call. The TCL test script implements the 2664 ** "test_collate" proc. 2665 ** 2666 ** Note that this will only work with one interpreter at a time, as the 2667 ** interp pointer to use when evaluating the TCL script is stored in 2668 ** pTestCollateInterp. 2669 */ 2670 static Tcl_Interp* pTestCollateInterp; 2671 static int test_collate_func( 2672 void *pCtx, 2673 int nA, const void *zA, 2674 int nB, const void *zB 2675 ){ 2676 Tcl_Interp *i = pTestCollateInterp; 2677 int encin = SQLITE_PTR_TO_INT(pCtx); 2678 int res; 2679 int n; 2680 2681 sqlite3_value *pVal; 2682 Tcl_Obj *pX; 2683 2684 pX = Tcl_NewStringObj("test_collate", -1); 2685 Tcl_IncrRefCount(pX); 2686 2687 switch( encin ){ 2688 case SQLITE_UTF8: 2689 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); 2690 break; 2691 case SQLITE_UTF16LE: 2692 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); 2693 break; 2694 case SQLITE_UTF16BE: 2695 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); 2696 break; 2697 default: 2698 assert(0); 2699 } 2700 2701 sqlite3BeginBenignMalloc(); 2702 pVal = sqlite3ValueNew(0); 2703 if( pVal ){ 2704 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); 2705 n = sqlite3_value_bytes(pVal); 2706 Tcl_ListObjAppendElement(i,pX, 2707 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2708 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); 2709 n = sqlite3_value_bytes(pVal); 2710 Tcl_ListObjAppendElement(i,pX, 2711 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2712 sqlite3ValueFree(pVal); 2713 } 2714 sqlite3EndBenignMalloc(); 2715 2716 Tcl_EvalObjEx(i, pX, 0); 2717 Tcl_DecrRefCount(pX); 2718 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); 2719 return res; 2720 } 2721 static int test_collate( 2722 void * clientData, 2723 Tcl_Interp *interp, 2724 int objc, 2725 Tcl_Obj *CONST objv[] 2726 ){ 2727 sqlite3 *db; 2728 int val; 2729 sqlite3_value *pVal; 2730 int rc; 2731 2732 if( objc!=5 ) goto bad_args; 2733 pTestCollateInterp = interp; 2734 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2735 2736 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2737 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, 2738 (void *)SQLITE_UTF8, val?test_collate_func:0); 2739 if( rc==SQLITE_OK ){ 2740 const void *zUtf16; 2741 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2742 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, 2743 (void *)SQLITE_UTF16LE, val?test_collate_func:0); 2744 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2745 2746 #if 0 2747 if( sqlite3_iMallocFail>0 ){ 2748 sqlite3_iMallocFail++; 2749 } 2750 #endif 2751 sqlite3_mutex_enter(db->mutex); 2752 pVal = sqlite3ValueNew(db); 2753 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); 2754 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 2755 if( db->mallocFailed ){ 2756 rc = SQLITE_NOMEM; 2757 }else{ 2758 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, 2759 (void *)SQLITE_UTF16BE, val?test_collate_func:0); 2760 } 2761 sqlite3ValueFree(pVal); 2762 sqlite3_mutex_leave(db->mutex); 2763 } 2764 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2765 2766 if( rc!=SQLITE_OK ){ 2767 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 2768 return TCL_ERROR; 2769 } 2770 return TCL_OK; 2771 2772 bad_args: 2773 Tcl_AppendResult(interp, "wrong # args: should be \"", 2774 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2775 return TCL_ERROR; 2776 } 2777 2778 /* 2779 ** Usage: add_test_utf16bin_collate <db ptr> 2780 ** 2781 ** Add a utf-16 collation sequence named "utf16bin" to the database 2782 ** handle. This collation sequence compares arguments in the same way as the 2783 ** built-in collation "binary". 2784 */ 2785 static int test_utf16bin_collate_func( 2786 void *pCtx, 2787 int nA, const void *zA, 2788 int nB, const void *zB 2789 ){ 2790 int nCmp = (nA>nB ? nB : nA); 2791 int res = memcmp(zA, zB, nCmp); 2792 if( res==0 ) res = nA - nB; 2793 return res; 2794 } 2795 static int test_utf16bin_collate( 2796 void * clientData, 2797 Tcl_Interp *interp, 2798 int objc, 2799 Tcl_Obj *CONST objv[] 2800 ){ 2801 sqlite3 *db; 2802 int rc; 2803 2804 if( objc!=2 ) goto bad_args; 2805 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2806 2807 rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0, 2808 test_utf16bin_collate_func 2809 ); 2810 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2811 return TCL_OK; 2812 2813 bad_args: 2814 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 2815 return TCL_ERROR; 2816 } 2817 2818 /* 2819 ** When the collation needed callback is invoked, record the name of 2820 ** the requested collating function here. The recorded name is linked 2821 ** to a TCL variable and used to make sure that the requested collation 2822 ** name is correct. 2823 */ 2824 static char zNeededCollation[200]; 2825 static char *pzNeededCollation = zNeededCollation; 2826 2827 2828 /* 2829 ** Called when a collating sequence is needed. Registered using 2830 ** sqlite3_collation_needed16(). 2831 */ 2832 static void test_collate_needed_cb( 2833 void *pCtx, 2834 sqlite3 *db, 2835 int eTextRep, 2836 const void *pName 2837 ){ 2838 int enc = ENC(db); 2839 int i; 2840 char *z; 2841 for(z = (char*)pName, i=0; *z || z[1]; z++){ 2842 if( *z ) zNeededCollation[i++] = *z; 2843 } 2844 zNeededCollation[i] = 0; 2845 sqlite3_create_collation( 2846 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func); 2847 } 2848 2849 /* 2850 ** Usage: add_test_collate_needed DB 2851 */ 2852 static int test_collate_needed( 2853 void * clientData, 2854 Tcl_Interp *interp, 2855 int objc, 2856 Tcl_Obj *CONST objv[] 2857 ){ 2858 sqlite3 *db; 2859 int rc; 2860 2861 if( objc!=2 ) goto bad_args; 2862 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2863 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); 2864 zNeededCollation[0] = 0; 2865 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2866 return TCL_OK; 2867 2868 bad_args: 2869 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 2870 return TCL_ERROR; 2871 } 2872 2873 /* 2874 ** tclcmd: add_alignment_test_collations DB 2875 ** 2876 ** Add two new collating sequences to the database DB 2877 ** 2878 ** utf16_aligned 2879 ** utf16_unaligned 2880 ** 2881 ** Both collating sequences use the same sort order as BINARY. 2882 ** The only difference is that the utf16_aligned collating 2883 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. 2884 ** Both collating functions increment the unaligned utf16 counter 2885 ** whenever they see a string that begins on an odd byte boundary. 2886 */ 2887 static int unaligned_string_counter = 0; 2888 static int alignmentCollFunc( 2889 void *NotUsed, 2890 int nKey1, const void *pKey1, 2891 int nKey2, const void *pKey2 2892 ){ 2893 int rc, n; 2894 n = nKey1<nKey2 ? nKey1 : nKey2; 2895 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++; 2896 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++; 2897 rc = memcmp(pKey1, pKey2, n); 2898 if( rc==0 ){ 2899 rc = nKey1 - nKey2; 2900 } 2901 return rc; 2902 } 2903 static int add_alignment_test_collations( 2904 void * clientData, 2905 Tcl_Interp *interp, 2906 int objc, 2907 Tcl_Obj *CONST objv[] 2908 ){ 2909 sqlite3 *db; 2910 if( objc>=2 ){ 2911 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2912 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, 2913 0, alignmentCollFunc); 2914 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED, 2915 0, alignmentCollFunc); 2916 } 2917 return SQLITE_OK; 2918 } 2919 #endif /* !defined(SQLITE_OMIT_UTF16) */ 2920 2921 /* 2922 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> 2923 ** 2924 ** This function is used to test that SQLite selects the correct user 2925 ** function callback when multiple versions (for different text encodings) 2926 ** are available. 2927 ** 2928 ** Calling this routine registers up to three versions of the user function 2929 ** "test_function" with database handle <db>. If the second argument is 2930 ** true, then a version of test_function is registered for UTF-8, if the 2931 ** third is true, a version is registered for UTF-16le, if the fourth is 2932 ** true, a UTF-16be version is available. Previous versions of 2933 ** test_function are deleted. 2934 ** 2935 ** The user function is implemented by calling the following TCL script: 2936 ** 2937 ** "test_function <enc> <arg>" 2938 ** 2939 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the 2940 ** single argument passed to the SQL function. The value returned by 2941 ** the TCL script is used as the return value of the SQL function. It 2942 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 2943 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that 2944 ** prefers UTF-16BE. 2945 */ 2946 #ifndef SQLITE_OMIT_UTF16 2947 static void test_function_utf8( 2948 sqlite3_context *pCtx, 2949 int nArg, 2950 sqlite3_value **argv 2951 ){ 2952 Tcl_Interp *interp; 2953 Tcl_Obj *pX; 2954 sqlite3_value *pVal; 2955 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2956 pX = Tcl_NewStringObj("test_function", -1); 2957 Tcl_IncrRefCount(pX); 2958 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); 2959 Tcl_ListObjAppendElement(interp, pX, 2960 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2961 Tcl_EvalObjEx(interp, pX, 0); 2962 Tcl_DecrRefCount(pX); 2963 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); 2964 pVal = sqlite3ValueNew(0); 2965 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2966 SQLITE_UTF8, SQLITE_STATIC); 2967 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), 2968 -1, SQLITE_TRANSIENT); 2969 sqlite3ValueFree(pVal); 2970 } 2971 static void test_function_utf16le( 2972 sqlite3_context *pCtx, 2973 int nArg, 2974 sqlite3_value **argv 2975 ){ 2976 Tcl_Interp *interp; 2977 Tcl_Obj *pX; 2978 sqlite3_value *pVal; 2979 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2980 pX = Tcl_NewStringObj("test_function", -1); 2981 Tcl_IncrRefCount(pX); 2982 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); 2983 Tcl_ListObjAppendElement(interp, pX, 2984 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2985 Tcl_EvalObjEx(interp, pX, 0); 2986 Tcl_DecrRefCount(pX); 2987 pVal = sqlite3ValueNew(0); 2988 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2989 SQLITE_UTF8, SQLITE_STATIC); 2990 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); 2991 sqlite3ValueFree(pVal); 2992 } 2993 static void test_function_utf16be( 2994 sqlite3_context *pCtx, 2995 int nArg, 2996 sqlite3_value **argv 2997 ){ 2998 Tcl_Interp *interp; 2999 Tcl_Obj *pX; 3000 sqlite3_value *pVal; 3001 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 3002 pX = Tcl_NewStringObj("test_function", -1); 3003 Tcl_IncrRefCount(pX); 3004 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); 3005 Tcl_ListObjAppendElement(interp, pX, 3006 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 3007 Tcl_EvalObjEx(interp, pX, 0); 3008 Tcl_DecrRefCount(pX); 3009 pVal = sqlite3ValueNew(0); 3010 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 3011 SQLITE_UTF8, SQLITE_STATIC); 3012 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), 3013 -1, SQLITE_TRANSIENT); 3014 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), 3015 -1, SQLITE_TRANSIENT); 3016 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), 3017 -1, SQLITE_TRANSIENT); 3018 sqlite3ValueFree(pVal); 3019 } 3020 #endif /* SQLITE_OMIT_UTF16 */ 3021 static int test_function( 3022 void * clientData, 3023 Tcl_Interp *interp, 3024 int objc, 3025 Tcl_Obj *CONST objv[] 3026 ){ 3027 #ifndef SQLITE_OMIT_UTF16 3028 sqlite3 *db; 3029 int val; 3030 3031 if( objc!=5 ) goto bad_args; 3032 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3033 3034 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 3035 if( val ){ 3036 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, 3037 interp, test_function_utf8, 0, 0); 3038 } 3039 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 3040 if( val ){ 3041 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, 3042 interp, test_function_utf16le, 0, 0); 3043 } 3044 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 3045 if( val ){ 3046 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, 3047 interp, test_function_utf16be, 0, 0); 3048 } 3049 3050 return TCL_OK; 3051 bad_args: 3052 Tcl_AppendResult(interp, "wrong # args: should be \"", 3053 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 3054 #endif /* SQLITE_OMIT_UTF16 */ 3055 return TCL_ERROR; 3056 } 3057 3058 /* 3059 ** Usage: sqlite3_test_errstr <err code> 3060 ** 3061 ** Test that the english language string equivalents for sqlite error codes 3062 ** are sane. The parameter is an integer representing an sqlite error code. 3063 ** The result is a list of two elements, the string representation of the 3064 ** error code and the english language explanation. 3065 */ 3066 static int test_errstr( 3067 void * clientData, 3068 Tcl_Interp *interp, 3069 int objc, 3070 Tcl_Obj *CONST objv[] 3071 ){ 3072 char *zCode; 3073 int i; 3074 if( objc!=1 ){ 3075 Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); 3076 } 3077 3078 zCode = Tcl_GetString(objv[1]); 3079 for(i=0; i<200; i++){ 3080 if( 0==strcmp(t1ErrorName(i), zCode) ) break; 3081 } 3082 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); 3083 return TCL_OK; 3084 } 3085 3086 /* 3087 ** Usage: breakpoint 3088 ** 3089 ** This routine exists for one purpose - to provide a place to put a 3090 ** breakpoint with GDB that can be triggered using TCL code. The use 3091 ** for this is when a particular test fails on (say) the 1485th iteration. 3092 ** In the TCL test script, we can add code like this: 3093 ** 3094 ** if {$i==1485} breakpoint 3095 ** 3096 ** Then run testfixture in the debugger and wait for the breakpoint to 3097 ** fire. Then additional breakpoints can be set to trace down the bug. 3098 */ 3099 static int test_breakpoint( 3100 void *NotUsed, 3101 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 3102 int argc, /* Number of arguments */ 3103 char **argv /* Text of each argument */ 3104 ){ 3105 return TCL_OK; /* Do nothing */ 3106 } 3107 3108 /* 3109 ** Usage: sqlite3_bind_zeroblob STMT IDX N 3110 ** 3111 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. 3112 ** IDX is the index of a wildcard in the prepared statement. This command 3113 ** binds a N-byte zero-filled BLOB to the wildcard. 3114 */ 3115 static int test_bind_zeroblob( 3116 void * clientData, 3117 Tcl_Interp *interp, 3118 int objc, 3119 Tcl_Obj *CONST objv[] 3120 ){ 3121 sqlite3_stmt *pStmt; 3122 int idx; 3123 int n; 3124 int rc; 3125 3126 if( objc!=4 ){ 3127 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); 3128 return TCL_ERROR; 3129 } 3130 3131 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3132 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3133 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; 3134 3135 rc = sqlite3_bind_zeroblob(pStmt, idx, n); 3136 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3137 if( rc!=SQLITE_OK ){ 3138 return TCL_ERROR; 3139 } 3140 3141 return TCL_OK; 3142 } 3143 3144 /* 3145 ** Usage: sqlite3_bind_zeroblob64 STMT IDX N 3146 ** 3147 ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement. 3148 ** IDX is the index of a wildcard in the prepared statement. This command 3149 ** binds a N-byte zero-filled BLOB to the wildcard. 3150 */ 3151 static int test_bind_zeroblob64( 3152 void * clientData, 3153 Tcl_Interp *interp, 3154 int objc, 3155 Tcl_Obj *CONST objv[] 3156 ){ 3157 sqlite3_stmt *pStmt; 3158 int idx; 3159 Tcl_WideInt n; 3160 int rc; 3161 3162 if( objc!=4 ){ 3163 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); 3164 return TCL_ERROR; 3165 } 3166 3167 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3168 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3169 if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; 3170 3171 rc = sqlite3_bind_zeroblob64(pStmt, idx, n); 3172 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3173 if( rc!=SQLITE_OK ){ 3174 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3175 return TCL_ERROR; 3176 } 3177 3178 return TCL_OK; 3179 } 3180 3181 /* 3182 ** Usage: sqlite3_bind_int STMT N VALUE 3183 ** 3184 ** Test the sqlite3_bind_int interface. STMT is a prepared statement. 3185 ** N is the index of a wildcard in the prepared statement. This command 3186 ** binds a 32-bit integer VALUE to that wildcard. 3187 */ 3188 static int test_bind_int( 3189 void * clientData, 3190 Tcl_Interp *interp, 3191 int objc, 3192 Tcl_Obj *CONST objv[] 3193 ){ 3194 sqlite3_stmt *pStmt; 3195 int idx; 3196 int value; 3197 int rc; 3198 3199 if( objc!=4 ){ 3200 Tcl_AppendResult(interp, "wrong # args: should be \"", 3201 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3202 return TCL_ERROR; 3203 } 3204 3205 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3206 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3207 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3208 3209 rc = sqlite3_bind_int(pStmt, idx, value); 3210 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3211 if( rc!=SQLITE_OK ){ 3212 return TCL_ERROR; 3213 } 3214 3215 return TCL_OK; 3216 } 3217 3218 3219 /* 3220 ** Usage: sqlite3_bind_int64 STMT N VALUE 3221 ** 3222 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. 3223 ** N is the index of a wildcard in the prepared statement. This command 3224 ** binds a 64-bit integer VALUE to that wildcard. 3225 */ 3226 static int test_bind_int64( 3227 void * clientData, 3228 Tcl_Interp *interp, 3229 int objc, 3230 Tcl_Obj *CONST objv[] 3231 ){ 3232 sqlite3_stmt *pStmt; 3233 int idx; 3234 Tcl_WideInt value; 3235 int rc; 3236 3237 if( objc!=4 ){ 3238 Tcl_AppendResult(interp, "wrong # args: should be \"", 3239 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3240 return TCL_ERROR; 3241 } 3242 3243 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3244 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3245 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3246 3247 rc = sqlite3_bind_int64(pStmt, idx, value); 3248 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3249 if( rc!=SQLITE_OK ){ 3250 return TCL_ERROR; 3251 } 3252 3253 return TCL_OK; 3254 } 3255 3256 3257 /* 3258 ** Usage: sqlite3_bind_double STMT N VALUE 3259 ** 3260 ** Test the sqlite3_bind_double interface. STMT is a prepared statement. 3261 ** N is the index of a wildcard in the prepared statement. This command 3262 ** binds a 64-bit integer VALUE to that wildcard. 3263 */ 3264 static int test_bind_double( 3265 void * clientData, 3266 Tcl_Interp *interp, 3267 int objc, 3268 Tcl_Obj *CONST objv[] 3269 ){ 3270 sqlite3_stmt *pStmt; 3271 int idx; 3272 double value = 0; 3273 int rc; 3274 const char *zVal; 3275 int i; 3276 static const struct { 3277 const char *zName; /* Name of the special floating point value */ 3278 unsigned int iUpper; /* Upper 32 bits */ 3279 unsigned int iLower; /* Lower 32 bits */ 3280 } aSpecialFp[] = { 3281 { "NaN", 0x7fffffff, 0xffffffff }, 3282 { "SNaN", 0x7ff7ffff, 0xffffffff }, 3283 { "-NaN", 0xffffffff, 0xffffffff }, 3284 { "-SNaN", 0xfff7ffff, 0xffffffff }, 3285 { "+Inf", 0x7ff00000, 0x00000000 }, 3286 { "-Inf", 0xfff00000, 0x00000000 }, 3287 { "Epsilon", 0x00000000, 0x00000001 }, 3288 { "-Epsilon", 0x80000000, 0x00000001 }, 3289 { "NaN0", 0x7ff80000, 0x00000000 }, 3290 { "-NaN0", 0xfff80000, 0x00000000 }, 3291 }; 3292 3293 if( objc!=4 ){ 3294 Tcl_AppendResult(interp, "wrong # args: should be \"", 3295 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3296 return TCL_ERROR; 3297 } 3298 3299 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3300 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3301 3302 /* Intercept the string "NaN" and generate a NaN value for it. 3303 ** All other strings are passed through to Tcl_GetDoubleFromObj(). 3304 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions 3305 ** contain a bug. 3306 */ 3307 zVal = Tcl_GetString(objv[3]); 3308 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ 3309 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ 3310 sqlite3_uint64 x; 3311 x = aSpecialFp[i].iUpper; 3312 x <<= 32; 3313 x |= aSpecialFp[i].iLower; 3314 assert( sizeof(value)==8 ); 3315 assert( sizeof(x)==8 ); 3316 memcpy(&value, &x, 8); 3317 break; 3318 } 3319 } 3320 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && 3321 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ 3322 return TCL_ERROR; 3323 } 3324 rc = sqlite3_bind_double(pStmt, idx, value); 3325 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3326 if( rc!=SQLITE_OK ){ 3327 return TCL_ERROR; 3328 } 3329 3330 return TCL_OK; 3331 } 3332 3333 /* 3334 ** Usage: sqlite3_bind_null STMT N 3335 ** 3336 ** Test the sqlite3_bind_null interface. STMT is a prepared statement. 3337 ** N is the index of a wildcard in the prepared statement. This command 3338 ** binds a NULL to the wildcard. 3339 */ 3340 static int test_bind_null( 3341 void * clientData, 3342 Tcl_Interp *interp, 3343 int objc, 3344 Tcl_Obj *CONST objv[] 3345 ){ 3346 sqlite3_stmt *pStmt; 3347 int idx; 3348 int rc; 3349 3350 if( objc!=3 ){ 3351 Tcl_AppendResult(interp, "wrong # args: should be \"", 3352 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); 3353 return TCL_ERROR; 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 3359 rc = sqlite3_bind_null(pStmt, idx); 3360 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3361 if( rc!=SQLITE_OK ){ 3362 return TCL_ERROR; 3363 } 3364 3365 return TCL_OK; 3366 } 3367 3368 /* 3369 ** Usage: sqlite3_bind_text STMT N STRING BYTES 3370 ** 3371 ** Test the sqlite3_bind_text interface. STMT is a prepared statement. 3372 ** N is the index of a wildcard in the prepared statement. This command 3373 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes 3374 ** long. 3375 */ 3376 static int test_bind_text( 3377 void * clientData, 3378 Tcl_Interp *interp, 3379 int objc, 3380 Tcl_Obj *CONST objv[] 3381 ){ 3382 sqlite3_stmt *pStmt; 3383 int idx; 3384 int bytes; 3385 char *value; 3386 int rc; 3387 3388 if( objc!=5 ){ 3389 Tcl_AppendResult(interp, "wrong # args: should be \"", 3390 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3391 return TCL_ERROR; 3392 } 3393 3394 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3395 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3396 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); 3397 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3398 3399 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); 3400 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3401 if( rc!=SQLITE_OK ){ 3402 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3403 return TCL_ERROR; 3404 } 3405 3406 return TCL_OK; 3407 } 3408 3409 /* 3410 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES 3411 ** 3412 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. 3413 ** N is the index of a wildcard in the prepared statement. This command 3414 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes 3415 ** long. 3416 */ 3417 static int test_bind_text16( 3418 void * clientData, 3419 Tcl_Interp *interp, 3420 int objc, 3421 Tcl_Obj *CONST objv[] 3422 ){ 3423 #ifndef SQLITE_OMIT_UTF16 3424 sqlite3_stmt *pStmt; 3425 int idx; 3426 int bytes; 3427 char *value; 3428 int rc; 3429 3430 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); 3431 Tcl_Obj *oStmt = objv[objc-4]; 3432 Tcl_Obj *oN = objv[objc-3]; 3433 Tcl_Obj *oString = objv[objc-2]; 3434 Tcl_Obj *oBytes = objv[objc-1]; 3435 3436 if( objc!=5 && objc!=6){ 3437 Tcl_AppendResult(interp, "wrong # args: should be \"", 3438 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3439 return TCL_ERROR; 3440 } 3441 3442 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; 3443 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; 3444 value = (char*)Tcl_GetByteArrayFromObj(oString, 0); 3445 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; 3446 3447 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); 3448 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3449 if( rc!=SQLITE_OK ){ 3450 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3451 return TCL_ERROR; 3452 } 3453 3454 #endif /* SQLITE_OMIT_UTF16 */ 3455 return TCL_OK; 3456 } 3457 3458 /* 3459 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES 3460 ** 3461 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. 3462 ** N is the index of a wildcard in the prepared statement. This command 3463 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. 3464 */ 3465 static int test_bind_blob( 3466 void * clientData, 3467 Tcl_Interp *interp, 3468 int objc, 3469 Tcl_Obj *CONST objv[] 3470 ){ 3471 sqlite3_stmt *pStmt; 3472 int idx; 3473 int bytes; 3474 char *value; 3475 int rc; 3476 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; 3477 3478 if( objc!=5 && objc!=6 ){ 3479 Tcl_AppendResult(interp, "wrong # args: should be \"", 3480 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); 3481 return TCL_ERROR; 3482 } 3483 3484 if( objc==6 ){ 3485 xDestructor = SQLITE_STATIC; 3486 objv++; 3487 } 3488 3489 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3490 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3491 value = Tcl_GetString(objv[3]); 3492 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3493 3494 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); 3495 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3496 if( rc!=SQLITE_OK ){ 3497 return TCL_ERROR; 3498 } 3499 3500 return TCL_OK; 3501 } 3502 3503 /* 3504 ** Usage: sqlite3_bind_parameter_count STMT 3505 ** 3506 ** Return the number of wildcards in the given statement. 3507 */ 3508 static int test_bind_parameter_count( 3509 void * clientData, 3510 Tcl_Interp *interp, 3511 int objc, 3512 Tcl_Obj *CONST objv[] 3513 ){ 3514 sqlite3_stmt *pStmt; 3515 3516 if( objc!=2 ){ 3517 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3518 return TCL_ERROR; 3519 } 3520 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3521 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); 3522 return TCL_OK; 3523 } 3524 3525 /* 3526 ** Usage: sqlite3_bind_parameter_name STMT N 3527 ** 3528 ** Return the name of the Nth wildcard. The first wildcard is 1. 3529 ** An empty string is returned if N is out of range or if the wildcard 3530 ** is nameless. 3531 */ 3532 static int test_bind_parameter_name( 3533 void * clientData, 3534 Tcl_Interp *interp, 3535 int objc, 3536 Tcl_Obj *CONST objv[] 3537 ){ 3538 sqlite3_stmt *pStmt; 3539 int i; 3540 3541 if( objc!=3 ){ 3542 Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); 3543 return TCL_ERROR; 3544 } 3545 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3546 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; 3547 Tcl_SetObjResult(interp, 3548 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) 3549 ); 3550 return TCL_OK; 3551 } 3552 3553 /* 3554 ** Usage: sqlite3_bind_parameter_index STMT NAME 3555 ** 3556 ** Return the index of the wildcard called NAME. Return 0 if there is 3557 ** no such wildcard. 3558 */ 3559 static int test_bind_parameter_index( 3560 void * clientData, 3561 Tcl_Interp *interp, 3562 int objc, 3563 Tcl_Obj *CONST objv[] 3564 ){ 3565 sqlite3_stmt *pStmt; 3566 3567 if( objc!=3 ){ 3568 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); 3569 return TCL_ERROR; 3570 } 3571 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3572 Tcl_SetObjResult(interp, 3573 Tcl_NewIntObj( 3574 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) 3575 ) 3576 ); 3577 return TCL_OK; 3578 } 3579 3580 /* 3581 ** Usage: sqlite3_clear_bindings STMT 3582 ** 3583 */ 3584 static int test_clear_bindings( 3585 void * clientData, 3586 Tcl_Interp *interp, 3587 int objc, 3588 Tcl_Obj *CONST objv[] 3589 ){ 3590 sqlite3_stmt *pStmt; 3591 3592 if( objc!=2 ){ 3593 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3594 return TCL_ERROR; 3595 } 3596 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3597 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); 3598 return TCL_OK; 3599 } 3600 3601 /* 3602 ** Usage: sqlite3_sleep MILLISECONDS 3603 */ 3604 static int test_sleep( 3605 void * clientData, 3606 Tcl_Interp *interp, 3607 int objc, 3608 Tcl_Obj *CONST objv[] 3609 ){ 3610 int ms; 3611 3612 if( objc!=2 ){ 3613 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); 3614 return TCL_ERROR; 3615 } 3616 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ 3617 return TCL_ERROR; 3618 } 3619 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); 3620 return TCL_OK; 3621 } 3622 3623 /* 3624 ** Usage: sqlite3_extended_errcode DB 3625 ** 3626 ** Return the string representation of the most recent sqlite3_* API 3627 ** error code. e.g. "SQLITE_ERROR". 3628 */ 3629 static int test_ex_errcode( 3630 void * clientData, 3631 Tcl_Interp *interp, 3632 int objc, 3633 Tcl_Obj *CONST objv[] 3634 ){ 3635 sqlite3 *db; 3636 int rc; 3637 3638 if( objc!=2 ){ 3639 Tcl_AppendResult(interp, "wrong # args: should be \"", 3640 Tcl_GetString(objv[0]), " DB", 0); 3641 return TCL_ERROR; 3642 } 3643 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3644 rc = sqlite3_extended_errcode(db); 3645 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3646 return TCL_OK; 3647 } 3648 3649 3650 /* 3651 ** Usage: sqlite3_errcode DB 3652 ** 3653 ** Return the string representation of the most recent sqlite3_* API 3654 ** error code. e.g. "SQLITE_ERROR". 3655 */ 3656 static int test_errcode( 3657 void * clientData, 3658 Tcl_Interp *interp, 3659 int objc, 3660 Tcl_Obj *CONST objv[] 3661 ){ 3662 sqlite3 *db; 3663 int rc; 3664 3665 if( objc!=2 ){ 3666 Tcl_AppendResult(interp, "wrong # args: should be \"", 3667 Tcl_GetString(objv[0]), " DB", 0); 3668 return TCL_ERROR; 3669 } 3670 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3671 rc = sqlite3_errcode(db); 3672 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3673 return TCL_OK; 3674 } 3675 3676 /* 3677 ** Usage: sqlite3_errmsg DB 3678 ** 3679 ** Returns the UTF-8 representation of the error message string for the 3680 ** most recent sqlite3_* API call. 3681 */ 3682 static int test_errmsg( 3683 void * clientData, 3684 Tcl_Interp *interp, 3685 int objc, 3686 Tcl_Obj *CONST objv[] 3687 ){ 3688 sqlite3 *db; 3689 const char *zErr; 3690 3691 if( objc!=2 ){ 3692 Tcl_AppendResult(interp, "wrong # args: should be \"", 3693 Tcl_GetString(objv[0]), " DB", 0); 3694 return TCL_ERROR; 3695 } 3696 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3697 3698 zErr = sqlite3_errmsg(db); 3699 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 3700 return TCL_OK; 3701 } 3702 3703 /* 3704 ** Usage: test_errmsg16 DB 3705 ** 3706 ** Returns the UTF-16 representation of the error message string for the 3707 ** most recent sqlite3_* API call. This is a byte array object at the TCL 3708 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the 3709 ** UTF-16 string. 3710 */ 3711 static int test_errmsg16( 3712 void * clientData, 3713 Tcl_Interp *interp, 3714 int objc, 3715 Tcl_Obj *CONST objv[] 3716 ){ 3717 #ifndef SQLITE_OMIT_UTF16 3718 sqlite3 *db; 3719 const void *zErr; 3720 const char *z; 3721 int bytes = 0; 3722 3723 if( objc!=2 ){ 3724 Tcl_AppendResult(interp, "wrong # args: should be \"", 3725 Tcl_GetString(objv[0]), " DB", 0); 3726 return TCL_ERROR; 3727 } 3728 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3729 3730 zErr = sqlite3_errmsg16(db); 3731 if( zErr ){ 3732 z = zErr; 3733 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} 3734 } 3735 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); 3736 #endif /* SQLITE_OMIT_UTF16 */ 3737 return TCL_OK; 3738 } 3739 3740 /* 3741 ** Usage: sqlite3_prepare DB sql bytes ?tailvar? 3742 ** 3743 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3744 ** database handle <DB>. The parameter <tailval> is the name of a global 3745 ** variable that is set to the unused portion of <sql> (if any). A 3746 ** STMT handle is returned. 3747 */ 3748 static int test_prepare( 3749 void * clientData, 3750 Tcl_Interp *interp, 3751 int objc, 3752 Tcl_Obj *CONST objv[] 3753 ){ 3754 sqlite3 *db; 3755 const char *zSql; 3756 int bytes; 3757 const char *zTail = 0; 3758 sqlite3_stmt *pStmt = 0; 3759 char zBuf[50]; 3760 int rc; 3761 3762 if( objc!=5 && objc!=4 ){ 3763 Tcl_AppendResult(interp, "wrong # args: should be \"", 3764 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3765 return TCL_ERROR; 3766 } 3767 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3768 zSql = Tcl_GetString(objv[2]); 3769 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3770 3771 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3772 Tcl_ResetResult(interp); 3773 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3774 if( zTail && objc>=5 ){ 3775 if( bytes>=0 ){ 3776 bytes = bytes - (int)(zTail-zSql); 3777 } 3778 if( (int)strlen(zTail)<bytes ){ 3779 bytes = (int)strlen(zTail); 3780 } 3781 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3782 } 3783 if( rc!=SQLITE_OK ){ 3784 assert( pStmt==0 ); 3785 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 3786 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3787 return TCL_ERROR; 3788 } 3789 3790 if( pStmt ){ 3791 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3792 Tcl_AppendResult(interp, zBuf, 0); 3793 } 3794 return TCL_OK; 3795 } 3796 3797 /* 3798 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? 3799 ** 3800 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3801 ** database handle <DB>. The parameter <tailval> is the name of a global 3802 ** variable that is set to the unused portion of <sql> (if any). A 3803 ** STMT handle is returned. 3804 */ 3805 static int test_prepare_v2( 3806 void * clientData, 3807 Tcl_Interp *interp, 3808 int objc, 3809 Tcl_Obj *CONST objv[] 3810 ){ 3811 sqlite3 *db; 3812 const char *zSql; 3813 char *zCopy = 0; /* malloc() copy of zSql */ 3814 int bytes; 3815 const char *zTail = 0; 3816 sqlite3_stmt *pStmt = 0; 3817 char zBuf[50]; 3818 int rc; 3819 3820 if( objc!=5 && objc!=4 ){ 3821 Tcl_AppendResult(interp, "wrong # args: should be \"", 3822 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3823 return TCL_ERROR; 3824 } 3825 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3826 zSql = Tcl_GetString(objv[2]); 3827 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3828 3829 /* Instead of using zSql directly, make a copy into a buffer obtained 3830 ** directly from malloc(). The idea is to make it easier for valgrind 3831 ** to spot buffer overreads. */ 3832 if( bytes>=0 ){ 3833 zCopy = malloc(bytes); 3834 memcpy(zCopy, zSql, bytes); 3835 }else{ 3836 int n = (int)strlen(zSql) + 1; 3837 zCopy = malloc(n); 3838 memcpy(zCopy, zSql, n); 3839 } 3840 rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0); 3841 free(zCopy); 3842 zTail = &zSql[(zTail - zCopy)]; 3843 3844 assert(rc==SQLITE_OK || pStmt==0); 3845 Tcl_ResetResult(interp); 3846 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3847 if( rc==SQLITE_OK && zTail && objc>=5 ){ 3848 if( bytes>=0 ){ 3849 bytes = bytes - (int)(zTail-zSql); 3850 } 3851 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3852 } 3853 if( rc!=SQLITE_OK ){ 3854 assert( pStmt==0 ); 3855 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 3856 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3857 return TCL_ERROR; 3858 } 3859 3860 if( pStmt ){ 3861 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3862 Tcl_AppendResult(interp, zBuf, 0); 3863 } 3864 return TCL_OK; 3865 } 3866 3867 /* 3868 ** Usage: sqlite3_prepare_tkt3134 DB 3869 ** 3870 ** Generate a prepared statement for a zero-byte string as a test 3871 ** for ticket #3134. The string should be preceded by a zero byte. 3872 */ 3873 static int test_prepare_tkt3134( 3874 void * clientData, 3875 Tcl_Interp *interp, 3876 int objc, 3877 Tcl_Obj *CONST objv[] 3878 ){ 3879 sqlite3 *db; 3880 static const char zSql[] = "\000SELECT 1"; 3881 sqlite3_stmt *pStmt = 0; 3882 char zBuf[50]; 3883 int rc; 3884 3885 if( objc!=2 ){ 3886 Tcl_AppendResult(interp, "wrong # args: should be \"", 3887 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3888 return TCL_ERROR; 3889 } 3890 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3891 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); 3892 assert(rc==SQLITE_OK || pStmt==0); 3893 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3894 if( rc!=SQLITE_OK ){ 3895 assert( pStmt==0 ); 3896 sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); 3897 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3898 return TCL_ERROR; 3899 } 3900 3901 if( pStmt ){ 3902 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3903 Tcl_AppendResult(interp, zBuf, 0); 3904 } 3905 return TCL_OK; 3906 } 3907 3908 /* 3909 ** Usage: sqlite3_prepare16 DB sql bytes tailvar 3910 ** 3911 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3912 ** database handle <DB>. The parameter <tailval> is the name of a global 3913 ** variable that is set to the unused portion of <sql> (if any). A 3914 ** STMT handle is returned. 3915 */ 3916 static int test_prepare16( 3917 void * clientData, 3918 Tcl_Interp *interp, 3919 int objc, 3920 Tcl_Obj *CONST objv[] 3921 ){ 3922 #ifndef SQLITE_OMIT_UTF16 3923 sqlite3 *db; 3924 const void *zSql; 3925 const void *zTail = 0; 3926 Tcl_Obj *pTail = 0; 3927 sqlite3_stmt *pStmt = 0; 3928 char zBuf[50]; 3929 int rc; 3930 int bytes; /* The integer specified as arg 3 */ 3931 int objlen; /* The byte-array length of arg 2 */ 3932 3933 if( objc!=5 && objc!=4 ){ 3934 Tcl_AppendResult(interp, "wrong # args: should be \"", 3935 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3936 return TCL_ERROR; 3937 } 3938 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3939 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3940 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3941 3942 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3943 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3944 if( rc ){ 3945 return TCL_ERROR; 3946 } 3947 3948 if( objc>=5 ){ 3949 if( zTail ){ 3950 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3951 }else{ 3952 objlen = 0; 3953 } 3954 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3955 Tcl_IncrRefCount(pTail); 3956 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3957 Tcl_DecrRefCount(pTail); 3958 } 3959 3960 if( pStmt ){ 3961 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3962 } 3963 Tcl_AppendResult(interp, zBuf, 0); 3964 #endif /* SQLITE_OMIT_UTF16 */ 3965 return TCL_OK; 3966 } 3967 3968 /* 3969 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? 3970 ** 3971 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3972 ** database handle <DB>. The parameter <tailval> is the name of a global 3973 ** variable that is set to the unused portion of <sql> (if any). A 3974 ** STMT handle is returned. 3975 */ 3976 static int test_prepare16_v2( 3977 void * clientData, 3978 Tcl_Interp *interp, 3979 int objc, 3980 Tcl_Obj *CONST objv[] 3981 ){ 3982 #ifndef SQLITE_OMIT_UTF16 3983 sqlite3 *db; 3984 const void *zSql; 3985 const void *zTail = 0; 3986 Tcl_Obj *pTail = 0; 3987 sqlite3_stmt *pStmt = 0; 3988 char zBuf[50]; 3989 int rc; 3990 int bytes; /* The integer specified as arg 3 */ 3991 int objlen; /* The byte-array length of arg 2 */ 3992 3993 if( objc!=5 && objc!=4 ){ 3994 Tcl_AppendResult(interp, "wrong # args: should be \"", 3995 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3996 return TCL_ERROR; 3997 } 3998 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3999 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 4000 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 4001 4002 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 4003 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 4004 if( rc ){ 4005 return TCL_ERROR; 4006 } 4007 4008 if( objc>=5 ){ 4009 if( zTail ){ 4010 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 4011 }else{ 4012 objlen = 0; 4013 } 4014 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 4015 Tcl_IncrRefCount(pTail); 4016 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 4017 Tcl_DecrRefCount(pTail); 4018 } 4019 4020 if( pStmt ){ 4021 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 4022 } 4023 Tcl_AppendResult(interp, zBuf, 0); 4024 #endif /* SQLITE_OMIT_UTF16 */ 4025 return TCL_OK; 4026 } 4027 4028 /* 4029 ** Usage: sqlite3_open filename ?options-list? 4030 */ 4031 static int test_open( 4032 void * clientData, 4033 Tcl_Interp *interp, 4034 int objc, 4035 Tcl_Obj *CONST objv[] 4036 ){ 4037 const char *zFilename; 4038 sqlite3 *db; 4039 char zBuf[100]; 4040 4041 if( objc!=3 && objc!=2 && objc!=1 ){ 4042 Tcl_AppendResult(interp, "wrong # args: should be \"", 4043 Tcl_GetString(objv[0]), " filename options-list", 0); 4044 return TCL_ERROR; 4045 } 4046 4047 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; 4048 sqlite3_open(zFilename, &db); 4049 4050 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 4051 Tcl_AppendResult(interp, zBuf, 0); 4052 return TCL_OK; 4053 } 4054 4055 /* 4056 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS 4057 */ 4058 static int test_open_v2( 4059 void * clientData, 4060 Tcl_Interp *interp, 4061 int objc, 4062 Tcl_Obj *CONST objv[] 4063 ){ 4064 const char *zFilename; 4065 const char *zVfs; 4066 int flags = 0; 4067 sqlite3 *db; 4068 int rc; 4069 char zBuf[100]; 4070 4071 int nFlag; 4072 Tcl_Obj **apFlag; 4073 int i; 4074 4075 if( objc!=4 ){ 4076 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); 4077 return TCL_ERROR; 4078 } 4079 zFilename = Tcl_GetString(objv[1]); 4080 zVfs = Tcl_GetString(objv[3]); 4081 if( zVfs[0]==0x00 ) zVfs = 0; 4082 4083 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); 4084 if( rc!=TCL_OK ) return rc; 4085 for(i=0; i<nFlag; i++){ 4086 int iFlag; 4087 struct OpenFlag { 4088 const char *zFlag; 4089 int flag; 4090 } aFlag[] = { 4091 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, 4092 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, 4093 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, 4094 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, 4095 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, 4096 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, 4097 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, 4098 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, 4099 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, 4100 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, 4101 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, 4102 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, 4103 { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, 4104 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, 4105 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, 4106 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, 4107 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, 4108 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, 4109 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, 4110 { 0, 0 } 4111 }; 4112 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), 4113 "flag", 0, &iFlag 4114 ); 4115 if( rc!=TCL_OK ) return rc; 4116 flags |= aFlag[iFlag].flag; 4117 } 4118 4119 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); 4120 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 4121 Tcl_AppendResult(interp, zBuf, 0); 4122 return TCL_OK; 4123 } 4124 4125 /* 4126 ** Usage: sqlite3_open16 filename options 4127 */ 4128 static int test_open16( 4129 void * clientData, 4130 Tcl_Interp *interp, 4131 int objc, 4132 Tcl_Obj *CONST objv[] 4133 ){ 4134 #ifndef SQLITE_OMIT_UTF16 4135 const void *zFilename; 4136 sqlite3 *db; 4137 char zBuf[100]; 4138 4139 if( objc!=3 ){ 4140 Tcl_AppendResult(interp, "wrong # args: should be \"", 4141 Tcl_GetString(objv[0]), " filename options-list", 0); 4142 return TCL_ERROR; 4143 } 4144 4145 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); 4146 sqlite3_open16(zFilename, &db); 4147 4148 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 4149 Tcl_AppendResult(interp, zBuf, 0); 4150 #endif /* SQLITE_OMIT_UTF16 */ 4151 return TCL_OK; 4152 } 4153 4154 /* 4155 ** Usage: sqlite3_complete16 <UTF-16 string> 4156 ** 4157 ** Return 1 if the supplied argument is a complete SQL statement, or zero 4158 ** otherwise. 4159 */ 4160 static int test_complete16( 4161 void * clientData, 4162 Tcl_Interp *interp, 4163 int objc, 4164 Tcl_Obj *CONST objv[] 4165 ){ 4166 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) 4167 char *zBuf; 4168 4169 if( objc!=2 ){ 4170 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); 4171 return TCL_ERROR; 4172 } 4173 4174 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); 4175 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); 4176 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ 4177 return TCL_OK; 4178 } 4179 4180 /* 4181 ** Usage: sqlite3_step STMT 4182 ** 4183 ** Advance the statement to the next row. 4184 */ 4185 static int test_step( 4186 void * clientData, 4187 Tcl_Interp *interp, 4188 int objc, 4189 Tcl_Obj *CONST objv[] 4190 ){ 4191 sqlite3_stmt *pStmt; 4192 int rc; 4193 4194 if( objc!=2 ){ 4195 Tcl_AppendResult(interp, "wrong # args: should be \"", 4196 Tcl_GetString(objv[0]), " STMT", 0); 4197 return TCL_ERROR; 4198 } 4199 4200 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4201 rc = sqlite3_step(pStmt); 4202 4203 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ 4204 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 4205 return TCL_OK; 4206 } 4207 4208 static int test_sql( 4209 void * clientData, 4210 Tcl_Interp *interp, 4211 int objc, 4212 Tcl_Obj *CONST objv[] 4213 ){ 4214 sqlite3_stmt *pStmt; 4215 4216 if( objc!=2 ){ 4217 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 4218 return TCL_ERROR; 4219 } 4220 4221 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4222 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); 4223 return TCL_OK; 4224 } 4225 4226 /* 4227 ** Usage: sqlite3_column_count STMT 4228 ** 4229 ** Return the number of columns returned by the sql statement STMT. 4230 */ 4231 static int test_column_count( 4232 void * clientData, 4233 Tcl_Interp *interp, 4234 int objc, 4235 Tcl_Obj *CONST objv[] 4236 ){ 4237 sqlite3_stmt *pStmt; 4238 4239 if( objc!=2 ){ 4240 Tcl_AppendResult(interp, "wrong # args: should be \"", 4241 Tcl_GetString(objv[0]), " STMT column", 0); 4242 return TCL_ERROR; 4243 } 4244 4245 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4246 4247 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); 4248 return TCL_OK; 4249 } 4250 4251 /* 4252 ** Usage: sqlite3_column_type STMT column 4253 ** 4254 ** Return the type of the data in column 'column' of the current row. 4255 */ 4256 static int test_column_type( 4257 void * clientData, 4258 Tcl_Interp *interp, 4259 int objc, 4260 Tcl_Obj *CONST objv[] 4261 ){ 4262 sqlite3_stmt *pStmt; 4263 int col; 4264 int tp; 4265 4266 if( objc!=3 ){ 4267 Tcl_AppendResult(interp, "wrong # args: should be \"", 4268 Tcl_GetString(objv[0]), " STMT column", 0); 4269 return TCL_ERROR; 4270 } 4271 4272 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4273 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4274 4275 tp = sqlite3_column_type(pStmt, col); 4276 switch( tp ){ 4277 case SQLITE_INTEGER: 4278 Tcl_SetResult(interp, "INTEGER", TCL_STATIC); 4279 break; 4280 case SQLITE_NULL: 4281 Tcl_SetResult(interp, "NULL", TCL_STATIC); 4282 break; 4283 case SQLITE_FLOAT: 4284 Tcl_SetResult(interp, "FLOAT", TCL_STATIC); 4285 break; 4286 case SQLITE_TEXT: 4287 Tcl_SetResult(interp, "TEXT", TCL_STATIC); 4288 break; 4289 case SQLITE_BLOB: 4290 Tcl_SetResult(interp, "BLOB", TCL_STATIC); 4291 break; 4292 default: 4293 assert(0); 4294 } 4295 4296 return TCL_OK; 4297 } 4298 4299 /* 4300 ** Usage: sqlite3_column_int64 STMT column 4301 ** 4302 ** Return the data in column 'column' of the current row cast as an 4303 ** wide (64-bit) integer. 4304 */ 4305 static int test_column_int64( 4306 void * clientData, 4307 Tcl_Interp *interp, 4308 int objc, 4309 Tcl_Obj *CONST objv[] 4310 ){ 4311 sqlite3_stmt *pStmt; 4312 int col; 4313 i64 iVal; 4314 4315 if( objc!=3 ){ 4316 Tcl_AppendResult(interp, "wrong # args: should be \"", 4317 Tcl_GetString(objv[0]), " STMT column", 0); 4318 return TCL_ERROR; 4319 } 4320 4321 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4322 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4323 4324 iVal = sqlite3_column_int64(pStmt, col); 4325 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); 4326 return TCL_OK; 4327 } 4328 4329 /* 4330 ** Usage: sqlite3_column_blob STMT column 4331 */ 4332 static int test_column_blob( 4333 void * clientData, 4334 Tcl_Interp *interp, 4335 int objc, 4336 Tcl_Obj *CONST objv[] 4337 ){ 4338 sqlite3_stmt *pStmt; 4339 int col; 4340 4341 int len; 4342 const void *pBlob; 4343 4344 if( objc!=3 ){ 4345 Tcl_AppendResult(interp, "wrong # args: should be \"", 4346 Tcl_GetString(objv[0]), " STMT column", 0); 4347 return TCL_ERROR; 4348 } 4349 4350 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4351 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4352 4353 len = sqlite3_column_bytes(pStmt, col); 4354 pBlob = sqlite3_column_blob(pStmt, col); 4355 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); 4356 return TCL_OK; 4357 } 4358 4359 /* 4360 ** Usage: sqlite3_column_double STMT column 4361 ** 4362 ** Return the data in column 'column' of the current row cast as a double. 4363 */ 4364 static int test_column_double( 4365 void * clientData, 4366 Tcl_Interp *interp, 4367 int objc, 4368 Tcl_Obj *CONST objv[] 4369 ){ 4370 sqlite3_stmt *pStmt; 4371 int col; 4372 double rVal; 4373 4374 if( objc!=3 ){ 4375 Tcl_AppendResult(interp, "wrong # args: should be \"", 4376 Tcl_GetString(objv[0]), " STMT column", 0); 4377 return TCL_ERROR; 4378 } 4379 4380 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4381 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4382 4383 rVal = sqlite3_column_double(pStmt, col); 4384 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); 4385 return TCL_OK; 4386 } 4387 4388 /* 4389 ** Usage: sqlite3_data_count STMT 4390 ** 4391 ** Return the number of columns returned by the sql statement STMT. 4392 */ 4393 static int test_data_count( 4394 void * clientData, 4395 Tcl_Interp *interp, 4396 int objc, 4397 Tcl_Obj *CONST objv[] 4398 ){ 4399 sqlite3_stmt *pStmt; 4400 4401 if( objc!=2 ){ 4402 Tcl_AppendResult(interp, "wrong # args: should be \"", 4403 Tcl_GetString(objv[0]), " STMT column", 0); 4404 return TCL_ERROR; 4405 } 4406 4407 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4408 4409 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); 4410 return TCL_OK; 4411 } 4412 4413 /* 4414 ** Usage: sqlite3_column_text STMT column 4415 ** 4416 ** Usage: sqlite3_column_decltype STMT column 4417 ** 4418 ** Usage: sqlite3_column_name STMT column 4419 */ 4420 static int test_stmt_utf8( 4421 void * clientData, /* Pointer to SQLite API function to be invoke */ 4422 Tcl_Interp *interp, 4423 int objc, 4424 Tcl_Obj *CONST objv[] 4425 ){ 4426 sqlite3_stmt *pStmt; 4427 int col; 4428 const char *(*xFunc)(sqlite3_stmt*, int); 4429 const char *zRet; 4430 4431 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; 4432 if( objc!=3 ){ 4433 Tcl_AppendResult(interp, "wrong # args: should be \"", 4434 Tcl_GetString(objv[0]), " STMT column", 0); 4435 return TCL_ERROR; 4436 } 4437 4438 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4439 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4440 zRet = xFunc(pStmt, col); 4441 if( zRet ){ 4442 Tcl_SetResult(interp, (char *)zRet, 0); 4443 } 4444 return TCL_OK; 4445 } 4446 4447 static int test_global_recover( 4448 void * clientData, 4449 Tcl_Interp *interp, 4450 int objc, 4451 Tcl_Obj *CONST objv[] 4452 ){ 4453 #ifndef SQLITE_OMIT_DEPRECATED 4454 int rc; 4455 if( objc!=1 ){ 4456 Tcl_WrongNumArgs(interp, 1, objv, ""); 4457 return TCL_ERROR; 4458 } 4459 rc = sqlite3_global_recover(); 4460 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4461 #endif 4462 return TCL_OK; 4463 } 4464 4465 /* 4466 ** Usage: sqlite3_column_text STMT column 4467 ** 4468 ** Usage: sqlite3_column_decltype STMT column 4469 ** 4470 ** Usage: sqlite3_column_name STMT column 4471 */ 4472 static int test_stmt_utf16( 4473 void * clientData, /* Pointer to SQLite API function to be invoked */ 4474 Tcl_Interp *interp, 4475 int objc, 4476 Tcl_Obj *CONST objv[] 4477 ){ 4478 #ifndef SQLITE_OMIT_UTF16 4479 sqlite3_stmt *pStmt; 4480 int col; 4481 Tcl_Obj *pRet; 4482 const void *zName16; 4483 const void *(*xFunc)(sqlite3_stmt*, int); 4484 4485 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; 4486 if( objc!=3 ){ 4487 Tcl_AppendResult(interp, "wrong # args: should be \"", 4488 Tcl_GetString(objv[0]), " STMT column", 0); 4489 return TCL_ERROR; 4490 } 4491 4492 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4493 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4494 4495 zName16 = xFunc(pStmt, col); 4496 if( zName16 ){ 4497 int n; 4498 const char *z = zName16; 4499 for(n=0; z[n] || z[n+1]; n+=2){} 4500 pRet = Tcl_NewByteArrayObj(zName16, n+2); 4501 Tcl_SetObjResult(interp, pRet); 4502 } 4503 #endif /* SQLITE_OMIT_UTF16 */ 4504 4505 return TCL_OK; 4506 } 4507 4508 /* 4509 ** Usage: sqlite3_column_int STMT column 4510 ** 4511 ** Usage: sqlite3_column_bytes STMT column 4512 ** 4513 ** Usage: sqlite3_column_bytes16 STMT column 4514 ** 4515 */ 4516 static int test_stmt_int( 4517 void * clientData, /* Pointer to SQLite API function to be invoked */ 4518 Tcl_Interp *interp, 4519 int objc, 4520 Tcl_Obj *CONST objv[] 4521 ){ 4522 sqlite3_stmt *pStmt; 4523 int col; 4524 int (*xFunc)(sqlite3_stmt*, int); 4525 4526 xFunc = (int (*)(sqlite3_stmt*, int))clientData; 4527 if( objc!=3 ){ 4528 Tcl_AppendResult(interp, "wrong # args: should be \"", 4529 Tcl_GetString(objv[0]), " STMT column", 0); 4530 return TCL_ERROR; 4531 } 4532 4533 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4534 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4535 4536 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); 4537 return TCL_OK; 4538 } 4539 4540 /* 4541 ** Usage: sqlite_set_magic DB MAGIC-NUMBER 4542 ** 4543 ** Set the db->magic value. This is used to test error recovery logic. 4544 */ 4545 static int sqlite_set_magic( 4546 void * clientData, 4547 Tcl_Interp *interp, 4548 int argc, 4549 char **argv 4550 ){ 4551 sqlite3 *db; 4552 if( argc!=3 ){ 4553 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4554 " DB MAGIC", 0); 4555 return TCL_ERROR; 4556 } 4557 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4558 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ 4559 db->magic = SQLITE_MAGIC_OPEN; 4560 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ 4561 db->magic = SQLITE_MAGIC_CLOSED; 4562 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ 4563 db->magic = SQLITE_MAGIC_BUSY; 4564 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ 4565 db->magic = SQLITE_MAGIC_ERROR; 4566 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ 4567 return TCL_ERROR; 4568 } 4569 return TCL_OK; 4570 } 4571 4572 /* 4573 ** Usage: sqlite3_interrupt DB 4574 ** 4575 ** Trigger an interrupt on DB 4576 */ 4577 static int test_interrupt( 4578 void * clientData, 4579 Tcl_Interp *interp, 4580 int argc, 4581 char **argv 4582 ){ 4583 sqlite3 *db; 4584 if( argc!=2 ){ 4585 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); 4586 return TCL_ERROR; 4587 } 4588 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4589 sqlite3_interrupt(db); 4590 return TCL_OK; 4591 } 4592 4593 static u8 *sqlite3_stack_baseline = 0; 4594 4595 /* 4596 ** Fill the stack with a known bitpattern. 4597 */ 4598 static void prepStack(void){ 4599 int i; 4600 u32 bigBuf[65536]; 4601 for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; 4602 sqlite3_stack_baseline = (u8*)&bigBuf[65536]; 4603 } 4604 4605 /* 4606 ** Get the current stack depth. Used for debugging only. 4607 */ 4608 u64 sqlite3StackDepth(void){ 4609 u8 x; 4610 return (u64)(sqlite3_stack_baseline - &x); 4611 } 4612 4613 /* 4614 ** Usage: sqlite3_stack_used DB SQL 4615 ** 4616 ** Try to measure the amount of stack space used by a call to sqlite3_exec 4617 */ 4618 static int test_stack_used( 4619 void * clientData, 4620 Tcl_Interp *interp, 4621 int argc, 4622 char **argv 4623 ){ 4624 sqlite3 *db; 4625 int i; 4626 if( argc!=3 ){ 4627 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4628 " DB SQL", 0); 4629 return TCL_ERROR; 4630 } 4631 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4632 prepStack(); 4633 (void)sqlite3_exec(db, argv[2], 0, 0, 0); 4634 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} 4635 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); 4636 return TCL_OK; 4637 } 4638 4639 /* 4640 ** Usage: sqlite_delete_function DB function-name 4641 ** 4642 ** Delete the user function 'function-name' from database handle DB. It 4643 ** is assumed that the user function was created as UTF8, any number of 4644 ** arguments (the way the TCL interface does it). 4645 */ 4646 static int delete_function( 4647 void * clientData, 4648 Tcl_Interp *interp, 4649 int argc, 4650 char **argv 4651 ){ 4652 int rc; 4653 sqlite3 *db; 4654 if( argc!=3 ){ 4655 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4656 " DB function-name", 0); 4657 return TCL_ERROR; 4658 } 4659 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4660 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); 4661 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4662 return TCL_OK; 4663 } 4664 4665 /* 4666 ** Usage: sqlite_delete_collation DB collation-name 4667 ** 4668 ** Delete the collation sequence 'collation-name' from database handle 4669 ** DB. It is assumed that the collation sequence was created as UTF8 (the 4670 ** way the TCL interface does it). 4671 */ 4672 static int delete_collation( 4673 void * clientData, 4674 Tcl_Interp *interp, 4675 int argc, 4676 char **argv 4677 ){ 4678 int rc; 4679 sqlite3 *db; 4680 if( argc!=3 ){ 4681 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4682 " DB function-name", 0); 4683 return TCL_ERROR; 4684 } 4685 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4686 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); 4687 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4688 return TCL_OK; 4689 } 4690 4691 /* 4692 ** Usage: sqlite3_get_autocommit DB 4693 ** 4694 ** Return true if the database DB is currently in auto-commit mode. 4695 ** Return false if not. 4696 */ 4697 static int get_autocommit( 4698 void * clientData, 4699 Tcl_Interp *interp, 4700 int argc, 4701 char **argv 4702 ){ 4703 char zBuf[30]; 4704 sqlite3 *db; 4705 if( argc!=2 ){ 4706 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4707 " DB", 0); 4708 return TCL_ERROR; 4709 } 4710 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4711 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db)); 4712 Tcl_AppendResult(interp, zBuf, 0); 4713 return TCL_OK; 4714 } 4715 4716 /* 4717 ** Usage: sqlite3_busy_timeout DB MS 4718 ** 4719 ** Set the busy timeout. This is more easily done using the timeout 4720 ** method of the TCL interface. But we need a way to test the case 4721 ** where it returns SQLITE_MISUSE. 4722 */ 4723 static int test_busy_timeout( 4724 void * clientData, 4725 Tcl_Interp *interp, 4726 int argc, 4727 char **argv 4728 ){ 4729 int rc, ms; 4730 sqlite3 *db; 4731 if( argc!=3 ){ 4732 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4733 " DB", 0); 4734 return TCL_ERROR; 4735 } 4736 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4737 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; 4738 rc = sqlite3_busy_timeout(db, ms); 4739 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 4740 return TCL_OK; 4741 } 4742 4743 /* 4744 ** Usage: tcl_variable_type VARIABLENAME 4745 ** 4746 ** Return the name of the internal representation for the 4747 ** value of the given variable. 4748 */ 4749 static int tcl_variable_type( 4750 void * clientData, 4751 Tcl_Interp *interp, 4752 int objc, 4753 Tcl_Obj *CONST objv[] 4754 ){ 4755 Tcl_Obj *pVar; 4756 if( objc!=2 ){ 4757 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); 4758 return TCL_ERROR; 4759 } 4760 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); 4761 if( pVar==0 ) return TCL_ERROR; 4762 if( pVar->typePtr ){ 4763 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); 4764 } 4765 return TCL_OK; 4766 } 4767 4768 /* 4769 ** Usage: sqlite3_release_memory ?N? 4770 ** 4771 ** Attempt to release memory currently held but not actually required. 4772 ** The integer N is the number of bytes we are trying to release. The 4773 ** return value is the amount of memory actually released. 4774 */ 4775 static int test_release_memory( 4776 void * clientData, 4777 Tcl_Interp *interp, 4778 int objc, 4779 Tcl_Obj *CONST objv[] 4780 ){ 4781 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) 4782 int N; 4783 int amt; 4784 if( objc!=1 && objc!=2 ){ 4785 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4786 return TCL_ERROR; 4787 } 4788 if( objc==2 ){ 4789 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4790 }else{ 4791 N = -1; 4792 } 4793 amt = sqlite3_release_memory(N); 4794 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); 4795 #endif 4796 return TCL_OK; 4797 } 4798 4799 4800 /* 4801 ** Usage: sqlite3_db_release_memory DB 4802 ** 4803 ** Attempt to release memory currently held by database DB. Return the 4804 ** result code (which in the current implementation is always zero). 4805 */ 4806 static int test_db_release_memory( 4807 void * clientData, 4808 Tcl_Interp *interp, 4809 int objc, 4810 Tcl_Obj *CONST objv[] 4811 ){ 4812 sqlite3 *db; 4813 int rc; 4814 if( objc!=2 ){ 4815 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 4816 return TCL_ERROR; 4817 } 4818 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4819 rc = sqlite3_db_release_memory(db); 4820 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 4821 return TCL_OK; 4822 } 4823 4824 /* 4825 ** Usage: sqlite3_db_cacheflush DB 4826 ** 4827 ** Attempt to flush any dirty pages to disk. 4828 */ 4829 static int test_db_cacheflush( 4830 void * clientData, 4831 Tcl_Interp *interp, 4832 int objc, 4833 Tcl_Obj *CONST objv[] 4834 ){ 4835 sqlite3 *db; 4836 int rc; 4837 if( objc!=2 ){ 4838 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 4839 return TCL_ERROR; 4840 } 4841 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4842 rc = sqlite3_db_cacheflush(db); 4843 if( rc ){ 4844 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); 4845 return TCL_ERROR; 4846 } 4847 4848 Tcl_ResetResult(interp); 4849 return TCL_OK; 4850 } 4851 4852 /* 4853 ** Usage: sqlite3_db_filename DB DBNAME 4854 ** 4855 ** Return the name of a file associated with a database. 4856 */ 4857 static int test_db_filename( 4858 void * clientData, 4859 Tcl_Interp *interp, 4860 int objc, 4861 Tcl_Obj *CONST objv[] 4862 ){ 4863 sqlite3 *db; 4864 const char *zDbName; 4865 if( objc!=3 ){ 4866 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4867 return TCL_ERROR; 4868 } 4869 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4870 zDbName = Tcl_GetString(objv[2]); 4871 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); 4872 return TCL_OK; 4873 } 4874 4875 /* 4876 ** Usage: sqlite3_db_readonly DB DBNAME 4877 ** 4878 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does 4879 ** not exist. 4880 */ 4881 static int test_db_readonly( 4882 void * clientData, 4883 Tcl_Interp *interp, 4884 int objc, 4885 Tcl_Obj *CONST objv[] 4886 ){ 4887 sqlite3 *db; 4888 const char *zDbName; 4889 if( objc!=3 ){ 4890 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4891 return TCL_ERROR; 4892 } 4893 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4894 zDbName = Tcl_GetString(objv[2]); 4895 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName))); 4896 return TCL_OK; 4897 } 4898 4899 /* 4900 ** Usage: sqlite3_soft_heap_limit ?N? 4901 ** 4902 ** Query or set the soft heap limit for the current thread. The 4903 ** limit is only changed if the N is present. The previous limit 4904 ** is returned. 4905 */ 4906 static int test_soft_heap_limit( 4907 void * clientData, 4908 Tcl_Interp *interp, 4909 int objc, 4910 Tcl_Obj *CONST objv[] 4911 ){ 4912 sqlite3_int64 amt; 4913 Tcl_WideInt N = -1; 4914 if( objc!=1 && objc!=2 ){ 4915 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4916 return TCL_ERROR; 4917 } 4918 if( objc==2 ){ 4919 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4920 } 4921 amt = sqlite3_soft_heap_limit64(N); 4922 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); 4923 return TCL_OK; 4924 } 4925 4926 /* 4927 ** Usage: sqlite3_thread_cleanup 4928 ** 4929 ** Call the sqlite3_thread_cleanup API. 4930 */ 4931 static int test_thread_cleanup( 4932 void * clientData, 4933 Tcl_Interp *interp, 4934 int objc, 4935 Tcl_Obj *CONST objv[] 4936 ){ 4937 #ifndef SQLITE_OMIT_DEPRECATED 4938 sqlite3_thread_cleanup(); 4939 #endif 4940 return TCL_OK; 4941 } 4942 4943 /* 4944 ** Usage: sqlite3_pager_refcounts DB 4945 ** 4946 ** Return a list of numbers which are the PagerRefcount for all 4947 ** pagers on each database connection. 4948 */ 4949 static int test_pager_refcounts( 4950 void * clientData, 4951 Tcl_Interp *interp, 4952 int objc, 4953 Tcl_Obj *CONST objv[] 4954 ){ 4955 sqlite3 *db; 4956 int i; 4957 int v, *a; 4958 Tcl_Obj *pResult; 4959 4960 if( objc!=2 ){ 4961 Tcl_AppendResult(interp, "wrong # args: should be \"", 4962 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 4963 return TCL_ERROR; 4964 } 4965 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4966 pResult = Tcl_NewObj(); 4967 for(i=0; i<db->nDb; i++){ 4968 if( db->aDb[i].pBt==0 ){ 4969 v = -1; 4970 }else{ 4971 sqlite3_mutex_enter(db->mutex); 4972 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); 4973 v = a[0]; 4974 sqlite3_mutex_leave(db->mutex); 4975 } 4976 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); 4977 } 4978 Tcl_SetObjResult(interp, pResult); 4979 return TCL_OK; 4980 } 4981 4982 4983 /* 4984 ** tclcmd: working_64bit_int 4985 ** 4986 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This 4987 ** leads to a number of test failures. The present command checks the 4988 ** TCL build to see whether or not it supports 64-bit integers. It 4989 ** returns TRUE if it does and FALSE if not. 4990 ** 4991 ** This command is used to warn users that their TCL build is defective 4992 ** and that the errors they are seeing in the test scripts might be 4993 ** a result of their defective TCL rather than problems in SQLite. 4994 */ 4995 static int working_64bit_int( 4996 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4997 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4998 int objc, /* Number of arguments */ 4999 Tcl_Obj *CONST objv[] /* Command arguments */ 5000 ){ 5001 Tcl_Obj *pTestObj; 5002 int working = 0; 5003 5004 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); 5005 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; 5006 Tcl_DecrRefCount(pTestObj); 5007 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); 5008 return TCL_OK; 5009 } 5010 5011 5012 /* 5013 ** tclcmd: vfs_unlink_test 5014 ** 5015 ** This TCL command unregisters the primary VFS and then registers 5016 ** it back again. This is used to test the ability to register a 5017 ** VFS when none are previously registered, and the ability to 5018 ** unregister the only available VFS. Ticket #2738 5019 */ 5020 static int vfs_unlink_test( 5021 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5022 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5023 int objc, /* Number of arguments */ 5024 Tcl_Obj *CONST objv[] /* Command arguments */ 5025 ){ 5026 int i; 5027 sqlite3_vfs *pMain; 5028 sqlite3_vfs *apVfs[20]; 5029 sqlite3_vfs one, two; 5030 5031 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ 5032 one.zName = "__one"; 5033 two.zName = "__two"; 5034 5035 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not 5036 ** change the default VFS 5037 */ 5038 pMain = sqlite3_vfs_find(0); 5039 sqlite3_vfs_register(&one, 0); 5040 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 5041 sqlite3_vfs_register(&two, 0); 5042 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 5043 5044 /* We can find a VFS by its name */ 5045 assert( sqlite3_vfs_find("__one")==&one ); 5046 assert( sqlite3_vfs_find("__two")==&two ); 5047 5048 /* Calling sqlite_vfs_register with non-zero second parameter changes the 5049 ** default VFS, even if the 1st parameter is an existig VFS that is 5050 ** previously registered as the non-default. 5051 */ 5052 sqlite3_vfs_register(&one, 1); 5053 assert( sqlite3_vfs_find("__one")==&one ); 5054 assert( sqlite3_vfs_find("__two")==&two ); 5055 assert( sqlite3_vfs_find(0)==&one ); 5056 sqlite3_vfs_register(&two, 1); 5057 assert( sqlite3_vfs_find("__one")==&one ); 5058 assert( sqlite3_vfs_find("__two")==&two ); 5059 assert( sqlite3_vfs_find(0)==&two ); 5060 if( pMain ){ 5061 sqlite3_vfs_register(pMain, 1); 5062 assert( sqlite3_vfs_find("__one")==&one ); 5063 assert( sqlite3_vfs_find("__two")==&two ); 5064 assert( sqlite3_vfs_find(0)==pMain ); 5065 } 5066 5067 /* Unlink the default VFS. Repeat until there are no more VFSes 5068 ** registered. 5069 */ 5070 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ 5071 apVfs[i] = sqlite3_vfs_find(0); 5072 if( apVfs[i] ){ 5073 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 5074 sqlite3_vfs_unregister(apVfs[i]); 5075 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); 5076 } 5077 } 5078 assert( 0==sqlite3_vfs_find(0) ); 5079 5080 /* Register the main VFS as non-default (will be made default, since 5081 ** it'll be the only one in existence). 5082 */ 5083 sqlite3_vfs_register(pMain, 0); 5084 assert( sqlite3_vfs_find(0)==pMain ); 5085 5086 /* Un-register the main VFS again to restore an empty VFS list */ 5087 sqlite3_vfs_unregister(pMain); 5088 assert( 0==sqlite3_vfs_find(0) ); 5089 5090 /* Relink all VFSes in reverse order. */ 5091 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ 5092 if( apVfs[i] ){ 5093 sqlite3_vfs_register(apVfs[i], 1); 5094 assert( apVfs[i]==sqlite3_vfs_find(0) ); 5095 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 5096 } 5097 } 5098 5099 /* Unregister out sample VFSes. */ 5100 sqlite3_vfs_unregister(&one); 5101 sqlite3_vfs_unregister(&two); 5102 5103 /* Unregistering a VFS that is not currently registered is harmless */ 5104 sqlite3_vfs_unregister(&one); 5105 sqlite3_vfs_unregister(&two); 5106 assert( sqlite3_vfs_find("__one")==0 ); 5107 assert( sqlite3_vfs_find("__two")==0 ); 5108 5109 /* We should be left with the original default VFS back as the 5110 ** original */ 5111 assert( sqlite3_vfs_find(0)==pMain ); 5112 5113 return TCL_OK; 5114 } 5115 5116 /* 5117 ** tclcmd: vfs_initfail_test 5118 ** 5119 ** This TCL command attempts to vfs_find and vfs_register when the 5120 ** sqlite3_initialize() interface is failing. All calls should fail. 5121 */ 5122 static int vfs_initfail_test( 5123 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5124 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5125 int objc, /* Number of arguments */ 5126 Tcl_Obj *CONST objv[] /* Command arguments */ 5127 ){ 5128 sqlite3_vfs one; 5129 one.zName = "__one"; 5130 5131 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 5132 sqlite3_vfs_register(&one, 0); 5133 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 5134 sqlite3_vfs_register(&one, 1); 5135 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 5136 return TCL_OK; 5137 } 5138 5139 /* 5140 ** Saved VFSes 5141 */ 5142 static sqlite3_vfs *apVfs[20]; 5143 static int nVfs = 0; 5144 5145 /* 5146 ** tclcmd: vfs_unregister_all 5147 ** 5148 ** Unregister all VFSes. 5149 */ 5150 static int vfs_unregister_all( 5151 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5152 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5153 int objc, /* Number of arguments */ 5154 Tcl_Obj *CONST objv[] /* Command arguments */ 5155 ){ 5156 int i; 5157 for(i=0; i<ArraySize(apVfs); i++){ 5158 apVfs[i] = sqlite3_vfs_find(0); 5159 if( apVfs[i]==0 ) break; 5160 sqlite3_vfs_unregister(apVfs[i]); 5161 } 5162 nVfs = i; 5163 return TCL_OK; 5164 } 5165 /* 5166 ** tclcmd: vfs_reregister_all 5167 ** 5168 ** Restore all VFSes that were removed using vfs_unregister_all 5169 */ 5170 static int vfs_reregister_all( 5171 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5172 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5173 int objc, /* Number of arguments */ 5174 Tcl_Obj *CONST objv[] /* Command arguments */ 5175 ){ 5176 int i; 5177 for(i=0; i<nVfs; i++){ 5178 sqlite3_vfs_register(apVfs[i], i==0); 5179 } 5180 return TCL_OK; 5181 } 5182 5183 5184 /* 5185 ** tclcmd: file_control_test DB 5186 ** 5187 ** This TCL command runs the sqlite3_file_control interface and 5188 ** verifies correct operation of the same. 5189 */ 5190 static int file_control_test( 5191 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5192 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5193 int objc, /* Number of arguments */ 5194 Tcl_Obj *CONST objv[] /* Command arguments */ 5195 ){ 5196 int iArg = 0; 5197 sqlite3 *db; 5198 int rc; 5199 5200 if( objc!=2 ){ 5201 Tcl_AppendResult(interp, "wrong # args: should be \"", 5202 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5203 return TCL_ERROR; 5204 } 5205 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5206 rc = sqlite3_file_control(db, 0, 0, &iArg); 5207 assert( rc==SQLITE_NOTFOUND ); 5208 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); 5209 assert( rc==SQLITE_ERROR ); 5210 rc = sqlite3_file_control(db, "main", -1, &iArg); 5211 assert( rc==SQLITE_NOTFOUND ); 5212 rc = sqlite3_file_control(db, "temp", -1, &iArg); 5213 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); 5214 5215 return TCL_OK; 5216 } 5217 5218 5219 /* 5220 ** tclcmd: file_control_lasterrno_test DB 5221 ** 5222 ** This TCL command runs the sqlite3_file_control interface and 5223 ** verifies correct operation of the SQLITE_LAST_ERRNO verb. 5224 */ 5225 static int file_control_lasterrno_test( 5226 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5227 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5228 int objc, /* Number of arguments */ 5229 Tcl_Obj *CONST objv[] /* Command arguments */ 5230 ){ 5231 int iArg = 0; 5232 sqlite3 *db; 5233 int rc; 5234 5235 if( objc!=2 ){ 5236 Tcl_AppendResult(interp, "wrong # args: should be \"", 5237 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5238 return TCL_ERROR; 5239 } 5240 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5241 return TCL_ERROR; 5242 } 5243 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); 5244 if( rc ){ 5245 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5246 return TCL_ERROR; 5247 } 5248 if( iArg!=0 ) { 5249 Tcl_AppendResult(interp, "Unexpected non-zero errno: ", 5250 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); 5251 return TCL_ERROR; 5252 } 5253 return TCL_OK; 5254 } 5255 5256 /* 5257 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE 5258 ** 5259 ** This TCL command runs the sqlite3_file_control interface and 5260 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5261 ** SQLITE_SET_LOCKPROXYFILE verbs. 5262 */ 5263 static int file_control_chunksize_test( 5264 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5265 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5266 int objc, /* Number of arguments */ 5267 Tcl_Obj *CONST objv[] /* Command arguments */ 5268 ){ 5269 int nSize; /* New chunk size */ 5270 char *zDb; /* Db name ("main", "temp" etc.) */ 5271 sqlite3 *db; /* Database handle */ 5272 int rc; /* file_control() return code */ 5273 5274 if( objc!=4 ){ 5275 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5276 return TCL_ERROR; 5277 } 5278 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5279 || Tcl_GetIntFromObj(interp, objv[3], &nSize) 5280 ){ 5281 return TCL_ERROR; 5282 } 5283 zDb = Tcl_GetString(objv[2]); 5284 if( zDb[0]=='\0' ) zDb = NULL; 5285 5286 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); 5287 if( rc ){ 5288 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5289 return TCL_ERROR; 5290 } 5291 return TCL_OK; 5292 } 5293 5294 /* 5295 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE 5296 ** 5297 ** This TCL command runs the sqlite3_file_control interface 5298 ** with SQLITE_FCNTL_SIZE_HINT 5299 */ 5300 static int file_control_sizehint_test( 5301 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5303 int objc, /* Number of arguments */ 5304 Tcl_Obj *CONST objv[] /* Command arguments */ 5305 ){ 5306 Tcl_WideInt nSize; /* Hinted size */ 5307 char *zDb; /* Db name ("main", "temp" etc.) */ 5308 sqlite3 *db; /* Database handle */ 5309 int rc; /* file_control() return code */ 5310 5311 if( objc!=4 ){ 5312 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5313 return TCL_ERROR; 5314 } 5315 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5316 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) 5317 ){ 5318 return TCL_ERROR; 5319 } 5320 zDb = Tcl_GetString(objv[2]); 5321 if( zDb[0]=='\0' ) zDb = NULL; 5322 5323 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); 5324 if( rc ){ 5325 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5326 return TCL_ERROR; 5327 } 5328 return TCL_OK; 5329 } 5330 5331 /* 5332 ** tclcmd: file_control_lockproxy_test DB PWD 5333 ** 5334 ** This TCL command runs the sqlite3_file_control interface and 5335 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5336 ** SQLITE_SET_LOCKPROXYFILE verbs. 5337 */ 5338 static int file_control_lockproxy_test( 5339 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5340 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5341 int objc, /* Number of arguments */ 5342 Tcl_Obj *CONST objv[] /* Command arguments */ 5343 ){ 5344 sqlite3 *db; 5345 5346 if( objc!=3 ){ 5347 Tcl_AppendResult(interp, "wrong # args: should be \"", 5348 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); 5349 return TCL_ERROR; 5350 } 5351 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5352 return TCL_ERROR; 5353 } 5354 5355 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 5356 # if defined(__APPLE__) 5357 # define SQLITE_ENABLE_LOCKING_STYLE 1 5358 # else 5359 # define SQLITE_ENABLE_LOCKING_STYLE 0 5360 # endif 5361 #endif 5362 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 5363 { 5364 char *testPath; 5365 int rc; 5366 int nPwd; 5367 const char *zPwd; 5368 char proxyPath[400]; 5369 5370 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); 5371 if( sizeof(proxyPath)<nPwd+20 ){ 5372 Tcl_AppendResult(interp, "PWD too big", (void*)0); 5373 return TCL_ERROR; 5374 } 5375 sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd); 5376 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5377 if( rc ){ 5378 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5379 return TCL_ERROR; 5380 } 5381 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); 5382 if( strncmp(proxyPath,testPath,11) ){ 5383 Tcl_AppendResult(interp, "Lock proxy file did not match the " 5384 "previously assigned value", 0); 5385 return TCL_ERROR; 5386 } 5387 if( rc ){ 5388 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5389 return TCL_ERROR; 5390 } 5391 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5392 if( rc ){ 5393 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5394 return TCL_ERROR; 5395 } 5396 } 5397 #endif 5398 return TCL_OK; 5399 } 5400 5401 #if SQLITE_OS_WIN 5402 /* 5403 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY 5404 ** 5405 ** This TCL command runs the sqlite3_file_control interface with 5406 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. 5407 */ 5408 static int file_control_win32_av_retry( 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 int rc; 5416 int a[2]; 5417 char z[100]; 5418 5419 if( objc!=4 ){ 5420 Tcl_AppendResult(interp, "wrong # args: should be \"", 5421 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); 5422 return TCL_ERROR; 5423 } 5424 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5425 return TCL_ERROR; 5426 } 5427 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; 5428 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; 5429 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); 5430 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); 5431 Tcl_AppendResult(interp, z, (char*)0); 5432 return TCL_OK; 5433 } 5434 5435 /* 5436 ** tclcmd: file_control_win32_set_handle DB HANDLE 5437 ** 5438 ** This TCL command runs the sqlite3_file_control interface with 5439 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode. 5440 */ 5441 static int file_control_win32_set_handle( 5442 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5443 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5444 int objc, /* Number of arguments */ 5445 Tcl_Obj *CONST objv[] /* Command arguments */ 5446 ){ 5447 sqlite3 *db; 5448 int rc; 5449 HANDLE hFile = NULL; 5450 char z[100]; 5451 5452 if( objc!=3 ){ 5453 Tcl_AppendResult(interp, "wrong # args: should be \"", 5454 Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0); 5455 return TCL_ERROR; 5456 } 5457 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5458 return TCL_ERROR; 5459 } 5460 if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){ 5461 return TCL_ERROR; 5462 } 5463 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE, 5464 (void*)&hFile); 5465 sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile); 5466 Tcl_AppendResult(interp, z, (char*)0); 5467 return TCL_OK; 5468 } 5469 #endif 5470 5471 /* 5472 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG 5473 ** 5474 ** This TCL command runs the sqlite3_file_control interface with 5475 ** the SQLITE_FCNTL_PERSIST_WAL opcode. 5476 */ 5477 static int file_control_persist_wal( 5478 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5479 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5480 int objc, /* Number of arguments */ 5481 Tcl_Obj *CONST objv[] /* Command arguments */ 5482 ){ 5483 sqlite3 *db; 5484 int rc; 5485 int bPersist; 5486 char z[100]; 5487 5488 if( objc!=3 ){ 5489 Tcl_AppendResult(interp, "wrong # args: should be \"", 5490 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5491 return TCL_ERROR; 5492 } 5493 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5494 return TCL_ERROR; 5495 } 5496 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; 5497 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); 5498 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); 5499 Tcl_AppendResult(interp, z, (char*)0); 5500 return TCL_OK; 5501 } 5502 5503 /* 5504 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG 5505 ** 5506 ** This TCL command runs the sqlite3_file_control interface with 5507 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. 5508 */ 5509 static int file_control_powersafe_overwrite( 5510 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5511 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5512 int objc, /* Number of arguments */ 5513 Tcl_Obj *CONST objv[] /* Command arguments */ 5514 ){ 5515 sqlite3 *db; 5516 int rc; 5517 int b; 5518 char z[100]; 5519 5520 if( objc!=3 ){ 5521 Tcl_AppendResult(interp, "wrong # args: should be \"", 5522 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5523 return TCL_ERROR; 5524 } 5525 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5526 return TCL_ERROR; 5527 } 5528 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR; 5529 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b); 5530 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b); 5531 Tcl_AppendResult(interp, z, (char*)0); 5532 return TCL_OK; 5533 } 5534 5535 5536 /* 5537 ** tclcmd: file_control_vfsname DB ?AUXDB? 5538 ** 5539 ** Return a string that describes the stack of VFSes. 5540 */ 5541 static int file_control_vfsname( 5542 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5543 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5544 int objc, /* Number of arguments */ 5545 Tcl_Obj *CONST objv[] /* Command arguments */ 5546 ){ 5547 sqlite3 *db; 5548 const char *zDbName = "main"; 5549 char *zVfsName = 0; 5550 5551 if( objc!=2 && objc!=3 ){ 5552 Tcl_AppendResult(interp, "wrong # args: should be \"", 5553 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5554 return TCL_ERROR; 5555 } 5556 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5557 return TCL_ERROR; 5558 } 5559 if( objc==3 ){ 5560 zDbName = Tcl_GetString(objv[2]); 5561 } 5562 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); 5563 Tcl_AppendResult(interp, zVfsName, (char*)0); 5564 sqlite3_free(zVfsName); 5565 return TCL_OK; 5566 } 5567 5568 /* 5569 ** tclcmd: file_control_tempfilename DB ?AUXDB? 5570 ** 5571 ** Return a string that is a temporary filename 5572 */ 5573 static int file_control_tempfilename( 5574 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5575 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5576 int objc, /* Number of arguments */ 5577 Tcl_Obj *CONST objv[] /* Command arguments */ 5578 ){ 5579 sqlite3 *db; 5580 const char *zDbName = "main"; 5581 char *zTName = 0; 5582 5583 if( objc!=2 && objc!=3 ){ 5584 Tcl_AppendResult(interp, "wrong # args: should be \"", 5585 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5586 return TCL_ERROR; 5587 } 5588 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5589 return TCL_ERROR; 5590 } 5591 if( objc==3 ){ 5592 zDbName = Tcl_GetString(objv[2]); 5593 } 5594 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName); 5595 Tcl_AppendResult(interp, zTName, (char*)0); 5596 sqlite3_free(zTName); 5597 return TCL_OK; 5598 } 5599 5600 5601 /* 5602 ** tclcmd: sqlite3_vfs_list 5603 ** 5604 ** Return a tcl list containing the names of all registered vfs's. 5605 */ 5606 static int vfs_list( 5607 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5608 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5609 int objc, /* Number of arguments */ 5610 Tcl_Obj *CONST objv[] /* Command arguments */ 5611 ){ 5612 sqlite3_vfs *pVfs; 5613 Tcl_Obj *pRet = Tcl_NewObj(); 5614 if( objc!=1 ){ 5615 Tcl_WrongNumArgs(interp, 1, objv, ""); 5616 return TCL_ERROR; 5617 } 5618 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 5619 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); 5620 } 5621 Tcl_SetObjResult(interp, pRet); 5622 return TCL_OK; 5623 } 5624 5625 /* 5626 ** tclcmd: sqlite3_limit DB ID VALUE 5627 ** 5628 ** This TCL command runs the sqlite3_limit interface and 5629 ** verifies correct operation of the same. 5630 */ 5631 static int test_limit( 5632 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5633 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5634 int objc, /* Number of arguments */ 5635 Tcl_Obj *CONST objv[] /* Command arguments */ 5636 ){ 5637 sqlite3 *db; 5638 int rc; 5639 static const struct { 5640 char *zName; 5641 int id; 5642 } aId[] = { 5643 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, 5644 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, 5645 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, 5646 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, 5647 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, 5648 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, 5649 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, 5650 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, 5651 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 5652 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, 5653 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, 5654 { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS }, 5655 5656 /* Out of range test cases */ 5657 { "SQLITE_LIMIT_TOOSMALL", -1, }, 5658 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_WORKER_THREADS+1 }, 5659 }; 5660 int i, id = 0; 5661 int val; 5662 const char *zId; 5663 5664 if( objc!=4 ){ 5665 Tcl_AppendResult(interp, "wrong # args: should be \"", 5666 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); 5667 return TCL_ERROR; 5668 } 5669 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5670 zId = Tcl_GetString(objv[2]); 5671 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ 5672 if( strcmp(zId, aId[i].zName)==0 ){ 5673 id = aId[i].id; 5674 break; 5675 } 5676 } 5677 if( i>=sizeof(aId)/sizeof(aId[0]) ){ 5678 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); 5679 return TCL_ERROR; 5680 } 5681 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 5682 rc = sqlite3_limit(db, id, val); 5683 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5684 return TCL_OK; 5685 } 5686 5687 /* 5688 ** tclcmd: save_prng_state 5689 ** 5690 ** Save the state of the pseudo-random number generator. 5691 ** At the same time, verify that sqlite3_test_control works even when 5692 ** called with an out-of-range opcode. 5693 */ 5694 static int save_prng_state( 5695 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5696 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5697 int objc, /* Number of arguments */ 5698 Tcl_Obj *CONST objv[] /* Command arguments */ 5699 ){ 5700 int rc = sqlite3_test_control(9999); 5701 assert( rc==0 ); 5702 rc = sqlite3_test_control(-1); 5703 assert( rc==0 ); 5704 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); 5705 return TCL_OK; 5706 } 5707 /* 5708 ** tclcmd: restore_prng_state 5709 */ 5710 static int restore_prng_state( 5711 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5712 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5713 int objc, /* Number of arguments */ 5714 Tcl_Obj *CONST objv[] /* Command arguments */ 5715 ){ 5716 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); 5717 return TCL_OK; 5718 } 5719 /* 5720 ** tclcmd: reset_prng_state 5721 */ 5722 static int reset_prng_state( 5723 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5724 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5725 int objc, /* Number of arguments */ 5726 Tcl_Obj *CONST objv[] /* Command arguments */ 5727 ){ 5728 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); 5729 return TCL_OK; 5730 } 5731 5732 /* 5733 ** tclcmd: database_may_be_corrupt 5734 ** 5735 ** Indicate that database files might be corrupt. In other words, set the normal 5736 ** state of operation. 5737 */ 5738 static int database_may_be_corrupt( 5739 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5740 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5741 int objc, /* Number of arguments */ 5742 Tcl_Obj *CONST objv[] /* Command arguments */ 5743 ){ 5744 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0); 5745 return TCL_OK; 5746 } 5747 /* 5748 ** tclcmd: database_never_corrupt 5749 ** 5750 ** Indicate that database files are always well-formed. This enables extra assert() 5751 ** statements that test conditions that are always true for well-formed databases. 5752 */ 5753 static int database_never_corrupt( 5754 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5755 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5756 int objc, /* Number of arguments */ 5757 Tcl_Obj *CONST objv[] /* Command arguments */ 5758 ){ 5759 sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1); 5760 return TCL_OK; 5761 } 5762 5763 /* 5764 ** tclcmd: pcache_stats 5765 */ 5766 static int test_pcache_stats( 5767 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5768 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5769 int objc, /* Number of arguments */ 5770 Tcl_Obj *CONST objv[] /* Command arguments */ 5771 ){ 5772 int nMin; 5773 int nMax; 5774 int nCurrent; 5775 int nRecyclable; 5776 Tcl_Obj *pRet; 5777 5778 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); 5779 5780 pRet = Tcl_NewObj(); 5781 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); 5782 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); 5783 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); 5784 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); 5785 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); 5786 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); 5787 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); 5788 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); 5789 5790 Tcl_SetObjResult(interp, pRet); 5791 5792 return TCL_OK; 5793 } 5794 5795 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5796 static void test_unlock_notify_cb(void **aArg, int nArg){ 5797 int ii; 5798 for(ii=0; ii<nArg; ii++){ 5799 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); 5800 } 5801 } 5802 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ 5803 5804 /* 5805 ** tclcmd: sqlite3_unlock_notify db 5806 */ 5807 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5808 static int test_unlock_notify( 5809 ClientData clientData, /* Unused */ 5810 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5811 int objc, /* Number of arguments */ 5812 Tcl_Obj *CONST objv[] /* Command arguments */ 5813 ){ 5814 sqlite3 *db; 5815 int rc; 5816 5817 if( objc!=2 ){ 5818 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 5819 return TCL_ERROR; 5820 } 5821 5822 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5823 return TCL_ERROR; 5824 } 5825 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); 5826 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5827 return TCL_OK; 5828 } 5829 #endif 5830 5831 /* 5832 ** tclcmd: sqlite3_wal_checkpoint db ?NAME? 5833 */ 5834 static int test_wal_checkpoint( 5835 ClientData clientData, /* Unused */ 5836 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5837 int objc, /* Number of arguments */ 5838 Tcl_Obj *CONST objv[] /* Command arguments */ 5839 ){ 5840 char *zDb = 0; 5841 sqlite3 *db; 5842 int rc; 5843 5844 if( objc!=3 && objc!=2 ){ 5845 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); 5846 return TCL_ERROR; 5847 } 5848 5849 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5850 return TCL_ERROR; 5851 } 5852 if( objc==3 ){ 5853 zDb = Tcl_GetString(objv[2]); 5854 } 5855 rc = sqlite3_wal_checkpoint(db, zDb); 5856 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5857 return TCL_OK; 5858 } 5859 5860 /* 5861 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? 5862 ** 5863 ** This command calls the wal_checkpoint_v2() function with the specified 5864 ** mode argument (passive, full or restart). If present, the database name 5865 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the 5866 ** NAME argument is not present, a NULL pointer is passed instead. 5867 ** 5868 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or 5869 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set 5870 ** to the error message obtained from sqlite3_errmsg(). 5871 ** 5872 ** Otherwise, this command returns a list of three integers. The first integer 5873 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers 5874 ** are the values returned via the output parameters by wal_checkpoint_v2() - 5875 ** the number of frames in the log and the number of frames in the log 5876 ** that have been checkpointed. 5877 */ 5878 static int test_wal_checkpoint_v2( 5879 ClientData clientData, /* Unused */ 5880 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5881 int objc, /* Number of arguments */ 5882 Tcl_Obj *CONST objv[] /* Command arguments */ 5883 ){ 5884 char *zDb = 0; 5885 sqlite3 *db; 5886 int rc; 5887 5888 int eMode; 5889 int nLog = -555; 5890 int nCkpt = -555; 5891 Tcl_Obj *pRet; 5892 5893 const char * aMode[] = { "passive", "full", "restart", "truncate", 0 }; 5894 assert( SQLITE_CHECKPOINT_PASSIVE==0 ); 5895 assert( SQLITE_CHECKPOINT_FULL==1 ); 5896 assert( SQLITE_CHECKPOINT_RESTART==2 ); 5897 assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); 5898 5899 if( objc!=3 && objc!=4 ){ 5900 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); 5901 return TCL_ERROR; 5902 } 5903 5904 if( objc==4 ){ 5905 zDb = Tcl_GetString(objv[3]); 5906 } 5907 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || ( 5908 TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode) 5909 && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) 5910 )){ 5911 return TCL_ERROR; 5912 } 5913 5914 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); 5915 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 5916 const char *zErrCode = sqlite3ErrName(rc); 5917 Tcl_ResetResult(interp); 5918 Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0); 5919 return TCL_ERROR; 5920 } 5921 5922 pRet = Tcl_NewObj(); 5923 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); 5924 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); 5925 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); 5926 Tcl_SetObjResult(interp, pRet); 5927 5928 return TCL_OK; 5929 } 5930 5931 /* 5932 ** tclcmd: sqlite3_wal_autocheckpoint db VALUE 5933 */ 5934 static int test_wal_autocheckpoint( 5935 ClientData clientData, /* Unused */ 5936 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5937 int objc, /* Number of arguments */ 5938 Tcl_Obj *CONST objv[] /* Command arguments */ 5939 ){ 5940 sqlite3 *db; 5941 int rc; 5942 int iVal; 5943 5944 5945 if( objc!=3 ){ 5946 Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE"); 5947 return TCL_ERROR; 5948 } 5949 5950 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5951 || Tcl_GetIntFromObj(0, objv[2], &iVal) 5952 ){ 5953 return TCL_ERROR; 5954 } 5955 5956 rc = sqlite3_wal_autocheckpoint(db, iVal); 5957 Tcl_ResetResult(interp); 5958 if( rc!=SQLITE_OK ){ 5959 const char *zErrCode = sqlite3ErrName(rc); 5960 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1)); 5961 return TCL_ERROR; 5962 } 5963 5964 return TCL_OK; 5965 } 5966 5967 5968 /* 5969 ** tclcmd: test_sqlite3_log ?SCRIPT? 5970 */ 5971 static struct LogCallback { 5972 Tcl_Interp *pInterp; 5973 Tcl_Obj *pObj; 5974 } logcallback = {0, 0}; 5975 static void xLogcallback(void *unused, int err, char *zMsg){ 5976 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); 5977 Tcl_IncrRefCount(pNew); 5978 Tcl_ListObjAppendElement( 5979 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1) 5980 ); 5981 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); 5982 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 5983 Tcl_DecrRefCount(pNew); 5984 } 5985 static int test_sqlite3_log( 5986 ClientData clientData, 5987 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5988 int objc, /* Number of arguments */ 5989 Tcl_Obj *CONST objv[] /* Command arguments */ 5990 ){ 5991 if( objc>2 ){ 5992 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); 5993 return TCL_ERROR; 5994 } 5995 if( logcallback.pObj ){ 5996 Tcl_DecrRefCount(logcallback.pObj); 5997 logcallback.pObj = 0; 5998 logcallback.pInterp = 0; 5999 sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0); 6000 } 6001 if( objc>1 ){ 6002 logcallback.pObj = objv[1]; 6003 Tcl_IncrRefCount(logcallback.pObj); 6004 logcallback.pInterp = interp; 6005 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0); 6006 } 6007 return TCL_OK; 6008 } 6009 6010 /* 6011 ** tcl_objproc COMMANDNAME ARGS... 6012 ** 6013 ** Run a TCL command using its objProc interface. Throw an error if 6014 ** the command has no objProc interface. 6015 */ 6016 static int runAsObjProc( 6017 void * clientData, 6018 Tcl_Interp *interp, 6019 int objc, 6020 Tcl_Obj *CONST objv[] 6021 ){ 6022 Tcl_CmdInfo cmdInfo; 6023 if( objc<2 ){ 6024 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); 6025 return TCL_ERROR; 6026 } 6027 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 6028 Tcl_AppendResult(interp, "command not found: ", 6029 Tcl_GetString(objv[1]), (char*)0); 6030 return TCL_ERROR; 6031 } 6032 if( cmdInfo.objProc==0 ){ 6033 Tcl_AppendResult(interp, "command has no objProc: ", 6034 Tcl_GetString(objv[1]), (char*)0); 6035 return TCL_ERROR; 6036 } 6037 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); 6038 } 6039 6040 #ifndef SQLITE_OMIT_EXPLAIN 6041 /* 6042 ** WARNING: The following function, printExplainQueryPlan() is an exact 6043 ** copy of example code from eqp.in (eqp.html). If this code is modified, 6044 ** then the documentation copy needs to be modified as well. 6045 */ 6046 /* 6047 ** Argument pStmt is a prepared SQL statement. This function compiles 6048 ** an EXPLAIN QUERY PLAN command to report on the prepared statement, 6049 ** and prints the report to stdout using printf(). 6050 */ 6051 int printExplainQueryPlan(sqlite3_stmt *pStmt){ 6052 const char *zSql; /* Input SQL */ 6053 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ 6054 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ 6055 int rc; /* Return code from sqlite3_prepare_v2() */ 6056 6057 zSql = sqlite3_sql(pStmt); 6058 if( zSql==0 ) return SQLITE_ERROR; 6059 6060 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); 6061 if( zExplain==0 ) return SQLITE_NOMEM; 6062 6063 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); 6064 sqlite3_free(zExplain); 6065 if( rc!=SQLITE_OK ) return rc; 6066 6067 while( SQLITE_ROW==sqlite3_step(pExplain) ){ 6068 int iSelectid = sqlite3_column_int(pExplain, 0); 6069 int iOrder = sqlite3_column_int(pExplain, 1); 6070 int iFrom = sqlite3_column_int(pExplain, 2); 6071 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); 6072 6073 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); 6074 } 6075 6076 return sqlite3_finalize(pExplain); 6077 } 6078 6079 static int test_print_eqp( 6080 void * clientData, 6081 Tcl_Interp *interp, 6082 int objc, 6083 Tcl_Obj *CONST objv[] 6084 ){ 6085 int rc; 6086 sqlite3_stmt *pStmt; 6087 6088 if( objc!=2 ){ 6089 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 6090 return TCL_ERROR; 6091 } 6092 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 6093 rc = printExplainQueryPlan(pStmt); 6094 /* This is needed on Windows so that a test case using this 6095 ** function can open a read pipe and get the output of 6096 ** printExplainQueryPlan() immediately. 6097 */ 6098 fflush(stdout); 6099 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 6100 return TCL_OK; 6101 } 6102 #endif /* SQLITE_OMIT_EXPLAIN */ 6103 6104 /* 6105 ** sqlite3_test_control VERB ARGS... 6106 */ 6107 static int test_test_control( 6108 void * clientData, 6109 Tcl_Interp *interp, 6110 int objc, 6111 Tcl_Obj *CONST objv[] 6112 ){ 6113 struct Verb { 6114 const char *zName; 6115 int i; 6116 } aVerb[] = { 6117 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, 6118 { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP }, 6119 { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER }, 6120 }; 6121 int iVerb; 6122 int iFlag; 6123 int rc; 6124 6125 if( objc<2 ){ 6126 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); 6127 return TCL_ERROR; 6128 } 6129 6130 rc = Tcl_GetIndexFromObjStruct( 6131 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb 6132 ); 6133 if( rc!=TCL_OK ) return rc; 6134 6135 iFlag = aVerb[iVerb].i; 6136 switch( iFlag ){ 6137 case SQLITE_TESTCTRL_LOCALTIME_FAULT: { 6138 int val; 6139 if( objc!=3 ){ 6140 Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); 6141 return TCL_ERROR; 6142 } 6143 if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 6144 sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); 6145 break; 6146 } 6147 6148 case SQLITE_TESTCTRL_SORTER_MMAP: { 6149 int val; 6150 sqlite3 *db; 6151 if( objc!=4 ){ 6152 Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT"); 6153 return TCL_ERROR; 6154 } 6155 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; 6156 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 6157 sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val); 6158 break; 6159 } 6160 6161 case SQLITE_TESTCTRL_IMPOSTER: { 6162 int onOff, tnum; 6163 const char *zDbName; 6164 sqlite3 *db; 6165 if( objc!=6 ){ 6166 Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum"); 6167 return TCL_ERROR; 6168 } 6169 if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; 6170 zDbName = Tcl_GetString(objv[3]); 6171 if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR; 6172 if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR; 6173 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum); 6174 break; 6175 } 6176 } 6177 6178 Tcl_ResetResult(interp); 6179 return TCL_OK; 6180 } 6181 6182 #if SQLITE_OS_UNIX 6183 #include <sys/time.h> 6184 #include <sys/resource.h> 6185 6186 static int test_getrusage( 6187 void * clientData, 6188 Tcl_Interp *interp, 6189 int objc, 6190 Tcl_Obj *CONST objv[] 6191 ){ 6192 char buf[1024]; 6193 struct rusage r; 6194 memset(&r, 0, sizeof(r)); 6195 getrusage(RUSAGE_SELF, &r); 6196 6197 sqlite3_snprintf(sizeof(buf), buf, 6198 "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", 6199 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, 6200 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, 6201 (int)r.ru_minflt, (int)r.ru_majflt 6202 ); 6203 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1)); 6204 return TCL_OK; 6205 } 6206 #endif 6207 6208 #if SQLITE_OS_WIN 6209 /* 6210 ** Information passed from the main thread into the windows file locker 6211 ** background thread. 6212 */ 6213 struct win32FileLocker { 6214 char *evName; /* Name of event to signal thread startup */ 6215 HANDLE h; /* Handle of the file to be locked */ 6216 int delay1; /* Delay before locking */ 6217 int delay2; /* Delay before unlocking */ 6218 int ok; /* Finished ok */ 6219 int err; /* True if an error occurs */ 6220 }; 6221 #endif 6222 6223 6224 #if SQLITE_OS_WIN 6225 #include <process.h> 6226 /* 6227 ** The background thread that does file locking. 6228 */ 6229 static void win32_file_locker(void *pAppData){ 6230 struct win32FileLocker *p = (struct win32FileLocker*)pAppData; 6231 if( p->evName ){ 6232 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); 6233 if ( ev ){ 6234 SetEvent(ev); 6235 CloseHandle(ev); 6236 } 6237 } 6238 if( p->delay1 ) Sleep(p->delay1); 6239 if( LockFile(p->h, 0, 0, 100000000, 0) ){ 6240 Sleep(p->delay2); 6241 UnlockFile(p->h, 0, 0, 100000000, 0); 6242 p->ok = 1; 6243 }else{ 6244 p->err = 1; 6245 } 6246 CloseHandle(p->h); 6247 p->h = 0; 6248 p->delay1 = 0; 6249 p->delay2 = 0; 6250 } 6251 #endif 6252 6253 #if SQLITE_OS_WIN 6254 /* 6255 ** lock_win32_file FILENAME DELAY1 DELAY2 6256 ** 6257 ** Get an exclusive manditory lock on file for DELAY2 milliseconds. 6258 ** Wait DELAY1 milliseconds before acquiring the lock. 6259 */ 6260 static int win32_file_lock( 6261 void * clientData, 6262 Tcl_Interp *interp, 6263 int objc, 6264 Tcl_Obj *CONST objv[] 6265 ){ 6266 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; 6267 const char *zFilename; 6268 char zBuf[200]; 6269 int retry = 0; 6270 HANDLE ev; 6271 DWORD wResult; 6272 6273 if( objc!=4 && objc!=1 ){ 6274 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); 6275 return TCL_ERROR; 6276 } 6277 if( objc==1 ){ 6278 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", 6279 x.ok, x.err, x.delay1, x.delay2, x.h); 6280 Tcl_AppendResult(interp, zBuf, (char*)0); 6281 return TCL_OK; 6282 } 6283 while( x.h && retry<30 ){ 6284 retry++; 6285 Sleep(100); 6286 } 6287 if( x.h ){ 6288 Tcl_AppendResult(interp, "busy", (char*)0); 6289 return TCL_ERROR; 6290 } 6291 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; 6292 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; 6293 zFilename = Tcl_GetString(objv[1]); 6294 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, 6295 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 6296 FILE_ATTRIBUTE_NORMAL, 0); 6297 if( !x.h ){ 6298 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); 6299 return TCL_ERROR; 6300 } 6301 ev = CreateEvent(NULL, TRUE, FALSE, x.evName); 6302 if ( !ev ){ 6303 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); 6304 return TCL_ERROR; 6305 } 6306 _beginthread(win32_file_locker, 0, (void*)&x); 6307 Sleep(0); 6308 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ 6309 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); 6310 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); 6311 CloseHandle(ev); 6312 return TCL_ERROR; 6313 } 6314 CloseHandle(ev); 6315 return TCL_OK; 6316 } 6317 6318 /* 6319 ** exists_win32_path PATH 6320 ** 6321 ** Returns non-zero if the specified path exists, whose fully qualified name 6322 ** may exceed 260 characters if it is prefixed with "\\?\". 6323 */ 6324 static int win32_exists_path( 6325 void *clientData, 6326 Tcl_Interp *interp, 6327 int objc, 6328 Tcl_Obj *CONST objv[] 6329 ){ 6330 if( objc!=2 ){ 6331 Tcl_WrongNumArgs(interp, 1, objv, "PATH"); 6332 return TCL_ERROR; 6333 } 6334 Tcl_SetObjResult(interp, Tcl_NewBooleanObj( 6335 GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES )); 6336 return TCL_OK; 6337 } 6338 6339 /* 6340 ** find_win32_file PATTERN 6341 ** 6342 ** Returns a list of entries in a directory that match the specified pattern, 6343 ** whose fully qualified name may exceed 248 characters if it is prefixed with 6344 ** "\\?\". 6345 */ 6346 static int win32_find_file( 6347 void *clientData, 6348 Tcl_Interp *interp, 6349 int objc, 6350 Tcl_Obj *CONST objv[] 6351 ){ 6352 HANDLE hFindFile = INVALID_HANDLE_VALUE; 6353 WIN32_FIND_DATAW findData; 6354 Tcl_Obj *listObj; 6355 DWORD lastErrno; 6356 if( objc!=2 ){ 6357 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN"); 6358 return TCL_ERROR; 6359 } 6360 hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData); 6361 if( hFindFile==INVALID_HANDLE_VALUE ){ 6362 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6363 return TCL_ERROR; 6364 } 6365 listObj = Tcl_NewObj(); 6366 Tcl_IncrRefCount(listObj); 6367 do { 6368 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj( 6369 findData.cFileName, -1)); 6370 Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj( 6371 findData.dwFileAttributes)); 6372 } while( FindNextFileW(hFindFile, &findData) ); 6373 lastErrno = GetLastError(); 6374 if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){ 6375 FindClose(hFindFile); 6376 Tcl_DecrRefCount(listObj); 6377 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6378 return TCL_ERROR; 6379 } 6380 FindClose(hFindFile); 6381 Tcl_SetObjResult(interp, listObj); 6382 return TCL_OK; 6383 } 6384 6385 /* 6386 ** delete_win32_file FILENAME 6387 ** 6388 ** Deletes the specified file, whose fully qualified name may exceed 260 6389 ** characters if it is prefixed with "\\?\". 6390 */ 6391 static int win32_delete_file( 6392 void *clientData, 6393 Tcl_Interp *interp, 6394 int objc, 6395 Tcl_Obj *CONST objv[] 6396 ){ 6397 if( objc!=2 ){ 6398 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 6399 return TCL_ERROR; 6400 } 6401 if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){ 6402 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6403 return TCL_ERROR; 6404 } 6405 Tcl_ResetResult(interp); 6406 return TCL_OK; 6407 } 6408 6409 /* 6410 ** make_win32_dir DIRECTORY 6411 ** 6412 ** Creates the specified directory, whose fully qualified name may exceed 248 6413 ** characters if it is prefixed with "\\?\". 6414 */ 6415 static int win32_mkdir( 6416 void *clientData, 6417 Tcl_Interp *interp, 6418 int objc, 6419 Tcl_Obj *CONST objv[] 6420 ){ 6421 if( objc!=2 ){ 6422 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); 6423 return TCL_ERROR; 6424 } 6425 if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){ 6426 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6427 return TCL_ERROR; 6428 } 6429 Tcl_ResetResult(interp); 6430 return TCL_OK; 6431 } 6432 6433 /* 6434 ** remove_win32_dir DIRECTORY 6435 ** 6436 ** Removes the specified directory, whose fully qualified name may exceed 248 6437 ** characters if it is prefixed with "\\?\". 6438 */ 6439 static int win32_rmdir( 6440 void *clientData, 6441 Tcl_Interp *interp, 6442 int objc, 6443 Tcl_Obj *CONST objv[] 6444 ){ 6445 if( objc!=2 ){ 6446 Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); 6447 return TCL_ERROR; 6448 } 6449 if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){ 6450 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); 6451 return TCL_ERROR; 6452 } 6453 Tcl_ResetResult(interp); 6454 return TCL_OK; 6455 } 6456 #endif 6457 6458 6459 /* 6460 ** optimization_control DB OPT BOOLEAN 6461 ** 6462 ** Enable or disable query optimizations using the sqlite3_test_control() 6463 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. 6464 ** OPT is the name of the optimization to be disabled. 6465 */ 6466 static int optimization_control( 6467 void * clientData, 6468 Tcl_Interp *interp, 6469 int objc, 6470 Tcl_Obj *CONST objv[] 6471 ){ 6472 int i; 6473 sqlite3 *db; 6474 const char *zOpt; 6475 int onoff; 6476 int mask = 0; 6477 static const struct { 6478 const char *zOptName; 6479 int mask; 6480 } aOpt[] = { 6481 { "all", SQLITE_AllOpts }, 6482 { "none", 0 }, 6483 { "query-flattener", SQLITE_QueryFlattener }, 6484 { "column-cache", SQLITE_ColumnCache }, 6485 { "groupby-order", SQLITE_GroupByOrder }, 6486 { "factor-constants", SQLITE_FactorOutConst }, 6487 { "distinct-opt", SQLITE_DistinctOpt }, 6488 { "cover-idx-scan", SQLITE_CoverIdxScan }, 6489 { "order-by-idx-join", SQLITE_OrderByIdxJoin }, 6490 { "transitive", SQLITE_Transitive }, 6491 { "subquery-coroutine", SQLITE_SubqCoroutine }, 6492 { "omit-noop-join", SQLITE_OmitNoopJoin }, 6493 { "stat3", SQLITE_Stat34 }, 6494 { "stat4", SQLITE_Stat34 }, 6495 }; 6496 6497 if( objc!=4 ){ 6498 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); 6499 return TCL_ERROR; 6500 } 6501 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6502 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; 6503 zOpt = Tcl_GetString(objv[2]); 6504 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 6505 if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ 6506 mask = aOpt[i].mask; 6507 break; 6508 } 6509 } 6510 if( onoff ) mask = ~mask; 6511 if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ 6512 Tcl_AppendResult(interp, "unknown optimization - should be one of:", 6513 (char*)0); 6514 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 6515 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0); 6516 } 6517 return TCL_ERROR; 6518 } 6519 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); 6520 return TCL_OK; 6521 } 6522 6523 typedef struct sqlite3_api_routines sqlite3_api_routines; 6524 /* 6525 ** load_static_extension DB NAME ... 6526 ** 6527 ** Load one or more statically linked extensions. 6528 */ 6529 static int tclLoadStaticExtensionCmd( 6530 void * clientData, 6531 Tcl_Interp *interp, 6532 int objc, 6533 Tcl_Obj *CONST objv[] 6534 ){ 6535 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); 6536 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); 6537 extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*); 6538 extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*); 6539 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); 6540 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); 6541 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); 6542 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*); 6543 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); 6544 extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*); 6545 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); 6546 extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); 6547 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); 6548 static const struct { 6549 const char *zExtName; 6550 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); 6551 } aExtension[] = { 6552 { "amatch", sqlite3_amatch_init }, 6553 { "closure", sqlite3_closure_init }, 6554 { "eval", sqlite3_eval_init }, 6555 { "fileio", sqlite3_fileio_init }, 6556 { "fuzzer", sqlite3_fuzzer_init }, 6557 { "ieee754", sqlite3_ieee_init }, 6558 { "nextchar", sqlite3_nextchar_init }, 6559 { "percentile", sqlite3_percentile_init }, 6560 { "regexp", sqlite3_regexp_init }, 6561 { "series", sqlite3_series_init }, 6562 { "spellfix", sqlite3_spellfix_init }, 6563 { "totype", sqlite3_totype_init }, 6564 { "wholenumber", sqlite3_wholenumber_init }, 6565 }; 6566 sqlite3 *db; 6567 const char *zName; 6568 int i, j, rc; 6569 char *zErrMsg = 0; 6570 if( objc<3 ){ 6571 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); 6572 return TCL_ERROR; 6573 } 6574 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6575 for(j=2; j<objc; j++){ 6576 zName = Tcl_GetString(objv[j]); 6577 for(i=0; i<ArraySize(aExtension); i++){ 6578 if( strcmp(zName, aExtension[i].zExtName)==0 ) break; 6579 } 6580 if( i>=ArraySize(aExtension) ){ 6581 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0); 6582 return TCL_ERROR; 6583 } 6584 if( aExtension[i].pInit ){ 6585 rc = aExtension[i].pInit(db, &zErrMsg, 0); 6586 }else{ 6587 rc = SQLITE_OK; 6588 } 6589 if( rc!=SQLITE_OK || zErrMsg ){ 6590 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, 6591 (char*)0); 6592 sqlite3_free(zErrMsg); 6593 return TCL_ERROR; 6594 } 6595 } 6596 return TCL_OK; 6597 } 6598 6599 /* 6600 ** sorter_test_fakeheap BOOL 6601 ** 6602 */ 6603 static int sorter_test_fakeheap( 6604 void * clientData, 6605 Tcl_Interp *interp, 6606 int objc, 6607 Tcl_Obj *CONST objv[] 6608 ){ 6609 int bArg; 6610 if( objc!=2 ){ 6611 Tcl_WrongNumArgs(interp, 1, objv, "BOOL"); 6612 return TCL_ERROR; 6613 } 6614 6615 if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){ 6616 return TCL_ERROR; 6617 } 6618 6619 if( bArg ){ 6620 if( sqlite3GlobalConfig.pHeap==0 ){ 6621 sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1); 6622 } 6623 }else{ 6624 if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){ 6625 sqlite3GlobalConfig.pHeap = 0; 6626 } 6627 } 6628 6629 Tcl_ResetResult(interp); 6630 return TCL_OK; 6631 } 6632 6633 /* 6634 ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2 6635 ** 6636 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row, 6637 ** check that the leftmost and rightmost columns returned are both integers, 6638 ** and that both contain the same value. 6639 ** 6640 ** Then execute statement $SQL2. Check that the statement returns the same 6641 ** set of integers in the same order as in the previous step (using $SQL1). 6642 */ 6643 static int sorter_test_sort4_helper( 6644 void * clientData, 6645 Tcl_Interp *interp, 6646 int objc, 6647 Tcl_Obj *CONST objv[] 6648 ){ 6649 const char *zSql1; 6650 const char *zSql2; 6651 int nStep; 6652 int iStep; 6653 int iCksum1 = 0; 6654 int iCksum2 = 0; 6655 int rc; 6656 int iB; 6657 sqlite3 *db; 6658 sqlite3_stmt *pStmt; 6659 6660 if( objc!=5 ){ 6661 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2"); 6662 return TCL_ERROR; 6663 } 6664 6665 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6666 zSql1 = Tcl_GetString(objv[2]); 6667 if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR; 6668 zSql2 = Tcl_GetString(objv[4]); 6669 6670 rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0); 6671 if( rc!=SQLITE_OK ) goto sql_error; 6672 6673 iB = sqlite3_column_count(pStmt)-1; 6674 for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){ 6675 int a = sqlite3_column_int(pStmt, 0); 6676 if( a!=sqlite3_column_int(pStmt, iB) ){ 6677 Tcl_AppendResult(interp, "data error: (a!=b)", 0); 6678 return TCL_ERROR; 6679 } 6680 6681 iCksum1 += (iCksum1 << 3) + a; 6682 } 6683 rc = sqlite3_finalize(pStmt); 6684 if( rc!=SQLITE_OK ) goto sql_error; 6685 6686 rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0); 6687 if( rc!=SQLITE_OK ) goto sql_error; 6688 for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){ 6689 int a = sqlite3_column_int(pStmt, 0); 6690 iCksum2 += (iCksum2 << 3) + a; 6691 } 6692 rc = sqlite3_finalize(pStmt); 6693 if( rc!=SQLITE_OK ) goto sql_error; 6694 6695 if( iCksum1!=iCksum2 ){ 6696 Tcl_AppendResult(interp, "checksum mismatch", 0); 6697 return TCL_ERROR; 6698 } 6699 6700 return TCL_OK; 6701 sql_error: 6702 Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0); 6703 return TCL_ERROR; 6704 } 6705 6706 6707 #ifdef SQLITE_USER_AUTHENTICATION 6708 #include "sqlite3userauth.h" 6709 /* 6710 ** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD 6711 */ 6712 static int test_user_authenticate( 6713 ClientData clientData, /* Unused */ 6714 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6715 int objc, /* Number of arguments */ 6716 Tcl_Obj *CONST objv[] /* Command arguments */ 6717 ){ 6718 char *zUser = 0; 6719 char *zPasswd = 0; 6720 int nPasswd = 0; 6721 sqlite3 *db; 6722 int rc; 6723 6724 if( objc!=4 ){ 6725 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD"); 6726 return TCL_ERROR; 6727 } 6728 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6729 return TCL_ERROR; 6730 } 6731 zUser = Tcl_GetString(objv[2]); 6732 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6733 rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd); 6734 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6735 return TCL_OK; 6736 } 6737 #endif /* SQLITE_USER_AUTHENTICATION */ 6738 6739 #ifdef SQLITE_USER_AUTHENTICATION 6740 /* 6741 ** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN 6742 */ 6743 static int test_user_add( 6744 ClientData clientData, /* Unused */ 6745 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6746 int objc, /* Number of arguments */ 6747 Tcl_Obj *CONST objv[] /* Command arguments */ 6748 ){ 6749 char *zUser = 0; 6750 char *zPasswd = 0; 6751 int nPasswd = 0; 6752 int isAdmin = 0; 6753 sqlite3 *db; 6754 int rc; 6755 6756 if( objc!=5 ){ 6757 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); 6758 return TCL_ERROR; 6759 } 6760 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6761 return TCL_ERROR; 6762 } 6763 zUser = Tcl_GetString(objv[2]); 6764 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6765 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); 6766 rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin); 6767 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6768 return TCL_OK; 6769 } 6770 #endif /* SQLITE_USER_AUTHENTICATION */ 6771 6772 #ifdef SQLITE_USER_AUTHENTICATION 6773 /* 6774 ** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN 6775 */ 6776 static int test_user_change( 6777 ClientData clientData, /* Unused */ 6778 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6779 int objc, /* Number of arguments */ 6780 Tcl_Obj *CONST objv[] /* Command arguments */ 6781 ){ 6782 char *zUser = 0; 6783 char *zPasswd = 0; 6784 int nPasswd = 0; 6785 int isAdmin = 0; 6786 sqlite3 *db; 6787 int rc; 6788 6789 if( objc!=5 ){ 6790 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); 6791 return TCL_ERROR; 6792 } 6793 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6794 return TCL_ERROR; 6795 } 6796 zUser = Tcl_GetString(objv[2]); 6797 zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); 6798 Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); 6799 rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin); 6800 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6801 return TCL_OK; 6802 } 6803 #endif /* SQLITE_USER_AUTHENTICATION */ 6804 6805 #ifdef SQLITE_USER_AUTHENTICATION 6806 /* 6807 ** tclcmd: sqlite3_user_delete DB USERNAME 6808 */ 6809 static int test_user_delete( 6810 ClientData clientData, /* Unused */ 6811 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6812 int objc, /* Number of arguments */ 6813 Tcl_Obj *CONST objv[] /* Command arguments */ 6814 ){ 6815 char *zUser = 0; 6816 sqlite3 *db; 6817 int rc; 6818 6819 if( objc!=3 ){ 6820 Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME"); 6821 return TCL_ERROR; 6822 } 6823 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 6824 return TCL_ERROR; 6825 } 6826 zUser = Tcl_GetString(objv[2]); 6827 rc = sqlite3_user_delete(db, zUser); 6828 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 6829 return TCL_OK; 6830 } 6831 #endif /* SQLITE_USER_AUTHENTICATION */ 6832 6833 /* 6834 ** tclcmd: bad_behavior TYPE 6835 ** 6836 ** Do some things that should trigger a valgrind or -fsanitize=undefined 6837 ** warning. This is used to verify that errors and warnings output by those 6838 ** tools are detected by the test scripts. 6839 ** 6840 ** TYPE BEHAVIOR 6841 ** 1 Overflow a signed integer 6842 ** 2 Jump based on an uninitialized variable 6843 ** 3 Read after free 6844 ** 4 Panic 6845 */ 6846 static int test_bad_behavior( 6847 ClientData clientData, /* Pointer to an integer containing zero */ 6848 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 6849 int objc, /* Number of arguments */ 6850 Tcl_Obj *CONST objv[] /* Command arguments */ 6851 ){ 6852 int iType; 6853 int xyz; 6854 int i = *(int*)clientData; 6855 int j; 6856 int w[10]; 6857 int *a; 6858 if( objc!=2 ){ 6859 Tcl_WrongNumArgs(interp, 1, objv, "TYPE"); 6860 return TCL_ERROR; 6861 } 6862 if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR; 6863 switch( iType ){ 6864 case 1: { 6865 xyz = 0x7fffff00 - i; 6866 xyz += 0x100; 6867 Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz)); 6868 break; 6869 } 6870 case 2: { 6871 w[1] = 5; 6872 if( w[i]>0 ) w[1]++; 6873 Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1])); 6874 break; 6875 } 6876 case 3: { 6877 a = malloc( sizeof(int)*10 ); 6878 for(j=0; j<10; j++) a[j] = j; 6879 free(a); 6880 Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i])); 6881 break; 6882 } 6883 case 4: { 6884 Tcl_Panic("Deliberate panic"); 6885 break; 6886 } 6887 } 6888 return TCL_OK; 6889 } 6890 6891 /* 6892 ** tclcmd: register_dbstat_vtab DB 6893 ** 6894 ** Cause the dbstat virtual table to be available on the connection DB 6895 */ 6896 static int test_register_dbstat_vtab( 6897 void *clientData, 6898 Tcl_Interp *interp, 6899 int objc, 6900 Tcl_Obj *CONST objv[] 6901 ){ 6902 #ifdef SQLITE_OMIT_VIRTUALTABLE 6903 Tcl_AppendResult(interp, "dbstat not available because of " 6904 "SQLITE_OMIT_VIRTUALTABLE", (void*)0); 6905 return TCL_ERROR; 6906 #else 6907 struct SqliteDb { sqlite3 *db; }; 6908 char *zDb; 6909 Tcl_CmdInfo cmdInfo; 6910 6911 if( objc!=2 ){ 6912 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 6913 return TCL_ERROR; 6914 } 6915 6916 zDb = Tcl_GetString(objv[1]); 6917 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 6918 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 6919 sqlite3DbstatRegister(db); 6920 } 6921 return TCL_OK; 6922 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6923 } 6924 6925 /* 6926 ** tclcmd: sqlite3_db_config DB SETTING VALUE 6927 ** 6928 ** Invoke sqlite3_db_config() for one of the setting values. 6929 */ 6930 static int test_sqlite3_db_config( 6931 void *clientData, 6932 Tcl_Interp *interp, 6933 int objc, 6934 Tcl_Obj *CONST objv[] 6935 ){ 6936 static const struct { 6937 const char *zName; 6938 int eVal; 6939 } aSetting[] = { 6940 { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY }, 6941 { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER }, 6942 { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, 6943 }; 6944 int i; 6945 int v; 6946 const char *zSetting; 6947 sqlite3 *db; 6948 6949 if( objc!=4 ){ 6950 Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING VALUE"); 6951 return TCL_ERROR; 6952 } 6953 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6954 zSetting = Tcl_GetString(objv[2]); 6955 if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7; 6956 if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9; 6957 if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7; 6958 for(i=0; i<ArraySize(aSetting); i++){ 6959 if( strcmp(zSetting, aSetting[i].zName)==0 ) break; 6960 } 6961 if( i>=ArraySize(aSetting) ){ 6962 Tcl_SetObjResult(interp, 6963 Tcl_NewStringObj("unknown sqlite3_db_config setting", -1)); 6964 return TCL_ERROR; 6965 } 6966 if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR; 6967 sqlite3_db_config(db, aSetting[i].eVal, v, &v); 6968 Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 6969 return TCL_OK; 6970 } 6971 6972 /* 6973 ** Register commands with the TCL interpreter. 6974 */ 6975 int Sqlitetest1_Init(Tcl_Interp *interp){ 6976 extern int sqlite3_search_count; 6977 extern int sqlite3_found_count; 6978 extern int sqlite3_interrupt_count; 6979 extern int sqlite3_open_file_count; 6980 extern int sqlite3_sort_count; 6981 extern int sqlite3_current_time; 6982 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6983 extern int sqlite3_hostid_num; 6984 #endif 6985 extern int sqlite3_max_blobsize; 6986 extern int sqlite3BtreeSharedCacheReport(void*, 6987 Tcl_Interp*,int,Tcl_Obj*CONST*); 6988 static int iZero = 0; 6989 static struct { 6990 char *zName; 6991 Tcl_CmdProc *xProc; 6992 } aCmd[] = { 6993 { "db_enter", (Tcl_CmdProc*)db_enter }, 6994 { "db_leave", (Tcl_CmdProc*)db_leave }, 6995 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, 6996 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, 6997 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, 6998 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, 6999 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, 7000 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, 7001 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, 7002 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, 7003 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, 7004 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, 7005 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, 7006 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, 7007 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, 7008 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, 7009 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, 7010 { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, 7011 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, 7012 #ifndef SQLITE_OMIT_GET_TABLE 7013 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, 7014 #endif 7015 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, 7016 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, 7017 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, 7018 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, 7019 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, 7020 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, 7021 { "sqlite_bind", (Tcl_CmdProc*)test_bind }, 7022 { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, 7023 { "sqlite3_key", (Tcl_CmdProc*)test_key }, 7024 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, 7025 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, 7026 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, 7027 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, 7028 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, 7029 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, 7030 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, 7031 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, 7032 { "printf", (Tcl_CmdProc*)test_printf }, 7033 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, 7034 { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address }, 7035 }; 7036 static struct { 7037 char *zName; 7038 Tcl_ObjCmdProc *xProc; 7039 void *clientData; 7040 } aObjCmd[] = { 7041 { "sqlite3_db_config", test_sqlite3_db_config, 0 }, 7042 { "bad_behavior", test_bad_behavior, (void*)&iZero }, 7043 { "register_dbstat_vtab", test_register_dbstat_vtab }, 7044 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, 7045 { "sqlite3_bind_int", test_bind_int, 0 }, 7046 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, 7047 { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 }, 7048 { "sqlite3_bind_int64", test_bind_int64, 0 }, 7049 { "sqlite3_bind_double", test_bind_double, 0 }, 7050 { "sqlite3_bind_null", test_bind_null ,0 }, 7051 { "sqlite3_bind_text", test_bind_text ,0 }, 7052 { "sqlite3_bind_text16", test_bind_text16 ,0 }, 7053 { "sqlite3_bind_blob", test_bind_blob ,0 }, 7054 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, 7055 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, 7056 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, 7057 { "sqlite3_clear_bindings", test_clear_bindings, 0}, 7058 { "sqlite3_sleep", test_sleep, 0}, 7059 { "sqlite3_errcode", test_errcode ,0 }, 7060 { "sqlite3_extended_errcode", test_ex_errcode ,0 }, 7061 { "sqlite3_errmsg", test_errmsg ,0 }, 7062 { "sqlite3_errmsg16", test_errmsg16 ,0 }, 7063 { "sqlite3_open", test_open ,0 }, 7064 { "sqlite3_open16", test_open16 ,0 }, 7065 { "sqlite3_open_v2", test_open_v2 ,0 }, 7066 { "sqlite3_complete16", test_complete16 ,0 }, 7067 7068 { "sqlite3_prepare", test_prepare ,0 }, 7069 { "sqlite3_prepare16", test_prepare16 ,0 }, 7070 { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, 7071 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, 7072 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, 7073 { "sqlite3_finalize", test_finalize ,0 }, 7074 { "sqlite3_stmt_status", test_stmt_status ,0 }, 7075 { "sqlite3_reset", test_reset ,0 }, 7076 { "sqlite3_expired", test_expired ,0 }, 7077 { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, 7078 { "sqlite3_changes", test_changes ,0 }, 7079 { "sqlite3_step", test_step ,0 }, 7080 { "sqlite3_sql", test_sql ,0 }, 7081 { "sqlite3_next_stmt", test_next_stmt ,0 }, 7082 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, 7083 { "sqlite3_stmt_busy", test_stmt_busy ,0 }, 7084 { "uses_stmt_journal", uses_stmt_journal ,0 }, 7085 7086 { "sqlite3_release_memory", test_release_memory, 0}, 7087 { "sqlite3_db_release_memory", test_db_release_memory, 0}, 7088 { "sqlite3_db_cacheflush", test_db_cacheflush, 0}, 7089 { "sqlite3_db_filename", test_db_filename, 0}, 7090 { "sqlite3_db_readonly", test_db_readonly, 0}, 7091 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, 7092 { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, 7093 { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, 7094 7095 { "sqlite3_load_extension", test_load_extension, 0}, 7096 { "sqlite3_enable_load_extension", test_enable_load, 0}, 7097 { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, 7098 { "sqlite3_limit", test_limit, 0}, 7099 7100 { "save_prng_state", save_prng_state, 0 }, 7101 { "restore_prng_state", restore_prng_state, 0 }, 7102 { "reset_prng_state", reset_prng_state, 0 }, 7103 { "database_never_corrupt", database_never_corrupt, 0}, 7104 { "database_may_be_corrupt", database_may_be_corrupt, 0}, 7105 { "optimization_control", optimization_control,0}, 7106 #if SQLITE_OS_WIN 7107 { "lock_win32_file", win32_file_lock, 0 }, 7108 { "exists_win32_path", win32_exists_path, 0 }, 7109 { "find_win32_file", win32_find_file, 0 }, 7110 { "delete_win32_file", win32_delete_file, 0 }, 7111 { "make_win32_dir", win32_mkdir, 0 }, 7112 { "remove_win32_dir", win32_rmdir, 0 }, 7113 #endif 7114 { "tcl_objproc", runAsObjProc, 0 }, 7115 7116 /* sqlite3_column_*() API */ 7117 { "sqlite3_column_count", test_column_count ,0 }, 7118 { "sqlite3_data_count", test_data_count ,0 }, 7119 { "sqlite3_column_type", test_column_type ,0 }, 7120 { "sqlite3_column_blob", test_column_blob ,0 }, 7121 { "sqlite3_column_double", test_column_double ,0 }, 7122 { "sqlite3_column_int64", test_column_int64 ,0 }, 7123 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, 7124 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, 7125 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, 7126 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, 7127 #ifndef SQLITE_OMIT_DECLTYPE 7128 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, 7129 #endif 7130 #ifdef SQLITE_ENABLE_COLUMN_METADATA 7131 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, 7132 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, 7133 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, 7134 #endif 7135 7136 #ifndef SQLITE_OMIT_UTF16 7137 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, 7138 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, 7139 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, 7140 { "add_alignment_test_collations", add_alignment_test_collations, 0 }, 7141 #ifndef SQLITE_OMIT_DECLTYPE 7142 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, 7143 #endif 7144 #ifdef SQLITE_ENABLE_COLUMN_METADATA 7145 {"sqlite3_column_database_name16", 7146 test_stmt_utf16, (void*)sqlite3_column_database_name16}, 7147 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, 7148 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, 7149 #endif 7150 #endif 7151 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, 7152 { "sqlite3_global_recover", test_global_recover, 0 }, 7153 { "working_64bit_int", working_64bit_int, 0 }, 7154 { "vfs_unlink_test", vfs_unlink_test, 0 }, 7155 { "vfs_initfail_test", vfs_initfail_test, 0 }, 7156 { "vfs_unregister_all", vfs_unregister_all, 0 }, 7157 { "vfs_reregister_all", vfs_reregister_all, 0 }, 7158 { "file_control_test", file_control_test, 0 }, 7159 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, 7160 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, 7161 { "file_control_chunksize_test", file_control_chunksize_test, 0 }, 7162 { "file_control_sizehint_test", file_control_sizehint_test, 0 }, 7163 #if SQLITE_OS_WIN 7164 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, 7165 { "file_control_win32_set_handle", file_control_win32_set_handle, 0 }, 7166 #endif 7167 { "file_control_persist_wal", file_control_persist_wal, 0 }, 7168 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, 7169 { "file_control_vfsname", file_control_vfsname, 0 }, 7170 { "file_control_tempfilename", file_control_tempfilename, 0 }, 7171 { "sqlite3_vfs_list", vfs_list, 0 }, 7172 { "sqlite3_create_function_v2", test_create_function_v2, 0 }, 7173 7174 /* Functions from os.h */ 7175 #ifndef SQLITE_OMIT_UTF16 7176 { "add_test_collate", test_collate, 0 }, 7177 { "add_test_collate_needed", test_collate_needed, 0 }, 7178 { "add_test_function", test_function, 0 }, 7179 { "add_test_utf16bin_collate", test_utf16bin_collate, 0 }, 7180 #endif 7181 { "sqlite3_test_errstr", test_errstr, 0 }, 7182 { "tcl_variable_type", tcl_variable_type, 0 }, 7183 #ifndef SQLITE_OMIT_SHARED_CACHE 7184 { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, 7185 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, 7186 #endif 7187 { "sqlite3_libversion_number", test_libversion_number, 0 }, 7188 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, 7189 #ifndef SQLITE_OMIT_INCRBLOB 7190 { "sqlite3_blob_reopen", test_blob_reopen, 0 }, 7191 #endif 7192 { "pcache_stats", test_pcache_stats, 0 }, 7193 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 7194 { "sqlite3_unlock_notify", test_unlock_notify, 0 }, 7195 #endif 7196 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, 7197 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, 7198 { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 }, 7199 { "test_sqlite3_log", test_sqlite3_log, 0 }, 7200 #ifndef SQLITE_OMIT_EXPLAIN 7201 { "print_explain_query_plan", test_print_eqp, 0 }, 7202 #endif 7203 { "sqlite3_test_control", test_test_control }, 7204 #if SQLITE_OS_UNIX 7205 { "getrusage", test_getrusage }, 7206 #endif 7207 { "load_static_extension", tclLoadStaticExtensionCmd }, 7208 { "sorter_test_fakeheap", sorter_test_fakeheap }, 7209 { "sorter_test_sort4_helper", sorter_test_sort4_helper }, 7210 #ifdef SQLITE_USER_AUTHENTICATION 7211 { "sqlite3_user_authenticate", test_user_authenticate, 0 }, 7212 { "sqlite3_user_add", test_user_add, 0 }, 7213 { "sqlite3_user_change", test_user_change, 0 }, 7214 { "sqlite3_user_delete", test_user_delete, 0 }, 7215 #endif 7216 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 7217 { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 }, 7218 { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 }, 7219 #endif 7220 #ifdef SQLITE_ENABLE_SQLLOG 7221 { "sqlite3_config_sqllog", test_config_sqllog, 0 }, 7222 #endif 7223 { "vfs_current_time_int64", vfsCurrentTimeInt64, 0 }, 7224 #ifdef SQLITE_ENABLE_SNAPSHOT 7225 { "sqlite3_snapshot_get", test_snapshot_get, 0 }, 7226 { "sqlite3_snapshot_open", test_snapshot_open, 0 }, 7227 { "sqlite3_snapshot_free", test_snapshot_free, 0 }, 7228 #endif 7229 }; 7230 static int bitmask_size = sizeof(Bitmask)*8; 7231 static int longdouble_size = sizeof(LONGDOUBLE_TYPE); 7232 int i; 7233 extern int sqlite3_sync_count, sqlite3_fullsync_count; 7234 extern int sqlite3_opentemp_count; 7235 extern int sqlite3_like_count; 7236 extern int sqlite3_xferopt_count; 7237 extern int sqlite3_pager_readdb_count; 7238 extern int sqlite3_pager_writedb_count; 7239 extern int sqlite3_pager_writej_count; 7240 #if SQLITE_OS_WIN 7241 extern LONG volatile sqlite3_os_type; 7242 #endif 7243 #ifdef SQLITE_DEBUG 7244 extern int sqlite3WhereTrace; 7245 extern int sqlite3OSTrace; 7246 extern int sqlite3WalTrace; 7247 #endif 7248 #ifdef SQLITE_TEST 7249 #ifdef SQLITE_ENABLE_FTS3 7250 extern int sqlite3_fts3_enable_parentheses; 7251 #endif 7252 #endif 7253 7254 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 7255 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 7256 } 7257 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 7258 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 7259 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 7260 } 7261 Tcl_LinkVar(interp, "sqlite_search_count", 7262 (char*)&sqlite3_search_count, TCL_LINK_INT); 7263 Tcl_LinkVar(interp, "sqlite_found_count", 7264 (char*)&sqlite3_found_count, TCL_LINK_INT); 7265 Tcl_LinkVar(interp, "sqlite_sort_count", 7266 (char*)&sqlite3_sort_count, TCL_LINK_INT); 7267 Tcl_LinkVar(interp, "sqlite3_max_blobsize", 7268 (char*)&sqlite3_max_blobsize, TCL_LINK_INT); 7269 Tcl_LinkVar(interp, "sqlite_like_count", 7270 (char*)&sqlite3_like_count, TCL_LINK_INT); 7271 Tcl_LinkVar(interp, "sqlite_interrupt_count", 7272 (char*)&sqlite3_interrupt_count, TCL_LINK_INT); 7273 Tcl_LinkVar(interp, "sqlite_open_file_count", 7274 (char*)&sqlite3_open_file_count, TCL_LINK_INT); 7275 Tcl_LinkVar(interp, "sqlite_current_time", 7276 (char*)&sqlite3_current_time, TCL_LINK_INT); 7277 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 7278 Tcl_LinkVar(interp, "sqlite_hostid_num", 7279 (char*)&sqlite3_hostid_num, TCL_LINK_INT); 7280 #endif 7281 Tcl_LinkVar(interp, "sqlite3_xferopt_count", 7282 (char*)&sqlite3_xferopt_count, TCL_LINK_INT); 7283 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", 7284 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); 7285 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", 7286 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); 7287 Tcl_LinkVar(interp, "sqlite3_pager_writej_count", 7288 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); 7289 #ifndef SQLITE_OMIT_UTF16 7290 Tcl_LinkVar(interp, "unaligned_string_counter", 7291 (char*)&unaligned_string_counter, TCL_LINK_INT); 7292 #endif 7293 #ifndef SQLITE_OMIT_UTF16 7294 Tcl_LinkVar(interp, "sqlite_last_needed_collation", 7295 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 7296 #endif 7297 #if SQLITE_OS_WIN 7298 Tcl_LinkVar(interp, "sqlite_os_type", 7299 (char*)&sqlite3_os_type, TCL_LINK_LONG); 7300 #endif 7301 #ifdef SQLITE_TEST 7302 { 7303 static const char *query_plan = "*** OBSOLETE VARIABLE ***"; 7304 Tcl_LinkVar(interp, "sqlite_query_plan", 7305 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 7306 } 7307 #endif 7308 #ifdef SQLITE_DEBUG 7309 Tcl_LinkVar(interp, "sqlite_where_trace", 7310 (char*)&sqlite3WhereTrace, TCL_LINK_INT); 7311 Tcl_LinkVar(interp, "sqlite_os_trace", 7312 (char*)&sqlite3OSTrace, TCL_LINK_INT); 7313 #ifndef SQLITE_OMIT_WAL 7314 Tcl_LinkVar(interp, "sqlite_wal_trace", 7315 (char*)&sqlite3WalTrace, TCL_LINK_INT); 7316 #endif 7317 #endif 7318 #ifndef SQLITE_OMIT_DISKIO 7319 Tcl_LinkVar(interp, "sqlite_opentemp_count", 7320 (char*)&sqlite3_opentemp_count, TCL_LINK_INT); 7321 #endif 7322 Tcl_LinkVar(interp, "sqlite_static_bind_value", 7323 (char*)&sqlite_static_bind_value, TCL_LINK_STRING); 7324 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", 7325 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); 7326 Tcl_LinkVar(interp, "sqlite_temp_directory", 7327 (char*)&sqlite3_temp_directory, TCL_LINK_STRING); 7328 Tcl_LinkVar(interp, "sqlite_data_directory", 7329 (char*)&sqlite3_data_directory, TCL_LINK_STRING); 7330 Tcl_LinkVar(interp, "bitmask_size", 7331 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); 7332 Tcl_LinkVar(interp, "longdouble_size", 7333 (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); 7334 Tcl_LinkVar(interp, "sqlite_sync_count", 7335 (char*)&sqlite3_sync_count, TCL_LINK_INT); 7336 Tcl_LinkVar(interp, "sqlite_fullsync_count", 7337 (char*)&sqlite3_fullsync_count, TCL_LINK_INT); 7338 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) 7339 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", 7340 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); 7341 #endif 7342 return TCL_OK; 7343 } 7344