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