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> 19ef9f719dSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2005d7bfd0Sdrh #include <math.h> 21ef9f719dSdrh #endif 2288208050Sdanielk1977 #include "vdbeInt.h" 230bce8354Sdrh 2455ef4d97Sdrh /* 2555ef4d97Sdrh ** Return the collating function associated with a function. 2655ef4d97Sdrh */ 27dc1bdc4fSdanielk1977 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 28a9e03b1bSdrh VdbeOp *pOp; 29a9e03b1bSdrh assert( context->pVdbe!=0 ); 30a9e03b1bSdrh pOp = &context->pVdbe->aOp[context->iOp-1]; 31a15cc47fSdrh assert( pOp->opcode==OP_CollSeq ); 32a15cc47fSdrh assert( pOp->p4type==P4_COLLSEQ ); 33a15cc47fSdrh return pOp->p4.pColl; 34dc1bdc4fSdanielk1977 } 35dc1bdc4fSdanielk1977 360bce8354Sdrh /* 377a95789cSdrh ** Indicate that the accumulator load should be skipped on this 387a95789cSdrh ** iteration of the aggregate loop. 397a95789cSdrh */ 407a95789cSdrh static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ 4121d59784Sdrh assert( context->isError<=0 ); 4221d59784Sdrh context->isError = -1; 437a95789cSdrh context->skipFlag = 1; 447a95789cSdrh } 457a95789cSdrh 467a95789cSdrh /* 470bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions 480bce8354Sdrh */ 49f9b596ebSdrh static void minmaxFunc( 50f9b596ebSdrh sqlite3_context *context, 51f9b596ebSdrh int argc, 52f9b596ebSdrh sqlite3_value **argv 53f9b596ebSdrh ){ 540bce8354Sdrh int i; 55268380caSdrh int mask; /* 0 for min() or 0xffffffff for max() */ 56f9b596ebSdrh int iBest; 57dc1bdc4fSdanielk1977 CollSeq *pColl; 580bce8354Sdrh 5965595cd6Sdrh assert( argc>1 ); 60c44af71cSdrh mask = sqlite3_user_data(context)==0 ? 0 : -1; 61dc1bdc4fSdanielk1977 pColl = sqlite3GetFuncCollSeq(context); 62dc1bdc4fSdanielk1977 assert( pColl ); 63c572ef7fSdanielk1977 assert( mask==-1 || mask==0 ); 64f9b596ebSdrh iBest = 0; 659c054830Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 66f9b596ebSdrh for(i=1; i<argc; i++){ 679c054830Sdrh if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 68dc1bdc4fSdanielk1977 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 6965595cd6Sdrh testcase( mask==0 ); 70f9b596ebSdrh iBest = i; 710bce8354Sdrh } 720bce8354Sdrh } 73f4479501Sdrh sqlite3_result_value(context, argv[iBest]); 740bce8354Sdrh } 750bce8354Sdrh 76268380caSdrh /* 77268380caSdrh ** Return the type of the argument. 78268380caSdrh */ 79f9b596ebSdrh static void typeofFunc( 80f9b596ebSdrh sqlite3_context *context, 8162c14b34Sdanielk1977 int NotUsed, 82f9b596ebSdrh sqlite3_value **argv 83f9b596ebSdrh ){ 849d8e401cSdrh static const char *azType[] = { "integer", "real", "text", "blob", "null" }; 859d8e401cSdrh int i = sqlite3_value_type(argv[0]) - 1; 8662c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 879d8e401cSdrh assert( i>=0 && i<ArraySize(azType) ); 889d8e401cSdrh assert( SQLITE_INTEGER==1 ); 899d8e401cSdrh assert( SQLITE_FLOAT==2 ); 909d8e401cSdrh assert( SQLITE_TEXT==3 ); 919d8e401cSdrh assert( SQLITE_BLOB==4 ); 929d8e401cSdrh assert( SQLITE_NULL==5 ); 933cef3649Sdrh /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns 943cef3649Sdrh ** the datatype code for the initial datatype of the sqlite3_value object 953cef3649Sdrh ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, 963cef3649Sdrh ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */ 979d8e401cSdrh sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC); 980bce8354Sdrh } 990bce8354Sdrh 1005708d2deSdrh 1015708d2deSdrh /* 1020bce8354Sdrh ** Implementation of the length() function 1030bce8354Sdrh */ 104f9b596ebSdrh static void lengthFunc( 105f9b596ebSdrh sqlite3_context *context, 106f9b596ebSdrh int argc, 107f9b596ebSdrh sqlite3_value **argv 108f9b596ebSdrh ){ 1090bce8354Sdrh assert( argc==1 ); 110f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 111f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1129c054830Sdrh case SQLITE_BLOB: 1139c054830Sdrh case SQLITE_INTEGER: 1149c054830Sdrh case SQLITE_FLOAT: { 115f4479501Sdrh sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 116f9b596ebSdrh break; 117f9b596ebSdrh } 1189c054830Sdrh case SQLITE_TEXT: { 1192646da7eSdrh const unsigned char *z = sqlite3_value_text(argv[0]); 1207ea3469eSdrh const unsigned char *z0; 1217ea3469eSdrh unsigned char c; 1227a521cfbSdrh if( z==0 ) return; 1237ea3469eSdrh z0 = z; 1247ea3469eSdrh while( (c = *z)!=0 ){ 1257ea3469eSdrh z++; 1267ea3469eSdrh if( c>=0xc0 ){ 1277ea3469eSdrh while( (*z & 0xc0)==0x80 ){ z++; z0++; } 1284a919118Sdrh } 1297ea3469eSdrh } 1307ea3469eSdrh sqlite3_result_int(context, (int)(z-z0)); 131f9b596ebSdrh break; 132f9b596ebSdrh } 133f9b596ebSdrh default: { 134f9b596ebSdrh sqlite3_result_null(context); 135f9b596ebSdrh break; 136f9b596ebSdrh } 137f9b596ebSdrh } 1380bce8354Sdrh } 1390bce8354Sdrh 1400bce8354Sdrh /* 1412ba3cccfSdrh ** Implementation of the abs() function. 1422ba3cccfSdrh ** 1432ba3cccfSdrh ** IMP: R-23979-26855 The abs(X) function returns the absolute value of 1442ba3cccfSdrh ** the numeric argument X. 1450bce8354Sdrh */ 1460ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1470bce8354Sdrh assert( argc==1 ); 148f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 149f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1509c054830Sdrh case SQLITE_INTEGER: { 151f93bbbeaSdanielk1977 i64 iVal = sqlite3_value_int64(argv[0]); 15252fc849aSdrh if( iVal<0 ){ 153693e6719Sdrh if( iVal==SMALLEST_INT64 ){ 154eb091cdfSdrh /* IMP: R-31676-45509 If X is the integer -9223372036854775808 155eb091cdfSdrh ** then abs(X) throws an integer overflow error since there is no 1562ba3cccfSdrh ** equivalent positive 64-bit two complement value. */ 15752fc849aSdrh sqlite3_result_error(context, "integer overflow", -1); 15852fc849aSdrh return; 15952fc849aSdrh } 16052fc849aSdrh iVal = -iVal; 16152fc849aSdrh } 162f93bbbeaSdanielk1977 sqlite3_result_int64(context, iVal); 163f9b596ebSdrh break; 164f9b596ebSdrh } 1659c054830Sdrh case SQLITE_NULL: { 1662ba3cccfSdrh /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ 167f9b596ebSdrh sqlite3_result_null(context); 168f9b596ebSdrh break; 169f9b596ebSdrh } 170f9b596ebSdrh default: { 1712ba3cccfSdrh /* Because sqlite3_value_double() returns 0.0 if the argument is not 1722ba3cccfSdrh ** something that can be converted into a number, we have: 173643091f0Sdrh ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob 174643091f0Sdrh ** that cannot be converted to a numeric value. 1752ba3cccfSdrh */ 176f93bbbeaSdanielk1977 double rVal = sqlite3_value_double(argv[0]); 17752fc849aSdrh if( rVal<0 ) rVal = -rVal; 178f93bbbeaSdanielk1977 sqlite3_result_double(context, rVal); 179f9b596ebSdrh break; 180f9b596ebSdrh } 181f9b596ebSdrh } 1820bce8354Sdrh } 1830bce8354Sdrh 1840bce8354Sdrh /* 185d55e0729Sdrh ** Implementation of the instr() function. 186d55e0729Sdrh ** 187d55e0729Sdrh ** instr(haystack,needle) finds the first occurrence of needle 188d55e0729Sdrh ** in haystack and returns the number of previous characters plus 1, 189d55e0729Sdrh ** or 0 if needle does not occur within haystack. 190d55e0729Sdrh ** 191d55e0729Sdrh ** If both haystack and needle are BLOBs, then the result is one more than 192d55e0729Sdrh ** the number of bytes in haystack prior to the first occurrence of needle, 193d55e0729Sdrh ** or 0 if needle never occurs in haystack. 194d55e0729Sdrh */ 195d55e0729Sdrh static void instrFunc( 196d55e0729Sdrh sqlite3_context *context, 197d55e0729Sdrh int argc, 198d55e0729Sdrh sqlite3_value **argv 199d55e0729Sdrh ){ 200d55e0729Sdrh const unsigned char *zHaystack; 201d55e0729Sdrh const unsigned char *zNeedle; 202d55e0729Sdrh int nHaystack; 203d55e0729Sdrh int nNeedle; 204d55e0729Sdrh int typeHaystack, typeNeedle; 205d55e0729Sdrh int N = 1; 206d55e0729Sdrh int isText; 207c930b405Sdrh unsigned char firstChar; 20897b02505Sdrh sqlite3_value *pC1 = 0; 20997b02505Sdrh sqlite3_value *pC2 = 0; 210d55e0729Sdrh 21168c804b9Sdrh UNUSED_PARAMETER(argc); 212d55e0729Sdrh typeHaystack = sqlite3_value_type(argv[0]); 213d55e0729Sdrh typeNeedle = sqlite3_value_type(argv[1]); 214d55e0729Sdrh if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; 215d55e0729Sdrh nHaystack = sqlite3_value_bytes(argv[0]); 216d55e0729Sdrh nNeedle = sqlite3_value_bytes(argv[1]); 217895decf6Sdan if( nNeedle>0 ){ 218d55e0729Sdrh if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ 219d55e0729Sdrh zHaystack = sqlite3_value_blob(argv[0]); 220d55e0729Sdrh zNeedle = sqlite3_value_blob(argv[1]); 221d55e0729Sdrh isText = 0; 22297b02505Sdrh }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){ 223d55e0729Sdrh zHaystack = sqlite3_value_text(argv[0]); 224d55e0729Sdrh zNeedle = sqlite3_value_text(argv[1]); 225d55e0729Sdrh isText = 1; 22697b02505Sdrh }else{ 22797b02505Sdrh pC1 = sqlite3_value_dup(argv[0]); 22897b02505Sdrh zHaystack = sqlite3_value_text(pC1); 2299d702840Sdrh if( zHaystack==0 ) goto endInstrOOM; 2309d702840Sdrh nHaystack = sqlite3_value_bytes(pC1); 23197b02505Sdrh pC2 = sqlite3_value_dup(argv[1]); 23297b02505Sdrh zNeedle = sqlite3_value_text(pC2); 2339d702840Sdrh if( zNeedle==0 ) goto endInstrOOM; 2349d702840Sdrh nNeedle = sqlite3_value_bytes(pC2); 23597b02505Sdrh isText = 1; 236d55e0729Sdrh } 2379d702840Sdrh if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM; 238c930b405Sdrh firstChar = zNeedle[0]; 239c930b405Sdrh while( nNeedle<=nHaystack 240c930b405Sdrh && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0) 241c930b405Sdrh ){ 242d55e0729Sdrh N++; 243d55e0729Sdrh do{ 244d55e0729Sdrh nHaystack--; 245d55e0729Sdrh zHaystack++; 246d55e0729Sdrh }while( isText && (zHaystack[0]&0xc0)==0x80 ); 247d55e0729Sdrh } 248d55e0729Sdrh if( nNeedle>nHaystack ) N = 0; 249895decf6Sdan } 250d55e0729Sdrh sqlite3_result_int(context, N); 25197b02505Sdrh endInstr: 25297b02505Sdrh sqlite3_value_free(pC1); 25397b02505Sdrh sqlite3_value_free(pC2); 2549d702840Sdrh return; 2559d702840Sdrh endInstrOOM: 2569d702840Sdrh sqlite3_result_error_nomem(context); 2579d702840Sdrh goto endInstr; 258d55e0729Sdrh } 259d55e0729Sdrh 260d55e0729Sdrh /* 261a5c1416dSdrh ** Implementation of the printf() function. 262a5c1416dSdrh */ 263a5c1416dSdrh static void printfFunc( 264a5c1416dSdrh sqlite3_context *context, 265a5c1416dSdrh int argc, 266a5c1416dSdrh sqlite3_value **argv 267a5c1416dSdrh ){ 268a5c1416dSdrh PrintfArguments x; 269a5c1416dSdrh StrAccum str; 270a5c1416dSdrh const char *zFormat; 271a5c1416dSdrh int n; 272c0490572Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 273a5c1416dSdrh 274a5c1416dSdrh if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 275a5c1416dSdrh x.nArg = argc-1; 276a5c1416dSdrh x.nUsed = 0; 277a5c1416dSdrh x.apArg = argv+1; 278c0490572Sdrh sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); 2795f4a686fSdrh str.printfFlags = SQLITE_PRINTF_SQLFUNC; 2800cdbe1aeSdrh sqlite3_str_appendf(&str, zFormat, &x); 281a5c1416dSdrh n = str.nChar; 282a5c1416dSdrh sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, 283a5c1416dSdrh SQLITE_DYNAMIC); 284a5c1416dSdrh } 285a5c1416dSdrh } 286a5c1416dSdrh 287a5c1416dSdrh /* 288f764e6fcSdrh ** Implementation of the substr() function. 289f764e6fcSdrh ** 290f764e6fcSdrh ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. 291f764e6fcSdrh ** p1 is 1-indexed. So substr(x,1,1) returns the first character 292f764e6fcSdrh ** of x. If x is text, then we actually count UTF-8 characters. 293f764e6fcSdrh ** If x is a blob, then we count bytes. 294f764e6fcSdrh ** 295f764e6fcSdrh ** If p1 is negative, then we begin abs(p1) from the end of x[]. 296779b8f12Sshaneh ** 297f7b5496eSdrh ** If p2 is negative, return the p2 characters preceding p1. 2980bce8354Sdrh */ 299f9b596ebSdrh static void substrFunc( 300f9b596ebSdrh sqlite3_context *context, 301f9b596ebSdrh int argc, 302f9b596ebSdrh sqlite3_value **argv 303f9b596ebSdrh ){ 3042646da7eSdrh const unsigned char *z; 3052646da7eSdrh const unsigned char *z2; 306023ae03aSdrh int len; 307f764e6fcSdrh int p0type; 308023ae03aSdrh i64 p1, p2; 30965595cd6Sdrh int negP2 = 0; 310f9b596ebSdrh 31164f31519Sdrh assert( argc==3 || argc==2 ); 3128198d254Sdrh if( sqlite3_value_type(argv[1])==SQLITE_NULL 3138198d254Sdrh || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) 3148198d254Sdrh ){ 3158198d254Sdrh return; 3168198d254Sdrh } 317f764e6fcSdrh p0type = sqlite3_value_type(argv[0]); 3184adc4cb9Sdrh p1 = sqlite3_value_int(argv[1]); 319f764e6fcSdrh if( p0type==SQLITE_BLOB ){ 320f764e6fcSdrh len = sqlite3_value_bytes(argv[0]); 321f764e6fcSdrh z = sqlite3_value_blob(argv[0]); 322f764e6fcSdrh if( z==0 ) return; 3231f0feef8Sdrh assert( len==sqlite3_value_bytes(argv[0]) ); 324f764e6fcSdrh }else{ 3254f26d6c4Sdrh z = sqlite3_value_text(argv[0]); 3260bce8354Sdrh if( z==0 ) return; 3274a919118Sdrh len = 0; 3284adc4cb9Sdrh if( p1<0 ){ 3294a919118Sdrh for(z2=z; *z2; len++){ 3304a919118Sdrh SQLITE_SKIP_UTF8(z2); 3314a919118Sdrh } 332f764e6fcSdrh } 3334adc4cb9Sdrh } 334883ad049Sdrh #ifdef SQLITE_SUBSTR_COMPATIBILITY 335883ad049Sdrh /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as 336883ad049Sdrh ** as substr(X,1,N) - it returns the first N characters of X. This 337883ad049Sdrh ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] 338883ad049Sdrh ** from 2009-02-02 for compatibility of applications that exploited the 339883ad049Sdrh ** old buggy behavior. */ 340883ad049Sdrh if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ 341883ad049Sdrh #endif 34264f31519Sdrh if( argc==3 ){ 34351ad0ecdSdanielk1977 p2 = sqlite3_value_int(argv[2]); 34465595cd6Sdrh if( p2<0 ){ 34565595cd6Sdrh p2 = -p2; 34665595cd6Sdrh negP2 = 1; 34765595cd6Sdrh } 34864f31519Sdrh }else{ 349bb4957f8Sdrh p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; 35064f31519Sdrh } 3510bce8354Sdrh if( p1<0 ){ 35289425d5eSdrh p1 += len; 353653bc759Sdrh if( p1<0 ){ 354653bc759Sdrh p2 += p1; 35565595cd6Sdrh if( p2<0 ) p2 = 0; 356653bc759Sdrh p1 = 0; 357653bc759Sdrh } 3580bce8354Sdrh }else if( p1>0 ){ 3590bce8354Sdrh p1--; 36065595cd6Sdrh }else if( p2>0 ){ 36165595cd6Sdrh p2--; 3620bce8354Sdrh } 36365595cd6Sdrh if( negP2 ){ 36465595cd6Sdrh p1 -= p2; 3654e79c594Sdrh if( p1<0 ){ 3664e79c594Sdrh p2 += p1; 3674e79c594Sdrh p1 = 0; 3684e79c594Sdrh } 3694e79c594Sdrh } 37065595cd6Sdrh assert( p1>=0 && p2>=0 ); 371f764e6fcSdrh if( p0type!=SQLITE_BLOB ){ 3724a919118Sdrh while( *z && p1 ){ 3734a919118Sdrh SQLITE_SKIP_UTF8(z); 3744a919118Sdrh p1--; 3750bce8354Sdrh } 3764a919118Sdrh for(z2=z; *z2 && p2; p2--){ 3774a919118Sdrh SQLITE_SKIP_UTF8(z2); 3780bce8354Sdrh } 379bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, 380bbf483f8Sdrh SQLITE_UTF8); 381f764e6fcSdrh }else{ 3824adc4cb9Sdrh if( p1+p2>len ){ 3834adc4cb9Sdrh p2 = len-p1; 3844adc4cb9Sdrh if( p2<0 ) p2 = 0; 3854adc4cb9Sdrh } 386bbf483f8Sdrh sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); 387f764e6fcSdrh } 3880bce8354Sdrh } 3890bce8354Sdrh 3900bce8354Sdrh /* 3910bce8354Sdrh ** Implementation of the round() function 3920bce8354Sdrh */ 393fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 3940ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 39551ad0ecdSdanielk1977 int n = 0; 3960bce8354Sdrh double r; 39750d654daSdrh char *zBuf; 3980bce8354Sdrh assert( argc==1 || argc==2 ); 39951ad0ecdSdanielk1977 if( argc==2 ){ 4009c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 40151ad0ecdSdanielk1977 n = sqlite3_value_int(argv[1]); 4020bce8354Sdrh if( n>30 ) n = 30; 4030bce8354Sdrh if( n<0 ) n = 0; 40451ad0ecdSdanielk1977 } 405d589a92aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 4064f26d6c4Sdrh r = sqlite3_value_double(argv[0]); 407147e176aSshaneh /* If Y==0 and X will fit in a 64-bit int, 408147e176aSshaneh ** handle the rounding directly, 409147e176aSshaneh ** otherwise use printf. 410147e176aSshaneh */ 41184422db9Sdrh if( r<-4503599627370496.0 || r>+4503599627370496.0 ){ 41284422db9Sdrh /* The value has no fractional part so there is nothing to round */ 41384422db9Sdrh }else if( n==0 ){ 41484422db9Sdrh r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); 415147e176aSshaneh }else{ 41650d654daSdrh zBuf = sqlite3_mprintf("%.*f",n,r); 41750d654daSdrh if( zBuf==0 ){ 41850d654daSdrh sqlite3_result_error_nomem(context); 419147e176aSshaneh return; 420147e176aSshaneh } 42155700bcdSdrh sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); 42250d654daSdrh sqlite3_free(zBuf); 4230bce8354Sdrh } 424147e176aSshaneh sqlite3_result_double(context, r); 42550d654daSdrh } 426fbd60f82Sshane #endif 427dc04c583Sdrh 42826783a58Sdanielk1977 /* 429f3cdcdccSdrh ** Allocate nByte bytes of space using sqlite3Malloc(). If the 43026783a58Sdanielk1977 ** allocation fails, call sqlite3_result_error_nomem() to notify 43127e62dbeSdrh ** the database handle that malloc() has failed and return NULL. 43227e62dbeSdrh ** If nByte is larger than the maximum string or blob length, then 43327e62dbeSdrh ** raise an SQLITE_TOOBIG exception and return NULL. 43426783a58Sdanielk1977 */ 435b1a6c3c1Sdrh static void *contextMalloc(sqlite3_context *context, i64 nByte){ 436bb4957f8Sdrh char *z; 43727e62dbeSdrh sqlite3 *db = sqlite3_context_db_handle(context); 438ef31c6aaSdrh assert( nByte>0 ); 43927e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); 44027e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); 44127e62dbeSdrh if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 442bb4957f8Sdrh sqlite3_result_error_toobig(context); 443bb4957f8Sdrh z = 0; 444bb4957f8Sdrh }else{ 445da4ca9d1Sdrh z = sqlite3Malloc(nByte); 446ef31c6aaSdrh if( !z ){ 447a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 448a1644fd8Sdanielk1977 } 449bb4957f8Sdrh } 450a1644fd8Sdanielk1977 return z; 451a1644fd8Sdanielk1977 } 452a1644fd8Sdanielk1977 453dc04c583Sdrh /* 454dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 455dc04c583Sdrh */ 4560ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 4577a521cfbSdrh char *z1; 4587a521cfbSdrh const char *z2; 4599310ef23Sdrh int i, n; 4601d34fdecSdrh UNUSED_PARAMETER(argc); 4617a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4621f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4631f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4641f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4657a521cfbSdrh if( z2 ){ 466b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4677a521cfbSdrh if( z1 ){ 468df901d34Sdrh for(i=0; i<n; i++){ 469df901d34Sdrh z1[i] = (char)sqlite3Toupper(z2[i]); 470dc04c583Sdrh } 471df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4727a521cfbSdrh } 4737a521cfbSdrh } 474dc04c583Sdrh } 4750ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 476df901d34Sdrh char *z1; 4777a521cfbSdrh const char *z2; 4789310ef23Sdrh int i, n; 4791d34fdecSdrh UNUSED_PARAMETER(argc); 4807a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4811f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4821f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4831f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4847a521cfbSdrh if( z2 ){ 485b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4867a521cfbSdrh if( z1 ){ 487df901d34Sdrh for(i=0; i<n; i++){ 488df901d34Sdrh z1[i] = sqlite3Tolower(z2[i]); 489dc04c583Sdrh } 490df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4917a521cfbSdrh } 4927a521cfbSdrh } 493dc04c583Sdrh } 494dc04c583Sdrh 495ae6bb957Sdrh /* 496cca9f3d2Sdrh ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented 497cca9f3d2Sdrh ** as VDBE code so that unused argument values do not have to be computed. 498cca9f3d2Sdrh ** However, we still need some kind of function implementation for this 499cca9f3d2Sdrh ** routines in the function table. The noopFunc macro provides this. 500cca9f3d2Sdrh ** noopFunc will never be called so it doesn't matter what the implementation 501cca9f3d2Sdrh ** is. We might as well use the "version()" function as a substitute. 502ae6bb957Sdrh */ 503cca9f3d2Sdrh #define noopFunc versionFunc /* Substitute function - never called */ 5043212e182Sdrh 5053212e182Sdrh /* 506f9ffac96Sdrh ** Implementation of random(). Return a random integer. 507f9ffac96Sdrh */ 508f9b596ebSdrh static void randomFunc( 509f9b596ebSdrh sqlite3_context *context, 51062c14b34Sdanielk1977 int NotUsed, 51162c14b34Sdanielk1977 sqlite3_value **NotUsed2 512f9b596ebSdrh ){ 51352fc849aSdrh sqlite_int64 r; 51462c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 5152fa1868fSdrh sqlite3_randomness(sizeof(r), &r); 5163034e3d3Sdrh if( r<0 ){ 5173034e3d3Sdrh /* We need to prevent a random number of 0x8000000000000000 5183034e3d3Sdrh ** (or -9223372036854775808) since when you do abs() of that 5193034e3d3Sdrh ** number of you get the same value back again. To do this 5203034e3d3Sdrh ** in a way that is testable, mask the sign bit off of negative 5213034e3d3Sdrh ** values, resulting in a positive value. Then take the 5223034e3d3Sdrh ** 2s complement of that positive value. The end result can 5233034e3d3Sdrh ** therefore be no less than -9223372036854775807. 5243034e3d3Sdrh */ 525af8001bfSdrh r = -(r & LARGEST_INT64); 5263034e3d3Sdrh } 52752fc849aSdrh sqlite3_result_int64(context, r); 528f9ffac96Sdrh } 529f9ffac96Sdrh 530f9ffac96Sdrh /* 531137c728fSdrh ** Implementation of randomblob(N). Return a random blob 532137c728fSdrh ** that is N bytes long. 53363cf66f0Sdrh */ 534137c728fSdrh static void randomBlob( 53563cf66f0Sdrh sqlite3_context *context, 53663cf66f0Sdrh int argc, 53763cf66f0Sdrh sqlite3_value **argv 53863cf66f0Sdrh ){ 5393cb79202Sdrh sqlite3_int64 n; 540137c728fSdrh unsigned char *p; 54163cf66f0Sdrh assert( argc==1 ); 542f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 5433cb79202Sdrh n = sqlite3_value_int64(argv[0]); 544023ae03aSdrh if( n<1 ){ 545023ae03aSdrh n = 1; 546023ae03aSdrh } 547a1644fd8Sdanielk1977 p = contextMalloc(context, n); 54802d85836Sdrh if( p ){ 5492fa1868fSdrh sqlite3_randomness(n, p); 55017435752Sdrh sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 55102d85836Sdrh } 55263cf66f0Sdrh } 55363cf66f0Sdrh 55463cf66f0Sdrh /* 5556ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 55624b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 5576ed41ad7Sdrh */ 55851ad0ecdSdanielk1977 static void last_insert_rowid( 5590ae8b831Sdanielk1977 sqlite3_context *context, 56062c14b34Sdanielk1977 int NotUsed, 56162c14b34Sdanielk1977 sqlite3_value **NotUsed2 56251ad0ecdSdanielk1977 ){ 563fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 56462c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 565ab2f1f95Sdrh /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a 566ab2f1f95Sdrh ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface 567ab2f1f95Sdrh ** function. */ 568f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 5696ed41ad7Sdrh } 5706ed41ad7Sdrh 571f146a776Srdc /* 572ab2f1f95Sdrh ** Implementation of the changes() SQL function. 573ab2f1f95Sdrh ** 574c0bd26a2Sdrh ** IMP: R-32760-32347 The changes() SQL function is a wrapper 575c0bd26a2Sdrh ** around the sqlite3_changes64() C/C++ function and hence follows the 576c0bd26a2Sdrh ** same rules for counting changes. 577f146a776Srdc */ 578b28af71aSdanielk1977 static void changes( 579f9b596ebSdrh sqlite3_context *context, 58062c14b34Sdanielk1977 int NotUsed, 58162c14b34Sdanielk1977 sqlite3_value **NotUsed2 582f9b596ebSdrh ){ 583fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 58462c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 5852c718873Sdan sqlite3_result_int64(context, sqlite3_changes64(db)); 586b0c374ffSrdc } 587f146a776Srdc 588f146a776Srdc /* 589b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 59010496f76Slarrybr ** the same as the sqlite3_total_changes64() API function. 591f146a776Srdc */ 592b28af71aSdanielk1977 static void total_changes( 5930ae8b831Sdanielk1977 sqlite3_context *context, 59462c14b34Sdanielk1977 int NotUsed, 59562c14b34Sdanielk1977 sqlite3_value **NotUsed2 59651ad0ecdSdanielk1977 ){ 597fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 59862c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 599c0bd26a2Sdrh /* IMP: R-11217-42568 This function is a wrapper around the 600c0bd26a2Sdrh ** sqlite3_total_changes64() C/C++ interface. */ 60110496f76Slarrybr sqlite3_result_int64(context, sqlite3_total_changes64(db)); 602b0c374ffSrdc } 603b0c374ffSrdc 6046ed41ad7Sdrh /* 6054e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 606d02eb1fdSdanielk1977 */ 6074e5ffc5fSdrh struct compareInfo { 60807e83472Sdrh u8 matchAll; /* "*" or "%" */ 60907e83472Sdrh u8 matchOne; /* "?" or "_" */ 61007e83472Sdrh u8 matchSet; /* "[" or 0 */ 61107e83472Sdrh u8 noCase; /* true to ignore case differences */ 612d02eb1fdSdanielk1977 }; 61355ef4d97Sdrh 614b9175aedSdrh /* 615b9175aedSdrh ** For LIKE and GLOB matching on EBCDIC machines, assume that every 616b0870486Sdrh ** character is exactly one byte in size. Also, provde the Utf8Read() 617b0870486Sdrh ** macro for fast reading of the next character in the common case where 618b0870486Sdrh ** the next character is ASCII. 619b9175aedSdrh */ 620b9175aedSdrh #if defined(SQLITE_EBCDIC) 62142610961Sdrh # define sqlite3Utf8Read(A) (*((*A)++)) 622b0870486Sdrh # define Utf8Read(A) (*(A++)) 623b9175aedSdrh #else 624b0870486Sdrh # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) 625b9175aedSdrh #endif 626b9175aedSdrh 6274e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 62870031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 62970031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 63055ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 63170031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 63270031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 63355ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 634d02eb1fdSdanielk1977 635d02eb1fdSdanielk1977 /* 636698a01caSdrh ** Possible error returns from patternMatch() 637698a01caSdrh */ 638698a01caSdrh #define SQLITE_MATCH 0 639698a01caSdrh #define SQLITE_NOMATCH 1 640698a01caSdrh #define SQLITE_NOWILDCARDMATCH 2 641698a01caSdrh 642698a01caSdrh /* 643698a01caSdrh ** Compare two UTF-8 strings for equality where the first string is 644698a01caSdrh ** a GLOB or LIKE expression. Return values: 645698a01caSdrh ** 646698a01caSdrh ** SQLITE_MATCH: Match 647698a01caSdrh ** SQLITE_NOMATCH: No match 648698a01caSdrh ** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards. 6490ac65892Sdrh ** 6504e5ffc5fSdrh ** Globbing rules: 6510ac65892Sdrh ** 6524e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 653d02eb1fdSdanielk1977 ** 6544e5ffc5fSdrh ** '?' Matches exactly one character. 6554e5ffc5fSdrh ** 6564e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 6574e5ffc5fSdrh ** characters. 6584e5ffc5fSdrh ** 6594e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 6604e5ffc5fSdrh ** 6614e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 6624e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 6634e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 6644e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 6654e5ffc5fSdrh ** it the last character in the list. 6664e5ffc5fSdrh ** 6679fdfdc89Sdrh ** Like matching rules: 6689fdfdc89Sdrh ** 6699fdfdc89Sdrh ** '%' Matches any sequence of zero or more characters 6709fdfdc89Sdrh ** 6719fdfdc89Sdrh *** '_' Matches any one character 6729fdfdc89Sdrh ** 6739fdfdc89Sdrh ** Ec Where E is the "esc" character and c is any other 6749fdfdc89Sdrh ** character, including '%', '_', and esc, match exactly c. 6759fdfdc89Sdrh ** 676b0870486Sdrh ** The comments within this routine usually assume glob matching. 6779fdfdc89Sdrh ** 6784e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 6790ac65892Sdrh */ 6807c6303c0Sdanielk1977 static int patternCompare( 6814e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 6824e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 6837c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 684698a01caSdrh u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */ 68551ad0ecdSdanielk1977 ){ 6869fdfdc89Sdrh u32 c, c2; /* Next pattern and input string chars */ 6879fdfdc89Sdrh u32 matchOne = pInfo->matchOne; /* "?" or "_" */ 6889fdfdc89Sdrh u32 matchAll = pInfo->matchAll; /* "*" or "%" */ 6899fdfdc89Sdrh u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ 6909fdfdc89Sdrh const u8 *zEscaped = 0; /* One past the last escaped input char */ 691d02eb1fdSdanielk1977 692b0870486Sdrh while( (c = Utf8Read(zPattern))!=0 ){ 6939fdfdc89Sdrh if( c==matchAll ){ /* Match "*" */ 6949fdfdc89Sdrh /* Skip over multiple "*" characters in the pattern. If there 6959fdfdc89Sdrh ** are also "?" characters, skip those as well, but consume a 6969fdfdc89Sdrh ** single character of the input string for each "?" skipped */ 69733941691Sdrh while( (c=Utf8Read(zPattern)) == matchAll 69833941691Sdrh || (c == matchOne && matchOne!=0) ){ 69942610961Sdrh if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ 700698a01caSdrh return SQLITE_NOWILDCARDMATCH; 701d02eb1fdSdanielk1977 } 7024e5ffc5fSdrh } 70366150956Sdrh if( c==0 ){ 704698a01caSdrh return SQLITE_MATCH; /* "*" at the end of the pattern matches */ 70588b3322fSdrh }else if( c==matchOther ){ 70607e83472Sdrh if( pInfo->matchSet==0 ){ 70742610961Sdrh c = sqlite3Utf8Read(&zPattern); 708698a01caSdrh if( c==0 ) return SQLITE_NOWILDCARDMATCH; 70988b3322fSdrh }else{ 7109fdfdc89Sdrh /* "[...]" immediately follows the "*". We have to do a slow 7119fdfdc89Sdrh ** recursive search in this case, but it is an unusual case. */ 71288b3322fSdrh assert( matchOther<0x80 ); /* '[' is a single-byte character */ 713698a01caSdrh while( *zString ){ 714698a01caSdrh int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther); 715698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 7164a919118Sdrh SQLITE_SKIP_UTF8(zString); 7174e5ffc5fSdrh } 718698a01caSdrh return SQLITE_NOWILDCARDMATCH; 71966150956Sdrh } 72088b3322fSdrh } 7219fdfdc89Sdrh 7229fdfdc89Sdrh /* At this point variable c contains the first character of the 7239fdfdc89Sdrh ** pattern string past the "*". Search in the input string for the 7241a4a7376Sdan ** first matching character and recursively continue the match from 7259fdfdc89Sdrh ** that point. 7269fdfdc89Sdrh ** 7279fdfdc89Sdrh ** For a case-insensitive search, set variable cx to be the same as 7289fdfdc89Sdrh ** c but in the other case and search the input string for either 7299fdfdc89Sdrh ** c or cx. 7309fdfdc89Sdrh */ 7319fdfdc89Sdrh if( c<=0x80 ){ 732eba21f9eSdrh char zStop[3]; 733698a01caSdrh int bMatch; 7349fdfdc89Sdrh if( noCase ){ 735eba21f9eSdrh zStop[0] = sqlite3Toupper(c); 736eba21f9eSdrh zStop[1] = sqlite3Tolower(c); 737eba21f9eSdrh zStop[2] = 0; 738ad7dd425Sdanielk1977 }else{ 739eba21f9eSdrh zStop[0] = c; 740eba21f9eSdrh zStop[1] = 0; 74166150956Sdrh } 742eba21f9eSdrh while(1){ 743eba21f9eSdrh zString += strcspn((const char*)zString, zStop); 744eba21f9eSdrh if( zString[0]==0 ) break; 745eba21f9eSdrh zString++; 746698a01caSdrh bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 747698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 7484e5ffc5fSdrh } 74988b3322fSdrh }else{ 750698a01caSdrh int bMatch; 751b0870486Sdrh while( (c2 = Utf8Read(zString))!=0 ){ 7529fdfdc89Sdrh if( c2!=c ) continue; 753698a01caSdrh bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 754698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 75566150956Sdrh } 75688b3322fSdrh } 757698a01caSdrh return SQLITE_NOWILDCARDMATCH; 7589fdfdc89Sdrh } 75988b3322fSdrh if( c==matchOther ){ 76007e83472Sdrh if( pInfo->matchSet==0 ){ 76188b3322fSdrh c = sqlite3Utf8Read(&zPattern); 762698a01caSdrh if( c==0 ) return SQLITE_NOMATCH; 7639fdfdc89Sdrh zEscaped = zPattern; 76488b3322fSdrh }else{ 7651aa4f3e5Sdrh u32 prior_c = 0; 7669fdfdc89Sdrh int seen = 0; 7679fdfdc89Sdrh int invert = 0; 76842610961Sdrh c = sqlite3Utf8Read(&zString); 769698a01caSdrh if( c==0 ) return SQLITE_NOMATCH; 77042610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 77166150956Sdrh if( c2=='^' ){ 77266150956Sdrh invert = 1; 77342610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 77466150956Sdrh } 7754e5ffc5fSdrh if( c2==']' ){ 7764e5ffc5fSdrh if( c==']' ) seen = 1; 77742610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7784e5ffc5fSdrh } 77966150956Sdrh while( c2 && c2!=']' ){ 78066150956Sdrh if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ 78142610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7824e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 7834e5ffc5fSdrh prior_c = 0; 78466150956Sdrh }else{ 78566150956Sdrh if( c==c2 ){ 7864e5ffc5fSdrh seen = 1; 78766150956Sdrh } 7884e5ffc5fSdrh prior_c = c2; 789d02eb1fdSdanielk1977 } 79042610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 791d02eb1fdSdanielk1977 } 79266150956Sdrh if( c2==0 || (seen ^ invert)==0 ){ 793698a01caSdrh return SQLITE_NOMATCH; 79466150956Sdrh } 79588b3322fSdrh continue; 796328d913cSdrh } 79788b3322fSdrh } 798b0870486Sdrh c2 = Utf8Read(zString); 79988b3322fSdrh if( c==c2 ) continue; 800c80937a5Sdrh if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){ 8019fdfdc89Sdrh continue; 8029fdfdc89Sdrh } 8039fdfdc89Sdrh if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; 804698a01caSdrh return SQLITE_NOMATCH; 8050ac65892Sdrh } 806698a01caSdrh return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH; 8074e5ffc5fSdrh } 8084e5ffc5fSdrh 80955ef4d97Sdrh /* 810698a01caSdrh ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and 811698a01caSdrh ** non-zero if there is no match. 81256282a5bSdrh */ 81356282a5bSdrh int sqlite3_strglob(const char *zGlobPattern, const char *zString){ 814698a01caSdrh return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); 81556282a5bSdrh } 81656282a5bSdrh 81756282a5bSdrh /* 818698a01caSdrh ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for 819698a01caSdrh ** a miss - like strcmp(). 8208b4a94adSdrh */ 8218b4a94adSdrh int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ 822698a01caSdrh return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); 8238b4a94adSdrh } 8248b4a94adSdrh 8258b4a94adSdrh /* 82655ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 82755ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 82855ef4d97Sdrh ** only. 82955ef4d97Sdrh */ 83055ef4d97Sdrh #ifdef SQLITE_TEST 83155ef4d97Sdrh int sqlite3_like_count = 0; 83255ef4d97Sdrh #endif 83355ef4d97Sdrh 8343f6b0874Sdanielk1977 8353f6b0874Sdanielk1977 /* 8363f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 8373f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 8383f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 8393f6b0874Sdanielk1977 ** 8403f6b0874Sdanielk1977 ** A LIKE B 8413f6b0874Sdanielk1977 ** 8423f6b0874Sdanielk1977 ** is implemented as like(B,A). 8433f6b0874Sdanielk1977 ** 84455ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 84555ef4d97Sdrh ** the GLOB operator. 8463f6b0874Sdanielk1977 */ 8473f6b0874Sdanielk1977 static void likeFunc( 8483f6b0874Sdanielk1977 sqlite3_context *context, 8493f6b0874Sdanielk1977 int argc, 8503f6b0874Sdanielk1977 sqlite3_value **argv 8513f6b0874Sdanielk1977 ){ 852beb818d1Sdrh const unsigned char *zA, *zB; 85307e83472Sdrh u32 escape; 85427e62dbeSdrh int nPat; 855bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 85607e83472Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 857589c7876Sdrh struct compareInfo backupInfo; 858beb818d1Sdrh 85941d2e66eSdrh #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS 86041d2e66eSdrh if( sqlite3_value_type(argv[0])==SQLITE_BLOB 86141d2e66eSdrh || sqlite3_value_type(argv[1])==SQLITE_BLOB 86241d2e66eSdrh ){ 86341d2e66eSdrh #ifdef SQLITE_TEST 86441d2e66eSdrh sqlite3_like_count++; 86541d2e66eSdrh #endif 86641d2e66eSdrh sqlite3_result_int(context, 0); 86741d2e66eSdrh return; 86841d2e66eSdrh } 86941d2e66eSdrh #endif 8701f0feef8Sdrh 871beb818d1Sdrh /* Limit the length of the LIKE or GLOB pattern to avoid problems 872beb818d1Sdrh ** of deep recursion and N*N behavior in patternCompare(). 873beb818d1Sdrh */ 87427e62dbeSdrh nPat = sqlite3_value_bytes(argv[0]); 87527e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); 87627e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); 87727e62dbeSdrh if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ 878beb818d1Sdrh sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); 879beb818d1Sdrh return; 880beb818d1Sdrh } 8817c6303c0Sdanielk1977 if( argc==3 ){ 8827c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 8837c6303c0Sdanielk1977 ** Otherwise, return an error. 8847c6303c0Sdanielk1977 */ 8857c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 8867a521cfbSdrh if( zEsc==0 ) return; 887ee85813cSdrh if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ 8887c6303c0Sdanielk1977 sqlite3_result_error(context, 8897c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 8907c6303c0Sdanielk1977 return; 8917c6303c0Sdanielk1977 } 89242610961Sdrh escape = sqlite3Utf8Read(&zEsc); 893589c7876Sdrh if( escape==pInfo->matchAll || escape==pInfo->matchOne ){ 894589c7876Sdrh memcpy(&backupInfo, pInfo, sizeof(backupInfo)); 895589c7876Sdrh pInfo = &backupInfo; 896589c7876Sdrh if( escape==pInfo->matchAll ) pInfo->matchAll = 0; 897589c7876Sdrh if( escape==pInfo->matchOne ) pInfo->matchOne = 0; 898589c7876Sdrh } 89907e83472Sdrh }else{ 90007e83472Sdrh escape = pInfo->matchSet; 9017c6303c0Sdanielk1977 } 902cf833239Sdrh zB = sqlite3_value_text(argv[0]); 903cf833239Sdrh zA = sqlite3_value_text(argv[1]); 9043f6b0874Sdanielk1977 if( zA && zB ){ 90555ef4d97Sdrh #ifdef SQLITE_TEST 90655ef4d97Sdrh sqlite3_like_count++; 90755ef4d97Sdrh #endif 908f49759bfSdrh sqlite3_result_int(context, 909f49759bfSdrh patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH); 91051ad0ecdSdanielk1977 } 9118912d106Sdrh } 9128912d106Sdrh 9138912d106Sdrh /* 9148912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 9158912d106Sdrh ** argument if the arguments are different. The result is NULL if the 9168912d106Sdrh ** arguments are equal to each other. 9178912d106Sdrh */ 918f9b596ebSdrh static void nullifFunc( 919f9b596ebSdrh sqlite3_context *context, 92062c14b34Sdanielk1977 int NotUsed, 921f9b596ebSdrh sqlite3_value **argv 922f9b596ebSdrh ){ 923dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 92462c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 925dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 926f4479501Sdrh sqlite3_result_value(context, argv[0]); 9278912d106Sdrh } 9280ac65892Sdrh } 9290ac65892Sdrh 930647cb0e1Sdrh /* 93147baebc2Sdrh ** Implementation of the sqlite_version() function. The result is the version 932647cb0e1Sdrh ** of the SQLite library that is running. 933647cb0e1Sdrh */ 934f9b596ebSdrh static void versionFunc( 935f9b596ebSdrh sqlite3_context *context, 93662c14b34Sdanielk1977 int NotUsed, 93762c14b34Sdanielk1977 sqlite3_value **NotUsed2 938f9b596ebSdrh ){ 93962c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 940ab2f1f95Sdrh /* IMP: R-48699-48617 This function is an SQL wrapper around the 941ab2f1f95Sdrh ** sqlite3_libversion() C-interface. */ 942ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); 943647cb0e1Sdrh } 944647cb0e1Sdrh 94547baebc2Sdrh /* 94647baebc2Sdrh ** Implementation of the sqlite_source_id() function. The result is a string 94747baebc2Sdrh ** that identifies the particular version of the source code used to build 94847baebc2Sdrh ** SQLite. 94947baebc2Sdrh */ 95047baebc2Sdrh static void sourceidFunc( 95147baebc2Sdrh sqlite3_context *context, 95247baebc2Sdrh int NotUsed, 95347baebc2Sdrh sqlite3_value **NotUsed2 95447baebc2Sdrh ){ 95547baebc2Sdrh UNUSED_PARAMETER2(NotUsed, NotUsed2); 956ab2f1f95Sdrh /* IMP: R-24470-31136 This function is an SQL wrapper around the 957ab2f1f95Sdrh ** sqlite3_sourceid() C interface. */ 958ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); 95947baebc2Sdrh } 96047baebc2Sdrh 961bdea6d13Sshaneh /* 9623ca84ef6Sdrh ** Implementation of the sqlite_log() function. This is a wrapper around 9633ca84ef6Sdrh ** sqlite3_log(). The return value is NULL. The function exists purely for 9643ca84ef6Sdrh ** its side-effects. 9653ca84ef6Sdrh */ 966840561f2Sdrh static void errlogFunc( 9673ca84ef6Sdrh sqlite3_context *context, 9683ca84ef6Sdrh int argc, 9693ca84ef6Sdrh sqlite3_value **argv 9703ca84ef6Sdrh ){ 9713ca84ef6Sdrh UNUSED_PARAMETER(argc); 9723ca84ef6Sdrh UNUSED_PARAMETER(context); 9733ca84ef6Sdrh sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); 9743ca84ef6Sdrh } 9753ca84ef6Sdrh 9763ca84ef6Sdrh /* 977dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_used() function. 978dc97a8cdSshaneh ** The result is an integer that identifies if the compiler option 979dc97a8cdSshaneh ** was used to build SQLite. 980bdea6d13Sshaneh */ 981dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 982dc97a8cdSshaneh static void compileoptionusedFunc( 983bdea6d13Sshaneh sqlite3_context *context, 984dc97a8cdSshaneh int argc, 985dc97a8cdSshaneh sqlite3_value **argv 986bdea6d13Sshaneh ){ 987dc97a8cdSshaneh const char *zOptName; 988dc97a8cdSshaneh assert( argc==1 ); 989dc97a8cdSshaneh UNUSED_PARAMETER(argc); 990a3e414cdSdrh /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL 991a3e414cdSdrh ** function is a wrapper around the sqlite3_compileoption_used() C/C++ 992a3e414cdSdrh ** function. 993a3e414cdSdrh */ 994264a2d4dSdrh if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 995dc97a8cdSshaneh sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); 996bdea6d13Sshaneh } 997dc97a8cdSshaneh } 998dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 999dc97a8cdSshaneh 1000dc97a8cdSshaneh /* 1001dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_get() function. 1002dc97a8cdSshaneh ** The result is a string that identifies the compiler options 1003dc97a8cdSshaneh ** used to build SQLite. 1004dc97a8cdSshaneh */ 1005dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 1006dc97a8cdSshaneh static void compileoptiongetFunc( 1007dc97a8cdSshaneh sqlite3_context *context, 1008dc97a8cdSshaneh int argc, 1009dc97a8cdSshaneh sqlite3_value **argv 1010dc97a8cdSshaneh ){ 1011dc97a8cdSshaneh int n; 1012dc97a8cdSshaneh assert( argc==1 ); 1013dc97a8cdSshaneh UNUSED_PARAMETER(argc); 1014a3e414cdSdrh /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function 1015a3e414cdSdrh ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. 1016a3e414cdSdrh */ 1017dc97a8cdSshaneh n = sqlite3_value_int(argv[0]); 1018dc97a8cdSshaneh sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); 1019dc97a8cdSshaneh } 1020dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 1021bdea6d13Sshaneh 1022137c728fSdrh /* Array for converting from half-bytes (nybbles) into ASCII hex 1023137c728fSdrh ** digits. */ 1024137c728fSdrh static const char hexdigits[] = { 1025137c728fSdrh '0', '1', '2', '3', '4', '5', '6', '7', 1026137c728fSdrh '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 1027137c728fSdrh }; 1028d641d646Sdanielk1977 102947394703Sdrh /* 103047394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 103147394703Sdrh ** argument. If the argument is numeric, the return value is the same as 103247394703Sdrh ** the argument. If the argument is NULL, the return value is the string 103347394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 103447394703Sdrh ** single-quote escapes. 103547394703Sdrh */ 10360ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1037a0df4ccfSdrh assert( argc==1 ); 10381d34fdecSdrh UNUSED_PARAMETER(argc); 1039f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 10409c054830Sdrh case SQLITE_FLOAT: { 104172b3fbc7Sdrh double r1, r2; 104272b3fbc7Sdrh char zBuf[50]; 10432b434a7eSmistachkin r1 = sqlite3_value_double(argv[0]); 104472b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); 104572b3fbc7Sdrh sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); 104672b3fbc7Sdrh if( r1!=r2 ){ 104772b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); 104872b3fbc7Sdrh } 104972b3fbc7Sdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 105072b3fbc7Sdrh break; 105172b3fbc7Sdrh } 105272b3fbc7Sdrh case SQLITE_INTEGER: { 1053f4479501Sdrh sqlite3_result_value(context, argv[0]); 1054f9b596ebSdrh break; 1055f9b596ebSdrh } 10563f41e976Sdanielk1977 case SQLITE_BLOB: { 10573f41e976Sdanielk1977 char *zText = 0; 10583f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 10591f0feef8Sdrh int nBlob = sqlite3_value_bytes(argv[0]); 10601f0feef8Sdrh assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1061b1a6c3c1Sdrh zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 1062a1644fd8Sdanielk1977 if( zText ){ 10633f41e976Sdanielk1977 int i; 10643f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 10653f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 10663f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 10673f41e976Sdanielk1977 } 10683f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 10693f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 10703f41e976Sdanielk1977 zText[0] = 'X'; 10713f41e976Sdanielk1977 zText[1] = '\''; 1072d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 107317435752Sdrh sqlite3_free(zText); 10743f41e976Sdanielk1977 } 10753f41e976Sdanielk1977 break; 10763f41e976Sdanielk1977 } 10779c054830Sdrh case SQLITE_TEXT: { 1078023ae03aSdrh int i,j; 1079023ae03aSdrh u64 n; 10802646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 108147394703Sdrh char *z; 1082f9b596ebSdrh 10837a521cfbSdrh if( zArg==0 ) return; 1084023ae03aSdrh for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 1085b1a6c3c1Sdrh z = contextMalloc(context, ((i64)i)+((i64)n)+3); 1086a1644fd8Sdanielk1977 if( z ){ 108747394703Sdrh z[0] = '\''; 108851ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 108951ad0ecdSdanielk1977 z[j++] = zArg[i]; 109051ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 109147394703Sdrh z[j++] = '\''; 109247394703Sdrh } 109347394703Sdrh } 109447394703Sdrh z[j++] = '\''; 109547394703Sdrh z[j] = 0; 1096a1644fd8Sdanielk1977 sqlite3_result_text(context, z, j, sqlite3_free); 1097a1644fd8Sdanielk1977 } 1098a0df4ccfSdrh break; 1099a0df4ccfSdrh } 1100a0df4ccfSdrh default: { 1101a0df4ccfSdrh assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 1102a0df4ccfSdrh sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 1103a0df4ccfSdrh break; 110447394703Sdrh } 110547394703Sdrh } 1106f9b596ebSdrh } 110747394703Sdrh 1108137c728fSdrh /* 1109d495d8c9Sdrh ** The unicode() function. Return the integer unicode code-point value 1110d495d8c9Sdrh ** for the first character of the input string. 1111d495d8c9Sdrh */ 1112d495d8c9Sdrh static void unicodeFunc( 1113d495d8c9Sdrh sqlite3_context *context, 1114d495d8c9Sdrh int argc, 1115d495d8c9Sdrh sqlite3_value **argv 1116d495d8c9Sdrh ){ 1117d495d8c9Sdrh const unsigned char *z = sqlite3_value_text(argv[0]); 11181d59d036Sdrh (void)argc; 1119d495d8c9Sdrh if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); 1120d495d8c9Sdrh } 1121d495d8c9Sdrh 1122d495d8c9Sdrh /* 1123d495d8c9Sdrh ** The char() function takes zero or more arguments, each of which is 1124d495d8c9Sdrh ** an integer. It constructs a string where each character of the string 1125d495d8c9Sdrh ** is the unicode character for the corresponding integer argument. 1126d495d8c9Sdrh */ 1127d495d8c9Sdrh static void charFunc( 1128d495d8c9Sdrh sqlite3_context *context, 1129d495d8c9Sdrh int argc, 1130d495d8c9Sdrh sqlite3_value **argv 1131d495d8c9Sdrh ){ 1132d495d8c9Sdrh unsigned char *z, *zOut; 1133d495d8c9Sdrh int i; 1134f3cdcdccSdrh zOut = z = sqlite3_malloc64( argc*4+1 ); 1135d495d8c9Sdrh if( z==0 ){ 1136d495d8c9Sdrh sqlite3_result_error_nomem(context); 1137d495d8c9Sdrh return; 1138d495d8c9Sdrh } 1139d495d8c9Sdrh for(i=0; i<argc; i++){ 1140c9545442Smistachkin sqlite3_int64 x; 1141d495d8c9Sdrh unsigned c; 1142d495d8c9Sdrh x = sqlite3_value_int64(argv[i]); 1143d495d8c9Sdrh if( x<0 || x>0x10ffff ) x = 0xfffd; 1144d495d8c9Sdrh c = (unsigned)(x & 0x1fffff); 1145fe7a5d11Sdrh if( c<0x00080 ){ 1146fe7a5d11Sdrh *zOut++ = (u8)(c&0xFF); 1147fe7a5d11Sdrh }else if( c<0x00800 ){ 1148fe7a5d11Sdrh *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); 1149fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1150fe7a5d11Sdrh }else if( c<0x10000 ){ 1151fe7a5d11Sdrh *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); 1152fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1153fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1154d495d8c9Sdrh }else{ 1155fe7a5d11Sdrh *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); 1156fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); 1157fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1158fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1159fe7a5d11Sdrh } \ 1160d495d8c9Sdrh } 1161bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); 1162d495d8c9Sdrh } 1163d495d8c9Sdrh 1164d495d8c9Sdrh /* 1165137c728fSdrh ** The hex() function. Interpret the argument as a blob. Return 1166137c728fSdrh ** a hexadecimal rendering as text. 1167137c728fSdrh */ 1168137c728fSdrh static void hexFunc( 1169137c728fSdrh sqlite3_context *context, 1170137c728fSdrh int argc, 1171137c728fSdrh sqlite3_value **argv 1172137c728fSdrh ){ 1173137c728fSdrh int i, n; 1174137c728fSdrh const unsigned char *pBlob; 1175137c728fSdrh char *zHex, *z; 1176137c728fSdrh assert( argc==1 ); 1177f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 11781f0feef8Sdrh pBlob = sqlite3_value_blob(argv[0]); 1179137c728fSdrh n = sqlite3_value_bytes(argv[0]); 11801f0feef8Sdrh assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1181b1a6c3c1Sdrh z = zHex = contextMalloc(context, ((i64)n)*2 + 1); 1182a1644fd8Sdanielk1977 if( zHex ){ 1183137c728fSdrh for(i=0; i<n; i++, pBlob++){ 1184137c728fSdrh unsigned char c = *pBlob; 1185137c728fSdrh *(z++) = hexdigits[(c>>4)&0xf]; 1186137c728fSdrh *(z++) = hexdigits[c&0xf]; 1187137c728fSdrh } 1188137c728fSdrh *z = 0; 1189137c728fSdrh sqlite3_result_text(context, zHex, n*2, sqlite3_free); 1190137c728fSdrh } 1191a1644fd8Sdanielk1977 } 1192137c728fSdrh 119326b6d90dSdrh /* 11948cff382eSdrh ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 11958cff382eSdrh */ 11968cff382eSdrh static void zeroblobFunc( 11978cff382eSdrh sqlite3_context *context, 11988cff382eSdrh int argc, 11998cff382eSdrh sqlite3_value **argv 12008cff382eSdrh ){ 120198640a3fSdrh i64 n; 1202a4d5ae8fSdan int rc; 12038cff382eSdrh assert( argc==1 ); 1204f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 120598640a3fSdrh n = sqlite3_value_int64(argv[0]); 120653e66c3cSdrh if( n<0 ) n = 0; 1207a4d5ae8fSdan rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ 1208a4d5ae8fSdan if( rc ){ 1209a4d5ae8fSdan sqlite3_result_error_code(context, rc); 12108cff382eSdrh } 121198640a3fSdrh } 12128cff382eSdrh 12138cff382eSdrh /* 121426b6d90dSdrh ** The replace() function. Three arguments are all strings: call 121526b6d90dSdrh ** them A, B, and C. The result is also a string which is derived 1216f7b5496eSdrh ** from A by replacing every occurrence of B with C. The match 121726b6d90dSdrh ** must be exact. Collating sequences are not used. 121826b6d90dSdrh */ 121926b6d90dSdrh static void replaceFunc( 122026b6d90dSdrh sqlite3_context *context, 122126b6d90dSdrh int argc, 122226b6d90dSdrh sqlite3_value **argv 122326b6d90dSdrh ){ 122426b6d90dSdrh const unsigned char *zStr; /* The input string A */ 122526b6d90dSdrh const unsigned char *zPattern; /* The pattern string B */ 122626b6d90dSdrh const unsigned char *zRep; /* The replacement string C */ 122726b6d90dSdrh unsigned char *zOut; /* The output */ 122826b6d90dSdrh int nStr; /* Size of zStr */ 122926b6d90dSdrh int nPattern; /* Size of zPattern */ 123026b6d90dSdrh int nRep; /* Size of zRep */ 12312e6400baSdrh i64 nOut; /* Maximum size of zOut */ 123226b6d90dSdrh int loopLimit; /* Last zStr[] that might match zPattern[] */ 123326b6d90dSdrh int i, j; /* Loop counters */ 1234f3139520Sdrh unsigned cntExpand; /* Number zOut expansions */ 1235f3139520Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 123626b6d90dSdrh 123726b6d90dSdrh assert( argc==3 ); 1238f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 123926b6d90dSdrh zStr = sqlite3_value_text(argv[0]); 12407a521cfbSdrh if( zStr==0 ) return; 12411f0feef8Sdrh nStr = sqlite3_value_bytes(argv[0]); 12421f0feef8Sdrh assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ 124326b6d90dSdrh zPattern = sqlite3_value_text(argv[1]); 1244a605fe8dSdrh if( zPattern==0 ){ 12452333606eSdrh assert( sqlite3_value_type(argv[1])==SQLITE_NULL 12462333606eSdrh || sqlite3_context_db_handle(context)->mallocFailed ); 1247a605fe8dSdrh return; 1248a605fe8dSdrh } 1249a605fe8dSdrh if( zPattern[0]==0 ){ 1250a605fe8dSdrh assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); 1251a605fe8dSdrh sqlite3_result_value(context, argv[0]); 1252a605fe8dSdrh return; 1253a605fe8dSdrh } 12541f0feef8Sdrh nPattern = sqlite3_value_bytes(argv[1]); 12551f0feef8Sdrh assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ 125626b6d90dSdrh zRep = sqlite3_value_text(argv[2]); 12577a521cfbSdrh if( zRep==0 ) return; 12581f0feef8Sdrh nRep = sqlite3_value_bytes(argv[2]); 12591f0feef8Sdrh assert( zRep==sqlite3_value_text(argv[2]) ); 12602e6400baSdrh nOut = nStr + 1; 12612e6400baSdrh assert( nOut<SQLITE_MAX_LENGTH ); 1262b1a6c3c1Sdrh zOut = contextMalloc(context, (i64)nOut); 12632e6400baSdrh if( zOut==0 ){ 12642e6400baSdrh return; 126526b6d90dSdrh } 126626b6d90dSdrh loopLimit = nStr - nPattern; 1267f3139520Sdrh cntExpand = 0; 126826b6d90dSdrh for(i=j=0; i<=loopLimit; i++){ 126926b6d90dSdrh if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 127026b6d90dSdrh zOut[j++] = zStr[i]; 127126b6d90dSdrh }else{ 1272f3139520Sdrh if( nRep>nPattern ){ 12732e6400baSdrh nOut += nRep - nPattern; 1274c86d82f2Sdrh testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); 1275c86d82f2Sdrh testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); 127627e62dbeSdrh if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1277a0206bc8Sdrh sqlite3_result_error_toobig(context); 1278b975598eSdrh sqlite3_free(zOut); 127917374e8fSdanielk1977 return; 128017374e8fSdanielk1977 } 1281f3139520Sdrh cntExpand++; 1282f3139520Sdrh if( (cntExpand&(cntExpand-1))==0 ){ 1283f3139520Sdrh /* Grow the size of the output buffer only on substitutions 1284f3139520Sdrh ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ 1285f3139520Sdrh u8 *zOld; 12864a50aac5Sdrh zOld = zOut; 1287d924e7bcSdrh zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1)); 12882e6400baSdrh if( zOut==0 ){ 1289a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 1290b975598eSdrh sqlite3_free(zOld); 12912e6400baSdrh return; 12922e6400baSdrh } 1293f3139520Sdrh } 1294f3139520Sdrh } 129526b6d90dSdrh memcpy(&zOut[j], zRep, nRep); 129626b6d90dSdrh j += nRep; 129726b6d90dSdrh i += nPattern-1; 129826b6d90dSdrh } 129926b6d90dSdrh } 1300f3139520Sdrh assert( j+nStr-i+1<=nOut ); 130126b6d90dSdrh memcpy(&zOut[j], &zStr[i], nStr-i); 130226b6d90dSdrh j += nStr - i; 130326b6d90dSdrh assert( j<=nOut ); 130426b6d90dSdrh zOut[j] = 0; 130526b6d90dSdrh sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 130626b6d90dSdrh } 130726b6d90dSdrh 1308309b3386Sdrh /* 1309309b3386Sdrh ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 1310309b3386Sdrh ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 1311309b3386Sdrh */ 1312309b3386Sdrh static void trimFunc( 1313309b3386Sdrh sqlite3_context *context, 1314309b3386Sdrh int argc, 1315309b3386Sdrh sqlite3_value **argv 1316309b3386Sdrh ){ 1317309b3386Sdrh const unsigned char *zIn; /* Input string */ 1318309b3386Sdrh const unsigned char *zCharSet; /* Set of characters to trim */ 1319972da427Sdrh unsigned int nIn; /* Number of bytes in input */ 13207209c697Sdrh int flags; /* 1: trimleft 2: trimright 3: trim */ 1321d1e3a616Sdrh int i; /* Loop counter */ 1322972da427Sdrh unsigned int *aLen = 0; /* Length of each character in zCharSet */ 13231bd10f8aSdrh unsigned char **azChar = 0; /* Individual characters in zCharSet */ 1324d1e3a616Sdrh int nChar; /* Number of characters in zCharSet */ 1325d1e3a616Sdrh 1326309b3386Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1327309b3386Sdrh return; 1328309b3386Sdrh } 1329309b3386Sdrh zIn = sqlite3_value_text(argv[0]); 13307a521cfbSdrh if( zIn==0 ) return; 1331972da427Sdrh nIn = (unsigned)sqlite3_value_bytes(argv[0]); 13321f0feef8Sdrh assert( zIn==sqlite3_value_text(argv[0]) ); 1333309b3386Sdrh if( argc==1 ){ 1334972da427Sdrh static const unsigned lenOne[] = { 1 }; 1335a4de4532Sdanielk1977 static unsigned char * const azOne[] = { (u8*)" " }; 1336d1e3a616Sdrh nChar = 1; 1337972da427Sdrh aLen = (unsigned*)lenOne; 1338bc67da48Sdanielk1977 azChar = (unsigned char **)azOne; 1339d1e3a616Sdrh zCharSet = 0; 13407a521cfbSdrh }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 1341309b3386Sdrh return; 1342d1e3a616Sdrh }else{ 1343d1e3a616Sdrh const unsigned char *z; 1344d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 13454a919118Sdrh SQLITE_SKIP_UTF8(z); 1346309b3386Sdrh } 1347d1e3a616Sdrh if( nChar>0 ){ 1348972da427Sdrh azChar = contextMalloc(context, 1349972da427Sdrh ((i64)nChar)*(sizeof(char*)+sizeof(unsigned))); 1350d1e3a616Sdrh if( azChar==0 ){ 1351d1e3a616Sdrh return; 1352d1e3a616Sdrh } 1353972da427Sdrh aLen = (unsigned*)&azChar[nChar]; 1354d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 1355bc67da48Sdanielk1977 azChar[nChar] = (unsigned char *)z; 13564a919118Sdrh SQLITE_SKIP_UTF8(z); 1357972da427Sdrh aLen[nChar] = (unsigned)(z - azChar[nChar]); 1358d1e3a616Sdrh } 1359d1e3a616Sdrh } 1360d1e3a616Sdrh } 1361d1e3a616Sdrh if( nChar>0 ){ 13621fc4129dSshane flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); 1363309b3386Sdrh if( flags & 1 ){ 1364d1e3a616Sdrh while( nIn>0 ){ 1365972da427Sdrh unsigned int len = 0; 1366d1e3a616Sdrh for(i=0; i<nChar; i++){ 1367d1e3a616Sdrh len = aLen[i]; 136827e62dbeSdrh if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break; 1369d1e3a616Sdrh } 1370d1e3a616Sdrh if( i>=nChar ) break; 1371d1e3a616Sdrh zIn += len; 1372d1e3a616Sdrh nIn -= len; 1373309b3386Sdrh } 1374309b3386Sdrh } 1375309b3386Sdrh if( flags & 2 ){ 1376d1e3a616Sdrh while( nIn>0 ){ 1377972da427Sdrh unsigned int len = 0; 1378d1e3a616Sdrh for(i=0; i<nChar; i++){ 1379d1e3a616Sdrh len = aLen[i]; 1380d1e3a616Sdrh if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 1381309b3386Sdrh } 1382d1e3a616Sdrh if( i>=nChar ) break; 1383d1e3a616Sdrh nIn -= len; 1384d1e3a616Sdrh } 1385d1e3a616Sdrh } 1386d1e3a616Sdrh if( zCharSet ){ 1387d1e3a616Sdrh sqlite3_free(azChar); 1388309b3386Sdrh } 1389309b3386Sdrh } 1390309b3386Sdrh sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 1391309b3386Sdrh } 139226b6d90dSdrh 1393a4de4532Sdanielk1977 1394cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 1395cc15313cSdrh /* 1396cc15313cSdrh ** The "unknown" function is automatically substituted in place of 1397cc15313cSdrh ** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN 1398cc15313cSdrh ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used. 1399cc15313cSdrh ** When the "sqlite3" command-line shell is built using this functionality, 1400cc15313cSdrh ** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries 1401cc15313cSdrh ** involving application-defined functions to be examined in a generic 1402cc15313cSdrh ** sqlite3 shell. 1403cc15313cSdrh */ 1404cc15313cSdrh static void unknownFunc( 1405cc15313cSdrh sqlite3_context *context, 1406cc15313cSdrh int argc, 1407cc15313cSdrh sqlite3_value **argv 1408cc15313cSdrh ){ 1409cc15313cSdrh /* no-op */ 1410cc15313cSdrh } 1411cc15313cSdrh #endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/ 1412cc15313cSdrh 1413cc15313cSdrh 14142ba3cccfSdrh /* IMP: R-25361-16150 This function is omitted from SQLite by default. It 14152ba3cccfSdrh ** is only available if the SQLITE_SOUNDEX compile-time option is used 14162ba3cccfSdrh ** when SQLite is built. 14172ba3cccfSdrh */ 1418d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1419d24cc427Sdrh /* 1420d24cc427Sdrh ** Compute the soundex encoding of a word. 14212ba3cccfSdrh ** 14222ba3cccfSdrh ** IMP: R-59782-00072 The soundex(X) function returns a string that is the 14232ba3cccfSdrh ** soundex encoding of the string X. 1424d24cc427Sdrh */ 1425137c728fSdrh static void soundexFunc( 1426137c728fSdrh sqlite3_context *context, 1427137c728fSdrh int argc, 1428137c728fSdrh sqlite3_value **argv 1429137c728fSdrh ){ 1430d24cc427Sdrh char zResult[8]; 14314c755c0fSdrh const u8 *zIn; 1432d24cc427Sdrh int i, j; 1433d24cc427Sdrh static const unsigned char iCode[] = { 1434d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1435d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1436d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1437d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1438d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1439d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1440d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1441d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1442d24cc427Sdrh }; 1443d24cc427Sdrh assert( argc==1 ); 14444c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 1445bdf67e0eSdrh if( zIn==0 ) zIn = (u8*)""; 1446dc86e2b2Sdrh for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} 1447d24cc427Sdrh if( zIn[i] ){ 1448bdf67e0eSdrh u8 prevcode = iCode[zIn[i]&0x7f]; 144978ca0e7eSdanielk1977 zResult[0] = sqlite3Toupper(zIn[i]); 1450d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 1451d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 1452d24cc427Sdrh if( code>0 ){ 1453bdf67e0eSdrh if( code!=prevcode ){ 1454bdf67e0eSdrh prevcode = code; 1455d24cc427Sdrh zResult[j++] = code + '0'; 1456d24cc427Sdrh } 1457bdf67e0eSdrh }else{ 1458bdf67e0eSdrh prevcode = 0; 1459bdf67e0eSdrh } 1460d24cc427Sdrh } 1461d24cc427Sdrh while( j<4 ){ 1462d24cc427Sdrh zResult[j++] = '0'; 1463d24cc427Sdrh } 1464d24cc427Sdrh zResult[j] = 0; 1465d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 1466d24cc427Sdrh }else{ 14672ba3cccfSdrh /* IMP: R-64894-50321 The string "?000" is returned if the argument 14682ba3cccfSdrh ** is NULL or contains no ASCII alphabetic characters. */ 1469d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 1470d24cc427Sdrh } 1471d24cc427Sdrh } 14722ba3cccfSdrh #endif /* SQLITE_SOUNDEX */ 1473d24cc427Sdrh 1474fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 1475fdb83b2fSdrh /* 1476fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL. 1477fdb83b2fSdrh */ 1478fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 147965fd59f7Sdanielk1977 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 14807a521cfbSdrh const char *zProc; 1481fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1482fdb83b2fSdrh char *zErrMsg = 0; 1483fdb83b2fSdrh 1484191dd061Sdrh /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc 14851a55dedfSdrh ** flag is set. See the sqlite3_enable_load_extension() API. 14861a55dedfSdrh */ 1487f602a161Sdrh if( (db->flags & SQLITE_LoadExtFunc)==0 ){ 1488f602a161Sdrh sqlite3_result_error(context, "not authorized", -1); 1489f602a161Sdrh return; 1490f602a161Sdrh } 14911a55dedfSdrh 1492fdb83b2fSdrh if( argc==2 ){ 149365fd59f7Sdanielk1977 zProc = (const char *)sqlite3_value_text(argv[1]); 14947a521cfbSdrh }else{ 14957a521cfbSdrh zProc = 0; 1496fdb83b2fSdrh } 14977a521cfbSdrh if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 1498fdb83b2fSdrh sqlite3_result_error(context, zErrMsg, -1); 1499fdb83b2fSdrh sqlite3_free(zErrMsg); 1500fdb83b2fSdrh } 1501fdb83b2fSdrh } 1502fdb83b2fSdrh #endif 1503fdb83b2fSdrh 150401427a62Sdanielk1977 15050ac65892Sdrh /* 1506d3a149efSdrh ** An instance of the following structure holds the context of a 1507dd5baa95Sdrh ** sum() or avg() aggregate computation. 1508dd5baa95Sdrh */ 1509dd5baa95Sdrh typedef struct SumCtx SumCtx; 1510dd5baa95Sdrh struct SumCtx { 15118c08e861Sdrh double rSum; /* Floating point sum */ 15128c08e861Sdrh i64 iSum; /* Integer sum */ 1513cf85a51cSdrh i64 cnt; /* Number of elements summed */ 15148c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 15158c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 1516dd5baa95Sdrh }; 1517dd5baa95Sdrh 1518dd5baa95Sdrh /* 1519a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 1520a97fdd3bSdrh ** 1521a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 1522a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 1523a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 1524a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 1525c806d857Sdrh ** value. TOTAL never fails, but SUM might through an exception if 1526c806d857Sdrh ** it overflows an integer. 1527dd5baa95Sdrh */ 15280ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1529dd5baa95Sdrh SumCtx *p; 15303d1d95e6Sdrh int type; 15313f219f46Sdrh assert( argc==1 ); 1532f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 15334f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 153429d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 15353d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 1536739105c7Sdrh p->cnt++; 153729d72108Sdrh if( type==SQLITE_INTEGER ){ 15388c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 15398c08e861Sdrh p->rSum += v; 1540158b9cb9Sdrh if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ 1541a546ef21Sdrh p->approx = p->overflow = 1; 154229d72108Sdrh } 154329d72108Sdrh }else{ 15448c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 154529d72108Sdrh p->approx = 1; 15463f219f46Sdrh } 1547739105c7Sdrh } 1548dd5baa95Sdrh } 154967a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1550c3a20c19Sdan static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){ 1551c3a20c19Sdan SumCtx *p; 1552c3a20c19Sdan int type; 1553c3a20c19Sdan assert( argc==1 ); 1554c3a20c19Sdan UNUSED_PARAMETER(argc); 1555c3a20c19Sdan p = sqlite3_aggregate_context(context, sizeof(*p)); 1556c3a20c19Sdan type = sqlite3_value_numeric_type(argv[0]); 1557fd4b7285Sdrh /* p is always non-NULL because sumStep() will have been called first 1558fd4b7285Sdrh ** to initialize it */ 1559fd4b7285Sdrh if( ALWAYS(p) && type!=SQLITE_NULL ){ 1560a546ef21Sdrh assert( p->cnt>0 ); 1561c3a20c19Sdan p->cnt--; 1562a546ef21Sdrh assert( type==SQLITE_INTEGER || p->approx ); 1563a546ef21Sdrh if( type==SQLITE_INTEGER && p->approx==0 ){ 1564c3a20c19Sdan i64 v = sqlite3_value_int64(argv[0]); 1565c3a20c19Sdan p->rSum -= v; 1566a546ef21Sdrh p->iSum -= v; 1567c3a20c19Sdan }else{ 1568d736829eSdan p->rSum -= sqlite3_value_double(argv[0]); 1569c3a20c19Sdan } 1570c3a20c19Sdan } 1571c3a20c19Sdan } 157267a9b8edSdan #else 157367a9b8edSdan # define sumInverse 0 157467a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 15750ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 1576dd5baa95Sdrh SumCtx *p; 1577abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1578c2bd913aSdrh if( p && p->cnt>0 ){ 15798c08e861Sdrh if( p->overflow ){ 15808c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 15818c08e861Sdrh }else if( p->approx ){ 15828c08e861Sdrh sqlite3_result_double(context, p->rSum); 1583c2bd913aSdrh }else{ 15848c08e861Sdrh sqlite3_result_int64(context, p->iSum); 15853d1d95e6Sdrh } 1586dd5baa95Sdrh } 1587c2bd913aSdrh } 15880ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 1589dd5baa95Sdrh SumCtx *p; 1590abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1591739105c7Sdrh if( p && p->cnt>0 ){ 15928c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 1593dd5baa95Sdrh } 1594dd5baa95Sdrh } 1595a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 1596a97fdd3bSdrh SumCtx *p; 1597a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 1598fbd60f82Sshane /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 1599fbd60f82Sshane sqlite3_result_double(context, p ? p->rSum : (double)0); 1600a97fdd3bSdrh } 1601dd5baa95Sdrh 1602dd5baa95Sdrh /* 16030bce8354Sdrh ** The following structure keeps track of state information for the 16040bce8354Sdrh ** count() aggregate function. 16050bce8354Sdrh */ 16060bce8354Sdrh typedef struct CountCtx CountCtx; 16070bce8354Sdrh struct CountCtx { 1608fc6ad39cSdrh i64 n; 16097262ca94Sdan #ifdef SQLITE_DEBUG 16107262ca94Sdan int bInverse; /* True if xInverse() ever called */ 16117262ca94Sdan #endif 16120bce8354Sdrh }; 1613dd5baa95Sdrh 16140bce8354Sdrh /* 16150bce8354Sdrh ** Routines to implement the count() aggregate function. 16160bce8354Sdrh */ 16170ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 16180bce8354Sdrh CountCtx *p; 16194f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 16209c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 16210bce8354Sdrh p->n++; 16220bce8354Sdrh } 16232e79c3d5Sdrh 1624d3264c7cSdrh #ifndef SQLITE_OMIT_DEPRECATED 16252e79c3d5Sdrh /* The sqlite3_aggregate_count() function is deprecated. But just to make 16262e79c3d5Sdrh ** sure it still operates correctly, verify that its count agrees with our 16272e79c3d5Sdrh ** internal count when using count(*) and when the total count can be 16282e79c3d5Sdrh ** expressed as a 32-bit integer. */ 16297262ca94Sdan assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse 16302e79c3d5Sdrh || p->n==sqlite3_aggregate_count(context) ); 1631d3264c7cSdrh #endif 16320bce8354Sdrh } 16330ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 16340bce8354Sdrh CountCtx *p; 1635abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1636fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 16370bce8354Sdrh } 16387262ca94Sdan #ifndef SQLITE_OMIT_WINDOWFUNC 16397262ca94Sdan static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){ 16407262ca94Sdan CountCtx *p; 16417262ca94Sdan p = sqlite3_aggregate_context(ctx, sizeof(*p)); 1642fd4b7285Sdrh /* p is always non-NULL since countStep() will have been called first */ 1643fd4b7285Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){ 16447262ca94Sdan p->n--; 16457262ca94Sdan #ifdef SQLITE_DEBUG 16467262ca94Sdan p->bInverse = 1; 16477262ca94Sdan #endif 16487262ca94Sdan } 16497262ca94Sdan } 16506b4b8820Sdan #else 16516b4b8820Sdan # define countInverse 0 16526b4b8820Sdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 16530bce8354Sdrh 16540bce8354Sdrh /* 16550bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 16560bce8354Sdrh */ 165762c14b34Sdanielk1977 static void minmaxStep( 165862c14b34Sdanielk1977 sqlite3_context *context, 165962c14b34Sdanielk1977 int NotUsed, 166062c14b34Sdanielk1977 sqlite3_value **argv 166162c14b34Sdanielk1977 ){ 166288208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 16639eb516c0Sdrh Mem *pBest; 166462c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 16659eb516c0Sdrh 16669eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 16673aeab9e4Sdanielk1977 if( !pBest ) return; 1668268380caSdrh 16696fb2b54cSdan if( sqlite3_value_type(pArg)==SQLITE_NULL ){ 167094a6d998Sdrh if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); 167194a6d998Sdrh }else if( pBest->flags ){ 16729eb516c0Sdrh int max; 16739eb516c0Sdrh int cmp; 1674dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 16757e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 16767e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 16777e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 16787e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 16797e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 16807e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 16817e18c259Sdanielk1977 ** aggregate, or 0 for min(). 16827e18c259Sdanielk1977 */ 1683309b3386Sdrh max = sqlite3_user_data(context)!=0; 1684dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 168588208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 1686b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 16877a95789cSdrh }else{ 16887a95789cSdrh sqlite3SkipAccumulatorLoad(context); 168988208050Sdanielk1977 } 16900bce8354Sdrh }else{ 1691035e563bSdrh pBest->db = sqlite3_context_db_handle(context); 1692b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 16930bce8354Sdrh } 16940bce8354Sdrh } 16956fb2b54cSdan static void minMaxValueFinalize(sqlite3_context *context, int bValue){ 169688208050Sdanielk1977 sqlite3_value *pRes; 1697abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1698abfcea25Sdrh if( pRes ){ 169994a6d998Sdrh if( pRes->flags ){ 1700f4479501Sdrh sqlite3_result_value(context, pRes); 17010bce8354Sdrh } 17026fb2b54cSdan if( bValue==0 ) sqlite3VdbeMemRelease(pRes); 17030bce8354Sdrh } 1704abfcea25Sdrh } 170567a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 17066fb2b54cSdan static void minMaxValue(sqlite3_context *context){ 1707c7bf5716Sdrh minMaxValueFinalize(context, 1); 17086fb2b54cSdan } 170967a9b8edSdan #else 171067a9b8edSdan # define minMaxValue 0 171167a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 17126fb2b54cSdan static void minMaxFinalize(sqlite3_context *context){ 1713c7bf5716Sdrh minMaxValueFinalize(context, 0); 17146fb2b54cSdan } 1715dd5baa95Sdrh 1716b0689696Sdrh /* 1717b0689696Sdrh ** group_concat(EXPR, ?SEPARATOR?) 1718f06db3e8Sdrh ** 1719f06db3e8Sdrh ** The SEPARATOR goes before the EXPR string. This is tragic. The 1720f06db3e8Sdrh ** groupConcatInverse() implementation would have been easier if the 1721f06db3e8Sdrh ** SEPARATOR were appended after EXPR. And the order is undocumented, 1722f06db3e8Sdrh ** so we could change it, in theory. But the old behavior has been 1723f06db3e8Sdrh ** around for so long that we dare not, for fear of breaking something. 1724b0689696Sdrh */ 1725dde13e6fSlarrybr typedef struct { 1726dde13e6fSlarrybr StrAccum str; /* The accumulated concatenation */ 1727dde13e6fSlarrybr #ifndef SQLITE_OMIT_WINDOWFUNC 1728dde13e6fSlarrybr int nAccum; /* Number of strings presently concatenated */ 1729dde13e6fSlarrybr int nFirstSepLength; /* Used to detect separator length change */ 1730dde13e6fSlarrybr /* If pnSepLengths!=0, refs an array of inter-string separator lengths, 1731f06db3e8Sdrh ** stored as actually incorporated into presently accumulated result. 1732f06db3e8Sdrh ** (Hence, its slots in use number nAccum-1 between method calls.) 1733f06db3e8Sdrh ** If pnSepLengths==0, nFirstSepLength is the length used throughout. 1734dde13e6fSlarrybr */ 1735dde13e6fSlarrybr int *pnSepLengths; 1736dde13e6fSlarrybr #endif 1737dde13e6fSlarrybr } GroupConcatCtx; 1738dde13e6fSlarrybr 1739b0689696Sdrh static void groupConcatStep( 1740b0689696Sdrh sqlite3_context *context, 1741b0689696Sdrh int argc, 1742b0689696Sdrh sqlite3_value **argv 1743b0689696Sdrh ){ 1744b0689696Sdrh const char *zVal; 1745dde13e6fSlarrybr GroupConcatCtx *pGCC; 1746b0689696Sdrh const char *zSep; 174707d3117aSdrh int nVal, nSep; 174807d3117aSdrh assert( argc==1 || argc==2 ); 174907d3117aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1750dde13e6fSlarrybr pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC)); 1751dde13e6fSlarrybr if( pGCC ){ 1752bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1753dde13e6fSlarrybr int firstTerm = pGCC->str.mxAlloc==0; 1754dde13e6fSlarrybr pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 1755f06db3e8Sdrh if( argc==1 ){ 17568bfd7190Sdrh if( !firstTerm ){ 1757f06db3e8Sdrh sqlite3_str_appendchar(&pGCC->str, 1, ','); 1758f06db3e8Sdrh } 1759f06db3e8Sdrh #ifndef SQLITE_OMIT_WINDOWFUNC 1760f06db3e8Sdrh else{ 1761f06db3e8Sdrh pGCC->nFirstSepLength = 1; 1762f06db3e8Sdrh } 1763f06db3e8Sdrh #endif 1764f06db3e8Sdrh }else if( !firstTerm ){ 176507d3117aSdrh zSep = (char*)sqlite3_value_text(argv[1]); 176607d3117aSdrh nSep = sqlite3_value_bytes(argv[1]); 1767f06db3e8Sdrh if( zSep ){ 1768dde13e6fSlarrybr sqlite3_str_append(&pGCC->str, zSep, nSep); 1769f06db3e8Sdrh } 1770dde13e6fSlarrybr #ifndef SQLITE_OMIT_WINDOWFUNC 1771f06db3e8Sdrh else{ 1772dde13e6fSlarrybr nSep = 0; 1773f06db3e8Sdrh } 1774dde13e6fSlarrybr if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){ 1775dde13e6fSlarrybr int *pnsl = pGCC->pnSepLengths; 1776dde13e6fSlarrybr if( pnsl == 0 ){ 1777dde13e6fSlarrybr /* First separator length variation seen, start tracking them. */ 1778dde13e6fSlarrybr pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int)); 1779dde13e6fSlarrybr if( pnsl!=0 ){ 1780dde13e6fSlarrybr int i = 0, nA = pGCC->nAccum-1; 1781dde13e6fSlarrybr while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength; 1782b0689696Sdrh } 1783b0689696Sdrh }else{ 1784dde13e6fSlarrybr pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int)); 178507d3117aSdrh } 1786dde13e6fSlarrybr if( pnsl!=0 ){ 17873dd01111Sdrh if( ALWAYS(pGCC->nAccum>0) ){ 1788dde13e6fSlarrybr pnsl[pGCC->nAccum-1] = nSep; 1789b0689696Sdrh } 1790dde13e6fSlarrybr pGCC->pnSepLengths = pnsl; 1791dde13e6fSlarrybr }else{ 1792f06db3e8Sdrh sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM); 1793dde13e6fSlarrybr } 1794dde13e6fSlarrybr } 1795dde13e6fSlarrybr #endif 1796dde13e6fSlarrybr } 1797dde13e6fSlarrybr #ifndef SQLITE_OMIT_WINDOWFUNC 1798dde13e6fSlarrybr else{ 17993dd01111Sdrh pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]); 1800dde13e6fSlarrybr } 1801dde13e6fSlarrybr pGCC->nAccum += 1; 1802dde13e6fSlarrybr #endif 180307d3117aSdrh zVal = (char*)sqlite3_value_text(argv[0]); 180407d3117aSdrh nVal = sqlite3_value_bytes(argv[0]); 1805dde13e6fSlarrybr if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal); 1806b0689696Sdrh } 1807b0689696Sdrh } 1808dde13e6fSlarrybr 180967a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 181003854d2eSdan static void groupConcatInverse( 181103854d2eSdan sqlite3_context *context, 181203854d2eSdan int argc, 181303854d2eSdan sqlite3_value **argv 181403854d2eSdan ){ 1815dde13e6fSlarrybr GroupConcatCtx *pGCC; 1816c7bf5716Sdrh assert( argc==1 || argc==2 ); 1817260ff081Sdrh (void)argc; /* Suppress unused parameter warning */ 181803854d2eSdan if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1819dde13e6fSlarrybr pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC)); 1820dde13e6fSlarrybr /* pGCC is always non-NULL since groupConcatStep() will have always 1821fd4b7285Sdrh ** run frist to initialize it */ 1822dde13e6fSlarrybr if( ALWAYS(pGCC) ){ 1823dde13e6fSlarrybr int nVS = sqlite3_value_bytes(argv[0]); 1824dde13e6fSlarrybr pGCC->nAccum -= 1; 1825dde13e6fSlarrybr if( pGCC->pnSepLengths!=0 ){ 1826dde13e6fSlarrybr assert(pGCC->nAccum >= 0); 1827dde13e6fSlarrybr if( pGCC->nAccum>0 ){ 1828dde13e6fSlarrybr nVS += *pGCC->pnSepLengths; 1829dde13e6fSlarrybr memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1, 1830dde13e6fSlarrybr (pGCC->nAccum-1)*sizeof(int)); 183103854d2eSdan } 183203854d2eSdan }else{ 1833dde13e6fSlarrybr /* If removing single accumulated string, harmlessly over-do. */ 1834dde13e6fSlarrybr nVS += pGCC->nFirstSepLength; 183503854d2eSdan } 1836dde13e6fSlarrybr if( nVS>=(int)pGCC->str.nChar ){ 1837dde13e6fSlarrybr pGCC->str.nChar = 0; 1838dde13e6fSlarrybr }else{ 1839dde13e6fSlarrybr pGCC->str.nChar -= nVS; 1840dde13e6fSlarrybr memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar); 1841dde13e6fSlarrybr } 1842dde13e6fSlarrybr if( pGCC->str.nChar==0 ){ 1843dde13e6fSlarrybr pGCC->str.mxAlloc = 0; 1844dde13e6fSlarrybr sqlite3_free(pGCC->pnSepLengths); 1845dde13e6fSlarrybr pGCC->pnSepLengths = 0; 1846dde13e6fSlarrybr } 184703854d2eSdan } 184803854d2eSdan } 184967a9b8edSdan #else 185067a9b8edSdan # define groupConcatInverse 0 185167a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 1852b0689696Sdrh static void groupConcatFinalize(sqlite3_context *context){ 1853dde13e6fSlarrybr GroupConcatCtx *pGCC 1854dde13e6fSlarrybr = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0); 1855dde13e6fSlarrybr if( pGCC ){ 18565bf4715eSdrh sqlite3ResultStrAccum(context, &pGCC->str); 1857dde13e6fSlarrybr #ifndef SQLITE_OMIT_WINDOWFUNC 1858dde13e6fSlarrybr sqlite3_free(pGCC->pnSepLengths); 1859dde13e6fSlarrybr #endif 1860b0689696Sdrh } 1861ade86483Sdrh } 186267a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1863e2f781b9Sdan static void groupConcatValue(sqlite3_context *context){ 1864dde13e6fSlarrybr GroupConcatCtx *pGCC 1865dde13e6fSlarrybr = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0); 1866dde13e6fSlarrybr if( pGCC ){ 1867dde13e6fSlarrybr StrAccum *pAccum = &pGCC->str; 1868e2f781b9Sdan if( pAccum->accError==SQLITE_TOOBIG ){ 1869e2f781b9Sdan sqlite3_result_error_toobig(context); 1870e2f781b9Sdan }else if( pAccum->accError==SQLITE_NOMEM ){ 1871e2f781b9Sdan sqlite3_result_error_nomem(context); 1872e2f781b9Sdan }else{ 1873e2f781b9Sdan const char *zText = sqlite3_str_value(pAccum); 1874f06db3e8Sdrh sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT); 1875e2f781b9Sdan } 1876e2f781b9Sdan } 1877e2f781b9Sdan } 187867a9b8edSdan #else 187967a9b8edSdan # define groupConcatValue 0 188067a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 18814e5ffc5fSdrh 1882d3a149efSdrh /* 1883a4741840Sdrh ** This routine does per-connection function registration. Most 1884a4741840Sdrh ** of the built-in functions above are part of the global function set. 1885a4741840Sdrh ** This routine only deals with those that are not global. 1886dc04c583Sdrh */ 188780738d9cSdrh void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ 1888832a58a6Sdanielk1977 int rc = sqlite3_overload_function(db, "MATCH", 2); 1889832a58a6Sdanielk1977 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 1890832a58a6Sdanielk1977 if( rc==SQLITE_NOMEM ){ 18914a642b60Sdrh sqlite3OomFault(db); 1892832a58a6Sdanielk1977 } 1893832a58a6Sdanielk1977 } 189455ef4d97Sdrh 189555ef4d97Sdrh /* 189608652b5eSdrh ** Re-register the built-in LIKE functions. The caseSensitive 189755ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 189808652b5eSdrh ** sensitive. 189955ef4d97Sdrh */ 190055ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 190155ef4d97Sdrh struct compareInfo *pInfo; 1902ea5c040fSdrh int flags; 190355ef4d97Sdrh if( caseSensitive ){ 190455ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 1905ea5c040fSdrh flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; 190655ef4d97Sdrh }else{ 190755ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 1908ea5c040fSdrh flags = SQLITE_FUNC_LIKE; 190955ef4d97Sdrh } 1910660af939Sdan sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1911660af939Sdan sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1912ea5c040fSdrh sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags; 1913ea5c040fSdrh sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags; 191455ef4d97Sdrh } 191555ef4d97Sdrh 191655ef4d97Sdrh /* 191755ef4d97Sdrh ** pExpr points to an expression which implements a function. If 191855ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 19191d42ea71Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and the 19201d42ea71Sdrh ** escape character and then return TRUE. If the function is not a 19211d42ea71Sdrh ** LIKE-style function then return FALSE. 19221d42ea71Sdrh ** 19231d42ea71Sdrh ** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE 19241d42ea71Sdrh ** operator if c is a string literal that is exactly one byte in length. 19251d42ea71Sdrh ** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is 19261d42ea71Sdrh ** no ESCAPE clause. 192716897072Sdrh ** 192816897072Sdrh ** *pIsNocase is set to true if uppercase and lowercase are equivalent for 192916897072Sdrh ** the function (default for LIKE). If the function makes the distinction 193016897072Sdrh ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to 193116897072Sdrh ** false. 193255ef4d97Sdrh */ 1933d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 193455ef4d97Sdrh FuncDef *pDef; 19351d42ea71Sdrh int nExpr; 193617988aaeSdrh assert( pExpr!=0 ); 193717988aaeSdrh assert( pExpr->op==TK_FUNCTION ); 1938*a4eeccdfSdrh assert( ExprUseXList(pExpr) ); 193917988aaeSdrh if( !pExpr->x.pList ){ 194055ef4d97Sdrh return 0; 194155ef4d97Sdrh } 19421d42ea71Sdrh nExpr = pExpr->x.pList->nExpr; 1943f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 19441d42ea71Sdrh pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0); 194578b52203Sdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 194678b52203Sdrh if( pDef==0 ) return 0; 194778b52203Sdrh #endif 1948d36e1041Sdrh if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ 194955ef4d97Sdrh return 0; 195055ef4d97Sdrh } 195155ef4d97Sdrh 195255ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 195355ef4d97Sdrh ** the first three statements in the compareInfo structure. The 195455ef4d97Sdrh ** asserts() that follow verify that assumption 195555ef4d97Sdrh */ 195655ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 195755ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 195855ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 195955ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1960589c7876Sdrh 1961589c7876Sdrh if( nExpr<3 ){ 1962589c7876Sdrh aWc[3] = 0; 1963589c7876Sdrh }else{ 1964589c7876Sdrh Expr *pEscape = pExpr->x.pList->a[2].pExpr; 1965589c7876Sdrh char *zEscape; 1966589c7876Sdrh if( pEscape->op!=TK_STRING ) return 0; 1967f9751074Sdrh assert( !ExprHasProperty(pEscape, EP_IntValue) ); 1968589c7876Sdrh zEscape = pEscape->u.zToken; 1969589c7876Sdrh if( zEscape[0]==0 || zEscape[1]!=0 ) return 0; 1970589c7876Sdrh if( zEscape[0]==aWc[0] ) return 0; 1971589c7876Sdrh if( zEscape[0]==aWc[1] ) return 0; 1972589c7876Sdrh aWc[3] = zEscape[0]; 1973589c7876Sdrh } 1974589c7876Sdrh 1975d36e1041Sdrh *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; 197655ef4d97Sdrh return 1; 1977dc04c583Sdrh } 19788c0a791aSdanielk1977 197963f8f98aSdrh /* Mathematical Constants */ 198063f8f98aSdrh #ifndef M_PI 198163f8f98aSdrh # define M_PI 3.141592653589793238462643383279502884 198263f8f98aSdrh #endif 198363f8f98aSdrh #ifndef M_LN10 198463f8f98aSdrh # define M_LN10 2.302585092994045684017991454684364208 198563f8f98aSdrh #endif 198663f8f98aSdrh #ifndef M_LN2 198763f8f98aSdrh # define M_LN2 0.693147180559945309417232121458176568 198863f8f98aSdrh #endif 198963f8f98aSdrh 199063f8f98aSdrh 1991f6e904bdSdrh /* Extra math functions that require linking with -lm 1992f6e904bdSdrh */ 1993f6e904bdSdrh #ifdef SQLITE_ENABLE_MATH_FUNCTIONS 1994f6e904bdSdrh /* 1995f6e904bdSdrh ** Implementation SQL functions: 1996f6e904bdSdrh ** 1997f6e904bdSdrh ** ceil(X) 1998f6e904bdSdrh ** ceiling(X) 1999f6e904bdSdrh ** floor(X) 2000f6e904bdSdrh ** 2001f6e904bdSdrh ** The sqlite3_user_data() pointer is a pointer to the libm implementation 2002f6e904bdSdrh ** of the underlying C function. 2003f6e904bdSdrh */ 2004f6e904bdSdrh static void ceilingFunc( 2005f6e904bdSdrh sqlite3_context *context, 2006f6e904bdSdrh int argc, 2007f6e904bdSdrh sqlite3_value **argv 2008f6e904bdSdrh ){ 2009f6e904bdSdrh assert( argc==1 ); 201063f8f98aSdrh switch( sqlite3_value_numeric_type(argv[0]) ){ 201163f8f98aSdrh case SQLITE_INTEGER: { 2012f6e904bdSdrh sqlite3_result_int64(context, sqlite3_value_int64(argv[0])); 2013f6e904bdSdrh break; 201463f8f98aSdrh } 201563f8f98aSdrh case SQLITE_FLOAT: { 2016f6e904bdSdrh double (*x)(double) = (double(*)(double))sqlite3_user_data(context); 2017f6e904bdSdrh sqlite3_result_double(context, x(sqlite3_value_double(argv[0]))); 2018f6e904bdSdrh break; 2019f6e904bdSdrh } 202063f8f98aSdrh default: { 202163f8f98aSdrh break; 202263f8f98aSdrh } 2023f6e904bdSdrh } 2024f6e904bdSdrh } 2025f6e904bdSdrh 2026f6e904bdSdrh /* 20274fd4a7a1Sdrh ** On some systems, ceil() and floor() are intrinsic function. You are 2028c2dbf35fSdrh ** unable to take a pointer to these functions. Hence, we here wrap them 20294fd4a7a1Sdrh ** in our own actual functions. 20304fd4a7a1Sdrh */ 20314fd4a7a1Sdrh static double xCeil(double x){ return ceil(x); } 20324fd4a7a1Sdrh static double xFloor(double x){ return floor(x); } 20334fd4a7a1Sdrh 20344fd4a7a1Sdrh /* 2035f6e904bdSdrh ** Implementation of SQL functions: 2036f6e904bdSdrh ** 2037f6e904bdSdrh ** ln(X) - natural logarithm 203863f8f98aSdrh ** log(X) - log X base 10 203963f8f98aSdrh ** log10(X) - log X base 10 204063f8f98aSdrh ** log(B,X) - log X base B 2041f6e904bdSdrh */ 2042f6e904bdSdrh static void logFunc( 2043f6e904bdSdrh sqlite3_context *context, 2044f6e904bdSdrh int argc, 2045f6e904bdSdrh sqlite3_value **argv 2046f6e904bdSdrh ){ 204763f8f98aSdrh double x, b, ans; 2048f6e904bdSdrh assert( argc==1 || argc==2 ); 204963f8f98aSdrh switch( sqlite3_value_numeric_type(argv[0]) ){ 205063f8f98aSdrh case SQLITE_INTEGER: 205163f8f98aSdrh case SQLITE_FLOAT: 205263f8f98aSdrh x = sqlite3_value_double(argv[0]); 205302d6f9b2Sdrh if( x<=0.0 ) return; 205463f8f98aSdrh break; 205563f8f98aSdrh default: 205663f8f98aSdrh return; 2057f6e904bdSdrh } 2058f6e904bdSdrh if( argc==2 ){ 205963f8f98aSdrh switch( sqlite3_value_numeric_type(argv[0]) ){ 206063f8f98aSdrh case SQLITE_INTEGER: 206163f8f98aSdrh case SQLITE_FLOAT: 206202d6f9b2Sdrh b = log(x); 206302d6f9b2Sdrh if( b<=0.0 ) return; 206463f8f98aSdrh x = sqlite3_value_double(argv[1]); 206502d6f9b2Sdrh if( x<=0.0 ) return; 206663f8f98aSdrh break; 206763f8f98aSdrh default: 206863f8f98aSdrh return; 2069f6e904bdSdrh } 207002d6f9b2Sdrh ans = log(x)/b; 207163f8f98aSdrh }else{ 207263f8f98aSdrh ans = log(x); 207363f8f98aSdrh switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){ 207463f8f98aSdrh case 1: 2075f6e904bdSdrh /* Convert from natural logarithm to log base 10 */ 207663f8f98aSdrh ans *= 1.0/M_LN10; 207763f8f98aSdrh break; 207863f8f98aSdrh case 2: 207963f8f98aSdrh /* Convert from natural logarithm to log base 2 */ 208063f8f98aSdrh ans *= 1.0/M_LN2; 208163f8f98aSdrh break; 208263f8f98aSdrh default: 208363f8f98aSdrh break; 208463f8f98aSdrh } 2085f6e904bdSdrh } 2086f6e904bdSdrh sqlite3_result_double(context, ans); 2087f6e904bdSdrh } 208863f8f98aSdrh 208963f8f98aSdrh /* 209063f8f98aSdrh ** Functions to converts degrees to radians and radians to degrees. 209163f8f98aSdrh */ 209263f8f98aSdrh static double degToRad(double x){ return x*(M_PI/180.0); } 209363f8f98aSdrh static double radToDeg(double x){ return x*(180.0/M_PI); } 209463f8f98aSdrh 209563f8f98aSdrh /* 209663f8f98aSdrh ** Implementation of 1-argument SQL math functions: 209763f8f98aSdrh ** 209863f8f98aSdrh ** exp(X) - Compute e to the X-th power 209963f8f98aSdrh */ 210063f8f98aSdrh static void math1Func( 210163f8f98aSdrh sqlite3_context *context, 210263f8f98aSdrh int argc, 210363f8f98aSdrh sqlite3_value **argv 210463f8f98aSdrh ){ 210563f8f98aSdrh int type0; 210663f8f98aSdrh double v0, ans; 210763f8f98aSdrh double (*x)(double); 2108d97a4c00Smistachkin assert( argc==1 ); 210963f8f98aSdrh type0 = sqlite3_value_numeric_type(argv[0]); 211063f8f98aSdrh if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; 211163f8f98aSdrh v0 = sqlite3_value_double(argv[0]); 211263f8f98aSdrh x = (double(*)(double))sqlite3_user_data(context); 211363f8f98aSdrh ans = x(v0); 211463f8f98aSdrh sqlite3_result_double(context, ans); 211563f8f98aSdrh } 211663f8f98aSdrh 211763f8f98aSdrh /* 211863f8f98aSdrh ** Implementation of 2-argument SQL math functions: 211963f8f98aSdrh ** 212063f8f98aSdrh ** power(X,Y) - Compute X to the Y-th power 212163f8f98aSdrh */ 212263f8f98aSdrh static void math2Func( 212363f8f98aSdrh sqlite3_context *context, 212463f8f98aSdrh int argc, 212563f8f98aSdrh sqlite3_value **argv 212663f8f98aSdrh ){ 212763f8f98aSdrh int type0, type1; 212863f8f98aSdrh double v0, v1, ans; 212963f8f98aSdrh double (*x)(double,double); 2130d97a4c00Smistachkin assert( argc==2 ); 213163f8f98aSdrh type0 = sqlite3_value_numeric_type(argv[0]); 213263f8f98aSdrh if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; 213363f8f98aSdrh type1 = sqlite3_value_numeric_type(argv[1]); 213463f8f98aSdrh if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return; 213563f8f98aSdrh v0 = sqlite3_value_double(argv[0]); 213663f8f98aSdrh v1 = sqlite3_value_double(argv[1]); 213763f8f98aSdrh x = (double(*)(double,double))sqlite3_user_data(context); 213863f8f98aSdrh ans = x(v0, v1); 213963f8f98aSdrh sqlite3_result_double(context, ans); 214063f8f98aSdrh } 214163f8f98aSdrh 214263f8f98aSdrh /* 214381e5a9a6Sdrh ** Implementation of 0-argument pi() function. 214463f8f98aSdrh */ 214563f8f98aSdrh static void piFunc( 214663f8f98aSdrh sqlite3_context *context, 214763f8f98aSdrh int argc, 214863f8f98aSdrh sqlite3_value **argv 214963f8f98aSdrh ){ 215063f8f98aSdrh assert( argc==0 ); 215163f8f98aSdrh sqlite3_result_double(context, M_PI); 215263f8f98aSdrh } 215363f8f98aSdrh 2154f6e904bdSdrh #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ 2155f6e904bdSdrh 215693ce741bSdanielk1977 /* 215763f8f98aSdrh ** Implementation of sign(X) function. 215863f8f98aSdrh */ 215963f8f98aSdrh static void signFunc( 216063f8f98aSdrh sqlite3_context *context, 216163f8f98aSdrh int argc, 216263f8f98aSdrh sqlite3_value **argv 216363f8f98aSdrh ){ 216463f8f98aSdrh int type0; 216563f8f98aSdrh double x; 2166e5baf5c2Sdrh UNUSED_PARAMETER(argc); 2167d97a4c00Smistachkin assert( argc==1 ); 216863f8f98aSdrh type0 = sqlite3_value_numeric_type(argv[0]); 216963f8f98aSdrh if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; 217063f8f98aSdrh x = sqlite3_value_double(argv[0]); 217163f8f98aSdrh sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); 217263f8f98aSdrh } 217363f8f98aSdrh 217463f8f98aSdrh /* 217560ec914cSpeter.d.reid ** All of the FuncDef structures in the aBuiltinFunc[] array above 217693ce741bSdanielk1977 ** to the global function hash table. This occurs at start-time (as 217793ce741bSdanielk1977 ** a consequence of calling sqlite3_initialize()). 217893ce741bSdanielk1977 ** 217993ce741bSdanielk1977 ** After this routine runs 218093ce741bSdanielk1977 */ 218180738d9cSdrh void sqlite3RegisterBuiltinFunctions(void){ 21828c0a791aSdanielk1977 /* 2183777c5386Sdrh ** The following array holds FuncDef structures for all of the functions 2184777c5386Sdrh ** defined in this file. 21858c0a791aSdanielk1977 ** 2186777c5386Sdrh ** The array cannot be constant since changes are made to the 2187777c5386Sdrh ** FuncDef.pHash elements at start-time. The elements of this array 2188777c5386Sdrh ** are read-only after initialization is complete. 218980738d9cSdrh ** 219080738d9cSdrh ** For peak efficiency, put the most frequently used function last. 21918c0a791aSdanielk1977 */ 219280738d9cSdrh static FuncDef aBuiltinFunc[] = { 2193171c50ecSdrh /***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/ 21943780f9a4Sdrh #if !defined(SQLITE_UNTESTABLE) 2195171c50ecSdrh TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0), 2196171c50ecSdrh TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0), 2197171c50ecSdrh TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0), 2198171c50ecSdrh TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0), 21993780f9a4Sdrh #endif /* !defined(SQLITE_UNTESTABLE) */ 2200171c50ecSdrh /***** Regular functions *****/ 220180738d9cSdrh #ifdef SQLITE_SOUNDEX 220280738d9cSdrh FUNCTION(soundex, 1, 0, 0, soundexFunc ), 220380738d9cSdrh #endif 220480738d9cSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 220564de2a5fSdrh SFUNCTION(load_extension, 1, 0, 0, loadExt ), 220664de2a5fSdrh SFUNCTION(load_extension, 2, 0, 0, loadExt ), 220780738d9cSdrh #endif 220880738d9cSdrh #if SQLITE_USER_AUTHENTICATION 220980738d9cSdrh FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), 221080738d9cSdrh #endif 221180738d9cSdrh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 221280738d9cSdrh DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), 221380738d9cSdrh DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), 221480738d9cSdrh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 221525c4296bSdrh INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 221625c4296bSdrh INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 221725c4296bSdrh INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 2218092457b1Sdrh #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 221935100fb1Sdrh FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET| 22202fc865c1Sdrh SQLITE_FUNC_TYPEOF), 2221092457b1Sdrh #endif 22228c0a791aSdanielk1977 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 22238c0a791aSdanielk1977 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 22248c0a791aSdanielk1977 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 22258c0a791aSdanielk1977 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 22268c0a791aSdanielk1977 FUNCTION(trim, 1, 3, 0, trimFunc ), 22278c0a791aSdanielk1977 FUNCTION(trim, 2, 3, 0, trimFunc ), 22288c0a791aSdanielk1977 FUNCTION(min, -1, 0, 1, minmaxFunc ), 22298c0a791aSdanielk1977 FUNCTION(min, 0, 0, 1, 0 ), 22306fb2b54cSdan WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 2231bb301231Sdrh SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ), 22328c0a791aSdanielk1977 FUNCTION(max, -1, 1, 1, minmaxFunc ), 22338c0a791aSdanielk1977 FUNCTION(max, 0, 1, 1, 0 ), 22346fb2b54cSdan WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 2235bb301231Sdrh SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ), 2236a748fdccSdrh FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 2237a748fdccSdrh FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 2238d55e0729Sdrh FUNCTION(instr, 2, 0, 0, instrFunc ), 2239a5c1416dSdrh FUNCTION(printf, -1, 0, 0, printfFunc ), 2240d495d8c9Sdrh FUNCTION(unicode, 1, 0, 0, unicodeFunc ), 2241d495d8c9Sdrh FUNCTION(char, -1, 0, 0, charFunc ), 22428c0a791aSdanielk1977 FUNCTION(abs, 1, 0, 0, absFunc ), 2243fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 22448c0a791aSdanielk1977 FUNCTION(round, 1, 0, 0, roundFunc ), 22458c0a791aSdanielk1977 FUNCTION(round, 2, 0, 0, roundFunc ), 2246fbd60f82Sshane #endif 22478c0a791aSdanielk1977 FUNCTION(upper, 1, 0, 0, upperFunc ), 22488c0a791aSdanielk1977 FUNCTION(lower, 1, 0, 0, lowerFunc ), 22498c0a791aSdanielk1977 FUNCTION(hex, 1, 0, 0, hexFunc ), 2250ffe421c7Sdrh INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ), 2251b1fba286Sdrh VFUNCTION(random, 0, 0, 0, randomFunc ), 2252b1fba286Sdrh VFUNCTION(randomblob, 1, 0, 0, randomBlob ), 22538c0a791aSdanielk1977 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 225403bf26d9Sdrh DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 225503bf26d9Sdrh DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), 2256840561f2Sdrh FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), 22578c0a791aSdanielk1977 FUNCTION(quote, 1, 0, 0, quoteFunc ), 2258b1fba286Sdrh VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 2259b1fba286Sdrh VFUNCTION(changes, 0, 0, 0, changes ), 2260b1fba286Sdrh VFUNCTION(total_changes, 0, 0, 0, total_changes ), 22618c0a791aSdanielk1977 FUNCTION(replace, 3, 0, 0, replaceFunc ), 22628c0a791aSdanielk1977 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 226380738d9cSdrh FUNCTION(substr, 2, 0, 0, substrFunc ), 226480738d9cSdrh FUNCTION(substr, 3, 0, 0, substrFunc ), 22651335ec7dSdrh FUNCTION(substring, 2, 0, 0, substrFunc ), 22661335ec7dSdrh FUNCTION(substring, 3, 0, 0, substrFunc ), 22676fb2b54cSdan WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0), 22686fb2b54cSdan WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0), 22696fb2b54cSdan WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0), 22707262ca94Sdan WAGGREGATE(count, 0,0,0, countStep, 2271bb301231Sdrh countFinalize, countFinalize, countInverse, 2272bb301231Sdrh SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ), 22737262ca94Sdan WAGGREGATE(count, 1,0,0, countStep, 2274bb301231Sdrh countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ), 227503854d2eSdan WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, 22766fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 227703854d2eSdan WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, 22786fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 22798c0a791aSdanielk1977 22808c0a791aSdanielk1977 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 22818c0a791aSdanielk1977 #ifdef SQLITE_CASE_SENSITIVE_LIKE 22828c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 22838c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 22848c0a791aSdanielk1977 #else 22858c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 22868c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 22878c0a791aSdanielk1977 #endif 2288cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 2289cc15313cSdrh FUNCTION(unknown, -1, 0, 0, unknownFunc ), 2290cc15313cSdrh #endif 229180738d9cSdrh FUNCTION(coalesce, 1, 0, 0, 0 ), 229280738d9cSdrh FUNCTION(coalesce, 0, 0, 0, 0 ), 2293f6e904bdSdrh #ifdef SQLITE_ENABLE_MATH_FUNCTIONS 22944fd4a7a1Sdrh MFUNCTION(ceil, 1, xCeil, ceilingFunc ), 22954fd4a7a1Sdrh MFUNCTION(ceiling, 1, xCeil, ceilingFunc ), 22964fd4a7a1Sdrh MFUNCTION(floor, 1, xFloor, ceilingFunc ), 2297d97a4c00Smistachkin #if SQLITE_HAVE_C99_MATH_FUNCS 229863f8f98aSdrh MFUNCTION(trunc, 1, trunc, ceilingFunc ), 2299d97a4c00Smistachkin #endif 2300f6e904bdSdrh FUNCTION(ln, 1, 0, 0, logFunc ), 2301f6e904bdSdrh FUNCTION(log, 1, 1, 0, logFunc ), 2302f6e904bdSdrh FUNCTION(log10, 1, 1, 0, logFunc ), 230363f8f98aSdrh FUNCTION(log2, 1, 2, 0, logFunc ), 2304f6e904bdSdrh FUNCTION(log, 2, 0, 0, logFunc ), 230563f8f98aSdrh MFUNCTION(exp, 1, exp, math1Func ), 230663f8f98aSdrh MFUNCTION(pow, 2, pow, math2Func ), 230763f8f98aSdrh MFUNCTION(power, 2, pow, math2Func ), 230863f8f98aSdrh MFUNCTION(mod, 2, fmod, math2Func ), 230963f8f98aSdrh MFUNCTION(acos, 1, acos, math1Func ), 231063f8f98aSdrh MFUNCTION(asin, 1, asin, math1Func ), 231163f8f98aSdrh MFUNCTION(atan, 1, atan, math1Func ), 231263f8f98aSdrh MFUNCTION(atan2, 2, atan2, math2Func ), 231363f8f98aSdrh MFUNCTION(cos, 1, cos, math1Func ), 231463f8f98aSdrh MFUNCTION(sin, 1, sin, math1Func ), 231563f8f98aSdrh MFUNCTION(tan, 1, tan, math1Func ), 231663f8f98aSdrh MFUNCTION(cosh, 1, cosh, math1Func ), 231763f8f98aSdrh MFUNCTION(sinh, 1, sinh, math1Func ), 231863f8f98aSdrh MFUNCTION(tanh, 1, tanh, math1Func ), 2319d97a4c00Smistachkin #if SQLITE_HAVE_C99_MATH_FUNCS 232063f8f98aSdrh MFUNCTION(acosh, 1, acosh, math1Func ), 232163f8f98aSdrh MFUNCTION(asinh, 1, asinh, math1Func ), 232263f8f98aSdrh MFUNCTION(atanh, 1, atanh, math1Func ), 2323d97a4c00Smistachkin #endif 232463f8f98aSdrh MFUNCTION(sqrt, 1, sqrt, math1Func ), 232563f8f98aSdrh MFUNCTION(radians, 1, degToRad, math1Func ), 232663f8f98aSdrh MFUNCTION(degrees, 1, radToDeg, math1Func ), 232763f8f98aSdrh FUNCTION(pi, 0, 0, 0, piFunc ), 2328f6e904bdSdrh #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ 232963f8f98aSdrh FUNCTION(sign, 1, 0, 0, signFunc ), 233063f8f98aSdrh INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ), 233163f8f98aSdrh INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ), 23328c0a791aSdanielk1977 }; 2333545f587fSdrh #ifndef SQLITE_OMIT_ALTERTABLE 2334545f587fSdrh sqlite3AlterFunctions(); 2335545f587fSdrh #endif 2336dfa552f4Sdan sqlite3WindowFunctions(); 233780738d9cSdrh sqlite3RegisterDateTimeFunctions(); 233880738d9cSdrh sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); 233980738d9cSdrh 234080738d9cSdrh #if 0 /* Enable to print out how the built-in functions are hashed */ 234180738d9cSdrh { 234280738d9cSdrh int i; 234380738d9cSdrh FuncDef *p; 234480738d9cSdrh for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ 234580738d9cSdrh printf("FUNC-HASH %02d:", i); 234680738d9cSdrh for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){ 234780738d9cSdrh int n = sqlite3Strlen30(p->zName); 234880738d9cSdrh int h = p->zName[0] + n; 2349f9751074Sdrh assert( p->funcFlags & SQLITE_FUNC_BUILTIN ); 235080738d9cSdrh printf(" %s(%d)", p->zName, h); 235180738d9cSdrh } 235280738d9cSdrh printf("\n"); 235380738d9cSdrh } 235480738d9cSdrh } 235580738d9cSdrh #endif 235670a8ca3cSdrh } 2357