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