Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/test_func.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / test_func.c` 957 lines ยท 28688 bytes Raw Blame History 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 #include "tclsqlite.h" 17 #include 18 #include 19 #include 20 21 #include "sqliteInt.h" 22 #include "vdbeInt.h" 23 24 /* 25 ** Allocate nByte bytes of space using sqlite3_malloc(). If the 26 ** allocation fails, call sqlite3_result_error_nomem() to notify 27 ** the database handle that malloc() has failed. 28 */ 29 static void *testContextMalloc(sqlite3_context *context, int nByte){ 30 char *z = sqlite3_malloc(nByte); 31 if( !z && nByte>0 ){ 32 sqlite3_result_error_nomem(context); 33 } 34 return z; 35 } 36 37 /* 38 ** This function generates a string of random characters. Used for 39 ** generating test data. 40 */ 41 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 42 static const unsigned char zSrc[] = 43 "abcdefghijklmnopqrstuvwxyz" 44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 45 "`0123456789`" 46 ".-!,:*^+=_|?/<> "; 47 int iMin, iMax, n, r, i; 48 unsigned char zBuf[1000]; 49 50 /* It used to be possible to call randstr() with any number of arguments, 51 ** but now it is registered with SQLite as requiring exactly 2. 52 */ 53 assert(argc==2); 54 55 iMin = sqlite3_value_int(argv[0]); 56 if( iMin<0 ) iMin = 0; 57 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 58 iMax = sqlite3_value_int(argv[1]); 59 if( iMax=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 61 n = iMin; 62 if( iMax>iMin ){ 63 sqlite3_randomness(sizeof(r), &r); 64 r &= 0x7fffffff; 65 n += r%(iMax + 1 - iMin); 66 } 67 assert( n='0' && c<='9' ){ 330 return c - '0'; 331 }else if( c>='a' && c<='f' ){ 332 return c - 'a' + 10; 333 }else if( c>='A' && c<='F' ){ 334 return c - 'A' + 10; 335 } 336 return 0; 337 } 338 339 /* 340 ** Convert hex to binary. 341 */ 342 static void testHexToBin(const char *zIn, char *zOut){ 343 while( zIn[0] && zIn[1] ){ 344 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); 345 zIn += 2; 346 } 347 } 348 349 /* 350 ** hex_to_utf16be(HEX) 351 ** 352 ** Convert the input string from HEX into binary. Then return the 353 ** result using sqlite3_result_text16le(). 354 */ 355 #ifndef SQLITE_OMIT_UTF16 356 static void testHexToUtf16be( 357 sqlite3_context *pCtx, 358 int nArg, 359 sqlite3_value **argv 360 ){ 361 int n; 362 const char *zIn; 363 char *zOut; 364 assert( nArg==1 ); 365 n = sqlite3_value_bytes(argv[0]); 366 zIn = (const char*)sqlite3_value_text(argv[0]); 367 zOut = sqlite3_malloc( n/2 ); 368 if( zOut==0 ){ 369 sqlite3_result_error_nomem(pCtx); 370 }else{ 371 testHexToBin(zIn, zOut); 372 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); 373 } 374 } 375 #endif 376 377 /* 378 ** hex_to_utf8(HEX) 379 ** 380 ** Convert the input string from HEX into binary. Then return the 381 ** result using sqlite3_result_text16le(). 382 */ 383 static void testHexToUtf8( 384 sqlite3_context *pCtx, 385 int nArg, 386 sqlite3_value **argv 387 ){ 388 int n; 389 const char *zIn; 390 char *zOut; 391 assert( nArg==1 ); 392 n = sqlite3_value_bytes(argv[0]); 393 zIn = (const char*)sqlite3_value_text(argv[0]); 394 zOut = sqlite3_malloc( n/2 ); 395 if( zOut==0 ){ 396 sqlite3_result_error_nomem(pCtx); 397 }else{ 398 testHexToBin(zIn, zOut); 399 sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); 400 } 401 } 402 403 /* 404 ** hex_to_utf16le(HEX) 405 ** 406 ** Convert the input string from HEX into binary. Then return the 407 ** result using sqlite3_result_text16le(). 408 */ 409 #ifndef SQLITE_OMIT_UTF16 410 static void testHexToUtf16le( 411 sqlite3_context *pCtx, 412 int nArg, 413 sqlite3_value **argv 414 ){ 415 int n; 416 const char *zIn; 417 char *zOut; 418 assert( nArg==1 ); 419 n = sqlite3_value_bytes(argv[0]); 420 zIn = (const char*)sqlite3_value_text(argv[0]); 421 zOut = sqlite3_malloc( n/2 ); 422 if( zOut==0 ){ 423 sqlite3_result_error_nomem(pCtx); 424 }else{ 425 testHexToBin(zIn, zOut); 426 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); 427 } 428 } 429 #endif 430 431 /* 432 ** SQL function: real2hex(X) 433 ** 434 ** If argument X is a real number, then convert it into a string which is 435 ** the big-endian hexadecimal representation of the ieee754 encoding of 436 ** that number. If X is not a real number, return NULL. 437 */ 438 static void real2hex( 439 sqlite3_context *context, 440 int argc, 441 sqlite3_value **argv 442 ){ 443 union { 444 sqlite3_uint64 i; 445 double r; 446 unsigned char x[8]; 447 } v; 448 char zOut[20]; 449 int i; 450 int bigEndian; 451 v.i = 1; 452 bigEndian = v.x[0]==0; 453 v.r = sqlite3_value_double(argv[0]); 454 for(i=0; i<8; i++){ 455 if( bigEndian ){ 456 zOut[i*2] = "`0123456789abcdef`"[v.x[i]>>4]; 457 zOut[i*2+1] = "`0123456789abcdef`"[v.x[i]&0xf]; 458 }else{ 459 zOut[14-i*2] = "`0123456789abcdef`"[v.x[i]>>4]; 460 zOut[14-i*2+1] = "`0123456789abcdef`"[v.x[i]&0xf]; 461 } 462 } 463 zOut[16] = 0; 464 sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); 465 } 466 467 /* 468 ** test_extract(record, field) 469 ** 470 ** This function implements an SQL user-function that accepts a blob 471 ** containing a formatted database record as the first argument. The 472 ** second argument is the index of the field within that record to 473 ** extract and return. 474 */ 475 static void test_extract( 476 sqlite3_context *context, 477 int argc, 478 sqlite3_value **argv 479 ){ 480 sqlite3 *db = sqlite3_context_db_handle(context); 481 u8 *pRec; 482 u8 *pEndHdr; /* Points to one byte past record header */ 483 u8 *pHdr; /* Current point in record header */ 484 u8 *pBody; /* Current point in record data */ 485 u64 nHdr; /* Bytes in record header */ 486 int iIdx; /* Required field */ 487 int iCurrent = 0; /* Current field */ 488 489 assert( argc==2 ); 490 pRec = (u8*)sqlite3_value_blob(argv[0]); 491 iIdx = sqlite3_value_int(argv[1]); 492 493 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); 494 pBody = pEndHdr = &pRec[nHdr]; 495 496 for(iCurrent=0; pHdr> 4) & 0x0F)]; 573 hex[1] = hexdigit[(z[i] & 0x0F)]; 574 hex[2] = '\0'; 575 Tcl_AppendStringsToObj(pVal, hex, 0); 576 } 577 Tcl_AppendStringsToObj(pVal, "'", 0); 578 break; 579 } 580 581 case SQLITE_FLOAT: 582 pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem)); 583 break; 584 585 case SQLITE_INTEGER: 586 pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem)); 587 break; 588 589 case SQLITE_NULL: 590 pVal = Tcl_NewStringObj("NULL", -1); 591 break; 592 593 default: 594 assert( 0 ); 595 } 596 597 Tcl_ListObjAppendElement(0, pRet, pVal); 598 599 if( mem.szMalloc ){ 600 sqlite3DbFree(db, mem.zMalloc); 601 } 602 } 603 604 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); 605 Tcl_DecrRefCount(pRet); 606 } 607 608 /* 609 ** test_zeroblob(N) 610 ** 611 ** The implementation of scalar SQL function "test_zeroblob()". This is 612 ** similar to the built-in zeroblob() function, except that it does not 613 ** check that the integer parameter is within range before passing it 614 ** to sqlite3_result_zeroblob(). 615 */ 616 static void test_zeroblob( 617 sqlite3_context *context, 618 int argc, 619 sqlite3_value **argv 620 ){ 621 int nZero = sqlite3_value_int(argv[0]); 622 sqlite3_result_zeroblob(context, nZero); 623 } 624 625 /* test_getsubtype(V) 626 ** 627 ** Return the subtype for value V. 628 */ 629 static void test_getsubtype( 630 sqlite3_context *context, 631 int argc, 632 sqlite3_value **argv 633 ){ 634 sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0])); 635 } 636 637 /* test_frombind(A,B,C,...) 638 ** 639 ** Return an integer bitmask that has a bit set for every argument 640 ** (up to the first 63 arguments) that originates from a bind a parameter. 641 */ 642 static void test_frombind( 643 sqlite3_context *context, 644 int argc, 645 sqlite3_value **argv 646 ){ 647 sqlite3_uint64 m = 0; 648 int i; 649 for(i=0; i / ) * 829 ** 830 ** where is the number of instances of the phrase in the 831 ** column value of the current row and is the number 832 ** of instances of the phrase in the same column of all rows in the FTS 833 ** table. The is a weighting factor assigned to each 834 ** column by the caller (see below). 835 ** 836 ** The first argument to this function must be the return value of the FTS 837 ** matchinfo() function. Following this must be one argument for each column 838 ** of the FTS table containing a numeric weight factor for the corresponding 839 ** column. Example: 840 ** 841 ** CREATE VIRTUAL TABLE documents USING fts3(title, content) 842 ** 843 ** The following query returns the docids of documents that match the full-text 844 ** query sorted from most to least relevant. When calculating 845 ** relevance, query term instances in the 'title' column are given twice the 846 ** weighting of those in the 'content' column. 847 ** 848 ** SELECT docid FROM documents 849 ** WHERE documents MATCH 850 ** ORDER BY rank(matchinfo(documents), 1.0, 0.5) DESC 851 */ 852 static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ 853 int *aMatchinfo; /* Return value of matchinfo() */ 854 int nMatchinfo; /* Number of elements in aMatchinfo[] */ 855 int nCol = 0; /* Number of columns in the table */ 856 int nPhrase = 0; /* Number of phrases in the query */ 857 int iPhrase; /* Current phrase */ 858 double score = 0.0; /* Value to return */ 859 860 assert( sizeof(int)==4 ); 861 862 /* Check that the number of arguments passed to this function is correct. 863 ** If not, jump to wrong_number_args. Set aMatchinfo to point to the array 864 ** of unsigned integer values returned by FTS function matchinfo. Set 865 ** nPhrase to contain the number of reportable phrases in the users full-text 866 ** query, and nCol to the number of columns in the table. Then check that the 867 ** size of the matchinfo blob is as expected. Return an error if it is not. 868 */ 869 if( nVal<1 ) goto wrong_number_args; 870 aMatchinfo = (int*)sqlite3_value_blob(apVal[0]); 871 nMatchinfo = sqlite3_value_bytes(apVal[0]) / sizeof(int); 872 if( nMatchinfo>=2 ){ 873 nPhrase = aMatchinfo[0]; 874 nCol = aMatchinfo[1]; 875 } 876 if( nMatchinfo!=(2+3*nCol*nPhrase) ){ 877 sqlite3_result_error(pCtx, 878 "invalid matchinfo blob passed to function rank()", -1); 879 return; 880 } 881 if( nVal!=(1+nCol) ) goto wrong_number_args; 882 883 /* Iterate through each phrase in the users query. */ 884 for(iPhrase=0; iPhrase / ) * 891 ** 892 ** aPhraseinfo[] points to the start of the data for phrase iPhrase. So 893 ** the hit count and global hit counts for each column are found in 894 ** aPhraseinfo[iCol*3] and aPhraseinfo[iCol*3+1], respectively. 895 */ 896 int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3]; 897 for(iCol=0; iCol0 ){ 902 score += ((double)nHitCount / (double)nGlobalHitCount) * weight; 903 } 904 } 905 } 906 907 sqlite3_result_double(pCtx, score); 908 return; 909 910 /* Jump here if the wrong number of arguments are passed to this function */ 911 wrong_number_args: 912 sqlite3_result_error(pCtx, "wrong number of arguments to function rank()", -1); 913 } 914 915 static int SQLITE_TCLAPI install_fts3_rank_function( 916 void * clientData, 917 Tcl_Interp *interp, 918 int objc, 919 Tcl_Obj *CONST objv[] 920 ){ 921 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); 922 sqlite3 *db; 923 924 if( objc!=2 ){ 925 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 926 return TCL_ERROR; 927 } 928 929 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 930 sqlite3_create_function(db, "rank", -1, SQLITE_UTF8, 0, rankfunc, 0, 0); 931 return TCL_OK; 932 } 933 934 935 /* 936 ** Register commands with the TCL interpreter. 937 */ 938 int Sqlitetest_func_Init(Tcl_Interp *interp){ 939 static struct { 940 char *zName; 941 Tcl_ObjCmdProc *xProc; 942 } aObjCmd[] = { 943 { "autoinstall_test_functions", autoinstall_test_funcs }, 944 { "abuse_create_function", abuse_create_function }, 945 { "install_fts3_rank_function", install_fts3_rank_function }, 946 }; 947 int i; 948 extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); 949 950 for(i=0; i