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