1 /* 2 ** 2008 March 19 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 ** implements new SQL functions used by the test scripts. 14 */ 15 #include "sqlite3.h" 16 #if defined(INCLUDE_SQLITE_TCL_H) 17 # include "sqlite_tcl.h" 18 #else 19 # include "tcl.h" 20 #endif 21 #include <stdlib.h> 22 #include <string.h> 23 #include <assert.h> 24 25 #include "sqliteInt.h" 26 #include "vdbeInt.h" 27 28 /* 29 ** Allocate nByte bytes of space using sqlite3_malloc(). If the 30 ** allocation fails, call sqlite3_result_error_nomem() to notify 31 ** the database handle that malloc() has failed. 32 */ 33 static void *testContextMalloc(sqlite3_context *context, int nByte){ 34 char *z = sqlite3_malloc(nByte); 35 if( !z && nByte>0 ){ 36 sqlite3_result_error_nomem(context); 37 } 38 return z; 39 } 40 41 /* 42 ** This function generates a string of random characters. Used for 43 ** generating test data. 44 */ 45 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 46 static const unsigned char zSrc[] = 47 "abcdefghijklmnopqrstuvwxyz" 48 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 49 "0123456789" 50 ".-!,:*^+=_|?/<> "; 51 int iMin, iMax, n, r, i; 52 unsigned char zBuf[1000]; 53 54 /* It used to be possible to call randstr() with any number of arguments, 55 ** but now it is registered with SQLite as requiring exactly 2. 56 */ 57 assert(argc==2); 58 59 iMin = sqlite3_value_int(argv[0]); 60 if( iMin<0 ) iMin = 0; 61 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 62 iMax = sqlite3_value_int(argv[1]); 63 if( iMax<iMin ) iMax = iMin; 64 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 65 n = iMin; 66 if( iMax>iMin ){ 67 sqlite3_randomness(sizeof(r), &r); 68 r &= 0x7fffffff; 69 n += r%(iMax + 1 - iMin); 70 } 71 assert( n<sizeof(zBuf) ); 72 sqlite3_randomness(n, zBuf); 73 for(i=0; i<n; i++){ 74 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 75 } 76 zBuf[n] = 0; 77 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 78 } 79 80 /* 81 ** The following two SQL functions are used to test returning a text 82 ** result with a destructor. Function 'test_destructor' takes one argument 83 ** and returns the same argument interpreted as TEXT. A destructor is 84 ** passed with the sqlite3_result_text() call. 85 ** 86 ** SQL function 'test_destructor_count' returns the number of outstanding 87 ** allocations made by 'test_destructor'; 88 ** 89 ** WARNING: Not threadsafe. 90 */ 91 static int test_destructor_count_var = 0; 92 static void destructor(void *p){ 93 char *zVal = (char *)p; 94 assert(zVal); 95 zVal--; 96 sqlite3_free(zVal); 97 test_destructor_count_var--; 98 } 99 static void test_destructor( 100 sqlite3_context *pCtx, 101 int nArg, 102 sqlite3_value **argv 103 ){ 104 char *zVal; 105 int len; 106 107 test_destructor_count_var++; 108 assert( nArg==1 ); 109 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 110 len = sqlite3_value_bytes(argv[0]); 111 zVal = testContextMalloc(pCtx, len+3); 112 if( !zVal ){ 113 return; 114 } 115 zVal[len+1] = 0; 116 zVal[len+2] = 0; 117 zVal++; 118 memcpy(zVal, sqlite3_value_text(argv[0]), len); 119 sqlite3_result_text(pCtx, zVal, -1, destructor); 120 } 121 #ifndef SQLITE_OMIT_UTF16 122 static void test_destructor16( 123 sqlite3_context *pCtx, 124 int nArg, 125 sqlite3_value **argv 126 ){ 127 char *zVal; 128 int len; 129 130 test_destructor_count_var++; 131 assert( nArg==1 ); 132 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 133 len = sqlite3_value_bytes16(argv[0]); 134 zVal = testContextMalloc(pCtx, len+3); 135 if( !zVal ){ 136 return; 137 } 138 zVal[len+1] = 0; 139 zVal[len+2] = 0; 140 zVal++; 141 memcpy(zVal, sqlite3_value_text16(argv[0]), len); 142 sqlite3_result_text16(pCtx, zVal, -1, destructor); 143 } 144 #endif 145 static void test_destructor_count( 146 sqlite3_context *pCtx, 147 int nArg, 148 sqlite3_value **argv 149 ){ 150 sqlite3_result_int(pCtx, test_destructor_count_var); 151 } 152 153 /* 154 ** The following aggregate function, test_agg_errmsg16(), takes zero 155 ** arguments. It returns the text value returned by the sqlite3_errmsg16() 156 ** API function. 157 */ 158 #ifndef SQLITE_UNTESTABLE 159 void sqlite3BeginBenignMalloc(void); 160 void sqlite3EndBenignMalloc(void); 161 #else 162 #define sqlite3BeginBenignMalloc() 163 #define sqlite3EndBenignMalloc() 164 #endif 165 static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ 166 } 167 static void test_agg_errmsg16_final(sqlite3_context *ctx){ 168 #ifndef SQLITE_OMIT_UTF16 169 const void *z; 170 sqlite3 * db = sqlite3_context_db_handle(ctx); 171 sqlite3_aggregate_context(ctx, 2048); 172 z = sqlite3_errmsg16(db); 173 sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); 174 #endif 175 } 176 177 /* 178 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 179 ** interface. 180 ** 181 ** The test_auxdata() SQL function attempts to register each of its arguments 182 ** as auxiliary data. If there are no prior registrations of aux data for 183 ** that argument (meaning the argument is not a constant or this is its first 184 ** call) then the result for that argument is 0. If there is a prior 185 ** registration, the result for that argument is 1. The overall result 186 ** is the individual argument results separated by spaces. 187 */ 188 static void free_test_auxdata(void *p) {sqlite3_free(p);} 189 static void test_auxdata( 190 sqlite3_context *pCtx, 191 int nArg, 192 sqlite3_value **argv 193 ){ 194 int i; 195 char *zRet = testContextMalloc(pCtx, nArg*2); 196 if( !zRet ) return; 197 memset(zRet, 0, nArg*2); 198 for(i=0; i<nArg; i++){ 199 char const *z = (char*)sqlite3_value_text(argv[i]); 200 if( z ){ 201 int n; 202 char *zAux = sqlite3_get_auxdata(pCtx, i); 203 if( zAux ){ 204 zRet[i*2] = '1'; 205 assert( strcmp(zAux,z)==0 ); 206 }else { 207 zRet[i*2] = '0'; 208 } 209 n = (int)strlen(z) + 1; 210 zAux = testContextMalloc(pCtx, n); 211 if( zAux ){ 212 memcpy(zAux, z, n); 213 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 214 } 215 zRet[i*2+1] = ' '; 216 } 217 } 218 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 219 } 220 221 /* 222 ** A function to test error reporting from user functions. This function 223 ** returns a copy of its first argument as the error message. If the 224 ** second argument exists, it becomes the error code. 225 */ 226 static void test_error( 227 sqlite3_context *pCtx, 228 int nArg, 229 sqlite3_value **argv 230 ){ 231 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); 232 if( nArg==2 ){ 233 sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); 234 } 235 } 236 237 /* 238 ** Implementation of the counter(X) function. If X is an integer 239 ** constant, then the first invocation will return X. The second X+1. 240 ** and so forth. Can be used (for example) to provide a sequence number 241 ** in a result set. 242 */ 243 static void counterFunc( 244 sqlite3_context *pCtx, /* Function context */ 245 int nArg, /* Number of function arguments */ 246 sqlite3_value **argv /* Values for all function arguments */ 247 ){ 248 int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); 249 if( pCounter==0 ){ 250 pCounter = sqlite3_malloc( sizeof(*pCounter) ); 251 if( pCounter==0 ){ 252 sqlite3_result_error_nomem(pCtx); 253 return; 254 } 255 *pCounter = sqlite3_value_int(argv[0]); 256 sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); 257 }else{ 258 ++*pCounter; 259 } 260 sqlite3_result_int(pCtx, *pCounter); 261 } 262 263 264 /* 265 ** This function takes two arguments. It performance UTF-8/16 type 266 ** conversions on the first argument then returns a copy of the second 267 ** argument. 268 ** 269 ** This function is used in cases such as the following: 270 ** 271 ** SELECT test_isolation(x,x) FROM t1; 272 ** 273 ** We want to verify that the type conversions that occur on the 274 ** first argument do not invalidate the second argument. 275 */ 276 static void test_isolation( 277 sqlite3_context *pCtx, 278 int nArg, 279 sqlite3_value **argv 280 ){ 281 #ifndef SQLITE_OMIT_UTF16 282 sqlite3_value_text16(argv[0]); 283 sqlite3_value_text(argv[0]); 284 sqlite3_value_text16(argv[0]); 285 sqlite3_value_text(argv[0]); 286 #endif 287 sqlite3_result_value(pCtx, argv[1]); 288 } 289 290 /* 291 ** Invoke an SQL statement recursively. The function result is the 292 ** first column of the first row of the result set. 293 */ 294 static void test_eval( 295 sqlite3_context *pCtx, 296 int nArg, 297 sqlite3_value **argv 298 ){ 299 sqlite3_stmt *pStmt; 300 int rc; 301 sqlite3 *db = sqlite3_context_db_handle(pCtx); 302 const char *zSql; 303 304 zSql = (char*)sqlite3_value_text(argv[0]); 305 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 306 if( rc==SQLITE_OK ){ 307 rc = sqlite3_step(pStmt); 308 if( rc==SQLITE_ROW ){ 309 sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); 310 } 311 rc = sqlite3_finalize(pStmt); 312 } 313 if( rc ){ 314 char *zErr; 315 assert( pStmt==0 ); 316 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); 317 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); 318 sqlite3_result_error_code(pCtx, rc); 319 } 320 } 321 322 323 /* 324 ** convert one character from hex to binary 325 */ 326 static int testHexChar(char c){ 327 if( c>='0' && c<='9' ){ 328 return c - '0'; 329 }else if( c>='a' && c<='f' ){ 330 return c - 'a' + 10; 331 }else if( c>='A' && c<='F' ){ 332 return c - 'A' + 10; 333 } 334 return 0; 335 } 336 337 /* 338 ** Convert hex to binary. 339 */ 340 static void testHexToBin(const char *zIn, char *zOut){ 341 while( zIn[0] && zIn[1] ){ 342 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); 343 zIn += 2; 344 } 345 } 346 347 /* 348 ** hex_to_utf16be(HEX) 349 ** 350 ** Convert the input string from HEX into binary. Then return the 351 ** result using sqlite3_result_text16le(). 352 */ 353 #ifndef SQLITE_OMIT_UTF16 354 static void testHexToUtf16be( 355 sqlite3_context *pCtx, 356 int nArg, 357 sqlite3_value **argv 358 ){ 359 int n; 360 const char *zIn; 361 char *zOut; 362 assert( nArg==1 ); 363 n = sqlite3_value_bytes(argv[0]); 364 zIn = (const char*)sqlite3_value_text(argv[0]); 365 zOut = sqlite3_malloc( n/2 ); 366 if( zOut==0 ){ 367 sqlite3_result_error_nomem(pCtx); 368 }else{ 369 testHexToBin(zIn, zOut); 370 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); 371 } 372 } 373 #endif 374 375 /* 376 ** hex_to_utf8(HEX) 377 ** 378 ** Convert the input string from HEX into binary. Then return the 379 ** result using sqlite3_result_text16le(). 380 */ 381 static void testHexToUtf8( 382 sqlite3_context *pCtx, 383 int nArg, 384 sqlite3_value **argv 385 ){ 386 int n; 387 const char *zIn; 388 char *zOut; 389 assert( nArg==1 ); 390 n = sqlite3_value_bytes(argv[0]); 391 zIn = (const char*)sqlite3_value_text(argv[0]); 392 zOut = sqlite3_malloc( n/2 ); 393 if( zOut==0 ){ 394 sqlite3_result_error_nomem(pCtx); 395 }else{ 396 testHexToBin(zIn, zOut); 397 sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); 398 } 399 } 400 401 /* 402 ** hex_to_utf16le(HEX) 403 ** 404 ** Convert the input string from HEX into binary. Then return the 405 ** result using sqlite3_result_text16le(). 406 */ 407 #ifndef SQLITE_OMIT_UTF16 408 static void testHexToUtf16le( 409 sqlite3_context *pCtx, 410 int nArg, 411 sqlite3_value **argv 412 ){ 413 int n; 414 const char *zIn; 415 char *zOut; 416 assert( nArg==1 ); 417 n = sqlite3_value_bytes(argv[0]); 418 zIn = (const char*)sqlite3_value_text(argv[0]); 419 zOut = sqlite3_malloc( n/2 ); 420 if( zOut==0 ){ 421 sqlite3_result_error_nomem(pCtx); 422 }else{ 423 testHexToBin(zIn, zOut); 424 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); 425 } 426 } 427 #endif 428 429 /* 430 ** SQL function: real2hex(X) 431 ** 432 ** If argument X is a real number, then convert it into a string which is 433 ** the big-endian hexadecimal representation of the ieee754 encoding of 434 ** that number. If X is not a real number, return NULL. 435 */ 436 static void real2hex( 437 sqlite3_context *context, 438 int argc, 439 sqlite3_value **argv 440 ){ 441 union { 442 sqlite3_uint64 i; 443 double r; 444 unsigned char x[8]; 445 } v; 446 char zOut[20]; 447 int i; 448 int bigEndian; 449 v.i = 1; 450 bigEndian = v.x[0]==0; 451 v.r = sqlite3_value_double(argv[0]); 452 for(i=0; i<8; i++){ 453 if( bigEndian ){ 454 zOut[i*2] = "0123456789abcdef"[v.x[i]>>4]; 455 zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; 456 }else{ 457 zOut[14-i*2] = "0123456789abcdef"[v.x[i]>>4]; 458 zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; 459 } 460 } 461 zOut[16] = 0; 462 sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); 463 } 464 465 /* 466 ** test_extract(record, field) 467 ** 468 ** This function implements an SQL user-function that accepts a blob 469 ** containing a formatted database record as the first argument. The 470 ** second argument is the index of the field within that record to 471 ** extract and return. 472 */ 473 static void test_extract( 474 sqlite3_context *context, 475 int argc, 476 sqlite3_value **argv 477 ){ 478 sqlite3 *db = sqlite3_context_db_handle(context); 479 u8 *pRec; 480 u8 *pEndHdr; /* Points to one byte past record header */ 481 u8 *pHdr; /* Current point in record header */ 482 u8 *pBody; /* Current point in record data */ 483 u64 nHdr; /* Bytes in record header */ 484 int iIdx; /* Required field */ 485 int iCurrent = 0; /* Current field */ 486 487 assert( argc==2 ); 488 pRec = (u8*)sqlite3_value_blob(argv[0]); 489 iIdx = sqlite3_value_int(argv[1]); 490 491 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); 492 pBody = pEndHdr = &pRec[nHdr]; 493 494 for(iCurrent=0; pHdr<pEndHdr && iCurrent<=iIdx; iCurrent++){ 495 u64 iSerialType; 496 Mem mem; 497 498 memset(&mem, 0, sizeof(mem)); 499 mem.db = db; 500 mem.enc = ENC(db); 501 pHdr += sqlite3GetVarint(pHdr, &iSerialType); 502 sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); 503 pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType); 504 505 if( iCurrent==iIdx ){ 506 sqlite3_result_value(context, &mem); 507 } 508 509 if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc); 510 } 511 } 512 513 /* 514 ** test_decode(record) 515 ** 516 ** This function implements an SQL user-function that accepts a blob 517 ** containing a formatted database record as its only argument. It returns 518 ** a tcl list (type SQLITE_TEXT) containing each of the values stored 519 ** in the record. 520 */ 521 static void test_decode( 522 sqlite3_context *context, 523 int argc, 524 sqlite3_value **argv 525 ){ 526 sqlite3 *db = sqlite3_context_db_handle(context); 527 u8 *pRec; 528 u8 *pEndHdr; /* Points to one byte past record header */ 529 u8 *pHdr; /* Current point in record header */ 530 u8 *pBody; /* Current point in record data */ 531 u64 nHdr; /* Bytes in record header */ 532 Tcl_Obj *pRet; /* Return value */ 533 534 pRet = Tcl_NewObj(); 535 Tcl_IncrRefCount(pRet); 536 537 assert( argc==1 ); 538 pRec = (u8*)sqlite3_value_blob(argv[0]); 539 540 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); 541 pBody = pEndHdr = &pRec[nHdr]; 542 while( pHdr<pEndHdr ){ 543 Tcl_Obj *pVal = 0; 544 u64 iSerialType; 545 Mem mem; 546 547 memset(&mem, 0, sizeof(mem)); 548 mem.db = db; 549 mem.enc = ENC(db); 550 pHdr += sqlite3GetVarint(pHdr, &iSerialType); 551 sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); 552 pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType); 553 554 switch( sqlite3_value_type(&mem) ){ 555 case SQLITE_TEXT: 556 pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1); 557 break; 558 559 case SQLITE_BLOB: { 560 char hexdigit[] = { 561 '0', '1', '2', '3', '4', '5', '6', '7', 562 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 563 }; 564 int n = sqlite3_value_bytes(&mem); 565 u8 *z = (u8*)sqlite3_value_blob(&mem); 566 int i; 567 pVal = Tcl_NewStringObj("x'", -1); 568 for(i=0; i<n; i++){ 569 char hex[3]; 570 hex[0] = hexdigit[((z[i] >> 4) & 0x0F)]; 571 hex[1] = hexdigit[(z[i] & 0x0F)]; 572 hex[2] = '\0'; 573 Tcl_AppendStringsToObj(pVal, hex, 0); 574 } 575 Tcl_AppendStringsToObj(pVal, "'", 0); 576 break; 577 } 578 579 case SQLITE_FLOAT: 580 pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem)); 581 break; 582 583 case SQLITE_INTEGER: 584 pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem)); 585 break; 586 587 case SQLITE_NULL: 588 pVal = Tcl_NewStringObj("NULL", -1); 589 break; 590 591 default: 592 assert( 0 ); 593 } 594 595 Tcl_ListObjAppendElement(0, pRet, pVal); 596 597 if( mem.szMalloc ){ 598 sqlite3DbFree(db, mem.zMalloc); 599 } 600 } 601 602 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); 603 Tcl_DecrRefCount(pRet); 604 } 605 606 /* 607 ** test_zeroblob(N) 608 ** 609 ** The implementation of scalar SQL function "test_zeroblob()". This is 610 ** similar to the built-in zeroblob() function, except that it does not 611 ** check that the integer parameter is within range before passing it 612 ** to sqlite3_result_zeroblob(). 613 */ 614 static void test_zeroblob( 615 sqlite3_context *context, 616 int argc, 617 sqlite3_value **argv 618 ){ 619 int nZero = sqlite3_value_int(argv[0]); 620 sqlite3_result_zeroblob(context, nZero); 621 } 622 623 /* test_getsubtype(V) 624 ** 625 ** Return the subtype for value V. 626 */ 627 static void test_getsubtype( 628 sqlite3_context *context, 629 int argc, 630 sqlite3_value **argv 631 ){ 632 sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0])); 633 } 634 635 /* test_frombind(A,B,C,...) 636 ** 637 ** Return an integer bitmask that has a bit set for every argument 638 ** (up to the first 63 arguments) that originates from a bind a parameter. 639 */ 640 static void test_frombind( 641 sqlite3_context *context, 642 int argc, 643 sqlite3_value **argv 644 ){ 645 sqlite3_uint64 m = 0; 646 int i; 647 for(i=0; i<argc && i<63; i++){ 648 if( sqlite3_value_frombind(argv[i]) ) m |= ((sqlite3_uint64)1)<<i; 649 } 650 sqlite3_result_int64(context, (sqlite3_int64)m); 651 } 652 653 /* test_setsubtype(V, T) 654 ** 655 ** Return the value V with its subtype changed to T 656 */ 657 static void test_setsubtype( 658 sqlite3_context *context, 659 int argc, 660 sqlite3_value **argv 661 ){ 662 sqlite3_result_value(context, argv[0]); 663 sqlite3_result_subtype(context, (unsigned int)sqlite3_value_int(argv[1])); 664 } 665 666 static int registerTestFunctions( 667 sqlite3 *db, 668 char **pzErrMsg, 669 const sqlite3_api_routines *pThunk 670 ){ 671 static const struct { 672 char *zName; 673 signed char nArg; 674 unsigned int eTextRep; /* 1: UTF-16. 0: UTF-8 */ 675 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 676 } aFuncs[] = { 677 { "randstr", 2, SQLITE_UTF8, randStr }, 678 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, 679 #ifndef SQLITE_OMIT_UTF16 680 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, 681 { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, 682 { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, 683 #endif 684 { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, 685 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, 686 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, 687 { "test_error", 1, SQLITE_UTF8, test_error}, 688 { "test_error", 2, SQLITE_UTF8, test_error}, 689 { "test_eval", 1, SQLITE_UTF8, test_eval}, 690 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, 691 { "test_counter", 1, SQLITE_UTF8, counterFunc}, 692 { "real2hex", 1, SQLITE_UTF8, real2hex}, 693 { "test_decode", 1, SQLITE_UTF8, test_decode}, 694 { "test_extract", 2, SQLITE_UTF8, test_extract}, 695 { "test_zeroblob", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, test_zeroblob}, 696 { "test_getsubtype", 1, SQLITE_UTF8, test_getsubtype}, 697 { "test_setsubtype", 2, SQLITE_UTF8, test_setsubtype}, 698 { "test_frombind", -1, SQLITE_UTF8, test_frombind}, 699 }; 700 int i; 701 702 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 703 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 704 aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); 705 } 706 707 sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, 708 test_agg_errmsg16_step, test_agg_errmsg16_final); 709 710 return SQLITE_OK; 711 } 712 713 /* 714 ** TCLCMD: autoinstall_test_functions 715 ** 716 ** Invoke this TCL command to use sqlite3_auto_extension() to cause 717 ** the standard set of test functions to be loaded into each new 718 ** database connection. 719 */ 720 static int SQLITE_TCLAPI autoinstall_test_funcs( 721 void * clientData, 722 Tcl_Interp *interp, 723 int objc, 724 Tcl_Obj *CONST objv[] 725 ){ 726 extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); 727 int rc = sqlite3_auto_extension((void(*)(void))registerTestFunctions); 728 if( rc==SQLITE_OK ){ 729 rc = sqlite3_auto_extension((void(*)(void))Md5_Register); 730 } 731 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 732 return TCL_OK; 733 } 734 735 /* 736 ** A bogus step function and finalizer function. 737 */ 738 static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} 739 static void tFinal(sqlite3_context *a){} 740 741 742 /* 743 ** tclcmd: abuse_create_function 744 ** 745 ** Make various calls to sqlite3_create_function that do not have valid 746 ** parameters. Verify that the error condition is detected and reported. 747 */ 748 static int SQLITE_TCLAPI abuse_create_function( 749 void * clientData, 750 Tcl_Interp *interp, 751 int objc, 752 Tcl_Obj *CONST objv[] 753 ){ 754 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); 755 sqlite3 *db; 756 int rc; 757 int mxArg; 758 759 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 760 761 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); 762 if( rc!=SQLITE_MISUSE ) goto abuse_err; 763 764 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); 765 if( rc!=SQLITE_MISUSE ) goto abuse_err; 766 767 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); 768 if( rc!=SQLITE_MISUSE) goto abuse_err; 769 770 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); 771 if( rc!=SQLITE_MISUSE ) goto abuse_err; 772 773 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); 774 if( rc!=SQLITE_MISUSE ) goto abuse_err; 775 776 rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); 777 if( rc!=SQLITE_MISUSE ) goto abuse_err; 778 779 rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); 780 if( rc!=SQLITE_MISUSE ) goto abuse_err; 781 782 rc = sqlite3_create_function(db, "funcxx" 783 "_123456789_123456789_123456789_123456789_123456789" 784 "_123456789_123456789_123456789_123456789_123456789" 785 "_123456789_123456789_123456789_123456789_123456789" 786 "_123456789_123456789_123456789_123456789_123456789" 787 "_123456789_123456789_123456789_123456789_123456789", 788 1, SQLITE_UTF8, 0, tStep, 0, 0); 789 if( rc!=SQLITE_MISUSE ) goto abuse_err; 790 791 /* This last function registration should actually work. Generate 792 ** a no-op function (that always returns NULL) and which has the 793 ** maximum-length function name and the maximum number of parameters. 794 */ 795 sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); 796 mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); 797 rc = sqlite3_create_function(db, "nullx" 798 "_123456789_123456789_123456789_123456789_123456789" 799 "_123456789_123456789_123456789_123456789_123456789" 800 "_123456789_123456789_123456789_123456789_123456789" 801 "_123456789_123456789_123456789_123456789_123456789" 802 "_123456789_123456789_123456789_123456789_123456789", 803 mxArg, SQLITE_UTF8, 0, tStep, 0, 0); 804 if( rc!=SQLITE_OK ) goto abuse_err; 805 806 return TCL_OK; 807 808 abuse_err: 809 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", 810 (char*)0); 811 return TCL_ERROR; 812 } 813 814 815 /* 816 ** SQLite user defined function to use with matchinfo() to calculate the 817 ** relevancy of an FTS match. The value returned is the relevancy score 818 ** (a real value greater than or equal to zero). A larger value indicates 819 ** a more relevant document. 820 ** 821 ** The overall relevancy returned is the sum of the relevancies of each 822 ** column value in the FTS table. The relevancy of a column value is the 823 ** sum of the following for each reportable phrase in the FTS query: 824 ** 825 ** (<hit count> / <global hit count>) * <column weight> 826 ** 827 ** where <hit count> is the number of instances of the phrase in the 828 ** column value of the current row and <global hit count> is the number 829 ** of instances of the phrase in the same column of all rows in the FTS 830 ** table. The <column weight> is a weighting factor assigned to each 831 ** column by the caller (see below). 832 ** 833 ** The first argument to this function must be the return value of the FTS 834 ** matchinfo() function. Following this must be one argument for each column 835 ** of the FTS table containing a numeric weight factor for the corresponding 836 ** column. Example: 837 ** 838 ** CREATE VIRTUAL TABLE documents USING fts3(title, content) 839 ** 840 ** The following query returns the docids of documents that match the full-text 841 ** query <query> sorted from most to least relevant. When calculating 842 ** relevance, query term instances in the 'title' column are given twice the 843 ** weighting of those in the 'content' column. 844 ** 845 ** SELECT docid FROM documents 846 ** WHERE documents MATCH <query> 847 ** ORDER BY rank(matchinfo(documents), 1.0, 0.5) DESC 848 */ 849 static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ 850 int *aMatchinfo; /* Return value of matchinfo() */ 851 int nMatchinfo; /* Number of elements in aMatchinfo[] */ 852 int nCol = 0; /* Number of columns in the table */ 853 int nPhrase = 0; /* Number of phrases in the query */ 854 int iPhrase; /* Current phrase */ 855 double score = 0.0; /* Value to return */ 856 857 assert( sizeof(int)==4 ); 858 859 /* Check that the number of arguments passed to this function is correct. 860 ** If not, jump to wrong_number_args. Set aMatchinfo to point to the array 861 ** of unsigned integer values returned by FTS function matchinfo. Set 862 ** nPhrase to contain the number of reportable phrases in the users full-text 863 ** query, and nCol to the number of columns in the table. Then check that the 864 ** size of the matchinfo blob is as expected. Return an error if it is not. 865 */ 866 if( nVal<1 ) goto wrong_number_args; 867 aMatchinfo = (int*)sqlite3_value_blob(apVal[0]); 868 nMatchinfo = sqlite3_value_bytes(apVal[0]) / sizeof(int); 869 if( nMatchinfo>=2 ){ 870 nPhrase = aMatchinfo[0]; 871 nCol = aMatchinfo[1]; 872 } 873 if( nMatchinfo!=(2+3*nCol*nPhrase) ){ 874 sqlite3_result_error(pCtx, 875 "invalid matchinfo blob passed to function rank()", -1); 876 return; 877 } 878 if( nVal!=(1+nCol) ) goto wrong_number_args; 879 880 /* Iterate through each phrase in the users query. */ 881 for(iPhrase=0; iPhrase<nPhrase; iPhrase++){ 882 int iCol; /* Current column */ 883 884 /* Now iterate through each column in the users query. For each column, 885 ** increment the relevancy score by: 886 ** 887 ** (<hit count> / <global hit count>) * <column weight> 888 ** 889 ** aPhraseinfo[] points to the start of the data for phrase iPhrase. So 890 ** the hit count and global hit counts for each column are found in 891 ** aPhraseinfo[iCol*3] and aPhraseinfo[iCol*3+1], respectively. 892 */ 893 int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3]; 894 for(iCol=0; iCol<nCol; iCol++){ 895 int nHitCount = aPhraseinfo[3*iCol]; 896 int nGlobalHitCount = aPhraseinfo[3*iCol+1]; 897 double weight = sqlite3_value_double(apVal[iCol+1]); 898 if( nHitCount>0 ){ 899 score += ((double)nHitCount / (double)nGlobalHitCount) * weight; 900 } 901 } 902 } 903 904 sqlite3_result_double(pCtx, score); 905 return; 906 907 /* Jump here if the wrong number of arguments are passed to this function */ 908 wrong_number_args: 909 sqlite3_result_error(pCtx, "wrong number of arguments to function rank()", -1); 910 } 911 912 static int SQLITE_TCLAPI install_fts3_rank_function( 913 void * clientData, 914 Tcl_Interp *interp, 915 int objc, 916 Tcl_Obj *CONST objv[] 917 ){ 918 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); 919 sqlite3 *db; 920 921 if( objc!=2 ){ 922 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 923 return TCL_ERROR; 924 } 925 926 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 927 sqlite3_create_function(db, "rank", -1, SQLITE_UTF8, 0, rankfunc, 0, 0); 928 return TCL_OK; 929 } 930 931 932 /* 933 ** Register commands with the TCL interpreter. 934 */ 935 int Sqlitetest_func_Init(Tcl_Interp *interp){ 936 static struct { 937 char *zName; 938 Tcl_ObjCmdProc *xProc; 939 } aObjCmd[] = { 940 { "autoinstall_test_functions", autoinstall_test_funcs }, 941 { "abuse_create_function", abuse_create_function }, 942 { "install_fts3_rank_function", install_fts3_rank_function }, 943 }; 944 int i; 945 extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); 946 947 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 948 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); 949 } 950 sqlite3_initialize(); 951 sqlite3_auto_extension((void(*)(void))registerTestFunctions); 952 sqlite3_auto_extension((void(*)(void))Md5_Register); 953 return TCL_OK; 954 } 955