| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2002 February 23 | 2 ** 2002 February 23 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains the C-language implementations for many of the SQL | 12 ** This file contains the C-language implementations for many of the SQL |
| 13 ** functions of SQLite. (Some function, and in particular the date and | 13 ** functions of SQLite. (Some function, and in particular the date and |
| 14 ** time functions, are implemented separately.) | 14 ** time functions, are implemented separately.) |
| 15 */ | 15 */ |
| 16 #include "sqliteInt.h" | 16 #include "sqliteInt.h" |
| 17 #include <stdlib.h> | 17 #include <stdlib.h> |
| 18 #include <assert.h> | 18 #include <assert.h> |
| 19 #include "vdbeInt.h" | 19 #include "vdbeInt.h" |
| 20 | 20 |
| 21 /* | 21 /* |
| 22 ** Return the collating function associated with a function. | 22 ** Return the collating function associated with a function. |
| 23 */ | 23 */ |
| 24 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ | 24 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 25 VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1]; | 25 VdbeOp *pOp; |
| 26 assert( context->pVdbe!=0 ); |
| 27 pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 26 assert( pOp->opcode==OP_CollSeq ); | 28 assert( pOp->opcode==OP_CollSeq ); |
| 27 assert( pOp->p4type==P4_COLLSEQ ); | 29 assert( pOp->p4type==P4_COLLSEQ ); |
| 28 return pOp->p4.pColl; | 30 return pOp->p4.pColl; |
| 29 } | 31 } |
| 30 | 32 |
| 31 /* | 33 /* |
| 32 ** Indicate that the accumulator load should be skipped on this | 34 ** Indicate that the accumulator load should be skipped on this |
| 33 ** iteration of the aggregate loop. | 35 ** iteration of the aggregate loop. |
| 34 */ | 36 */ |
| 35 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ | 37 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 break; | 152 break; |
| 151 } | 153 } |
| 152 case SQLITE_NULL: { | 154 case SQLITE_NULL: { |
| 153 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ | 155 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ |
| 154 sqlite3_result_null(context); | 156 sqlite3_result_null(context); |
| 155 break; | 157 break; |
| 156 } | 158 } |
| 157 default: { | 159 default: { |
| 158 /* Because sqlite3_value_double() returns 0.0 if the argument is not | 160 /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 159 ** something that can be converted into a number, we have: | 161 ** something that can be converted into a number, we have: |
| 160 ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that | 162 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob |
| 161 ** cannot be converted to a numeric value. | 163 ** that cannot be converted to a numeric value. |
| 162 */ | 164 */ |
| 163 double rVal = sqlite3_value_double(argv[0]); | 165 double rVal = sqlite3_value_double(argv[0]); |
| 164 if( rVal<0 ) rVal = -rVal; | 166 if( rVal<0 ) rVal = -rVal; |
| 165 sqlite3_result_double(context, rVal); | 167 sqlite3_result_double(context, rVal); |
| 166 break; | 168 break; |
| 167 } | 169 } |
| 168 } | 170 } |
| 169 } | 171 } |
| 170 | 172 |
| 171 /* | 173 /* |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 */ | 225 */ |
| 224 static void printfFunc( | 226 static void printfFunc( |
| 225 sqlite3_context *context, | 227 sqlite3_context *context, |
| 226 int argc, | 228 int argc, |
| 227 sqlite3_value **argv | 229 sqlite3_value **argv |
| 228 ){ | 230 ){ |
| 229 PrintfArguments x; | 231 PrintfArguments x; |
| 230 StrAccum str; | 232 StrAccum str; |
| 231 const char *zFormat; | 233 const char *zFormat; |
| 232 int n; | 234 int n; |
| 235 sqlite3 *db = sqlite3_context_db_handle(context); |
| 233 | 236 |
| 234 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ | 237 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ |
| 235 x.nArg = argc-1; | 238 x.nArg = argc-1; |
| 236 x.nUsed = 0; | 239 x.nUsed = 0; |
| 237 x.apArg = argv+1; | 240 x.apArg = argv+1; |
| 238 sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH); | 241 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 239 str.db = sqlite3_context_db_handle(context); | |
| 240 sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x); | 242 sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x); |
| 241 n = str.nChar; | 243 n = str.nChar; |
| 242 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, | 244 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, |
| 243 SQLITE_DYNAMIC); | 245 SQLITE_DYNAMIC); |
| 244 } | 246 } |
| 245 } | 247 } |
| 246 | 248 |
| 247 /* | 249 /* |
| 248 ** Implementation of the substr() function. | 250 ** Implementation of the substr() function. |
| 249 ** | 251 ** |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 }else{ | 286 }else{ |
| 285 z = sqlite3_value_text(argv[0]); | 287 z = sqlite3_value_text(argv[0]); |
| 286 if( z==0 ) return; | 288 if( z==0 ) return; |
| 287 len = 0; | 289 len = 0; |
| 288 if( p1<0 ){ | 290 if( p1<0 ){ |
| 289 for(z2=z; *z2; len++){ | 291 for(z2=z; *z2; len++){ |
| 290 SQLITE_SKIP_UTF8(z2); | 292 SQLITE_SKIP_UTF8(z2); |
| 291 } | 293 } |
| 292 } | 294 } |
| 293 } | 295 } |
| 296 #ifdef SQLITE_SUBSTR_COMPATIBILITY |
| 297 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as |
| 298 ** as substr(X,1,N) - it returns the first N characters of X. This |
| 299 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] |
| 300 ** from 2009-02-02 for compatibility of applications that exploited the |
| 301 ** old buggy behavior. */ |
| 302 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ |
| 303 #endif |
| 294 if( argc==3 ){ | 304 if( argc==3 ){ |
| 295 p2 = sqlite3_value_int(argv[2]); | 305 p2 = sqlite3_value_int(argv[2]); |
| 296 if( p2<0 ){ | 306 if( p2<0 ){ |
| 297 p2 = -p2; | 307 p2 = -p2; |
| 298 negP2 = 1; | 308 negP2 = 1; |
| 299 } | 309 } |
| 300 }else{ | 310 }else{ |
| 301 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; | 311 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; |
| 302 } | 312 } |
| 303 if( p1<0 ){ | 313 if( p1<0 ){ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 return; | 381 return; |
| 372 } | 382 } |
| 373 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); | 383 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); |
| 374 sqlite3_free(zBuf); | 384 sqlite3_free(zBuf); |
| 375 } | 385 } |
| 376 sqlite3_result_double(context, r); | 386 sqlite3_result_double(context, r); |
| 377 } | 387 } |
| 378 #endif | 388 #endif |
| 379 | 389 |
| 380 /* | 390 /* |
| 381 ** Allocate nByte bytes of space using sqlite3_malloc(). If the | 391 ** Allocate nByte bytes of space using sqlite3Malloc(). If the |
| 382 ** allocation fails, call sqlite3_result_error_nomem() to notify | 392 ** allocation fails, call sqlite3_result_error_nomem() to notify |
| 383 ** the database handle that malloc() has failed and return NULL. | 393 ** the database handle that malloc() has failed and return NULL. |
| 384 ** If nByte is larger than the maximum string or blob length, then | 394 ** If nByte is larger than the maximum string or blob length, then |
| 385 ** raise an SQLITE_TOOBIG exception and return NULL. | 395 ** raise an SQLITE_TOOBIG exception and return NULL. |
| 386 */ | 396 */ |
| 387 static void *contextMalloc(sqlite3_context *context, i64 nByte){ | 397 static void *contextMalloc(sqlite3_context *context, i64 nByte){ |
| 388 char *z; | 398 char *z; |
| 389 sqlite3 *db = sqlite3_context_db_handle(context); | 399 sqlite3 *db = sqlite3_context_db_handle(context); |
| 390 assert( nByte>0 ); | 400 assert( nByte>0 ); |
| 391 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); | 401 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 */ | 568 */ |
| 559 struct compareInfo { | 569 struct compareInfo { |
| 560 u8 matchAll; | 570 u8 matchAll; |
| 561 u8 matchOne; | 571 u8 matchOne; |
| 562 u8 matchSet; | 572 u8 matchSet; |
| 563 u8 noCase; | 573 u8 noCase; |
| 564 }; | 574 }; |
| 565 | 575 |
| 566 /* | 576 /* |
| 567 ** For LIKE and GLOB matching on EBCDIC machines, assume that every | 577 ** For LIKE and GLOB matching on EBCDIC machines, assume that every |
| 568 ** character is exactly one byte in size. Also, all characters are | 578 ** character is exactly one byte in size. Also, provde the Utf8Read() |
| 569 ** able to participate in upper-case-to-lower-case mappings in EBCDIC | 579 ** macro for fast reading of the next character in the common case where |
| 570 ** whereas only characters less than 0x80 do in ASCII. | 580 ** the next character is ASCII. |
| 571 */ | 581 */ |
| 572 #if defined(SQLITE_EBCDIC) | 582 #if defined(SQLITE_EBCDIC) |
| 573 # define sqlite3Utf8Read(A) (*((*A)++)) | 583 # define sqlite3Utf8Read(A) (*((*A)++)) |
| 574 # define GlobUpperToLower(A) A = sqlite3UpperToLower[A] | 584 # define Utf8Read(A) (*(A++)) |
| 575 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] | |
| 576 #else | 585 #else |
| 577 # define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; } | 586 # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) |
| 578 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] | |
| 579 #endif | 587 #endif |
| 580 | 588 |
| 581 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; | 589 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 582 /* The correct SQL-92 behavior is for the LIKE operator to ignore | 590 /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 583 ** case. Thus 'a' LIKE 'A' would be true. */ | 591 ** case. Thus 'a' LIKE 'A' would be true. */ |
| 584 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; | 592 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; |
| 585 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator | 593 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator |
| 586 ** is case sensitive causing 'a' LIKE 'A' to be false */ | 594 ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 587 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; | 595 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
| 588 | 596 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 610 ** | 618 ** |
| 611 ** Like matching rules: | 619 ** Like matching rules: |
| 612 ** | 620 ** |
| 613 ** '%' Matches any sequence of zero or more characters | 621 ** '%' Matches any sequence of zero or more characters |
| 614 ** | 622 ** |
| 615 *** '_' Matches any one character | 623 *** '_' Matches any one character |
| 616 ** | 624 ** |
| 617 ** Ec Where E is the "esc" character and c is any other | 625 ** Ec Where E is the "esc" character and c is any other |
| 618 ** character, including '%', '_', and esc, match exactly c. | 626 ** character, including '%', '_', and esc, match exactly c. |
| 619 ** | 627 ** |
| 620 ** The comments through this routine usually assume glob matching. | 628 ** The comments within this routine usually assume glob matching. |
| 621 ** | 629 ** |
| 622 ** This routine is usually quick, but can be N**2 in the worst case. | 630 ** This routine is usually quick, but can be N**2 in the worst case. |
| 623 */ | 631 */ |
| 624 static int patternCompare( | 632 static int patternCompare( |
| 625 const u8 *zPattern, /* The glob pattern */ | 633 const u8 *zPattern, /* The glob pattern */ |
| 626 const u8 *zString, /* The string to compare against the glob */ | 634 const u8 *zString, /* The string to compare against the glob */ |
| 627 const struct compareInfo *pInfo, /* Information about how to do the compare */ | 635 const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 628 u32 esc /* The escape character */ | 636 u32 esc /* The escape character */ |
| 629 ){ | 637 ){ |
| 630 u32 c, c2; /* Next pattern and input string chars */ | 638 u32 c, c2; /* Next pattern and input string chars */ |
| 631 u32 matchOne = pInfo->matchOne; /* "?" or "_" */ | 639 u32 matchOne = pInfo->matchOne; /* "?" or "_" */ |
| 632 u32 matchAll = pInfo->matchAll; /* "*" or "%" */ | 640 u32 matchAll = pInfo->matchAll; /* "*" or "%" */ |
| 633 u32 matchOther; /* "[" or the escape character */ | 641 u32 matchOther; /* "[" or the escape character */ |
| 634 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ | 642 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ |
| 635 const u8 *zEscaped = 0; /* One past the last escaped input char */ | 643 const u8 *zEscaped = 0; /* One past the last escaped input char */ |
| 636 | 644 |
| 637 /* The GLOB operator does not have an ESCAPE clause. And LIKE does not | 645 /* The GLOB operator does not have an ESCAPE clause. And LIKE does not |
| 638 ** have the matchSet operator. So we either have to look for one or | 646 ** have the matchSet operator. So we either have to look for one or |
| 639 ** the other, never both. Hence the single variable matchOther is used | 647 ** the other, never both. Hence the single variable matchOther is used |
| 640 ** to store the one we have to look for. | 648 ** to store the one we have to look for. |
| 641 */ | 649 */ |
| 642 matchOther = esc ? esc : pInfo->matchSet; | 650 matchOther = esc ? esc : pInfo->matchSet; |
| 643 | 651 |
| 644 while( (c = sqlite3Utf8Read(&zPattern))!=0 ){ | 652 while( (c = Utf8Read(zPattern))!=0 ){ |
| 645 if( c==matchAll ){ /* Match "*" */ | 653 if( c==matchAll ){ /* Match "*" */ |
| 646 /* Skip over multiple "*" characters in the pattern. If there | 654 /* Skip over multiple "*" characters in the pattern. If there |
| 647 ** are also "?" characters, skip those as well, but consume a | 655 ** are also "?" characters, skip those as well, but consume a |
| 648 ** single character of the input string for each "?" skipped */ | 656 ** single character of the input string for each "?" skipped */ |
| 649 while( (c=sqlite3Utf8Read(&zPattern)) == matchAll | 657 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ |
| 650 || c == matchOne ){ | |
| 651 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ | 658 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ |
| 652 return 0; | 659 return 0; |
| 653 } | 660 } |
| 654 } | 661 } |
| 655 if( c==0 ){ | 662 if( c==0 ){ |
| 656 return 1; /* "*" at the end of the pattern matches */ | 663 return 1; /* "*" at the end of the pattern matches */ |
| 657 }else if( c==matchOther ){ | 664 }else if( c==matchOther ){ |
| 658 if( esc ){ | 665 if( esc ){ |
| 659 c = sqlite3Utf8Read(&zPattern); | 666 c = sqlite3Utf8Read(&zPattern); |
| 660 if( c==0 ) return 0; | 667 if( c==0 ) return 0; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 685 cx = sqlite3Toupper(c); | 692 cx = sqlite3Toupper(c); |
| 686 c = sqlite3Tolower(c); | 693 c = sqlite3Tolower(c); |
| 687 }else{ | 694 }else{ |
| 688 cx = c; | 695 cx = c; |
| 689 } | 696 } |
| 690 while( (c2 = *(zString++))!=0 ){ | 697 while( (c2 = *(zString++))!=0 ){ |
| 691 if( c2!=c && c2!=cx ) continue; | 698 if( c2!=c && c2!=cx ) continue; |
| 692 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; | 699 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 693 } | 700 } |
| 694 }else{ | 701 }else{ |
| 695 while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ | 702 while( (c2 = Utf8Read(zString))!=0 ){ |
| 696 if( c2!=c ) continue; | 703 if( c2!=c ) continue; |
| 697 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; | 704 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 698 } | 705 } |
| 699 } | 706 } |
| 700 return 0; | 707 return 0; |
| 701 } | 708 } |
| 702 if( c==matchOther ){ | 709 if( c==matchOther ){ |
| 703 if( esc ){ | 710 if( esc ){ |
| 704 c = sqlite3Utf8Read(&zPattern); | 711 c = sqlite3Utf8Read(&zPattern); |
| 705 if( c==0 ) return 0; | 712 if( c==0 ) return 0; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 731 prior_c = c2; | 738 prior_c = c2; |
| 732 } | 739 } |
| 733 c2 = sqlite3Utf8Read(&zPattern); | 740 c2 = sqlite3Utf8Read(&zPattern); |
| 734 } | 741 } |
| 735 if( c2==0 || (seen ^ invert)==0 ){ | 742 if( c2==0 || (seen ^ invert)==0 ){ |
| 736 return 0; | 743 return 0; |
| 737 } | 744 } |
| 738 continue; | 745 continue; |
| 739 } | 746 } |
| 740 } | 747 } |
| 741 c2 = sqlite3Utf8Read(&zString); | 748 c2 = Utf8Read(zString); |
| 742 if( c==c2 ) continue; | 749 if( c==c2 ) continue; |
| 743 if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ | 750 if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ |
| 744 continue; | 751 continue; |
| 745 } | 752 } |
| 746 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; | 753 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; |
| 747 return 0; | 754 return 0; |
| 748 } | 755 } |
| 749 return *zString==0; | 756 return *zString==0; |
| 750 } | 757 } |
| 751 | 758 |
| 752 /* | 759 /* |
| 753 ** The sqlite3_strglob() interface. | 760 ** The sqlite3_strglob() interface. |
| 754 */ | 761 */ |
| 755 int sqlite3_strglob(const char *zGlobPattern, const char *zString){ | 762 int sqlite3_strglob(const char *zGlobPattern, const char *zString){ |
| 756 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0; | 763 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0; |
| 757 } | 764 } |
| 758 | 765 |
| 759 /* | 766 /* |
| 767 ** The sqlite3_strlike() interface. |
| 768 */ |
| 769 int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ |
| 770 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc)==0; |
| 771 } |
| 772 |
| 773 /* |
| 760 ** Count the number of times that the LIKE operator (or GLOB which is | 774 ** Count the number of times that the LIKE operator (or GLOB which is |
| 761 ** just a variation of LIKE) gets called. This is used for testing | 775 ** just a variation of LIKE) gets called. This is used for testing |
| 762 ** only. | 776 ** only. |
| 763 */ | 777 */ |
| 764 #ifdef SQLITE_TEST | 778 #ifdef SQLITE_TEST |
| 765 int sqlite3_like_count = 0; | 779 int sqlite3_like_count = 0; |
| 766 #endif | 780 #endif |
| 767 | 781 |
| 768 | 782 |
| 769 /* | 783 /* |
| (...skipping 11 matching lines...) Expand all Loading... |
| 781 static void likeFunc( | 795 static void likeFunc( |
| 782 sqlite3_context *context, | 796 sqlite3_context *context, |
| 783 int argc, | 797 int argc, |
| 784 sqlite3_value **argv | 798 sqlite3_value **argv |
| 785 ){ | 799 ){ |
| 786 const unsigned char *zA, *zB; | 800 const unsigned char *zA, *zB; |
| 787 u32 escape = 0; | 801 u32 escape = 0; |
| 788 int nPat; | 802 int nPat; |
| 789 sqlite3 *db = sqlite3_context_db_handle(context); | 803 sqlite3 *db = sqlite3_context_db_handle(context); |
| 790 | 804 |
| 805 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 806 if( sqlite3_value_type(argv[0])==SQLITE_BLOB |
| 807 || sqlite3_value_type(argv[1])==SQLITE_BLOB |
| 808 ){ |
| 809 #ifdef SQLITE_TEST |
| 810 sqlite3_like_count++; |
| 811 #endif |
| 812 sqlite3_result_int(context, 0); |
| 813 return; |
| 814 } |
| 815 #endif |
| 791 zB = sqlite3_value_text(argv[0]); | 816 zB = sqlite3_value_text(argv[0]); |
| 792 zA = sqlite3_value_text(argv[1]); | 817 zA = sqlite3_value_text(argv[1]); |
| 793 | 818 |
| 794 /* Limit the length of the LIKE or GLOB pattern to avoid problems | 819 /* Limit the length of the LIKE or GLOB pattern to avoid problems |
| 795 ** of deep recursion and N*N behavior in patternCompare(). | 820 ** of deep recursion and N*N behavior in patternCompare(). |
| 796 */ | 821 */ |
| 797 nPat = sqlite3_value_bytes(argv[0]); | 822 nPat = sqlite3_value_bytes(argv[0]); |
| 798 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); | 823 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); |
| 799 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); | 824 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); |
| 800 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ | 825 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 ** an integer. It constructs a string where each character of the string | 1065 ** an integer. It constructs a string where each character of the string |
| 1041 ** is the unicode character for the corresponding integer argument. | 1066 ** is the unicode character for the corresponding integer argument. |
| 1042 */ | 1067 */ |
| 1043 static void charFunc( | 1068 static void charFunc( |
| 1044 sqlite3_context *context, | 1069 sqlite3_context *context, |
| 1045 int argc, | 1070 int argc, |
| 1046 sqlite3_value **argv | 1071 sqlite3_value **argv |
| 1047 ){ | 1072 ){ |
| 1048 unsigned char *z, *zOut; | 1073 unsigned char *z, *zOut; |
| 1049 int i; | 1074 int i; |
| 1050 zOut = z = sqlite3_malloc( argc*4+1 ); | 1075 zOut = z = sqlite3_malloc64( argc*4+1 ); |
| 1051 if( z==0 ){ | 1076 if( z==0 ){ |
| 1052 sqlite3_result_error_nomem(context); | 1077 sqlite3_result_error_nomem(context); |
| 1053 return; | 1078 return; |
| 1054 } | 1079 } |
| 1055 for(i=0; i<argc; i++){ | 1080 for(i=0; i<argc; i++){ |
| 1056 sqlite3_int64 x; | 1081 sqlite3_int64 x; |
| 1057 unsigned c; | 1082 unsigned c; |
| 1058 x = sqlite3_value_int64(argv[i]); | 1083 x = sqlite3_value_int64(argv[i]); |
| 1059 if( x<0 || x>0x10ffff ) x = 0xfffd; | 1084 if( x<0 || x>0x10ffff ) x = 0xfffd; |
| 1060 c = (unsigned)(x & 0x1fffff); | 1085 c = (unsigned)(x & 0x1fffff); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 | 1133 |
| 1109 /* | 1134 /* |
| 1110 ** The zeroblob(N) function returns a zero-filled blob of size N bytes. | 1135 ** The zeroblob(N) function returns a zero-filled blob of size N bytes. |
| 1111 */ | 1136 */ |
| 1112 static void zeroblobFunc( | 1137 static void zeroblobFunc( |
| 1113 sqlite3_context *context, | 1138 sqlite3_context *context, |
| 1114 int argc, | 1139 int argc, |
| 1115 sqlite3_value **argv | 1140 sqlite3_value **argv |
| 1116 ){ | 1141 ){ |
| 1117 i64 n; | 1142 i64 n; |
| 1118 sqlite3 *db = sqlite3_context_db_handle(context); | 1143 int rc; |
| 1119 assert( argc==1 ); | 1144 assert( argc==1 ); |
| 1120 UNUSED_PARAMETER(argc); | 1145 UNUSED_PARAMETER(argc); |
| 1121 n = sqlite3_value_int64(argv[0]); | 1146 n = sqlite3_value_int64(argv[0]); |
| 1122 testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] ); | 1147 if( n<0 ) n = 0; |
| 1123 testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); | 1148 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ |
| 1124 if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | 1149 if( rc ){ |
| 1125 sqlite3_result_error_toobig(context); | 1150 sqlite3_result_error_code(context, rc); |
| 1126 }else{ | |
| 1127 sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */ | |
| 1128 } | 1151 } |
| 1129 } | 1152 } |
| 1130 | 1153 |
| 1131 /* | 1154 /* |
| 1132 ** The replace() function. Three arguments are all strings: call | 1155 ** The replace() function. Three arguments are all strings: call |
| 1133 ** them A, B, and C. The result is also a string which is derived | 1156 ** them A, B, and C. The result is also a string which is derived |
| 1134 ** from A by replacing every occurrence of B with C. The match | 1157 ** from A by replacing every occurrence of B with C. The match |
| 1135 ** must be exact. Collating sequences are not used. | 1158 ** must be exact. Collating sequences are not used. |
| 1136 */ | 1159 */ |
| 1137 static void replaceFunc( | 1160 static void replaceFunc( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1188 sqlite3 *db = sqlite3_context_db_handle(context); | 1211 sqlite3 *db = sqlite3_context_db_handle(context); |
| 1189 nOut += nRep - nPattern; | 1212 nOut += nRep - nPattern; |
| 1190 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); | 1213 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 1191 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); | 1214 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 1192 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | 1215 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 1193 sqlite3_result_error_toobig(context); | 1216 sqlite3_result_error_toobig(context); |
| 1194 sqlite3_free(zOut); | 1217 sqlite3_free(zOut); |
| 1195 return; | 1218 return; |
| 1196 } | 1219 } |
| 1197 zOld = zOut; | 1220 zOld = zOut; |
| 1198 zOut = sqlite3_realloc(zOut, (int)nOut); | 1221 zOut = sqlite3_realloc64(zOut, (int)nOut); |
| 1199 if( zOut==0 ){ | 1222 if( zOut==0 ){ |
| 1200 sqlite3_result_error_nomem(context); | 1223 sqlite3_result_error_nomem(context); |
| 1201 sqlite3_free(zOld); | 1224 sqlite3_free(zOld); |
| 1202 return; | 1225 return; |
| 1203 } | 1226 } |
| 1204 memcpy(&zOut[j], zRep, nRep); | 1227 memcpy(&zOut[j], zRep, nRep); |
| 1205 j += nRep; | 1228 j += nRep; |
| 1206 i += nPattern-1; | 1229 i += nPattern-1; |
| 1207 } | 1230 } |
| 1208 } | 1231 } |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1550 const char *zVal; | 1573 const char *zVal; |
| 1551 StrAccum *pAccum; | 1574 StrAccum *pAccum; |
| 1552 const char *zSep; | 1575 const char *zSep; |
| 1553 int nVal, nSep; | 1576 int nVal, nSep; |
| 1554 assert( argc==1 || argc==2 ); | 1577 assert( argc==1 || argc==2 ); |
| 1555 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; | 1578 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 1556 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); | 1579 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); |
| 1557 | 1580 |
| 1558 if( pAccum ){ | 1581 if( pAccum ){ |
| 1559 sqlite3 *db = sqlite3_context_db_handle(context); | 1582 sqlite3 *db = sqlite3_context_db_handle(context); |
| 1560 int firstTerm = pAccum->useMalloc==0; | 1583 int firstTerm = pAccum->mxAlloc==0; |
| 1561 pAccum->useMalloc = 2; | |
| 1562 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; | 1584 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 1563 if( !firstTerm ){ | 1585 if( !firstTerm ){ |
| 1564 if( argc==2 ){ | 1586 if( argc==2 ){ |
| 1565 zSep = (char*)sqlite3_value_text(argv[1]); | 1587 zSep = (char*)sqlite3_value_text(argv[1]); |
| 1566 nSep = sqlite3_value_bytes(argv[1]); | 1588 nSep = sqlite3_value_bytes(argv[1]); |
| 1567 }else{ | 1589 }else{ |
| 1568 zSep = ","; | 1590 zSep = ","; |
| 1569 nSep = 1; | 1591 nSep = 1; |
| 1570 } | 1592 } |
| 1571 if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); | 1593 if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1635 setLikeOptFlag(db, "like", | 1657 setLikeOptFlag(db, "like", |
| 1636 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); | 1658 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
| 1637 } | 1659 } |
| 1638 | 1660 |
| 1639 /* | 1661 /* |
| 1640 ** pExpr points to an expression which implements a function. If | 1662 ** pExpr points to an expression which implements a function. If |
| 1641 ** it is appropriate to apply the LIKE optimization to that function | 1663 ** it is appropriate to apply the LIKE optimization to that function |
| 1642 ** then set aWc[0] through aWc[2] to the wildcard characters and | 1664 ** then set aWc[0] through aWc[2] to the wildcard characters and |
| 1643 ** return TRUE. If the function is not a LIKE-style function then | 1665 ** return TRUE. If the function is not a LIKE-style function then |
| 1644 ** return FALSE. | 1666 ** return FALSE. |
| 1667 ** |
| 1668 ** *pIsNocase is set to true if uppercase and lowercase are equivalent for |
| 1669 ** the function (default for LIKE). If the function makes the distinction |
| 1670 ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to |
| 1671 ** false. |
| 1645 */ | 1672 */ |
| 1646 int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ | 1673 int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ |
| 1647 FuncDef *pDef; | 1674 FuncDef *pDef; |
| 1648 if( pExpr->op!=TK_FUNCTION | 1675 if( pExpr->op!=TK_FUNCTION |
| 1649 || !pExpr->x.pList | 1676 || !pExpr->x.pList |
| 1650 || pExpr->x.pList->nExpr!=2 | 1677 || pExpr->x.pList->nExpr!=2 |
| 1651 ){ | 1678 ){ |
| 1652 return 0; | 1679 return 0; |
| 1653 } | 1680 } |
| 1654 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | 1681 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1721 FUNCTION(coalesce, 0, 0, 0, 0 ), | 1748 FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 1722 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), | 1749 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), |
| 1723 FUNCTION(hex, 1, 0, 0, hexFunc ), | 1750 FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 1724 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), | 1751 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), |
| 1725 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), | 1752 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 1726 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), | 1753 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 1727 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), | 1754 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 1728 VFUNCTION(random, 0, 0, 0, randomFunc ), | 1755 VFUNCTION(random, 0, 0, 0, randomFunc ), |
| 1729 VFUNCTION(randomblob, 1, 0, 0, randomBlob ), | 1756 VFUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 1730 FUNCTION(nullif, 2, 0, 1, nullifFunc ), | 1757 FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 1731 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), | 1758 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 1732 FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), | 1759 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| 1733 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), | 1760 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), |
| 1734 #if SQLITE_USER_AUTHENTICATION | 1761 #if SQLITE_USER_AUTHENTICATION |
| 1735 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), | 1762 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), |
| 1736 #endif | 1763 #endif |
| 1737 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS | 1764 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 1738 FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), | 1765 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), |
| 1739 FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), | 1766 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), |
| 1740 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ | 1767 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 1741 FUNCTION(quote, 1, 0, 0, quoteFunc ), | 1768 FUNCTION(quote, 1, 0, 0, quoteFunc ), |
| 1742 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), | 1769 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), |
| 1743 VFUNCTION(changes, 0, 0, 0, changes ), | 1770 VFUNCTION(changes, 0, 0, 0, changes ), |
| 1744 VFUNCTION(total_changes, 0, 0, 0, total_changes ), | 1771 VFUNCTION(total_changes, 0, 0, 0, total_changes ), |
| 1745 FUNCTION(replace, 3, 0, 0, replaceFunc ), | 1772 FUNCTION(replace, 3, 0, 0, replaceFunc ), |
| 1746 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), | 1773 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), |
| 1747 #ifdef SQLITE_SOUNDEX | 1774 #ifdef SQLITE_SOUNDEX |
| 1748 FUNCTION(soundex, 1, 0, 0, soundexFunc ), | 1775 FUNCTION(soundex, 1, 0, 0, soundexFunc ), |
| 1749 #endif | 1776 #endif |
| 1750 #ifndef SQLITE_OMIT_LOAD_EXTENSION | 1777 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 1751 FUNCTION(load_extension, 1, 0, 0, loadExt ), | 1778 VFUNCTION(load_extension, 1, 0, 0, loadExt ), |
| 1752 FUNCTION(load_extension, 2, 0, 0, loadExt ), | 1779 VFUNCTION(load_extension, 2, 0, 0, loadExt ), |
| 1753 #endif | 1780 #endif |
| 1754 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), | 1781 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), |
| 1755 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), | 1782 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), |
| 1756 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), | 1783 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), |
| 1757 AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, | 1784 AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, |
| 1758 SQLITE_FUNC_COUNT ), | 1785 SQLITE_FUNC_COUNT ), |
| 1759 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), | 1786 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), |
| 1760 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), | 1787 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), |
| 1761 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), | 1788 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), |
| 1762 | 1789 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1778 sqlite3FuncDefInsert(pHash, &aFunc[i]); | 1805 sqlite3FuncDefInsert(pHash, &aFunc[i]); |
| 1779 } | 1806 } |
| 1780 sqlite3RegisterDateTimeFunctions(); | 1807 sqlite3RegisterDateTimeFunctions(); |
| 1781 #ifndef SQLITE_OMIT_ALTERTABLE | 1808 #ifndef SQLITE_OMIT_ALTERTABLE |
| 1782 sqlite3AlterFunctions(); | 1809 sqlite3AlterFunctions(); |
| 1783 #endif | 1810 #endif |
| 1784 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) | 1811 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) |
| 1785 sqlite3AnalyzeFunctions(); | 1812 sqlite3AnalyzeFunctions(); |
| 1786 #endif | 1813 #endif |
| 1787 } | 1814 } |
| OLD | NEW |