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