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