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