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 ** $Id: test1.c,v 1.271 2007/08/23 02:47:53 drh Exp $ 17 */ 18 #include "sqliteInt.h" 19 #include "tcl.h" 20 #include <stdlib.h> 21 #include <string.h> 22 23 /* 24 ** This is a copy of the first part of the SqliteDb structure in 25 ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine 26 ** can extract the sqlite3* pointer from an existing Tcl SQLite 27 ** connection. 28 */ 29 struct SqliteDb { 30 sqlite3 *db; 31 }; 32 33 /* 34 ** Convert text generated by the "%p" conversion format back into 35 ** a pointer. 36 */ 37 static int testHexToInt(int h){ 38 if( h>='0' && h<='9' ){ 39 return h - '0'; 40 }else if( h>='a' && h<='f' ){ 41 return h - 'a' + 10; 42 }else{ 43 assert( h>='A' && h<='F' ); 44 return h - 'A' + 10; 45 } 46 } 47 void *sqlite3TextToPtr(const char *z){ 48 void *p; 49 u64 v; 50 u32 v2; 51 if( z[0]=='0' && z[1]=='x' ){ 52 z += 2; 53 } 54 v = 0; 55 while( *z ){ 56 v = (v<<4) + testHexToInt(*z); 57 z++; 58 } 59 if( sizeof(p)==sizeof(v) ){ 60 memcpy(&p, &v, sizeof(p)); 61 }else{ 62 assert( sizeof(p)==sizeof(v2) ); 63 v2 = (u32)v; 64 memcpy(&p, &v2, sizeof(p)); 65 } 66 return p; 67 } 68 69 70 /* 71 ** A TCL command that returns the address of the sqlite* pointer 72 ** for an sqlite connection instance. Bad things happen if the 73 ** input is not an sqlite connection. 74 */ 75 static int get_sqlite_pointer( 76 void * clientData, 77 Tcl_Interp *interp, 78 int objc, 79 Tcl_Obj *CONST objv[] 80 ){ 81 struct SqliteDb *p; 82 Tcl_CmdInfo cmdInfo; 83 char zBuf[100]; 84 if( objc!=2 ){ 85 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION"); 86 return TCL_ERROR; 87 } 88 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 89 Tcl_AppendResult(interp, "command not found: ", 90 Tcl_GetString(objv[1]), (char*)0); 91 return TCL_ERROR; 92 } 93 p = (struct SqliteDb*)cmdInfo.objClientData; 94 sprintf(zBuf, "%p", p->db); 95 if( strncmp(zBuf,"0x",2) ){ 96 sprintf(zBuf, "0x%p", p->db); 97 } 98 Tcl_AppendResult(interp, zBuf, 0); 99 return TCL_OK; 100 } 101 102 /* 103 ** Decode a pointer to an sqlite3 object. 104 */ 105 static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ 106 struct SqliteDb *p; 107 Tcl_CmdInfo cmdInfo; 108 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){ 109 p = (struct SqliteDb*)cmdInfo.objClientData; 110 *ppDb = p->db; 111 }else{ 112 *ppDb = (sqlite3*)sqlite3TextToPtr(zA); 113 } 114 return TCL_OK; 115 } 116 117 118 const char *sqlite3TestErrorName(int rc){ 119 const char *zName = 0; 120 switch( rc & 0xff ){ 121 case SQLITE_OK: zName = "SQLITE_OK"; break; 122 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; 123 case SQLITE_PERM: zName = "SQLITE_PERM"; break; 124 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; 125 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; 126 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; 127 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; 128 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; 129 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; 130 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; 131 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; 132 case SQLITE_FULL: zName = "SQLITE_FULL"; break; 133 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; 134 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; 135 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; 136 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; 137 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; 138 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; 139 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; 140 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; 141 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; 142 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; 143 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; 144 case SQLITE_ROW: zName = "SQLITE_ROW"; break; 145 case SQLITE_DONE: zName = "SQLITE_DONE"; break; 146 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; 147 default: zName = "SQLITE_Unknown"; break; 148 } 149 return zName; 150 } 151 #define t1ErrorName sqlite3TestErrorName 152 153 /* 154 ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the 155 ** fact that the sqlite3* is the first field in the Vdbe structure. 156 */ 157 #define StmtToDb(X) sqlite3_db_handle(X) 158 159 /* 160 ** Check a return value to make sure it agrees with the results 161 ** from sqlite3_errcode. 162 */ 163 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ 164 if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){ 165 char zBuf[200]; 166 int r2 = sqlite3_errcode(db); 167 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)", 168 t1ErrorName(rc), rc, t1ErrorName(r2), r2); 169 Tcl_ResetResult(interp); 170 Tcl_AppendResult(interp, zBuf, 0); 171 return 1; 172 } 173 return 0; 174 } 175 176 /* 177 ** Decode a pointer to an sqlite3_stmt object. 178 */ 179 static int getStmtPointer( 180 Tcl_Interp *interp, 181 const char *zArg, 182 sqlite3_stmt **ppStmt 183 ){ 184 *ppStmt = (sqlite3_stmt*)sqlite3TextToPtr(zArg); 185 return TCL_OK; 186 } 187 188 /* 189 ** Generate a text representation of a pointer that can be understood 190 ** by the getDbPointer and getVmPointer routines above. 191 ** 192 ** The problem is, on some machines (Solaris) if you do a printf with 193 ** "%p" you cannot turn around and do a scanf with the same "%p" and 194 ** get your pointer back. You have to prepend a "0x" before it will 195 ** work. Or at least that is what is reported to me (drh). But this 196 ** behavior varies from machine to machine. The solution used her is 197 ** to test the string right after it is generated to see if it can be 198 ** understood by scanf, and if not, try prepending an "0x" to see if 199 ** that helps. If nothing works, a fatal error is generated. 200 */ 201 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){ 202 sqlite3_snprintf(100, zPtr, "%p", p); 203 return TCL_OK; 204 } 205 206 /* 207 ** The callback routine for sqlite3_exec_printf(). 208 */ 209 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ 210 Tcl_DString *str = (Tcl_DString*)pArg; 211 int i; 212 213 if( Tcl_DStringLength(str)==0 ){ 214 for(i=0; i<argc; i++){ 215 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); 216 } 217 } 218 for(i=0; i<argc; i++){ 219 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); 220 } 221 return 0; 222 } 223 224 /* 225 ** The I/O tracing callback. 226 */ 227 static FILE *iotrace_file = 0; 228 static void io_trace_callback(const char *zFormat, ...){ 229 va_list ap; 230 va_start(ap, zFormat); 231 vfprintf(iotrace_file, zFormat, ap); 232 va_end(ap); 233 fflush(iotrace_file); 234 } 235 236 /* 237 ** Usage: io_trace FILENAME 238 ** 239 ** Turn I/O tracing on or off. If FILENAME is not an empty string, 240 ** I/O tracing begins going into FILENAME. If FILENAME is an empty 241 ** string, I/O tracing is turned off. 242 */ 243 static int test_io_trace( 244 void *NotUsed, 245 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 246 int argc, /* Number of arguments */ 247 char **argv /* Text of each argument */ 248 ){ 249 if( argc!=2 ){ 250 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 251 " FILENAME\"", 0); 252 return TCL_ERROR; 253 } 254 if( iotrace_file ){ 255 if( iotrace_file!=stdout && iotrace_file!=stderr ){ 256 fclose(iotrace_file); 257 } 258 iotrace_file = 0; 259 sqlite3_io_trace = 0; 260 } 261 if( argv[1][0] ){ 262 if( strcmp(argv[1],"stdout")==0 ){ 263 iotrace_file = stdout; 264 }else if( strcmp(argv[1],"stderr")==0 ){ 265 iotrace_file = stderr; 266 }else{ 267 iotrace_file = fopen(argv[1], "w"); 268 } 269 sqlite3_io_trace = io_trace_callback; 270 } 271 return SQLITE_OK; 272 } 273 274 275 /* 276 ** Usage: sqlite3_exec_printf DB FORMAT STRING 277 ** 278 ** Invoke the sqlite3_exec_printf() interface using the open database 279 ** DB. The SQL is the string FORMAT. The format string should contain 280 ** one %s or %q. STRING is the value inserted into %s or %q. 281 */ 282 static int test_exec_printf( 283 void *NotUsed, 284 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 285 int argc, /* Number of arguments */ 286 char **argv /* Text of each argument */ 287 ){ 288 sqlite3 *db; 289 Tcl_DString str; 290 int rc; 291 char *zErr = 0; 292 char *zSql; 293 char zBuf[30]; 294 if( argc!=4 ){ 295 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 296 " DB FORMAT STRING", 0); 297 return TCL_ERROR; 298 } 299 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 300 Tcl_DStringInit(&str); 301 zSql = sqlite3_mprintf(argv[2], argv[3]); 302 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); 303 sqlite3_free(zSql); 304 sprintf(zBuf, "%d", rc); 305 Tcl_AppendElement(interp, zBuf); 306 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); 307 Tcl_DStringFree(&str); 308 if( zErr ) sqlite3_free(zErr); 309 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 310 return TCL_OK; 311 } 312 313 /* 314 ** Usage: db_enter DB 315 ** db_leave DB 316 ** 317 ** Enter or leave the mutex on a database connection. 318 */ 319 static int db_enter( 320 void *NotUsed, 321 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 322 int argc, /* Number of arguments */ 323 char **argv /* Text of each argument */ 324 ){ 325 sqlite3 *db; 326 if( argc!=2 ){ 327 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 328 " DB", 0); 329 return TCL_ERROR; 330 } 331 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 332 sqlite3_mutex_enter(db->mutex); 333 return TCL_OK; 334 } 335 static int db_leave( 336 void *NotUsed, 337 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 338 int argc, /* Number of arguments */ 339 char **argv /* Text of each argument */ 340 ){ 341 sqlite3 *db; 342 if( argc!=2 ){ 343 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 344 " DB", 0); 345 return TCL_ERROR; 346 } 347 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 348 sqlite3_mutex_leave(db->mutex); 349 return TCL_OK; 350 } 351 352 /* 353 ** Usage: sqlite3_exec DB SQL 354 ** 355 ** Invoke the sqlite3_exec interface using the open database DB 356 */ 357 static int test_exec( 358 void *NotUsed, 359 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 360 int argc, /* Number of arguments */ 361 char **argv /* Text of each argument */ 362 ){ 363 sqlite3 *db; 364 Tcl_DString str; 365 int rc; 366 char *zErr = 0; 367 char *zSql; 368 int i, j; 369 char zBuf[30]; 370 if( argc!=3 ){ 371 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 372 " DB SQL", 0); 373 return TCL_ERROR; 374 } 375 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 376 Tcl_DStringInit(&str); 377 zSql = sqlite3_mprintf("%s", argv[2]); 378 for(i=j=0; zSql[i];){ 379 if( zSql[i]=='%' ){ 380 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]); 381 i += 3; 382 }else{ 383 zSql[j++] = zSql[i++]; 384 } 385 } 386 zSql[j] = 0; 387 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); 388 sqlite3_free(zSql); 389 sprintf(zBuf, "%d", rc); 390 Tcl_AppendElement(interp, zBuf); 391 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); 392 Tcl_DStringFree(&str); 393 if( zErr ) sqlite3_free(zErr); 394 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 395 return TCL_OK; 396 } 397 398 /* 399 ** Usage: sqlite3_exec_nr DB SQL 400 ** 401 ** Invoke the sqlite3_exec interface using the open database DB. Discard 402 ** all results 403 */ 404 static int test_exec_nr( 405 void *NotUsed, 406 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 407 int argc, /* Number of arguments */ 408 char **argv /* Text of each argument */ 409 ){ 410 sqlite3 *db; 411 int rc; 412 char *zErr = 0; 413 if( argc!=3 ){ 414 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 415 " DB SQL", 0); 416 return TCL_ERROR; 417 } 418 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 419 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); 420 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 421 return TCL_OK; 422 } 423 424 /* 425 ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ... 426 ** 427 ** Test the %z format of sqliteMPrintf(). Use multiple mprintf() calls to 428 ** concatenate arg0 through argn using separator as the separator. 429 ** Return the result. 430 */ 431 static int test_mprintf_z( 432 void *NotUsed, 433 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 434 int argc, /* Number of arguments */ 435 char **argv /* Text of each argument */ 436 ){ 437 char *zResult = 0; 438 int i; 439 440 for(i=2; i<argc; i++){ 441 zResult = sqlite3MPrintf(0, "%z%s%s", zResult, argv[1], argv[i]); 442 } 443 Tcl_AppendResult(interp, zResult, 0); 444 sqlite3_free(zResult); 445 return TCL_OK; 446 } 447 448 /* 449 ** Usage: sqlite3_mprintf_n_test STRING 450 ** 451 ** Test the %n format of sqliteMPrintf(). Return the length of the 452 ** input string. 453 */ 454 static int test_mprintf_n( 455 void *NotUsed, 456 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 457 int argc, /* Number of arguments */ 458 char **argv /* Text of each argument */ 459 ){ 460 char *zStr; 461 int n = 0; 462 zStr = sqlite3MPrintf(0, "%s%n", argv[1], &n); 463 sqlite3_free(zStr); 464 Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); 465 return TCL_OK; 466 } 467 468 /* 469 ** Usage: sqlite3_snprintf_int SIZE FORMAT INT 470 ** 471 ** Test the of sqlite3_snprintf() routine. SIZE is the size of the 472 ** output buffer in bytes. The maximum size is 100. FORMAT is the 473 ** format string. INT is a single integer argument. The FORMAT 474 ** string must require no more than this one integer argument. If 475 ** You pass in a format string that requires more than one argument, 476 ** bad things will happen. 477 */ 478 static int test_snprintf_int( 479 void *NotUsed, 480 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 481 int argc, /* Number of arguments */ 482 char **argv /* Text of each argument */ 483 ){ 484 char zStr[100]; 485 int n = atoi(argv[1]); 486 const char *zFormat = argv[2]; 487 int a1 = atoi(argv[3]); 488 if( n>sizeof(zStr) ) n = sizeof(zStr); 489 strcpy(zStr, "abcdefghijklmnopqrstuvwxyz"); 490 sqlite3_snprintf(n, zStr, zFormat, a1); 491 Tcl_AppendResult(interp, zStr, 0); 492 return TCL_OK; 493 } 494 495 /* 496 ** Usage: sqlite3_get_table_printf DB FORMAT STRING 497 ** 498 ** Invoke the sqlite3_get_table_printf() interface using the open database 499 ** DB. The SQL is the string FORMAT. The format string should contain 500 ** one %s or %q. STRING is the value inserted into %s or %q. 501 */ 502 static int test_get_table_printf( 503 void *NotUsed, 504 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 505 int argc, /* Number of arguments */ 506 char **argv /* Text of each argument */ 507 ){ 508 sqlite3 *db; 509 Tcl_DString str; 510 int rc; 511 char *zErr = 0; 512 int nRow, nCol; 513 char **aResult; 514 int i; 515 char zBuf[30]; 516 char *zSql; 517 if( argc!=4 ){ 518 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 519 " DB FORMAT STRING", 0); 520 return TCL_ERROR; 521 } 522 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 523 Tcl_DStringInit(&str); 524 zSql = sqlite3_mprintf(argv[2],argv[3]); 525 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr); 526 sqlite3_free(zSql); 527 sprintf(zBuf, "%d", rc); 528 Tcl_AppendElement(interp, zBuf); 529 if( rc==SQLITE_OK ){ 530 sprintf(zBuf, "%d", nRow); 531 Tcl_AppendElement(interp, zBuf); 532 sprintf(zBuf, "%d", nCol); 533 Tcl_AppendElement(interp, zBuf); 534 for(i=0; i<(nRow+1)*nCol; i++){ 535 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); 536 } 537 }else{ 538 Tcl_AppendElement(interp, zErr); 539 } 540 sqlite3_free_table(aResult); 541 if( zErr ) sqlite3_free(zErr); 542 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 543 return TCL_OK; 544 } 545 546 547 /* 548 ** Usage: sqlite3_last_insert_rowid DB 549 ** 550 ** Returns the integer ROWID of the most recent insert. 551 */ 552 static int test_last_rowid( 553 void *NotUsed, 554 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 555 int argc, /* Number of arguments */ 556 char **argv /* Text of each argument */ 557 ){ 558 sqlite3 *db; 559 char zBuf[30]; 560 561 if( argc!=2 ){ 562 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); 563 return TCL_ERROR; 564 } 565 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 566 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db)); 567 Tcl_AppendResult(interp, zBuf, 0); 568 return SQLITE_OK; 569 } 570 571 /* 572 ** Usage: sqlite3_key DB KEY 573 ** 574 ** Set the codec key. 575 */ 576 static int test_key( 577 void *NotUsed, 578 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 579 int argc, /* Number of arguments */ 580 char **argv /* Text of each argument */ 581 ){ 582 sqlite3 *db; 583 const char *zKey; 584 int nKey; 585 if( argc!=3 ){ 586 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 587 " FILENAME\"", 0); 588 return TCL_ERROR; 589 } 590 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 591 zKey = argv[2]; 592 nKey = strlen(zKey); 593 #ifdef SQLITE_HAS_CODEC 594 sqlite3_key(db, zKey, nKey); 595 #endif 596 return TCL_OK; 597 } 598 599 /* 600 ** Usage: sqlite3_rekey DB KEY 601 ** 602 ** Change the codec key. 603 */ 604 static int test_rekey( 605 void *NotUsed, 606 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 607 int argc, /* Number of arguments */ 608 char **argv /* Text of each argument */ 609 ){ 610 sqlite3 *db; 611 const char *zKey; 612 int nKey; 613 if( argc!=3 ){ 614 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 615 " FILENAME\"", 0); 616 return TCL_ERROR; 617 } 618 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 619 zKey = argv[2]; 620 nKey = strlen(zKey); 621 #ifdef SQLITE_HAS_CODEC 622 sqlite3_rekey(db, zKey, nKey); 623 #endif 624 return TCL_OK; 625 } 626 627 /* 628 ** Usage: sqlite3_close DB 629 ** 630 ** Closes the database opened by sqlite3_open. 631 */ 632 static int sqlite_test_close( 633 void *NotUsed, 634 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 635 int argc, /* Number of arguments */ 636 char **argv /* Text of each argument */ 637 ){ 638 sqlite3 *db; 639 int rc; 640 if( argc!=2 ){ 641 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 642 " FILENAME\"", 0); 643 return TCL_ERROR; 644 } 645 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 646 rc = sqlite3_close(db); 647 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 648 return TCL_OK; 649 } 650 651 /* 652 ** Implementation of the x_coalesce() function. 653 ** Return the first argument non-NULL argument. 654 */ 655 static void t1_ifnullFunc( 656 sqlite3_context *context, 657 int argc, 658 sqlite3_value **argv 659 ){ 660 int i; 661 for(i=0; i<argc; i++){ 662 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 663 int n = sqlite3_value_bytes(argv[i]); 664 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]), 665 n, SQLITE_TRANSIENT); 666 break; 667 } 668 } 669 } 670 671 /* 672 ** These are test functions. hex8() interprets its argument as 673 ** UTF8 and returns a hex encoding. hex16le() interprets its argument 674 ** as UTF16le and returns a hex encoding. 675 */ 676 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ 677 const unsigned char *z; 678 int i; 679 char zBuf[200]; 680 z = sqlite3_value_text(argv[0]); 681 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ 682 sprintf(&zBuf[i*2], "%02x", z[i]&0xff); 683 } 684 zBuf[i*2] = 0; 685 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); 686 } 687 #ifndef SQLITE_OMIT_UTF16 688 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ 689 const unsigned short int *z; 690 int i; 691 char zBuf[400]; 692 z = sqlite3_value_text16(argv[0]); 693 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ 694 sprintf(&zBuf[i*4], "%04x", z[i]&0xff); 695 } 696 zBuf[i*4] = 0; 697 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); 698 } 699 #endif 700 701 /* 702 ** A structure into which to accumulate text. 703 */ 704 struct dstr { 705 int nAlloc; /* Space allocated */ 706 int nUsed; /* Space used */ 707 char *z; /* The space */ 708 }; 709 710 /* 711 ** Append text to a dstr 712 */ 713 static void dstrAppend(struct dstr *p, const char *z, int divider){ 714 int n = strlen(z); 715 if( p->nUsed + n + 2 > p->nAlloc ){ 716 char *zNew; 717 p->nAlloc = p->nAlloc*2 + n + 200; 718 zNew = sqlite3_realloc(p->z, p->nAlloc); 719 if( zNew==0 ){ 720 sqlite3_free(p->z); 721 memset(p, 0, sizeof(*p)); 722 return; 723 } 724 p->z = zNew; 725 } 726 if( divider && p->nUsed>0 ){ 727 p->z[p->nUsed++] = divider; 728 } 729 memcpy(&p->z[p->nUsed], z, n+1); 730 p->nUsed += n; 731 } 732 733 /* 734 ** Invoked for each callback from sqlite3ExecFunc 735 */ 736 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ 737 struct dstr *p = (struct dstr*)pData; 738 int i; 739 for(i=0; i<argc; i++){ 740 if( argv[i]==0 ){ 741 dstrAppend(p, "NULL", ' '); 742 }else{ 743 dstrAppend(p, argv[i], ' '); 744 } 745 } 746 return 0; 747 } 748 749 /* 750 ** Implementation of the x_sqlite_exec() function. This function takes 751 ** a single argument and attempts to execute that argument as SQL code. 752 ** This is illegal and should set the SQLITE_MISUSE flag on the database. 753 ** 754 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec() 755 ** from within a function call. 756 ** 757 ** This routine simulates the effect of having two threads attempt to 758 ** use the same database at the same time. 759 */ 760 static void sqlite3ExecFunc( 761 sqlite3_context *context, 762 int argc, 763 sqlite3_value **argv 764 ){ 765 struct dstr x; 766 memset(&x, 0, sizeof(x)); 767 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), 768 (char*)sqlite3_value_text(argv[0]), 769 execFuncCallback, &x, 0); 770 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); 771 sqlite3_free(x.z); 772 } 773 774 /* 775 ** Implementation of tkt2213func(), a scalar function that takes exactly 776 ** one argument. It has two interesting features: 777 ** 778 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*. 779 ** If the three pointers returned are not the same an SQL error is raised. 780 ** 781 ** * Otherwise it returns a copy of the text representation of it's 782 ** argument in such a way as the VDBE representation is a Mem* cell 783 ** with the MEM_Term flag clear. 784 ** 785 ** Ticket #2213 can therefore be tested by evaluating the following 786 ** SQL expression: 787 ** 788 ** tkt2213func(tkt2213func('a string')); 789 */ 790 static void tkt2213Function( 791 sqlite3_context *context, 792 int argc, 793 sqlite3_value **argv 794 ){ 795 int nText; 796 unsigned char const *zText1; 797 unsigned char const *zText2; 798 unsigned char const *zText3; 799 800 nText = sqlite3_value_bytes(argv[0]); 801 zText1 = sqlite3_value_text(argv[0]); 802 zText2 = sqlite3_value_text(argv[0]); 803 zText3 = sqlite3_value_text(argv[0]); 804 805 if( zText1!=zText2 || zText2!=zText3 ){ 806 sqlite3_result_error(context, "tkt2213 is not fixed", -1); 807 }else{ 808 char *zCopy = (char *)sqlite3_malloc(nText); 809 memcpy(zCopy, zText1, nText); 810 sqlite3_result_text(context, zCopy, nText, sqlite3_free); 811 } 812 } 813 814 /* 815 ** The following SQL function takes 4 arguments. The 2nd and 816 ** 4th argument must be one of these strings: 'text', 'text16', 817 ** or 'blob' corresponding to API functions 818 ** 819 ** sqlite3_value_text() 820 ** sqlite3_value_text16() 821 ** sqlite3_value_blob() 822 ** 823 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop', 824 ** corresponding to APIs: 825 ** 826 ** sqlite3_value_bytes() 827 ** sqlite3_value_bytes16() 828 ** noop 829 ** 830 ** The APIs designated by the 2nd through 4th arguments are applied 831 ** to the first argument in order. If the pointers returned by the 832 ** second and fourth are different, this routine returns 1. Otherwise, 833 ** this routine returns 0. 834 ** 835 ** This function is used to test to see when returned pointers from 836 ** the _text(), _text16() and _blob() APIs become invalidated. 837 */ 838 static void ptrChngFunction( 839 sqlite3_context *context, 840 int argc, 841 sqlite3_value **argv 842 ){ 843 const void *p1, *p2; 844 const char *zCmd; 845 if( argc!=4 ) return; 846 zCmd = (const char*)sqlite3_value_text(argv[1]); 847 if( zCmd==0 ) return; 848 if( strcmp(zCmd,"text")==0 ){ 849 p1 = (const void*)sqlite3_value_text(argv[0]); 850 #ifndef SQLITE_OMIT_UTF16 851 }else if( strcmp(zCmd, "text16")==0 ){ 852 p1 = (const void*)sqlite3_value_text16(argv[0]); 853 #endif 854 }else if( strcmp(zCmd, "blob")==0 ){ 855 p1 = (const void*)sqlite3_value_blob(argv[0]); 856 }else{ 857 return; 858 } 859 zCmd = (const char*)sqlite3_value_text(argv[2]); 860 if( zCmd==0 ) return; 861 if( strcmp(zCmd,"bytes")==0 ){ 862 sqlite3_value_bytes(argv[0]); 863 #ifndef SQLITE_OMIT_UTF16 864 }else if( strcmp(zCmd, "bytes16")==0 ){ 865 sqlite3_value_bytes16(argv[0]); 866 #endif 867 }else if( strcmp(zCmd, "noop")==0 ){ 868 /* do nothing */ 869 }else{ 870 return; 871 } 872 zCmd = (const char*)sqlite3_value_text(argv[3]); 873 if( zCmd==0 ) return; 874 if( strcmp(zCmd,"text")==0 ){ 875 p2 = (const void*)sqlite3_value_text(argv[0]); 876 #ifndef SQLITE_OMIT_UTF16 877 }else if( strcmp(zCmd, "text16")==0 ){ 878 p2 = (const void*)sqlite3_value_text16(argv[0]); 879 #endif 880 }else if( strcmp(zCmd, "blob")==0 ){ 881 p2 = (const void*)sqlite3_value_blob(argv[0]); 882 }else{ 883 return; 884 } 885 sqlite3_result_int(context, p1!=p2); 886 } 887 888 889 /* 890 ** Usage: sqlite_test_create_function DB 891 ** 892 ** Call the sqlite3_create_function API on the given database in order 893 ** to create a function named "x_coalesce". This function does the same thing 894 ** as the "coalesce" function. This function also registers an SQL function 895 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec() 896 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error. 897 ** The effect is similar to trying to use the same database connection from 898 ** two threads at the same time. 899 ** 900 ** The original motivation for this routine was to be able to call the 901 ** sqlite3_create_function function while a query is in progress in order 902 ** to test the SQLITE_MISUSE detection logic. 903 */ 904 static int test_create_function( 905 void *NotUsed, 906 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 907 int argc, /* Number of arguments */ 908 char **argv /* Text of each argument */ 909 ){ 910 int rc; 911 sqlite3 *db; 912 extern void Md5_Register(sqlite3*); 913 914 if( argc!=2 ){ 915 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 916 " DB\"", 0); 917 return TCL_ERROR; 918 } 919 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 920 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, 921 t1_ifnullFunc, 0, 0); 922 if( rc==SQLITE_OK ){ 923 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, 924 hex8Func, 0, 0); 925 } 926 #ifndef SQLITE_OMIT_UTF16 927 if( rc==SQLITE_OK ){ 928 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, 929 hex16Func, 0, 0); 930 } 931 #endif 932 if( rc==SQLITE_OK ){ 933 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, 934 tkt2213Function, 0, 0); 935 } 936 if( rc==SQLITE_OK ){ 937 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, 938 ptrChngFunction, 0, 0); 939 } 940 941 #ifndef SQLITE_OMIT_UTF16 942 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also 943 ** because it is not tested anywhere else. */ 944 if( rc==SQLITE_OK ){ 945 sqlite3_value *pVal; 946 sqlite3_mutex_enter(db->mutex); 947 pVal = sqlite3ValueNew(db); 948 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); 949 if( db->mallocFailed ){ 950 rc = SQLITE_NOMEM; 951 }else{ 952 rc = sqlite3_create_function16(db, 953 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), 954 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); 955 } 956 sqlite3ValueFree(pVal); 957 sqlite3_mutex_leave(db->mutex); 958 } 959 #endif 960 961 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 962 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 963 return TCL_OK; 964 } 965 966 /* 967 ** Routines to implement the x_count() aggregate function. 968 ** 969 ** x_count() counts the number of non-null arguments. But there are 970 ** some twists for testing purposes. 971 ** 972 ** If the argument to x_count() is 40 then a UTF-8 error is reported 973 ** on the step function. If x_count(41) is seen, then a UTF-16 error 974 ** is reported on the step function. If the total count is 42, then 975 ** a UTF-8 error is reported on the finalize function. 976 */ 977 typedef struct t1CountCtx t1CountCtx; 978 struct t1CountCtx { 979 int n; 980 }; 981 static void t1CountStep( 982 sqlite3_context *context, 983 int argc, 984 sqlite3_value **argv 985 ){ 986 t1CountCtx *p; 987 p = sqlite3_aggregate_context(context, sizeof(*p)); 988 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ 989 p->n++; 990 } 991 if( argc>0 ){ 992 int v = sqlite3_value_int(argv[0]); 993 if( v==40 ){ 994 sqlite3_result_error(context, "value of 40 handed to x_count", -1); 995 #ifndef SQLITE_OMIT_UTF16 996 }else if( v==41 ){ 997 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; 998 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1); 999 #endif 1000 } 1001 } 1002 } 1003 static void t1CountFinalize(sqlite3_context *context){ 1004 t1CountCtx *p; 1005 p = sqlite3_aggregate_context(context, sizeof(*p)); 1006 if( p ){ 1007 if( p->n==42 ){ 1008 sqlite3_result_error(context, "x_count totals to 42", -1); 1009 }else{ 1010 sqlite3_result_int(context, p ? p->n : 0); 1011 } 1012 } 1013 } 1014 1015 /* 1016 ** Usage: sqlite_test_create_aggregate DB 1017 ** 1018 ** Call the sqlite3_create_function API on the given database in order 1019 ** to create a function named "x_count". This function does the same thing 1020 ** as the "md5sum" function. 1021 ** 1022 ** The original motivation for this routine was to be able to call the 1023 ** sqlite3_create_aggregate function while a query is in progress in order 1024 ** to test the SQLITE_MISUSE detection logic. See misuse.test. 1025 ** 1026 ** This routine was later extended to test the use of sqlite3_result_error() 1027 ** within aggregate functions. 1028 */ 1029 static int test_create_aggregate( 1030 void *NotUsed, 1031 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1032 int argc, /* Number of arguments */ 1033 char **argv /* Text of each argument */ 1034 ){ 1035 sqlite3 *db; 1036 int rc; 1037 if( argc!=2 ){ 1038 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1039 " FILENAME\"", 0); 1040 return TCL_ERROR; 1041 } 1042 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 1043 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, 1044 t1CountStep,t1CountFinalize); 1045 if( rc==SQLITE_OK ){ 1046 sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, 1047 t1CountStep,t1CountFinalize); 1048 } 1049 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 1050 return TCL_OK; 1051 } 1052 1053 1054 /* 1055 ** Usage: printf TEXT 1056 ** 1057 ** Send output to printf. Use this rather than puts to merge the output 1058 ** in the correct sequence with debugging printfs inserted into C code. 1059 ** Puts uses a separate buffer and debugging statements will be out of 1060 ** sequence if it is used. 1061 */ 1062 static int test_printf( 1063 void *NotUsed, 1064 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1065 int argc, /* Number of arguments */ 1066 char **argv /* Text of each argument */ 1067 ){ 1068 if( argc!=2 ){ 1069 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1070 " TEXT\"", 0); 1071 return TCL_ERROR; 1072 } 1073 printf("%s\n", argv[1]); 1074 return TCL_OK; 1075 } 1076 1077 1078 1079 /* 1080 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER 1081 ** 1082 ** Call mprintf with three integer arguments 1083 */ 1084 static int sqlite3_mprintf_int( 1085 void *NotUsed, 1086 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1087 int argc, /* Number of arguments */ 1088 char **argv /* Text of each argument */ 1089 ){ 1090 int a[3], i; 1091 char *z; 1092 if( argc!=5 ){ 1093 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1094 " FORMAT INT INT INT\"", 0); 1095 return TCL_ERROR; 1096 } 1097 for(i=2; i<5; i++){ 1098 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; 1099 } 1100 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); 1101 Tcl_AppendResult(interp, z, 0); 1102 sqlite3_free(z); 1103 return TCL_OK; 1104 } 1105 1106 /* 1107 ** If zNum represents an integer that will fit in 64-bits, then set 1108 ** *pValue to that integer and return true. Otherwise return false. 1109 */ 1110 static int sqlite3GetInt64(const char *zNum, i64 *pValue){ 1111 if( sqlite3FitsIn64Bits(zNum) ){ 1112 sqlite3Atoi64(zNum, pValue); 1113 return 1; 1114 } 1115 return 0; 1116 } 1117 1118 /* 1119 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER 1120 ** 1121 ** Call mprintf with three 64-bit integer arguments 1122 */ 1123 static int sqlite3_mprintf_int64( 1124 void *NotUsed, 1125 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1126 int argc, /* Number of arguments */ 1127 char **argv /* Text of each argument */ 1128 ){ 1129 int i; 1130 sqlite_int64 a[3]; 1131 char *z; 1132 if( argc!=5 ){ 1133 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1134 " FORMAT INT INT INT\"", 0); 1135 return TCL_ERROR; 1136 } 1137 for(i=2; i<5; i++){ 1138 if( !sqlite3GetInt64(argv[i], &a[i-2]) ){ 1139 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); 1140 return TCL_ERROR; 1141 } 1142 } 1143 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); 1144 Tcl_AppendResult(interp, z, 0); 1145 sqlite3_free(z); 1146 return TCL_OK; 1147 } 1148 1149 /* 1150 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING 1151 ** 1152 ** Call mprintf with two integer arguments and one string argument 1153 */ 1154 static int sqlite3_mprintf_str( 1155 void *NotUsed, 1156 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1157 int argc, /* Number of arguments */ 1158 char **argv /* Text of each argument */ 1159 ){ 1160 int a[3], i; 1161 char *z; 1162 if( argc<4 || argc>5 ){ 1163 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1164 " FORMAT INT INT ?STRING?\"", 0); 1165 return TCL_ERROR; 1166 } 1167 for(i=2; i<4; i++){ 1168 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; 1169 } 1170 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL); 1171 Tcl_AppendResult(interp, z, 0); 1172 sqlite3_free(z); 1173 return TCL_OK; 1174 } 1175 1176 /* 1177 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING 1178 ** 1179 ** Call mprintf with two integer arguments and one string argument 1180 */ 1181 static int sqlite3_snprintf_str( 1182 void *NotUsed, 1183 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1184 int argc, /* Number of arguments */ 1185 char **argv /* Text of each argument */ 1186 ){ 1187 int a[3], i; 1188 int n; 1189 char *z; 1190 if( argc<5 || argc>6 ){ 1191 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1192 " INT FORMAT INT INT ?STRING?\"", 0); 1193 return TCL_ERROR; 1194 } 1195 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; 1196 if( n<0 ){ 1197 Tcl_AppendResult(interp, "N must be non-negative", 0); 1198 return TCL_ERROR; 1199 } 1200 for(i=3; i<5; i++){ 1201 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR; 1202 } 1203 z = sqlite3_malloc( n+1 ); 1204 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); 1205 Tcl_AppendResult(interp, z, 0); 1206 sqlite3_free(z); 1207 return TCL_OK; 1208 } 1209 1210 /* 1211 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE 1212 ** 1213 ** Call mprintf with two integer arguments and one double argument 1214 */ 1215 static int sqlite3_mprintf_double( 1216 void *NotUsed, 1217 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1218 int argc, /* Number of arguments */ 1219 char **argv /* Text of each argument */ 1220 ){ 1221 int a[3], i; 1222 double r; 1223 char *z; 1224 if( argc!=5 ){ 1225 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1226 " FORMAT INT INT DOUBLE\"", 0); 1227 return TCL_ERROR; 1228 } 1229 for(i=2; i<4; i++){ 1230 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; 1231 } 1232 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; 1233 z = sqlite3_mprintf(argv[1], a[0], a[1], r); 1234 Tcl_AppendResult(interp, z, 0); 1235 sqlite3_free(z); 1236 return TCL_OK; 1237 } 1238 1239 /* 1240 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE 1241 ** 1242 ** Call mprintf with a single double argument which is the product of the 1243 ** two arguments given above. This is used to generate overflow and underflow 1244 ** doubles to test that they are converted properly. 1245 */ 1246 static int sqlite3_mprintf_scaled( 1247 void *NotUsed, 1248 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1249 int argc, /* Number of arguments */ 1250 char **argv /* Text of each argument */ 1251 ){ 1252 int i; 1253 double r[2]; 1254 char *z; 1255 if( argc!=4 ){ 1256 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1257 " FORMAT DOUBLE DOUBLE\"", 0); 1258 return TCL_ERROR; 1259 } 1260 for(i=2; i<4; i++){ 1261 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; 1262 } 1263 z = sqlite3_mprintf(argv[1], r[0]*r[1]); 1264 Tcl_AppendResult(interp, z, 0); 1265 sqlite3_free(z); 1266 return TCL_OK; 1267 } 1268 1269 /* 1270 ** Usage: sqlite3_mprintf_stronly FORMAT STRING 1271 ** 1272 ** Call mprintf with a single double argument which is the product of the 1273 ** two arguments given above. This is used to generate overflow and underflow 1274 ** doubles to test that they are converted properly. 1275 */ 1276 static int sqlite3_mprintf_stronly( 1277 void *NotUsed, 1278 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1279 int argc, /* Number of arguments */ 1280 char **argv /* Text of each argument */ 1281 ){ 1282 char *z; 1283 if( argc!=3 ){ 1284 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1285 " FORMAT STRING\"", 0); 1286 return TCL_ERROR; 1287 } 1288 z = sqlite3_mprintf(argv[1], argv[2]); 1289 Tcl_AppendResult(interp, z, 0); 1290 sqlite3_free(z); 1291 return TCL_OK; 1292 } 1293 1294 /* 1295 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX 1296 ** 1297 ** Call mprintf with a single double argument which is derived from the 1298 ** hexadecimal encoding of an IEEE double. 1299 */ 1300 static int sqlite3_mprintf_hexdouble( 1301 void *NotUsed, 1302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1303 int argc, /* Number of arguments */ 1304 char **argv /* Text of each argument */ 1305 ){ 1306 char *z; 1307 double r; 1308 unsigned x1, x2; 1309 long long unsigned d; 1310 if( argc!=3 ){ 1311 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1312 " FORMAT STRING\"", 0); 1313 return TCL_ERROR; 1314 } 1315 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ 1316 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0); 1317 return TCL_ERROR; 1318 } 1319 d = x2; 1320 d = (d<<32) + x1; 1321 memcpy(&r, &d, sizeof(r)); 1322 z = sqlite3_mprintf(argv[1], r); 1323 Tcl_AppendResult(interp, z, 0); 1324 sqlite3_free(z); 1325 return TCL_OK; 1326 } 1327 1328 /* 1329 ** Usage: sqlite3_enable_shared_cache BOOLEAN 1330 ** 1331 */ 1332 #if !defined(SQLITE_OMIT_SHARED_CACHE) 1333 static int test_enable_shared( 1334 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1335 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1336 int objc, /* Number of arguments */ 1337 Tcl_Obj *CONST objv[] /* Command arguments */ 1338 ){ 1339 int rc; 1340 int enable; 1341 int ret = 0; 1342 extern int sqlite3SharedCacheEnabled; 1343 1344 if( objc!=2 ){ 1345 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); 1346 return TCL_ERROR; 1347 } 1348 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){ 1349 return TCL_ERROR; 1350 } 1351 ret = sqlite3SharedCacheEnabled; 1352 rc = sqlite3_enable_shared_cache(enable); 1353 if( rc!=SQLITE_OK ){ 1354 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); 1355 return TCL_ERROR; 1356 } 1357 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret)); 1358 return TCL_OK; 1359 } 1360 #endif 1361 1362 1363 1364 /* 1365 ** Usage: sqlite3_extended_result_codes DB BOOLEAN 1366 ** 1367 */ 1368 static int test_extended_result_codes( 1369 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1370 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1371 int objc, /* Number of arguments */ 1372 Tcl_Obj *CONST objv[] /* Command arguments */ 1373 ){ 1374 int enable; 1375 sqlite3 *db; 1376 1377 if( objc!=3 ){ 1378 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); 1379 return TCL_ERROR; 1380 } 1381 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1382 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR; 1383 sqlite3_extended_result_codes(db, enable); 1384 return TCL_OK; 1385 } 1386 1387 /* 1388 ** Usage: sqlite3_libversion_number 1389 ** 1390 */ 1391 static int test_libversion_number( 1392 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1393 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1394 int objc, /* Number of arguments */ 1395 Tcl_Obj *CONST objv[] /* Command arguments */ 1396 ){ 1397 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number())); 1398 return TCL_OK; 1399 } 1400 1401 /* 1402 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname 1403 ** 1404 */ 1405 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1406 static int test_table_column_metadata( 1407 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 1408 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1409 int objc, /* Number of arguments */ 1410 Tcl_Obj *CONST objv[] /* Command arguments */ 1411 ){ 1412 sqlite3 *db; 1413 const char *zDb; 1414 const char *zTbl; 1415 const char *zCol; 1416 int rc; 1417 Tcl_Obj *pRet; 1418 1419 const char *zDatatype; 1420 const char *zCollseq; 1421 int notnull; 1422 int primarykey; 1423 int autoincrement; 1424 1425 if( objc!=5 ){ 1426 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname"); 1427 return TCL_ERROR; 1428 } 1429 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1430 zDb = Tcl_GetString(objv[2]); 1431 zTbl = Tcl_GetString(objv[3]); 1432 zCol = Tcl_GetString(objv[4]); 1433 1434 if( strlen(zDb)==0 ) zDb = 0; 1435 1436 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, 1437 &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement); 1438 1439 if( rc!=SQLITE_OK ){ 1440 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0); 1441 return TCL_ERROR; 1442 } 1443 1444 pRet = Tcl_NewObj(); 1445 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1)); 1446 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1)); 1447 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull)); 1448 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey)); 1449 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement)); 1450 Tcl_SetObjResult(interp, pRet); 1451 1452 return TCL_OK; 1453 } 1454 #endif 1455 1456 #ifndef SQLITE_OMIT_INCRBLOB 1457 1458 /* 1459 ** sqlite3_blob_read CHANNEL OFFSET N 1460 ** 1461 ** This command is used to test the sqlite3_blob_read() in ways that 1462 ** the Tcl channel interface does not. The first argument should 1463 ** be the name of a valid channel created by the [incrblob] method 1464 ** of a database handle. This function calls sqlite3_blob_read() 1465 ** to read N bytes from offset OFFSET from the underlying SQLite 1466 ** blob handle. 1467 ** 1468 ** On success, a byte-array object containing the read data is 1469 ** returned. On failure, the interpreter result is set to the 1470 ** text representation of the returned error code (i.e. "SQLITE_NOMEM") 1471 ** and a Tcl exception is thrown. 1472 */ 1473 static int test_blob_read( 1474 ClientData clientData, /* Not used */ 1475 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1476 int objc, /* Number of arguments */ 1477 Tcl_Obj *CONST objv[] /* Command arguments */ 1478 ){ 1479 Tcl_Channel channel; 1480 ClientData instanceData; 1481 sqlite3_blob *pBlob; 1482 int notUsed; 1483 int nByte; 1484 int iOffset; 1485 unsigned char *zBuf; 1486 int rc; 1487 1488 if( objc!=4 ){ 1489 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N"); 1490 return TCL_ERROR; 1491 } 1492 1493 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), ¬Used); 1494 if( !channel 1495 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) 1496 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte) 1497 || nByte<0 || iOffset<0 1498 ){ 1499 return TCL_ERROR; 1500 } 1501 1502 instanceData = Tcl_GetChannelInstanceData(channel); 1503 pBlob = *((sqlite3_blob **)instanceData); 1504 1505 zBuf = (unsigned char *)Tcl_Alloc(nByte); 1506 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset); 1507 if( rc==SQLITE_OK ){ 1508 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte)); 1509 }else{ 1510 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); 1511 } 1512 Tcl_Free((char *)zBuf); 1513 1514 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); 1515 } 1516 1517 /* 1518 ** sqlite3_blob_write CHANNEL OFFSET DATA 1519 ** 1520 ** This command is used to test the sqlite3_blob_write() in ways that 1521 ** the Tcl channel interface does not. The first argument should 1522 ** be the name of a valid channel created by the [incrblob] method 1523 ** of a database handle. This function calls sqlite3_blob_write() 1524 ** to write the DATA byte-array to the underlying SQLite blob handle. 1525 ** at offset OFFSET. 1526 ** 1527 ** On success, an empty string is returned. On failure, the interpreter 1528 ** result is set to the text representation of the returned error code 1529 ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown. 1530 */ 1531 static int test_blob_write( 1532 ClientData clientData, /* Not used */ 1533 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1534 int objc, /* Number of arguments */ 1535 Tcl_Obj *CONST objv[] /* Command arguments */ 1536 ){ 1537 Tcl_Channel channel; 1538 ClientData instanceData; 1539 sqlite3_blob *pBlob; 1540 int notUsed; 1541 int iOffset; 1542 int rc; 1543 1544 unsigned char *zBuf; 1545 int nBuf; 1546 1547 if( objc!=4 ){ 1548 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA"); 1549 return TCL_ERROR; 1550 } 1551 1552 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), ¬Used); 1553 if( !channel 1554 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) 1555 || iOffset<0 1556 ){ 1557 return TCL_ERROR; 1558 } 1559 1560 instanceData = Tcl_GetChannelInstanceData(channel); 1561 pBlob = *((sqlite3_blob **)instanceData); 1562 1563 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf); 1564 rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset); 1565 if( rc!=SQLITE_OK ){ 1566 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); 1567 } 1568 1569 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); 1570 } 1571 #endif 1572 1573 /* 1574 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC 1575 ** 1576 ** This Tcl proc is used for testing the experimental 1577 ** sqlite3_create_collation_v2() interface. 1578 */ 1579 struct TestCollationX { 1580 Tcl_Interp *interp; 1581 Tcl_Obj *pCmp; 1582 Tcl_Obj *pDel; 1583 }; 1584 typedef struct TestCollationX TestCollationX; 1585 static void testCreateCollationDel(void *pCtx){ 1586 TestCollationX *p = (TestCollationX *)pCtx; 1587 1588 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL); 1589 if( rc!=TCL_OK ){ 1590 Tcl_BackgroundError(p->interp); 1591 } 1592 1593 Tcl_DecrRefCount(p->pCmp); 1594 Tcl_DecrRefCount(p->pDel); 1595 sqlite3_free((void *)p); 1596 } 1597 static int testCreateCollationCmp( 1598 void *pCtx, 1599 int nLeft, 1600 const void *zLeft, 1601 int nRight, 1602 const void *zRight 1603 ){ 1604 TestCollationX *p = (TestCollationX *)pCtx; 1605 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp); 1606 int iRes = 0; 1607 1608 Tcl_IncrRefCount(pScript); 1609 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft)); 1610 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight)); 1611 1612 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL) 1613 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes) 1614 ){ 1615 Tcl_BackgroundError(p->interp); 1616 } 1617 Tcl_DecrRefCount(pScript); 1618 1619 return iRes; 1620 } 1621 static int test_create_collation_v2( 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 TestCollationX *p; 1628 sqlite3 *db; 1629 1630 if( objc!=5 ){ 1631 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC"); 1632 return TCL_ERROR; 1633 } 1634 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1635 1636 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX)); 1637 p->pCmp = objv[3]; 1638 p->pDel = objv[4]; 1639 p->interp = interp; 1640 Tcl_IncrRefCount(p->pCmp); 1641 Tcl_IncrRefCount(p->pDel); 1642 1643 sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, 1644 (void *)p, testCreateCollationCmp, testCreateCollationDel 1645 ); 1646 return TCL_OK; 1647 } 1648 1649 /* 1650 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? 1651 */ 1652 static int test_load_extension( 1653 ClientData clientData, /* Not used */ 1654 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1655 int objc, /* Number of arguments */ 1656 Tcl_Obj *CONST objv[] /* Command arguments */ 1657 ){ 1658 Tcl_CmdInfo cmdInfo; 1659 sqlite3 *db; 1660 int rc; 1661 char *zDb; 1662 char *zFile; 1663 char *zProc = 0; 1664 char *zErr = 0; 1665 1666 if( objc!=4 && objc!=3 ){ 1667 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?"); 1668 return TCL_ERROR; 1669 } 1670 zDb = Tcl_GetString(objv[1]); 1671 zFile = Tcl_GetString(objv[2]); 1672 if( objc==4 ){ 1673 zProc = Tcl_GetString(objv[3]); 1674 } 1675 1676 /* Extract the C database handle from the Tcl command name */ 1677 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 1678 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); 1679 return TCL_ERROR; 1680 } 1681 db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 1682 assert(db); 1683 1684 /* Call the underlying C function. If an error occurs, set rc to 1685 ** TCL_ERROR and load any error string into the interpreter. If no 1686 ** error occurs, set rc to TCL_OK. 1687 */ 1688 #ifdef SQLITE_OMIT_LOAD_EXTENSION 1689 rc = SQLITE_ERROR; 1690 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); 1691 #else 1692 rc = sqlite3_load_extension(db, zFile, zProc, &zErr); 1693 #endif 1694 if( rc!=SQLITE_OK ){ 1695 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); 1696 rc = TCL_ERROR; 1697 }else{ 1698 rc = TCL_OK; 1699 } 1700 sqlite3_free(zErr); 1701 1702 return rc; 1703 } 1704 1705 /* 1706 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF 1707 */ 1708 static int test_enable_load( 1709 ClientData clientData, /* Not used */ 1710 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1711 int objc, /* Number of arguments */ 1712 Tcl_Obj *CONST objv[] /* Command arguments */ 1713 ){ 1714 Tcl_CmdInfo cmdInfo; 1715 sqlite3 *db; 1716 char *zDb; 1717 int onoff; 1718 1719 if( objc!=3 ){ 1720 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); 1721 return TCL_ERROR; 1722 } 1723 zDb = Tcl_GetString(objv[1]); 1724 1725 /* Extract the C database handle from the Tcl command name */ 1726 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ 1727 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); 1728 return TCL_ERROR; 1729 } 1730 db = ((struct SqliteDb*)cmdInfo.objClientData)->db; 1731 assert(db); 1732 1733 /* Get the onoff parameter */ 1734 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 1735 return TCL_ERROR; 1736 } 1737 1738 #ifdef SQLITE_OMIT_LOAD_EXTENSION 1739 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); 1740 return TCL_ERROR; 1741 #else 1742 sqlite3_enable_load_extension(db, onoff); 1743 return TCL_OK; 1744 #endif 1745 } 1746 1747 /* 1748 ** Usage: sqlite_abort 1749 ** 1750 ** Shutdown the process immediately. This is not a clean shutdown. 1751 ** This command is used to test the recoverability of a database in 1752 ** the event of a program crash. 1753 */ 1754 static int sqlite_abort( 1755 void *NotUsed, 1756 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1757 int argc, /* Number of arguments */ 1758 char **argv /* Text of each argument */ 1759 ){ 1760 assert( interp==0 ); /* This will always fail */ 1761 return TCL_OK; 1762 } 1763 1764 /* 1765 ** The following routine is a user-defined SQL function whose purpose 1766 ** is to test the sqlite_set_result() API. 1767 */ 1768 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1769 while( argc>=2 ){ 1770 const char *zArg0 = (char*)sqlite3_value_text(argv[0]); 1771 if( zArg0 ){ 1772 if( 0==sqlite3StrICmp(zArg0, "int") ){ 1773 sqlite3_result_int(context, sqlite3_value_int(argv[1])); 1774 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ 1775 sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); 1776 }else if( sqlite3StrICmp(zArg0,"string")==0 ){ 1777 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, 1778 SQLITE_TRANSIENT); 1779 }else if( sqlite3StrICmp(zArg0,"double")==0 ){ 1780 sqlite3_result_double(context, sqlite3_value_double(argv[1])); 1781 }else if( sqlite3StrICmp(zArg0,"null")==0 ){ 1782 sqlite3_result_null(context); 1783 }else if( sqlite3StrICmp(zArg0,"value")==0 ){ 1784 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); 1785 }else{ 1786 goto error_out; 1787 } 1788 }else{ 1789 goto error_out; 1790 } 1791 argc -= 2; 1792 argv += 2; 1793 } 1794 return; 1795 1796 error_out: 1797 sqlite3_result_error(context,"first argument should be one of: " 1798 "int int64 string double null value", -1); 1799 } 1800 1801 /* 1802 ** Usage: sqlite_register_test_function DB NAME 1803 ** 1804 ** Register the test SQL function on the database DB under the name NAME. 1805 */ 1806 static int test_register_func( 1807 void *NotUsed, 1808 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1809 int argc, /* Number of arguments */ 1810 char **argv /* Text of each argument */ 1811 ){ 1812 sqlite3 *db; 1813 int rc; 1814 if( argc!=3 ){ 1815 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1816 " DB FUNCTION-NAME", 0); 1817 return TCL_ERROR; 1818 } 1819 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 1820 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 1821 testFunc, 0, 0); 1822 if( rc!=0 ){ 1823 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); 1824 return TCL_ERROR; 1825 } 1826 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 1827 return TCL_OK; 1828 } 1829 1830 /* 1831 ** Usage: sqlite3_finalize STMT 1832 ** 1833 ** Finalize a statement handle. 1834 */ 1835 static int test_finalize( 1836 void * clientData, 1837 Tcl_Interp *interp, 1838 int objc, 1839 Tcl_Obj *CONST objv[] 1840 ){ 1841 sqlite3_stmt *pStmt; 1842 int rc; 1843 sqlite3 *db = 0; 1844 1845 if( objc!=2 ){ 1846 Tcl_AppendResult(interp, "wrong # args: should be \"", 1847 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 1848 return TCL_ERROR; 1849 } 1850 1851 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 1852 1853 if( pStmt ){ 1854 db = StmtToDb(pStmt); 1855 } 1856 rc = sqlite3_finalize(pStmt); 1857 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 1858 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 1859 return TCL_OK; 1860 } 1861 1862 /* 1863 ** Usage: sqlite3_reset STMT 1864 ** 1865 ** Reset a statement handle. 1866 */ 1867 static int test_reset( 1868 void * clientData, 1869 Tcl_Interp *interp, 1870 int objc, 1871 Tcl_Obj *CONST objv[] 1872 ){ 1873 sqlite3_stmt *pStmt; 1874 int rc; 1875 1876 if( objc!=2 ){ 1877 Tcl_AppendResult(interp, "wrong # args: should be \"", 1878 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 1879 return TCL_ERROR; 1880 } 1881 1882 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 1883 1884 rc = sqlite3_reset(pStmt); 1885 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ 1886 return TCL_ERROR; 1887 } 1888 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 1889 /* 1890 if( rc ){ 1891 return TCL_ERROR; 1892 } 1893 */ 1894 return TCL_OK; 1895 } 1896 1897 /* 1898 ** Usage: sqlite3_expired STMT 1899 ** 1900 ** Return TRUE if a recompilation of the statement is recommended. 1901 */ 1902 static int test_expired( 1903 void * clientData, 1904 Tcl_Interp *interp, 1905 int objc, 1906 Tcl_Obj *CONST objv[] 1907 ){ 1908 sqlite3_stmt *pStmt; 1909 if( objc!=2 ){ 1910 Tcl_AppendResult(interp, "wrong # args: should be \"", 1911 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); 1912 return TCL_ERROR; 1913 } 1914 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 1915 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); 1916 return TCL_OK; 1917 } 1918 1919 /* 1920 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT 1921 ** 1922 ** Transfer all bindings from FROMSTMT over to TOSTMT 1923 */ 1924 static int test_transfer_bind( 1925 void * clientData, 1926 Tcl_Interp *interp, 1927 int objc, 1928 Tcl_Obj *CONST objv[] 1929 ){ 1930 sqlite3_stmt *pStmt1, *pStmt2; 1931 if( objc!=3 ){ 1932 Tcl_AppendResult(interp, "wrong # args: should be \"", 1933 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); 1934 return TCL_ERROR; 1935 } 1936 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; 1937 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; 1938 Tcl_SetObjResult(interp, 1939 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); 1940 return TCL_OK; 1941 } 1942 1943 /* 1944 ** Usage: sqlite3_changes DB 1945 ** 1946 ** Return the number of changes made to the database by the last SQL 1947 ** execution. 1948 */ 1949 static int test_changes( 1950 void * clientData, 1951 Tcl_Interp *interp, 1952 int objc, 1953 Tcl_Obj *CONST objv[] 1954 ){ 1955 sqlite3 *db; 1956 if( objc!=2 ){ 1957 Tcl_AppendResult(interp, "wrong # args: should be \"", 1958 Tcl_GetString(objv[0]), " DB", 0); 1959 return TCL_ERROR; 1960 } 1961 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 1962 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); 1963 return TCL_OK; 1964 } 1965 1966 /* 1967 ** This is the "static_bind_value" that variables are bound to when 1968 ** the FLAG option of sqlite3_bind is "static" 1969 */ 1970 static char *sqlite_static_bind_value = 0; 1971 static int sqlite_static_bind_nbyte = 0; 1972 1973 /* 1974 ** Usage: sqlite3_bind VM IDX VALUE FLAGS 1975 ** 1976 ** Sets the value of the IDX-th occurance of "?" in the original SQL 1977 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is 1978 ** ignored and the value is set to NULL. If FLAGS=="static" then 1979 ** the value is set to the value of a static variable named 1980 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy 1981 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored 1982 ** an a 10-byte blob "abc\000xyz\000pq" is inserted. 1983 */ 1984 static int test_bind( 1985 void *NotUsed, 1986 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 1987 int argc, /* Number of arguments */ 1988 char **argv /* Text of each argument */ 1989 ){ 1990 sqlite3_stmt *pStmt; 1991 int rc; 1992 int idx; 1993 if( argc!=5 ){ 1994 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 1995 " VM IDX VALUE (null|static|normal)\"", 0); 1996 return TCL_ERROR; 1997 } 1998 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; 1999 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; 2000 if( strcmp(argv[4],"null")==0 ){ 2001 rc = sqlite3_bind_null(pStmt, idx); 2002 }else if( strcmp(argv[4],"static")==0 ){ 2003 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); 2004 }else if( strcmp(argv[4],"static-nbytes")==0 ){ 2005 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, 2006 sqlite_static_bind_nbyte, 0); 2007 }else if( strcmp(argv[4],"normal")==0 ){ 2008 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); 2009 }else if( strcmp(argv[4],"blob10")==0 ){ 2010 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); 2011 }else{ 2012 Tcl_AppendResult(interp, "4th argument should be " 2013 "\"null\" or \"static\" or \"normal\"", 0); 2014 return TCL_ERROR; 2015 } 2016 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2017 if( rc ){ 2018 char zBuf[50]; 2019 sprintf(zBuf, "(%d) ", rc); 2020 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); 2021 return TCL_ERROR; 2022 } 2023 return TCL_OK; 2024 } 2025 2026 #ifndef SQLITE_OMIT_UTF16 2027 /* 2028 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> 2029 ** 2030 ** This function is used to test that SQLite selects the correct collation 2031 ** sequence callback when multiple versions (for different text encodings) 2032 ** are available. 2033 ** 2034 ** Calling this routine registers the collation sequence "test_collate" 2035 ** with database handle <db>. The second argument must be a list of three 2036 ** boolean values. If the first is true, then a version of test_collate is 2037 ** registered for UTF-8, if the second is true, a version is registered for 2038 ** UTF-16le, if the third is true, a UTF-16be version is available. 2039 ** Previous versions of test_collate are deleted. 2040 ** 2041 ** The collation sequence test_collate is implemented by calling the 2042 ** following TCL script: 2043 ** 2044 ** "test_collate <enc> <lhs> <rhs>" 2045 ** 2046 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. 2047 ** The <enc> parameter is the encoding of the collation function that 2048 ** SQLite selected to call. The TCL test script implements the 2049 ** "test_collate" proc. 2050 ** 2051 ** Note that this will only work with one intepreter at a time, as the 2052 ** interp pointer to use when evaluating the TCL script is stored in 2053 ** pTestCollateInterp. 2054 */ 2055 static Tcl_Interp* pTestCollateInterp; 2056 static int test_collate_func( 2057 void *pCtx, 2058 int nA, const void *zA, 2059 int nB, const void *zB 2060 ){ 2061 Tcl_Interp *i = pTestCollateInterp; 2062 int encin = (int)pCtx; 2063 int res; 2064 int n; 2065 2066 sqlite3_value *pVal; 2067 Tcl_Obj *pX; 2068 2069 pX = Tcl_NewStringObj("test_collate", -1); 2070 Tcl_IncrRefCount(pX); 2071 2072 switch( encin ){ 2073 case SQLITE_UTF8: 2074 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); 2075 break; 2076 case SQLITE_UTF16LE: 2077 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); 2078 break; 2079 case SQLITE_UTF16BE: 2080 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); 2081 break; 2082 default: 2083 assert(0); 2084 } 2085 2086 pVal = sqlite3ValueNew(0); 2087 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); 2088 n = sqlite3_value_bytes(pVal); 2089 Tcl_ListObjAppendElement(i,pX, 2090 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2091 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); 2092 n = sqlite3_value_bytes(pVal); 2093 Tcl_ListObjAppendElement(i,pX, 2094 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); 2095 sqlite3ValueFree(pVal); 2096 2097 Tcl_EvalObjEx(i, pX, 0); 2098 Tcl_DecrRefCount(pX); 2099 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); 2100 return res; 2101 } 2102 static int test_collate( 2103 void * clientData, 2104 Tcl_Interp *interp, 2105 int objc, 2106 Tcl_Obj *CONST objv[] 2107 ){ 2108 sqlite3 *db; 2109 int val; 2110 sqlite3_value *pVal; 2111 int rc; 2112 2113 if( objc!=5 ) goto bad_args; 2114 pTestCollateInterp = interp; 2115 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2116 2117 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2118 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, 2119 (void *)SQLITE_UTF8, val?test_collate_func:0); 2120 if( rc==SQLITE_OK ){ 2121 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2122 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, 2123 (void *)SQLITE_UTF16LE, val?test_collate_func:0); 2124 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2125 2126 #if 0 2127 if( sqlite3_iMallocFail>0 ){ 2128 sqlite3_iMallocFail++; 2129 } 2130 #endif 2131 sqlite3_mutex_enter(db->mutex); 2132 pVal = sqlite3ValueNew(db); 2133 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); 2134 if( db->mallocFailed ){ 2135 rc = SQLITE_NOMEM; 2136 }else{ 2137 rc = sqlite3_create_collation16(db, 2138 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE, 2139 (void *)SQLITE_UTF16BE, val?test_collate_func:0); 2140 } 2141 sqlite3ValueFree(pVal); 2142 sqlite3_mutex_leave(db->mutex); 2143 } 2144 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2145 2146 if( rc!=SQLITE_OK ){ 2147 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); 2148 return TCL_ERROR; 2149 } 2150 return TCL_OK; 2151 2152 bad_args: 2153 Tcl_AppendResult(interp, "wrong # args: should be \"", 2154 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2155 return TCL_ERROR; 2156 } 2157 2158 /* 2159 ** When the collation needed callback is invoked, record the name of 2160 ** the requested collating function here. The recorded name is linked 2161 ** to a TCL variable and used to make sure that the requested collation 2162 ** name is correct. 2163 */ 2164 static char zNeededCollation[200]; 2165 static char *pzNeededCollation = zNeededCollation; 2166 2167 2168 /* 2169 ** Called when a collating sequence is needed. Registered using 2170 ** sqlite3_collation_needed16(). 2171 */ 2172 static void test_collate_needed_cb( 2173 void *pCtx, 2174 sqlite3 *db, 2175 int eTextRep, 2176 const void *pName 2177 ){ 2178 int enc = ENC(db); 2179 int i; 2180 char *z; 2181 for(z = (char*)pName, i=0; *z || z[1]; z++){ 2182 if( *z ) zNeededCollation[i++] = *z; 2183 } 2184 zNeededCollation[i] = 0; 2185 sqlite3_create_collation( 2186 db, "test_collate", ENC(db), (void *)enc, test_collate_func); 2187 } 2188 2189 /* 2190 ** Usage: add_test_collate_needed DB 2191 */ 2192 static int test_collate_needed( 2193 void * clientData, 2194 Tcl_Interp *interp, 2195 int objc, 2196 Tcl_Obj *CONST objv[] 2197 ){ 2198 sqlite3 *db; 2199 int rc; 2200 2201 if( objc!=2 ) goto bad_args; 2202 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2203 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); 2204 zNeededCollation[0] = 0; 2205 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 2206 return TCL_OK; 2207 2208 bad_args: 2209 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 2210 return TCL_ERROR; 2211 } 2212 2213 /* 2214 ** tclcmd: add_alignment_test_collations DB 2215 ** 2216 ** Add two new collating sequences to the database DB 2217 ** 2218 ** utf16_aligned 2219 ** utf16_unaligned 2220 ** 2221 ** Both collating sequences use the same sort order as BINARY. 2222 ** The only difference is that the utf16_aligned collating 2223 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. 2224 ** Both collating functions increment the unaligned utf16 counter 2225 ** whenever they see a string that begins on an odd byte boundary. 2226 */ 2227 static int unaligned_string_counter = 0; 2228 static int alignmentCollFunc( 2229 void *NotUsed, 2230 int nKey1, const void *pKey1, 2231 int nKey2, const void *pKey2 2232 ){ 2233 int rc, n; 2234 n = nKey1<nKey2 ? nKey1 : nKey2; 2235 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++; 2236 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++; 2237 rc = memcmp(pKey1, pKey2, n); 2238 if( rc==0 ){ 2239 rc = nKey1 - nKey2; 2240 } 2241 return rc; 2242 } 2243 static int add_alignment_test_collations( 2244 void * clientData, 2245 Tcl_Interp *interp, 2246 int objc, 2247 Tcl_Obj *CONST objv[] 2248 ){ 2249 sqlite3 *db; 2250 if( objc>=2 ){ 2251 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2252 sqlite3_create_collation(db, "utf16_unaligned", 2253 SQLITE_UTF16, 2254 0, alignmentCollFunc); 2255 sqlite3_create_collation(db, "utf16_aligned", 2256 SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, 2257 0, alignmentCollFunc); 2258 } 2259 return SQLITE_OK; 2260 } 2261 #endif /* !defined(SQLITE_OMIT_UTF16) */ 2262 2263 /* 2264 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> 2265 ** 2266 ** This function is used to test that SQLite selects the correct user 2267 ** function callback when multiple versions (for different text encodings) 2268 ** are available. 2269 ** 2270 ** Calling this routine registers up to three versions of the user function 2271 ** "test_function" with database handle <db>. If the second argument is 2272 ** true, then a version of test_function is registered for UTF-8, if the 2273 ** third is true, a version is registered for UTF-16le, if the fourth is 2274 ** true, a UTF-16be version is available. Previous versions of 2275 ** test_function are deleted. 2276 ** 2277 ** The user function is implemented by calling the following TCL script: 2278 ** 2279 ** "test_function <enc> <arg>" 2280 ** 2281 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the 2282 ** single argument passed to the SQL function. The value returned by 2283 ** the TCL script is used as the return value of the SQL function. It 2284 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 2285 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that 2286 ** prefers UTF-16BE. 2287 */ 2288 #ifndef SQLITE_OMIT_UTF16 2289 static void test_function_utf8( 2290 sqlite3_context *pCtx, 2291 int nArg, 2292 sqlite3_value **argv 2293 ){ 2294 Tcl_Interp *interp; 2295 Tcl_Obj *pX; 2296 sqlite3_value *pVal; 2297 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2298 pX = Tcl_NewStringObj("test_function", -1); 2299 Tcl_IncrRefCount(pX); 2300 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); 2301 Tcl_ListObjAppendElement(interp, pX, 2302 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2303 Tcl_EvalObjEx(interp, pX, 0); 2304 Tcl_DecrRefCount(pX); 2305 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); 2306 pVal = sqlite3ValueNew(0); 2307 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2308 SQLITE_UTF8, SQLITE_STATIC); 2309 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), 2310 -1, SQLITE_TRANSIENT); 2311 sqlite3ValueFree(pVal); 2312 } 2313 static void test_function_utf16le( 2314 sqlite3_context *pCtx, 2315 int nArg, 2316 sqlite3_value **argv 2317 ){ 2318 Tcl_Interp *interp; 2319 Tcl_Obj *pX; 2320 sqlite3_value *pVal; 2321 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2322 pX = Tcl_NewStringObj("test_function", -1); 2323 Tcl_IncrRefCount(pX); 2324 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); 2325 Tcl_ListObjAppendElement(interp, pX, 2326 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2327 Tcl_EvalObjEx(interp, pX, 0); 2328 Tcl_DecrRefCount(pX); 2329 pVal = sqlite3ValueNew(0); 2330 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2331 SQLITE_UTF8, SQLITE_STATIC); 2332 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); 2333 sqlite3ValueFree(pVal); 2334 } 2335 static void test_function_utf16be( 2336 sqlite3_context *pCtx, 2337 int nArg, 2338 sqlite3_value **argv 2339 ){ 2340 Tcl_Interp *interp; 2341 Tcl_Obj *pX; 2342 sqlite3_value *pVal; 2343 interp = (Tcl_Interp *)sqlite3_user_data(pCtx); 2344 pX = Tcl_NewStringObj("test_function", -1); 2345 Tcl_IncrRefCount(pX); 2346 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); 2347 Tcl_ListObjAppendElement(interp, pX, 2348 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); 2349 Tcl_EvalObjEx(interp, pX, 0); 2350 Tcl_DecrRefCount(pX); 2351 pVal = sqlite3ValueNew(0); 2352 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 2353 SQLITE_UTF8, SQLITE_STATIC); 2354 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), 2355 -1, SQLITE_TRANSIENT); 2356 sqlite3ValueFree(pVal); 2357 } 2358 #endif /* SQLITE_OMIT_UTF16 */ 2359 static int test_function( 2360 void * clientData, 2361 Tcl_Interp *interp, 2362 int objc, 2363 Tcl_Obj *CONST objv[] 2364 ){ 2365 #ifndef SQLITE_OMIT_UTF16 2366 sqlite3 *db; 2367 int val; 2368 2369 if( objc!=5 ) goto bad_args; 2370 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2371 2372 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; 2373 if( val ){ 2374 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, 2375 interp, test_function_utf8, 0, 0); 2376 } 2377 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; 2378 if( val ){ 2379 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, 2380 interp, test_function_utf16le, 0, 0); 2381 } 2382 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; 2383 if( val ){ 2384 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, 2385 interp, test_function_utf16be, 0, 0); 2386 } 2387 2388 return TCL_OK; 2389 bad_args: 2390 Tcl_AppendResult(interp, "wrong # args: should be \"", 2391 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); 2392 #endif /* SQLITE_OMIT_UTF16 */ 2393 return TCL_ERROR; 2394 } 2395 2396 /* 2397 ** Usage: test_errstr <err code> 2398 ** 2399 ** Test that the english language string equivalents for sqlite error codes 2400 ** are sane. The parameter is an integer representing an sqlite error code. 2401 ** The result is a list of two elements, the string representation of the 2402 ** error code and the english language explanation. 2403 */ 2404 static int test_errstr( 2405 void * clientData, 2406 Tcl_Interp *interp, 2407 int objc, 2408 Tcl_Obj *CONST objv[] 2409 ){ 2410 char *zCode; 2411 int i; 2412 if( objc!=1 ){ 2413 Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); 2414 } 2415 2416 zCode = Tcl_GetString(objv[1]); 2417 for(i=0; i<200; i++){ 2418 if( 0==strcmp(t1ErrorName(i), zCode) ) break; 2419 } 2420 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); 2421 return TCL_OK; 2422 } 2423 2424 /* 2425 ** Usage: breakpoint 2426 ** 2427 ** This routine exists for one purpose - to provide a place to put a 2428 ** breakpoint with GDB that can be triggered using TCL code. The use 2429 ** for this is when a particular test fails on (say) the 1485th iteration. 2430 ** In the TCL test script, we can add code like this: 2431 ** 2432 ** if {$i==1485} breakpoint 2433 ** 2434 ** Then run testfixture in the debugger and wait for the breakpoint to 2435 ** fire. Then additional breakpoints can be set to trace down the bug. 2436 */ 2437 static int test_breakpoint( 2438 void *NotUsed, 2439 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 2440 int argc, /* Number of arguments */ 2441 char **argv /* Text of each argument */ 2442 ){ 2443 return TCL_OK; /* Do nothing */ 2444 } 2445 2446 /* 2447 ** Usage: sqlite3_bind_zeroblob STMT IDX N 2448 ** 2449 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. 2450 ** IDX is the index of a wildcard in the prepared statement. This command 2451 ** binds a N-byte zero-filled BLOB to the wildcard. 2452 */ 2453 static int test_bind_zeroblob( 2454 void * clientData, 2455 Tcl_Interp *interp, 2456 int objc, 2457 Tcl_Obj *CONST objv[] 2458 ){ 2459 sqlite3_stmt *pStmt; 2460 int idx; 2461 int n; 2462 int rc; 2463 2464 if( objc!=4 ){ 2465 Tcl_AppendResult(interp, "wrong # args: should be \"", 2466 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 2467 return TCL_ERROR; 2468 } 2469 2470 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2471 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2472 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; 2473 2474 rc = sqlite3_bind_zeroblob(pStmt, idx, n); 2475 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2476 if( rc!=SQLITE_OK ){ 2477 return TCL_ERROR; 2478 } 2479 2480 return TCL_OK; 2481 } 2482 2483 /* 2484 ** Usage: sqlite3_bind_int STMT N VALUE 2485 ** 2486 ** Test the sqlite3_bind_int interface. STMT is a prepared statement. 2487 ** N is the index of a wildcard in the prepared statement. This command 2488 ** binds a 32-bit integer VALUE to that wildcard. 2489 */ 2490 static int test_bind_int( 2491 void * clientData, 2492 Tcl_Interp *interp, 2493 int objc, 2494 Tcl_Obj *CONST objv[] 2495 ){ 2496 sqlite3_stmt *pStmt; 2497 int idx; 2498 int value; 2499 int rc; 2500 2501 if( objc!=4 ){ 2502 Tcl_AppendResult(interp, "wrong # args: should be \"", 2503 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 2504 return TCL_ERROR; 2505 } 2506 2507 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2508 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2509 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 2510 2511 rc = sqlite3_bind_int(pStmt, idx, value); 2512 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2513 if( rc!=SQLITE_OK ){ 2514 return TCL_ERROR; 2515 } 2516 2517 return TCL_OK; 2518 } 2519 2520 2521 /* 2522 ** Usage: sqlite3_bind_int64 STMT N VALUE 2523 ** 2524 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. 2525 ** N is the index of a wildcard in the prepared statement. This command 2526 ** binds a 64-bit integer VALUE to that wildcard. 2527 */ 2528 static int test_bind_int64( 2529 void * clientData, 2530 Tcl_Interp *interp, 2531 int objc, 2532 Tcl_Obj *CONST objv[] 2533 ){ 2534 sqlite3_stmt *pStmt; 2535 int idx; 2536 i64 value; 2537 int rc; 2538 2539 if( objc!=4 ){ 2540 Tcl_AppendResult(interp, "wrong # args: should be \"", 2541 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 2542 return TCL_ERROR; 2543 } 2544 2545 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2546 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2547 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; 2548 2549 rc = sqlite3_bind_int64(pStmt, idx, value); 2550 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2551 if( rc!=SQLITE_OK ){ 2552 return TCL_ERROR; 2553 } 2554 2555 return TCL_OK; 2556 } 2557 2558 2559 /* 2560 ** Usage: sqlite3_bind_double STMT N VALUE 2561 ** 2562 ** Test the sqlite3_bind_double interface. STMT is a prepared statement. 2563 ** N is the index of a wildcard in the prepared statement. This command 2564 ** binds a 64-bit integer VALUE to that wildcard. 2565 */ 2566 static int test_bind_double( 2567 void * clientData, 2568 Tcl_Interp *interp, 2569 int objc, 2570 Tcl_Obj *CONST objv[] 2571 ){ 2572 sqlite3_stmt *pStmt; 2573 int idx; 2574 double value; 2575 int rc; 2576 2577 if( objc!=4 ){ 2578 Tcl_AppendResult(interp, "wrong # args: should be \"", 2579 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); 2580 return TCL_ERROR; 2581 } 2582 2583 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2584 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2585 if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR; 2586 2587 rc = sqlite3_bind_double(pStmt, idx, value); 2588 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2589 if( rc!=SQLITE_OK ){ 2590 return TCL_ERROR; 2591 } 2592 2593 return TCL_OK; 2594 } 2595 2596 /* 2597 ** Usage: sqlite3_bind_null STMT N 2598 ** 2599 ** Test the sqlite3_bind_null interface. STMT is a prepared statement. 2600 ** N is the index of a wildcard in the prepared statement. This command 2601 ** binds a NULL to the wildcard. 2602 */ 2603 static int test_bind_null( 2604 void * clientData, 2605 Tcl_Interp *interp, 2606 int objc, 2607 Tcl_Obj *CONST objv[] 2608 ){ 2609 sqlite3_stmt *pStmt; 2610 int idx; 2611 int rc; 2612 2613 if( objc!=3 ){ 2614 Tcl_AppendResult(interp, "wrong # args: should be \"", 2615 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); 2616 return TCL_ERROR; 2617 } 2618 2619 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2620 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2621 2622 rc = sqlite3_bind_null(pStmt, idx); 2623 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2624 if( rc!=SQLITE_OK ){ 2625 return TCL_ERROR; 2626 } 2627 2628 return TCL_OK; 2629 } 2630 2631 /* 2632 ** Usage: sqlite3_bind_text STMT N STRING BYTES 2633 ** 2634 ** Test the sqlite3_bind_text interface. STMT is a prepared statement. 2635 ** N is the index of a wildcard in the prepared statement. This command 2636 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes 2637 ** long. 2638 */ 2639 static int test_bind_text( 2640 void * clientData, 2641 Tcl_Interp *interp, 2642 int objc, 2643 Tcl_Obj *CONST objv[] 2644 ){ 2645 sqlite3_stmt *pStmt; 2646 int idx; 2647 int bytes; 2648 char *value; 2649 int rc; 2650 2651 if( objc!=5 ){ 2652 Tcl_AppendResult(interp, "wrong # args: should be \"", 2653 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 2654 return TCL_ERROR; 2655 } 2656 2657 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2658 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2659 value = Tcl_GetString(objv[3]); 2660 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 2661 2662 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); 2663 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2664 if( rc!=SQLITE_OK ){ 2665 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); 2666 return TCL_ERROR; 2667 } 2668 2669 return TCL_OK; 2670 } 2671 2672 /* 2673 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES 2674 ** 2675 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. 2676 ** N is the index of a wildcard in the prepared statement. This command 2677 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes 2678 ** long. 2679 */ 2680 static int test_bind_text16( 2681 void * clientData, 2682 Tcl_Interp *interp, 2683 int objc, 2684 Tcl_Obj *CONST objv[] 2685 ){ 2686 #ifndef SQLITE_OMIT_UTF16 2687 sqlite3_stmt *pStmt; 2688 int idx; 2689 int bytes; 2690 char *value; 2691 int rc; 2692 2693 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); 2694 Tcl_Obj *oStmt = objv[objc-4]; 2695 Tcl_Obj *oN = objv[objc-3]; 2696 Tcl_Obj *oString = objv[objc-2]; 2697 Tcl_Obj *oBytes = objv[objc-1]; 2698 2699 if( objc!=5 && objc!=6){ 2700 Tcl_AppendResult(interp, "wrong # args: should be \"", 2701 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); 2702 return TCL_ERROR; 2703 } 2704 2705 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; 2706 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; 2707 value = (char*)Tcl_GetByteArrayFromObj(oString, 0); 2708 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; 2709 2710 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); 2711 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2712 if( rc!=SQLITE_OK ){ 2713 return TCL_ERROR; 2714 } 2715 2716 #endif /* SQLITE_OMIT_UTF16 */ 2717 return TCL_OK; 2718 } 2719 2720 /* 2721 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES 2722 ** 2723 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. 2724 ** N is the index of a wildcard in the prepared statement. This command 2725 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. 2726 */ 2727 static int test_bind_blob( 2728 void * clientData, 2729 Tcl_Interp *interp, 2730 int objc, 2731 Tcl_Obj *CONST objv[] 2732 ){ 2733 sqlite3_stmt *pStmt; 2734 int idx; 2735 int bytes; 2736 char *value; 2737 int rc; 2738 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; 2739 2740 if( objc!=5 && objc!=6 ){ 2741 Tcl_AppendResult(interp, "wrong # args: should be \"", 2742 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); 2743 return TCL_ERROR; 2744 } 2745 2746 if( objc==6 ){ 2747 xDestructor = SQLITE_STATIC; 2748 objv++; 2749 } 2750 2751 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2752 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; 2753 value = Tcl_GetString(objv[3]); 2754 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; 2755 2756 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); 2757 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; 2758 if( rc!=SQLITE_OK ){ 2759 return TCL_ERROR; 2760 } 2761 2762 return TCL_OK; 2763 } 2764 2765 /* 2766 ** Usage: sqlite3_bind_parameter_count STMT 2767 ** 2768 ** Return the number of wildcards in the given statement. 2769 */ 2770 static int test_bind_parameter_count( 2771 void * clientData, 2772 Tcl_Interp *interp, 2773 int objc, 2774 Tcl_Obj *CONST objv[] 2775 ){ 2776 sqlite3_stmt *pStmt; 2777 2778 if( objc!=2 ){ 2779 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 2780 return TCL_ERROR; 2781 } 2782 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2783 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); 2784 return TCL_OK; 2785 } 2786 2787 /* 2788 ** Usage: sqlite3_bind_parameter_name STMT N 2789 ** 2790 ** Return the name of the Nth wildcard. The first wildcard is 1. 2791 ** An empty string is returned if N is out of range or if the wildcard 2792 ** is nameless. 2793 */ 2794 static int test_bind_parameter_name( 2795 void * clientData, 2796 Tcl_Interp *interp, 2797 int objc, 2798 Tcl_Obj *CONST objv[] 2799 ){ 2800 sqlite3_stmt *pStmt; 2801 int i; 2802 2803 if( objc!=3 ){ 2804 Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); 2805 return TCL_ERROR; 2806 } 2807 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2808 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; 2809 Tcl_SetObjResult(interp, 2810 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) 2811 ); 2812 return TCL_OK; 2813 } 2814 2815 /* 2816 ** Usage: sqlite3_bind_parameter_index STMT NAME 2817 ** 2818 ** Return the index of the wildcard called NAME. Return 0 if there is 2819 ** no such wildcard. 2820 */ 2821 static int test_bind_parameter_index( 2822 void * clientData, 2823 Tcl_Interp *interp, 2824 int objc, 2825 Tcl_Obj *CONST objv[] 2826 ){ 2827 sqlite3_stmt *pStmt; 2828 2829 if( objc!=3 ){ 2830 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); 2831 return TCL_ERROR; 2832 } 2833 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2834 Tcl_SetObjResult(interp, 2835 Tcl_NewIntObj( 2836 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) 2837 ) 2838 ); 2839 return TCL_OK; 2840 } 2841 2842 /* 2843 ** Usage: sqlite3_clear_bindings STMT 2844 ** 2845 */ 2846 static int test_clear_bindings( 2847 void * clientData, 2848 Tcl_Interp *interp, 2849 int objc, 2850 Tcl_Obj *CONST objv[] 2851 ){ 2852 sqlite3_stmt *pStmt; 2853 2854 if( objc!=2 ){ 2855 Tcl_WrongNumArgs(interp, 1, objv, "STMT"); 2856 return TCL_ERROR; 2857 } 2858 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 2859 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); 2860 return TCL_OK; 2861 } 2862 2863 /* 2864 ** Usage: sqlite3_sleep MILLISECONDS 2865 */ 2866 static int test_sleep( 2867 void * clientData, 2868 Tcl_Interp *interp, 2869 int objc, 2870 Tcl_Obj *CONST objv[] 2871 ){ 2872 int ms; 2873 2874 if( objc!=2 ){ 2875 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); 2876 return TCL_ERROR; 2877 } 2878 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ 2879 return TCL_ERROR; 2880 } 2881 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); 2882 return TCL_OK; 2883 } 2884 2885 /* 2886 ** Usage: sqlite3_errcode DB 2887 ** 2888 ** Return the string representation of the most recent sqlite3_* API 2889 ** error code. e.g. "SQLITE_ERROR". 2890 */ 2891 static int test_errcode( 2892 void * clientData, 2893 Tcl_Interp *interp, 2894 int objc, 2895 Tcl_Obj *CONST objv[] 2896 ){ 2897 sqlite3 *db; 2898 int rc; 2899 char zBuf[30]; 2900 2901 if( objc!=2 ){ 2902 Tcl_AppendResult(interp, "wrong # args: should be \"", 2903 Tcl_GetString(objv[0]), " DB", 0); 2904 return TCL_ERROR; 2905 } 2906 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2907 rc = sqlite3_errcode(db); 2908 if( (rc&0xff)==rc ){ 2909 zBuf[0] = 0; 2910 }else{ 2911 sprintf(zBuf,"+%d", rc>>8); 2912 } 2913 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0); 2914 return TCL_OK; 2915 } 2916 2917 /* 2918 ** Usage: test_errmsg DB 2919 ** 2920 ** Returns the UTF-8 representation of the error message string for the 2921 ** most recent sqlite3_* API call. 2922 */ 2923 static int test_errmsg( 2924 void * clientData, 2925 Tcl_Interp *interp, 2926 int objc, 2927 Tcl_Obj *CONST objv[] 2928 ){ 2929 sqlite3 *db; 2930 const char *zErr; 2931 2932 if( objc!=2 ){ 2933 Tcl_AppendResult(interp, "wrong # args: should be \"", 2934 Tcl_GetString(objv[0]), " DB", 0); 2935 return TCL_ERROR; 2936 } 2937 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2938 2939 zErr = sqlite3_errmsg(db); 2940 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 2941 return TCL_OK; 2942 } 2943 2944 /* 2945 ** Usage: test_errmsg16 DB 2946 ** 2947 ** Returns the UTF-16 representation of the error message string for the 2948 ** most recent sqlite3_* API call. This is a byte array object at the TCL 2949 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the 2950 ** UTF-16 string. 2951 */ 2952 static int test_errmsg16( 2953 void * clientData, 2954 Tcl_Interp *interp, 2955 int objc, 2956 Tcl_Obj *CONST objv[] 2957 ){ 2958 #ifndef SQLITE_OMIT_UTF16 2959 sqlite3 *db; 2960 const void *zErr; 2961 int bytes = 0; 2962 2963 if( objc!=2 ){ 2964 Tcl_AppendResult(interp, "wrong # args: should be \"", 2965 Tcl_GetString(objv[0]), " DB", 0); 2966 return TCL_ERROR; 2967 } 2968 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 2969 2970 zErr = sqlite3_errmsg16(db); 2971 if( zErr ){ 2972 bytes = sqlite3Utf16ByteLen(zErr, -1); 2973 } 2974 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); 2975 #endif /* SQLITE_OMIT_UTF16 */ 2976 return TCL_OK; 2977 } 2978 2979 /* 2980 ** Usage: sqlite3_prepare DB sql bytes tailvar 2981 ** 2982 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 2983 ** database handle <DB>. The parameter <tailval> is the name of a global 2984 ** variable that is set to the unused portion of <sql> (if any). A 2985 ** STMT handle is returned. 2986 */ 2987 static int test_prepare( 2988 void * clientData, 2989 Tcl_Interp *interp, 2990 int objc, 2991 Tcl_Obj *CONST objv[] 2992 ){ 2993 sqlite3 *db; 2994 const char *zSql; 2995 int bytes; 2996 const char *zTail = 0; 2997 sqlite3_stmt *pStmt = 0; 2998 char zBuf[50]; 2999 int rc; 3000 3001 if( objc!=5 ){ 3002 Tcl_AppendResult(interp, "wrong # args: should be \"", 3003 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3004 return TCL_ERROR; 3005 } 3006 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3007 zSql = Tcl_GetString(objv[2]); 3008 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3009 3010 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail); 3011 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3012 if( zTail ){ 3013 if( bytes>=0 ){ 3014 bytes = bytes - (zTail-zSql); 3015 } 3016 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3017 } 3018 if( rc!=SQLITE_OK ){ 3019 assert( pStmt==0 ); 3020 sprintf(zBuf, "(%d) ", rc); 3021 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3022 return TCL_ERROR; 3023 } 3024 3025 if( pStmt ){ 3026 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3027 Tcl_AppendResult(interp, zBuf, 0); 3028 } 3029 return TCL_OK; 3030 } 3031 3032 /* 3033 ** Usage: sqlite3_prepare_v2 DB sql bytes tailvar 3034 ** 3035 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3036 ** database handle <DB>. The parameter <tailval> is the name of a global 3037 ** variable that is set to the unused portion of <sql> (if any). A 3038 ** STMT handle is returned. 3039 */ 3040 static int test_prepare_v2( 3041 void * clientData, 3042 Tcl_Interp *interp, 3043 int objc, 3044 Tcl_Obj *CONST objv[] 3045 ){ 3046 sqlite3 *db; 3047 const char *zSql; 3048 int bytes; 3049 const char *zTail = 0; 3050 sqlite3_stmt *pStmt = 0; 3051 char zBuf[50]; 3052 int rc; 3053 3054 if( objc!=5 ){ 3055 Tcl_AppendResult(interp, "wrong # args: should be \"", 3056 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3057 return TCL_ERROR; 3058 } 3059 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3060 zSql = Tcl_GetString(objv[2]); 3061 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3062 3063 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail); 3064 assert(rc==SQLITE_OK || pStmt==0); 3065 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3066 if( zTail ){ 3067 if( bytes>=0 ){ 3068 bytes = bytes - (zTail-zSql); 3069 } 3070 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); 3071 } 3072 if( rc!=SQLITE_OK ){ 3073 assert( pStmt==0 ); 3074 sprintf(zBuf, "(%d) ", rc); 3075 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); 3076 return TCL_ERROR; 3077 } 3078 3079 if( pStmt ){ 3080 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3081 Tcl_AppendResult(interp, zBuf, 0); 3082 } 3083 return TCL_OK; 3084 } 3085 3086 /* 3087 ** Usage: sqlite3_prepare16 DB sql bytes tailvar 3088 ** 3089 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3090 ** database handle <DB>. The parameter <tailval> is the name of a global 3091 ** variable that is set to the unused portion of <sql> (if any). A 3092 ** STMT handle is returned. 3093 */ 3094 static int test_prepare16( 3095 void * clientData, 3096 Tcl_Interp *interp, 3097 int objc, 3098 Tcl_Obj *CONST objv[] 3099 ){ 3100 #ifndef SQLITE_OMIT_UTF16 3101 sqlite3 *db; 3102 const void *zSql; 3103 const void *zTail = 0; 3104 Tcl_Obj *pTail = 0; 3105 sqlite3_stmt *pStmt = 0; 3106 char zBuf[50]; 3107 int rc; 3108 int bytes; /* The integer specified as arg 3 */ 3109 int objlen; /* The byte-array length of arg 2 */ 3110 3111 if( objc!=5 ){ 3112 Tcl_AppendResult(interp, "wrong # args: should be \"", 3113 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3114 return TCL_ERROR; 3115 } 3116 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3117 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3118 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3119 3120 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail); 3121 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3122 if( rc ){ 3123 return TCL_ERROR; 3124 } 3125 3126 if( zTail ){ 3127 objlen = objlen - ((u8 *)zTail-(u8 *)zSql); 3128 }else{ 3129 objlen = 0; 3130 } 3131 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3132 Tcl_IncrRefCount(pTail); 3133 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3134 Tcl_DecrRefCount(pTail); 3135 3136 if( pStmt ){ 3137 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3138 } 3139 Tcl_AppendResult(interp, zBuf, 0); 3140 #endif /* SQLITE_OMIT_UTF16 */ 3141 return TCL_OK; 3142 } 3143 3144 /* 3145 ** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar 3146 ** 3147 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using 3148 ** database handle <DB>. The parameter <tailval> is the name of a global 3149 ** variable that is set to the unused portion of <sql> (if any). A 3150 ** STMT handle is returned. 3151 */ 3152 static int test_prepare16_v2( 3153 void * clientData, 3154 Tcl_Interp *interp, 3155 int objc, 3156 Tcl_Obj *CONST objv[] 3157 ){ 3158 #ifndef SQLITE_OMIT_UTF16 3159 sqlite3 *db; 3160 const void *zSql; 3161 const void *zTail = 0; 3162 Tcl_Obj *pTail = 0; 3163 sqlite3_stmt *pStmt = 0; 3164 char zBuf[50]; 3165 int rc; 3166 int bytes; /* The integer specified as arg 3 */ 3167 int objlen; /* The byte-array length of arg 2 */ 3168 3169 if( objc!=5 ){ 3170 Tcl_AppendResult(interp, "wrong # args: should be \"", 3171 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); 3172 return TCL_ERROR; 3173 } 3174 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 3175 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); 3176 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; 3177 3178 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail); 3179 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; 3180 if( rc ){ 3181 return TCL_ERROR; 3182 } 3183 3184 if( zTail ){ 3185 objlen = objlen - ((u8 *)zTail-(u8 *)zSql); 3186 }else{ 3187 objlen = 0; 3188 } 3189 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); 3190 Tcl_IncrRefCount(pTail); 3191 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); 3192 Tcl_DecrRefCount(pTail); 3193 3194 if( pStmt ){ 3195 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; 3196 } 3197 Tcl_AppendResult(interp, zBuf, 0); 3198 #endif /* SQLITE_OMIT_UTF16 */ 3199 return TCL_OK; 3200 } 3201 3202 /* 3203 ** Usage: sqlite3_open filename ?options-list? 3204 */ 3205 static int test_open( 3206 void * clientData, 3207 Tcl_Interp *interp, 3208 int objc, 3209 Tcl_Obj *CONST objv[] 3210 ){ 3211 const char *zFilename; 3212 sqlite3 *db; 3213 int rc; 3214 char zBuf[100]; 3215 3216 if( objc!=3 && objc!=2 ){ 3217 Tcl_AppendResult(interp, "wrong # args: should be \"", 3218 Tcl_GetString(objv[0]), " filename options-list", 0); 3219 return TCL_ERROR; 3220 } 3221 3222 zFilename = Tcl_GetString(objv[1]); 3223 rc = sqlite3_open(zFilename, &db); 3224 3225 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3226 Tcl_AppendResult(interp, zBuf, 0); 3227 return TCL_OK; 3228 } 3229 3230 /* 3231 ** Usage: sqlite3_open16 filename options 3232 */ 3233 static int test_open16( 3234 void * clientData, 3235 Tcl_Interp *interp, 3236 int objc, 3237 Tcl_Obj *CONST objv[] 3238 ){ 3239 #ifndef SQLITE_OMIT_UTF16 3240 const void *zFilename; 3241 sqlite3 *db; 3242 int rc; 3243 char zBuf[100]; 3244 3245 if( objc!=3 ){ 3246 Tcl_AppendResult(interp, "wrong # args: should be \"", 3247 Tcl_GetString(objv[0]), " filename options-list", 0); 3248 return TCL_ERROR; 3249 } 3250 3251 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); 3252 rc = sqlite3_open16(zFilename, &db); 3253 3254 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; 3255 Tcl_AppendResult(interp, zBuf, 0); 3256 #endif /* SQLITE_OMIT_UTF16 */ 3257 return TCL_OK; 3258 } 3259 3260 /* 3261 ** Usage: sqlite3_complete16 <UTF-16 string> 3262 ** 3263 ** Return 1 if the supplied argument is a complete SQL statement, or zero 3264 ** otherwise. 3265 */ 3266 static int test_complete16( 3267 void * clientData, 3268 Tcl_Interp *interp, 3269 int objc, 3270 Tcl_Obj *CONST objv[] 3271 ){ 3272 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) 3273 char *zBuf; 3274 3275 if( objc!=2 ){ 3276 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); 3277 return TCL_ERROR; 3278 } 3279 3280 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); 3281 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); 3282 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ 3283 return TCL_OK; 3284 } 3285 3286 /* 3287 ** Usage: sqlite3_step STMT 3288 ** 3289 ** Advance the statement to the next row. 3290 */ 3291 static int test_step( 3292 void * clientData, 3293 Tcl_Interp *interp, 3294 int objc, 3295 Tcl_Obj *CONST objv[] 3296 ){ 3297 sqlite3_stmt *pStmt; 3298 int rc; 3299 3300 if( objc!=2 ){ 3301 Tcl_AppendResult(interp, "wrong # args: should be \"", 3302 Tcl_GetString(objv[0]), " STMT", 0); 3303 return TCL_ERROR; 3304 } 3305 3306 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3307 rc = sqlite3_step(pStmt); 3308 3309 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ 3310 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); 3311 return TCL_OK; 3312 } 3313 3314 /* 3315 ** Usage: sqlite3_column_count STMT 3316 ** 3317 ** Return the number of columns returned by the sql statement STMT. 3318 */ 3319 static int test_column_count( 3320 void * clientData, 3321 Tcl_Interp *interp, 3322 int objc, 3323 Tcl_Obj *CONST objv[] 3324 ){ 3325 sqlite3_stmt *pStmt; 3326 3327 if( objc!=2 ){ 3328 Tcl_AppendResult(interp, "wrong # args: should be \"", 3329 Tcl_GetString(objv[0]), " STMT column", 0); 3330 return TCL_ERROR; 3331 } 3332 3333 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3334 3335 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); 3336 return TCL_OK; 3337 } 3338 3339 /* 3340 ** Usage: sqlite3_column_type STMT column 3341 ** 3342 ** Return the type of the data in column 'column' of the current row. 3343 */ 3344 static int test_column_type( 3345 void * clientData, 3346 Tcl_Interp *interp, 3347 int objc, 3348 Tcl_Obj *CONST objv[] 3349 ){ 3350 sqlite3_stmt *pStmt; 3351 int col; 3352 int tp; 3353 3354 if( objc!=3 ){ 3355 Tcl_AppendResult(interp, "wrong # args: should be \"", 3356 Tcl_GetString(objv[0]), " STMT column", 0); 3357 return TCL_ERROR; 3358 } 3359 3360 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3361 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3362 3363 tp = sqlite3_column_type(pStmt, col); 3364 switch( tp ){ 3365 case SQLITE_INTEGER: 3366 Tcl_SetResult(interp, "INTEGER", TCL_STATIC); 3367 break; 3368 case SQLITE_NULL: 3369 Tcl_SetResult(interp, "NULL", TCL_STATIC); 3370 break; 3371 case SQLITE_FLOAT: 3372 Tcl_SetResult(interp, "FLOAT", TCL_STATIC); 3373 break; 3374 case SQLITE_TEXT: 3375 Tcl_SetResult(interp, "TEXT", TCL_STATIC); 3376 break; 3377 case SQLITE_BLOB: 3378 Tcl_SetResult(interp, "BLOB", TCL_STATIC); 3379 break; 3380 default: 3381 assert(0); 3382 } 3383 3384 return TCL_OK; 3385 } 3386 3387 /* 3388 ** Usage: sqlite3_column_int64 STMT column 3389 ** 3390 ** Return the data in column 'column' of the current row cast as an 3391 ** wide (64-bit) integer. 3392 */ 3393 static int test_column_int64( 3394 void * clientData, 3395 Tcl_Interp *interp, 3396 int objc, 3397 Tcl_Obj *CONST objv[] 3398 ){ 3399 sqlite3_stmt *pStmt; 3400 int col; 3401 i64 iVal; 3402 3403 if( objc!=3 ){ 3404 Tcl_AppendResult(interp, "wrong # args: should be \"", 3405 Tcl_GetString(objv[0]), " STMT column", 0); 3406 return TCL_ERROR; 3407 } 3408 3409 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3410 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3411 3412 iVal = sqlite3_column_int64(pStmt, col); 3413 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); 3414 return TCL_OK; 3415 } 3416 3417 /* 3418 ** Usage: sqlite3_column_blob STMT column 3419 */ 3420 static int test_column_blob( 3421 void * clientData, 3422 Tcl_Interp *interp, 3423 int objc, 3424 Tcl_Obj *CONST objv[] 3425 ){ 3426 sqlite3_stmt *pStmt; 3427 int col; 3428 3429 int len; 3430 const void *pBlob; 3431 3432 if( objc!=3 ){ 3433 Tcl_AppendResult(interp, "wrong # args: should be \"", 3434 Tcl_GetString(objv[0]), " STMT column", 0); 3435 return TCL_ERROR; 3436 } 3437 3438 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3439 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3440 3441 len = sqlite3_column_bytes(pStmt, col); 3442 pBlob = sqlite3_column_blob(pStmt, col); 3443 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); 3444 return TCL_OK; 3445 } 3446 3447 /* 3448 ** Usage: sqlite3_column_double STMT column 3449 ** 3450 ** Return the data in column 'column' of the current row cast as a double. 3451 */ 3452 static int test_column_double( 3453 void * clientData, 3454 Tcl_Interp *interp, 3455 int objc, 3456 Tcl_Obj *CONST objv[] 3457 ){ 3458 sqlite3_stmt *pStmt; 3459 int col; 3460 double rVal; 3461 3462 if( objc!=3 ){ 3463 Tcl_AppendResult(interp, "wrong # args: should be \"", 3464 Tcl_GetString(objv[0]), " STMT column", 0); 3465 return TCL_ERROR; 3466 } 3467 3468 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3469 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3470 3471 rVal = sqlite3_column_double(pStmt, col); 3472 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); 3473 return TCL_OK; 3474 } 3475 3476 /* 3477 ** Usage: sqlite3_data_count STMT 3478 ** 3479 ** Return the number of columns returned by the sql statement STMT. 3480 */ 3481 static int test_data_count( 3482 void * clientData, 3483 Tcl_Interp *interp, 3484 int objc, 3485 Tcl_Obj *CONST objv[] 3486 ){ 3487 sqlite3_stmt *pStmt; 3488 3489 if( objc!=2 ){ 3490 Tcl_AppendResult(interp, "wrong # args: should be \"", 3491 Tcl_GetString(objv[0]), " STMT column", 0); 3492 return TCL_ERROR; 3493 } 3494 3495 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3496 3497 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); 3498 return TCL_OK; 3499 } 3500 3501 /* 3502 ** Usage: sqlite3_column_text STMT column 3503 ** 3504 ** Usage: sqlite3_column_decltype STMT column 3505 ** 3506 ** Usage: sqlite3_column_name STMT column 3507 */ 3508 static int test_stmt_utf8( 3509 void * clientData, /* Pointer to SQLite API function to be invoke */ 3510 Tcl_Interp *interp, 3511 int objc, 3512 Tcl_Obj *CONST objv[] 3513 ){ 3514 sqlite3_stmt *pStmt; 3515 int col; 3516 const char *(*xFunc)(sqlite3_stmt*, int) = clientData; 3517 const char *zRet; 3518 3519 if( objc!=3 ){ 3520 Tcl_AppendResult(interp, "wrong # args: should be \"", 3521 Tcl_GetString(objv[0]), " STMT column", 0); 3522 return TCL_ERROR; 3523 } 3524 3525 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3526 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3527 zRet = xFunc(pStmt, col); 3528 if( zRet ){ 3529 Tcl_SetResult(interp, (char *)zRet, 0); 3530 } 3531 return TCL_OK; 3532 } 3533 3534 static int test_global_recover( 3535 void * clientData, 3536 Tcl_Interp *interp, 3537 int objc, 3538 Tcl_Obj *CONST objv[] 3539 ){ 3540 #ifndef SQLITE_OMIT_GLOBALRECOVER 3541 int rc; 3542 if( objc!=1 ){ 3543 Tcl_WrongNumArgs(interp, 1, objv, ""); 3544 return TCL_ERROR; 3545 } 3546 rc = sqlite3_global_recover(); 3547 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3548 #endif 3549 return TCL_OK; 3550 } 3551 3552 /* 3553 ** Usage: sqlite3_column_text STMT column 3554 ** 3555 ** Usage: sqlite3_column_decltype STMT column 3556 ** 3557 ** Usage: sqlite3_column_name STMT column 3558 */ 3559 static int test_stmt_utf16( 3560 void * clientData, /* Pointer to SQLite API function to be invoked */ 3561 Tcl_Interp *interp, 3562 int objc, 3563 Tcl_Obj *CONST objv[] 3564 ){ 3565 #ifndef SQLITE_OMIT_UTF16 3566 sqlite3_stmt *pStmt; 3567 int col; 3568 Tcl_Obj *pRet; 3569 const void *zName16; 3570 const void *(*xFunc)(sqlite3_stmt*, int) = clientData; 3571 3572 if( objc!=3 ){ 3573 Tcl_AppendResult(interp, "wrong # args: should be \"", 3574 Tcl_GetString(objv[0]), " STMT column", 0); 3575 return TCL_ERROR; 3576 } 3577 3578 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3579 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3580 3581 zName16 = xFunc(pStmt, col); 3582 if( zName16 ){ 3583 pRet = Tcl_NewByteArrayObj(zName16, sqlite3Utf16ByteLen(zName16, -1)+2); 3584 Tcl_SetObjResult(interp, pRet); 3585 } 3586 #endif /* SQLITE_OMIT_UTF16 */ 3587 3588 return TCL_OK; 3589 } 3590 3591 /* 3592 ** Usage: sqlite3_column_int STMT column 3593 ** 3594 ** Usage: sqlite3_column_bytes STMT column 3595 ** 3596 ** Usage: sqlite3_column_bytes16 STMT column 3597 ** 3598 */ 3599 static int test_stmt_int( 3600 void * clientData, /* Pointer to SQLite API function to be invoked */ 3601 Tcl_Interp *interp, 3602 int objc, 3603 Tcl_Obj *CONST objv[] 3604 ){ 3605 sqlite3_stmt *pStmt; 3606 int col; 3607 int (*xFunc)(sqlite3_stmt*, int) = clientData; 3608 3609 if( objc!=3 ){ 3610 Tcl_AppendResult(interp, "wrong # args: should be \"", 3611 Tcl_GetString(objv[0]), " STMT column", 0); 3612 return TCL_ERROR; 3613 } 3614 3615 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; 3616 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; 3617 3618 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); 3619 return TCL_OK; 3620 } 3621 3622 #ifndef SQLITE_OMIT_DISKIO 3623 #if 0 3624 /* 3625 ** Usage: sqlite3OsOpenReadWrite <filename> 3626 */ 3627 static int test_sqlite3OsOpenReadWrite( 3628 void * clientData, 3629 Tcl_Interp *interp, 3630 int objc, 3631 Tcl_Obj *CONST objv[] 3632 ){ 3633 sqlite3_file *pFile; 3634 int rc; 3635 int dummy; 3636 char zBuf[100]; 3637 3638 if( objc!=2 ){ 3639 Tcl_AppendResult(interp, "wrong # args: should be \"", 3640 Tcl_GetString(objv[0]), " filename", 0); 3641 return TCL_ERROR; 3642 } 3643 3644 rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy); 3645 if( rc!=SQLITE_OK ){ 3646 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3647 return TCL_ERROR; 3648 } 3649 sqlite3TestMakePointerStr(interp, zBuf, pFile); 3650 Tcl_SetResult(interp, zBuf, 0); 3651 return TCL_ERROR; 3652 } 3653 3654 /* 3655 ** Usage: sqlite3OsClose <file handle> 3656 */ 3657 static int test_sqlite3OsClose( 3658 void * clientData, 3659 Tcl_Interp *interp, 3660 int objc, 3661 Tcl_Obj *CONST objv[] 3662 ){ 3663 sqlite3_file *pFile; 3664 int rc; 3665 3666 if( objc!=2 ){ 3667 Tcl_AppendResult(interp, "wrong # args: should be \"", 3668 Tcl_GetString(objv[0]), " filehandle", 0); 3669 return TCL_ERROR; 3670 } 3671 3672 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){ 3673 return TCL_ERROR; 3674 } 3675 rc = sqlite3OsClose(&pFile); 3676 if( rc!=SQLITE_OK ){ 3677 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3678 return TCL_ERROR; 3679 } 3680 return TCL_OK; 3681 } 3682 3683 /* 3684 ** Usage: sqlite3OsLock <file handle> <locktype> 3685 */ 3686 static int test_sqlite3OsLock( 3687 void * clientData, 3688 Tcl_Interp *interp, 3689 int objc, 3690 Tcl_Obj *CONST objv[] 3691 ){ 3692 sqlite3_file * pFile; 3693 int rc; 3694 3695 if( objc!=3 ){ 3696 Tcl_AppendResult(interp, "wrong # args: should be \"", 3697 Tcl_GetString(objv[0]), 3698 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0); 3699 return TCL_ERROR; 3700 } 3701 3702 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){ 3703 return TCL_ERROR; 3704 } 3705 3706 if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){ 3707 rc = sqlite3OsLock(pFile, SHARED_LOCK); 3708 } 3709 else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){ 3710 rc = sqlite3OsLock(pFile, RESERVED_LOCK); 3711 } 3712 else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){ 3713 rc = sqlite3OsLock(pFile, PENDING_LOCK); 3714 } 3715 else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){ 3716 rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK); 3717 }else{ 3718 Tcl_AppendResult(interp, "wrong # args: should be \"", 3719 Tcl_GetString(objv[0]), 3720 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0); 3721 return TCL_ERROR; 3722 } 3723 3724 if( rc!=SQLITE_OK ){ 3725 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3726 return TCL_ERROR; 3727 } 3728 return TCL_OK; 3729 } 3730 3731 /* 3732 ** Usage: sqlite3OsUnlock <file handle> 3733 */ 3734 static int test_sqlite3OsUnlock( 3735 void * clientData, 3736 Tcl_Interp *interp, 3737 int objc, 3738 Tcl_Obj *CONST objv[] 3739 ){ 3740 sqlite3_file * pFile; 3741 int rc; 3742 3743 if( objc!=2 ){ 3744 Tcl_AppendResult(interp, "wrong # args: should be \"", 3745 Tcl_GetString(objv[0]), " filehandle", 0); 3746 return TCL_ERROR; 3747 } 3748 3749 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){ 3750 return TCL_ERROR; 3751 } 3752 rc = sqlite3OsUnlock(pFile, NO_LOCK); 3753 if( rc!=SQLITE_OK ){ 3754 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3755 return TCL_ERROR; 3756 } 3757 return TCL_OK; 3758 } 3759 3760 /* 3761 ** Usage: sqlite3OsTempFileName 3762 */ 3763 static int test_sqlite3OsTempFileName( 3764 void * clientData, 3765 Tcl_Interp *interp, 3766 int objc, 3767 Tcl_Obj *CONST objv[] 3768 ){ 3769 char zFile[SQLITE_TEMPNAME_SIZE]; 3770 int rc; 3771 3772 rc = sqlite3OsTempFileName(zFile); 3773 if( rc!=SQLITE_OK ){ 3774 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3775 return TCL_ERROR; 3776 } 3777 Tcl_AppendResult(interp, zFile, 0); 3778 return TCL_OK; 3779 } 3780 #endif 3781 #endif 3782 3783 /* 3784 ** Usage: sqlite_set_magic DB MAGIC-NUMBER 3785 ** 3786 ** Set the db->magic value. This is used to test error recovery logic. 3787 */ 3788 static int sqlite_set_magic( 3789 void * clientData, 3790 Tcl_Interp *interp, 3791 int argc, 3792 char **argv 3793 ){ 3794 sqlite3 *db; 3795 if( argc!=3 ){ 3796 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 3797 " DB MAGIC", 0); 3798 return TCL_ERROR; 3799 } 3800 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3801 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ 3802 db->magic = SQLITE_MAGIC_OPEN; 3803 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ 3804 db->magic = SQLITE_MAGIC_CLOSED; 3805 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ 3806 db->magic = SQLITE_MAGIC_BUSY; 3807 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ 3808 db->magic = SQLITE_MAGIC_ERROR; 3809 }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){ 3810 return TCL_ERROR; 3811 } 3812 return TCL_OK; 3813 } 3814 3815 /* 3816 ** Usage: sqlite3_interrupt DB 3817 ** 3818 ** Trigger an interrupt on DB 3819 */ 3820 static int test_interrupt( 3821 void * clientData, 3822 Tcl_Interp *interp, 3823 int argc, 3824 char **argv 3825 ){ 3826 sqlite3 *db; 3827 if( argc!=2 ){ 3828 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); 3829 return TCL_ERROR; 3830 } 3831 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3832 sqlite3_interrupt(db); 3833 return TCL_OK; 3834 } 3835 3836 static u8 *sqlite3_stack_baseline = 0; 3837 3838 /* 3839 ** Fill the stack with a known bitpattern. 3840 */ 3841 static void prepStack(void){ 3842 int i; 3843 u32 bigBuf[65536]; 3844 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef; 3845 sqlite3_stack_baseline = (u8*)&bigBuf[65536]; 3846 } 3847 3848 /* 3849 ** Get the current stack depth. Used for debugging only. 3850 */ 3851 u64 sqlite3StackDepth(void){ 3852 u8 x; 3853 return (u64)(sqlite3_stack_baseline - &x); 3854 } 3855 3856 /* 3857 ** Usage: sqlite3_stack_used DB SQL 3858 ** 3859 ** Try to measure the amount of stack space used by a call to sqlite3_exec 3860 */ 3861 static int test_stack_used( 3862 void * clientData, 3863 Tcl_Interp *interp, 3864 int argc, 3865 char **argv 3866 ){ 3867 sqlite3 *db; 3868 int i; 3869 if( argc!=3 ){ 3870 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 3871 " DB SQL", 0); 3872 return TCL_ERROR; 3873 } 3874 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3875 prepStack(); 3876 (void)sqlite3_exec(db, argv[2], 0, 0, 0); 3877 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} 3878 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); 3879 return TCL_OK; 3880 } 3881 3882 /* 3883 ** Usage: sqlite_delete_function DB function-name 3884 ** 3885 ** Delete the user function 'function-name' from database handle DB. It 3886 ** is assumed that the user function was created as UTF8, any number of 3887 ** arguments (the way the TCL interface does it). 3888 */ 3889 static int delete_function( 3890 void * clientData, 3891 Tcl_Interp *interp, 3892 int argc, 3893 char **argv 3894 ){ 3895 int rc; 3896 sqlite3 *db; 3897 if( argc!=3 ){ 3898 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 3899 " DB function-name", 0); 3900 return TCL_ERROR; 3901 } 3902 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3903 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); 3904 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3905 return TCL_OK; 3906 } 3907 3908 /* 3909 ** Usage: sqlite_delete_collation DB collation-name 3910 ** 3911 ** Delete the collation sequence 'collation-name' from database handle 3912 ** DB. It is assumed that the collation sequence was created as UTF8 (the 3913 ** way the TCL interface does it). 3914 */ 3915 static int delete_collation( 3916 void * clientData, 3917 Tcl_Interp *interp, 3918 int argc, 3919 char **argv 3920 ){ 3921 int rc; 3922 sqlite3 *db; 3923 if( argc!=3 ){ 3924 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 3925 " DB function-name", 0); 3926 return TCL_ERROR; 3927 } 3928 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3929 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); 3930 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); 3931 return TCL_OK; 3932 } 3933 3934 /* 3935 ** Usage: sqlite3_get_autocommit DB 3936 ** 3937 ** Return true if the database DB is currently in auto-commit mode. 3938 ** Return false if not. 3939 */ 3940 static int get_autocommit( 3941 void * clientData, 3942 Tcl_Interp *interp, 3943 int argc, 3944 char **argv 3945 ){ 3946 char zBuf[30]; 3947 sqlite3 *db; 3948 if( argc!=2 ){ 3949 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 3950 " DB", 0); 3951 return TCL_ERROR; 3952 } 3953 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3954 sprintf(zBuf, "%d", sqlite3_get_autocommit(db)); 3955 Tcl_AppendResult(interp, zBuf, 0); 3956 return TCL_OK; 3957 } 3958 3959 /* 3960 ** Usage: sqlite3_busy_timeout DB MS 3961 ** 3962 ** Set the busy timeout. This is more easily done using the timeout 3963 ** method of the TCL interface. But we need a way to test the case 3964 ** where it returns SQLITE_MISUSE. 3965 */ 3966 static int test_busy_timeout( 3967 void * clientData, 3968 Tcl_Interp *interp, 3969 int argc, 3970 char **argv 3971 ){ 3972 int rc, ms; 3973 sqlite3 *db; 3974 if( argc!=3 ){ 3975 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 3976 " DB", 0); 3977 return TCL_ERROR; 3978 } 3979 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; 3980 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; 3981 rc = sqlite3_busy_timeout(db, ms); 3982 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); 3983 return TCL_OK; 3984 } 3985 3986 /* 3987 ** Usage: tcl_variable_type VARIABLENAME 3988 ** 3989 ** Return the name of the internal representation for the 3990 ** value of the given variable. 3991 */ 3992 static int tcl_variable_type( 3993 void * clientData, 3994 Tcl_Interp *interp, 3995 int objc, 3996 Tcl_Obj *CONST objv[] 3997 ){ 3998 Tcl_Obj *pVar; 3999 if( objc!=2 ){ 4000 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); 4001 return TCL_ERROR; 4002 } 4003 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); 4004 if( pVar==0 ) return TCL_ERROR; 4005 if( pVar->typePtr ){ 4006 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); 4007 } 4008 return TCL_OK; 4009 } 4010 4011 /* 4012 ** Usage: sqlite3_release_memory ?N? 4013 ** 4014 ** Attempt to release memory currently held but not actually required. 4015 ** The integer N is the number of bytes we are trying to release. The 4016 ** return value is the amount of memory actually released. 4017 */ 4018 static int test_release_memory( 4019 void * clientData, 4020 Tcl_Interp *interp, 4021 int objc, 4022 Tcl_Obj *CONST objv[] 4023 ){ 4024 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) 4025 int N; 4026 int amt; 4027 if( objc!=1 && objc!=2 ){ 4028 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4029 return TCL_ERROR; 4030 } 4031 if( objc==2 ){ 4032 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4033 }else{ 4034 N = -1; 4035 } 4036 amt = sqlite3_release_memory(N); 4037 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); 4038 #endif 4039 return TCL_OK; 4040 } 4041 4042 /* 4043 ** Usage: sqlite3_soft_heap_limit ?N? 4044 ** 4045 ** Query or set the soft heap limit for the current thread. The 4046 ** limit is only changed if the N is present. The previous limit 4047 ** is returned. 4048 */ 4049 static int test_soft_heap_limit( 4050 void * clientData, 4051 Tcl_Interp *interp, 4052 int objc, 4053 Tcl_Obj *CONST objv[] 4054 ){ 4055 static int softHeapLimit = 0; 4056 int amt; 4057 if( objc!=1 && objc!=2 ){ 4058 Tcl_WrongNumArgs(interp, 1, objv, "?N?"); 4059 return TCL_ERROR; 4060 } 4061 amt = softHeapLimit; 4062 if( objc==2 ){ 4063 int N; 4064 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; 4065 sqlite3_soft_heap_limit(N); 4066 softHeapLimit = N; 4067 } 4068 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); 4069 return TCL_OK; 4070 } 4071 4072 /* 4073 ** Usage: sqlite3_clear_tsd_memdebug 4074 ** 4075 ** Clear all of the MEMDEBUG information out of thread-specific data. 4076 ** This will allow it to be deallocated. 4077 */ 4078 static int test_clear_tsd_memdebug( 4079 void * clientData, 4080 Tcl_Interp *interp, 4081 int objc, 4082 Tcl_Obj *CONST objv[] 4083 ){ 4084 return TCL_OK; 4085 } 4086 4087 /* 4088 ** Usage: sqlite3_tsd_release 4089 ** 4090 ** Call sqlite3ReleaseThreadData. 4091 */ 4092 static int test_tsd_release( 4093 void * clientData, 4094 Tcl_Interp *interp, 4095 int objc, 4096 Tcl_Obj *CONST objv[] 4097 ){ 4098 return TCL_OK; 4099 } 4100 4101 /* 4102 ** Usage: sqlite3_thread_cleanup 4103 ** 4104 ** Call the sqlite3_thread_cleanup API. 4105 */ 4106 static int test_thread_cleanup( 4107 void * clientData, 4108 Tcl_Interp *interp, 4109 int objc, 4110 Tcl_Obj *CONST objv[] 4111 ){ 4112 sqlite3_thread_cleanup(); 4113 return TCL_OK; 4114 } 4115 4116 4117 /* 4118 ** Usage: sqlite3_pager_refcounts DB 4119 ** 4120 ** Return a list of numbers which are the PagerRefcount for all 4121 ** pagers on each database connection. 4122 */ 4123 static int test_pager_refcounts( 4124 void * clientData, 4125 Tcl_Interp *interp, 4126 int objc, 4127 Tcl_Obj *CONST objv[] 4128 ){ 4129 sqlite3 *db; 4130 int i; 4131 int v, *a; 4132 Tcl_Obj *pResult; 4133 4134 if( objc!=2 ){ 4135 Tcl_AppendResult(interp, "wrong # args: should be \"", 4136 Tcl_GetStringFromObj(objv[0], 0), " DB", 0); 4137 return TCL_ERROR; 4138 } 4139 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 4140 pResult = Tcl_NewObj(); 4141 for(i=0; i<db->nDb; i++){ 4142 if( db->aDb[i].pBt==0 ){ 4143 v = -1; 4144 }else{ 4145 sqlite3_mutex_enter(db->mutex); 4146 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); 4147 v = a[0]; 4148 sqlite3_mutex_leave(db->mutex); 4149 } 4150 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); 4151 } 4152 Tcl_SetObjResult(interp, pResult); 4153 return TCL_OK; 4154 } 4155 4156 4157 /* 4158 ** tclcmd: working_64bit_int 4159 ** 4160 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This 4161 ** leads to a number of test failures. The present command checks the 4162 ** TCL build to see whether or not it supports 64-bit integers. It 4163 ** returns TRUE if it does and FALSE if not. 4164 ** 4165 ** This command is used to warn users that their TCL build is defective 4166 ** and that the errors they are seeing in the test scripts might be 4167 ** a result of their defective TCL rather than problems in SQLite. 4168 */ 4169 static int working_64bit_int( 4170 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 4171 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 4172 int objc, /* Number of arguments */ 4173 Tcl_Obj *CONST objv[] /* Command arguments */ 4174 ){ 4175 Tcl_Obj *pTestObj; 4176 int working = 0; 4177 4178 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); 4179 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; 4180 Tcl_DecrRefCount(pTestObj); 4181 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); 4182 return TCL_OK; 4183 } 4184 4185 4186 /* 4187 ** Register commands with the TCL interpreter. 4188 */ 4189 int Sqlitetest1_Init(Tcl_Interp *interp){ 4190 extern int sqlite3_search_count; 4191 extern int sqlite3_interrupt_count; 4192 extern int sqlite3_open_file_count; 4193 extern int sqlite3_sort_count; 4194 extern int sqlite3_current_time; 4195 extern int sqlite3_max_blobsize; 4196 extern int sqlite3BtreeSharedCacheReport(void*, 4197 Tcl_Interp*,int,Tcl_Obj*CONST*); 4198 static struct { 4199 char *zName; 4200 Tcl_CmdProc *xProc; 4201 } aCmd[] = { 4202 { "db_enter", (Tcl_CmdProc*)db_enter }, 4203 { "db_leave", (Tcl_CmdProc*)db_leave }, 4204 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, 4205 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, 4206 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, 4207 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, 4208 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, 4209 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, 4210 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, 4211 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, 4212 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, 4213 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, 4214 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, 4215 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, 4216 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, 4217 { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, 4218 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, 4219 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, 4220 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, 4221 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, 4222 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, 4223 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, 4224 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, 4225 { "sqlite_bind", (Tcl_CmdProc*)test_bind }, 4226 { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, 4227 { "sqlite3_key", (Tcl_CmdProc*)test_key }, 4228 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, 4229 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, 4230 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, 4231 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, 4232 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, 4233 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, 4234 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, 4235 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, 4236 { "printf", (Tcl_CmdProc*)test_printf }, 4237 { "sqlite3_io_trace", (Tcl_CmdProc*)test_io_trace }, 4238 }; 4239 static struct { 4240 char *zName; 4241 Tcl_ObjCmdProc *xProc; 4242 void *clientData; 4243 } aObjCmd[] = { 4244 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, 4245 { "sqlite3_bind_int", test_bind_int, 0 }, 4246 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, 4247 { "sqlite3_bind_int64", test_bind_int64, 0 }, 4248 { "sqlite3_bind_double", test_bind_double, 0 }, 4249 { "sqlite3_bind_null", test_bind_null ,0 }, 4250 { "sqlite3_bind_text", test_bind_text ,0 }, 4251 { "sqlite3_bind_text16", test_bind_text16 ,0 }, 4252 { "sqlite3_bind_blob", test_bind_blob ,0 }, 4253 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, 4254 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, 4255 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, 4256 { "sqlite3_clear_bindings", test_clear_bindings, 0}, 4257 { "sqlite3_sleep", test_sleep, 0}, 4258 { "sqlite3_errcode", test_errcode ,0 }, 4259 { "sqlite3_errmsg", test_errmsg ,0 }, 4260 { "sqlite3_errmsg16", test_errmsg16 ,0 }, 4261 { "sqlite3_open", test_open ,0 }, 4262 { "sqlite3_open16", test_open16 ,0 }, 4263 { "sqlite3_complete16", test_complete16 ,0 }, 4264 4265 { "sqlite3_prepare", test_prepare ,0 }, 4266 { "sqlite3_prepare16", test_prepare16 ,0 }, 4267 { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, 4268 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, 4269 { "sqlite3_finalize", test_finalize ,0 }, 4270 { "sqlite3_reset", test_reset ,0 }, 4271 { "sqlite3_expired", test_expired ,0 }, 4272 { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, 4273 { "sqlite3_changes", test_changes ,0 }, 4274 { "sqlite3_step", test_step ,0 }, 4275 4276 { "sqlite3_release_memory", test_release_memory, 0}, 4277 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, 4278 { "sqlite3_clear_tsd_memdebug", test_clear_tsd_memdebug, 0}, 4279 { "sqlite3_tsd_release", test_tsd_release, 0}, 4280 { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, 4281 { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, 4282 4283 { "sqlite3_load_extension", test_load_extension, 0}, 4284 { "sqlite3_enable_load_extension", test_enable_load, 0}, 4285 { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, 4286 4287 /* sqlite3_column_*() API */ 4288 { "sqlite3_column_count", test_column_count ,0 }, 4289 { "sqlite3_data_count", test_data_count ,0 }, 4290 { "sqlite3_column_type", test_column_type ,0 }, 4291 { "sqlite3_column_blob", test_column_blob ,0 }, 4292 { "sqlite3_column_double", test_column_double ,0 }, 4293 { "sqlite3_column_int64", test_column_int64 ,0 }, 4294 { "sqlite3_column_text", test_stmt_utf8, sqlite3_column_text }, 4295 { "sqlite3_column_decltype", test_stmt_utf8, sqlite3_column_decltype }, 4296 { "sqlite3_column_name", test_stmt_utf8, sqlite3_column_name }, 4297 { "sqlite3_column_int", test_stmt_int, sqlite3_column_int }, 4298 { "sqlite3_column_bytes", test_stmt_int, sqlite3_column_bytes }, 4299 #ifdef SQLITE_ENABLE_COLUMN_METADATA 4300 { "sqlite3_column_database_name", test_stmt_utf8, sqlite3_column_database_name}, 4301 { "sqlite3_column_table_name", test_stmt_utf8, sqlite3_column_table_name}, 4302 { "sqlite3_column_origin_name", test_stmt_utf8, sqlite3_column_origin_name}, 4303 #endif 4304 4305 #ifndef SQLITE_OMIT_UTF16 4306 { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 }, 4307 { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 }, 4308 { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16}, 4309 { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 }, 4310 { "add_alignment_test_collations", add_alignment_test_collations, 0 }, 4311 #ifdef SQLITE_ENABLE_COLUMN_METADATA 4312 {"sqlite3_column_database_name16", 4313 test_stmt_utf16, sqlite3_column_database_name16}, 4314 {"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16}, 4315 {"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16}, 4316 #endif 4317 #endif 4318 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, 4319 { "sqlite3_global_recover", test_global_recover, 0 }, 4320 { "working_64bit_int", working_64bit_int, 0 }, 4321 4322 /* Functions from os.h */ 4323 #ifndef SQLITE_OMIT_DISKIO 4324 #if 0 4325 { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 }, 4326 { "sqlite3OsClose", test_sqlite3OsClose, 0 }, 4327 { "sqlite3OsLock", test_sqlite3OsLock, 0 }, 4328 { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 }, 4329 4330 /* Custom test interfaces */ 4331 { "sqlite3OsUnlock", test_sqlite3OsUnlock, 0 }, 4332 #endif 4333 #endif 4334 #ifndef SQLITE_OMIT_UTF16 4335 { "add_test_collate", test_collate, 0 }, 4336 { "add_test_collate_needed", test_collate_needed, 0 }, 4337 { "add_test_function", test_function, 0 }, 4338 #endif 4339 { "sqlite3_test_errstr", test_errstr, 0 }, 4340 { "tcl_variable_type", tcl_variable_type, 0 }, 4341 #ifndef SQLITE_OMIT_SHARED_CACHE 4342 { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, 4343 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, 4344 #endif 4345 { "sqlite3_libversion_number", test_libversion_number, 0 }, 4346 #ifdef SQLITE_ENABLE_COLUMN_METADATA 4347 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, 4348 #endif 4349 #ifndef SQLITE_OMIT_INCRBLOB 4350 { "sqlite3_blob_read", test_blob_read, 0 }, 4351 { "sqlite3_blob_write", test_blob_write, 0 }, 4352 #endif 4353 }; 4354 static int bitmask_size = sizeof(Bitmask)*8; 4355 int i; 4356 extern int sqlite3_where_trace; 4357 extern int sqlite3_sync_count, sqlite3_fullsync_count; 4358 extern int sqlite3_opentemp_count; 4359 extern int sqlite3_like_count; 4360 extern int sqlite3_xferopt_count; 4361 extern int sqlite3_pager_readdb_count; 4362 extern int sqlite3_pager_writedb_count; 4363 extern int sqlite3_pager_writej_count; 4364 extern int sqlite3_pager_pgfree_count; 4365 #if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE 4366 extern int threadsOverrideEachOthersLocks; 4367 #endif 4368 #if OS_WIN 4369 extern int sqlite3_os_type; 4370 #endif 4371 #ifdef SQLITE_DEBUG 4372 extern int sqlite3_os_trace; 4373 extern int sqlite3_vdbe_addop_trace; 4374 #endif 4375 #ifdef SQLITE_TEST 4376 extern char sqlite3_query_plan[]; 4377 static char *query_plan = sqlite3_query_plan; 4378 #endif 4379 4380 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 4381 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 4382 } 4383 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 4384 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 4385 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 4386 } 4387 Tcl_LinkVar(interp, "sqlite_search_count", 4388 (char*)&sqlite3_search_count, TCL_LINK_INT); 4389 Tcl_LinkVar(interp, "sqlite_sort_count", 4390 (char*)&sqlite3_sort_count, TCL_LINK_INT); 4391 Tcl_LinkVar(interp, "sqlite3_max_blobsize", 4392 (char*)&sqlite3_max_blobsize, TCL_LINK_INT); 4393 Tcl_LinkVar(interp, "sqlite_like_count", 4394 (char*)&sqlite3_like_count, TCL_LINK_INT); 4395 Tcl_LinkVar(interp, "sqlite_interrupt_count", 4396 (char*)&sqlite3_interrupt_count, TCL_LINK_INT); 4397 Tcl_LinkVar(interp, "sqlite_open_file_count", 4398 (char*)&sqlite3_open_file_count, TCL_LINK_INT); 4399 Tcl_LinkVar(interp, "sqlite_current_time", 4400 (char*)&sqlite3_current_time, TCL_LINK_INT); 4401 Tcl_LinkVar(interp, "sqlite3_xferopt_count", 4402 (char*)&sqlite3_xferopt_count, TCL_LINK_INT); 4403 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", 4404 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); 4405 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", 4406 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); 4407 Tcl_LinkVar(interp, "sqlite3_pager_writej_count", 4408 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); 4409 Tcl_LinkVar(interp, "sqlite3_pager_pgfree_count", 4410 (char*)&sqlite3_pager_pgfree_count, TCL_LINK_INT); 4411 #ifndef SQLITE_OMIT_UTF16 4412 Tcl_LinkVar(interp, "unaligned_string_counter", 4413 (char*)&unaligned_string_counter, TCL_LINK_INT); 4414 #endif 4415 #if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE 4416 Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks", 4417 (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT); 4418 #endif 4419 #ifndef SQLITE_OMIT_UTF16 4420 Tcl_LinkVar(interp, "sqlite_last_needed_collation", 4421 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 4422 #endif 4423 #if OS_WIN 4424 Tcl_LinkVar(interp, "sqlite_os_type", 4425 (char*)&sqlite3_os_type, TCL_LINK_INT); 4426 #endif 4427 #ifdef SQLITE_TEST 4428 Tcl_LinkVar(interp, "sqlite_query_plan", 4429 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); 4430 #endif 4431 #ifdef SQLITE_DEBUG 4432 Tcl_LinkVar(interp, "sqlite_addop_trace", 4433 (char*)&sqlite3_vdbe_addop_trace, TCL_LINK_INT); 4434 Tcl_LinkVar(interp, "sqlite_where_trace", 4435 (char*)&sqlite3_where_trace, TCL_LINK_INT); 4436 Tcl_LinkVar(interp, "sqlite_os_trace", 4437 (char*)&sqlite3_os_trace, TCL_LINK_INT); 4438 #endif 4439 #ifndef SQLITE_OMIT_DISKIO 4440 Tcl_LinkVar(interp, "sqlite_opentemp_count", 4441 (char*)&sqlite3_opentemp_count, TCL_LINK_INT); 4442 #endif 4443 Tcl_LinkVar(interp, "sqlite_static_bind_value", 4444 (char*)&sqlite_static_bind_value, TCL_LINK_STRING); 4445 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", 4446 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); 4447 Tcl_LinkVar(interp, "sqlite_temp_directory", 4448 (char*)&sqlite3_temp_directory, TCL_LINK_STRING); 4449 Tcl_LinkVar(interp, "bitmask_size", 4450 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); 4451 #if OS_UNIX 4452 Tcl_LinkVar(interp, "sqlite_sync_count", 4453 (char*)&sqlite3_sync_count, TCL_LINK_INT); 4454 Tcl_LinkVar(interp, "sqlite_fullsync_count", 4455 (char*)&sqlite3_fullsync_count, TCL_LINK_INT); 4456 #endif /* OS_UNIX */ 4457 4458 return TCL_OK; 4459 } 4460