1dc04c583Sdrh /* 2dc04c583Sdrh ** 2002 February 23 3dc04c583Sdrh ** 4dc04c583Sdrh ** The author disclaims copyright to this source code. In place of 5dc04c583Sdrh ** a legal notice, here is a blessing: 6dc04c583Sdrh ** 7dc04c583Sdrh ** May you do good and not evil. 8dc04c583Sdrh ** May you find forgiveness for yourself and forgive others. 9dc04c583Sdrh ** May you share freely, never taking more than you give. 10dc04c583Sdrh ** 11dc04c583Sdrh ************************************************************************* 1260ec914cSpeter.d.reid ** This file contains the C-language implementations for many of the SQL 13ede7ae31Sdrh ** functions of SQLite. (Some function, and in particular the date and 14ede7ae31Sdrh ** time functions, are implemented separately.) 15dc04c583Sdrh */ 16b659e9bfSdrh #include "sqliteInt.h" 17d3a149efSdrh #include <stdlib.h> 180bce8354Sdrh #include <assert.h> 1905d7bfd0Sdrh #include <math.h> 2088208050Sdanielk1977 #include "vdbeInt.h" 210bce8354Sdrh 2255ef4d97Sdrh /* 2355ef4d97Sdrh ** Return the collating function associated with a function. 2455ef4d97Sdrh */ 25dc1bdc4fSdanielk1977 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 26a9e03b1bSdrh VdbeOp *pOp; 27a9e03b1bSdrh assert( context->pVdbe!=0 ); 28a9e03b1bSdrh pOp = &context->pVdbe->aOp[context->iOp-1]; 29a15cc47fSdrh assert( pOp->opcode==OP_CollSeq ); 30a15cc47fSdrh assert( pOp->p4type==P4_COLLSEQ ); 31a15cc47fSdrh return pOp->p4.pColl; 32dc1bdc4fSdanielk1977 } 33dc1bdc4fSdanielk1977 340bce8354Sdrh /* 357a95789cSdrh ** Indicate that the accumulator load should be skipped on this 367a95789cSdrh ** iteration of the aggregate loop. 377a95789cSdrh */ 387a95789cSdrh static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ 3921d59784Sdrh assert( context->isError<=0 ); 4021d59784Sdrh context->isError = -1; 417a95789cSdrh context->skipFlag = 1; 427a95789cSdrh } 437a95789cSdrh 447a95789cSdrh /* 450bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions 460bce8354Sdrh */ 47f9b596ebSdrh static void minmaxFunc( 48f9b596ebSdrh sqlite3_context *context, 49f9b596ebSdrh int argc, 50f9b596ebSdrh sqlite3_value **argv 51f9b596ebSdrh ){ 520bce8354Sdrh int i; 53268380caSdrh int mask; /* 0 for min() or 0xffffffff for max() */ 54f9b596ebSdrh int iBest; 55dc1bdc4fSdanielk1977 CollSeq *pColl; 560bce8354Sdrh 5765595cd6Sdrh assert( argc>1 ); 58c44af71cSdrh mask = sqlite3_user_data(context)==0 ? 0 : -1; 59dc1bdc4fSdanielk1977 pColl = sqlite3GetFuncCollSeq(context); 60dc1bdc4fSdanielk1977 assert( pColl ); 61c572ef7fSdanielk1977 assert( mask==-1 || mask==0 ); 62f9b596ebSdrh iBest = 0; 639c054830Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 64f9b596ebSdrh for(i=1; i<argc; i++){ 659c054830Sdrh if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 66dc1bdc4fSdanielk1977 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 6765595cd6Sdrh testcase( mask==0 ); 68f9b596ebSdrh iBest = i; 690bce8354Sdrh } 700bce8354Sdrh } 71f4479501Sdrh sqlite3_result_value(context, argv[iBest]); 720bce8354Sdrh } 730bce8354Sdrh 74268380caSdrh /* 75268380caSdrh ** Return the type of the argument. 76268380caSdrh */ 77f9b596ebSdrh static void typeofFunc( 78f9b596ebSdrh sqlite3_context *context, 7962c14b34Sdanielk1977 int NotUsed, 80f9b596ebSdrh sqlite3_value **argv 81f9b596ebSdrh ){ 829d8e401cSdrh static const char *azType[] = { "integer", "real", "text", "blob", "null" }; 839d8e401cSdrh int i = sqlite3_value_type(argv[0]) - 1; 8462c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 859d8e401cSdrh assert( i>=0 && i<ArraySize(azType) ); 869d8e401cSdrh assert( SQLITE_INTEGER==1 ); 879d8e401cSdrh assert( SQLITE_FLOAT==2 ); 889d8e401cSdrh assert( SQLITE_TEXT==3 ); 899d8e401cSdrh assert( SQLITE_BLOB==4 ); 909d8e401cSdrh assert( SQLITE_NULL==5 ); 913cef3649Sdrh /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns 923cef3649Sdrh ** the datatype code for the initial datatype of the sqlite3_value object 933cef3649Sdrh ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, 943cef3649Sdrh ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */ 959d8e401cSdrh sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC); 960bce8354Sdrh } 970bce8354Sdrh 985708d2deSdrh 995708d2deSdrh /* 1000bce8354Sdrh ** Implementation of the length() function 1010bce8354Sdrh */ 102f9b596ebSdrh static void lengthFunc( 103f9b596ebSdrh sqlite3_context *context, 104f9b596ebSdrh int argc, 105f9b596ebSdrh sqlite3_value **argv 106f9b596ebSdrh ){ 1070bce8354Sdrh assert( argc==1 ); 108f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 109f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1109c054830Sdrh case SQLITE_BLOB: 1119c054830Sdrh case SQLITE_INTEGER: 1129c054830Sdrh case SQLITE_FLOAT: { 113f4479501Sdrh sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 114f9b596ebSdrh break; 115f9b596ebSdrh } 1169c054830Sdrh case SQLITE_TEXT: { 1172646da7eSdrh const unsigned char *z = sqlite3_value_text(argv[0]); 1187ea3469eSdrh const unsigned char *z0; 1197ea3469eSdrh unsigned char c; 1207a521cfbSdrh if( z==0 ) return; 1217ea3469eSdrh z0 = z; 1227ea3469eSdrh while( (c = *z)!=0 ){ 1237ea3469eSdrh z++; 1247ea3469eSdrh if( c>=0xc0 ){ 1257ea3469eSdrh while( (*z & 0xc0)==0x80 ){ z++; z0++; } 1264a919118Sdrh } 1277ea3469eSdrh } 1287ea3469eSdrh sqlite3_result_int(context, (int)(z-z0)); 129f9b596ebSdrh break; 130f9b596ebSdrh } 131f9b596ebSdrh default: { 132f9b596ebSdrh sqlite3_result_null(context); 133f9b596ebSdrh break; 134f9b596ebSdrh } 135f9b596ebSdrh } 1360bce8354Sdrh } 1370bce8354Sdrh 1380bce8354Sdrh /* 1392ba3cccfSdrh ** Implementation of the abs() function. 1402ba3cccfSdrh ** 1412ba3cccfSdrh ** IMP: R-23979-26855 The abs(X) function returns the absolute value of 1422ba3cccfSdrh ** the numeric argument X. 1430bce8354Sdrh */ 1440ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1450bce8354Sdrh assert( argc==1 ); 146f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 147f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1489c054830Sdrh case SQLITE_INTEGER: { 149f93bbbeaSdanielk1977 i64 iVal = sqlite3_value_int64(argv[0]); 15052fc849aSdrh if( iVal<0 ){ 151693e6719Sdrh if( iVal==SMALLEST_INT64 ){ 152eb091cdfSdrh /* IMP: R-31676-45509 If X is the integer -9223372036854775808 153eb091cdfSdrh ** then abs(X) throws an integer overflow error since there is no 1542ba3cccfSdrh ** equivalent positive 64-bit two complement value. */ 15552fc849aSdrh sqlite3_result_error(context, "integer overflow", -1); 15652fc849aSdrh return; 15752fc849aSdrh } 15852fc849aSdrh iVal = -iVal; 15952fc849aSdrh } 160f93bbbeaSdanielk1977 sqlite3_result_int64(context, iVal); 161f9b596ebSdrh break; 162f9b596ebSdrh } 1639c054830Sdrh case SQLITE_NULL: { 1642ba3cccfSdrh /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ 165f9b596ebSdrh sqlite3_result_null(context); 166f9b596ebSdrh break; 167f9b596ebSdrh } 168f9b596ebSdrh default: { 1692ba3cccfSdrh /* Because sqlite3_value_double() returns 0.0 if the argument is not 1702ba3cccfSdrh ** something that can be converted into a number, we have: 171643091f0Sdrh ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob 172643091f0Sdrh ** that cannot be converted to a numeric value. 1732ba3cccfSdrh */ 174f93bbbeaSdanielk1977 double rVal = sqlite3_value_double(argv[0]); 17552fc849aSdrh if( rVal<0 ) rVal = -rVal; 176f93bbbeaSdanielk1977 sqlite3_result_double(context, rVal); 177f9b596ebSdrh break; 178f9b596ebSdrh } 179f9b596ebSdrh } 1800bce8354Sdrh } 1810bce8354Sdrh 1820bce8354Sdrh /* 183d55e0729Sdrh ** Implementation of the instr() function. 184d55e0729Sdrh ** 185d55e0729Sdrh ** instr(haystack,needle) finds the first occurrence of needle 186d55e0729Sdrh ** in haystack and returns the number of previous characters plus 1, 187d55e0729Sdrh ** or 0 if needle does not occur within haystack. 188d55e0729Sdrh ** 189d55e0729Sdrh ** If both haystack and needle are BLOBs, then the result is one more than 190d55e0729Sdrh ** the number of bytes in haystack prior to the first occurrence of needle, 191d55e0729Sdrh ** or 0 if needle never occurs in haystack. 192d55e0729Sdrh */ 193d55e0729Sdrh static void instrFunc( 194d55e0729Sdrh sqlite3_context *context, 195d55e0729Sdrh int argc, 196d55e0729Sdrh sqlite3_value **argv 197d55e0729Sdrh ){ 198d55e0729Sdrh const unsigned char *zHaystack; 199d55e0729Sdrh const unsigned char *zNeedle; 200d55e0729Sdrh int nHaystack; 201d55e0729Sdrh int nNeedle; 202d55e0729Sdrh int typeHaystack, typeNeedle; 203d55e0729Sdrh int N = 1; 204d55e0729Sdrh int isText; 205c930b405Sdrh unsigned char firstChar; 206d55e0729Sdrh 20768c804b9Sdrh UNUSED_PARAMETER(argc); 208d55e0729Sdrh typeHaystack = sqlite3_value_type(argv[0]); 209d55e0729Sdrh typeNeedle = sqlite3_value_type(argv[1]); 210d55e0729Sdrh if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; 211d55e0729Sdrh nHaystack = sqlite3_value_bytes(argv[0]); 212d55e0729Sdrh nNeedle = sqlite3_value_bytes(argv[1]); 213895decf6Sdan if( nNeedle>0 ){ 214d55e0729Sdrh if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ 215d55e0729Sdrh zHaystack = sqlite3_value_blob(argv[0]); 216d55e0729Sdrh zNeedle = sqlite3_value_blob(argv[1]); 217d55e0729Sdrh isText = 0; 218d55e0729Sdrh }else{ 219d55e0729Sdrh zHaystack = sqlite3_value_text(argv[0]); 220d55e0729Sdrh zNeedle = sqlite3_value_text(argv[1]); 221d55e0729Sdrh isText = 1; 222d55e0729Sdrh } 223b30574bcSdrh if( zNeedle==0 || (nHaystack && zHaystack==0) ) return; 224c930b405Sdrh firstChar = zNeedle[0]; 225c930b405Sdrh while( nNeedle<=nHaystack 226c930b405Sdrh && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0) 227c930b405Sdrh ){ 228d55e0729Sdrh N++; 229d55e0729Sdrh do{ 230d55e0729Sdrh nHaystack--; 231d55e0729Sdrh zHaystack++; 232d55e0729Sdrh }while( isText && (zHaystack[0]&0xc0)==0x80 ); 233d55e0729Sdrh } 234d55e0729Sdrh if( nNeedle>nHaystack ) N = 0; 235895decf6Sdan } 236d55e0729Sdrh sqlite3_result_int(context, N); 237d55e0729Sdrh } 238d55e0729Sdrh 239d55e0729Sdrh /* 240a5c1416dSdrh ** Implementation of the printf() function. 241a5c1416dSdrh */ 242a5c1416dSdrh static void printfFunc( 243a5c1416dSdrh sqlite3_context *context, 244a5c1416dSdrh int argc, 245a5c1416dSdrh sqlite3_value **argv 246a5c1416dSdrh ){ 247a5c1416dSdrh PrintfArguments x; 248a5c1416dSdrh StrAccum str; 249a5c1416dSdrh const char *zFormat; 250a5c1416dSdrh int n; 251c0490572Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 252a5c1416dSdrh 253a5c1416dSdrh if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 254a5c1416dSdrh x.nArg = argc-1; 255a5c1416dSdrh x.nUsed = 0; 256a5c1416dSdrh x.apArg = argv+1; 257c0490572Sdrh sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); 2585f4a686fSdrh str.printfFlags = SQLITE_PRINTF_SQLFUNC; 2590cdbe1aeSdrh sqlite3_str_appendf(&str, zFormat, &x); 260a5c1416dSdrh n = str.nChar; 261a5c1416dSdrh sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, 262a5c1416dSdrh SQLITE_DYNAMIC); 263a5c1416dSdrh } 264a5c1416dSdrh } 265a5c1416dSdrh 266a5c1416dSdrh /* 267f764e6fcSdrh ** Implementation of the substr() function. 268f764e6fcSdrh ** 269f764e6fcSdrh ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. 270f764e6fcSdrh ** p1 is 1-indexed. So substr(x,1,1) returns the first character 271f764e6fcSdrh ** of x. If x is text, then we actually count UTF-8 characters. 272f764e6fcSdrh ** If x is a blob, then we count bytes. 273f764e6fcSdrh ** 274f764e6fcSdrh ** If p1 is negative, then we begin abs(p1) from the end of x[]. 275779b8f12Sshaneh ** 276f7b5496eSdrh ** If p2 is negative, return the p2 characters preceding p1. 2770bce8354Sdrh */ 278f9b596ebSdrh static void substrFunc( 279f9b596ebSdrh sqlite3_context *context, 280f9b596ebSdrh int argc, 281f9b596ebSdrh sqlite3_value **argv 282f9b596ebSdrh ){ 2832646da7eSdrh const unsigned char *z; 2842646da7eSdrh const unsigned char *z2; 285023ae03aSdrh int len; 286f764e6fcSdrh int p0type; 287023ae03aSdrh i64 p1, p2; 28865595cd6Sdrh int negP2 = 0; 289f9b596ebSdrh 29064f31519Sdrh assert( argc==3 || argc==2 ); 2918198d254Sdrh if( sqlite3_value_type(argv[1])==SQLITE_NULL 2928198d254Sdrh || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) 2938198d254Sdrh ){ 2948198d254Sdrh return; 2958198d254Sdrh } 296f764e6fcSdrh p0type = sqlite3_value_type(argv[0]); 2974adc4cb9Sdrh p1 = sqlite3_value_int(argv[1]); 298f764e6fcSdrh if( p0type==SQLITE_BLOB ){ 299f764e6fcSdrh len = sqlite3_value_bytes(argv[0]); 300f764e6fcSdrh z = sqlite3_value_blob(argv[0]); 301f764e6fcSdrh if( z==0 ) return; 3021f0feef8Sdrh assert( len==sqlite3_value_bytes(argv[0]) ); 303f764e6fcSdrh }else{ 3044f26d6c4Sdrh z = sqlite3_value_text(argv[0]); 3050bce8354Sdrh if( z==0 ) return; 3064a919118Sdrh len = 0; 3074adc4cb9Sdrh if( p1<0 ){ 3084a919118Sdrh for(z2=z; *z2; len++){ 3094a919118Sdrh SQLITE_SKIP_UTF8(z2); 3104a919118Sdrh } 311f764e6fcSdrh } 3124adc4cb9Sdrh } 313883ad049Sdrh #ifdef SQLITE_SUBSTR_COMPATIBILITY 314883ad049Sdrh /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as 315883ad049Sdrh ** as substr(X,1,N) - it returns the first N characters of X. This 316883ad049Sdrh ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] 317883ad049Sdrh ** from 2009-02-02 for compatibility of applications that exploited the 318883ad049Sdrh ** old buggy behavior. */ 319883ad049Sdrh if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ 320883ad049Sdrh #endif 32164f31519Sdrh if( argc==3 ){ 32251ad0ecdSdanielk1977 p2 = sqlite3_value_int(argv[2]); 32365595cd6Sdrh if( p2<0 ){ 32465595cd6Sdrh p2 = -p2; 32565595cd6Sdrh negP2 = 1; 32665595cd6Sdrh } 32764f31519Sdrh }else{ 328bb4957f8Sdrh p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; 32964f31519Sdrh } 3300bce8354Sdrh if( p1<0 ){ 33189425d5eSdrh p1 += len; 332653bc759Sdrh if( p1<0 ){ 333653bc759Sdrh p2 += p1; 33465595cd6Sdrh if( p2<0 ) p2 = 0; 335653bc759Sdrh p1 = 0; 336653bc759Sdrh } 3370bce8354Sdrh }else if( p1>0 ){ 3380bce8354Sdrh p1--; 33965595cd6Sdrh }else if( p2>0 ){ 34065595cd6Sdrh p2--; 3410bce8354Sdrh } 34265595cd6Sdrh if( negP2 ){ 34365595cd6Sdrh p1 -= p2; 3444e79c594Sdrh if( p1<0 ){ 3454e79c594Sdrh p2 += p1; 3464e79c594Sdrh p1 = 0; 3474e79c594Sdrh } 3484e79c594Sdrh } 34965595cd6Sdrh assert( p1>=0 && p2>=0 ); 350f764e6fcSdrh if( p0type!=SQLITE_BLOB ){ 3514a919118Sdrh while( *z && p1 ){ 3524a919118Sdrh SQLITE_SKIP_UTF8(z); 3534a919118Sdrh p1--; 3540bce8354Sdrh } 3554a919118Sdrh for(z2=z; *z2 && p2; p2--){ 3564a919118Sdrh SQLITE_SKIP_UTF8(z2); 3570bce8354Sdrh } 358bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, 359bbf483f8Sdrh SQLITE_UTF8); 360f764e6fcSdrh }else{ 3614adc4cb9Sdrh if( p1+p2>len ){ 3624adc4cb9Sdrh p2 = len-p1; 3634adc4cb9Sdrh if( p2<0 ) p2 = 0; 3644adc4cb9Sdrh } 365bbf483f8Sdrh sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); 366f764e6fcSdrh } 3670bce8354Sdrh } 3680bce8354Sdrh 3690bce8354Sdrh /* 3700bce8354Sdrh ** Implementation of the round() function 3710bce8354Sdrh */ 372fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 3730ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 37451ad0ecdSdanielk1977 int n = 0; 3750bce8354Sdrh double r; 37650d654daSdrh char *zBuf; 3770bce8354Sdrh assert( argc==1 || argc==2 ); 37851ad0ecdSdanielk1977 if( argc==2 ){ 3799c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 38051ad0ecdSdanielk1977 n = sqlite3_value_int(argv[1]); 3810bce8354Sdrh if( n>30 ) n = 30; 3820bce8354Sdrh if( n<0 ) n = 0; 38351ad0ecdSdanielk1977 } 384d589a92aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 3854f26d6c4Sdrh r = sqlite3_value_double(argv[0]); 386147e176aSshaneh /* If Y==0 and X will fit in a 64-bit int, 387147e176aSshaneh ** handle the rounding directly, 388147e176aSshaneh ** otherwise use printf. 389147e176aSshaneh */ 39084422db9Sdrh if( r<-4503599627370496.0 || r>+4503599627370496.0 ){ 39184422db9Sdrh /* The value has no fractional part so there is nothing to round */ 39284422db9Sdrh }else if( n==0 ){ 39384422db9Sdrh r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); 394147e176aSshaneh }else{ 39550d654daSdrh zBuf = sqlite3_mprintf("%.*f",n,r); 39650d654daSdrh if( zBuf==0 ){ 39750d654daSdrh sqlite3_result_error_nomem(context); 398147e176aSshaneh return; 399147e176aSshaneh } 400*55700bcdSdrh sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); 40150d654daSdrh sqlite3_free(zBuf); 4020bce8354Sdrh } 403147e176aSshaneh sqlite3_result_double(context, r); 40450d654daSdrh } 405fbd60f82Sshane #endif 406dc04c583Sdrh 40726783a58Sdanielk1977 /* 408f3cdcdccSdrh ** Allocate nByte bytes of space using sqlite3Malloc(). If the 40926783a58Sdanielk1977 ** allocation fails, call sqlite3_result_error_nomem() to notify 41027e62dbeSdrh ** the database handle that malloc() has failed and return NULL. 41127e62dbeSdrh ** If nByte is larger than the maximum string or blob length, then 41227e62dbeSdrh ** raise an SQLITE_TOOBIG exception and return NULL. 41326783a58Sdanielk1977 */ 414b1a6c3c1Sdrh static void *contextMalloc(sqlite3_context *context, i64 nByte){ 415bb4957f8Sdrh char *z; 41627e62dbeSdrh sqlite3 *db = sqlite3_context_db_handle(context); 417ef31c6aaSdrh assert( nByte>0 ); 41827e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); 41927e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); 42027e62dbeSdrh if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 421bb4957f8Sdrh sqlite3_result_error_toobig(context); 422bb4957f8Sdrh z = 0; 423bb4957f8Sdrh }else{ 424da4ca9d1Sdrh z = sqlite3Malloc(nByte); 425ef31c6aaSdrh if( !z ){ 426a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 427a1644fd8Sdanielk1977 } 428bb4957f8Sdrh } 429a1644fd8Sdanielk1977 return z; 430a1644fd8Sdanielk1977 } 431a1644fd8Sdanielk1977 432dc04c583Sdrh /* 433dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 434dc04c583Sdrh */ 4350ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 4367a521cfbSdrh char *z1; 4377a521cfbSdrh const char *z2; 4389310ef23Sdrh int i, n; 4391d34fdecSdrh UNUSED_PARAMETER(argc); 4407a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4411f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4421f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4431f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4447a521cfbSdrh if( z2 ){ 445b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4467a521cfbSdrh if( z1 ){ 447df901d34Sdrh for(i=0; i<n; i++){ 448df901d34Sdrh z1[i] = (char)sqlite3Toupper(z2[i]); 449dc04c583Sdrh } 450df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4517a521cfbSdrh } 4527a521cfbSdrh } 453dc04c583Sdrh } 4540ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 455df901d34Sdrh char *z1; 4567a521cfbSdrh const char *z2; 4579310ef23Sdrh int i, n; 4581d34fdecSdrh UNUSED_PARAMETER(argc); 4597a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4601f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4611f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4621f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4637a521cfbSdrh if( z2 ){ 464b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4657a521cfbSdrh if( z1 ){ 466df901d34Sdrh for(i=0; i<n; i++){ 467df901d34Sdrh z1[i] = sqlite3Tolower(z2[i]); 468dc04c583Sdrh } 469df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4707a521cfbSdrh } 4717a521cfbSdrh } 472dc04c583Sdrh } 473dc04c583Sdrh 474ae6bb957Sdrh /* 475cca9f3d2Sdrh ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented 476cca9f3d2Sdrh ** as VDBE code so that unused argument values do not have to be computed. 477cca9f3d2Sdrh ** However, we still need some kind of function implementation for this 478cca9f3d2Sdrh ** routines in the function table. The noopFunc macro provides this. 479cca9f3d2Sdrh ** noopFunc will never be called so it doesn't matter what the implementation 480cca9f3d2Sdrh ** is. We might as well use the "version()" function as a substitute. 481ae6bb957Sdrh */ 482cca9f3d2Sdrh #define noopFunc versionFunc /* Substitute function - never called */ 4833212e182Sdrh 4843212e182Sdrh /* 485f9ffac96Sdrh ** Implementation of random(). Return a random integer. 486f9ffac96Sdrh */ 487f9b596ebSdrh static void randomFunc( 488f9b596ebSdrh sqlite3_context *context, 48962c14b34Sdanielk1977 int NotUsed, 49062c14b34Sdanielk1977 sqlite3_value **NotUsed2 491f9b596ebSdrh ){ 49252fc849aSdrh sqlite_int64 r; 49362c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 4942fa1868fSdrh sqlite3_randomness(sizeof(r), &r); 4953034e3d3Sdrh if( r<0 ){ 4963034e3d3Sdrh /* We need to prevent a random number of 0x8000000000000000 4973034e3d3Sdrh ** (or -9223372036854775808) since when you do abs() of that 4983034e3d3Sdrh ** number of you get the same value back again. To do this 4993034e3d3Sdrh ** in a way that is testable, mask the sign bit off of negative 5003034e3d3Sdrh ** values, resulting in a positive value. Then take the 5013034e3d3Sdrh ** 2s complement of that positive value. The end result can 5023034e3d3Sdrh ** therefore be no less than -9223372036854775807. 5033034e3d3Sdrh */ 504af8001bfSdrh r = -(r & LARGEST_INT64); 5053034e3d3Sdrh } 50652fc849aSdrh sqlite3_result_int64(context, r); 507f9ffac96Sdrh } 508f9ffac96Sdrh 509f9ffac96Sdrh /* 510137c728fSdrh ** Implementation of randomblob(N). Return a random blob 511137c728fSdrh ** that is N bytes long. 51263cf66f0Sdrh */ 513137c728fSdrh static void randomBlob( 51463cf66f0Sdrh sqlite3_context *context, 51563cf66f0Sdrh int argc, 51663cf66f0Sdrh sqlite3_value **argv 51763cf66f0Sdrh ){ 5183cb79202Sdrh sqlite3_int64 n; 519137c728fSdrh unsigned char *p; 52063cf66f0Sdrh assert( argc==1 ); 521f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 5223cb79202Sdrh n = sqlite3_value_int64(argv[0]); 523023ae03aSdrh if( n<1 ){ 524023ae03aSdrh n = 1; 525023ae03aSdrh } 526a1644fd8Sdanielk1977 p = contextMalloc(context, n); 52702d85836Sdrh if( p ){ 5282fa1868fSdrh sqlite3_randomness(n, p); 52917435752Sdrh sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 53002d85836Sdrh } 53163cf66f0Sdrh } 53263cf66f0Sdrh 53363cf66f0Sdrh /* 5346ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 53524b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 5366ed41ad7Sdrh */ 53751ad0ecdSdanielk1977 static void last_insert_rowid( 5380ae8b831Sdanielk1977 sqlite3_context *context, 53962c14b34Sdanielk1977 int NotUsed, 54062c14b34Sdanielk1977 sqlite3_value **NotUsed2 54151ad0ecdSdanielk1977 ){ 542fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 54362c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 544ab2f1f95Sdrh /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a 545ab2f1f95Sdrh ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface 546ab2f1f95Sdrh ** function. */ 547f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 5486ed41ad7Sdrh } 5496ed41ad7Sdrh 550f146a776Srdc /* 551ab2f1f95Sdrh ** Implementation of the changes() SQL function. 552ab2f1f95Sdrh ** 553ab2f1f95Sdrh ** IMP: R-62073-11209 The changes() SQL function is a wrapper 554ab2f1f95Sdrh ** around the sqlite3_changes() C/C++ function and hence follows the same 555ab2f1f95Sdrh ** rules for counting changes. 556f146a776Srdc */ 557b28af71aSdanielk1977 static void changes( 558f9b596ebSdrh sqlite3_context *context, 55962c14b34Sdanielk1977 int NotUsed, 56062c14b34Sdanielk1977 sqlite3_value **NotUsed2 561f9b596ebSdrh ){ 562fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 56362c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 564f4479501Sdrh sqlite3_result_int(context, sqlite3_changes(db)); 565b0c374ffSrdc } 566f146a776Srdc 567f146a776Srdc /* 568b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 569b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function. 570f146a776Srdc */ 571b28af71aSdanielk1977 static void total_changes( 5720ae8b831Sdanielk1977 sqlite3_context *context, 57362c14b34Sdanielk1977 int NotUsed, 57462c14b34Sdanielk1977 sqlite3_value **NotUsed2 57551ad0ecdSdanielk1977 ){ 576fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 57762c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 578ab2f1f95Sdrh /* IMP: R-52756-41993 This function is a wrapper around the 579ab2f1f95Sdrh ** sqlite3_total_changes() C/C++ interface. */ 580b28af71aSdanielk1977 sqlite3_result_int(context, sqlite3_total_changes(db)); 581b0c374ffSrdc } 582b0c374ffSrdc 5836ed41ad7Sdrh /* 5844e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 585d02eb1fdSdanielk1977 */ 5864e5ffc5fSdrh struct compareInfo { 58707e83472Sdrh u8 matchAll; /* "*" or "%" */ 58807e83472Sdrh u8 matchOne; /* "?" or "_" */ 58907e83472Sdrh u8 matchSet; /* "[" or 0 */ 59007e83472Sdrh u8 noCase; /* true to ignore case differences */ 591d02eb1fdSdanielk1977 }; 59255ef4d97Sdrh 593b9175aedSdrh /* 594b9175aedSdrh ** For LIKE and GLOB matching on EBCDIC machines, assume that every 595b0870486Sdrh ** character is exactly one byte in size. Also, provde the Utf8Read() 596b0870486Sdrh ** macro for fast reading of the next character in the common case where 597b0870486Sdrh ** the next character is ASCII. 598b9175aedSdrh */ 599b9175aedSdrh #if defined(SQLITE_EBCDIC) 60042610961Sdrh # define sqlite3Utf8Read(A) (*((*A)++)) 601b0870486Sdrh # define Utf8Read(A) (*(A++)) 602b9175aedSdrh #else 603b0870486Sdrh # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) 604b9175aedSdrh #endif 605b9175aedSdrh 6064e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 60770031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 60870031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 60955ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 61070031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 61170031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 61255ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 613d02eb1fdSdanielk1977 614d02eb1fdSdanielk1977 /* 615698a01caSdrh ** Possible error returns from patternMatch() 616698a01caSdrh */ 617698a01caSdrh #define SQLITE_MATCH 0 618698a01caSdrh #define SQLITE_NOMATCH 1 619698a01caSdrh #define SQLITE_NOWILDCARDMATCH 2 620698a01caSdrh 621698a01caSdrh /* 622698a01caSdrh ** Compare two UTF-8 strings for equality where the first string is 623698a01caSdrh ** a GLOB or LIKE expression. Return values: 624698a01caSdrh ** 625698a01caSdrh ** SQLITE_MATCH: Match 626698a01caSdrh ** SQLITE_NOMATCH: No match 627698a01caSdrh ** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards. 6280ac65892Sdrh ** 6294e5ffc5fSdrh ** Globbing rules: 6300ac65892Sdrh ** 6314e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 632d02eb1fdSdanielk1977 ** 6334e5ffc5fSdrh ** '?' Matches exactly one character. 6344e5ffc5fSdrh ** 6354e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 6364e5ffc5fSdrh ** characters. 6374e5ffc5fSdrh ** 6384e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 6394e5ffc5fSdrh ** 6404e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 6414e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 6424e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 6434e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 6444e5ffc5fSdrh ** it the last character in the list. 6454e5ffc5fSdrh ** 6469fdfdc89Sdrh ** Like matching rules: 6479fdfdc89Sdrh ** 6489fdfdc89Sdrh ** '%' Matches any sequence of zero or more characters 6499fdfdc89Sdrh ** 6509fdfdc89Sdrh *** '_' Matches any one character 6519fdfdc89Sdrh ** 6529fdfdc89Sdrh ** Ec Where E is the "esc" character and c is any other 6539fdfdc89Sdrh ** character, including '%', '_', and esc, match exactly c. 6549fdfdc89Sdrh ** 655b0870486Sdrh ** The comments within this routine usually assume glob matching. 6569fdfdc89Sdrh ** 6574e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 6580ac65892Sdrh */ 6597c6303c0Sdanielk1977 static int patternCompare( 6604e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 6614e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 6627c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 663698a01caSdrh u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */ 66451ad0ecdSdanielk1977 ){ 6659fdfdc89Sdrh u32 c, c2; /* Next pattern and input string chars */ 6669fdfdc89Sdrh u32 matchOne = pInfo->matchOne; /* "?" or "_" */ 6679fdfdc89Sdrh u32 matchAll = pInfo->matchAll; /* "*" or "%" */ 6689fdfdc89Sdrh u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ 6699fdfdc89Sdrh const u8 *zEscaped = 0; /* One past the last escaped input char */ 670d02eb1fdSdanielk1977 671b0870486Sdrh while( (c = Utf8Read(zPattern))!=0 ){ 6729fdfdc89Sdrh if( c==matchAll ){ /* Match "*" */ 6739fdfdc89Sdrh /* Skip over multiple "*" characters in the pattern. If there 6749fdfdc89Sdrh ** are also "?" characters, skip those as well, but consume a 6759fdfdc89Sdrh ** single character of the input string for each "?" skipped */ 676b0870486Sdrh while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ 67742610961Sdrh if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ 678698a01caSdrh return SQLITE_NOWILDCARDMATCH; 679d02eb1fdSdanielk1977 } 6804e5ffc5fSdrh } 68166150956Sdrh if( c==0 ){ 682698a01caSdrh return SQLITE_MATCH; /* "*" at the end of the pattern matches */ 68388b3322fSdrh }else if( c==matchOther ){ 68407e83472Sdrh if( pInfo->matchSet==0 ){ 68542610961Sdrh c = sqlite3Utf8Read(&zPattern); 686698a01caSdrh if( c==0 ) return SQLITE_NOWILDCARDMATCH; 68788b3322fSdrh }else{ 6889fdfdc89Sdrh /* "[...]" immediately follows the "*". We have to do a slow 6899fdfdc89Sdrh ** recursive search in this case, but it is an unusual case. */ 69088b3322fSdrh assert( matchOther<0x80 ); /* '[' is a single-byte character */ 691698a01caSdrh while( *zString ){ 692698a01caSdrh int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther); 693698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 6944a919118Sdrh SQLITE_SKIP_UTF8(zString); 6954e5ffc5fSdrh } 696698a01caSdrh return SQLITE_NOWILDCARDMATCH; 69766150956Sdrh } 69888b3322fSdrh } 6999fdfdc89Sdrh 7009fdfdc89Sdrh /* At this point variable c contains the first character of the 7019fdfdc89Sdrh ** pattern string past the "*". Search in the input string for the 7021a4a7376Sdan ** first matching character and recursively continue the match from 7039fdfdc89Sdrh ** that point. 7049fdfdc89Sdrh ** 7059fdfdc89Sdrh ** For a case-insensitive search, set variable cx to be the same as 7069fdfdc89Sdrh ** c but in the other case and search the input string for either 7079fdfdc89Sdrh ** c or cx. 7089fdfdc89Sdrh */ 7099fdfdc89Sdrh if( c<=0x80 ){ 710eba21f9eSdrh char zStop[3]; 711698a01caSdrh int bMatch; 7129fdfdc89Sdrh if( noCase ){ 713eba21f9eSdrh zStop[0] = sqlite3Toupper(c); 714eba21f9eSdrh zStop[1] = sqlite3Tolower(c); 715eba21f9eSdrh zStop[2] = 0; 716ad7dd425Sdanielk1977 }else{ 717eba21f9eSdrh zStop[0] = c; 718eba21f9eSdrh zStop[1] = 0; 71966150956Sdrh } 720eba21f9eSdrh while(1){ 721eba21f9eSdrh zString += strcspn((const char*)zString, zStop); 722eba21f9eSdrh if( zString[0]==0 ) break; 723eba21f9eSdrh zString++; 724698a01caSdrh bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 725698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 7264e5ffc5fSdrh } 72788b3322fSdrh }else{ 728698a01caSdrh int bMatch; 729b0870486Sdrh while( (c2 = Utf8Read(zString))!=0 ){ 7309fdfdc89Sdrh if( c2!=c ) continue; 731698a01caSdrh bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 732698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 73366150956Sdrh } 73488b3322fSdrh } 735698a01caSdrh return SQLITE_NOWILDCARDMATCH; 7369fdfdc89Sdrh } 73788b3322fSdrh if( c==matchOther ){ 73807e83472Sdrh if( pInfo->matchSet==0 ){ 73988b3322fSdrh c = sqlite3Utf8Read(&zPattern); 740698a01caSdrh if( c==0 ) return SQLITE_NOMATCH; 7419fdfdc89Sdrh zEscaped = zPattern; 74288b3322fSdrh }else{ 7431aa4f3e5Sdrh u32 prior_c = 0; 7449fdfdc89Sdrh int seen = 0; 7459fdfdc89Sdrh int invert = 0; 74642610961Sdrh c = sqlite3Utf8Read(&zString); 747698a01caSdrh if( c==0 ) return SQLITE_NOMATCH; 74842610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 74966150956Sdrh if( c2=='^' ){ 75066150956Sdrh invert = 1; 75142610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 75266150956Sdrh } 7534e5ffc5fSdrh if( c2==']' ){ 7544e5ffc5fSdrh if( c==']' ) seen = 1; 75542610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7564e5ffc5fSdrh } 75766150956Sdrh while( c2 && c2!=']' ){ 75866150956Sdrh if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ 75942610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7604e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 7614e5ffc5fSdrh prior_c = 0; 76266150956Sdrh }else{ 76366150956Sdrh if( c==c2 ){ 7644e5ffc5fSdrh seen = 1; 76566150956Sdrh } 7664e5ffc5fSdrh prior_c = c2; 767d02eb1fdSdanielk1977 } 76842610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 769d02eb1fdSdanielk1977 } 77066150956Sdrh if( c2==0 || (seen ^ invert)==0 ){ 771698a01caSdrh return SQLITE_NOMATCH; 77266150956Sdrh } 77388b3322fSdrh continue; 774328d913cSdrh } 77588b3322fSdrh } 776b0870486Sdrh c2 = Utf8Read(zString); 77788b3322fSdrh if( c==c2 ) continue; 778c80937a5Sdrh if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){ 7799fdfdc89Sdrh continue; 7809fdfdc89Sdrh } 7819fdfdc89Sdrh if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; 782698a01caSdrh return SQLITE_NOMATCH; 7830ac65892Sdrh } 784698a01caSdrh return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH; 7854e5ffc5fSdrh } 7864e5ffc5fSdrh 78755ef4d97Sdrh /* 788698a01caSdrh ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and 789698a01caSdrh ** non-zero if there is no match. 79056282a5bSdrh */ 79156282a5bSdrh int sqlite3_strglob(const char *zGlobPattern, const char *zString){ 792698a01caSdrh return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); 79356282a5bSdrh } 79456282a5bSdrh 79556282a5bSdrh /* 796698a01caSdrh ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for 797698a01caSdrh ** a miss - like strcmp(). 7988b4a94adSdrh */ 7998b4a94adSdrh int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ 800698a01caSdrh return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); 8018b4a94adSdrh } 8028b4a94adSdrh 8038b4a94adSdrh /* 80455ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 80555ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 80655ef4d97Sdrh ** only. 80755ef4d97Sdrh */ 80855ef4d97Sdrh #ifdef SQLITE_TEST 80955ef4d97Sdrh int sqlite3_like_count = 0; 81055ef4d97Sdrh #endif 81155ef4d97Sdrh 8123f6b0874Sdanielk1977 8133f6b0874Sdanielk1977 /* 8143f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 8153f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 8163f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 8173f6b0874Sdanielk1977 ** 8183f6b0874Sdanielk1977 ** A LIKE B 8193f6b0874Sdanielk1977 ** 8203f6b0874Sdanielk1977 ** is implemented as like(B,A). 8213f6b0874Sdanielk1977 ** 82255ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 82355ef4d97Sdrh ** the GLOB operator. 8243f6b0874Sdanielk1977 */ 8253f6b0874Sdanielk1977 static void likeFunc( 8263f6b0874Sdanielk1977 sqlite3_context *context, 8273f6b0874Sdanielk1977 int argc, 8283f6b0874Sdanielk1977 sqlite3_value **argv 8293f6b0874Sdanielk1977 ){ 830beb818d1Sdrh const unsigned char *zA, *zB; 83107e83472Sdrh u32 escape; 83227e62dbeSdrh int nPat; 833bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 83407e83472Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 835beb818d1Sdrh 83641d2e66eSdrh #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS 83741d2e66eSdrh if( sqlite3_value_type(argv[0])==SQLITE_BLOB 83841d2e66eSdrh || sqlite3_value_type(argv[1])==SQLITE_BLOB 83941d2e66eSdrh ){ 84041d2e66eSdrh #ifdef SQLITE_TEST 84141d2e66eSdrh sqlite3_like_count++; 84241d2e66eSdrh #endif 84341d2e66eSdrh sqlite3_result_int(context, 0); 84441d2e66eSdrh return; 84541d2e66eSdrh } 84641d2e66eSdrh #endif 8471f0feef8Sdrh 848beb818d1Sdrh /* Limit the length of the LIKE or GLOB pattern to avoid problems 849beb818d1Sdrh ** of deep recursion and N*N behavior in patternCompare(). 850beb818d1Sdrh */ 85127e62dbeSdrh nPat = sqlite3_value_bytes(argv[0]); 85227e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); 85327e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); 85427e62dbeSdrh if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ 855beb818d1Sdrh sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); 856beb818d1Sdrh return; 857beb818d1Sdrh } 8587c6303c0Sdanielk1977 if( argc==3 ){ 8597c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 8607c6303c0Sdanielk1977 ** Otherwise, return an error. 8617c6303c0Sdanielk1977 */ 8627c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 8637a521cfbSdrh if( zEsc==0 ) return; 864ee85813cSdrh if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ 8657c6303c0Sdanielk1977 sqlite3_result_error(context, 8667c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 8677c6303c0Sdanielk1977 return; 8687c6303c0Sdanielk1977 } 86942610961Sdrh escape = sqlite3Utf8Read(&zEsc); 87007e83472Sdrh }else{ 87107e83472Sdrh escape = pInfo->matchSet; 8727c6303c0Sdanielk1977 } 873cf833239Sdrh zB = sqlite3_value_text(argv[0]); 874cf833239Sdrh zA = sqlite3_value_text(argv[1]); 8753f6b0874Sdanielk1977 if( zA && zB ){ 87655ef4d97Sdrh #ifdef SQLITE_TEST 87755ef4d97Sdrh sqlite3_like_count++; 87855ef4d97Sdrh #endif 879f49759bfSdrh sqlite3_result_int(context, 880f49759bfSdrh patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH); 88151ad0ecdSdanielk1977 } 8828912d106Sdrh } 8838912d106Sdrh 8848912d106Sdrh /* 8858912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 8868912d106Sdrh ** argument if the arguments are different. The result is NULL if the 8878912d106Sdrh ** arguments are equal to each other. 8888912d106Sdrh */ 889f9b596ebSdrh static void nullifFunc( 890f9b596ebSdrh sqlite3_context *context, 89162c14b34Sdanielk1977 int NotUsed, 892f9b596ebSdrh sqlite3_value **argv 893f9b596ebSdrh ){ 894dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 89562c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 896dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 897f4479501Sdrh sqlite3_result_value(context, argv[0]); 8988912d106Sdrh } 8990ac65892Sdrh } 9000ac65892Sdrh 901647cb0e1Sdrh /* 90247baebc2Sdrh ** Implementation of the sqlite_version() function. The result is the version 903647cb0e1Sdrh ** of the SQLite library that is running. 904647cb0e1Sdrh */ 905f9b596ebSdrh static void versionFunc( 906f9b596ebSdrh sqlite3_context *context, 90762c14b34Sdanielk1977 int NotUsed, 90862c14b34Sdanielk1977 sqlite3_value **NotUsed2 909f9b596ebSdrh ){ 91062c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 911ab2f1f95Sdrh /* IMP: R-48699-48617 This function is an SQL wrapper around the 912ab2f1f95Sdrh ** sqlite3_libversion() C-interface. */ 913ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); 914647cb0e1Sdrh } 915647cb0e1Sdrh 91647baebc2Sdrh /* 91747baebc2Sdrh ** Implementation of the sqlite_source_id() function. The result is a string 91847baebc2Sdrh ** that identifies the particular version of the source code used to build 91947baebc2Sdrh ** SQLite. 92047baebc2Sdrh */ 92147baebc2Sdrh static void sourceidFunc( 92247baebc2Sdrh sqlite3_context *context, 92347baebc2Sdrh int NotUsed, 92447baebc2Sdrh sqlite3_value **NotUsed2 92547baebc2Sdrh ){ 92647baebc2Sdrh UNUSED_PARAMETER2(NotUsed, NotUsed2); 927ab2f1f95Sdrh /* IMP: R-24470-31136 This function is an SQL wrapper around the 928ab2f1f95Sdrh ** sqlite3_sourceid() C interface. */ 929ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); 93047baebc2Sdrh } 93147baebc2Sdrh 932bdea6d13Sshaneh /* 9333ca84ef6Sdrh ** Implementation of the sqlite_log() function. This is a wrapper around 9343ca84ef6Sdrh ** sqlite3_log(). The return value is NULL. The function exists purely for 9353ca84ef6Sdrh ** its side-effects. 9363ca84ef6Sdrh */ 937840561f2Sdrh static void errlogFunc( 9383ca84ef6Sdrh sqlite3_context *context, 9393ca84ef6Sdrh int argc, 9403ca84ef6Sdrh sqlite3_value **argv 9413ca84ef6Sdrh ){ 9423ca84ef6Sdrh UNUSED_PARAMETER(argc); 9433ca84ef6Sdrh UNUSED_PARAMETER(context); 9443ca84ef6Sdrh sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); 9453ca84ef6Sdrh } 9463ca84ef6Sdrh 9473ca84ef6Sdrh /* 948dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_used() function. 949dc97a8cdSshaneh ** The result is an integer that identifies if the compiler option 950dc97a8cdSshaneh ** was used to build SQLite. 951bdea6d13Sshaneh */ 952dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 953dc97a8cdSshaneh static void compileoptionusedFunc( 954bdea6d13Sshaneh sqlite3_context *context, 955dc97a8cdSshaneh int argc, 956dc97a8cdSshaneh sqlite3_value **argv 957bdea6d13Sshaneh ){ 958dc97a8cdSshaneh const char *zOptName; 959dc97a8cdSshaneh assert( argc==1 ); 960dc97a8cdSshaneh UNUSED_PARAMETER(argc); 961a3e414cdSdrh /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL 962a3e414cdSdrh ** function is a wrapper around the sqlite3_compileoption_used() C/C++ 963a3e414cdSdrh ** function. 964a3e414cdSdrh */ 965264a2d4dSdrh if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 966dc97a8cdSshaneh sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); 967bdea6d13Sshaneh } 968dc97a8cdSshaneh } 969dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 970dc97a8cdSshaneh 971dc97a8cdSshaneh /* 972dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_get() function. 973dc97a8cdSshaneh ** The result is a string that identifies the compiler options 974dc97a8cdSshaneh ** used to build SQLite. 975dc97a8cdSshaneh */ 976dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 977dc97a8cdSshaneh static void compileoptiongetFunc( 978dc97a8cdSshaneh sqlite3_context *context, 979dc97a8cdSshaneh int argc, 980dc97a8cdSshaneh sqlite3_value **argv 981dc97a8cdSshaneh ){ 982dc97a8cdSshaneh int n; 983dc97a8cdSshaneh assert( argc==1 ); 984dc97a8cdSshaneh UNUSED_PARAMETER(argc); 985a3e414cdSdrh /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function 986a3e414cdSdrh ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. 987a3e414cdSdrh */ 988dc97a8cdSshaneh n = sqlite3_value_int(argv[0]); 989dc97a8cdSshaneh sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); 990dc97a8cdSshaneh } 991dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 992bdea6d13Sshaneh 993137c728fSdrh /* Array for converting from half-bytes (nybbles) into ASCII hex 994137c728fSdrh ** digits. */ 995137c728fSdrh static const char hexdigits[] = { 996137c728fSdrh '0', '1', '2', '3', '4', '5', '6', '7', 997137c728fSdrh '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 998137c728fSdrh }; 999d641d646Sdanielk1977 100047394703Sdrh /* 100147394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 100247394703Sdrh ** argument. If the argument is numeric, the return value is the same as 100347394703Sdrh ** the argument. If the argument is NULL, the return value is the string 100447394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 100547394703Sdrh ** single-quote escapes. 100647394703Sdrh */ 10070ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1008a0df4ccfSdrh assert( argc==1 ); 10091d34fdecSdrh UNUSED_PARAMETER(argc); 1010f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 10119c054830Sdrh case SQLITE_FLOAT: { 101272b3fbc7Sdrh double r1, r2; 101372b3fbc7Sdrh char zBuf[50]; 10142b434a7eSmistachkin r1 = sqlite3_value_double(argv[0]); 101572b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); 101672b3fbc7Sdrh sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); 101772b3fbc7Sdrh if( r1!=r2 ){ 101872b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); 101972b3fbc7Sdrh } 102072b3fbc7Sdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 102172b3fbc7Sdrh break; 102272b3fbc7Sdrh } 102372b3fbc7Sdrh case SQLITE_INTEGER: { 1024f4479501Sdrh sqlite3_result_value(context, argv[0]); 1025f9b596ebSdrh break; 1026f9b596ebSdrh } 10273f41e976Sdanielk1977 case SQLITE_BLOB: { 10283f41e976Sdanielk1977 char *zText = 0; 10293f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 10301f0feef8Sdrh int nBlob = sqlite3_value_bytes(argv[0]); 10311f0feef8Sdrh assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1032b1a6c3c1Sdrh zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 1033a1644fd8Sdanielk1977 if( zText ){ 10343f41e976Sdanielk1977 int i; 10353f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 10363f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 10373f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 10383f41e976Sdanielk1977 } 10393f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 10403f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 10413f41e976Sdanielk1977 zText[0] = 'X'; 10423f41e976Sdanielk1977 zText[1] = '\''; 1043d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 104417435752Sdrh sqlite3_free(zText); 10453f41e976Sdanielk1977 } 10463f41e976Sdanielk1977 break; 10473f41e976Sdanielk1977 } 10489c054830Sdrh case SQLITE_TEXT: { 1049023ae03aSdrh int i,j; 1050023ae03aSdrh u64 n; 10512646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 105247394703Sdrh char *z; 1053f9b596ebSdrh 10547a521cfbSdrh if( zArg==0 ) return; 1055023ae03aSdrh for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 1056b1a6c3c1Sdrh z = contextMalloc(context, ((i64)i)+((i64)n)+3); 1057a1644fd8Sdanielk1977 if( z ){ 105847394703Sdrh z[0] = '\''; 105951ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 106051ad0ecdSdanielk1977 z[j++] = zArg[i]; 106151ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 106247394703Sdrh z[j++] = '\''; 106347394703Sdrh } 106447394703Sdrh } 106547394703Sdrh z[j++] = '\''; 106647394703Sdrh z[j] = 0; 1067a1644fd8Sdanielk1977 sqlite3_result_text(context, z, j, sqlite3_free); 1068a1644fd8Sdanielk1977 } 1069a0df4ccfSdrh break; 1070a0df4ccfSdrh } 1071a0df4ccfSdrh default: { 1072a0df4ccfSdrh assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 1073a0df4ccfSdrh sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 1074a0df4ccfSdrh break; 107547394703Sdrh } 107647394703Sdrh } 1077f9b596ebSdrh } 107847394703Sdrh 1079137c728fSdrh /* 1080d495d8c9Sdrh ** The unicode() function. Return the integer unicode code-point value 1081d495d8c9Sdrh ** for the first character of the input string. 1082d495d8c9Sdrh */ 1083d495d8c9Sdrh static void unicodeFunc( 1084d495d8c9Sdrh sqlite3_context *context, 1085d495d8c9Sdrh int argc, 1086d495d8c9Sdrh sqlite3_value **argv 1087d495d8c9Sdrh ){ 1088d495d8c9Sdrh const unsigned char *z = sqlite3_value_text(argv[0]); 10891d59d036Sdrh (void)argc; 1090d495d8c9Sdrh if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); 1091d495d8c9Sdrh } 1092d495d8c9Sdrh 1093d495d8c9Sdrh /* 1094d495d8c9Sdrh ** The char() function takes zero or more arguments, each of which is 1095d495d8c9Sdrh ** an integer. It constructs a string where each character of the string 1096d495d8c9Sdrh ** is the unicode character for the corresponding integer argument. 1097d495d8c9Sdrh */ 1098d495d8c9Sdrh static void charFunc( 1099d495d8c9Sdrh sqlite3_context *context, 1100d495d8c9Sdrh int argc, 1101d495d8c9Sdrh sqlite3_value **argv 1102d495d8c9Sdrh ){ 1103d495d8c9Sdrh unsigned char *z, *zOut; 1104d495d8c9Sdrh int i; 1105f3cdcdccSdrh zOut = z = sqlite3_malloc64( argc*4+1 ); 1106d495d8c9Sdrh if( z==0 ){ 1107d495d8c9Sdrh sqlite3_result_error_nomem(context); 1108d495d8c9Sdrh return; 1109d495d8c9Sdrh } 1110d495d8c9Sdrh for(i=0; i<argc; i++){ 1111c9545442Smistachkin sqlite3_int64 x; 1112d495d8c9Sdrh unsigned c; 1113d495d8c9Sdrh x = sqlite3_value_int64(argv[i]); 1114d495d8c9Sdrh if( x<0 || x>0x10ffff ) x = 0xfffd; 1115d495d8c9Sdrh c = (unsigned)(x & 0x1fffff); 1116fe7a5d11Sdrh if( c<0x00080 ){ 1117fe7a5d11Sdrh *zOut++ = (u8)(c&0xFF); 1118fe7a5d11Sdrh }else if( c<0x00800 ){ 1119fe7a5d11Sdrh *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); 1120fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1121fe7a5d11Sdrh }else if( c<0x10000 ){ 1122fe7a5d11Sdrh *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); 1123fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1124fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1125d495d8c9Sdrh }else{ 1126fe7a5d11Sdrh *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); 1127fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); 1128fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1129fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1130fe7a5d11Sdrh } \ 1131d495d8c9Sdrh } 1132bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); 1133d495d8c9Sdrh } 1134d495d8c9Sdrh 1135d495d8c9Sdrh /* 1136137c728fSdrh ** The hex() function. Interpret the argument as a blob. Return 1137137c728fSdrh ** a hexadecimal rendering as text. 1138137c728fSdrh */ 1139137c728fSdrh static void hexFunc( 1140137c728fSdrh sqlite3_context *context, 1141137c728fSdrh int argc, 1142137c728fSdrh sqlite3_value **argv 1143137c728fSdrh ){ 1144137c728fSdrh int i, n; 1145137c728fSdrh const unsigned char *pBlob; 1146137c728fSdrh char *zHex, *z; 1147137c728fSdrh assert( argc==1 ); 1148f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 11491f0feef8Sdrh pBlob = sqlite3_value_blob(argv[0]); 1150137c728fSdrh n = sqlite3_value_bytes(argv[0]); 11511f0feef8Sdrh assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1152b1a6c3c1Sdrh z = zHex = contextMalloc(context, ((i64)n)*2 + 1); 1153a1644fd8Sdanielk1977 if( zHex ){ 1154137c728fSdrh for(i=0; i<n; i++, pBlob++){ 1155137c728fSdrh unsigned char c = *pBlob; 1156137c728fSdrh *(z++) = hexdigits[(c>>4)&0xf]; 1157137c728fSdrh *(z++) = hexdigits[c&0xf]; 1158137c728fSdrh } 1159137c728fSdrh *z = 0; 1160137c728fSdrh sqlite3_result_text(context, zHex, n*2, sqlite3_free); 1161137c728fSdrh } 1162a1644fd8Sdanielk1977 } 1163137c728fSdrh 116426b6d90dSdrh /* 11658cff382eSdrh ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 11668cff382eSdrh */ 11678cff382eSdrh static void zeroblobFunc( 11688cff382eSdrh sqlite3_context *context, 11698cff382eSdrh int argc, 11708cff382eSdrh sqlite3_value **argv 11718cff382eSdrh ){ 117298640a3fSdrh i64 n; 1173a4d5ae8fSdan int rc; 11748cff382eSdrh assert( argc==1 ); 1175f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 117698640a3fSdrh n = sqlite3_value_int64(argv[0]); 117753e66c3cSdrh if( n<0 ) n = 0; 1178a4d5ae8fSdan rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ 1179a4d5ae8fSdan if( rc ){ 1180a4d5ae8fSdan sqlite3_result_error_code(context, rc); 11818cff382eSdrh } 118298640a3fSdrh } 11838cff382eSdrh 11848cff382eSdrh /* 118526b6d90dSdrh ** The replace() function. Three arguments are all strings: call 118626b6d90dSdrh ** them A, B, and C. The result is also a string which is derived 1187f7b5496eSdrh ** from A by replacing every occurrence of B with C. The match 118826b6d90dSdrh ** must be exact. Collating sequences are not used. 118926b6d90dSdrh */ 119026b6d90dSdrh static void replaceFunc( 119126b6d90dSdrh sqlite3_context *context, 119226b6d90dSdrh int argc, 119326b6d90dSdrh sqlite3_value **argv 119426b6d90dSdrh ){ 119526b6d90dSdrh const unsigned char *zStr; /* The input string A */ 119626b6d90dSdrh const unsigned char *zPattern; /* The pattern string B */ 119726b6d90dSdrh const unsigned char *zRep; /* The replacement string C */ 119826b6d90dSdrh unsigned char *zOut; /* The output */ 119926b6d90dSdrh int nStr; /* Size of zStr */ 120026b6d90dSdrh int nPattern; /* Size of zPattern */ 120126b6d90dSdrh int nRep; /* Size of zRep */ 12022e6400baSdrh i64 nOut; /* Maximum size of zOut */ 120326b6d90dSdrh int loopLimit; /* Last zStr[] that might match zPattern[] */ 120426b6d90dSdrh int i, j; /* Loop counters */ 1205f3139520Sdrh unsigned cntExpand; /* Number zOut expansions */ 1206f3139520Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 120726b6d90dSdrh 120826b6d90dSdrh assert( argc==3 ); 1209f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 121026b6d90dSdrh zStr = sqlite3_value_text(argv[0]); 12117a521cfbSdrh if( zStr==0 ) return; 12121f0feef8Sdrh nStr = sqlite3_value_bytes(argv[0]); 12131f0feef8Sdrh assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ 121426b6d90dSdrh zPattern = sqlite3_value_text(argv[1]); 1215a605fe8dSdrh if( zPattern==0 ){ 12162333606eSdrh assert( sqlite3_value_type(argv[1])==SQLITE_NULL 12172333606eSdrh || sqlite3_context_db_handle(context)->mallocFailed ); 1218a605fe8dSdrh return; 1219a605fe8dSdrh } 1220a605fe8dSdrh if( zPattern[0]==0 ){ 1221a605fe8dSdrh assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); 1222a605fe8dSdrh sqlite3_result_value(context, argv[0]); 1223a605fe8dSdrh return; 1224a605fe8dSdrh } 12251f0feef8Sdrh nPattern = sqlite3_value_bytes(argv[1]); 12261f0feef8Sdrh assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ 122726b6d90dSdrh zRep = sqlite3_value_text(argv[2]); 12287a521cfbSdrh if( zRep==0 ) return; 12291f0feef8Sdrh nRep = sqlite3_value_bytes(argv[2]); 12301f0feef8Sdrh assert( zRep==sqlite3_value_text(argv[2]) ); 12312e6400baSdrh nOut = nStr + 1; 12322e6400baSdrh assert( nOut<SQLITE_MAX_LENGTH ); 1233b1a6c3c1Sdrh zOut = contextMalloc(context, (i64)nOut); 12342e6400baSdrh if( zOut==0 ){ 12352e6400baSdrh return; 123626b6d90dSdrh } 123726b6d90dSdrh loopLimit = nStr - nPattern; 1238f3139520Sdrh cntExpand = 0; 123926b6d90dSdrh for(i=j=0; i<=loopLimit; i++){ 124026b6d90dSdrh if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 124126b6d90dSdrh zOut[j++] = zStr[i]; 124226b6d90dSdrh }else{ 1243f3139520Sdrh if( nRep>nPattern ){ 12442e6400baSdrh nOut += nRep - nPattern; 1245c86d82f2Sdrh testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); 1246c86d82f2Sdrh testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); 124727e62dbeSdrh if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1248a0206bc8Sdrh sqlite3_result_error_toobig(context); 1249b975598eSdrh sqlite3_free(zOut); 125017374e8fSdanielk1977 return; 125117374e8fSdanielk1977 } 1252f3139520Sdrh cntExpand++; 1253f3139520Sdrh if( (cntExpand&(cntExpand-1))==0 ){ 1254f3139520Sdrh /* Grow the size of the output buffer only on substitutions 1255f3139520Sdrh ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ 1256f3139520Sdrh u8 *zOld; 12574a50aac5Sdrh zOld = zOut; 1258f3139520Sdrh zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1)); 12592e6400baSdrh if( zOut==0 ){ 1260a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 1261b975598eSdrh sqlite3_free(zOld); 12622e6400baSdrh return; 12632e6400baSdrh } 1264f3139520Sdrh } 1265f3139520Sdrh } 126626b6d90dSdrh memcpy(&zOut[j], zRep, nRep); 126726b6d90dSdrh j += nRep; 126826b6d90dSdrh i += nPattern-1; 126926b6d90dSdrh } 127026b6d90dSdrh } 1271f3139520Sdrh assert( j+nStr-i+1<=nOut ); 127226b6d90dSdrh memcpy(&zOut[j], &zStr[i], nStr-i); 127326b6d90dSdrh j += nStr - i; 127426b6d90dSdrh assert( j<=nOut ); 127526b6d90dSdrh zOut[j] = 0; 127626b6d90dSdrh sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 127726b6d90dSdrh } 127826b6d90dSdrh 1279309b3386Sdrh /* 1280309b3386Sdrh ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 1281309b3386Sdrh ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 1282309b3386Sdrh */ 1283309b3386Sdrh static void trimFunc( 1284309b3386Sdrh sqlite3_context *context, 1285309b3386Sdrh int argc, 1286309b3386Sdrh sqlite3_value **argv 1287309b3386Sdrh ){ 1288309b3386Sdrh const unsigned char *zIn; /* Input string */ 1289309b3386Sdrh const unsigned char *zCharSet; /* Set of characters to trim */ 1290309b3386Sdrh int nIn; /* Number of bytes in input */ 12917209c697Sdrh int flags; /* 1: trimleft 2: trimright 3: trim */ 1292d1e3a616Sdrh int i; /* Loop counter */ 12931bd10f8aSdrh unsigned char *aLen = 0; /* Length of each character in zCharSet */ 12941bd10f8aSdrh unsigned char **azChar = 0; /* Individual characters in zCharSet */ 1295d1e3a616Sdrh int nChar; /* Number of characters in zCharSet */ 1296d1e3a616Sdrh 1297309b3386Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1298309b3386Sdrh return; 1299309b3386Sdrh } 1300309b3386Sdrh zIn = sqlite3_value_text(argv[0]); 13017a521cfbSdrh if( zIn==0 ) return; 13021f0feef8Sdrh nIn = sqlite3_value_bytes(argv[0]); 13031f0feef8Sdrh assert( zIn==sqlite3_value_text(argv[0]) ); 1304309b3386Sdrh if( argc==1 ){ 1305d1e3a616Sdrh static const unsigned char lenOne[] = { 1 }; 1306a4de4532Sdanielk1977 static unsigned char * const azOne[] = { (u8*)" " }; 1307d1e3a616Sdrh nChar = 1; 13088cff382eSdrh aLen = (u8*)lenOne; 1309bc67da48Sdanielk1977 azChar = (unsigned char **)azOne; 1310d1e3a616Sdrh zCharSet = 0; 13117a521cfbSdrh }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 1312309b3386Sdrh return; 1313d1e3a616Sdrh }else{ 1314d1e3a616Sdrh const unsigned char *z; 1315d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 13164a919118Sdrh SQLITE_SKIP_UTF8(z); 1317309b3386Sdrh } 1318d1e3a616Sdrh if( nChar>0 ){ 1319b1a6c3c1Sdrh azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); 1320d1e3a616Sdrh if( azChar==0 ){ 1321d1e3a616Sdrh return; 1322d1e3a616Sdrh } 1323d1e3a616Sdrh aLen = (unsigned char*)&azChar[nChar]; 1324d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 1325bc67da48Sdanielk1977 azChar[nChar] = (unsigned char *)z; 13264a919118Sdrh SQLITE_SKIP_UTF8(z); 13273abbd39aSdrh aLen[nChar] = (u8)(z - azChar[nChar]); 1328d1e3a616Sdrh } 1329d1e3a616Sdrh } 1330d1e3a616Sdrh } 1331d1e3a616Sdrh if( nChar>0 ){ 13321fc4129dSshane flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); 1333309b3386Sdrh if( flags & 1 ){ 1334d1e3a616Sdrh while( nIn>0 ){ 13351bd10f8aSdrh int len = 0; 1336d1e3a616Sdrh for(i=0; i<nChar; i++){ 1337d1e3a616Sdrh len = aLen[i]; 133827e62dbeSdrh if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break; 1339d1e3a616Sdrh } 1340d1e3a616Sdrh if( i>=nChar ) break; 1341d1e3a616Sdrh zIn += len; 1342d1e3a616Sdrh nIn -= len; 1343309b3386Sdrh } 1344309b3386Sdrh } 1345309b3386Sdrh if( flags & 2 ){ 1346d1e3a616Sdrh while( nIn>0 ){ 13471bd10f8aSdrh int len = 0; 1348d1e3a616Sdrh for(i=0; i<nChar; i++){ 1349d1e3a616Sdrh len = aLen[i]; 1350d1e3a616Sdrh if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 1351309b3386Sdrh } 1352d1e3a616Sdrh if( i>=nChar ) break; 1353d1e3a616Sdrh nIn -= len; 1354d1e3a616Sdrh } 1355d1e3a616Sdrh } 1356d1e3a616Sdrh if( zCharSet ){ 1357d1e3a616Sdrh sqlite3_free(azChar); 1358309b3386Sdrh } 1359309b3386Sdrh } 1360309b3386Sdrh sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 1361309b3386Sdrh } 136226b6d90dSdrh 1363a4de4532Sdanielk1977 1364cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 1365cc15313cSdrh /* 1366cc15313cSdrh ** The "unknown" function is automatically substituted in place of 1367cc15313cSdrh ** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN 1368cc15313cSdrh ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used. 1369cc15313cSdrh ** When the "sqlite3" command-line shell is built using this functionality, 1370cc15313cSdrh ** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries 1371cc15313cSdrh ** involving application-defined functions to be examined in a generic 1372cc15313cSdrh ** sqlite3 shell. 1373cc15313cSdrh */ 1374cc15313cSdrh static void unknownFunc( 1375cc15313cSdrh sqlite3_context *context, 1376cc15313cSdrh int argc, 1377cc15313cSdrh sqlite3_value **argv 1378cc15313cSdrh ){ 1379cc15313cSdrh /* no-op */ 1380cc15313cSdrh } 1381cc15313cSdrh #endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/ 1382cc15313cSdrh 1383cc15313cSdrh 13842ba3cccfSdrh /* IMP: R-25361-16150 This function is omitted from SQLite by default. It 13852ba3cccfSdrh ** is only available if the SQLITE_SOUNDEX compile-time option is used 13862ba3cccfSdrh ** when SQLite is built. 13872ba3cccfSdrh */ 1388d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1389d24cc427Sdrh /* 1390d24cc427Sdrh ** Compute the soundex encoding of a word. 13912ba3cccfSdrh ** 13922ba3cccfSdrh ** IMP: R-59782-00072 The soundex(X) function returns a string that is the 13932ba3cccfSdrh ** soundex encoding of the string X. 1394d24cc427Sdrh */ 1395137c728fSdrh static void soundexFunc( 1396137c728fSdrh sqlite3_context *context, 1397137c728fSdrh int argc, 1398137c728fSdrh sqlite3_value **argv 1399137c728fSdrh ){ 1400d24cc427Sdrh char zResult[8]; 14014c755c0fSdrh const u8 *zIn; 1402d24cc427Sdrh int i, j; 1403d24cc427Sdrh static const unsigned char iCode[] = { 1404d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1405d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1406d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1407d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1408d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1409d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1410d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1411d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1412d24cc427Sdrh }; 1413d24cc427Sdrh assert( argc==1 ); 14144c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 1415bdf67e0eSdrh if( zIn==0 ) zIn = (u8*)""; 1416dc86e2b2Sdrh for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} 1417d24cc427Sdrh if( zIn[i] ){ 1418bdf67e0eSdrh u8 prevcode = iCode[zIn[i]&0x7f]; 141978ca0e7eSdanielk1977 zResult[0] = sqlite3Toupper(zIn[i]); 1420d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 1421d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 1422d24cc427Sdrh if( code>0 ){ 1423bdf67e0eSdrh if( code!=prevcode ){ 1424bdf67e0eSdrh prevcode = code; 1425d24cc427Sdrh zResult[j++] = code + '0'; 1426d24cc427Sdrh } 1427bdf67e0eSdrh }else{ 1428bdf67e0eSdrh prevcode = 0; 1429bdf67e0eSdrh } 1430d24cc427Sdrh } 1431d24cc427Sdrh while( j<4 ){ 1432d24cc427Sdrh zResult[j++] = '0'; 1433d24cc427Sdrh } 1434d24cc427Sdrh zResult[j] = 0; 1435d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 1436d24cc427Sdrh }else{ 14372ba3cccfSdrh /* IMP: R-64894-50321 The string "?000" is returned if the argument 14382ba3cccfSdrh ** is NULL or contains no ASCII alphabetic characters. */ 1439d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 1440d24cc427Sdrh } 1441d24cc427Sdrh } 14422ba3cccfSdrh #endif /* SQLITE_SOUNDEX */ 1443d24cc427Sdrh 1444fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 1445fdb83b2fSdrh /* 1446fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL. 1447fdb83b2fSdrh */ 1448fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 144965fd59f7Sdanielk1977 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 14507a521cfbSdrh const char *zProc; 1451fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1452fdb83b2fSdrh char *zErrMsg = 0; 1453fdb83b2fSdrh 1454191dd061Sdrh /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc 14551a55dedfSdrh ** flag is set. See the sqlite3_enable_load_extension() API. 14561a55dedfSdrh */ 1457f602a161Sdrh if( (db->flags & SQLITE_LoadExtFunc)==0 ){ 1458f602a161Sdrh sqlite3_result_error(context, "not authorized", -1); 1459f602a161Sdrh return; 1460f602a161Sdrh } 14611a55dedfSdrh 1462fdb83b2fSdrh if( argc==2 ){ 146365fd59f7Sdanielk1977 zProc = (const char *)sqlite3_value_text(argv[1]); 14647a521cfbSdrh }else{ 14657a521cfbSdrh zProc = 0; 1466fdb83b2fSdrh } 14677a521cfbSdrh if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 1468fdb83b2fSdrh sqlite3_result_error(context, zErrMsg, -1); 1469fdb83b2fSdrh sqlite3_free(zErrMsg); 1470fdb83b2fSdrh } 1471fdb83b2fSdrh } 1472fdb83b2fSdrh #endif 1473fdb83b2fSdrh 147401427a62Sdanielk1977 14750ac65892Sdrh /* 1476d3a149efSdrh ** An instance of the following structure holds the context of a 1477dd5baa95Sdrh ** sum() or avg() aggregate computation. 1478dd5baa95Sdrh */ 1479dd5baa95Sdrh typedef struct SumCtx SumCtx; 1480dd5baa95Sdrh struct SumCtx { 14818c08e861Sdrh double rSum; /* Floating point sum */ 14828c08e861Sdrh i64 iSum; /* Integer sum */ 1483cf85a51cSdrh i64 cnt; /* Number of elements summed */ 14848c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 14858c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 1486dd5baa95Sdrh }; 1487dd5baa95Sdrh 1488dd5baa95Sdrh /* 1489a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 1490a97fdd3bSdrh ** 1491a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 1492a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 1493a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 1494a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 1495c806d857Sdrh ** value. TOTAL never fails, but SUM might through an exception if 1496c806d857Sdrh ** it overflows an integer. 1497dd5baa95Sdrh */ 14980ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1499dd5baa95Sdrh SumCtx *p; 15003d1d95e6Sdrh int type; 15013f219f46Sdrh assert( argc==1 ); 1502f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 15034f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 150429d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 15053d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 1506739105c7Sdrh p->cnt++; 150729d72108Sdrh if( type==SQLITE_INTEGER ){ 15088c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 15098c08e861Sdrh p->rSum += v; 1510158b9cb9Sdrh if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ 1511a546ef21Sdrh p->approx = p->overflow = 1; 151229d72108Sdrh } 151329d72108Sdrh }else{ 15148c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 151529d72108Sdrh p->approx = 1; 15163f219f46Sdrh } 1517739105c7Sdrh } 1518dd5baa95Sdrh } 151967a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1520c3a20c19Sdan static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){ 1521c3a20c19Sdan SumCtx *p; 1522c3a20c19Sdan int type; 1523c3a20c19Sdan assert( argc==1 ); 1524c3a20c19Sdan UNUSED_PARAMETER(argc); 1525c3a20c19Sdan p = sqlite3_aggregate_context(context, sizeof(*p)); 1526c3a20c19Sdan type = sqlite3_value_numeric_type(argv[0]); 1527fd4b7285Sdrh /* p is always non-NULL because sumStep() will have been called first 1528fd4b7285Sdrh ** to initialize it */ 1529fd4b7285Sdrh if( ALWAYS(p) && type!=SQLITE_NULL ){ 1530a546ef21Sdrh assert( p->cnt>0 ); 1531c3a20c19Sdan p->cnt--; 1532a546ef21Sdrh assert( type==SQLITE_INTEGER || p->approx ); 1533a546ef21Sdrh if( type==SQLITE_INTEGER && p->approx==0 ){ 1534c3a20c19Sdan i64 v = sqlite3_value_int64(argv[0]); 1535c3a20c19Sdan p->rSum -= v; 1536a546ef21Sdrh p->iSum -= v; 1537c3a20c19Sdan }else{ 1538d736829eSdan p->rSum -= sqlite3_value_double(argv[0]); 1539c3a20c19Sdan } 1540c3a20c19Sdan } 1541c3a20c19Sdan } 154267a9b8edSdan #else 154367a9b8edSdan # define sumInverse 0 154467a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 15450ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 1546dd5baa95Sdrh SumCtx *p; 1547abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1548c2bd913aSdrh if( p && p->cnt>0 ){ 15498c08e861Sdrh if( p->overflow ){ 15508c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 15518c08e861Sdrh }else if( p->approx ){ 15528c08e861Sdrh sqlite3_result_double(context, p->rSum); 1553c2bd913aSdrh }else{ 15548c08e861Sdrh sqlite3_result_int64(context, p->iSum); 15553d1d95e6Sdrh } 1556dd5baa95Sdrh } 1557c2bd913aSdrh } 15580ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 1559dd5baa95Sdrh SumCtx *p; 1560abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1561739105c7Sdrh if( p && p->cnt>0 ){ 15628c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 1563dd5baa95Sdrh } 1564dd5baa95Sdrh } 1565a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 1566a97fdd3bSdrh SumCtx *p; 1567a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 1568fbd60f82Sshane /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 1569fbd60f82Sshane sqlite3_result_double(context, p ? p->rSum : (double)0); 1570a97fdd3bSdrh } 1571dd5baa95Sdrh 1572dd5baa95Sdrh /* 15730bce8354Sdrh ** The following structure keeps track of state information for the 15740bce8354Sdrh ** count() aggregate function. 15750bce8354Sdrh */ 15760bce8354Sdrh typedef struct CountCtx CountCtx; 15770bce8354Sdrh struct CountCtx { 1578fc6ad39cSdrh i64 n; 15797262ca94Sdan #ifdef SQLITE_DEBUG 15807262ca94Sdan int bInverse; /* True if xInverse() ever called */ 15817262ca94Sdan #endif 15820bce8354Sdrh }; 1583dd5baa95Sdrh 15840bce8354Sdrh /* 15850bce8354Sdrh ** Routines to implement the count() aggregate function. 15860bce8354Sdrh */ 15870ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 15880bce8354Sdrh CountCtx *p; 15894f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 15909c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 15910bce8354Sdrh p->n++; 15920bce8354Sdrh } 15932e79c3d5Sdrh 1594d3264c7cSdrh #ifndef SQLITE_OMIT_DEPRECATED 15952e79c3d5Sdrh /* The sqlite3_aggregate_count() function is deprecated. But just to make 15962e79c3d5Sdrh ** sure it still operates correctly, verify that its count agrees with our 15972e79c3d5Sdrh ** internal count when using count(*) and when the total count can be 15982e79c3d5Sdrh ** expressed as a 32-bit integer. */ 15997262ca94Sdan assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse 16002e79c3d5Sdrh || p->n==sqlite3_aggregate_count(context) ); 1601d3264c7cSdrh #endif 16020bce8354Sdrh } 16030ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 16040bce8354Sdrh CountCtx *p; 1605abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1606fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 16070bce8354Sdrh } 16087262ca94Sdan #ifndef SQLITE_OMIT_WINDOWFUNC 16097262ca94Sdan static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){ 16107262ca94Sdan CountCtx *p; 16117262ca94Sdan p = sqlite3_aggregate_context(ctx, sizeof(*p)); 1612fd4b7285Sdrh /* p is always non-NULL since countStep() will have been called first */ 1613fd4b7285Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){ 16147262ca94Sdan p->n--; 16157262ca94Sdan #ifdef SQLITE_DEBUG 16167262ca94Sdan p->bInverse = 1; 16177262ca94Sdan #endif 16187262ca94Sdan } 16197262ca94Sdan } 16206b4b8820Sdan #else 16216b4b8820Sdan # define countInverse 0 16226b4b8820Sdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 16230bce8354Sdrh 16240bce8354Sdrh /* 16250bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 16260bce8354Sdrh */ 162762c14b34Sdanielk1977 static void minmaxStep( 162862c14b34Sdanielk1977 sqlite3_context *context, 162962c14b34Sdanielk1977 int NotUsed, 163062c14b34Sdanielk1977 sqlite3_value **argv 163162c14b34Sdanielk1977 ){ 163288208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 16339eb516c0Sdrh Mem *pBest; 163462c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 16359eb516c0Sdrh 16369eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 16373aeab9e4Sdanielk1977 if( !pBest ) return; 1638268380caSdrh 16396fb2b54cSdan if( sqlite3_value_type(pArg)==SQLITE_NULL ){ 164094a6d998Sdrh if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); 164194a6d998Sdrh }else if( pBest->flags ){ 16429eb516c0Sdrh int max; 16439eb516c0Sdrh int cmp; 1644dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 16457e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 16467e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 16477e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 16487e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 16497e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 16507e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 16517e18c259Sdanielk1977 ** aggregate, or 0 for min(). 16527e18c259Sdanielk1977 */ 1653309b3386Sdrh max = sqlite3_user_data(context)!=0; 1654dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 165588208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 1656b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 16577a95789cSdrh }else{ 16587a95789cSdrh sqlite3SkipAccumulatorLoad(context); 165988208050Sdanielk1977 } 16600bce8354Sdrh }else{ 1661035e563bSdrh pBest->db = sqlite3_context_db_handle(context); 1662b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 16630bce8354Sdrh } 16640bce8354Sdrh } 16656fb2b54cSdan static void minMaxValueFinalize(sqlite3_context *context, int bValue){ 166688208050Sdanielk1977 sqlite3_value *pRes; 1667abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1668abfcea25Sdrh if( pRes ){ 166994a6d998Sdrh if( pRes->flags ){ 1670f4479501Sdrh sqlite3_result_value(context, pRes); 16710bce8354Sdrh } 16726fb2b54cSdan if( bValue==0 ) sqlite3VdbeMemRelease(pRes); 16730bce8354Sdrh } 1674abfcea25Sdrh } 167567a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 16766fb2b54cSdan static void minMaxValue(sqlite3_context *context){ 1677c7bf5716Sdrh minMaxValueFinalize(context, 1); 16786fb2b54cSdan } 167967a9b8edSdan #else 168067a9b8edSdan # define minMaxValue 0 168167a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 16826fb2b54cSdan static void minMaxFinalize(sqlite3_context *context){ 1683c7bf5716Sdrh minMaxValueFinalize(context, 0); 16846fb2b54cSdan } 1685dd5baa95Sdrh 1686b0689696Sdrh /* 1687b0689696Sdrh ** group_concat(EXPR, ?SEPARATOR?) 1688b0689696Sdrh */ 1689b0689696Sdrh static void groupConcatStep( 1690b0689696Sdrh sqlite3_context *context, 1691b0689696Sdrh int argc, 1692b0689696Sdrh sqlite3_value **argv 1693b0689696Sdrh ){ 1694b0689696Sdrh const char *zVal; 1695ade86483Sdrh StrAccum *pAccum; 1696b0689696Sdrh const char *zSep; 169707d3117aSdrh int nVal, nSep; 169807d3117aSdrh assert( argc==1 || argc==2 ); 169907d3117aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1700ade86483Sdrh pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1701ade86483Sdrh 1702ade86483Sdrh if( pAccum ){ 1703bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1704e3bf632cSdan int firstTerm = pAccum->mxAlloc==0; 1705bb4957f8Sdrh pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 17068bfd7190Sdrh if( !firstTerm ){ 170707d3117aSdrh if( argc==2 ){ 170807d3117aSdrh zSep = (char*)sqlite3_value_text(argv[1]); 170907d3117aSdrh nSep = sqlite3_value_bytes(argv[1]); 1710b0689696Sdrh }else{ 1711b0689696Sdrh zSep = ","; 1712ade86483Sdrh nSep = 1; 1713b0689696Sdrh } 17140cdbe1aeSdrh if( zSep ) sqlite3_str_append(pAccum, zSep, nSep); 1715b0689696Sdrh } 171607d3117aSdrh zVal = (char*)sqlite3_value_text(argv[0]); 171707d3117aSdrh nVal = sqlite3_value_bytes(argv[0]); 17180cdbe1aeSdrh if( zVal ) sqlite3_str_append(pAccum, zVal, nVal); 1719b0689696Sdrh } 1720b0689696Sdrh } 172167a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 172203854d2eSdan static void groupConcatInverse( 172303854d2eSdan sqlite3_context *context, 172403854d2eSdan int argc, 172503854d2eSdan sqlite3_value **argv 172603854d2eSdan ){ 172703854d2eSdan int n; 172803854d2eSdan StrAccum *pAccum; 1729c7bf5716Sdrh assert( argc==1 || argc==2 ); 173003854d2eSdan if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 173103854d2eSdan pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1732fd4b7285Sdrh /* pAccum is always non-NULL since groupConcatStep() will have always 1733fd4b7285Sdrh ** run frist to initialize it */ 1734fd4b7285Sdrh if( ALWAYS(pAccum) ){ 173503854d2eSdan n = sqlite3_value_bytes(argv[0]); 173603854d2eSdan if( argc==2 ){ 173703854d2eSdan n += sqlite3_value_bytes(argv[1]); 1738683b0fffSdan }else{ 1739683b0fffSdan n++; 174003854d2eSdan } 1741c7bf5716Sdrh if( n>=(int)pAccum->nChar ){ 174203854d2eSdan pAccum->nChar = 0; 174303854d2eSdan }else{ 174403854d2eSdan pAccum->nChar -= n; 174503854d2eSdan memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar); 174603854d2eSdan } 1747e3bf632cSdan if( pAccum->nChar==0 ) pAccum->mxAlloc = 0; 174803854d2eSdan } 174903854d2eSdan } 175067a9b8edSdan #else 175167a9b8edSdan # define groupConcatInverse 0 175267a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 1753b0689696Sdrh static void groupConcatFinalize(sqlite3_context *context){ 1754ade86483Sdrh StrAccum *pAccum; 1755ade86483Sdrh pAccum = sqlite3_aggregate_context(context, 0); 1756ade86483Sdrh if( pAccum ){ 17570cdbe1aeSdrh if( pAccum->accError==SQLITE_TOOBIG ){ 1758ade86483Sdrh sqlite3_result_error_toobig(context); 17590cdbe1aeSdrh }else if( pAccum->accError==SQLITE_NOMEM ){ 1760ade86483Sdrh sqlite3_result_error_nomem(context); 1761ade86483Sdrh }else{ 1762ade86483Sdrh sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 1763ade86483Sdrh sqlite3_free); 1764b0689696Sdrh } 1765b0689696Sdrh } 1766ade86483Sdrh } 176767a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1768e2f781b9Sdan static void groupConcatValue(sqlite3_context *context){ 1769e2f781b9Sdan sqlite3_str *pAccum; 1770e2f781b9Sdan pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0); 1771e2f781b9Sdan if( pAccum ){ 1772e2f781b9Sdan if( pAccum->accError==SQLITE_TOOBIG ){ 1773e2f781b9Sdan sqlite3_result_error_toobig(context); 1774e2f781b9Sdan }else if( pAccum->accError==SQLITE_NOMEM ){ 1775e2f781b9Sdan sqlite3_result_error_nomem(context); 1776e2f781b9Sdan }else{ 1777e2f781b9Sdan const char *zText = sqlite3_str_value(pAccum); 1778e2f781b9Sdan sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 1779e2f781b9Sdan } 1780e2f781b9Sdan } 1781e2f781b9Sdan } 178267a9b8edSdan #else 178367a9b8edSdan # define groupConcatValue 0 178467a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 17854e5ffc5fSdrh 1786d3a149efSdrh /* 1787a4741840Sdrh ** This routine does per-connection function registration. Most 1788a4741840Sdrh ** of the built-in functions above are part of the global function set. 1789a4741840Sdrh ** This routine only deals with those that are not global. 1790dc04c583Sdrh */ 179180738d9cSdrh void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ 1792832a58a6Sdanielk1977 int rc = sqlite3_overload_function(db, "MATCH", 2); 1793832a58a6Sdanielk1977 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 1794832a58a6Sdanielk1977 if( rc==SQLITE_NOMEM ){ 17954a642b60Sdrh sqlite3OomFault(db); 1796832a58a6Sdanielk1977 } 1797832a58a6Sdanielk1977 } 179855ef4d97Sdrh 179955ef4d97Sdrh /* 180008652b5eSdrh ** Re-register the built-in LIKE functions. The caseSensitive 180155ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 180208652b5eSdrh ** sensitive. 180355ef4d97Sdrh */ 180455ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 180555ef4d97Sdrh struct compareInfo *pInfo; 1806ea5c040fSdrh int flags; 180755ef4d97Sdrh if( caseSensitive ){ 180855ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 1809ea5c040fSdrh flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; 181055ef4d97Sdrh }else{ 181155ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 1812ea5c040fSdrh flags = SQLITE_FUNC_LIKE; 181355ef4d97Sdrh } 1814660af939Sdan sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1815660af939Sdan sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1816ea5c040fSdrh sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags; 1817ea5c040fSdrh sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags; 181855ef4d97Sdrh } 181955ef4d97Sdrh 182055ef4d97Sdrh /* 182155ef4d97Sdrh ** pExpr points to an expression which implements a function. If 182255ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 18231d42ea71Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and the 18241d42ea71Sdrh ** escape character and then return TRUE. If the function is not a 18251d42ea71Sdrh ** LIKE-style function then return FALSE. 18261d42ea71Sdrh ** 18271d42ea71Sdrh ** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE 18281d42ea71Sdrh ** operator if c is a string literal that is exactly one byte in length. 18291d42ea71Sdrh ** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is 18301d42ea71Sdrh ** no ESCAPE clause. 183116897072Sdrh ** 183216897072Sdrh ** *pIsNocase is set to true if uppercase and lowercase are equivalent for 183316897072Sdrh ** the function (default for LIKE). If the function makes the distinction 183416897072Sdrh ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to 183516897072Sdrh ** false. 183655ef4d97Sdrh */ 1837d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 183855ef4d97Sdrh FuncDef *pDef; 18391d42ea71Sdrh int nExpr; 18401d42ea71Sdrh if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){ 184155ef4d97Sdrh return 0; 184255ef4d97Sdrh } 18436ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 18441d42ea71Sdrh nExpr = pExpr->x.pList->nExpr; 18451d42ea71Sdrh pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0); 1846d36e1041Sdrh if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ 184755ef4d97Sdrh return 0; 184855ef4d97Sdrh } 18491d42ea71Sdrh if( nExpr<3 ){ 18501d42ea71Sdrh aWc[3] = 0; 18511d42ea71Sdrh }else{ 18521d42ea71Sdrh Expr *pEscape = pExpr->x.pList->a[2].pExpr; 18531d42ea71Sdrh char *zEscape; 18541d42ea71Sdrh if( pEscape->op!=TK_STRING ) return 0; 18551d42ea71Sdrh zEscape = pEscape->u.zToken; 18561d42ea71Sdrh if( zEscape[0]==0 || zEscape[1]!=0 ) return 0; 18571d42ea71Sdrh aWc[3] = zEscape[0]; 18581d42ea71Sdrh } 185955ef4d97Sdrh 186055ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 186155ef4d97Sdrh ** the first three statements in the compareInfo structure. The 186255ef4d97Sdrh ** asserts() that follow verify that assumption 186355ef4d97Sdrh */ 186455ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 186555ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 186655ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 186755ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1868d36e1041Sdrh *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; 186955ef4d97Sdrh return 1; 1870dc04c583Sdrh } 18718c0a791aSdanielk1977 187293ce741bSdanielk1977 /* 187360ec914cSpeter.d.reid ** All of the FuncDef structures in the aBuiltinFunc[] array above 187493ce741bSdanielk1977 ** to the global function hash table. This occurs at start-time (as 187593ce741bSdanielk1977 ** a consequence of calling sqlite3_initialize()). 187693ce741bSdanielk1977 ** 187793ce741bSdanielk1977 ** After this routine runs 187893ce741bSdanielk1977 */ 187980738d9cSdrh void sqlite3RegisterBuiltinFunctions(void){ 18808c0a791aSdanielk1977 /* 1881777c5386Sdrh ** The following array holds FuncDef structures for all of the functions 1882777c5386Sdrh ** defined in this file. 18838c0a791aSdanielk1977 ** 1884777c5386Sdrh ** The array cannot be constant since changes are made to the 1885777c5386Sdrh ** FuncDef.pHash elements at start-time. The elements of this array 1886777c5386Sdrh ** are read-only after initialization is complete. 188780738d9cSdrh ** 188880738d9cSdrh ** For peak efficiency, put the most frequently used function last. 18898c0a791aSdanielk1977 */ 189080738d9cSdrh static FuncDef aBuiltinFunc[] = { 189180738d9cSdrh #ifdef SQLITE_SOUNDEX 189280738d9cSdrh FUNCTION(soundex, 1, 0, 0, soundexFunc ), 189380738d9cSdrh #endif 189480738d9cSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 189580738d9cSdrh VFUNCTION(load_extension, 1, 0, 0, loadExt ), 189680738d9cSdrh VFUNCTION(load_extension, 2, 0, 0, loadExt ), 189780738d9cSdrh #endif 189880738d9cSdrh #if SQLITE_USER_AUTHENTICATION 189980738d9cSdrh FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), 190080738d9cSdrh #endif 190180738d9cSdrh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 190280738d9cSdrh DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), 190380738d9cSdrh DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), 190480738d9cSdrh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 190580738d9cSdrh FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 190680738d9cSdrh FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 190780738d9cSdrh FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 190854240751Sdrh #ifdef SQLITE_DEBUG 1909a1a523a5Sdrh FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY), 191054240751Sdrh #endif 1911092457b1Sdrh #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 191235100fb1Sdrh FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET| 19132fc865c1Sdrh SQLITE_FUNC_TYPEOF), 1914092457b1Sdrh #endif 19158c0a791aSdanielk1977 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 19168c0a791aSdanielk1977 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 19178c0a791aSdanielk1977 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 19188c0a791aSdanielk1977 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 19198c0a791aSdanielk1977 FUNCTION(trim, 1, 3, 0, trimFunc ), 19208c0a791aSdanielk1977 FUNCTION(trim, 2, 3, 0, trimFunc ), 19218c0a791aSdanielk1977 FUNCTION(min, -1, 0, 1, minmaxFunc ), 19228c0a791aSdanielk1977 FUNCTION(min, 0, 0, 1, 0 ), 19236fb2b54cSdan WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 19249588ad95Sdrh SQLITE_FUNC_MINMAX ), 19258c0a791aSdanielk1977 FUNCTION(max, -1, 1, 1, minmaxFunc ), 19268c0a791aSdanielk1977 FUNCTION(max, 0, 1, 1, 0 ), 19276fb2b54cSdan WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 19289588ad95Sdrh SQLITE_FUNC_MINMAX ), 1929a748fdccSdrh FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 1930a748fdccSdrh FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 1931d55e0729Sdrh FUNCTION(instr, 2, 0, 0, instrFunc ), 1932a5c1416dSdrh FUNCTION(printf, -1, 0, 0, printfFunc ), 1933d495d8c9Sdrh FUNCTION(unicode, 1, 0, 0, unicodeFunc ), 1934d495d8c9Sdrh FUNCTION(char, -1, 0, 0, charFunc ), 19358c0a791aSdanielk1977 FUNCTION(abs, 1, 0, 0, absFunc ), 1936fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 19378c0a791aSdanielk1977 FUNCTION(round, 1, 0, 0, roundFunc ), 19388c0a791aSdanielk1977 FUNCTION(round, 2, 0, 0, roundFunc ), 1939fbd60f82Sshane #endif 19408c0a791aSdanielk1977 FUNCTION(upper, 1, 0, 0, upperFunc ), 19418c0a791aSdanielk1977 FUNCTION(lower, 1, 0, 0, lowerFunc ), 19428c0a791aSdanielk1977 FUNCTION(hex, 1, 0, 0, hexFunc ), 1943cca9f3d2Sdrh FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 1944b1fba286Sdrh VFUNCTION(random, 0, 0, 0, randomFunc ), 1945b1fba286Sdrh VFUNCTION(randomblob, 1, 0, 0, randomBlob ), 19468c0a791aSdanielk1977 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 194703bf26d9Sdrh DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 194803bf26d9Sdrh DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), 1949840561f2Sdrh FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), 19508c0a791aSdanielk1977 FUNCTION(quote, 1, 0, 0, quoteFunc ), 1951b1fba286Sdrh VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 1952b1fba286Sdrh VFUNCTION(changes, 0, 0, 0, changes ), 1953b1fba286Sdrh VFUNCTION(total_changes, 0, 0, 0, total_changes ), 19548c0a791aSdanielk1977 FUNCTION(replace, 3, 0, 0, replaceFunc ), 19558c0a791aSdanielk1977 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 195680738d9cSdrh FUNCTION(substr, 2, 0, 0, substrFunc ), 195780738d9cSdrh FUNCTION(substr, 3, 0, 0, substrFunc ), 19586fb2b54cSdan WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0), 19596fb2b54cSdan WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0), 19606fb2b54cSdan WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0), 19617262ca94Sdan WAGGREGATE(count, 0,0,0, countStep, 19627262ca94Sdan countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ), 19637262ca94Sdan WAGGREGATE(count, 1,0,0, countStep, 19647262ca94Sdan countFinalize, countFinalize, countInverse, 0 ), 196503854d2eSdan WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, 19666fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 196703854d2eSdan WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, 19686fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 19698c0a791aSdanielk1977 19708c0a791aSdanielk1977 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 19718c0a791aSdanielk1977 #ifdef SQLITE_CASE_SENSITIVE_LIKE 19728c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 19738c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 19748c0a791aSdanielk1977 #else 19758c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 19768c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 19778c0a791aSdanielk1977 #endif 1978cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 1979cc15313cSdrh FUNCTION(unknown, -1, 0, 0, unknownFunc ), 1980cc15313cSdrh #endif 198180738d9cSdrh FUNCTION(coalesce, 1, 0, 0, 0 ), 198280738d9cSdrh FUNCTION(coalesce, 0, 0, 0, 0 ), 198380738d9cSdrh FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 19848c0a791aSdanielk1977 }; 1985545f587fSdrh #ifndef SQLITE_OMIT_ALTERTABLE 1986545f587fSdrh sqlite3AlterFunctions(); 1987545f587fSdrh #endif 1988dfa552f4Sdan sqlite3WindowFunctions(); 19890106e378Sdan #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) 19900106e378Sdan sqlite3AnalyzeFunctions(); 19910106e378Sdan #endif 199280738d9cSdrh sqlite3RegisterDateTimeFunctions(); 199380738d9cSdrh sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); 199480738d9cSdrh 199580738d9cSdrh #if 0 /* Enable to print out how the built-in functions are hashed */ 199680738d9cSdrh { 199780738d9cSdrh int i; 199880738d9cSdrh FuncDef *p; 199980738d9cSdrh for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ 200080738d9cSdrh printf("FUNC-HASH %02d:", i); 200180738d9cSdrh for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){ 200280738d9cSdrh int n = sqlite3Strlen30(p->zName); 200380738d9cSdrh int h = p->zName[0] + n; 200480738d9cSdrh printf(" %s(%d)", p->zName, h); 200580738d9cSdrh } 200680738d9cSdrh printf("\n"); 200780738d9cSdrh } 200880738d9cSdrh } 200980738d9cSdrh #endif 201070a8ca3cSdrh } 2011