1 /* 2 ** 2002 February 23 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 ** This file contains the C functions that implement various SQL 13 ** functions of SQLite. 14 ** 15 ** There is only one exported symbol in this file - the function 16 ** sqliteRegisterBuildinFunctions() found at the bottom of the file. 17 ** All other code has file scope. 18 ** 19 ** $Id: func.c,v 1.145 2007/05/04 13:15:56 drh Exp $ 20 */ 21 #include "sqliteInt.h" 22 #include <ctype.h> 23 /* #include <math.h> */ 24 #include <stdlib.h> 25 #include <assert.h> 26 #include "vdbeInt.h" 27 #include "os.h" 28 29 /* 30 ** Return the collating function associated with a function. 31 */ 32 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 33 return context->pColl; 34 } 35 36 /* 37 ** Implementation of the non-aggregate min() and max() functions 38 */ 39 static void minmaxFunc( 40 sqlite3_context *context, 41 int argc, 42 sqlite3_value **argv 43 ){ 44 int i; 45 int mask; /* 0 for min() or 0xffffffff for max() */ 46 int iBest; 47 CollSeq *pColl; 48 49 if( argc==0 ) return; 50 mask = sqlite3_user_data(context)==0 ? 0 : -1; 51 pColl = sqlite3GetFuncCollSeq(context); 52 assert( pColl ); 53 assert( mask==-1 || mask==0 ); 54 iBest = 0; 55 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 56 for(i=1; i<argc; i++){ 57 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 58 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 59 iBest = i; 60 } 61 } 62 sqlite3_result_value(context, argv[iBest]); 63 } 64 65 /* 66 ** Return the type of the argument. 67 */ 68 static void typeofFunc( 69 sqlite3_context *context, 70 int argc, 71 sqlite3_value **argv 72 ){ 73 const char *z = 0; 74 switch( sqlite3_value_type(argv[0]) ){ 75 case SQLITE_NULL: z = "null"; break; 76 case SQLITE_INTEGER: z = "integer"; break; 77 case SQLITE_TEXT: z = "text"; break; 78 case SQLITE_FLOAT: z = "real"; break; 79 case SQLITE_BLOB: z = "blob"; break; 80 } 81 sqlite3_result_text(context, z, -1, SQLITE_STATIC); 82 } 83 84 85 /* 86 ** Implementation of the length() function 87 */ 88 static void lengthFunc( 89 sqlite3_context *context, 90 int argc, 91 sqlite3_value **argv 92 ){ 93 int len; 94 95 assert( argc==1 ); 96 switch( sqlite3_value_type(argv[0]) ){ 97 case SQLITE_BLOB: 98 case SQLITE_INTEGER: 99 case SQLITE_FLOAT: { 100 sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 101 break; 102 } 103 case SQLITE_TEXT: { 104 const unsigned char *z = sqlite3_value_text(argv[0]); 105 if( z==0 ) return; 106 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } 107 sqlite3_result_int(context, len); 108 break; 109 } 110 default: { 111 sqlite3_result_null(context); 112 break; 113 } 114 } 115 } 116 117 /* 118 ** Implementation of the abs() function 119 */ 120 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 121 assert( argc==1 ); 122 switch( sqlite3_value_type(argv[0]) ){ 123 case SQLITE_INTEGER: { 124 i64 iVal = sqlite3_value_int64(argv[0]); 125 if( iVal<0 ){ 126 if( (iVal<<1)==0 ){ 127 sqlite3_result_error(context, "integer overflow", -1); 128 return; 129 } 130 iVal = -iVal; 131 } 132 sqlite3_result_int64(context, iVal); 133 break; 134 } 135 case SQLITE_NULL: { 136 sqlite3_result_null(context); 137 break; 138 } 139 default: { 140 double rVal = sqlite3_value_double(argv[0]); 141 if( rVal<0 ) rVal = -rVal; 142 sqlite3_result_double(context, rVal); 143 break; 144 } 145 } 146 } 147 148 /* 149 ** Implementation of the substr() function 150 */ 151 static void substrFunc( 152 sqlite3_context *context, 153 int argc, 154 sqlite3_value **argv 155 ){ 156 const unsigned char *z; 157 const unsigned char *z2; 158 int i; 159 int p1, p2, len; 160 161 assert( argc==3 ); 162 z = sqlite3_value_text(argv[0]); 163 if( z==0 ) return; 164 p1 = sqlite3_value_int(argv[1]); 165 p2 = sqlite3_value_int(argv[2]); 166 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } 167 if( p1<0 ){ 168 p1 += len; 169 if( p1<0 ){ 170 p2 += p1; 171 p1 = 0; 172 } 173 }else if( p1>0 ){ 174 p1--; 175 } 176 if( p1+p2>len ){ 177 p2 = len-p1; 178 } 179 for(i=0; i<p1 && z[i]; i++){ 180 if( (z[i]&0xc0)==0x80 ) p1++; 181 } 182 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } 183 for(; i<p1+p2 && z[i]; i++){ 184 if( (z[i]&0xc0)==0x80 ) p2++; 185 } 186 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } 187 if( p2<0 ) p2 = 0; 188 sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); 189 } 190 191 /* 192 ** Implementation of the round() function 193 */ 194 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 195 int n = 0; 196 double r; 197 char zBuf[500]; /* larger than the %f representation of the largest double */ 198 assert( argc==1 || argc==2 ); 199 if( argc==2 ){ 200 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 201 n = sqlite3_value_int(argv[1]); 202 if( n>30 ) n = 30; 203 if( n<0 ) n = 0; 204 } 205 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 206 r = sqlite3_value_double(argv[0]); 207 sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); 208 sqlite3AtoF(zBuf, &r); 209 sqlite3_result_double(context, r); 210 } 211 212 /* 213 ** Implementation of the upper() and lower() SQL functions. 214 */ 215 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 216 char *z1; 217 const char *z2; 218 int i, n; 219 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 220 n = sqlite3_value_bytes(argv[0]); 221 z2 = (char*)sqlite3_value_text(argv[0]); 222 if( z2 ){ 223 z1 = sqlite3_malloc(n+1); 224 if( z1 ){ 225 memcpy(z1, z2, n+1); 226 for(i=0; z1[i]; i++){ 227 z1[i] = toupper(z1[i]); 228 } 229 sqlite3_result_text(context, z1, -1, sqlite3_free); 230 } 231 } 232 } 233 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 234 char *z1; 235 const char *z2; 236 int i, n; 237 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 238 n = sqlite3_value_bytes(argv[0]); 239 z2 = (char*)sqlite3_value_text(argv[0]); 240 if( z2 ){ 241 z1 = sqlite3_malloc(n+1); 242 if( z1 ){ 243 memcpy(z1, z2, n+1); 244 for(i=0; z1[i]; i++){ 245 z1[i] = tolower(z1[i]); 246 } 247 sqlite3_result_text(context, z1, -1, sqlite3_free); 248 } 249 } 250 } 251 252 /* 253 ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 254 ** All three do the same thing. They return the first non-NULL 255 ** argument. 256 */ 257 static void ifnullFunc( 258 sqlite3_context *context, 259 int argc, 260 sqlite3_value **argv 261 ){ 262 int i; 263 for(i=0; i<argc; i++){ 264 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 265 sqlite3_result_value(context, argv[i]); 266 break; 267 } 268 } 269 } 270 271 /* 272 ** Implementation of random(). Return a random integer. 273 */ 274 static void randomFunc( 275 sqlite3_context *context, 276 int argc, 277 sqlite3_value **argv 278 ){ 279 sqlite_int64 r; 280 sqlite3Randomness(sizeof(r), &r); 281 if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ 282 /* can always do abs() of the result */ 283 sqlite3_result_int64(context, r); 284 } 285 286 /* 287 ** Implementation of randomblob(N). Return a random blob 288 ** that is N bytes long. 289 */ 290 static void randomBlob( 291 sqlite3_context *context, 292 int argc, 293 sqlite3_value **argv 294 ){ 295 int n; 296 unsigned char *p; 297 assert( argc==1 ); 298 n = sqlite3_value_int(argv[0]); 299 if( n<1 ) n = 1; 300 p = sqlite3_malloc(n); 301 sqlite3Randomness(n, p); 302 sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 303 } 304 305 /* 306 ** Implementation of the last_insert_rowid() SQL function. The return 307 ** value is the same as the sqlite3_last_insert_rowid() API function. 308 */ 309 static void last_insert_rowid( 310 sqlite3_context *context, 311 int arg, 312 sqlite3_value **argv 313 ){ 314 sqlite3 *db = sqlite3_user_data(context); 315 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 316 } 317 318 /* 319 ** Implementation of the changes() SQL function. The return value is the 320 ** same as the sqlite3_changes() API function. 321 */ 322 static void changes( 323 sqlite3_context *context, 324 int arg, 325 sqlite3_value **argv 326 ){ 327 sqlite3 *db = sqlite3_user_data(context); 328 sqlite3_result_int(context, sqlite3_changes(db)); 329 } 330 331 /* 332 ** Implementation of the total_changes() SQL function. The return value is 333 ** the same as the sqlite3_total_changes() API function. 334 */ 335 static void total_changes( 336 sqlite3_context *context, 337 int arg, 338 sqlite3_value **argv 339 ){ 340 sqlite3 *db = sqlite3_user_data(context); 341 sqlite3_result_int(context, sqlite3_total_changes(db)); 342 } 343 344 /* 345 ** A structure defining how to do GLOB-style comparisons. 346 */ 347 struct compareInfo { 348 u8 matchAll; 349 u8 matchOne; 350 u8 matchSet; 351 u8 noCase; 352 }; 353 354 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 355 /* The correct SQL-92 behavior is for the LIKE operator to ignore 356 ** case. Thus 'a' LIKE 'A' would be true. */ 357 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 358 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 359 ** is case sensitive causing 'a' LIKE 'A' to be false */ 360 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 361 362 /* 363 ** X is a pointer to the first byte of a UTF-8 character. Increment 364 ** X so that it points to the next character. This only works right 365 ** if X points to a well-formed UTF-8 string. 366 */ 367 #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} 368 #define sqliteCharVal(X) sqlite3ReadUtf8(X) 369 370 371 /* 372 ** Compare two UTF-8 strings for equality where the first string can 373 ** potentially be a "glob" expression. Return true (1) if they 374 ** are the same and false (0) if they are different. 375 ** 376 ** Globbing rules: 377 ** 378 ** '*' Matches any sequence of zero or more characters. 379 ** 380 ** '?' Matches exactly one character. 381 ** 382 ** [...] Matches one character from the enclosed list of 383 ** characters. 384 ** 385 ** [^...] Matches one character not in the enclosed list. 386 ** 387 ** With the [...] and [^...] matching, a ']' character can be included 388 ** in the list by making it the first character after '[' or '^'. A 389 ** range of characters can be specified using '-'. Example: 390 ** "[a-z]" matches any single lower-case letter. To match a '-', make 391 ** it the last character in the list. 392 ** 393 ** This routine is usually quick, but can be N**2 in the worst case. 394 ** 395 ** Hints: to match '*' or '?', put them in "[]". Like this: 396 ** 397 ** abc[*]xyz Matches "abc*xyz" only 398 */ 399 static int patternCompare( 400 const u8 *zPattern, /* The glob pattern */ 401 const u8 *zString, /* The string to compare against the glob */ 402 const struct compareInfo *pInfo, /* Information about how to do the compare */ 403 const int esc /* The escape character */ 404 ){ 405 register int c; 406 int invert; 407 int seen; 408 int c2; 409 u8 matchOne = pInfo->matchOne; 410 u8 matchAll = pInfo->matchAll; 411 u8 matchSet = pInfo->matchSet; 412 u8 noCase = pInfo->noCase; 413 int prevEscape = 0; /* True if the previous character was 'escape' */ 414 415 while( (c = *zPattern)!=0 ){ 416 if( !prevEscape && c==matchAll ){ 417 while( (c=zPattern[1]) == matchAll || c == matchOne ){ 418 if( c==matchOne ){ 419 if( *zString==0 ) return 0; 420 sqliteNextChar(zString); 421 } 422 zPattern++; 423 } 424 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ 425 u8 const *zTemp = &zPattern[1]; 426 sqliteNextChar(zTemp); 427 c = *zTemp; 428 } 429 if( c==0 ) return 1; 430 if( c==matchSet ){ 431 assert( esc==0 ); /* This is GLOB, not LIKE */ 432 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ 433 sqliteNextChar(zString); 434 } 435 return *zString!=0; 436 }else{ 437 while( (c2 = *zString)!=0 ){ 438 if( noCase ){ 439 c2 = sqlite3UpperToLower[c2]; 440 c = sqlite3UpperToLower[c]; 441 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } 442 }else{ 443 while( c2 != 0 && c2 != c ){ c2 = *++zString; } 444 } 445 if( c2==0 ) return 0; 446 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; 447 sqliteNextChar(zString); 448 } 449 return 0; 450 } 451 }else if( !prevEscape && c==matchOne ){ 452 if( *zString==0 ) return 0; 453 sqliteNextChar(zString); 454 zPattern++; 455 }else if( c==matchSet ){ 456 int prior_c = 0; 457 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ 458 seen = 0; 459 invert = 0; 460 c = sqliteCharVal(zString); 461 if( c==0 ) return 0; 462 c2 = *++zPattern; 463 if( c2=='^' ){ invert = 1; c2 = *++zPattern; } 464 if( c2==']' ){ 465 if( c==']' ) seen = 1; 466 c2 = *++zPattern; 467 } 468 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ 469 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ 470 zPattern++; 471 c2 = sqliteCharVal(zPattern); 472 if( c>=prior_c && c<=c2 ) seen = 1; 473 prior_c = 0; 474 }else if( c==c2 ){ 475 seen = 1; 476 prior_c = c2; 477 }else{ 478 prior_c = c2; 479 } 480 sqliteNextChar(zPattern); 481 } 482 if( c2==0 || (seen ^ invert)==0 ) return 0; 483 sqliteNextChar(zString); 484 zPattern++; 485 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ 486 prevEscape = 1; 487 sqliteNextChar(zPattern); 488 }else{ 489 if( noCase ){ 490 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; 491 }else{ 492 if( c != *zString ) return 0; 493 } 494 zPattern++; 495 zString++; 496 prevEscape = 0; 497 } 498 } 499 return *zString==0; 500 } 501 502 /* 503 ** Count the number of times that the LIKE operator (or GLOB which is 504 ** just a variation of LIKE) gets called. This is used for testing 505 ** only. 506 */ 507 #ifdef SQLITE_TEST 508 int sqlite3_like_count = 0; 509 #endif 510 511 512 /* 513 ** Implementation of the like() SQL function. This function implements 514 ** the build-in LIKE operator. The first argument to the function is the 515 ** pattern and the second argument is the string. So, the SQL statements: 516 ** 517 ** A LIKE B 518 ** 519 ** is implemented as like(B,A). 520 ** 521 ** This same function (with a different compareInfo structure) computes 522 ** the GLOB operator. 523 */ 524 static void likeFunc( 525 sqlite3_context *context, 526 int argc, 527 sqlite3_value **argv 528 ){ 529 const unsigned char *zA = sqlite3_value_text(argv[0]); 530 const unsigned char *zB = sqlite3_value_text(argv[1]); 531 int escape = 0; 532 if( argc==3 ){ 533 /* The escape character string must consist of a single UTF-8 character. 534 ** Otherwise, return an error. 535 */ 536 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 537 if( zEsc==0 ) return; 538 if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ 539 sqlite3_result_error(context, 540 "ESCAPE expression must be a single character", -1); 541 return; 542 } 543 escape = sqlite3ReadUtf8(zEsc); 544 } 545 if( zA && zB ){ 546 struct compareInfo *pInfo = sqlite3_user_data(context); 547 #ifdef SQLITE_TEST 548 sqlite3_like_count++; 549 #endif 550 sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); 551 } 552 } 553 554 /* 555 ** Implementation of the NULLIF(x,y) function. The result is the first 556 ** argument if the arguments are different. The result is NULL if the 557 ** arguments are equal to each other. 558 */ 559 static void nullifFunc( 560 sqlite3_context *context, 561 int argc, 562 sqlite3_value **argv 563 ){ 564 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 565 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 566 sqlite3_result_value(context, argv[0]); 567 } 568 } 569 570 /* 571 ** Implementation of the VERSION(*) function. The result is the version 572 ** of the SQLite library that is running. 573 */ 574 static void versionFunc( 575 sqlite3_context *context, 576 int argc, 577 sqlite3_value **argv 578 ){ 579 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); 580 } 581 582 /* Array for converting from half-bytes (nybbles) into ASCII hex 583 ** digits. */ 584 static const char hexdigits[] = { 585 '0', '1', '2', '3', '4', '5', '6', '7', 586 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 587 }; 588 589 /* 590 ** EXPERIMENTAL - This is not an official function. The interface may 591 ** change. This function may disappear. Do not write code that depends 592 ** on this function. 593 ** 594 ** Implementation of the QUOTE() function. This function takes a single 595 ** argument. If the argument is numeric, the return value is the same as 596 ** the argument. If the argument is NULL, the return value is the string 597 ** "NULL". Otherwise, the argument is enclosed in single quotes with 598 ** single-quote escapes. 599 */ 600 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 601 if( argc<1 ) return; 602 switch( sqlite3_value_type(argv[0]) ){ 603 case SQLITE_NULL: { 604 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 605 break; 606 } 607 case SQLITE_INTEGER: 608 case SQLITE_FLOAT: { 609 sqlite3_result_value(context, argv[0]); 610 break; 611 } 612 case SQLITE_BLOB: { 613 char *zText = 0; 614 int nBlob = sqlite3_value_bytes(argv[0]); 615 char const *zBlob = sqlite3_value_blob(argv[0]); 616 617 zText = (char *)sqliteMalloc((2*nBlob)+4); 618 if( !zText ){ 619 sqlite3_result_error(context, "out of memory", -1); 620 }else{ 621 int i; 622 for(i=0; i<nBlob; i++){ 623 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 624 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 625 } 626 zText[(nBlob*2)+2] = '\''; 627 zText[(nBlob*2)+3] = '\0'; 628 zText[0] = 'X'; 629 zText[1] = '\''; 630 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 631 sqliteFree(zText); 632 } 633 break; 634 } 635 case SQLITE_TEXT: { 636 int i,j,n; 637 const unsigned char *zArg = sqlite3_value_text(argv[0]); 638 char *z; 639 640 if( zArg==0 ) return; 641 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 642 z = sqliteMalloc( i+n+3 ); 643 if( z==0 ) return; 644 z[0] = '\''; 645 for(i=0, j=1; zArg[i]; i++){ 646 z[j++] = zArg[i]; 647 if( zArg[i]=='\'' ){ 648 z[j++] = '\''; 649 } 650 } 651 z[j++] = '\''; 652 z[j] = 0; 653 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); 654 sqliteFree(z); 655 } 656 } 657 } 658 659 /* 660 ** The hex() function. Interpret the argument as a blob. Return 661 ** a hexadecimal rendering as text. 662 */ 663 static void hexFunc( 664 sqlite3_context *context, 665 int argc, 666 sqlite3_value **argv 667 ){ 668 int i, n; 669 const unsigned char *pBlob; 670 char *zHex, *z; 671 assert( argc==1 ); 672 n = sqlite3_value_bytes(argv[0]); 673 pBlob = sqlite3_value_blob(argv[0]); 674 z = zHex = sqlite3_malloc(n*2 + 1); 675 if( zHex==0 ) return; 676 for(i=0; i<n; i++, pBlob++){ 677 unsigned char c = *pBlob; 678 *(z++) = hexdigits[(c>>4)&0xf]; 679 *(z++) = hexdigits[c&0xf]; 680 } 681 *z = 0; 682 sqlite3_result_text(context, zHex, n*2, sqlite3_free); 683 } 684 685 /* 686 ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 687 */ 688 static void zeroblobFunc( 689 sqlite3_context *context, 690 int argc, 691 sqlite3_value **argv 692 ){ 693 int n; 694 assert( argc==1 ); 695 n = sqlite3_value_int(argv[0]); 696 sqlite3_result_zeroblob(context, n); 697 } 698 699 /* 700 ** The replace() function. Three arguments are all strings: call 701 ** them A, B, and C. The result is also a string which is derived 702 ** from A by replacing every occurance of B with C. The match 703 ** must be exact. Collating sequences are not used. 704 */ 705 static void replaceFunc( 706 sqlite3_context *context, 707 int argc, 708 sqlite3_value **argv 709 ){ 710 const unsigned char *zStr; /* The input string A */ 711 const unsigned char *zPattern; /* The pattern string B */ 712 const unsigned char *zRep; /* The replacement string C */ 713 unsigned char *zOut; /* The output */ 714 int nStr; /* Size of zStr */ 715 int nPattern; /* Size of zPattern */ 716 int nRep; /* Size of zRep */ 717 int nOut; /* Maximum size of zOut */ 718 int loopLimit; /* Last zStr[] that might match zPattern[] */ 719 int i, j; /* Loop counters */ 720 721 assert( argc==3 ); 722 nStr = sqlite3_value_bytes(argv[0]); 723 zStr = sqlite3_value_text(argv[0]); 724 if( zStr==0 ) return; 725 nPattern = sqlite3_value_bytes(argv[1]); 726 zPattern = sqlite3_value_text(argv[1]); 727 if( zPattern==0 || zPattern[0]==0 ) return; 728 nRep = sqlite3_value_bytes(argv[2]); 729 zRep = sqlite3_value_text(argv[2]); 730 if( zRep==0 ) return; 731 if( nPattern>=nRep ){ 732 nOut = nStr; 733 }else{ 734 nOut = (nStr/nPattern + 1)*nRep; 735 } 736 zOut = sqlite3_malloc(nOut+1); 737 if( zOut==0 ) return; 738 loopLimit = nStr - nPattern; 739 for(i=j=0; i<=loopLimit; i++){ 740 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 741 zOut[j++] = zStr[i]; 742 }else{ 743 memcpy(&zOut[j], zRep, nRep); 744 j += nRep; 745 i += nPattern-1; 746 } 747 } 748 memcpy(&zOut[j], &zStr[i], nStr-i); 749 j += nStr - i; 750 assert( j<=nOut ); 751 zOut[j] = 0; 752 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 753 } 754 755 /* 756 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 757 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 758 */ 759 static void trimFunc( 760 sqlite3_context *context, 761 int argc, 762 sqlite3_value **argv 763 ){ 764 const unsigned char *zIn; /* Input string */ 765 const unsigned char *zCharSet; /* Set of characters to trim */ 766 int nIn; /* Number of bytes in input */ 767 int flags; /* 1: trimleft 2: trimright 3: trim */ 768 int i; /* Loop counter */ 769 unsigned char *aLen; /* Length of each character in zCharSet */ 770 const unsigned char **azChar; /* Individual characters in zCharSet */ 771 int nChar; /* Number of characters in zCharSet */ 772 773 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 774 return; 775 } 776 nIn = sqlite3_value_bytes(argv[0]); 777 zIn = sqlite3_value_text(argv[0]); 778 if( zIn==0 ) return; 779 if( argc==1 ){ 780 static const unsigned char lenOne[] = { 1 }; 781 static const unsigned char *azOne[] = { (u8*)" " }; 782 nChar = 1; 783 aLen = (u8*)lenOne; 784 azChar = azOne; 785 zCharSet = 0; 786 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 787 return; 788 }else{ 789 const unsigned char *z; 790 for(z=zCharSet, nChar=0; *z; nChar++){ 791 sqliteNextChar(z); 792 } 793 if( nChar>0 ){ 794 azChar = sqlite3_malloc( nChar*(sizeof(char*)+1) ); 795 if( azChar==0 ){ 796 return; 797 } 798 aLen = (unsigned char*)&azChar[nChar]; 799 for(z=zCharSet, nChar=0; *z; nChar++){ 800 azChar[nChar] = z; 801 sqliteNextChar(z); 802 aLen[nChar] = z - azChar[nChar]; 803 } 804 } 805 } 806 if( nChar>0 ){ 807 flags = (int)sqlite3_user_data(context); 808 if( flags & 1 ){ 809 while( nIn>0 ){ 810 int len; 811 for(i=0; i<nChar; i++){ 812 len = aLen[i]; 813 if( memcmp(zIn, azChar[i], len)==0 ) break; 814 } 815 if( i>=nChar ) break; 816 zIn += len; 817 nIn -= len; 818 } 819 } 820 if( flags & 2 ){ 821 while( nIn>0 ){ 822 int len; 823 for(i=0; i<nChar; i++){ 824 len = aLen[i]; 825 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 826 } 827 if( i>=nChar ) break; 828 nIn -= len; 829 } 830 } 831 if( zCharSet ){ 832 sqlite3_free(azChar); 833 } 834 } 835 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 836 } 837 838 #ifdef SQLITE_SOUNDEX 839 /* 840 ** Compute the soundex encoding of a word. 841 */ 842 static void soundexFunc( 843 sqlite3_context *context, 844 int argc, 845 sqlite3_value **argv 846 ){ 847 char zResult[8]; 848 const u8 *zIn; 849 int i, j; 850 static const unsigned char iCode[] = { 851 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 852 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 853 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 854 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 855 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 856 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 857 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 858 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 859 }; 860 assert( argc==1 ); 861 zIn = (u8*)sqlite3_value_text(argv[0]); 862 if( zIn==0 ) zIn = (u8*)""; 863 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} 864 if( zIn[i] ){ 865 u8 prevcode = iCode[zIn[i]&0x7f]; 866 zResult[0] = toupper(zIn[i]); 867 for(j=1; j<4 && zIn[i]; i++){ 868 int code = iCode[zIn[i]&0x7f]; 869 if( code>0 ){ 870 if( code!=prevcode ){ 871 prevcode = code; 872 zResult[j++] = code + '0'; 873 } 874 }else{ 875 prevcode = 0; 876 } 877 } 878 while( j<4 ){ 879 zResult[j++] = '0'; 880 } 881 zResult[j] = 0; 882 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 883 }else{ 884 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 885 } 886 } 887 #endif 888 889 #ifndef SQLITE_OMIT_LOAD_EXTENSION 890 /* 891 ** A function that loads a shared-library extension then returns NULL. 892 */ 893 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 894 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 895 const char *zProc; 896 sqlite3 *db = sqlite3_user_data(context); 897 char *zErrMsg = 0; 898 899 if( argc==2 ){ 900 zProc = (const char *)sqlite3_value_text(argv[1]); 901 }else{ 902 zProc = 0; 903 } 904 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 905 sqlite3_result_error(context, zErrMsg, -1); 906 sqlite3_free(zErrMsg); 907 } 908 } 909 #endif 910 911 #ifdef SQLITE_TEST 912 /* 913 ** This function generates a string of random characters. Used for 914 ** generating test data. 915 */ 916 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 917 static const unsigned char zSrc[] = 918 "abcdefghijklmnopqrstuvwxyz" 919 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 920 "0123456789" 921 ".-!,:*^+=_|?/<> "; 922 int iMin, iMax, n, r, i; 923 unsigned char zBuf[1000]; 924 if( argc>=1 ){ 925 iMin = sqlite3_value_int(argv[0]); 926 if( iMin<0 ) iMin = 0; 927 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 928 }else{ 929 iMin = 1; 930 } 931 if( argc>=2 ){ 932 iMax = sqlite3_value_int(argv[1]); 933 if( iMax<iMin ) iMax = iMin; 934 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 935 }else{ 936 iMax = 50; 937 } 938 n = iMin; 939 if( iMax>iMin ){ 940 sqlite3Randomness(sizeof(r), &r); 941 r &= 0x7fffffff; 942 n += r%(iMax + 1 - iMin); 943 } 944 assert( n<sizeof(zBuf) ); 945 sqlite3Randomness(n, zBuf); 946 for(i=0; i<n; i++){ 947 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 948 } 949 zBuf[n] = 0; 950 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 951 } 952 #endif /* SQLITE_TEST */ 953 954 #ifdef SQLITE_TEST 955 /* 956 ** The following two SQL functions are used to test returning a text 957 ** result with a destructor. Function 'test_destructor' takes one argument 958 ** and returns the same argument interpreted as TEXT. A destructor is 959 ** passed with the sqlite3_result_text() call. 960 ** 961 ** SQL function 'test_destructor_count' returns the number of outstanding 962 ** allocations made by 'test_destructor'; 963 ** 964 ** WARNING: Not threadsafe. 965 */ 966 static int test_destructor_count_var = 0; 967 static void destructor(void *p){ 968 char *zVal = (char *)p; 969 assert(zVal); 970 zVal--; 971 sqliteFree(zVal); 972 test_destructor_count_var--; 973 } 974 static void test_destructor( 975 sqlite3_context *pCtx, 976 int nArg, 977 sqlite3_value **argv 978 ){ 979 char *zVal; 980 int len; 981 sqlite3 *db = sqlite3_user_data(pCtx); 982 983 test_destructor_count_var++; 984 assert( nArg==1 ); 985 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 986 len = sqlite3ValueBytes(argv[0], ENC(db)); 987 zVal = sqliteMalloc(len+3); 988 zVal[len] = 0; 989 zVal[len-1] = 0; 990 assert( zVal ); 991 zVal++; 992 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len); 993 if( ENC(db)==SQLITE_UTF8 ){ 994 sqlite3_result_text(pCtx, zVal, -1, destructor); 995 #ifndef SQLITE_OMIT_UTF16 996 }else if( ENC(db)==SQLITE_UTF16LE ){ 997 sqlite3_result_text16le(pCtx, zVal, -1, destructor); 998 }else{ 999 sqlite3_result_text16be(pCtx, zVal, -1, destructor); 1000 #endif /* SQLITE_OMIT_UTF16 */ 1001 } 1002 } 1003 static void test_destructor_count( 1004 sqlite3_context *pCtx, 1005 int nArg, 1006 sqlite3_value **argv 1007 ){ 1008 sqlite3_result_int(pCtx, test_destructor_count_var); 1009 } 1010 #endif /* SQLITE_TEST */ 1011 1012 #ifdef SQLITE_TEST 1013 /* 1014 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 1015 ** interface. 1016 ** 1017 ** The test_auxdata() SQL function attempts to register each of its arguments 1018 ** as auxiliary data. If there are no prior registrations of aux data for 1019 ** that argument (meaning the argument is not a constant or this is its first 1020 ** call) then the result for that argument is 0. If there is a prior 1021 ** registration, the result for that argument is 1. The overall result 1022 ** is the individual argument results separated by spaces. 1023 */ 1024 static void free_test_auxdata(void *p) {sqliteFree(p);} 1025 static void test_auxdata( 1026 sqlite3_context *pCtx, 1027 int nArg, 1028 sqlite3_value **argv 1029 ){ 1030 int i; 1031 char *zRet = sqliteMalloc(nArg*2); 1032 if( !zRet ) return; 1033 for(i=0; i<nArg; i++){ 1034 char const *z = (char*)sqlite3_value_text(argv[i]); 1035 if( z ){ 1036 char *zAux = sqlite3_get_auxdata(pCtx, i); 1037 if( zAux ){ 1038 zRet[i*2] = '1'; 1039 if( strcmp(zAux, z) ){ 1040 sqlite3_result_error(pCtx, "Auxilary data corruption", -1); 1041 return; 1042 } 1043 }else{ 1044 zRet[i*2] = '0'; 1045 zAux = sqliteStrDup(z); 1046 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 1047 } 1048 zRet[i*2+1] = ' '; 1049 } 1050 } 1051 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 1052 } 1053 #endif /* SQLITE_TEST */ 1054 1055 #ifdef SQLITE_TEST 1056 /* 1057 ** A function to test error reporting from user functions. This function 1058 ** returns a copy of it's first argument as an error. 1059 */ 1060 static void test_error( 1061 sqlite3_context *pCtx, 1062 int nArg, 1063 sqlite3_value **argv 1064 ){ 1065 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0); 1066 } 1067 #endif /* SQLITE_TEST */ 1068 1069 /* 1070 ** An instance of the following structure holds the context of a 1071 ** sum() or avg() aggregate computation. 1072 */ 1073 typedef struct SumCtx SumCtx; 1074 struct SumCtx { 1075 double rSum; /* Floating point sum */ 1076 i64 iSum; /* Integer sum */ 1077 i64 cnt; /* Number of elements summed */ 1078 u8 overflow; /* True if integer overflow seen */ 1079 u8 approx; /* True if non-integer value was input to the sum */ 1080 }; 1081 1082 /* 1083 ** Routines used to compute the sum, average, and total. 1084 ** 1085 ** The SUM() function follows the (broken) SQL standard which means 1086 ** that it returns NULL if it sums over no inputs. TOTAL returns 1087 ** 0.0 in that case. In addition, TOTAL always returns a float where 1088 ** SUM might return an integer if it never encounters a floating point 1089 ** value. TOTAL never fails, but SUM might through an exception if 1090 ** it overflows an integer. 1091 */ 1092 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1093 SumCtx *p; 1094 int type; 1095 assert( argc==1 ); 1096 p = sqlite3_aggregate_context(context, sizeof(*p)); 1097 type = sqlite3_value_numeric_type(argv[0]); 1098 if( p && type!=SQLITE_NULL ){ 1099 p->cnt++; 1100 if( type==SQLITE_INTEGER ){ 1101 i64 v = sqlite3_value_int64(argv[0]); 1102 p->rSum += v; 1103 if( (p->approx|p->overflow)==0 ){ 1104 i64 iNewSum = p->iSum + v; 1105 int s1 = p->iSum >> (sizeof(i64)*8-1); 1106 int s2 = v >> (sizeof(i64)*8-1); 1107 int s3 = iNewSum >> (sizeof(i64)*8-1); 1108 p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); 1109 p->iSum = iNewSum; 1110 } 1111 }else{ 1112 p->rSum += sqlite3_value_double(argv[0]); 1113 p->approx = 1; 1114 } 1115 } 1116 } 1117 static void sumFinalize(sqlite3_context *context){ 1118 SumCtx *p; 1119 p = sqlite3_aggregate_context(context, 0); 1120 if( p && p->cnt>0 ){ 1121 if( p->overflow ){ 1122 sqlite3_result_error(context,"integer overflow",-1); 1123 }else if( p->approx ){ 1124 sqlite3_result_double(context, p->rSum); 1125 }else{ 1126 sqlite3_result_int64(context, p->iSum); 1127 } 1128 } 1129 } 1130 static void avgFinalize(sqlite3_context *context){ 1131 SumCtx *p; 1132 p = sqlite3_aggregate_context(context, 0); 1133 if( p && p->cnt>0 ){ 1134 sqlite3_result_double(context, p->rSum/(double)p->cnt); 1135 } 1136 } 1137 static void totalFinalize(sqlite3_context *context){ 1138 SumCtx *p; 1139 p = sqlite3_aggregate_context(context, 0); 1140 sqlite3_result_double(context, p ? p->rSum : 0.0); 1141 } 1142 1143 /* 1144 ** The following structure keeps track of state information for the 1145 ** count() aggregate function. 1146 */ 1147 typedef struct CountCtx CountCtx; 1148 struct CountCtx { 1149 i64 n; 1150 }; 1151 1152 /* 1153 ** Routines to implement the count() aggregate function. 1154 */ 1155 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1156 CountCtx *p; 1157 p = sqlite3_aggregate_context(context, sizeof(*p)); 1158 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 1159 p->n++; 1160 } 1161 } 1162 static void countFinalize(sqlite3_context *context){ 1163 CountCtx *p; 1164 p = sqlite3_aggregate_context(context, 0); 1165 sqlite3_result_int64(context, p ? p->n : 0); 1166 } 1167 1168 /* 1169 ** Routines to implement min() and max() aggregate functions. 1170 */ 1171 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1172 Mem *pArg = (Mem *)argv[0]; 1173 Mem *pBest; 1174 1175 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1176 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 1177 if( !pBest ) return; 1178 1179 if( pBest->flags ){ 1180 int max; 1181 int cmp; 1182 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 1183 /* This step function is used for both the min() and max() aggregates, 1184 ** the only difference between the two being that the sense of the 1185 ** comparison is inverted. For the max() aggregate, the 1186 ** sqlite3_user_data() function returns (void *)-1. For min() it 1187 ** returns (void *)db, where db is the sqlite3* database pointer. 1188 ** Therefore the next statement sets variable 'max' to 1 for the max() 1189 ** aggregate, or 0 for min(). 1190 */ 1191 max = sqlite3_user_data(context)!=0; 1192 cmp = sqlite3MemCompare(pBest, pArg, pColl); 1193 if( (max && cmp<0) || (!max && cmp>0) ){ 1194 sqlite3VdbeMemCopy(pBest, pArg); 1195 } 1196 }else{ 1197 sqlite3VdbeMemCopy(pBest, pArg); 1198 } 1199 } 1200 static void minMaxFinalize(sqlite3_context *context){ 1201 sqlite3_value *pRes; 1202 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1203 if( pRes ){ 1204 if( pRes->flags ){ 1205 sqlite3_result_value(context, pRes); 1206 } 1207 sqlite3VdbeMemRelease(pRes); 1208 } 1209 } 1210 1211 1212 /* 1213 ** This function registered all of the above C functions as SQL 1214 ** functions. This should be the only routine in this file with 1215 ** external linkage. 1216 */ 1217 void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ 1218 static const struct { 1219 char *zName; 1220 signed char nArg; 1221 u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */ 1222 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ 1223 u8 needCollSeq; 1224 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 1225 } aFuncs[] = { 1226 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, 1227 { "min", 0, 0, SQLITE_UTF8, 1, 0 }, 1228 { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc }, 1229 { "max", 0, 1, SQLITE_UTF8, 1, 0 }, 1230 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, 1231 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, 1232 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, 1233 #ifndef SQLITE_OMIT_UTF16 1234 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, 1235 #endif 1236 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, 1237 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, 1238 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, 1239 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, 1240 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, 1241 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, 1242 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, 1243 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, 1244 { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc }, 1245 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, 1246 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, 1247 { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob }, 1248 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, 1249 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, 1250 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, 1251 { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid }, 1252 { "changes", 0, 0xff, SQLITE_UTF8, 0, changes }, 1253 { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes }, 1254 { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc }, 1255 { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc }, 1256 { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc }, 1257 { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc }, 1258 { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc }, 1259 { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc }, 1260 { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc }, 1261 { "zeroblob", 1, 0, SQLITE_UTF8, 0, zeroblobFunc }, 1262 #ifdef SQLITE_SOUNDEX 1263 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, 1264 #endif 1265 #ifndef SQLITE_OMIT_LOAD_EXTENSION 1266 { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt }, 1267 { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt }, 1268 #endif 1269 #ifdef SQLITE_TEST 1270 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, 1271 { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor}, 1272 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, 1273 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, 1274 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, 1275 #endif 1276 }; 1277 static const struct { 1278 char *zName; 1279 signed char nArg; 1280 u8 argType; 1281 u8 needCollSeq; 1282 void (*xStep)(sqlite3_context*,int,sqlite3_value**); 1283 void (*xFinalize)(sqlite3_context*); 1284 } aAggs[] = { 1285 { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, 1286 { "max", 1, 1, 1, minmaxStep, minMaxFinalize }, 1287 { "sum", 1, 0, 0, sumStep, sumFinalize }, 1288 { "total", 1, 0, 0, sumStep, totalFinalize }, 1289 { "avg", 1, 0, 0, sumStep, avgFinalize }, 1290 { "count", 0, 0, 0, countStep, countFinalize }, 1291 { "count", 1, 0, 0, countStep, countFinalize }, 1292 }; 1293 int i; 1294 1295 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 1296 void *pArg; 1297 u8 argType = aFuncs[i].argType; 1298 if( argType==0xff ){ 1299 pArg = db; 1300 }else{ 1301 pArg = (void*)(int)argType; 1302 } 1303 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, 1304 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); 1305 if( aFuncs[i].needCollSeq ){ 1306 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 1307 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); 1308 if( pFunc && aFuncs[i].needCollSeq ){ 1309 pFunc->needCollSeq = 1; 1310 } 1311 } 1312 } 1313 #ifndef SQLITE_OMIT_ALTERTABLE 1314 sqlite3AlterFunctions(db); 1315 #endif 1316 #ifndef SQLITE_OMIT_PARSER 1317 sqlite3AttachFunctions(db); 1318 #endif 1319 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 1320 void *pArg = (void*)(int)aAggs[i].argType; 1321 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 1322 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); 1323 if( aAggs[i].needCollSeq ){ 1324 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, 1325 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); 1326 if( pFunc && aAggs[i].needCollSeq ){ 1327 pFunc->needCollSeq = 1; 1328 } 1329 } 1330 } 1331 sqlite3RegisterDateTimeFunctions(db); 1332 sqlite3_overload_function(db, "MATCH", 2); 1333 #ifdef SQLITE_SSE 1334 (void)sqlite3SseFunctions(db); 1335 #endif 1336 #ifdef SQLITE_CASE_SENSITIVE_LIKE 1337 sqlite3RegisterLikeFunctions(db, 1); 1338 #else 1339 sqlite3RegisterLikeFunctions(db, 0); 1340 #endif 1341 } 1342 1343 /* 1344 ** Set the LIKEOPT flag on the 2-argument function with the given name. 1345 */ 1346 static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ 1347 FuncDef *pDef; 1348 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); 1349 if( pDef ){ 1350 pDef->flags = flagVal; 1351 } 1352 } 1353 1354 /* 1355 ** Register the built-in LIKE and GLOB functions. The caseSensitive 1356 ** parameter determines whether or not the LIKE operator is case 1357 ** sensitive. GLOB is always case sensitive. 1358 */ 1359 void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 1360 struct compareInfo *pInfo; 1361 if( caseSensitive ){ 1362 pInfo = (struct compareInfo*)&likeInfoAlt; 1363 }else{ 1364 pInfo = (struct compareInfo*)&likeInfoNorm; 1365 } 1366 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1367 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1368 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 1369 (struct compareInfo*)&globInfo, likeFunc, 0,0); 1370 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 1371 setLikeOptFlag(db, "like", 1372 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 1373 } 1374 1375 /* 1376 ** pExpr points to an expression which implements a function. If 1377 ** it is appropriate to apply the LIKE optimization to that function 1378 ** then set aWc[0] through aWc[2] to the wildcard characters and 1379 ** return TRUE. If the function is not a LIKE-style function then 1380 ** return FALSE. 1381 */ 1382 int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 1383 FuncDef *pDef; 1384 if( pExpr->op!=TK_FUNCTION ){ 1385 return 0; 1386 } 1387 if( pExpr->pList->nExpr!=2 ){ 1388 return 0; 1389 } 1390 pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, 1391 SQLITE_UTF8, 0); 1392 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ 1393 return 0; 1394 } 1395 1396 /* The memcpy() statement assumes that the wildcard characters are 1397 ** the first three statements in the compareInfo structure. The 1398 ** asserts() that follow verify that assumption 1399 */ 1400 memcpy(aWc, pDef->pUserData, 3); 1401 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 1402 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 1403 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1404 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; 1405 return 1; 1406 } 1407