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 { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP }, 2222 }; 2223 if( objc!=4 ){ 2224 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); 2225 return TCL_ERROR; 2226 } 2227 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2228 zOpName = Tcl_GetString(objv[2]); 2229 for(i=0; i<ArraySize(aOp); i++){ 2230 if( strcmp(aOp[i].zName, zOpName)==0 ){ 2231 op = aOp[i].op; 2232 break; 2233 } 2234 } 2235 if( i>=ArraySize(aOp) ){ 2236 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; 2237 } 2238 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; 2239 iValue = sqlite3_stmt_status(pStmt, op, resetFlag); 2240 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue)); 2241 return TCL_OK; 2242 } 2243 2244 /* 2245 ** Usage: sqlite3_next_stmt DB STMT 2246 ** 2247 ** Return the next statment in sequence after STMT. 2248 */ 2249 static int test_next_stmt( 2250 void * clientData, 2251 Tcl_Interp *interp, 2252 int objc, 2253 Tcl_Obj *CONST objv[] 2254 ){ 2255 sqlite3_stmt *pStmt; 2256 sqlite3 *db = 0; 2257 char zBuf[50]; 2258 2259 if( objc!=3 ){ 2260 Tcl_AppendResult(interp, "wrong # args: should be \"", 2261 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); 2262 return TCL_ERROR; 2263 } 2264 2265 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2266 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; 2267 pStmt = sqlite3_next_stmt(db, pStmt); 2268 if( pStmt ){ 2269 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 2270 Tcl_AppendResult(interp, zBuf, 0); 2271 } 2272 return TCL_OK; 2273 } 2274 2275 /* 2276 ** Usage: sqlite3_stmt_readonly STMT 2277 ** 2278 ** Return true if STMT is a NULL pointer or a pointer to a statement 2279 ** that is guaranteed to leave the database unmodified. 2280 */ 2281 static int test_stmt_readonly( 2282 void * clientData, 2283 Tcl_Interp *interp, 2284 int objc, 2285 Tcl_Obj *CONST objv[] 2286 ){ 2287 sqlite3_stmt *pStmt; 2288 int rc; 2289 2290 if( objc!=2 ){ 2291 Tcl_AppendResult(interp, "wrong # args: should be \"", 2292 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2293 return TCL_ERROR; 2294 } 2295 2296 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2297 rc = sqlite3_stmt_readonly(pStmt); 2298 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); 2299 return TCL_OK; 2300 } 2301 2302 /* 2303 ** Usage: sqlite3_stmt_busy STMT 2304 ** 2305 ** Return true if STMT is a non-NULL pointer to a statement 2306 ** that has been stepped but not to completion. 2307 */ 2308 static int test_stmt_busy( 2309 void * clientData, 2310 Tcl_Interp *interp, 2311 int objc, 2312 Tcl_Obj *CONST objv[] 2313 ){ 2314 sqlite3_stmt *pStmt; 2315 int rc; 2316 2317 if( objc!=2 ){ 2318 Tcl_AppendResult(interp, "wrong # args: should be \"", 2319 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2320 return TCL_ERROR; 2321 } 2322 2323 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2324 rc = sqlite3_stmt_busy(pStmt); 2325 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); 2326 return TCL_OK; 2327 } 2328 2329 /* 2330 ** Usage: uses_stmt_journal STMT 2331 ** 2332 ** Return true if STMT uses a statement journal. 2333 */ 2334 static int uses_stmt_journal( 2335 void * clientData, 2336 Tcl_Interp *interp, 2337 int objc, 2338 Tcl_Obj *CONST objv[] 2339 ){ 2340 sqlite3_stmt *pStmt; 2341 2342 if( objc!=2 ){ 2343 Tcl_AppendResult(interp, "wrong # args: should be \"", 2344 Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); 2345 return TCL_ERROR; 2346 } 2347 2348 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2349 sqlite3_stmt_readonly(pStmt); 2350 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal)); 2351 return TCL_OK; 2352 } 2353 2354 2355 /* 2356 ** Usage: sqlite3_reset STMT 2357 ** 2358 ** Reset a statement handle. 2359 */ 2360 static int test_reset( 2361 void * clientData, 2362 Tcl_Interp *interp, 2363 int objc, 2364 Tcl_Obj *CONST objv[] 2365 ){ 2366 sqlite3_stmt *pStmt; 2367 int rc; 2368 2369 if( objc!=2 ){ 2370 Tcl_AppendResult(interp, "wrong # args: should be \"", 2371 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2372 return TCL_ERROR; 2373 } 2374 2375 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2376 2377 rc = sqlite3_reset(pStmt); 2378 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ 2379 return TCL_ERROR; 2380 } 2381 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 2382 /* 2383 if( rc ){ 2384 return TCL_ERROR; 2385 } 2386 */ 2387 return TCL_OK; 2388 } 2389 2390 /* 2391 ** Usage: sqlite3_expired STMT 2392 ** 2393 ** Return TRUE if a recompilation of the statement is recommended. 2394 */ 2395 static int test_expired( 2396 void * clientData, 2397 Tcl_Interp *interp, 2398 int objc, 2399 Tcl_Obj *CONST objv[] 2400 ){ 2401 #ifndef SQLITE_OMIT_DEPRECATED 2402 sqlite3_stmt *pStmt; 2403 if( objc!=2 ){ 2404 Tcl_AppendResult(interp, "wrong # args: should be \"", 2405 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 2406 return TCL_ERROR; 2407 } 2408 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2409 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); 2410 #endif 2411 return TCL_OK; 2412 } 2413 2414 /* 2415 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT 2416 ** 2417 ** Transfer all bindings from FROMSTMT over to TOSTMT 2418 */ 2419 static int test_transfer_bind( 2420 void * clientData, 2421 Tcl_Interp *interp, 2422 int objc, 2423 Tcl_Obj *CONST objv[] 2424 ){ 2425 #ifndef SQLITE_OMIT_DEPRECATED 2426 sqlite3_stmt *pStmt1, *pStmt2; 2427 if( objc!=3 ){ 2428 Tcl_AppendResult(interp, "wrong # args: should be \"", 2429 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); 2430 return TCL_ERROR; 2431 } 2432 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; 2433 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; 2434 Tcl_SetObjResult(interp, 2435 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); 2436 #endif 2437 return TCL_OK; 2438 } 2439 2440 /* 2441 ** Usage: sqlite3_changes DB 2442 ** 2443 ** Return the number of changes made to the database by the last SQL 2444 ** execution. 2445 */ 2446 static int test_changes( 2447 void * clientData, 2448 Tcl_Interp *interp, 2449 int objc, 2450 Tcl_Obj *CONST objv[] 2451 ){ 2452 sqlite3 *db; 2453 if( objc!=2 ){ 2454 Tcl_AppendResult(interp, "wrong # args: should be \"", 2455 Tcl_GetString(objv[0]), " DB", 0); 2456 return TCL_ERROR; 2457 } 2458 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2459 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); 2460 return TCL_OK; 2461 } 2462 2463 /* 2464 ** This is the "static_bind_value" that variables are bound to when 2465 ** the FLAG option of sqlite3_bind is "static" 2466 */ 2467 static char *sqlite_static_bind_value = 0; 2468 static int sqlite_static_bind_nbyte = 0; 2469 2470 /* 2471 ** Usage: sqlite3_bind VM IDX VALUE FLAGS 2472 ** 2473 ** Sets the value of the IDX-th occurrence of "?" in the original SQL 2474 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is 2475 ** ignored and the value is set to NULL. If FLAGS=="static" then 2476 ** the value is set to the value of a static variable named 2477 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy 2478 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored 2479 ** an a 10-byte blob "abc\000xyz\000pq" is inserted. 2480 */ 2481 static int test_bind( 2482 void *NotUsed, 2483 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2484 int argc, /* Number of arguments */ 2485 char **argv /* Text of each argument */ 2486 ){ 2487 sqlite3_stmt *pStmt; 2488 int rc; 2489 int idx; 2490 if( argc!=5 ){ 2491 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 2492 " VM IDX VALUE (null|static|normal)\"", 0); 2493 return TCL_ERROR; 2494 } 2495 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; 2496 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; 2497 if( strcmp(argv[4],"null")==0 ){ 2498 rc = sqlite3_bind_null(pStmt, idx); 2499 }else if( strcmp(argv[4],"static")==0 ){ 2500 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); 2501 }else if( strcmp(argv[4],"static-nbytes")==0 ){ 2502 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, 2503 sqlite_static_bind_nbyte, 0); 2504 }else if( strcmp(argv[4],"normal")==0 ){ 2505 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); 2506 }else if( strcmp(argv[4],"blob10")==0 ){ 2507 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); 2508 }else{ 2509 Tcl_AppendResult(interp, "4th argument should be " 2510 "\"null\" or \"static\" or \"normal\"", 0); 2511 return TCL_ERROR; 2512 } 2513 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2514 if( rc ){ 2515 char zBuf[50]; 2516 sprintf(zBuf, "(%d) ", rc); 2517 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); 2518 return TCL_ERROR; 2519 } 2520 return TCL_OK; 2521 } 2522 2523 #ifndef SQLITE_OMIT_UTF16 2524 /* 2525 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> 2526 ** 2527 ** This function is used to test that SQLite selects the correct collation 2528 ** sequence callback when multiple versions (for different text encodings) 2529 ** are available. 2530 ** 2531 ** Calling this routine registers the collation sequence "test_collate" 2532 ** with database handle <db>. The second argument must be a list of three 2533 ** boolean values. If the first is true, then a version of test_collate is 2534 ** registered for UTF-8, if the second is true, a version is registered for 2535 ** UTF-16le, if the third is true, a UTF-16be version is available. 2536 ** Previous versions of test_collate are deleted. 2537 ** 2538 ** The collation sequence test_collate is implemented by calling the 2539 ** following TCL script: 2540 ** 2541 ** "test_collate <enc> <lhs> <rhs>" 2542 ** 2543 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. 2544 ** The <enc> parameter is the encoding of the collation function that 2545 ** SQLite selected to call. The TCL test script implements the 2546 ** "test_collate" proc. 2547 ** 2548 ** Note that this will only work with one intepreter at a time, as the 2549 ** interp pointer to use when evaluating the TCL script is stored in 2550 ** pTestCollateInterp. 2551 */ 2552 static Tcl_Interp* pTestCollateInterp; 2553 static int test_collate_func( 2554 void *pCtx, 2555 int nA, const void *zA, 2556 int nB, const void *zB 2557 ){ 2558 Tcl_Interp *i = pTestCollateInterp; 2559 int encin = SQLITE_PTR_TO_INT(pCtx); 2560 int res; 2561 int n; 2562 2563 sqlite3_value *pVal; 2564 Tcl_Obj *pX; 2565 2566 pX = Tcl_NewStringObj("test_collate", -1); 2567 Tcl_IncrRefCount(pX); 2568 2569 switch( encin ){ 2570 case SQLITE_UTF8: 2571 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); 2572 break; 2573 case SQLITE_UTF16LE: 2574 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); 2575 break; 2576 case SQLITE_UTF16BE: 2577 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); 2578 break; 2579 default: 2580 assert(0); 2581 } 2582 2583 sqlite3BeginBenignMalloc(); 2584 pVal = sqlite3ValueNew(0); 2585 if( pVal ){ 2586 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); 2587 n = sqlite3_value_bytes(pVal); 2588 Tcl_ListObjAppendElement(i,pX, 2589 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2590 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); 2591 n = sqlite3_value_bytes(pVal); 2592 Tcl_ListObjAppendElement(i,pX, 2593 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2594 sqlite3ValueFree(pVal); 2595 } 2596 sqlite3EndBenignMalloc(); 2597 2598 Tcl_EvalObjEx(i, pX, 0); 2599 Tcl_DecrRefCount(pX); 2600 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); 2601 return res; 2602 } 2603 static int test_collate( 2604 void * clientData, 2605 Tcl_Interp *interp, 2606 int objc, 2607 Tcl_Obj *CONST objv[] 2608 ){ 2609 sqlite3 *db; 2610 int val; 2611 sqlite3_value *pVal; 2612 int rc; 2613 2614 if( objc!=5 ) goto bad_args; 2615 pTestCollateInterp = interp; 2616 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2617 2618 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2619 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, 2620 (void *)SQLITE_UTF8, val?test_collate_func:0); 2621 if( rc==SQLITE_OK ){ 2622 const void *zUtf16; 2623 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2624 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, 2625 (void *)SQLITE_UTF16LE, val?test_collate_func:0); 2626 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2627 2628 #if 0 2629 if( sqlite3_iMallocFail>0 ){ 2630 sqlite3_iMallocFail++; 2631 } 2632 #endif 2633 sqlite3_mutex_enter(db->mutex); 2634 pVal = sqlite3ValueNew(db); 2635 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); 2636 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 2637 if( db->mallocFailed ){ 2638 rc = SQLITE_NOMEM; 2639 }else{ 2640 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, 2641 (void *)SQLITE_UTF16BE, val?test_collate_func:0); 2642 } 2643 sqlite3ValueFree(pVal); 2644 sqlite3_mutex_leave(db->mutex); 2645 } 2646 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2647 2648 if( rc!=SQLITE_OK ){ 2649 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 2650 return TCL_ERROR; 2651 } 2652 return TCL_OK; 2653 2654 bad_args: 2655 Tcl_AppendResult(interp, "wrong # args: should be \"", 2656 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2657 return TCL_ERROR; 2658 } 2659 2660 /* 2661 ** When the collation needed callback is invoked, record the name of 2662 ** the requested collating function here. The recorded name is linked 2663 ** to a TCL variable and used to make sure that the requested collation 2664 ** name is correct. 2665 */ 2666 static char zNeededCollation[200]; 2667 static char *pzNeededCollation = zNeededCollation; 2668 2669 2670 /* 2671 ** Called when a collating sequence is needed. Registered using 2672 ** sqlite3_collation_needed16(). 2673 */ 2674 static void test_collate_needed_cb( 2675 void *pCtx, 2676 sqlite3 *db, 2677 int eTextRep, 2678 const void *pName 2679 ){ 2680 int enc = ENC(db); 2681 int i; 2682 char *z; 2683 for(z = (char*)pName, i=0; *z || z[1]; z++){ 2684 if( *z ) zNeededCollation[i++] = *z; 2685 } 2686 zNeededCollation[i] = 0; 2687 sqlite3_create_collation( 2688 db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func); 2689 } 2690 2691 /* 2692 ** Usage: add_test_collate_needed DB 2693 */ 2694 static int test_collate_needed( 2695 void * clientData, 2696 Tcl_Interp *interp, 2697 int objc, 2698 Tcl_Obj *CONST objv[] 2699 ){ 2700 sqlite3 *db; 2701 int rc; 2702 2703 if( objc!=2 ) goto bad_args; 2704 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2705 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); 2706 zNeededCollation[0] = 0; 2707 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2708 return TCL_OK; 2709 2710 bad_args: 2711 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 2712 return TCL_ERROR; 2713 } 2714 2715 /* 2716 ** tclcmd: add_alignment_test_collations DB 2717 ** 2718 ** Add two new collating sequences to the database DB 2719 ** 2720 ** utf16_aligned 2721 ** utf16_unaligned 2722 ** 2723 ** Both collating sequences use the same sort order as BINARY. 2724 ** The only difference is that the utf16_aligned collating 2725 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. 2726 ** Both collating functions increment the unaligned utf16 counter 2727 ** whenever they see a string that begins on an odd byte boundary. 2728 */ 2729 static int unaligned_string_counter = 0; 2730 static int alignmentCollFunc( 2731 void *NotUsed, 2732 int nKey1, const void *pKey1, 2733 int nKey2, const void *pKey2 2734 ){ 2735 int rc, n; 2736 n = nKey1<nKey2 ? nKey1 : nKey2; 2737 if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++; 2738 if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++; 2739 rc = memcmp(pKey1, pKey2, n); 2740 if( rc==0 ){ 2741 rc = nKey1 - nKey2; 2742 } 2743 return rc; 2744 } 2745 static int add_alignment_test_collations( 2746 void * clientData, 2747 Tcl_Interp *interp, 2748 int objc, 2749 Tcl_Obj *CONST objv[] 2750 ){ 2751 sqlite3 *db; 2752 if( objc>=2 ){ 2753 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2754 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, 2755 0, alignmentCollFunc); 2756 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED, 2757 0, alignmentCollFunc); 2758 } 2759 return SQLITE_OK; 2760 } 2761 #endif /* !defined(SQLITE_OMIT_UTF16) */ 2762 2763 /* 2764 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> 2765 ** 2766 ** This function is used to test that SQLite selects the correct user 2767 ** function callback when multiple versions (for different text encodings) 2768 ** are available. 2769 ** 2770 ** Calling this routine registers up to three versions of the user function 2771 ** "test_function" with database handle <db>. If the second argument is 2772 ** true, then a version of test_function is registered for UTF-8, if the 2773 ** third is true, a version is registered for UTF-16le, if the fourth is 2774 ** true, a UTF-16be version is available. Previous versions of 2775 ** test_function are deleted. 2776 ** 2777 ** The user function is implemented by calling the following TCL script: 2778 ** 2779 ** "test_function <enc> <arg>" 2780 ** 2781 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the 2782 ** single argument passed to the SQL function. The value returned by 2783 ** the TCL script is used as the return value of the SQL function. It 2784 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 2785 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that 2786 ** prefers UTF-16BE. 2787 */ 2788 #ifndef SQLITE_OMIT_UTF16 2789 static void test_function_utf8( 2790 sqlite3_context *pCtx, 2791 int nArg, 2792 sqlite3_value **argv 2793 ){ 2794 Tcl_Interp *interp; 2795 Tcl_Obj *pX; 2796 sqlite3_value *pVal; 2797 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2798 pX = Tcl_NewStringObj("test_function", -1); 2799 Tcl_IncrRefCount(pX); 2800 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); 2801 Tcl_ListObjAppendElement(interp, pX, 2802 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2803 Tcl_EvalObjEx(interp, pX, 0); 2804 Tcl_DecrRefCount(pX); 2805 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); 2806 pVal = sqlite3ValueNew(0); 2807 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2808 SQLITE_UTF8, SQLITE_STATIC); 2809 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), 2810 -1, SQLITE_TRANSIENT); 2811 sqlite3ValueFree(pVal); 2812 } 2813 static void test_function_utf16le( 2814 sqlite3_context *pCtx, 2815 int nArg, 2816 sqlite3_value **argv 2817 ){ 2818 Tcl_Interp *interp; 2819 Tcl_Obj *pX; 2820 sqlite3_value *pVal; 2821 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2822 pX = Tcl_NewStringObj("test_function", -1); 2823 Tcl_IncrRefCount(pX); 2824 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); 2825 Tcl_ListObjAppendElement(interp, pX, 2826 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2827 Tcl_EvalObjEx(interp, pX, 0); 2828 Tcl_DecrRefCount(pX); 2829 pVal = sqlite3ValueNew(0); 2830 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2831 SQLITE_UTF8, SQLITE_STATIC); 2832 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); 2833 sqlite3ValueFree(pVal); 2834 } 2835 static void test_function_utf16be( 2836 sqlite3_context *pCtx, 2837 int nArg, 2838 sqlite3_value **argv 2839 ){ 2840 Tcl_Interp *interp; 2841 Tcl_Obj *pX; 2842 sqlite3_value *pVal; 2843 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2844 pX = Tcl_NewStringObj("test_function", -1); 2845 Tcl_IncrRefCount(pX); 2846 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); 2847 Tcl_ListObjAppendElement(interp, pX, 2848 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2849 Tcl_EvalObjEx(interp, pX, 0); 2850 Tcl_DecrRefCount(pX); 2851 pVal = sqlite3ValueNew(0); 2852 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2853 SQLITE_UTF8, SQLITE_STATIC); 2854 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), 2855 -1, SQLITE_TRANSIENT); 2856 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), 2857 -1, SQLITE_TRANSIENT); 2858 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), 2859 -1, SQLITE_TRANSIENT); 2860 sqlite3ValueFree(pVal); 2861 } 2862 #endif /* SQLITE_OMIT_UTF16 */ 2863 static int test_function( 2864 void * clientData, 2865 Tcl_Interp *interp, 2866 int objc, 2867 Tcl_Obj *CONST objv[] 2868 ){ 2869 #ifndef SQLITE_OMIT_UTF16 2870 sqlite3 *db; 2871 int val; 2872 2873 if( objc!=5 ) goto bad_args; 2874 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2875 2876 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2877 if( val ){ 2878 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, 2879 interp, test_function_utf8, 0, 0); 2880 } 2881 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2882 if( val ){ 2883 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, 2884 interp, test_function_utf16le, 0, 0); 2885 } 2886 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2887 if( val ){ 2888 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, 2889 interp, test_function_utf16be, 0, 0); 2890 } 2891 2892 return TCL_OK; 2893 bad_args: 2894 Tcl_AppendResult(interp, "wrong # args: should be \"", 2895 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2896 #endif /* SQLITE_OMIT_UTF16 */ 2897 return TCL_ERROR; 2898 } 2899 2900 /* 2901 ** Usage: sqlite3_test_errstr <err code> 2902 ** 2903 ** Test that the english language string equivalents for sqlite error codes 2904 ** are sane. The parameter is an integer representing an sqlite error code. 2905 ** The result is a list of two elements, the string representation of the 2906 ** error code and the english language explanation. 2907 */ 2908 static int test_errstr( 2909 void * clientData, 2910 Tcl_Interp *interp, 2911 int objc, 2912 Tcl_Obj *CONST objv[] 2913 ){ 2914 char *zCode; 2915 int i; 2916 if( objc!=1 ){ 2917 Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); 2918 } 2919 2920 zCode = Tcl_GetString(objv[1]); 2921 for(i=0; i<200; i++){ 2922 if( 0==strcmp(t1ErrorName(i), zCode) ) break; 2923 } 2924 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); 2925 return TCL_OK; 2926 } 2927 2928 /* 2929 ** Usage: breakpoint 2930 ** 2931 ** This routine exists for one purpose - to provide a place to put a 2932 ** breakpoint with GDB that can be triggered using TCL code. The use 2933 ** for this is when a particular test fails on (say) the 1485th iteration. 2934 ** In the TCL test script, we can add code like this: 2935 ** 2936 ** if {$i==1485} breakpoint 2937 ** 2938 ** Then run testfixture in the debugger and wait for the breakpoint to 2939 ** fire. Then additional breakpoints can be set to trace down the bug. 2940 */ 2941 static int test_breakpoint( 2942 void *NotUsed, 2943 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2944 int argc, /* Number of arguments */ 2945 char **argv /* Text of each argument */ 2946 ){ 2947 return TCL_OK; /* Do nothing */ 2948 } 2949 2950 /* 2951 ** Usage: sqlite3_bind_zeroblob STMT IDX N 2952 ** 2953 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. 2954 ** IDX is the index of a wildcard in the prepared statement. This command 2955 ** binds a N-byte zero-filled BLOB to the wildcard. 2956 */ 2957 static int test_bind_zeroblob( 2958 void * clientData, 2959 Tcl_Interp *interp, 2960 int objc, 2961 Tcl_Obj *CONST objv[] 2962 ){ 2963 sqlite3_stmt *pStmt; 2964 int idx; 2965 int n; 2966 int rc; 2967 2968 if( objc!=4 ){ 2969 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); 2970 return TCL_ERROR; 2971 } 2972 2973 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2974 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2975 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; 2976 2977 rc = sqlite3_bind_zeroblob(pStmt, idx, n); 2978 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2979 if( rc!=SQLITE_OK ){ 2980 return TCL_ERROR; 2981 } 2982 2983 return TCL_OK; 2984 } 2985 2986 /* 2987 ** Usage: sqlite3_bind_int STMT N VALUE 2988 ** 2989 ** Test the sqlite3_bind_int interface. STMT is a prepared statement. 2990 ** N is the index of a wildcard in the prepared statement. This command 2991 ** binds a 32-bit integer VALUE to that wildcard. 2992 */ 2993 static int test_bind_int( 2994 void * clientData, 2995 Tcl_Interp *interp, 2996 int objc, 2997 Tcl_Obj *CONST objv[] 2998 ){ 2999 sqlite3_stmt *pStmt; 3000 int idx; 3001 int value; 3002 int rc; 3003 3004 if( objc!=4 ){ 3005 Tcl_AppendResult(interp, "wrong # args: should be \"", 3006 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3007 return TCL_ERROR; 3008 } 3009 3010 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3011 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3012 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3013 3014 rc = sqlite3_bind_int(pStmt, idx, value); 3015 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3016 if( rc!=SQLITE_OK ){ 3017 return TCL_ERROR; 3018 } 3019 3020 return TCL_OK; 3021 } 3022 3023 3024 /* 3025 ** Usage: sqlite3_bind_int64 STMT N VALUE 3026 ** 3027 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. 3028 ** N is the index of a wildcard in the prepared statement. This command 3029 ** binds a 64-bit integer VALUE to that wildcard. 3030 */ 3031 static int test_bind_int64( 3032 void * clientData, 3033 Tcl_Interp *interp, 3034 int objc, 3035 Tcl_Obj *CONST objv[] 3036 ){ 3037 sqlite3_stmt *pStmt; 3038 int idx; 3039 Tcl_WideInt value; 3040 int rc; 3041 3042 if( objc!=4 ){ 3043 Tcl_AppendResult(interp, "wrong # args: should be \"", 3044 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3045 return TCL_ERROR; 3046 } 3047 3048 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3049 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3050 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 3051 3052 rc = sqlite3_bind_int64(pStmt, idx, value); 3053 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3054 if( rc!=SQLITE_OK ){ 3055 return TCL_ERROR; 3056 } 3057 3058 return TCL_OK; 3059 } 3060 3061 3062 /* 3063 ** Usage: sqlite3_bind_double STMT N VALUE 3064 ** 3065 ** Test the sqlite3_bind_double interface. STMT is a prepared statement. 3066 ** N is the index of a wildcard in the prepared statement. This command 3067 ** binds a 64-bit integer VALUE to that wildcard. 3068 */ 3069 static int test_bind_double( 3070 void * clientData, 3071 Tcl_Interp *interp, 3072 int objc, 3073 Tcl_Obj *CONST objv[] 3074 ){ 3075 sqlite3_stmt *pStmt; 3076 int idx; 3077 double value; 3078 int rc; 3079 const char *zVal; 3080 int i; 3081 static const struct { 3082 const char *zName; /* Name of the special floating point value */ 3083 unsigned int iUpper; /* Upper 32 bits */ 3084 unsigned int iLower; /* Lower 32 bits */ 3085 } aSpecialFp[] = { 3086 { "NaN", 0x7fffffff, 0xffffffff }, 3087 { "SNaN", 0x7ff7ffff, 0xffffffff }, 3088 { "-NaN", 0xffffffff, 0xffffffff }, 3089 { "-SNaN", 0xfff7ffff, 0xffffffff }, 3090 { "+Inf", 0x7ff00000, 0x00000000 }, 3091 { "-Inf", 0xfff00000, 0x00000000 }, 3092 { "Epsilon", 0x00000000, 0x00000001 }, 3093 { "-Epsilon", 0x80000000, 0x00000001 }, 3094 { "NaN0", 0x7ff80000, 0x00000000 }, 3095 { "-NaN0", 0xfff80000, 0x00000000 }, 3096 }; 3097 3098 if( objc!=4 ){ 3099 Tcl_AppendResult(interp, "wrong # args: should be \"", 3100 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 3101 return TCL_ERROR; 3102 } 3103 3104 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3105 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3106 3107 /* Intercept the string "NaN" and generate a NaN value for it. 3108 ** All other strings are passed through to Tcl_GetDoubleFromObj(). 3109 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions 3110 ** contain a bug. 3111 */ 3112 zVal = Tcl_GetString(objv[3]); 3113 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ 3114 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ 3115 sqlite3_uint64 x; 3116 x = aSpecialFp[i].iUpper; 3117 x <<= 32; 3118 x |= aSpecialFp[i].iLower; 3119 assert( sizeof(value)==8 ); 3120 assert( sizeof(x)==8 ); 3121 memcpy(&value, &x, 8); 3122 break; 3123 } 3124 } 3125 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && 3126 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ 3127 return TCL_ERROR; 3128 } 3129 rc = sqlite3_bind_double(pStmt, idx, value); 3130 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3131 if( rc!=SQLITE_OK ){ 3132 return TCL_ERROR; 3133 } 3134 3135 return TCL_OK; 3136 } 3137 3138 /* 3139 ** Usage: sqlite3_bind_null STMT N 3140 ** 3141 ** Test the sqlite3_bind_null interface. STMT is a prepared statement. 3142 ** N is the index of a wildcard in the prepared statement. This command 3143 ** binds a NULL to the wildcard. 3144 */ 3145 static int test_bind_null( 3146 void * clientData, 3147 Tcl_Interp *interp, 3148 int objc, 3149 Tcl_Obj *CONST objv[] 3150 ){ 3151 sqlite3_stmt *pStmt; 3152 int idx; 3153 int rc; 3154 3155 if( objc!=3 ){ 3156 Tcl_AppendResult(interp, "wrong # args: should be \"", 3157 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); 3158 return TCL_ERROR; 3159 } 3160 3161 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3162 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3163 3164 rc = sqlite3_bind_null(pStmt, idx); 3165 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3166 if( rc!=SQLITE_OK ){ 3167 return TCL_ERROR; 3168 } 3169 3170 return TCL_OK; 3171 } 3172 3173 /* 3174 ** Usage: sqlite3_bind_text STMT N STRING BYTES 3175 ** 3176 ** Test the sqlite3_bind_text interface. STMT is a prepared statement. 3177 ** N is the index of a wildcard in the prepared statement. This command 3178 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes 3179 ** long. 3180 */ 3181 static int test_bind_text( 3182 void * clientData, 3183 Tcl_Interp *interp, 3184 int objc, 3185 Tcl_Obj *CONST objv[] 3186 ){ 3187 sqlite3_stmt *pStmt; 3188 int idx; 3189 int bytes; 3190 char *value; 3191 int rc; 3192 3193 if( objc!=5 ){ 3194 Tcl_AppendResult(interp, "wrong # args: should be \"", 3195 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3196 return TCL_ERROR; 3197 } 3198 3199 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3200 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3201 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); 3202 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3203 3204 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); 3205 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3206 if( rc!=SQLITE_OK ){ 3207 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3208 return TCL_ERROR; 3209 } 3210 3211 return TCL_OK; 3212 } 3213 3214 /* 3215 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES 3216 ** 3217 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. 3218 ** N is the index of a wildcard in the prepared statement. This command 3219 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes 3220 ** long. 3221 */ 3222 static int test_bind_text16( 3223 void * clientData, 3224 Tcl_Interp *interp, 3225 int objc, 3226 Tcl_Obj *CONST objv[] 3227 ){ 3228 #ifndef SQLITE_OMIT_UTF16 3229 sqlite3_stmt *pStmt; 3230 int idx; 3231 int bytes; 3232 char *value; 3233 int rc; 3234 3235 void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); 3236 Tcl_Obj *oStmt = objv[objc-4]; 3237 Tcl_Obj *oN = objv[objc-3]; 3238 Tcl_Obj *oString = objv[objc-2]; 3239 Tcl_Obj *oBytes = objv[objc-1]; 3240 3241 if( objc!=5 && objc!=6){ 3242 Tcl_AppendResult(interp, "wrong # args: should be \"", 3243 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 3244 return TCL_ERROR; 3245 } 3246 3247 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; 3248 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; 3249 value = (char*)Tcl_GetByteArrayFromObj(oString, 0); 3250 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; 3251 3252 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); 3253 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3254 if( rc!=SQLITE_OK ){ 3255 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 3256 return TCL_ERROR; 3257 } 3258 3259 #endif /* SQLITE_OMIT_UTF16 */ 3260 return TCL_OK; 3261 } 3262 3263 /* 3264 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES 3265 ** 3266 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. 3267 ** N is the index of a wildcard in the prepared statement. This command 3268 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. 3269 */ 3270 static int test_bind_blob( 3271 void * clientData, 3272 Tcl_Interp *interp, 3273 int objc, 3274 Tcl_Obj *CONST objv[] 3275 ){ 3276 sqlite3_stmt *pStmt; 3277 int idx; 3278 int bytes; 3279 char *value; 3280 int rc; 3281 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; 3282 3283 if( objc!=5 && objc!=6 ){ 3284 Tcl_AppendResult(interp, "wrong # args: should be \"", 3285 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); 3286 return TCL_ERROR; 3287 } 3288 3289 if( objc==6 ){ 3290 xDestructor = SQLITE_STATIC; 3291 objv++; 3292 } 3293 3294 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3295 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 3296 value = Tcl_GetString(objv[3]); 3297 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 3298 3299 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); 3300 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 3301 if( rc!=SQLITE_OK ){ 3302 return TCL_ERROR; 3303 } 3304 3305 return TCL_OK; 3306 } 3307 3308 /* 3309 ** Usage: sqlite3_bind_parameter_count STMT 3310 ** 3311 ** Return the number of wildcards in the given statement. 3312 */ 3313 static int test_bind_parameter_count( 3314 void * clientData, 3315 Tcl_Interp *interp, 3316 int objc, 3317 Tcl_Obj *CONST objv[] 3318 ){ 3319 sqlite3_stmt *pStmt; 3320 3321 if( objc!=2 ){ 3322 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3323 return TCL_ERROR; 3324 } 3325 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3326 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); 3327 return TCL_OK; 3328 } 3329 3330 /* 3331 ** Usage: sqlite3_bind_parameter_name STMT N 3332 ** 3333 ** Return the name of the Nth wildcard. The first wildcard is 1. 3334 ** An empty string is returned if N is out of range or if the wildcard 3335 ** is nameless. 3336 */ 3337 static int test_bind_parameter_name( 3338 void * clientData, 3339 Tcl_Interp *interp, 3340 int objc, 3341 Tcl_Obj *CONST objv[] 3342 ){ 3343 sqlite3_stmt *pStmt; 3344 int i; 3345 3346 if( objc!=3 ){ 3347 Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); 3348 return TCL_ERROR; 3349 } 3350 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3351 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; 3352 Tcl_SetObjResult(interp, 3353 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) 3354 ); 3355 return TCL_OK; 3356 } 3357 3358 /* 3359 ** Usage: sqlite3_bind_parameter_index STMT NAME 3360 ** 3361 ** Return the index of the wildcard called NAME. Return 0 if there is 3362 ** no such wildcard. 3363 */ 3364 static int test_bind_parameter_index( 3365 void * clientData, 3366 Tcl_Interp *interp, 3367 int objc, 3368 Tcl_Obj *CONST objv[] 3369 ){ 3370 sqlite3_stmt *pStmt; 3371 3372 if( objc!=3 ){ 3373 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); 3374 return TCL_ERROR; 3375 } 3376 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3377 Tcl_SetObjResult(interp, 3378 Tcl_NewIntObj( 3379 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) 3380 ) 3381 ); 3382 return TCL_OK; 3383 } 3384 3385 /* 3386 ** Usage: sqlite3_clear_bindings STMT 3387 ** 3388 */ 3389 static int test_clear_bindings( 3390 void * clientData, 3391 Tcl_Interp *interp, 3392 int objc, 3393 Tcl_Obj *CONST objv[] 3394 ){ 3395 sqlite3_stmt *pStmt; 3396 3397 if( objc!=2 ){ 3398 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 3399 return TCL_ERROR; 3400 } 3401 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3402 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); 3403 return TCL_OK; 3404 } 3405 3406 /* 3407 ** Usage: sqlite3_sleep MILLISECONDS 3408 */ 3409 static int test_sleep( 3410 void * clientData, 3411 Tcl_Interp *interp, 3412 int objc, 3413 Tcl_Obj *CONST objv[] 3414 ){ 3415 int ms; 3416 3417 if( objc!=2 ){ 3418 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); 3419 return TCL_ERROR; 3420 } 3421 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ 3422 return TCL_ERROR; 3423 } 3424 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); 3425 return TCL_OK; 3426 } 3427 3428 /* 3429 ** Usage: sqlite3_extended_errcode DB 3430 ** 3431 ** Return the string representation of the most recent sqlite3_* API 3432 ** error code. e.g. "SQLITE_ERROR". 3433 */ 3434 static int test_ex_errcode( 3435 void * clientData, 3436 Tcl_Interp *interp, 3437 int objc, 3438 Tcl_Obj *CONST objv[] 3439 ){ 3440 sqlite3 *db; 3441 int rc; 3442 3443 if( objc!=2 ){ 3444 Tcl_AppendResult(interp, "wrong # args: should be \"", 3445 Tcl_GetString(objv[0]), " DB", 0); 3446 return TCL_ERROR; 3447 } 3448 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3449 rc = sqlite3_extended_errcode(db); 3450 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3451 return TCL_OK; 3452 } 3453 3454 3455 /* 3456 ** Usage: sqlite3_errcode DB 3457 ** 3458 ** Return the string representation of the most recent sqlite3_* API 3459 ** error code. e.g. "SQLITE_ERROR". 3460 */ 3461 static int test_errcode( 3462 void * clientData, 3463 Tcl_Interp *interp, 3464 int objc, 3465 Tcl_Obj *CONST objv[] 3466 ){ 3467 sqlite3 *db; 3468 int rc; 3469 3470 if( objc!=2 ){ 3471 Tcl_AppendResult(interp, "wrong # args: should be \"", 3472 Tcl_GetString(objv[0]), " DB", 0); 3473 return TCL_ERROR; 3474 } 3475 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3476 rc = sqlite3_errcode(db); 3477 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); 3478 return TCL_OK; 3479 } 3480 3481 /* 3482 ** Usage: sqlite3_errmsg DB 3483 ** 3484 ** Returns the UTF-8 representation of the error message string for the 3485 ** most recent sqlite3_* API call. 3486 */ 3487 static int test_errmsg( 3488 void * clientData, 3489 Tcl_Interp *interp, 3490 int objc, 3491 Tcl_Obj *CONST objv[] 3492 ){ 3493 sqlite3 *db; 3494 const char *zErr; 3495 3496 if( objc!=2 ){ 3497 Tcl_AppendResult(interp, "wrong # args: should be \"", 3498 Tcl_GetString(objv[0]), " DB", 0); 3499 return TCL_ERROR; 3500 } 3501 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3502 3503 zErr = sqlite3_errmsg(db); 3504 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 3505 return TCL_OK; 3506 } 3507 3508 /* 3509 ** Usage: test_errmsg16 DB 3510 ** 3511 ** Returns the UTF-16 representation of the error message string for the 3512 ** most recent sqlite3_* API call. This is a byte array object at the TCL 3513 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the 3514 ** UTF-16 string. 3515 */ 3516 static int test_errmsg16( 3517 void * clientData, 3518 Tcl_Interp *interp, 3519 int objc, 3520 Tcl_Obj *CONST objv[] 3521 ){ 3522 #ifndef SQLITE_OMIT_UTF16 3523 sqlite3 *db; 3524 const void *zErr; 3525 const char *z; 3526 int bytes = 0; 3527 3528 if( objc!=2 ){ 3529 Tcl_AppendResult(interp, "wrong # args: should be \"", 3530 Tcl_GetString(objv[0]), " DB", 0); 3531 return TCL_ERROR; 3532 } 3533 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3534 3535 zErr = sqlite3_errmsg16(db); 3536 if( zErr ){ 3537 z = zErr; 3538 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} 3539 } 3540 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); 3541 #endif /* SQLITE_OMIT_UTF16 */ 3542 return TCL_OK; 3543 } 3544 3545 /* 3546 ** Usage: sqlite3_prepare DB sql bytes ?tailvar? 3547 ** 3548 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3549 ** database handle <DB>. The parameter <tailval> is the name of a global 3550 ** variable that is set to the unused portion of <sql> (if any). A 3551 ** STMT handle is returned. 3552 */ 3553 static int test_prepare( 3554 void * clientData, 3555 Tcl_Interp *interp, 3556 int objc, 3557 Tcl_Obj *CONST objv[] 3558 ){ 3559 sqlite3 *db; 3560 const char *zSql; 3561 int bytes; 3562 const char *zTail = 0; 3563 sqlite3_stmt *pStmt = 0; 3564 char zBuf[50]; 3565 int rc; 3566 3567 if( objc!=5 && objc!=4 ){ 3568 Tcl_AppendResult(interp, "wrong # args: should be \"", 3569 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3570 return TCL_ERROR; 3571 } 3572 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3573 zSql = Tcl_GetString(objv[2]); 3574 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3575 3576 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3577 Tcl_ResetResult(interp); 3578 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3579 if( zTail && objc>=5 ){ 3580 if( bytes>=0 ){ 3581 bytes = bytes - (int)(zTail-zSql); 3582 } 3583 if( (int)strlen(zTail)<bytes ){ 3584 bytes = (int)strlen(zTail); 3585 } 3586 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3587 } 3588 if( rc!=SQLITE_OK ){ 3589 assert( pStmt==0 ); 3590 sprintf(zBuf, "(%d) ", rc); 3591 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3592 return TCL_ERROR; 3593 } 3594 3595 if( pStmt ){ 3596 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3597 Tcl_AppendResult(interp, zBuf, 0); 3598 } 3599 return TCL_OK; 3600 } 3601 3602 /* 3603 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? 3604 ** 3605 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3606 ** database handle <DB>. The parameter <tailval> is the name of a global 3607 ** variable that is set to the unused portion of <sql> (if any). A 3608 ** STMT handle is returned. 3609 */ 3610 static int test_prepare_v2( 3611 void * clientData, 3612 Tcl_Interp *interp, 3613 int objc, 3614 Tcl_Obj *CONST objv[] 3615 ){ 3616 sqlite3 *db; 3617 const char *zSql; 3618 int bytes; 3619 const char *zTail = 0; 3620 sqlite3_stmt *pStmt = 0; 3621 char zBuf[50]; 3622 int rc; 3623 3624 if( objc!=5 && objc!=4 ){ 3625 Tcl_AppendResult(interp, "wrong # args: should be \"", 3626 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3627 return TCL_ERROR; 3628 } 3629 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3630 zSql = Tcl_GetString(objv[2]); 3631 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3632 3633 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3634 assert(rc==SQLITE_OK || pStmt==0); 3635 Tcl_ResetResult(interp); 3636 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3637 if( zTail && objc>=5 ){ 3638 if( bytes>=0 ){ 3639 bytes = bytes - (int)(zTail-zSql); 3640 } 3641 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3642 } 3643 if( rc!=SQLITE_OK ){ 3644 assert( pStmt==0 ); 3645 sprintf(zBuf, "(%d) ", rc); 3646 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3647 return TCL_ERROR; 3648 } 3649 3650 if( pStmt ){ 3651 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3652 Tcl_AppendResult(interp, zBuf, 0); 3653 } 3654 return TCL_OK; 3655 } 3656 3657 /* 3658 ** Usage: sqlite3_prepare_tkt3134 DB 3659 ** 3660 ** Generate a prepared statement for a zero-byte string as a test 3661 ** for ticket #3134. The string should be preceeded by a zero byte. 3662 */ 3663 static int test_prepare_tkt3134( 3664 void * clientData, 3665 Tcl_Interp *interp, 3666 int objc, 3667 Tcl_Obj *CONST objv[] 3668 ){ 3669 sqlite3 *db; 3670 static const char zSql[] = "\000SELECT 1"; 3671 sqlite3_stmt *pStmt = 0; 3672 char zBuf[50]; 3673 int rc; 3674 3675 if( objc!=2 ){ 3676 Tcl_AppendResult(interp, "wrong # args: should be \"", 3677 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3678 return TCL_ERROR; 3679 } 3680 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3681 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); 3682 assert(rc==SQLITE_OK || pStmt==0); 3683 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3684 if( rc!=SQLITE_OK ){ 3685 assert( pStmt==0 ); 3686 sprintf(zBuf, "(%d) ", rc); 3687 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3688 return TCL_ERROR; 3689 } 3690 3691 if( pStmt ){ 3692 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3693 Tcl_AppendResult(interp, zBuf, 0); 3694 } 3695 return TCL_OK; 3696 } 3697 3698 /* 3699 ** Usage: sqlite3_prepare16 DB sql bytes tailvar 3700 ** 3701 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3702 ** database handle <DB>. The parameter <tailval> is the name of a global 3703 ** variable that is set to the unused portion of <sql> (if any). A 3704 ** STMT handle is returned. 3705 */ 3706 static int test_prepare16( 3707 void * clientData, 3708 Tcl_Interp *interp, 3709 int objc, 3710 Tcl_Obj *CONST objv[] 3711 ){ 3712 #ifndef SQLITE_OMIT_UTF16 3713 sqlite3 *db; 3714 const void *zSql; 3715 const void *zTail = 0; 3716 Tcl_Obj *pTail = 0; 3717 sqlite3_stmt *pStmt = 0; 3718 char zBuf[50]; 3719 int rc; 3720 int bytes; /* The integer specified as arg 3 */ 3721 int objlen; /* The byte-array length of arg 2 */ 3722 3723 if( objc!=5 && objc!=4 ){ 3724 Tcl_AppendResult(interp, "wrong # args: should be \"", 3725 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3726 return TCL_ERROR; 3727 } 3728 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3729 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3730 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3731 3732 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3733 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3734 if( rc ){ 3735 return TCL_ERROR; 3736 } 3737 3738 if( objc>=5 ){ 3739 if( zTail ){ 3740 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3741 }else{ 3742 objlen = 0; 3743 } 3744 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3745 Tcl_IncrRefCount(pTail); 3746 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3747 Tcl_DecrRefCount(pTail); 3748 } 3749 3750 if( pStmt ){ 3751 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3752 } 3753 Tcl_AppendResult(interp, zBuf, 0); 3754 #endif /* SQLITE_OMIT_UTF16 */ 3755 return TCL_OK; 3756 } 3757 3758 /* 3759 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? 3760 ** 3761 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3762 ** database handle <DB>. The parameter <tailval> is the name of a global 3763 ** variable that is set to the unused portion of <sql> (if any). A 3764 ** STMT handle is returned. 3765 */ 3766 static int test_prepare16_v2( 3767 void * clientData, 3768 Tcl_Interp *interp, 3769 int objc, 3770 Tcl_Obj *CONST objv[] 3771 ){ 3772 #ifndef SQLITE_OMIT_UTF16 3773 sqlite3 *db; 3774 const void *zSql; 3775 const void *zTail = 0; 3776 Tcl_Obj *pTail = 0; 3777 sqlite3_stmt *pStmt = 0; 3778 char zBuf[50]; 3779 int rc; 3780 int bytes; /* The integer specified as arg 3 */ 3781 int objlen; /* The byte-array length of arg 2 */ 3782 3783 if( objc!=5 && objc!=4 ){ 3784 Tcl_AppendResult(interp, "wrong # args: should be \"", 3785 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); 3786 return TCL_ERROR; 3787 } 3788 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3789 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3790 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3791 3792 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); 3793 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3794 if( rc ){ 3795 return TCL_ERROR; 3796 } 3797 3798 if( objc>=5 ){ 3799 if( zTail ){ 3800 objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); 3801 }else{ 3802 objlen = 0; 3803 } 3804 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3805 Tcl_IncrRefCount(pTail); 3806 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3807 Tcl_DecrRefCount(pTail); 3808 } 3809 3810 if( pStmt ){ 3811 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3812 } 3813 Tcl_AppendResult(interp, zBuf, 0); 3814 #endif /* SQLITE_OMIT_UTF16 */ 3815 return TCL_OK; 3816 } 3817 3818 /* 3819 ** Usage: sqlite3_open filename ?options-list? 3820 */ 3821 static int test_open( 3822 void * clientData, 3823 Tcl_Interp *interp, 3824 int objc, 3825 Tcl_Obj *CONST objv[] 3826 ){ 3827 const char *zFilename; 3828 sqlite3 *db; 3829 char zBuf[100]; 3830 3831 if( objc!=3 && objc!=2 && objc!=1 ){ 3832 Tcl_AppendResult(interp, "wrong # args: should be \"", 3833 Tcl_GetString(objv[0]), " filename options-list", 0); 3834 return TCL_ERROR; 3835 } 3836 3837 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; 3838 sqlite3_open(zFilename, &db); 3839 3840 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3841 Tcl_AppendResult(interp, zBuf, 0); 3842 return TCL_OK; 3843 } 3844 3845 /* 3846 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS 3847 */ 3848 static int test_open_v2( 3849 void * clientData, 3850 Tcl_Interp *interp, 3851 int objc, 3852 Tcl_Obj *CONST objv[] 3853 ){ 3854 const char *zFilename; 3855 const char *zVfs; 3856 int flags = 0; 3857 sqlite3 *db; 3858 int rc; 3859 char zBuf[100]; 3860 3861 int nFlag; 3862 Tcl_Obj **apFlag; 3863 int i; 3864 3865 if( objc!=4 ){ 3866 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); 3867 return TCL_ERROR; 3868 } 3869 zFilename = Tcl_GetString(objv[1]); 3870 zVfs = Tcl_GetString(objv[3]); 3871 if( zVfs[0]==0x00 ) zVfs = 0; 3872 3873 rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); 3874 if( rc!=TCL_OK ) return rc; 3875 for(i=0; i<nFlag; i++){ 3876 int iFlag; 3877 struct OpenFlag { 3878 const char *zFlag; 3879 int flag; 3880 } aFlag[] = { 3881 { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, 3882 { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, 3883 { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, 3884 { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, 3885 { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, 3886 { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, 3887 { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, 3888 { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, 3889 { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, 3890 { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, 3891 { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, 3892 { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, 3893 { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, 3894 { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, 3895 { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, 3896 { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, 3897 { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, 3898 { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, 3899 { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, 3900 { 0, 0 } 3901 }; 3902 rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), 3903 "flag", 0, &iFlag 3904 ); 3905 if( rc!=TCL_OK ) return rc; 3906 flags |= aFlag[iFlag].flag; 3907 } 3908 3909 rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); 3910 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3911 Tcl_AppendResult(interp, zBuf, 0); 3912 return TCL_OK; 3913 } 3914 3915 /* 3916 ** Usage: sqlite3_open16 filename options 3917 */ 3918 static int test_open16( 3919 void * clientData, 3920 Tcl_Interp *interp, 3921 int objc, 3922 Tcl_Obj *CONST objv[] 3923 ){ 3924 #ifndef SQLITE_OMIT_UTF16 3925 const void *zFilename; 3926 sqlite3 *db; 3927 char zBuf[100]; 3928 3929 if( objc!=3 ){ 3930 Tcl_AppendResult(interp, "wrong # args: should be \"", 3931 Tcl_GetString(objv[0]), " filename options-list", 0); 3932 return TCL_ERROR; 3933 } 3934 3935 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); 3936 sqlite3_open16(zFilename, &db); 3937 3938 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3939 Tcl_AppendResult(interp, zBuf, 0); 3940 #endif /* SQLITE_OMIT_UTF16 */ 3941 return TCL_OK; 3942 } 3943 3944 /* 3945 ** Usage: sqlite3_complete16 <UTF-16 string> 3946 ** 3947 ** Return 1 if the supplied argument is a complete SQL statement, or zero 3948 ** otherwise. 3949 */ 3950 static int test_complete16( 3951 void * clientData, 3952 Tcl_Interp *interp, 3953 int objc, 3954 Tcl_Obj *CONST objv[] 3955 ){ 3956 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) 3957 char *zBuf; 3958 3959 if( objc!=2 ){ 3960 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); 3961 return TCL_ERROR; 3962 } 3963 3964 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); 3965 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); 3966 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ 3967 return TCL_OK; 3968 } 3969 3970 /* 3971 ** Usage: sqlite3_step STMT 3972 ** 3973 ** Advance the statement to the next row. 3974 */ 3975 static int test_step( 3976 void * clientData, 3977 Tcl_Interp *interp, 3978 int objc, 3979 Tcl_Obj *CONST objv[] 3980 ){ 3981 sqlite3_stmt *pStmt; 3982 int rc; 3983 3984 if( objc!=2 ){ 3985 Tcl_AppendResult(interp, "wrong # args: should be \"", 3986 Tcl_GetString(objv[0]), " STMT", 0); 3987 return TCL_ERROR; 3988 } 3989 3990 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3991 rc = sqlite3_step(pStmt); 3992 3993 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ 3994 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 3995 return TCL_OK; 3996 } 3997 3998 static int test_sql( 3999 void * clientData, 4000 Tcl_Interp *interp, 4001 int objc, 4002 Tcl_Obj *CONST objv[] 4003 ){ 4004 sqlite3_stmt *pStmt; 4005 4006 if( objc!=2 ){ 4007 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 4008 return TCL_ERROR; 4009 } 4010 4011 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4012 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); 4013 return TCL_OK; 4014 } 4015 4016 /* 4017 ** Usage: sqlite3_column_count STMT 4018 ** 4019 ** Return the number of columns returned by the sql statement STMT. 4020 */ 4021 static int test_column_count( 4022 void * clientData, 4023 Tcl_Interp *interp, 4024 int objc, 4025 Tcl_Obj *CONST objv[] 4026 ){ 4027 sqlite3_stmt *pStmt; 4028 4029 if( objc!=2 ){ 4030 Tcl_AppendResult(interp, "wrong # args: should be \"", 4031 Tcl_GetString(objv[0]), " STMT column", 0); 4032 return TCL_ERROR; 4033 } 4034 4035 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4036 4037 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); 4038 return TCL_OK; 4039 } 4040 4041 /* 4042 ** Usage: sqlite3_column_type STMT column 4043 ** 4044 ** Return the type of the data in column 'column' of the current row. 4045 */ 4046 static int test_column_type( 4047 void * clientData, 4048 Tcl_Interp *interp, 4049 int objc, 4050 Tcl_Obj *CONST objv[] 4051 ){ 4052 sqlite3_stmt *pStmt; 4053 int col; 4054 int tp; 4055 4056 if( objc!=3 ){ 4057 Tcl_AppendResult(interp, "wrong # args: should be \"", 4058 Tcl_GetString(objv[0]), " STMT column", 0); 4059 return TCL_ERROR; 4060 } 4061 4062 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4063 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4064 4065 tp = sqlite3_column_type(pStmt, col); 4066 switch( tp ){ 4067 case SQLITE_INTEGER: 4068 Tcl_SetResult(interp, "INTEGER", TCL_STATIC); 4069 break; 4070 case SQLITE_NULL: 4071 Tcl_SetResult(interp, "NULL", TCL_STATIC); 4072 break; 4073 case SQLITE_FLOAT: 4074 Tcl_SetResult(interp, "FLOAT", TCL_STATIC); 4075 break; 4076 case SQLITE_TEXT: 4077 Tcl_SetResult(interp, "TEXT", TCL_STATIC); 4078 break; 4079 case SQLITE_BLOB: 4080 Tcl_SetResult(interp, "BLOB", TCL_STATIC); 4081 break; 4082 default: 4083 assert(0); 4084 } 4085 4086 return TCL_OK; 4087 } 4088 4089 /* 4090 ** Usage: sqlite3_column_int64 STMT column 4091 ** 4092 ** Return the data in column 'column' of the current row cast as an 4093 ** wide (64-bit) integer. 4094 */ 4095 static int test_column_int64( 4096 void * clientData, 4097 Tcl_Interp *interp, 4098 int objc, 4099 Tcl_Obj *CONST objv[] 4100 ){ 4101 sqlite3_stmt *pStmt; 4102 int col; 4103 i64 iVal; 4104 4105 if( objc!=3 ){ 4106 Tcl_AppendResult(interp, "wrong # args: should be \"", 4107 Tcl_GetString(objv[0]), " STMT column", 0); 4108 return TCL_ERROR; 4109 } 4110 4111 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4112 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4113 4114 iVal = sqlite3_column_int64(pStmt, col); 4115 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); 4116 return TCL_OK; 4117 } 4118 4119 /* 4120 ** Usage: sqlite3_column_blob STMT column 4121 */ 4122 static int test_column_blob( 4123 void * clientData, 4124 Tcl_Interp *interp, 4125 int objc, 4126 Tcl_Obj *CONST objv[] 4127 ){ 4128 sqlite3_stmt *pStmt; 4129 int col; 4130 4131 int len; 4132 const void *pBlob; 4133 4134 if( objc!=3 ){ 4135 Tcl_AppendResult(interp, "wrong # args: should be \"", 4136 Tcl_GetString(objv[0]), " STMT column", 0); 4137 return TCL_ERROR; 4138 } 4139 4140 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4141 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4142 4143 len = sqlite3_column_bytes(pStmt, col); 4144 pBlob = sqlite3_column_blob(pStmt, col); 4145 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); 4146 return TCL_OK; 4147 } 4148 4149 /* 4150 ** Usage: sqlite3_column_double STMT column 4151 ** 4152 ** Return the data in column 'column' of the current row cast as a double. 4153 */ 4154 static int test_column_double( 4155 void * clientData, 4156 Tcl_Interp *interp, 4157 int objc, 4158 Tcl_Obj *CONST objv[] 4159 ){ 4160 sqlite3_stmt *pStmt; 4161 int col; 4162 double rVal; 4163 4164 if( objc!=3 ){ 4165 Tcl_AppendResult(interp, "wrong # args: should be \"", 4166 Tcl_GetString(objv[0]), " STMT column", 0); 4167 return TCL_ERROR; 4168 } 4169 4170 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4171 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4172 4173 rVal = sqlite3_column_double(pStmt, col); 4174 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); 4175 return TCL_OK; 4176 } 4177 4178 /* 4179 ** Usage: sqlite3_data_count STMT 4180 ** 4181 ** Return the number of columns returned by the sql statement STMT. 4182 */ 4183 static int test_data_count( 4184 void * clientData, 4185 Tcl_Interp *interp, 4186 int objc, 4187 Tcl_Obj *CONST objv[] 4188 ){ 4189 sqlite3_stmt *pStmt; 4190 4191 if( objc!=2 ){ 4192 Tcl_AppendResult(interp, "wrong # args: should be \"", 4193 Tcl_GetString(objv[0]), " STMT column", 0); 4194 return TCL_ERROR; 4195 } 4196 4197 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4198 4199 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); 4200 return TCL_OK; 4201 } 4202 4203 /* 4204 ** Usage: sqlite3_column_text STMT column 4205 ** 4206 ** Usage: sqlite3_column_decltype STMT column 4207 ** 4208 ** Usage: sqlite3_column_name STMT column 4209 */ 4210 static int test_stmt_utf8( 4211 void * clientData, /* Pointer to SQLite API function to be invoke */ 4212 Tcl_Interp *interp, 4213 int objc, 4214 Tcl_Obj *CONST objv[] 4215 ){ 4216 sqlite3_stmt *pStmt; 4217 int col; 4218 const char *(*xFunc)(sqlite3_stmt*, int); 4219 const char *zRet; 4220 4221 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; 4222 if( objc!=3 ){ 4223 Tcl_AppendResult(interp, "wrong # args: should be \"", 4224 Tcl_GetString(objv[0]), " STMT column", 0); 4225 return TCL_ERROR; 4226 } 4227 4228 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4229 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4230 zRet = xFunc(pStmt, col); 4231 if( zRet ){ 4232 Tcl_SetResult(interp, (char *)zRet, 0); 4233 } 4234 return TCL_OK; 4235 } 4236 4237 static int test_global_recover( 4238 void * clientData, 4239 Tcl_Interp *interp, 4240 int objc, 4241 Tcl_Obj *CONST objv[] 4242 ){ 4243 #ifndef SQLITE_OMIT_DEPRECATED 4244 int rc; 4245 if( objc!=1 ){ 4246 Tcl_WrongNumArgs(interp, 1, objv, ""); 4247 return TCL_ERROR; 4248 } 4249 rc = sqlite3_global_recover(); 4250 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4251 #endif 4252 return TCL_OK; 4253 } 4254 4255 /* 4256 ** Usage: sqlite3_column_text STMT column 4257 ** 4258 ** Usage: sqlite3_column_decltype STMT column 4259 ** 4260 ** Usage: sqlite3_column_name STMT column 4261 */ 4262 static int test_stmt_utf16( 4263 void * clientData, /* Pointer to SQLite API function to be invoked */ 4264 Tcl_Interp *interp, 4265 int objc, 4266 Tcl_Obj *CONST objv[] 4267 ){ 4268 #ifndef SQLITE_OMIT_UTF16 4269 sqlite3_stmt *pStmt; 4270 int col; 4271 Tcl_Obj *pRet; 4272 const void *zName16; 4273 const void *(*xFunc)(sqlite3_stmt*, int); 4274 4275 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; 4276 if( objc!=3 ){ 4277 Tcl_AppendResult(interp, "wrong # args: should be \"", 4278 Tcl_GetString(objv[0]), " STMT column", 0); 4279 return TCL_ERROR; 4280 } 4281 4282 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4283 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4284 4285 zName16 = xFunc(pStmt, col); 4286 if( zName16 ){ 4287 int n; 4288 const char *z = zName16; 4289 for(n=0; z[n] || z[n+1]; n+=2){} 4290 pRet = Tcl_NewByteArrayObj(zName16, n+2); 4291 Tcl_SetObjResult(interp, pRet); 4292 } 4293 #endif /* SQLITE_OMIT_UTF16 */ 4294 4295 return TCL_OK; 4296 } 4297 4298 /* 4299 ** Usage: sqlite3_column_int STMT column 4300 ** 4301 ** Usage: sqlite3_column_bytes STMT column 4302 ** 4303 ** Usage: sqlite3_column_bytes16 STMT column 4304 ** 4305 */ 4306 static int test_stmt_int( 4307 void * clientData, /* Pointer to SQLite API function to be invoked */ 4308 Tcl_Interp *interp, 4309 int objc, 4310 Tcl_Obj *CONST objv[] 4311 ){ 4312 sqlite3_stmt *pStmt; 4313 int col; 4314 int (*xFunc)(sqlite3_stmt*, int); 4315 4316 xFunc = (int (*)(sqlite3_stmt*, int))clientData; 4317 if( objc!=3 ){ 4318 Tcl_AppendResult(interp, "wrong # args: should be \"", 4319 Tcl_GetString(objv[0]), " STMT column", 0); 4320 return TCL_ERROR; 4321 } 4322 4323 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 4324 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 4325 4326 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); 4327 return TCL_OK; 4328 } 4329 4330 /* 4331 ** Usage: sqlite_set_magic DB MAGIC-NUMBER 4332 ** 4333 ** Set the db->magic value. This is used to test error recovery logic. 4334 */ 4335 static int sqlite_set_magic( 4336 void * clientData, 4337 Tcl_Interp *interp, 4338 int argc, 4339 char **argv 4340 ){ 4341 sqlite3 *db; 4342 if( argc!=3 ){ 4343 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4344 " DB MAGIC", 0); 4345 return TCL_ERROR; 4346 } 4347 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4348 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ 4349 db->magic = SQLITE_MAGIC_OPEN; 4350 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ 4351 db->magic = SQLITE_MAGIC_CLOSED; 4352 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ 4353 db->magic = SQLITE_MAGIC_BUSY; 4354 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ 4355 db->magic = SQLITE_MAGIC_ERROR; 4356 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ 4357 return TCL_ERROR; 4358 } 4359 return TCL_OK; 4360 } 4361 4362 /* 4363 ** Usage: sqlite3_interrupt DB 4364 ** 4365 ** Trigger an interrupt on DB 4366 */ 4367 static int test_interrupt( 4368 void * clientData, 4369 Tcl_Interp *interp, 4370 int argc, 4371 char **argv 4372 ){ 4373 sqlite3 *db; 4374 if( argc!=2 ){ 4375 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); 4376 return TCL_ERROR; 4377 } 4378 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4379 sqlite3_interrupt(db); 4380 return TCL_OK; 4381 } 4382 4383 static u8 *sqlite3_stack_baseline = 0; 4384 4385 /* 4386 ** Fill the stack with a known bitpattern. 4387 */ 4388 static void prepStack(void){ 4389 int i; 4390 u32 bigBuf[65536]; 4391 for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; 4392 sqlite3_stack_baseline = (u8*)&bigBuf[65536]; 4393 } 4394 4395 /* 4396 ** Get the current stack depth. Used for debugging only. 4397 */ 4398 u64 sqlite3StackDepth(void){ 4399 u8 x; 4400 return (u64)(sqlite3_stack_baseline - &x); 4401 } 4402 4403 /* 4404 ** Usage: sqlite3_stack_used DB SQL 4405 ** 4406 ** Try to measure the amount of stack space used by a call to sqlite3_exec 4407 */ 4408 static int test_stack_used( 4409 void * clientData, 4410 Tcl_Interp *interp, 4411 int argc, 4412 char **argv 4413 ){ 4414 sqlite3 *db; 4415 int i; 4416 if( argc!=3 ){ 4417 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4418 " DB SQL", 0); 4419 return TCL_ERROR; 4420 } 4421 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4422 prepStack(); 4423 (void)sqlite3_exec(db, argv[2], 0, 0, 0); 4424 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} 4425 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); 4426 return TCL_OK; 4427 } 4428 4429 /* 4430 ** Usage: sqlite_delete_function DB function-name 4431 ** 4432 ** Delete the user function 'function-name' from database handle DB. It 4433 ** is assumed that the user function was created as UTF8, any number of 4434 ** arguments (the way the TCL interface does it). 4435 */ 4436 static int delete_function( 4437 void * clientData, 4438 Tcl_Interp *interp, 4439 int argc, 4440 char **argv 4441 ){ 4442 int rc; 4443 sqlite3 *db; 4444 if( argc!=3 ){ 4445 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4446 " DB function-name", 0); 4447 return TCL_ERROR; 4448 } 4449 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4450 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); 4451 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4452 return TCL_OK; 4453 } 4454 4455 /* 4456 ** Usage: sqlite_delete_collation DB collation-name 4457 ** 4458 ** Delete the collation sequence 'collation-name' from database handle 4459 ** DB. It is assumed that the collation sequence was created as UTF8 (the 4460 ** way the TCL interface does it). 4461 */ 4462 static int delete_collation( 4463 void * clientData, 4464 Tcl_Interp *interp, 4465 int argc, 4466 char **argv 4467 ){ 4468 int rc; 4469 sqlite3 *db; 4470 if( argc!=3 ){ 4471 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4472 " DB function-name", 0); 4473 return TCL_ERROR; 4474 } 4475 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4476 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); 4477 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 4478 return TCL_OK; 4479 } 4480 4481 /* 4482 ** Usage: sqlite3_get_autocommit DB 4483 ** 4484 ** Return true if the database DB is currently in auto-commit mode. 4485 ** Return false if not. 4486 */ 4487 static int get_autocommit( 4488 void * clientData, 4489 Tcl_Interp *interp, 4490 int argc, 4491 char **argv 4492 ){ 4493 char zBuf[30]; 4494 sqlite3 *db; 4495 if( argc!=2 ){ 4496 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4497 " DB", 0); 4498 return TCL_ERROR; 4499 } 4500 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4501 sprintf(zBuf, "%d", sqlite3_get_autocommit(db)); 4502 Tcl_AppendResult(interp, zBuf, 0); 4503 return TCL_OK; 4504 } 4505 4506 /* 4507 ** Usage: sqlite3_busy_timeout DB MS 4508 ** 4509 ** Set the busy timeout. This is more easily done using the timeout 4510 ** method of the TCL interface. But we need a way to test the case 4511 ** where it returns SQLITE_MISUSE. 4512 */ 4513 static int test_busy_timeout( 4514 void * clientData, 4515 Tcl_Interp *interp, 4516 int argc, 4517 char **argv 4518 ){ 4519 int rc, ms; 4520 sqlite3 *db; 4521 if( argc!=3 ){ 4522 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 4523 " DB", 0); 4524 return TCL_ERROR; 4525 } 4526 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 4527 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; 4528 rc = sqlite3_busy_timeout(db, ms); 4529 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); 4530 return TCL_OK; 4531 } 4532 4533 /* 4534 ** Usage: tcl_variable_type VARIABLENAME 4535 ** 4536 ** Return the name of the internal representation for the 4537 ** value of the given variable. 4538 */ 4539 static int tcl_variable_type( 4540 void * clientData, 4541 Tcl_Interp *interp, 4542 int objc, 4543 Tcl_Obj *CONST objv[] 4544 ){ 4545 Tcl_Obj *pVar; 4546 if( objc!=2 ){ 4547 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); 4548 return TCL_ERROR; 4549 } 4550 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); 4551 if( pVar==0 ) return TCL_ERROR; 4552 if( pVar->typePtr ){ 4553 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); 4554 } 4555 return TCL_OK; 4556 } 4557 4558 /* 4559 ** Usage: sqlite3_release_memory ?N? 4560 ** 4561 ** Attempt to release memory currently held but not actually required. 4562 ** The integer N is the number of bytes we are trying to release. The 4563 ** return value is the amount of memory actually released. 4564 */ 4565 static int test_release_memory( 4566 void * clientData, 4567 Tcl_Interp *interp, 4568 int objc, 4569 Tcl_Obj *CONST objv[] 4570 ){ 4571 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) 4572 int N; 4573 int amt; 4574 if( objc!=1 && objc!=2 ){ 4575 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4576 return TCL_ERROR; 4577 } 4578 if( objc==2 ){ 4579 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4580 }else{ 4581 N = -1; 4582 } 4583 amt = sqlite3_release_memory(N); 4584 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); 4585 #endif 4586 return TCL_OK; 4587 } 4588 4589 4590 /* 4591 ** Usage: sqlite3_db_release_memory DB 4592 ** 4593 ** Attempt to release memory currently held by database DB. Return the 4594 ** result code (which in the current implementation is always zero). 4595 */ 4596 static int test_db_release_memory( 4597 void * clientData, 4598 Tcl_Interp *interp, 4599 int objc, 4600 Tcl_Obj *CONST objv[] 4601 ){ 4602 sqlite3 *db; 4603 int rc; 4604 if( objc!=2 ){ 4605 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 4606 return TCL_ERROR; 4607 } 4608 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4609 rc = sqlite3_db_release_memory(db); 4610 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 4611 return TCL_OK; 4612 } 4613 4614 /* 4615 ** Usage: sqlite3_db_filename DB DBNAME 4616 ** 4617 ** Return the name of a file associated with a database. 4618 */ 4619 static int test_db_filename( 4620 void * clientData, 4621 Tcl_Interp *interp, 4622 int objc, 4623 Tcl_Obj *CONST objv[] 4624 ){ 4625 sqlite3 *db; 4626 const char *zDbName; 4627 if( objc!=3 ){ 4628 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4629 return TCL_ERROR; 4630 } 4631 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4632 zDbName = Tcl_GetString(objv[2]); 4633 Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); 4634 return TCL_OK; 4635 } 4636 4637 /* 4638 ** Usage: sqlite3_db_readonly DB DBNAME 4639 ** 4640 ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does 4641 ** not exist. 4642 */ 4643 static int test_db_readonly( 4644 void * clientData, 4645 Tcl_Interp *interp, 4646 int objc, 4647 Tcl_Obj *CONST objv[] 4648 ){ 4649 sqlite3 *db; 4650 const char *zDbName; 4651 if( objc!=3 ){ 4652 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); 4653 return TCL_ERROR; 4654 } 4655 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4656 zDbName = Tcl_GetString(objv[2]); 4657 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName))); 4658 return TCL_OK; 4659 } 4660 4661 /* 4662 ** Usage: sqlite3_soft_heap_limit ?N? 4663 ** 4664 ** Query or set the soft heap limit for the current thread. The 4665 ** limit is only changed if the N is present. The previous limit 4666 ** is returned. 4667 */ 4668 static int test_soft_heap_limit( 4669 void * clientData, 4670 Tcl_Interp *interp, 4671 int objc, 4672 Tcl_Obj *CONST objv[] 4673 ){ 4674 sqlite3_int64 amt; 4675 Tcl_WideInt N = -1; 4676 if( objc!=1 && objc!=2 ){ 4677 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4678 return TCL_ERROR; 4679 } 4680 if( objc==2 ){ 4681 if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4682 } 4683 amt = sqlite3_soft_heap_limit64(N); 4684 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); 4685 return TCL_OK; 4686 } 4687 4688 /* 4689 ** Usage: sqlite3_thread_cleanup 4690 ** 4691 ** Call the sqlite3_thread_cleanup API. 4692 */ 4693 static int test_thread_cleanup( 4694 void * clientData, 4695 Tcl_Interp *interp, 4696 int objc, 4697 Tcl_Obj *CONST objv[] 4698 ){ 4699 #ifndef SQLITE_OMIT_DEPRECATED 4700 sqlite3_thread_cleanup(); 4701 #endif 4702 return TCL_OK; 4703 } 4704 4705 /* 4706 ** Usage: sqlite3_pager_refcounts DB 4707 ** 4708 ** Return a list of numbers which are the PagerRefcount for all 4709 ** pagers on each database connection. 4710 */ 4711 static int test_pager_refcounts( 4712 void * clientData, 4713 Tcl_Interp *interp, 4714 int objc, 4715 Tcl_Obj *CONST objv[] 4716 ){ 4717 sqlite3 *db; 4718 int i; 4719 int v, *a; 4720 Tcl_Obj *pResult; 4721 4722 if( objc!=2 ){ 4723 Tcl_AppendResult(interp, "wrong # args: should be \"", 4724 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 4725 return TCL_ERROR; 4726 } 4727 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4728 pResult = Tcl_NewObj(); 4729 for(i=0; i<db->nDb; i++){ 4730 if( db->aDb[i].pBt==0 ){ 4731 v = -1; 4732 }else{ 4733 sqlite3_mutex_enter(db->mutex); 4734 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); 4735 v = a[0]; 4736 sqlite3_mutex_leave(db->mutex); 4737 } 4738 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); 4739 } 4740 Tcl_SetObjResult(interp, pResult); 4741 return TCL_OK; 4742 } 4743 4744 4745 /* 4746 ** tclcmd: working_64bit_int 4747 ** 4748 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This 4749 ** leads to a number of test failures. The present command checks the 4750 ** TCL build to see whether or not it supports 64-bit integers. It 4751 ** returns TRUE if it does and FALSE if not. 4752 ** 4753 ** This command is used to warn users that their TCL build is defective 4754 ** and that the errors they are seeing in the test scripts might be 4755 ** a result of their defective TCL rather than problems in SQLite. 4756 */ 4757 static int working_64bit_int( 4758 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4759 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4760 int objc, /* Number of arguments */ 4761 Tcl_Obj *CONST objv[] /* Command arguments */ 4762 ){ 4763 Tcl_Obj *pTestObj; 4764 int working = 0; 4765 4766 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); 4767 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; 4768 Tcl_DecrRefCount(pTestObj); 4769 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); 4770 return TCL_OK; 4771 } 4772 4773 4774 /* 4775 ** tclcmd: vfs_unlink_test 4776 ** 4777 ** This TCL command unregisters the primary VFS and then registers 4778 ** it back again. This is used to test the ability to register a 4779 ** VFS when none are previously registered, and the ability to 4780 ** unregister the only available VFS. Ticket #2738 4781 */ 4782 static int vfs_unlink_test( 4783 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4784 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4785 int objc, /* Number of arguments */ 4786 Tcl_Obj *CONST objv[] /* Command arguments */ 4787 ){ 4788 int i; 4789 sqlite3_vfs *pMain; 4790 sqlite3_vfs *apVfs[20]; 4791 sqlite3_vfs one, two; 4792 4793 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ 4794 one.zName = "__one"; 4795 two.zName = "__two"; 4796 4797 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not 4798 ** change the default VFS 4799 */ 4800 pMain = sqlite3_vfs_find(0); 4801 sqlite3_vfs_register(&one, 0); 4802 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 4803 sqlite3_vfs_register(&two, 0); 4804 assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); 4805 4806 /* We can find a VFS by its name */ 4807 assert( sqlite3_vfs_find("__one")==&one ); 4808 assert( sqlite3_vfs_find("__two")==&two ); 4809 4810 /* Calling sqlite_vfs_register with non-zero second parameter changes the 4811 ** default VFS, even if the 1st parameter is an existig VFS that is 4812 ** previously registered as the non-default. 4813 */ 4814 sqlite3_vfs_register(&one, 1); 4815 assert( sqlite3_vfs_find("__one")==&one ); 4816 assert( sqlite3_vfs_find("__two")==&two ); 4817 assert( sqlite3_vfs_find(0)==&one ); 4818 sqlite3_vfs_register(&two, 1); 4819 assert( sqlite3_vfs_find("__one")==&one ); 4820 assert( sqlite3_vfs_find("__two")==&two ); 4821 assert( sqlite3_vfs_find(0)==&two ); 4822 if( pMain ){ 4823 sqlite3_vfs_register(pMain, 1); 4824 assert( sqlite3_vfs_find("__one")==&one ); 4825 assert( sqlite3_vfs_find("__two")==&two ); 4826 assert( sqlite3_vfs_find(0)==pMain ); 4827 } 4828 4829 /* Unlink the default VFS. Repeat until there are no more VFSes 4830 ** registered. 4831 */ 4832 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ 4833 apVfs[i] = sqlite3_vfs_find(0); 4834 if( apVfs[i] ){ 4835 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 4836 sqlite3_vfs_unregister(apVfs[i]); 4837 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); 4838 } 4839 } 4840 assert( 0==sqlite3_vfs_find(0) ); 4841 4842 /* Register the main VFS as non-default (will be made default, since 4843 ** it'll be the only one in existence). 4844 */ 4845 sqlite3_vfs_register(pMain, 0); 4846 assert( sqlite3_vfs_find(0)==pMain ); 4847 4848 /* Un-register the main VFS again to restore an empty VFS list */ 4849 sqlite3_vfs_unregister(pMain); 4850 assert( 0==sqlite3_vfs_find(0) ); 4851 4852 /* Relink all VFSes in reverse order. */ 4853 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ 4854 if( apVfs[i] ){ 4855 sqlite3_vfs_register(apVfs[i], 1); 4856 assert( apVfs[i]==sqlite3_vfs_find(0) ); 4857 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); 4858 } 4859 } 4860 4861 /* Unregister out sample VFSes. */ 4862 sqlite3_vfs_unregister(&one); 4863 sqlite3_vfs_unregister(&two); 4864 4865 /* Unregistering a VFS that is not currently registered is harmless */ 4866 sqlite3_vfs_unregister(&one); 4867 sqlite3_vfs_unregister(&two); 4868 assert( sqlite3_vfs_find("__one")==0 ); 4869 assert( sqlite3_vfs_find("__two")==0 ); 4870 4871 /* We should be left with the original default VFS back as the 4872 ** original */ 4873 assert( sqlite3_vfs_find(0)==pMain ); 4874 4875 return TCL_OK; 4876 } 4877 4878 /* 4879 ** tclcmd: vfs_initfail_test 4880 ** 4881 ** This TCL command attempts to vfs_find and vfs_register when the 4882 ** sqlite3_initialize() interface is failing. All calls should fail. 4883 */ 4884 static int vfs_initfail_test( 4885 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4886 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4887 int objc, /* Number of arguments */ 4888 Tcl_Obj *CONST objv[] /* Command arguments */ 4889 ){ 4890 sqlite3_vfs one; 4891 one.zName = "__one"; 4892 4893 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4894 sqlite3_vfs_register(&one, 0); 4895 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4896 sqlite3_vfs_register(&one, 1); 4897 if( sqlite3_vfs_find(0) ) return TCL_ERROR; 4898 return TCL_OK; 4899 } 4900 4901 /* 4902 ** Saved VFSes 4903 */ 4904 static sqlite3_vfs *apVfs[20]; 4905 static int nVfs = 0; 4906 4907 /* 4908 ** tclcmd: vfs_unregister_all 4909 ** 4910 ** Unregister all VFSes. 4911 */ 4912 static int vfs_unregister_all( 4913 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4914 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4915 int objc, /* Number of arguments */ 4916 Tcl_Obj *CONST objv[] /* Command arguments */ 4917 ){ 4918 int i; 4919 for(i=0; i<ArraySize(apVfs); i++){ 4920 apVfs[i] = sqlite3_vfs_find(0); 4921 if( apVfs[i]==0 ) break; 4922 sqlite3_vfs_unregister(apVfs[i]); 4923 } 4924 nVfs = i; 4925 return TCL_OK; 4926 } 4927 /* 4928 ** tclcmd: vfs_reregister_all 4929 ** 4930 ** Restore all VFSes that were removed using vfs_unregister_all 4931 */ 4932 static int vfs_reregister_all( 4933 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4934 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4935 int objc, /* Number of arguments */ 4936 Tcl_Obj *CONST objv[] /* Command arguments */ 4937 ){ 4938 int i; 4939 for(i=0; i<nVfs; i++){ 4940 sqlite3_vfs_register(apVfs[i], i==0); 4941 } 4942 return TCL_OK; 4943 } 4944 4945 4946 /* 4947 ** tclcmd: file_control_test DB 4948 ** 4949 ** This TCL command runs the sqlite3_file_control interface and 4950 ** verifies correct operation of the same. 4951 */ 4952 static int file_control_test( 4953 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4954 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4955 int objc, /* Number of arguments */ 4956 Tcl_Obj *CONST objv[] /* Command arguments */ 4957 ){ 4958 int iArg = 0; 4959 sqlite3 *db; 4960 int rc; 4961 4962 if( objc!=2 ){ 4963 Tcl_AppendResult(interp, "wrong # args: should be \"", 4964 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 4965 return TCL_ERROR; 4966 } 4967 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4968 rc = sqlite3_file_control(db, 0, 0, &iArg); 4969 assert( rc==SQLITE_NOTFOUND ); 4970 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); 4971 assert( rc==SQLITE_ERROR ); 4972 rc = sqlite3_file_control(db, "main", -1, &iArg); 4973 assert( rc==SQLITE_NOTFOUND ); 4974 rc = sqlite3_file_control(db, "temp", -1, &iArg); 4975 assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); 4976 4977 return TCL_OK; 4978 } 4979 4980 4981 /* 4982 ** tclcmd: file_control_lasterrno_test DB 4983 ** 4984 ** This TCL command runs the sqlite3_file_control interface and 4985 ** verifies correct operation of the SQLITE_LAST_ERRNO verb. 4986 */ 4987 static int file_control_lasterrno_test( 4988 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4989 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4990 int objc, /* Number of arguments */ 4991 Tcl_Obj *CONST objv[] /* Command arguments */ 4992 ){ 4993 int iArg = 0; 4994 sqlite3 *db; 4995 int rc; 4996 4997 if( objc!=2 ){ 4998 Tcl_AppendResult(interp, "wrong # args: should be \"", 4999 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 5000 return TCL_ERROR; 5001 } 5002 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5003 return TCL_ERROR; 5004 } 5005 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); 5006 if( rc ){ 5007 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5008 return TCL_ERROR; 5009 } 5010 if( iArg!=0 ) { 5011 Tcl_AppendResult(interp, "Unexpected non-zero errno: ", 5012 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); 5013 return TCL_ERROR; 5014 } 5015 return TCL_OK; 5016 } 5017 5018 /* 5019 ** tclcmd: file_control_chunksize_test DB DBNAME SIZE 5020 ** 5021 ** This TCL command runs the sqlite3_file_control interface and 5022 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5023 ** SQLITE_SET_LOCKPROXYFILE verbs. 5024 */ 5025 static int file_control_chunksize_test( 5026 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5027 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5028 int objc, /* Number of arguments */ 5029 Tcl_Obj *CONST objv[] /* Command arguments */ 5030 ){ 5031 int nSize; /* New chunk size */ 5032 char *zDb; /* Db name ("main", "temp" etc.) */ 5033 sqlite3 *db; /* Database handle */ 5034 int rc; /* file_control() return code */ 5035 5036 if( objc!=4 ){ 5037 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5038 return TCL_ERROR; 5039 } 5040 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5041 || Tcl_GetIntFromObj(interp, objv[3], &nSize) 5042 ){ 5043 return TCL_ERROR; 5044 } 5045 zDb = Tcl_GetString(objv[2]); 5046 if( zDb[0]=='\0' ) zDb = NULL; 5047 5048 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); 5049 if( rc ){ 5050 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5051 return TCL_ERROR; 5052 } 5053 return TCL_OK; 5054 } 5055 5056 /* 5057 ** tclcmd: file_control_sizehint_test DB DBNAME SIZE 5058 ** 5059 ** This TCL command runs the sqlite3_file_control interface 5060 ** with SQLITE_FCNTL_SIZE_HINT 5061 */ 5062 static int file_control_sizehint_test( 5063 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5064 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5065 int objc, /* Number of arguments */ 5066 Tcl_Obj *CONST objv[] /* Command arguments */ 5067 ){ 5068 Tcl_WideInt nSize; /* Hinted size */ 5069 char *zDb; /* Db name ("main", "temp" etc.) */ 5070 sqlite3 *db; /* Database handle */ 5071 int rc; /* file_control() return code */ 5072 5073 if( objc!=4 ){ 5074 Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); 5075 return TCL_ERROR; 5076 } 5077 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5078 || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) 5079 ){ 5080 return TCL_ERROR; 5081 } 5082 zDb = Tcl_GetString(objv[2]); 5083 if( zDb[0]=='\0' ) zDb = NULL; 5084 5085 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); 5086 if( rc ){ 5087 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); 5088 return TCL_ERROR; 5089 } 5090 return TCL_OK; 5091 } 5092 5093 /* 5094 ** tclcmd: file_control_lockproxy_test DB PWD 5095 ** 5096 ** This TCL command runs the sqlite3_file_control interface and 5097 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and 5098 ** SQLITE_SET_LOCKPROXYFILE verbs. 5099 */ 5100 static int file_control_lockproxy_test( 5101 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5102 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5103 int objc, /* Number of arguments */ 5104 Tcl_Obj *CONST objv[] /* Command arguments */ 5105 ){ 5106 sqlite3 *db; 5107 5108 if( objc!=3 ){ 5109 Tcl_AppendResult(interp, "wrong # args: should be \"", 5110 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); 5111 return TCL_ERROR; 5112 } 5113 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5114 return TCL_ERROR; 5115 } 5116 5117 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 5118 # if defined(__APPLE__) 5119 # define SQLITE_ENABLE_LOCKING_STYLE 1 5120 # else 5121 # define SQLITE_ENABLE_LOCKING_STYLE 0 5122 # endif 5123 #endif 5124 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 5125 { 5126 char *testPath; 5127 int rc; 5128 int nPwd; 5129 const char *zPwd; 5130 char proxyPath[400]; 5131 5132 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); 5133 if( sizeof(proxyPath)<nPwd+20 ){ 5134 Tcl_AppendResult(interp, "PWD too big", (void*)0); 5135 return TCL_ERROR; 5136 } 5137 sprintf(proxyPath, "%s/test.proxy", zPwd); 5138 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5139 if( rc ){ 5140 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5141 return TCL_ERROR; 5142 } 5143 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); 5144 if( strncmp(proxyPath,testPath,11) ){ 5145 Tcl_AppendResult(interp, "Lock proxy file did not match the " 5146 "previously assigned value", 0); 5147 return TCL_ERROR; 5148 } 5149 if( rc ){ 5150 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5151 return TCL_ERROR; 5152 } 5153 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); 5154 if( rc ){ 5155 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5156 return TCL_ERROR; 5157 } 5158 } 5159 #endif 5160 return TCL_OK; 5161 } 5162 5163 /* 5164 ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY 5165 ** 5166 ** This TCL command runs the sqlite3_file_control interface with 5167 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. 5168 */ 5169 static int file_control_win32_av_retry( 5170 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5171 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5172 int objc, /* Number of arguments */ 5173 Tcl_Obj *CONST objv[] /* Command arguments */ 5174 ){ 5175 sqlite3 *db; 5176 int rc; 5177 int a[2]; 5178 char z[100]; 5179 5180 if( objc!=4 ){ 5181 Tcl_AppendResult(interp, "wrong # args: should be \"", 5182 Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); 5183 return TCL_ERROR; 5184 } 5185 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5186 return TCL_ERROR; 5187 } 5188 if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; 5189 if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; 5190 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); 5191 sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); 5192 Tcl_AppendResult(interp, z, (char*)0); 5193 return TCL_OK; 5194 } 5195 5196 /* 5197 ** tclcmd: file_control_persist_wal DB PERSIST-FLAG 5198 ** 5199 ** This TCL command runs the sqlite3_file_control interface with 5200 ** the SQLITE_FCNTL_PERSIST_WAL opcode. 5201 */ 5202 static int file_control_persist_wal( 5203 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5204 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5205 int objc, /* Number of arguments */ 5206 Tcl_Obj *CONST objv[] /* Command arguments */ 5207 ){ 5208 sqlite3 *db; 5209 int rc; 5210 int bPersist; 5211 char z[100]; 5212 5213 if( objc!=3 ){ 5214 Tcl_AppendResult(interp, "wrong # args: should be \"", 5215 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5216 return TCL_ERROR; 5217 } 5218 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5219 return TCL_ERROR; 5220 } 5221 if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; 5222 rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); 5223 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); 5224 Tcl_AppendResult(interp, z, (char*)0); 5225 return TCL_OK; 5226 } 5227 5228 /* 5229 ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG 5230 ** 5231 ** This TCL command runs the sqlite3_file_control interface with 5232 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. 5233 */ 5234 static int file_control_powersafe_overwrite( 5235 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5236 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5237 int objc, /* Number of arguments */ 5238 Tcl_Obj *CONST objv[] /* Command arguments */ 5239 ){ 5240 sqlite3 *db; 5241 int rc; 5242 int b; 5243 char z[100]; 5244 5245 if( objc!=3 ){ 5246 Tcl_AppendResult(interp, "wrong # args: should be \"", 5247 Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); 5248 return TCL_ERROR; 5249 } 5250 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5251 return TCL_ERROR; 5252 } 5253 if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR; 5254 rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b); 5255 sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b); 5256 Tcl_AppendResult(interp, z, (char*)0); 5257 return TCL_OK; 5258 } 5259 5260 5261 /* 5262 ** tclcmd: file_control_vfsname DB ?AUXDB? 5263 ** 5264 ** Return a string that describes the stack of VFSes. 5265 */ 5266 static int file_control_vfsname( 5267 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5268 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5269 int objc, /* Number of arguments */ 5270 Tcl_Obj *CONST objv[] /* Command arguments */ 5271 ){ 5272 sqlite3 *db; 5273 const char *zDbName = "main"; 5274 char *zVfsName = 0; 5275 5276 if( objc!=2 && objc!=3 ){ 5277 Tcl_AppendResult(interp, "wrong # args: should be \"", 5278 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5279 return TCL_ERROR; 5280 } 5281 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5282 return TCL_ERROR; 5283 } 5284 if( objc==3 ){ 5285 zDbName = Tcl_GetString(objv[2]); 5286 } 5287 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); 5288 Tcl_AppendResult(interp, zVfsName, (char*)0); 5289 sqlite3_free(zVfsName); 5290 return TCL_OK; 5291 } 5292 5293 /* 5294 ** tclcmd: file_control_tempfilename DB ?AUXDB? 5295 ** 5296 ** Return a string that is a temporary filename 5297 */ 5298 static int file_control_tempfilename( 5299 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5300 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5301 int objc, /* Number of arguments */ 5302 Tcl_Obj *CONST objv[] /* Command arguments */ 5303 ){ 5304 sqlite3 *db; 5305 const char *zDbName = "main"; 5306 char *zTName = 0; 5307 5308 if( objc!=2 && objc!=3 ){ 5309 Tcl_AppendResult(interp, "wrong # args: should be \"", 5310 Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); 5311 return TCL_ERROR; 5312 } 5313 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5314 return TCL_ERROR; 5315 } 5316 if( objc==3 ){ 5317 zDbName = Tcl_GetString(objv[2]); 5318 } 5319 sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName); 5320 Tcl_AppendResult(interp, zTName, (char*)0); 5321 sqlite3_free(zTName); 5322 return TCL_OK; 5323 } 5324 5325 5326 /* 5327 ** tclcmd: sqlite3_vfs_list 5328 ** 5329 ** Return a tcl list containing the names of all registered vfs's. 5330 */ 5331 static int vfs_list( 5332 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5333 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5334 int objc, /* Number of arguments */ 5335 Tcl_Obj *CONST objv[] /* Command arguments */ 5336 ){ 5337 sqlite3_vfs *pVfs; 5338 Tcl_Obj *pRet = Tcl_NewObj(); 5339 if( objc!=1 ){ 5340 Tcl_WrongNumArgs(interp, 1, objv, ""); 5341 return TCL_ERROR; 5342 } 5343 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ 5344 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); 5345 } 5346 Tcl_SetObjResult(interp, pRet); 5347 return TCL_OK; 5348 } 5349 5350 /* 5351 ** tclcmd: sqlite3_limit DB ID VALUE 5352 ** 5353 ** This TCL command runs the sqlite3_limit interface and 5354 ** verifies correct operation of the same. 5355 */ 5356 static int test_limit( 5357 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5358 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5359 int objc, /* Number of arguments */ 5360 Tcl_Obj *CONST objv[] /* Command arguments */ 5361 ){ 5362 sqlite3 *db; 5363 int rc; 5364 static const struct { 5365 char *zName; 5366 int id; 5367 } aId[] = { 5368 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, 5369 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, 5370 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, 5371 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, 5372 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, 5373 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, 5374 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, 5375 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, 5376 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, 5377 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, 5378 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, 5379 5380 /* Out of range test cases */ 5381 { "SQLITE_LIMIT_TOOSMALL", -1, }, 5382 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 }, 5383 }; 5384 int i, id; 5385 int val; 5386 const char *zId; 5387 5388 if( objc!=4 ){ 5389 Tcl_AppendResult(interp, "wrong # args: should be \"", 5390 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); 5391 return TCL_ERROR; 5392 } 5393 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5394 zId = Tcl_GetString(objv[2]); 5395 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ 5396 if( strcmp(zId, aId[i].zName)==0 ){ 5397 id = aId[i].id; 5398 break; 5399 } 5400 } 5401 if( i>=sizeof(aId)/sizeof(aId[0]) ){ 5402 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); 5403 return TCL_ERROR; 5404 } 5405 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; 5406 rc = sqlite3_limit(db, id, val); 5407 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 5408 return TCL_OK; 5409 } 5410 5411 /* 5412 ** tclcmd: save_prng_state 5413 ** 5414 ** Save the state of the pseudo-random number generator. 5415 ** At the same time, verify that sqlite3_test_control works even when 5416 ** called with an out-of-range opcode. 5417 */ 5418 static int save_prng_state( 5419 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5420 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5421 int objc, /* Number of arguments */ 5422 Tcl_Obj *CONST objv[] /* Command arguments */ 5423 ){ 5424 int rc = sqlite3_test_control(9999); 5425 assert( rc==0 ); 5426 rc = sqlite3_test_control(-1); 5427 assert( rc==0 ); 5428 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); 5429 return TCL_OK; 5430 } 5431 /* 5432 ** tclcmd: restore_prng_state 5433 */ 5434 static int restore_prng_state( 5435 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5436 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5437 int objc, /* Number of arguments */ 5438 Tcl_Obj *CONST objv[] /* Command arguments */ 5439 ){ 5440 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); 5441 return TCL_OK; 5442 } 5443 /* 5444 ** tclcmd: reset_prng_state 5445 */ 5446 static int reset_prng_state( 5447 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5448 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5449 int objc, /* Number of arguments */ 5450 Tcl_Obj *CONST objv[] /* Command arguments */ 5451 ){ 5452 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); 5453 return TCL_OK; 5454 } 5455 5456 /* 5457 ** tclcmd: pcache_stats 5458 */ 5459 static int test_pcache_stats( 5460 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 5461 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5462 int objc, /* Number of arguments */ 5463 Tcl_Obj *CONST objv[] /* Command arguments */ 5464 ){ 5465 int nMin; 5466 int nMax; 5467 int nCurrent; 5468 int nRecyclable; 5469 Tcl_Obj *pRet; 5470 5471 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); 5472 5473 pRet = Tcl_NewObj(); 5474 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); 5475 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); 5476 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); 5477 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); 5478 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); 5479 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); 5480 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); 5481 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); 5482 5483 Tcl_SetObjResult(interp, pRet); 5484 5485 return TCL_OK; 5486 } 5487 5488 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5489 static void test_unlock_notify_cb(void **aArg, int nArg){ 5490 int ii; 5491 for(ii=0; ii<nArg; ii++){ 5492 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); 5493 } 5494 } 5495 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ 5496 5497 /* 5498 ** tclcmd: sqlite3_unlock_notify db 5499 */ 5500 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 5501 static int test_unlock_notify( 5502 ClientData clientData, /* Unused */ 5503 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5504 int objc, /* Number of arguments */ 5505 Tcl_Obj *CONST objv[] /* Command arguments */ 5506 ){ 5507 sqlite3 *db; 5508 int rc; 5509 5510 if( objc!=2 ){ 5511 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 5512 return TCL_ERROR; 5513 } 5514 5515 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5516 return TCL_ERROR; 5517 } 5518 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); 5519 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5520 return TCL_OK; 5521 } 5522 #endif 5523 5524 /* 5525 ** tclcmd: sqlite3_wal_checkpoint db ?NAME? 5526 */ 5527 static int test_wal_checkpoint( 5528 ClientData clientData, /* Unused */ 5529 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5530 int objc, /* Number of arguments */ 5531 Tcl_Obj *CONST objv[] /* Command arguments */ 5532 ){ 5533 char *zDb = 0; 5534 sqlite3 *db; 5535 int rc; 5536 5537 if( objc!=3 && objc!=2 ){ 5538 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); 5539 return TCL_ERROR; 5540 } 5541 5542 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ 5543 return TCL_ERROR; 5544 } 5545 if( objc==3 ){ 5546 zDb = Tcl_GetString(objv[2]); 5547 } 5548 rc = sqlite3_wal_checkpoint(db, zDb); 5549 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 5550 return TCL_OK; 5551 } 5552 5553 /* 5554 ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? 5555 ** 5556 ** This command calls the wal_checkpoint_v2() function with the specified 5557 ** mode argument (passive, full or restart). If present, the database name 5558 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the 5559 ** NAME argument is not present, a NULL pointer is passed instead. 5560 ** 5561 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or 5562 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set 5563 ** to the error message obtained from sqlite3_errmsg(). 5564 ** 5565 ** Otherwise, this command returns a list of three integers. The first integer 5566 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers 5567 ** are the values returned via the output parameters by wal_checkpoint_v2() - 5568 ** the number of frames in the log and the number of frames in the log 5569 ** that have been checkpointed. 5570 */ 5571 static int test_wal_checkpoint_v2( 5572 ClientData clientData, /* Unused */ 5573 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5574 int objc, /* Number of arguments */ 5575 Tcl_Obj *CONST objv[] /* Command arguments */ 5576 ){ 5577 char *zDb = 0; 5578 sqlite3 *db; 5579 int rc; 5580 5581 int eMode; 5582 int nLog = -555; 5583 int nCkpt = -555; 5584 Tcl_Obj *pRet; 5585 5586 const char * aMode[] = { "passive", "full", "restart", 0 }; 5587 assert( SQLITE_CHECKPOINT_PASSIVE==0 ); 5588 assert( SQLITE_CHECKPOINT_FULL==1 ); 5589 assert( SQLITE_CHECKPOINT_RESTART==2 ); 5590 5591 if( objc!=3 && objc!=4 ){ 5592 Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); 5593 return TCL_ERROR; 5594 } 5595 5596 if( objc==4 ){ 5597 zDb = Tcl_GetString(objv[3]); 5598 } 5599 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) 5600 || Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) 5601 ){ 5602 return TCL_ERROR; 5603 } 5604 5605 rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); 5606 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 5607 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 5608 return TCL_ERROR; 5609 } 5610 5611 pRet = Tcl_NewObj(); 5612 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); 5613 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); 5614 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); 5615 Tcl_SetObjResult(interp, pRet); 5616 5617 return TCL_OK; 5618 } 5619 5620 /* 5621 ** tclcmd: test_sqlite3_log ?SCRIPT? 5622 */ 5623 static struct LogCallback { 5624 Tcl_Interp *pInterp; 5625 Tcl_Obj *pObj; 5626 } logcallback = {0, 0}; 5627 static void xLogcallback(void *unused, int err, char *zMsg){ 5628 Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); 5629 Tcl_IncrRefCount(pNew); 5630 Tcl_ListObjAppendElement( 5631 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1) 5632 ); 5633 Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); 5634 Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 5635 Tcl_DecrRefCount(pNew); 5636 } 5637 static int test_sqlite3_log( 5638 ClientData clientData, 5639 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 5640 int objc, /* Number of arguments */ 5641 Tcl_Obj *CONST objv[] /* Command arguments */ 5642 ){ 5643 if( objc>2 ){ 5644 Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); 5645 return TCL_ERROR; 5646 } 5647 if( logcallback.pObj ){ 5648 Tcl_DecrRefCount(logcallback.pObj); 5649 logcallback.pObj = 0; 5650 logcallback.pInterp = 0; 5651 sqlite3_config(SQLITE_CONFIG_LOG, 0, 0); 5652 } 5653 if( objc>1 ){ 5654 logcallback.pObj = objv[1]; 5655 Tcl_IncrRefCount(logcallback.pObj); 5656 logcallback.pInterp = interp; 5657 sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0); 5658 } 5659 return TCL_OK; 5660 } 5661 5662 /* 5663 ** tcl_objproc COMMANDNAME ARGS... 5664 ** 5665 ** Run a TCL command using its objProc interface. Throw an error if 5666 ** the command has no objProc interface. 5667 */ 5668 static int runAsObjProc( 5669 void * clientData, 5670 Tcl_Interp *interp, 5671 int objc, 5672 Tcl_Obj *CONST objv[] 5673 ){ 5674 Tcl_CmdInfo cmdInfo; 5675 if( objc<2 ){ 5676 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); 5677 return TCL_ERROR; 5678 } 5679 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 5680 Tcl_AppendResult(interp, "command not found: ", 5681 Tcl_GetString(objv[1]), (char*)0); 5682 return TCL_ERROR; 5683 } 5684 if( cmdInfo.objProc==0 ){ 5685 Tcl_AppendResult(interp, "command has no objProc: ", 5686 Tcl_GetString(objv[1]), (char*)0); 5687 return TCL_ERROR; 5688 } 5689 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); 5690 } 5691 5692 #ifndef SQLITE_OMIT_EXPLAIN 5693 /* 5694 ** WARNING: The following function, printExplainQueryPlan() is an exact 5695 ** copy of example code from eqp.in (eqp.html). If this code is modified, 5696 ** then the documentation copy needs to be modified as well. 5697 */ 5698 /* 5699 ** Argument pStmt is a prepared SQL statement. This function compiles 5700 ** an EXPLAIN QUERY PLAN command to report on the prepared statement, 5701 ** and prints the report to stdout using printf(). 5702 */ 5703 int printExplainQueryPlan(sqlite3_stmt *pStmt){ 5704 const char *zSql; /* Input SQL */ 5705 char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ 5706 sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ 5707 int rc; /* Return code from sqlite3_prepare_v2() */ 5708 5709 zSql = sqlite3_sql(pStmt); 5710 if( zSql==0 ) return SQLITE_ERROR; 5711 5712 zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); 5713 if( zExplain==0 ) return SQLITE_NOMEM; 5714 5715 rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); 5716 sqlite3_free(zExplain); 5717 if( rc!=SQLITE_OK ) return rc; 5718 5719 while( SQLITE_ROW==sqlite3_step(pExplain) ){ 5720 int iSelectid = sqlite3_column_int(pExplain, 0); 5721 int iOrder = sqlite3_column_int(pExplain, 1); 5722 int iFrom = sqlite3_column_int(pExplain, 2); 5723 const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); 5724 5725 printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); 5726 } 5727 5728 return sqlite3_finalize(pExplain); 5729 } 5730 5731 static int test_print_eqp( 5732 void * clientData, 5733 Tcl_Interp *interp, 5734 int objc, 5735 Tcl_Obj *CONST objv[] 5736 ){ 5737 int rc; 5738 sqlite3_stmt *pStmt; 5739 5740 if( objc!=2 ){ 5741 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 5742 return TCL_ERROR; 5743 } 5744 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 5745 rc = printExplainQueryPlan(pStmt); 5746 /* This is needed on Windows so that a test case using this 5747 ** function can open a read pipe and get the output of 5748 ** printExplainQueryPlan() immediately. 5749 */ 5750 fflush(stdout); 5751 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 5752 return TCL_OK; 5753 } 5754 #endif /* SQLITE_OMIT_EXPLAIN */ 5755 5756 /* 5757 ** sqlite3_test_control VERB ARGS... 5758 */ 5759 static int test_test_control( 5760 void * clientData, 5761 Tcl_Interp *interp, 5762 int objc, 5763 Tcl_Obj *CONST objv[] 5764 ){ 5765 struct Verb { 5766 const char *zName; 5767 int i; 5768 } aVerb[] = { 5769 { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, 5770 }; 5771 int iVerb; 5772 int iFlag; 5773 int rc; 5774 5775 if( objc<2 ){ 5776 Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); 5777 return TCL_ERROR; 5778 } 5779 5780 rc = Tcl_GetIndexFromObjStruct( 5781 interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb 5782 ); 5783 if( rc!=TCL_OK ) return rc; 5784 5785 iFlag = aVerb[iVerb].i; 5786 switch( iFlag ){ 5787 case SQLITE_TESTCTRL_LOCALTIME_FAULT: { 5788 int val; 5789 if( objc!=3 ){ 5790 Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); 5791 return TCL_ERROR; 5792 } 5793 if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 5794 sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); 5795 break; 5796 } 5797 } 5798 5799 Tcl_ResetResult(interp); 5800 return TCL_OK; 5801 } 5802 5803 #if SQLITE_OS_UNIX 5804 #include <sys/time.h> 5805 #include <sys/resource.h> 5806 5807 static int test_getrusage( 5808 void * clientData, 5809 Tcl_Interp *interp, 5810 int objc, 5811 Tcl_Obj *CONST objv[] 5812 ){ 5813 char buf[1024]; 5814 struct rusage r; 5815 memset(&r, 0, sizeof(r)); 5816 getrusage(RUSAGE_SELF, &r); 5817 5818 sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", 5819 (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, 5820 (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, 5821 (int)r.ru_minflt, (int)r.ru_majflt 5822 ); 5823 Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1)); 5824 return TCL_OK; 5825 } 5826 #endif 5827 5828 #if SQLITE_OS_WIN 5829 /* 5830 ** Information passed from the main thread into the windows file locker 5831 ** background thread. 5832 */ 5833 struct win32FileLocker { 5834 char *evName; /* Name of event to signal thread startup */ 5835 HANDLE h; /* Handle of the file to be locked */ 5836 int delay1; /* Delay before locking */ 5837 int delay2; /* Delay before unlocking */ 5838 int ok; /* Finished ok */ 5839 int err; /* True if an error occurs */ 5840 }; 5841 #endif 5842 5843 5844 #if SQLITE_OS_WIN 5845 #include <process.h> 5846 /* 5847 ** The background thread that does file locking. 5848 */ 5849 static void win32_file_locker(void *pAppData){ 5850 struct win32FileLocker *p = (struct win32FileLocker*)pAppData; 5851 if( p->evName ){ 5852 HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); 5853 if ( ev ){ 5854 SetEvent(ev); 5855 CloseHandle(ev); 5856 } 5857 } 5858 if( p->delay1 ) Sleep(p->delay1); 5859 if( LockFile(p->h, 0, 0, 100000000, 0) ){ 5860 Sleep(p->delay2); 5861 UnlockFile(p->h, 0, 0, 100000000, 0); 5862 p->ok = 1; 5863 }else{ 5864 p->err = 1; 5865 } 5866 CloseHandle(p->h); 5867 p->h = 0; 5868 p->delay1 = 0; 5869 p->delay2 = 0; 5870 } 5871 #endif 5872 5873 #if SQLITE_OS_WIN 5874 /* 5875 ** lock_win32_file FILENAME DELAY1 DELAY2 5876 ** 5877 ** Get an exclusive manditory lock on file for DELAY2 milliseconds. 5878 ** Wait DELAY1 milliseconds before acquiring the lock. 5879 */ 5880 static int win32_file_lock( 5881 void * clientData, 5882 Tcl_Interp *interp, 5883 int objc, 5884 Tcl_Obj *CONST objv[] 5885 ){ 5886 static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; 5887 const char *zFilename; 5888 char zBuf[200]; 5889 int retry = 0; 5890 HANDLE ev; 5891 DWORD wResult; 5892 5893 if( objc!=4 && objc!=1 ){ 5894 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); 5895 return TCL_ERROR; 5896 } 5897 if( objc==1 ){ 5898 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", 5899 x.ok, x.err, x.delay1, x.delay2, x.h); 5900 Tcl_AppendResult(interp, zBuf, (char*)0); 5901 return TCL_OK; 5902 } 5903 while( x.h && retry<30 ){ 5904 retry++; 5905 Sleep(100); 5906 } 5907 if( x.h ){ 5908 Tcl_AppendResult(interp, "busy", (char*)0); 5909 return TCL_ERROR; 5910 } 5911 if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; 5912 if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; 5913 zFilename = Tcl_GetString(objv[1]); 5914 x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, 5915 FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 5916 FILE_ATTRIBUTE_NORMAL, 0); 5917 if( !x.h ){ 5918 Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); 5919 return TCL_ERROR; 5920 } 5921 ev = CreateEvent(NULL, TRUE, FALSE, x.evName); 5922 if ( !ev ){ 5923 Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); 5924 return TCL_ERROR; 5925 } 5926 _beginthread(win32_file_locker, 0, (void*)&x); 5927 Sleep(0); 5928 if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ 5929 sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); 5930 Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); 5931 CloseHandle(ev); 5932 return TCL_ERROR; 5933 } 5934 CloseHandle(ev); 5935 return TCL_OK; 5936 } 5937 #endif 5938 5939 5940 /* 5941 ** optimization_control DB OPT BOOLEAN 5942 ** 5943 ** Enable or disable query optimizations using the sqlite3_test_control() 5944 ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. 5945 ** OPT is the name of the optimization to be disabled. 5946 */ 5947 static int optimization_control( 5948 void * clientData, 5949 Tcl_Interp *interp, 5950 int objc, 5951 Tcl_Obj *CONST objv[] 5952 ){ 5953 int i; 5954 sqlite3 *db; 5955 const char *zOpt; 5956 int onoff; 5957 int mask = 0; 5958 static const struct { 5959 const char *zOptName; 5960 int mask; 5961 } aOpt[] = { 5962 { "all", SQLITE_AllOpts }, 5963 { "none", 0 }, 5964 { "query-flattener", SQLITE_QueryFlattener }, 5965 { "column-cache", SQLITE_ColumnCache }, 5966 { "groupby-order", SQLITE_GroupByOrder }, 5967 { "factor-constants", SQLITE_FactorOutConst }, 5968 { "real-as-int", SQLITE_IdxRealAsInt }, 5969 { "distinct-opt", SQLITE_DistinctOpt }, 5970 { "cover-idx-scan", SQLITE_CoverIdxScan }, 5971 { "order-by-idx-join", SQLITE_OrderByIdxJoin }, 5972 { "transitive", SQLITE_Transitive }, 5973 { "subquery-coroutine", SQLITE_SubqCoroutine }, 5974 { "omit-noop-join", SQLITE_OmitNoopJoin }, 5975 { "stat3", SQLITE_Stat3 }, 5976 }; 5977 5978 if( objc!=4 ){ 5979 Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); 5980 return TCL_ERROR; 5981 } 5982 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 5983 if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; 5984 zOpt = Tcl_GetString(objv[2]); 5985 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 5986 if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ 5987 mask = aOpt[i].mask; 5988 break; 5989 } 5990 } 5991 if( onoff ) mask = ~mask; 5992 if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ 5993 Tcl_AppendResult(interp, "unknown optimization - should be one of:", 5994 (char*)0); 5995 for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ 5996 Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0); 5997 } 5998 return TCL_ERROR; 5999 } 6000 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); 6001 return TCL_OK; 6002 } 6003 6004 typedef struct sqlite3_api_routines sqlite3_api_routines; 6005 /* 6006 ** load_static_extension DB NAME ... 6007 ** 6008 ** Load one or more statically linked extensions. 6009 */ 6010 static int tclLoadStaticExtensionCmd( 6011 void * clientData, 6012 Tcl_Interp *interp, 6013 int objc, 6014 Tcl_Obj *CONST objv[] 6015 ){ 6016 extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); 6017 extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); 6018 extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); 6019 extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); 6020 extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); 6021 extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*); 6022 extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); 6023 extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); 6024 extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); 6025 static const struct { 6026 const char *zExtName; 6027 int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); 6028 } aExtension[] = { 6029 { "amatch", sqlite3_amatch_init }, 6030 { "closure", sqlite3_closure_init }, 6031 { "fuzzer", sqlite3_fuzzer_init }, 6032 { "ieee754", sqlite3_ieee_init }, 6033 { "nextchar", sqlite3_nextchar_init }, 6034 { "percentile", sqlite3_percentile_init }, 6035 { "regexp", sqlite3_regexp_init }, 6036 { "spellfix", sqlite3_spellfix_init }, 6037 { "wholenumber", sqlite3_wholenumber_init }, 6038 }; 6039 sqlite3 *db; 6040 const char *zName; 6041 int i, j, rc; 6042 char *zErrMsg = 0; 6043 if( objc<3 ){ 6044 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); 6045 return TCL_ERROR; 6046 } 6047 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 6048 for(j=2; j<objc; j++){ 6049 zName = Tcl_GetString(objv[j]); 6050 for(i=0; i<ArraySize(aExtension); i++){ 6051 if( strcmp(zName, aExtension[i].zExtName)==0 ) break; 6052 } 6053 if( i>=ArraySize(aExtension) ){ 6054 Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0); 6055 return TCL_ERROR; 6056 } 6057 rc = aExtension[i].pInit(db, &zErrMsg, 0); 6058 if( rc!=SQLITE_OK || zErrMsg ){ 6059 Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, 6060 (char*)0); 6061 sqlite3_free(zErrMsg); 6062 return TCL_ERROR; 6063 } 6064 } 6065 return TCL_OK; 6066 } 6067 6068 6069 /* 6070 ** Register commands with the TCL interpreter. 6071 */ 6072 int Sqlitetest1_Init(Tcl_Interp *interp){ 6073 extern int sqlite3_search_count; 6074 extern int sqlite3_found_count; 6075 extern int sqlite3_interrupt_count; 6076 extern int sqlite3_open_file_count; 6077 extern int sqlite3_sort_count; 6078 extern int sqlite3_current_time; 6079 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6080 extern int sqlite3_hostid_num; 6081 #endif 6082 extern int sqlite3_max_blobsize; 6083 extern int sqlite3BtreeSharedCacheReport(void*, 6084 Tcl_Interp*,int,Tcl_Obj*CONST*); 6085 static struct { 6086 char *zName; 6087 Tcl_CmdProc *xProc; 6088 } aCmd[] = { 6089 { "db_enter", (Tcl_CmdProc*)db_enter }, 6090 { "db_leave", (Tcl_CmdProc*)db_leave }, 6091 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, 6092 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, 6093 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, 6094 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, 6095 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, 6096 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, 6097 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, 6098 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, 6099 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, 6100 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, 6101 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, 6102 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, 6103 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, 6104 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, 6105 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, 6106 { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, 6107 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, 6108 #ifndef SQLITE_OMIT_GET_TABLE 6109 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, 6110 #endif 6111 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, 6112 { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, 6113 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, 6114 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, 6115 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, 6116 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, 6117 { "sqlite_bind", (Tcl_CmdProc*)test_bind }, 6118 { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, 6119 { "sqlite3_key", (Tcl_CmdProc*)test_key }, 6120 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, 6121 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, 6122 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, 6123 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, 6124 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, 6125 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, 6126 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, 6127 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, 6128 { "printf", (Tcl_CmdProc*)test_printf }, 6129 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, 6130 }; 6131 static struct { 6132 char *zName; 6133 Tcl_ObjCmdProc *xProc; 6134 void *clientData; 6135 } aObjCmd[] = { 6136 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, 6137 { "sqlite3_bind_int", test_bind_int, 0 }, 6138 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, 6139 { "sqlite3_bind_int64", test_bind_int64, 0 }, 6140 { "sqlite3_bind_double", test_bind_double, 0 }, 6141 { "sqlite3_bind_null", test_bind_null ,0 }, 6142 { "sqlite3_bind_text", test_bind_text ,0 }, 6143 { "sqlite3_bind_text16", test_bind_text16 ,0 }, 6144 { "sqlite3_bind_blob", test_bind_blob ,0 }, 6145 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, 6146 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, 6147 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, 6148 { "sqlite3_clear_bindings", test_clear_bindings, 0}, 6149 { "sqlite3_sleep", test_sleep, 0}, 6150 { "sqlite3_errcode", test_errcode ,0 }, 6151 { "sqlite3_extended_errcode", test_ex_errcode ,0 }, 6152 { "sqlite3_errmsg", test_errmsg ,0 }, 6153 { "sqlite3_errmsg16", test_errmsg16 ,0 }, 6154 { "sqlite3_open", test_open ,0 }, 6155 { "sqlite3_open16", test_open16 ,0 }, 6156 { "sqlite3_open_v2", test_open_v2 ,0 }, 6157 { "sqlite3_complete16", test_complete16 ,0 }, 6158 6159 { "sqlite3_prepare", test_prepare ,0 }, 6160 { "sqlite3_prepare16", test_prepare16 ,0 }, 6161 { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, 6162 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, 6163 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, 6164 { "sqlite3_finalize", test_finalize ,0 }, 6165 { "sqlite3_stmt_status", test_stmt_status ,0 }, 6166 { "sqlite3_reset", test_reset ,0 }, 6167 { "sqlite3_expired", test_expired ,0 }, 6168 { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, 6169 { "sqlite3_changes", test_changes ,0 }, 6170 { "sqlite3_step", test_step ,0 }, 6171 { "sqlite3_sql", test_sql ,0 }, 6172 { "sqlite3_next_stmt", test_next_stmt ,0 }, 6173 { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, 6174 { "sqlite3_stmt_busy", test_stmt_busy ,0 }, 6175 { "uses_stmt_journal", uses_stmt_journal ,0 }, 6176 6177 { "sqlite3_release_memory", test_release_memory, 0}, 6178 { "sqlite3_db_release_memory", test_db_release_memory, 0}, 6179 { "sqlite3_db_filename", test_db_filename, 0}, 6180 { "sqlite3_db_readonly", test_db_readonly, 0}, 6181 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, 6182 { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, 6183 { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, 6184 6185 { "sqlite3_load_extension", test_load_extension, 0}, 6186 { "sqlite3_enable_load_extension", test_enable_load, 0}, 6187 { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, 6188 { "sqlite3_limit", test_limit, 0}, 6189 6190 { "save_prng_state", save_prng_state, 0 }, 6191 { "restore_prng_state", restore_prng_state, 0 }, 6192 { "reset_prng_state", reset_prng_state, 0 }, 6193 { "optimization_control", optimization_control,0}, 6194 #if SQLITE_OS_WIN 6195 { "lock_win32_file", win32_file_lock, 0 }, 6196 #endif 6197 { "tcl_objproc", runAsObjProc, 0 }, 6198 6199 /* sqlite3_column_*() API */ 6200 { "sqlite3_column_count", test_column_count ,0 }, 6201 { "sqlite3_data_count", test_data_count ,0 }, 6202 { "sqlite3_column_type", test_column_type ,0 }, 6203 { "sqlite3_column_blob", test_column_blob ,0 }, 6204 { "sqlite3_column_double", test_column_double ,0 }, 6205 { "sqlite3_column_int64", test_column_int64 ,0 }, 6206 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, 6207 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, 6208 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, 6209 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, 6210 #ifndef SQLITE_OMIT_DECLTYPE 6211 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, 6212 #endif 6213 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6214 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, 6215 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, 6216 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, 6217 #endif 6218 6219 #ifndef SQLITE_OMIT_UTF16 6220 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, 6221 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, 6222 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, 6223 { "add_alignment_test_collations", add_alignment_test_collations, 0 }, 6224 #ifndef SQLITE_OMIT_DECLTYPE 6225 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, 6226 #endif 6227 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6228 {"sqlite3_column_database_name16", 6229 test_stmt_utf16, (void*)sqlite3_column_database_name16}, 6230 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, 6231 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, 6232 #endif 6233 #endif 6234 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, 6235 { "sqlite3_global_recover", test_global_recover, 0 }, 6236 { "working_64bit_int", working_64bit_int, 0 }, 6237 { "vfs_unlink_test", vfs_unlink_test, 0 }, 6238 { "vfs_initfail_test", vfs_initfail_test, 0 }, 6239 { "vfs_unregister_all", vfs_unregister_all, 0 }, 6240 { "vfs_reregister_all", vfs_reregister_all, 0 }, 6241 { "file_control_test", file_control_test, 0 }, 6242 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, 6243 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, 6244 { "file_control_chunksize_test", file_control_chunksize_test, 0 }, 6245 { "file_control_sizehint_test", file_control_sizehint_test, 0 }, 6246 { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, 6247 { "file_control_persist_wal", file_control_persist_wal, 0 }, 6248 { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, 6249 { "file_control_vfsname", file_control_vfsname, 0 }, 6250 { "file_control_tempfilename", file_control_tempfilename, 0 }, 6251 { "sqlite3_vfs_list", vfs_list, 0 }, 6252 { "sqlite3_create_function_v2", test_create_function_v2, 0 }, 6253 6254 /* Functions from os.h */ 6255 #ifndef SQLITE_OMIT_UTF16 6256 { "add_test_collate", test_collate, 0 }, 6257 { "add_test_collate_needed", test_collate_needed, 0 }, 6258 { "add_test_function", test_function, 0 }, 6259 #endif 6260 { "sqlite3_test_errstr", test_errstr, 0 }, 6261 { "tcl_variable_type", tcl_variable_type, 0 }, 6262 #ifndef SQLITE_OMIT_SHARED_CACHE 6263 { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, 6264 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, 6265 #endif 6266 { "sqlite3_libversion_number", test_libversion_number, 0 }, 6267 #ifdef SQLITE_ENABLE_COLUMN_METADATA 6268 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, 6269 #endif 6270 #ifndef SQLITE_OMIT_INCRBLOB 6271 { "sqlite3_blob_read", test_blob_read, 0 }, 6272 { "sqlite3_blob_write", test_blob_write, 0 }, 6273 { "sqlite3_blob_reopen", test_blob_reopen, 0 }, 6274 { "sqlite3_blob_bytes", test_blob_bytes, 0 }, 6275 { "sqlite3_blob_close", test_blob_close, 0 }, 6276 #endif 6277 { "pcache_stats", test_pcache_stats, 0 }, 6278 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 6279 { "sqlite3_unlock_notify", test_unlock_notify, 0 }, 6280 #endif 6281 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, 6282 { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, 6283 { "test_sqlite3_log", test_sqlite3_log, 0 }, 6284 #ifndef SQLITE_OMIT_EXPLAIN 6285 { "print_explain_query_plan", test_print_eqp, 0 }, 6286 #endif 6287 { "sqlite3_test_control", test_test_control }, 6288 #if SQLITE_OS_UNIX 6289 { "getrusage", test_getrusage }, 6290 #endif 6291 { "load_static_extension", tclLoadStaticExtensionCmd }, 6292 }; 6293 static int bitmask_size = sizeof(Bitmask)*8; 6294 int i; 6295 extern int sqlite3_sync_count, sqlite3_fullsync_count; 6296 extern int sqlite3_opentemp_count; 6297 extern int sqlite3_like_count; 6298 extern int sqlite3_xferopt_count; 6299 extern int sqlite3_pager_readdb_count; 6300 extern int sqlite3_pager_writedb_count; 6301 extern int sqlite3_pager_writej_count; 6302 #if SQLITE_OS_WIN 6303 extern int sqlite3_os_type; 6304 #endif 6305 #ifdef SQLITE_DEBUG 6306 extern int sqlite3WhereTrace; 6307 extern int sqlite3OSTrace; 6308 extern int sqlite3WalTrace; 6309 #endif 6310 #ifdef SQLITE_TEST 6311 #ifdef SQLITE_ENABLE_FTS3 6312 extern int sqlite3_fts3_enable_parentheses; 6313 #endif 6314 #endif 6315 6316 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 6317 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 6318 } 6319 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 6320 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 6321 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 6322 } 6323 Tcl_LinkVar(interp, "sqlite_search_count", 6324 (char*)&sqlite3_search_count, TCL_LINK_INT); 6325 Tcl_LinkVar(interp, "sqlite_found_count", 6326 (char*)&sqlite3_found_count, TCL_LINK_INT); 6327 Tcl_LinkVar(interp, "sqlite_sort_count", 6328 (char*)&sqlite3_sort_count, TCL_LINK_INT); 6329 Tcl_LinkVar(interp, "sqlite3_max_blobsize", 6330 (char*)&sqlite3_max_blobsize, TCL_LINK_INT); 6331 Tcl_LinkVar(interp, "sqlite_like_count", 6332 (char*)&sqlite3_like_count, TCL_LINK_INT); 6333 Tcl_LinkVar(interp, "sqlite_interrupt_count", 6334 (char*)&sqlite3_interrupt_count, TCL_LINK_INT); 6335 Tcl_LinkVar(interp, "sqlite_open_file_count", 6336 (char*)&sqlite3_open_file_count, TCL_LINK_INT); 6337 Tcl_LinkVar(interp, "sqlite_current_time", 6338 (char*)&sqlite3_current_time, TCL_LINK_INT); 6339 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6340 Tcl_LinkVar(interp, "sqlite_hostid_num", 6341 (char*)&sqlite3_hostid_num, TCL_LINK_INT); 6342 #endif 6343 Tcl_LinkVar(interp, "sqlite3_xferopt_count", 6344 (char*)&sqlite3_xferopt_count, TCL_LINK_INT); 6345 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", 6346 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); 6347 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", 6348 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); 6349 Tcl_LinkVar(interp, "sqlite3_pager_writej_count", 6350 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); 6351 #ifndef SQLITE_OMIT_UTF16 6352 Tcl_LinkVar(interp, "unaligned_string_counter", 6353 (char*)&unaligned_string_counter, TCL_LINK_INT); 6354 #endif 6355 #ifndef SQLITE_OMIT_UTF16 6356 Tcl_LinkVar(interp, "sqlite_last_needed_collation", 6357 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 6358 #endif 6359 #if SQLITE_OS_WIN 6360 Tcl_LinkVar(interp, "sqlite_os_type", 6361 (char*)&sqlite3_os_type, TCL_LINK_INT); 6362 #endif 6363 #ifdef SQLITE_TEST 6364 { 6365 static const char *query_plan = "*** OBSOLETE VARIABLE ***"; 6366 Tcl_LinkVar(interp, "sqlite_query_plan", 6367 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 6368 } 6369 #endif 6370 #ifdef SQLITE_DEBUG 6371 Tcl_LinkVar(interp, "sqlite_where_trace", 6372 (char*)&sqlite3WhereTrace, TCL_LINK_INT); 6373 Tcl_LinkVar(interp, "sqlite_os_trace", 6374 (char*)&sqlite3OSTrace, TCL_LINK_INT); 6375 #ifndef SQLITE_OMIT_WAL 6376 Tcl_LinkVar(interp, "sqlite_wal_trace", 6377 (char*)&sqlite3WalTrace, TCL_LINK_INT); 6378 #endif 6379 #endif 6380 #ifndef SQLITE_OMIT_DISKIO 6381 Tcl_LinkVar(interp, "sqlite_opentemp_count", 6382 (char*)&sqlite3_opentemp_count, TCL_LINK_INT); 6383 #endif 6384 Tcl_LinkVar(interp, "sqlite_static_bind_value", 6385 (char*)&sqlite_static_bind_value, TCL_LINK_STRING); 6386 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", 6387 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); 6388 Tcl_LinkVar(interp, "sqlite_temp_directory", 6389 (char*)&sqlite3_temp_directory, TCL_LINK_STRING); 6390 Tcl_LinkVar(interp, "sqlite_data_directory", 6391 (char*)&sqlite3_data_directory, TCL_LINK_STRING); 6392 Tcl_LinkVar(interp, "bitmask_size", 6393 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); 6394 Tcl_LinkVar(interp, "sqlite_sync_count", 6395 (char*)&sqlite3_sync_count, TCL_LINK_INT); 6396 Tcl_LinkVar(interp, "sqlite_fullsync_count", 6397 (char*)&sqlite3_fullsync_count, TCL_LINK_INT); 6398 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) 6399 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", 6400 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); 6401 #endif 6402 return TCL_OK; 6403 } 6404