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 ** 574ab2f1f95Sdrh ** IMP: R-62073-11209 The changes() SQL function is a wrapper 57510496f76Slarrybr ** around the sqlite3_changes64() C/C++ function and hence follows the same 576ab2f1f95Sdrh ** 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); 59910496f76Slarrybr /* IMP: R-52756-41993 This function was a wrapper around the 600ab2f1f95Sdrh ** sqlite3_total_changes() 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?) 1718b0689696Sdrh */ 1719b0689696Sdrh static void groupConcatStep( 1720b0689696Sdrh sqlite3_context *context, 1721b0689696Sdrh int argc, 1722b0689696Sdrh sqlite3_value **argv 1723b0689696Sdrh ){ 1724b0689696Sdrh const char *zVal; 1725ade86483Sdrh StrAccum *pAccum; 1726b0689696Sdrh const char *zSep; 172707d3117aSdrh int nVal, nSep; 172807d3117aSdrh assert( argc==1 || argc==2 ); 172907d3117aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1730ade86483Sdrh pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1731ade86483Sdrh 1732ade86483Sdrh if( pAccum ){ 1733bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1734e3bf632cSdan int firstTerm = pAccum->mxAlloc==0; 1735bb4957f8Sdrh pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 17368bfd7190Sdrh if( !firstTerm ){ 173707d3117aSdrh if( argc==2 ){ 173807d3117aSdrh zSep = (char*)sqlite3_value_text(argv[1]); 173907d3117aSdrh nSep = sqlite3_value_bytes(argv[1]); 1740b0689696Sdrh }else{ 1741b0689696Sdrh zSep = ","; 1742ade86483Sdrh nSep = 1; 1743b0689696Sdrh } 17440cdbe1aeSdrh if( zSep ) sqlite3_str_append(pAccum, zSep, nSep); 1745b0689696Sdrh } 174607d3117aSdrh zVal = (char*)sqlite3_value_text(argv[0]); 174707d3117aSdrh nVal = sqlite3_value_bytes(argv[0]); 17480cdbe1aeSdrh if( zVal ) sqlite3_str_append(pAccum, zVal, nVal); 1749b0689696Sdrh } 1750b0689696Sdrh } 175167a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 175203854d2eSdan static void groupConcatInverse( 175303854d2eSdan sqlite3_context *context, 175403854d2eSdan int argc, 175503854d2eSdan sqlite3_value **argv 175603854d2eSdan ){ 175703854d2eSdan int n; 175803854d2eSdan StrAccum *pAccum; 1759c7bf5716Sdrh assert( argc==1 || argc==2 ); 176003854d2eSdan if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 176103854d2eSdan pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1762fd4b7285Sdrh /* pAccum is always non-NULL since groupConcatStep() will have always 1763fd4b7285Sdrh ** run frist to initialize it */ 1764fd4b7285Sdrh if( ALWAYS(pAccum) ){ 176503854d2eSdan n = sqlite3_value_bytes(argv[0]); 176603854d2eSdan if( argc==2 ){ 176703854d2eSdan n += sqlite3_value_bytes(argv[1]); 1768683b0fffSdan }else{ 1769683b0fffSdan n++; 177003854d2eSdan } 1771c7bf5716Sdrh if( n>=(int)pAccum->nChar ){ 177203854d2eSdan pAccum->nChar = 0; 177303854d2eSdan }else{ 177403854d2eSdan pAccum->nChar -= n; 177503854d2eSdan memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar); 177603854d2eSdan } 1777e3bf632cSdan if( pAccum->nChar==0 ) pAccum->mxAlloc = 0; 177803854d2eSdan } 177903854d2eSdan } 178067a9b8edSdan #else 178167a9b8edSdan # define groupConcatInverse 0 178267a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 1783b0689696Sdrh static void groupConcatFinalize(sqlite3_context *context){ 1784ade86483Sdrh StrAccum *pAccum; 1785ade86483Sdrh pAccum = sqlite3_aggregate_context(context, 0); 1786ade86483Sdrh if( pAccum ){ 17870cdbe1aeSdrh if( pAccum->accError==SQLITE_TOOBIG ){ 1788ade86483Sdrh sqlite3_result_error_toobig(context); 17890cdbe1aeSdrh }else if( pAccum->accError==SQLITE_NOMEM ){ 1790ade86483Sdrh sqlite3_result_error_nomem(context); 1791ade86483Sdrh }else{ 1792ade86483Sdrh sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 1793ade86483Sdrh sqlite3_free); 1794b0689696Sdrh } 1795b0689696Sdrh } 1796ade86483Sdrh } 179767a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1798e2f781b9Sdan static void groupConcatValue(sqlite3_context *context){ 1799e2f781b9Sdan sqlite3_str *pAccum; 1800e2f781b9Sdan pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0); 1801e2f781b9Sdan if( pAccum ){ 1802e2f781b9Sdan if( pAccum->accError==SQLITE_TOOBIG ){ 1803e2f781b9Sdan sqlite3_result_error_toobig(context); 1804e2f781b9Sdan }else if( pAccum->accError==SQLITE_NOMEM ){ 1805e2f781b9Sdan sqlite3_result_error_nomem(context); 1806e2f781b9Sdan }else{ 1807e2f781b9Sdan const char *zText = sqlite3_str_value(pAccum); 1808e2f781b9Sdan sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 1809e2f781b9Sdan } 1810e2f781b9Sdan } 1811e2f781b9Sdan } 181267a9b8edSdan #else 181367a9b8edSdan # define groupConcatValue 0 181467a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 18154e5ffc5fSdrh 1816d3a149efSdrh /* 1817a4741840Sdrh ** This routine does per-connection function registration. Most 1818a4741840Sdrh ** of the built-in functions above are part of the global function set. 1819a4741840Sdrh ** This routine only deals with those that are not global. 1820dc04c583Sdrh */ 182180738d9cSdrh void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ 1822832a58a6Sdanielk1977 int rc = sqlite3_overload_function(db, "MATCH", 2); 1823832a58a6Sdanielk1977 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 1824832a58a6Sdanielk1977 if( rc==SQLITE_NOMEM ){ 18254a642b60Sdrh sqlite3OomFault(db); 1826832a58a6Sdanielk1977 } 1827832a58a6Sdanielk1977 } 182855ef4d97Sdrh 182955ef4d97Sdrh /* 183008652b5eSdrh ** Re-register the built-in LIKE functions. The caseSensitive 183155ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 183208652b5eSdrh ** sensitive. 183355ef4d97Sdrh */ 183455ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 183555ef4d97Sdrh struct compareInfo *pInfo; 1836ea5c040fSdrh int flags; 183755ef4d97Sdrh if( caseSensitive ){ 183855ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 1839ea5c040fSdrh flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; 184055ef4d97Sdrh }else{ 184155ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 1842ea5c040fSdrh flags = SQLITE_FUNC_LIKE; 184355ef4d97Sdrh } 1844660af939Sdan sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1845660af939Sdan sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1846ea5c040fSdrh sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags; 1847ea5c040fSdrh sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags; 184855ef4d97Sdrh } 184955ef4d97Sdrh 185055ef4d97Sdrh /* 185155ef4d97Sdrh ** pExpr points to an expression which implements a function. If 185255ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 18531d42ea71Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and the 18541d42ea71Sdrh ** escape character and then return TRUE. If the function is not a 18551d42ea71Sdrh ** LIKE-style function then return FALSE. 18561d42ea71Sdrh ** 18571d42ea71Sdrh ** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE 18581d42ea71Sdrh ** operator if c is a string literal that is exactly one byte in length. 18591d42ea71Sdrh ** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is 18601d42ea71Sdrh ** no ESCAPE clause. 186116897072Sdrh ** 186216897072Sdrh ** *pIsNocase is set to true if uppercase and lowercase are equivalent for 186316897072Sdrh ** the function (default for LIKE). If the function makes the distinction 186416897072Sdrh ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to 186516897072Sdrh ** false. 186655ef4d97Sdrh */ 1867d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 186855ef4d97Sdrh FuncDef *pDef; 18691d42ea71Sdrh int nExpr; 187017988aaeSdrh assert( pExpr!=0 ); 187117988aaeSdrh assert( pExpr->op==TK_FUNCTION ); 187217988aaeSdrh if( !pExpr->x.pList ){ 187355ef4d97Sdrh return 0; 187455ef4d97Sdrh } 18756ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 18761d42ea71Sdrh nExpr = pExpr->x.pList->nExpr; 18771d42ea71Sdrh pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0); 187878b52203Sdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 187978b52203Sdrh if( pDef==0 ) return 0; 188078b52203Sdrh #endif 1881d36e1041Sdrh if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ 188255ef4d97Sdrh return 0; 188355ef4d97Sdrh } 188455ef4d97Sdrh 188555ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 188655ef4d97Sdrh ** the first three statements in the compareInfo structure. The 188755ef4d97Sdrh ** asserts() that follow verify that assumption 188855ef4d97Sdrh */ 188955ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 189055ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 189155ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 189255ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1893589c7876Sdrh 1894589c7876Sdrh if( nExpr<3 ){ 1895589c7876Sdrh aWc[3] = 0; 1896589c7876Sdrh }else{ 1897589c7876Sdrh Expr *pEscape = pExpr->x.pList->a[2].pExpr; 1898589c7876Sdrh char *zEscape; 1899589c7876Sdrh if( pEscape->op!=TK_STRING ) return 0; 1900589c7876Sdrh zEscape = pEscape->u.zToken; 1901589c7876Sdrh if( zEscape[0]==0 || zEscape[1]!=0 ) return 0; 1902589c7876Sdrh if( zEscape[0]==aWc[0] ) return 0; 1903589c7876Sdrh if( zEscape[0]==aWc[1] ) return 0; 1904589c7876Sdrh aWc[3] = zEscape[0]; 1905589c7876Sdrh } 1906589c7876Sdrh 1907d36e1041Sdrh *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; 190855ef4d97Sdrh return 1; 1909dc04c583Sdrh } 19108c0a791aSdanielk1977 191163f8f98aSdrh /* Mathematical Constants */ 191263f8f98aSdrh #ifndef M_PI 191363f8f98aSdrh # define M_PI 3.141592653589793238462643383279502884 191463f8f98aSdrh #endif 191563f8f98aSdrh #ifndef M_LN10 191663f8f98aSdrh # define M_LN10 2.302585092994045684017991454684364208 191763f8f98aSdrh #endif 191863f8f98aSdrh #ifndef M_LN2 191963f8f98aSdrh # define M_LN2 0.693147180559945309417232121458176568 192063f8f98aSdrh #endif 192163f8f98aSdrh 192263f8f98aSdrh 1923f6e904bdSdrh /* Extra math functions that require linking with -lm 1924f6e904bdSdrh */ 1925f6e904bdSdrh #ifdef SQLITE_ENABLE_MATH_FUNCTIONS 1926f6e904bdSdrh /* 1927f6e904bdSdrh ** Implementation SQL functions: 1928f6e904bdSdrh ** 1929f6e904bdSdrh ** ceil(X) 1930f6e904bdSdrh ** ceiling(X) 1931f6e904bdSdrh ** floor(X) 1932f6e904bdSdrh ** 1933f6e904bdSdrh ** The sqlite3_user_data() pointer is a pointer to the libm implementation 1934f6e904bdSdrh ** of the underlying C function. 1935f6e904bdSdrh */ 1936f6e904bdSdrh static void ceilingFunc( 1937f6e904bdSdrh sqlite3_context *context, 1938f6e904bdSdrh int argc, 1939f6e904bdSdrh sqlite3_value **argv 1940f6e904bdSdrh ){ 1941f6e904bdSdrh assert( argc==1 ); 194263f8f98aSdrh switch( sqlite3_value_numeric_type(argv[0]) ){ 194363f8f98aSdrh case SQLITE_INTEGER: { 1944f6e904bdSdrh sqlite3_result_int64(context, sqlite3_value_int64(argv[0])); 1945f6e904bdSdrh break; 194663f8f98aSdrh } 194763f8f98aSdrh case SQLITE_FLOAT: { 1948f6e904bdSdrh double (*x)(double) = (double(*)(double))sqlite3_user_data(context); 1949f6e904bdSdrh sqlite3_result_double(context, x(sqlite3_value_double(argv[0]))); 1950f6e904bdSdrh break; 1951f6e904bdSdrh } 195263f8f98aSdrh default: { 195363f8f98aSdrh break; 195463f8f98aSdrh } 1955f6e904bdSdrh } 1956f6e904bdSdrh } 1957f6e904bdSdrh 1958f6e904bdSdrh /* 19594fd4a7a1Sdrh ** On some systems, ceil() and floor() are intrinsic function. You are 1960c2dbf35fSdrh ** unable to take a pointer to these functions. Hence, we here wrap them 19614fd4a7a1Sdrh ** in our own actual functions. 19624fd4a7a1Sdrh */ 19634fd4a7a1Sdrh static double xCeil(double x){ return ceil(x); } 19644fd4a7a1Sdrh static double xFloor(double x){ return floor(x); } 19654fd4a7a1Sdrh 19664fd4a7a1Sdrh /* 1967f6e904bdSdrh ** Implementation of SQL functions: 1968f6e904bdSdrh ** 1969f6e904bdSdrh ** ln(X) - natural logarithm 197063f8f98aSdrh ** log(X) - log X base 10 197163f8f98aSdrh ** log10(X) - log X base 10 197263f8f98aSdrh ** log(B,X) - log X base B 1973f6e904bdSdrh */ 1974f6e904bdSdrh static void logFunc( 1975f6e904bdSdrh sqlite3_context *context, 1976f6e904bdSdrh int argc, 1977f6e904bdSdrh sqlite3_value **argv 1978f6e904bdSdrh ){ 197963f8f98aSdrh double x, b, ans; 1980f6e904bdSdrh assert( argc==1 || argc==2 ); 198163f8f98aSdrh switch( sqlite3_value_numeric_type(argv[0]) ){ 198263f8f98aSdrh case SQLITE_INTEGER: 198363f8f98aSdrh case SQLITE_FLOAT: 198463f8f98aSdrh x = sqlite3_value_double(argv[0]); 198502d6f9b2Sdrh if( x<=0.0 ) return; 198663f8f98aSdrh break; 198763f8f98aSdrh default: 198863f8f98aSdrh return; 1989f6e904bdSdrh } 1990f6e904bdSdrh if( argc==2 ){ 199163f8f98aSdrh switch( sqlite3_value_numeric_type(argv[0]) ){ 199263f8f98aSdrh case SQLITE_INTEGER: 199363f8f98aSdrh case SQLITE_FLOAT: 199402d6f9b2Sdrh b = log(x); 199502d6f9b2Sdrh if( b<=0.0 ) return; 199663f8f98aSdrh x = sqlite3_value_double(argv[1]); 199702d6f9b2Sdrh if( x<=0.0 ) return; 199863f8f98aSdrh break; 199963f8f98aSdrh default: 200063f8f98aSdrh return; 2001f6e904bdSdrh } 200202d6f9b2Sdrh ans = log(x)/b; 200363f8f98aSdrh }else{ 200463f8f98aSdrh ans = log(x); 200563f8f98aSdrh switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){ 200663f8f98aSdrh case 1: 2007f6e904bdSdrh /* Convert from natural logarithm to log base 10 */ 200863f8f98aSdrh ans *= 1.0/M_LN10; 200963f8f98aSdrh break; 201063f8f98aSdrh case 2: 201163f8f98aSdrh /* Convert from natural logarithm to log base 2 */ 201263f8f98aSdrh ans *= 1.0/M_LN2; 201363f8f98aSdrh break; 201463f8f98aSdrh default: 201563f8f98aSdrh break; 201663f8f98aSdrh } 2017f6e904bdSdrh } 2018f6e904bdSdrh sqlite3_result_double(context, ans); 2019f6e904bdSdrh } 202063f8f98aSdrh 202163f8f98aSdrh /* 202263f8f98aSdrh ** Functions to converts degrees to radians and radians to degrees. 202363f8f98aSdrh */ 202463f8f98aSdrh static double degToRad(double x){ return x*(M_PI/180.0); } 202563f8f98aSdrh static double radToDeg(double x){ return x*(180.0/M_PI); } 202663f8f98aSdrh 202763f8f98aSdrh /* 202863f8f98aSdrh ** Implementation of 1-argument SQL math functions: 202963f8f98aSdrh ** 203063f8f98aSdrh ** exp(X) - Compute e to the X-th power 203163f8f98aSdrh */ 203263f8f98aSdrh static void math1Func( 203363f8f98aSdrh sqlite3_context *context, 203463f8f98aSdrh int argc, 203563f8f98aSdrh sqlite3_value **argv 203663f8f98aSdrh ){ 203763f8f98aSdrh int type0; 203863f8f98aSdrh double v0, ans; 203963f8f98aSdrh double (*x)(double); 2040d97a4c00Smistachkin assert( argc==1 ); 204163f8f98aSdrh type0 = sqlite3_value_numeric_type(argv[0]); 204263f8f98aSdrh if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; 204363f8f98aSdrh v0 = sqlite3_value_double(argv[0]); 204463f8f98aSdrh x = (double(*)(double))sqlite3_user_data(context); 204563f8f98aSdrh ans = x(v0); 204663f8f98aSdrh sqlite3_result_double(context, ans); 204763f8f98aSdrh } 204863f8f98aSdrh 204963f8f98aSdrh /* 205063f8f98aSdrh ** Implementation of 2-argument SQL math functions: 205163f8f98aSdrh ** 205263f8f98aSdrh ** power(X,Y) - Compute X to the Y-th power 205363f8f98aSdrh */ 205463f8f98aSdrh static void math2Func( 205563f8f98aSdrh sqlite3_context *context, 205663f8f98aSdrh int argc, 205763f8f98aSdrh sqlite3_value **argv 205863f8f98aSdrh ){ 205963f8f98aSdrh int type0, type1; 206063f8f98aSdrh double v0, v1, ans; 206163f8f98aSdrh double (*x)(double,double); 2062d97a4c00Smistachkin assert( argc==2 ); 206363f8f98aSdrh type0 = sqlite3_value_numeric_type(argv[0]); 206463f8f98aSdrh if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; 206563f8f98aSdrh type1 = sqlite3_value_numeric_type(argv[1]); 206663f8f98aSdrh if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return; 206763f8f98aSdrh v0 = sqlite3_value_double(argv[0]); 206863f8f98aSdrh v1 = sqlite3_value_double(argv[1]); 206963f8f98aSdrh x = (double(*)(double,double))sqlite3_user_data(context); 207063f8f98aSdrh ans = x(v0, v1); 207163f8f98aSdrh sqlite3_result_double(context, ans); 207263f8f98aSdrh } 207363f8f98aSdrh 207463f8f98aSdrh /* 207581e5a9a6Sdrh ** Implementation of 0-argument pi() function. 207663f8f98aSdrh */ 207763f8f98aSdrh static void piFunc( 207863f8f98aSdrh sqlite3_context *context, 207963f8f98aSdrh int argc, 208063f8f98aSdrh sqlite3_value **argv 208163f8f98aSdrh ){ 208263f8f98aSdrh assert( argc==0 ); 208363f8f98aSdrh sqlite3_result_double(context, M_PI); 208463f8f98aSdrh } 208563f8f98aSdrh 2086f6e904bdSdrh #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ 2087f6e904bdSdrh 208893ce741bSdanielk1977 /* 208963f8f98aSdrh ** Implementation of sign(X) function. 209063f8f98aSdrh */ 209163f8f98aSdrh static void signFunc( 209263f8f98aSdrh sqlite3_context *context, 209363f8f98aSdrh int argc, 209463f8f98aSdrh sqlite3_value **argv 209563f8f98aSdrh ){ 209663f8f98aSdrh int type0; 209763f8f98aSdrh double x; 2098e5baf5c2Sdrh UNUSED_PARAMETER(argc); 2099d97a4c00Smistachkin assert( argc==1 ); 210063f8f98aSdrh type0 = sqlite3_value_numeric_type(argv[0]); 210163f8f98aSdrh if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; 210263f8f98aSdrh x = sqlite3_value_double(argv[0]); 210363f8f98aSdrh sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); 210463f8f98aSdrh } 210563f8f98aSdrh 210663f8f98aSdrh /* 210760ec914cSpeter.d.reid ** All of the FuncDef structures in the aBuiltinFunc[] array above 210893ce741bSdanielk1977 ** to the global function hash table. This occurs at start-time (as 210993ce741bSdanielk1977 ** a consequence of calling sqlite3_initialize()). 211093ce741bSdanielk1977 ** 211193ce741bSdanielk1977 ** After this routine runs 211293ce741bSdanielk1977 */ 211380738d9cSdrh void sqlite3RegisterBuiltinFunctions(void){ 21148c0a791aSdanielk1977 /* 2115777c5386Sdrh ** The following array holds FuncDef structures for all of the functions 2116777c5386Sdrh ** defined in this file. 21178c0a791aSdanielk1977 ** 2118777c5386Sdrh ** The array cannot be constant since changes are made to the 2119777c5386Sdrh ** FuncDef.pHash elements at start-time. The elements of this array 2120777c5386Sdrh ** are read-only after initialization is complete. 212180738d9cSdrh ** 212280738d9cSdrh ** For peak efficiency, put the most frequently used function last. 21238c0a791aSdanielk1977 */ 212480738d9cSdrh static FuncDef aBuiltinFunc[] = { 2125171c50ecSdrh /***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/ 2126171c50ecSdrh TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0), 2127171c50ecSdrh TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0), 2128171c50ecSdrh TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0), 2129171c50ecSdrh #ifdef SQLITE_DEBUG 2130171c50ecSdrh TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0), 2131171c50ecSdrh #endif 2132171c50ecSdrh /***** Regular functions *****/ 213380738d9cSdrh #ifdef SQLITE_SOUNDEX 213480738d9cSdrh FUNCTION(soundex, 1, 0, 0, soundexFunc ), 213580738d9cSdrh #endif 213680738d9cSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 213764de2a5fSdrh SFUNCTION(load_extension, 1, 0, 0, loadExt ), 213864de2a5fSdrh SFUNCTION(load_extension, 2, 0, 0, loadExt ), 213980738d9cSdrh #endif 214080738d9cSdrh #if SQLITE_USER_AUTHENTICATION 214180738d9cSdrh FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), 214280738d9cSdrh #endif 214380738d9cSdrh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 214480738d9cSdrh DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), 214580738d9cSdrh DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), 214680738d9cSdrh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 214725c4296bSdrh INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 214825c4296bSdrh INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 214925c4296bSdrh INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), 2150092457b1Sdrh #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 215135100fb1Sdrh FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET| 21522fc865c1Sdrh SQLITE_FUNC_TYPEOF), 2153092457b1Sdrh #endif 21548c0a791aSdanielk1977 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 21558c0a791aSdanielk1977 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 21568c0a791aSdanielk1977 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 21578c0a791aSdanielk1977 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 21588c0a791aSdanielk1977 FUNCTION(trim, 1, 3, 0, trimFunc ), 21598c0a791aSdanielk1977 FUNCTION(trim, 2, 3, 0, trimFunc ), 21608c0a791aSdanielk1977 FUNCTION(min, -1, 0, 1, minmaxFunc ), 21618c0a791aSdanielk1977 FUNCTION(min, 0, 0, 1, 0 ), 21626fb2b54cSdan WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 2163*bb301231Sdrh SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ), 21648c0a791aSdanielk1977 FUNCTION(max, -1, 1, 1, minmaxFunc ), 21658c0a791aSdanielk1977 FUNCTION(max, 0, 1, 1, 0 ), 21666fb2b54cSdan WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 2167*bb301231Sdrh SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ), 2168a748fdccSdrh FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 2169a748fdccSdrh FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 2170d55e0729Sdrh FUNCTION(instr, 2, 0, 0, instrFunc ), 2171a5c1416dSdrh FUNCTION(printf, -1, 0, 0, printfFunc ), 2172d495d8c9Sdrh FUNCTION(unicode, 1, 0, 0, unicodeFunc ), 2173d495d8c9Sdrh FUNCTION(char, -1, 0, 0, charFunc ), 21748c0a791aSdanielk1977 FUNCTION(abs, 1, 0, 0, absFunc ), 2175fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 21768c0a791aSdanielk1977 FUNCTION(round, 1, 0, 0, roundFunc ), 21778c0a791aSdanielk1977 FUNCTION(round, 2, 0, 0, roundFunc ), 2178fbd60f82Sshane #endif 21798c0a791aSdanielk1977 FUNCTION(upper, 1, 0, 0, upperFunc ), 21808c0a791aSdanielk1977 FUNCTION(lower, 1, 0, 0, lowerFunc ), 21818c0a791aSdanielk1977 FUNCTION(hex, 1, 0, 0, hexFunc ), 2182ffe421c7Sdrh INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ), 2183b1fba286Sdrh VFUNCTION(random, 0, 0, 0, randomFunc ), 2184b1fba286Sdrh VFUNCTION(randomblob, 1, 0, 0, randomBlob ), 21858c0a791aSdanielk1977 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 218603bf26d9Sdrh DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 218703bf26d9Sdrh DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), 2188840561f2Sdrh FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), 21898c0a791aSdanielk1977 FUNCTION(quote, 1, 0, 0, quoteFunc ), 2190b1fba286Sdrh VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 2191b1fba286Sdrh VFUNCTION(changes, 0, 0, 0, changes ), 2192b1fba286Sdrh VFUNCTION(total_changes, 0, 0, 0, total_changes ), 21938c0a791aSdanielk1977 FUNCTION(replace, 3, 0, 0, replaceFunc ), 21948c0a791aSdanielk1977 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 219580738d9cSdrh FUNCTION(substr, 2, 0, 0, substrFunc ), 219680738d9cSdrh FUNCTION(substr, 3, 0, 0, substrFunc ), 21971335ec7dSdrh FUNCTION(substring, 2, 0, 0, substrFunc ), 21981335ec7dSdrh FUNCTION(substring, 3, 0, 0, substrFunc ), 21996fb2b54cSdan WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0), 22006fb2b54cSdan WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0), 22016fb2b54cSdan WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0), 22027262ca94Sdan WAGGREGATE(count, 0,0,0, countStep, 2203*bb301231Sdrh countFinalize, countFinalize, countInverse, 2204*bb301231Sdrh SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ), 22057262ca94Sdan WAGGREGATE(count, 1,0,0, countStep, 2206*bb301231Sdrh countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ), 220703854d2eSdan WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, 22086fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 220903854d2eSdan WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, 22106fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 22118c0a791aSdanielk1977 22128c0a791aSdanielk1977 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 22138c0a791aSdanielk1977 #ifdef SQLITE_CASE_SENSITIVE_LIKE 22148c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 22158c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 22168c0a791aSdanielk1977 #else 22178c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 22188c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 22198c0a791aSdanielk1977 #endif 2220cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 2221cc15313cSdrh FUNCTION(unknown, -1, 0, 0, unknownFunc ), 2222cc15313cSdrh #endif 222380738d9cSdrh FUNCTION(coalesce, 1, 0, 0, 0 ), 222480738d9cSdrh FUNCTION(coalesce, 0, 0, 0, 0 ), 2225f6e904bdSdrh #ifdef SQLITE_ENABLE_MATH_FUNCTIONS 22264fd4a7a1Sdrh MFUNCTION(ceil, 1, xCeil, ceilingFunc ), 22274fd4a7a1Sdrh MFUNCTION(ceiling, 1, xCeil, ceilingFunc ), 22284fd4a7a1Sdrh MFUNCTION(floor, 1, xFloor, ceilingFunc ), 2229d97a4c00Smistachkin #if SQLITE_HAVE_C99_MATH_FUNCS 223063f8f98aSdrh MFUNCTION(trunc, 1, trunc, ceilingFunc ), 2231d97a4c00Smistachkin #endif 2232f6e904bdSdrh FUNCTION(ln, 1, 0, 0, logFunc ), 2233f6e904bdSdrh FUNCTION(log, 1, 1, 0, logFunc ), 2234f6e904bdSdrh FUNCTION(log10, 1, 1, 0, logFunc ), 223563f8f98aSdrh FUNCTION(log2, 1, 2, 0, logFunc ), 2236f6e904bdSdrh FUNCTION(log, 2, 0, 0, logFunc ), 223763f8f98aSdrh MFUNCTION(exp, 1, exp, math1Func ), 223863f8f98aSdrh MFUNCTION(pow, 2, pow, math2Func ), 223963f8f98aSdrh MFUNCTION(power, 2, pow, math2Func ), 224063f8f98aSdrh MFUNCTION(mod, 2, fmod, math2Func ), 224163f8f98aSdrh MFUNCTION(acos, 1, acos, math1Func ), 224263f8f98aSdrh MFUNCTION(asin, 1, asin, math1Func ), 224363f8f98aSdrh MFUNCTION(atan, 1, atan, math1Func ), 224463f8f98aSdrh MFUNCTION(atan2, 2, atan2, math2Func ), 224563f8f98aSdrh MFUNCTION(cos, 1, cos, math1Func ), 224663f8f98aSdrh MFUNCTION(sin, 1, sin, math1Func ), 224763f8f98aSdrh MFUNCTION(tan, 1, tan, math1Func ), 224863f8f98aSdrh MFUNCTION(cosh, 1, cosh, math1Func ), 224963f8f98aSdrh MFUNCTION(sinh, 1, sinh, math1Func ), 225063f8f98aSdrh MFUNCTION(tanh, 1, tanh, math1Func ), 2251d97a4c00Smistachkin #if SQLITE_HAVE_C99_MATH_FUNCS 225263f8f98aSdrh MFUNCTION(acosh, 1, acosh, math1Func ), 225363f8f98aSdrh MFUNCTION(asinh, 1, asinh, math1Func ), 225463f8f98aSdrh MFUNCTION(atanh, 1, atanh, math1Func ), 2255d97a4c00Smistachkin #endif 225663f8f98aSdrh MFUNCTION(sqrt, 1, sqrt, math1Func ), 225763f8f98aSdrh MFUNCTION(radians, 1, degToRad, math1Func ), 225863f8f98aSdrh MFUNCTION(degrees, 1, radToDeg, math1Func ), 225963f8f98aSdrh FUNCTION(pi, 0, 0, 0, piFunc ), 2260f6e904bdSdrh #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ 226163f8f98aSdrh FUNCTION(sign, 1, 0, 0, signFunc ), 226263f8f98aSdrh INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ), 226363f8f98aSdrh INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ), 22648c0a791aSdanielk1977 }; 2265545f587fSdrh #ifndef SQLITE_OMIT_ALTERTABLE 2266545f587fSdrh sqlite3AlterFunctions(); 2267545f587fSdrh #endif 2268dfa552f4Sdan sqlite3WindowFunctions(); 226980738d9cSdrh sqlite3RegisterDateTimeFunctions(); 227080738d9cSdrh sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); 227180738d9cSdrh 227280738d9cSdrh #if 0 /* Enable to print out how the built-in functions are hashed */ 227380738d9cSdrh { 227480738d9cSdrh int i; 227580738d9cSdrh FuncDef *p; 227680738d9cSdrh for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ 227780738d9cSdrh printf("FUNC-HASH %02d:", i); 227880738d9cSdrh for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){ 227980738d9cSdrh int n = sqlite3Strlen30(p->zName); 228080738d9cSdrh int h = p->zName[0] + n; 228180738d9cSdrh printf(" %s(%d)", p->zName, h); 228280738d9cSdrh } 228380738d9cSdrh printf("\n"); 228480738d9cSdrh } 228580738d9cSdrh } 228680738d9cSdrh #endif 228770a8ca3cSdrh } 2288