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> 1988208050Sdanielk1977 #include "vdbeInt.h" 200bce8354Sdrh 2155ef4d97Sdrh /* 2255ef4d97Sdrh ** Return the collating function associated with a function. 2355ef4d97Sdrh */ 24dc1bdc4fSdanielk1977 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 25a9e03b1bSdrh VdbeOp *pOp; 26a9e03b1bSdrh assert( context->pVdbe!=0 ); 27a9e03b1bSdrh pOp = &context->pVdbe->aOp[context->iOp-1]; 28a15cc47fSdrh assert( pOp->opcode==OP_CollSeq ); 29a15cc47fSdrh assert( pOp->p4type==P4_COLLSEQ ); 30a15cc47fSdrh return pOp->p4.pColl; 31dc1bdc4fSdanielk1977 } 32dc1bdc4fSdanielk1977 330bce8354Sdrh /* 347a95789cSdrh ** Indicate that the accumulator load should be skipped on this 357a95789cSdrh ** iteration of the aggregate loop. 367a95789cSdrh */ 377a95789cSdrh static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ 3821d59784Sdrh assert( context->isError<=0 ); 3921d59784Sdrh context->isError = -1; 407a95789cSdrh context->skipFlag = 1; 417a95789cSdrh } 427a95789cSdrh 437a95789cSdrh /* 440bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions 450bce8354Sdrh */ 46f9b596ebSdrh static void minmaxFunc( 47f9b596ebSdrh sqlite3_context *context, 48f9b596ebSdrh int argc, 49f9b596ebSdrh sqlite3_value **argv 50f9b596ebSdrh ){ 510bce8354Sdrh int i; 52268380caSdrh int mask; /* 0 for min() or 0xffffffff for max() */ 53f9b596ebSdrh int iBest; 54dc1bdc4fSdanielk1977 CollSeq *pColl; 550bce8354Sdrh 5665595cd6Sdrh assert( argc>1 ); 57c44af71cSdrh mask = sqlite3_user_data(context)==0 ? 0 : -1; 58dc1bdc4fSdanielk1977 pColl = sqlite3GetFuncCollSeq(context); 59dc1bdc4fSdanielk1977 assert( pColl ); 60c572ef7fSdanielk1977 assert( mask==-1 || mask==0 ); 61f9b596ebSdrh iBest = 0; 629c054830Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 63f9b596ebSdrh for(i=1; i<argc; i++){ 649c054830Sdrh if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 65dc1bdc4fSdanielk1977 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 6665595cd6Sdrh testcase( mask==0 ); 67f9b596ebSdrh iBest = i; 680bce8354Sdrh } 690bce8354Sdrh } 70f4479501Sdrh sqlite3_result_value(context, argv[iBest]); 710bce8354Sdrh } 720bce8354Sdrh 73268380caSdrh /* 74268380caSdrh ** Return the type of the argument. 75268380caSdrh */ 76f9b596ebSdrh static void typeofFunc( 77f9b596ebSdrh sqlite3_context *context, 7862c14b34Sdanielk1977 int NotUsed, 79f9b596ebSdrh sqlite3_value **argv 80f9b596ebSdrh ){ 819d8e401cSdrh static const char *azType[] = { "integer", "real", "text", "blob", "null" }; 829d8e401cSdrh int i = sqlite3_value_type(argv[0]) - 1; 8362c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 849d8e401cSdrh assert( i>=0 && i<ArraySize(azType) ); 859d8e401cSdrh assert( SQLITE_INTEGER==1 ); 869d8e401cSdrh assert( SQLITE_FLOAT==2 ); 879d8e401cSdrh assert( SQLITE_TEXT==3 ); 889d8e401cSdrh assert( SQLITE_BLOB==4 ); 899d8e401cSdrh assert( SQLITE_NULL==5 ); 903cef3649Sdrh /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns 913cef3649Sdrh ** the datatype code for the initial datatype of the sqlite3_value object 923cef3649Sdrh ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, 933cef3649Sdrh ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */ 949d8e401cSdrh sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC); 950bce8354Sdrh } 960bce8354Sdrh 975708d2deSdrh 985708d2deSdrh /* 990bce8354Sdrh ** Implementation of the length() function 1000bce8354Sdrh */ 101f9b596ebSdrh static void lengthFunc( 102f9b596ebSdrh sqlite3_context *context, 103f9b596ebSdrh int argc, 104f9b596ebSdrh sqlite3_value **argv 105f9b596ebSdrh ){ 1060bce8354Sdrh assert( argc==1 ); 107f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 108f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1099c054830Sdrh case SQLITE_BLOB: 1109c054830Sdrh case SQLITE_INTEGER: 1119c054830Sdrh case SQLITE_FLOAT: { 112f4479501Sdrh sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 113f9b596ebSdrh break; 114f9b596ebSdrh } 1159c054830Sdrh case SQLITE_TEXT: { 1162646da7eSdrh const unsigned char *z = sqlite3_value_text(argv[0]); 1177ea3469eSdrh const unsigned char *z0; 1187ea3469eSdrh unsigned char c; 1197a521cfbSdrh if( z==0 ) return; 1207ea3469eSdrh z0 = z; 1217ea3469eSdrh while( (c = *z)!=0 ){ 1227ea3469eSdrh z++; 1237ea3469eSdrh if( c>=0xc0 ){ 1247ea3469eSdrh while( (*z & 0xc0)==0x80 ){ z++; z0++; } 1254a919118Sdrh } 1267ea3469eSdrh } 1277ea3469eSdrh sqlite3_result_int(context, (int)(z-z0)); 128f9b596ebSdrh break; 129f9b596ebSdrh } 130f9b596ebSdrh default: { 131f9b596ebSdrh sqlite3_result_null(context); 132f9b596ebSdrh break; 133f9b596ebSdrh } 134f9b596ebSdrh } 1350bce8354Sdrh } 1360bce8354Sdrh 1370bce8354Sdrh /* 1382ba3cccfSdrh ** Implementation of the abs() function. 1392ba3cccfSdrh ** 1402ba3cccfSdrh ** IMP: R-23979-26855 The abs(X) function returns the absolute value of 1412ba3cccfSdrh ** the numeric argument X. 1420bce8354Sdrh */ 1430ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1440bce8354Sdrh assert( argc==1 ); 145f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 146f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1479c054830Sdrh case SQLITE_INTEGER: { 148f93bbbeaSdanielk1977 i64 iVal = sqlite3_value_int64(argv[0]); 14952fc849aSdrh if( iVal<0 ){ 150693e6719Sdrh if( iVal==SMALLEST_INT64 ){ 151eb091cdfSdrh /* IMP: R-31676-45509 If X is the integer -9223372036854775808 152eb091cdfSdrh ** then abs(X) throws an integer overflow error since there is no 1532ba3cccfSdrh ** equivalent positive 64-bit two complement value. */ 15452fc849aSdrh sqlite3_result_error(context, "integer overflow", -1); 15552fc849aSdrh return; 15652fc849aSdrh } 15752fc849aSdrh iVal = -iVal; 15852fc849aSdrh } 159f93bbbeaSdanielk1977 sqlite3_result_int64(context, iVal); 160f9b596ebSdrh break; 161f9b596ebSdrh } 1629c054830Sdrh case SQLITE_NULL: { 1632ba3cccfSdrh /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ 164f9b596ebSdrh sqlite3_result_null(context); 165f9b596ebSdrh break; 166f9b596ebSdrh } 167f9b596ebSdrh default: { 1682ba3cccfSdrh /* Because sqlite3_value_double() returns 0.0 if the argument is not 1692ba3cccfSdrh ** something that can be converted into a number, we have: 170643091f0Sdrh ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob 171643091f0Sdrh ** that cannot be converted to a numeric value. 1722ba3cccfSdrh */ 173f93bbbeaSdanielk1977 double rVal = sqlite3_value_double(argv[0]); 17452fc849aSdrh if( rVal<0 ) rVal = -rVal; 175f93bbbeaSdanielk1977 sqlite3_result_double(context, rVal); 176f9b596ebSdrh break; 177f9b596ebSdrh } 178f9b596ebSdrh } 1790bce8354Sdrh } 1800bce8354Sdrh 1810bce8354Sdrh /* 182d55e0729Sdrh ** Implementation of the instr() function. 183d55e0729Sdrh ** 184d55e0729Sdrh ** instr(haystack,needle) finds the first occurrence of needle 185d55e0729Sdrh ** in haystack and returns the number of previous characters plus 1, 186d55e0729Sdrh ** or 0 if needle does not occur within haystack. 187d55e0729Sdrh ** 188d55e0729Sdrh ** If both haystack and needle are BLOBs, then the result is one more than 189d55e0729Sdrh ** the number of bytes in haystack prior to the first occurrence of needle, 190d55e0729Sdrh ** or 0 if needle never occurs in haystack. 191d55e0729Sdrh */ 192d55e0729Sdrh static void instrFunc( 193d55e0729Sdrh sqlite3_context *context, 194d55e0729Sdrh int argc, 195d55e0729Sdrh sqlite3_value **argv 196d55e0729Sdrh ){ 197d55e0729Sdrh const unsigned char *zHaystack; 198d55e0729Sdrh const unsigned char *zNeedle; 199d55e0729Sdrh int nHaystack; 200d55e0729Sdrh int nNeedle; 201d55e0729Sdrh int typeHaystack, typeNeedle; 202d55e0729Sdrh int N = 1; 203d55e0729Sdrh int isText; 204c930b405Sdrh unsigned char firstChar; 205d55e0729Sdrh 20668c804b9Sdrh UNUSED_PARAMETER(argc); 207d55e0729Sdrh typeHaystack = sqlite3_value_type(argv[0]); 208d55e0729Sdrh typeNeedle = sqlite3_value_type(argv[1]); 209d55e0729Sdrh if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; 210d55e0729Sdrh nHaystack = sqlite3_value_bytes(argv[0]); 211d55e0729Sdrh nNeedle = sqlite3_value_bytes(argv[1]); 212895decf6Sdan if( nNeedle>0 ){ 213d55e0729Sdrh if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ 214d55e0729Sdrh zHaystack = sqlite3_value_blob(argv[0]); 215d55e0729Sdrh zNeedle = sqlite3_value_blob(argv[1]); 216d55e0729Sdrh isText = 0; 217d55e0729Sdrh }else{ 218d55e0729Sdrh zHaystack = sqlite3_value_text(argv[0]); 219d55e0729Sdrh zNeedle = sqlite3_value_text(argv[1]); 220d55e0729Sdrh isText = 1; 221d55e0729Sdrh } 222b30574bcSdrh if( zNeedle==0 || (nHaystack && zHaystack==0) ) return; 223c930b405Sdrh firstChar = zNeedle[0]; 224c930b405Sdrh while( nNeedle<=nHaystack 225c930b405Sdrh && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0) 226c930b405Sdrh ){ 227d55e0729Sdrh N++; 228d55e0729Sdrh do{ 229d55e0729Sdrh nHaystack--; 230d55e0729Sdrh zHaystack++; 231d55e0729Sdrh }while( isText && (zHaystack[0]&0xc0)==0x80 ); 232d55e0729Sdrh } 233d55e0729Sdrh if( nNeedle>nHaystack ) N = 0; 234895decf6Sdan } 235d55e0729Sdrh sqlite3_result_int(context, N); 236d55e0729Sdrh } 237d55e0729Sdrh 238d55e0729Sdrh /* 239a5c1416dSdrh ** Implementation of the printf() function. 240a5c1416dSdrh */ 241a5c1416dSdrh static void printfFunc( 242a5c1416dSdrh sqlite3_context *context, 243a5c1416dSdrh int argc, 244a5c1416dSdrh sqlite3_value **argv 245a5c1416dSdrh ){ 246a5c1416dSdrh PrintfArguments x; 247a5c1416dSdrh StrAccum str; 248a5c1416dSdrh const char *zFormat; 249a5c1416dSdrh int n; 250c0490572Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 251a5c1416dSdrh 252a5c1416dSdrh if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 253a5c1416dSdrh x.nArg = argc-1; 254a5c1416dSdrh x.nUsed = 0; 255a5c1416dSdrh x.apArg = argv+1; 256c0490572Sdrh sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); 2575f4a686fSdrh str.printfFlags = SQLITE_PRINTF_SQLFUNC; 2580cdbe1aeSdrh sqlite3_str_appendf(&str, zFormat, &x); 259a5c1416dSdrh n = str.nChar; 260a5c1416dSdrh sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, 261a5c1416dSdrh SQLITE_DYNAMIC); 262a5c1416dSdrh } 263a5c1416dSdrh } 264a5c1416dSdrh 265a5c1416dSdrh /* 266f764e6fcSdrh ** Implementation of the substr() function. 267f764e6fcSdrh ** 268f764e6fcSdrh ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. 269f764e6fcSdrh ** p1 is 1-indexed. So substr(x,1,1) returns the first character 270f764e6fcSdrh ** of x. If x is text, then we actually count UTF-8 characters. 271f764e6fcSdrh ** If x is a blob, then we count bytes. 272f764e6fcSdrh ** 273f764e6fcSdrh ** If p1 is negative, then we begin abs(p1) from the end of x[]. 274779b8f12Sshaneh ** 275f7b5496eSdrh ** If p2 is negative, return the p2 characters preceding p1. 2760bce8354Sdrh */ 277f9b596ebSdrh static void substrFunc( 278f9b596ebSdrh sqlite3_context *context, 279f9b596ebSdrh int argc, 280f9b596ebSdrh sqlite3_value **argv 281f9b596ebSdrh ){ 2822646da7eSdrh const unsigned char *z; 2832646da7eSdrh const unsigned char *z2; 284023ae03aSdrh int len; 285f764e6fcSdrh int p0type; 286023ae03aSdrh i64 p1, p2; 28765595cd6Sdrh int negP2 = 0; 288f9b596ebSdrh 28964f31519Sdrh assert( argc==3 || argc==2 ); 2908198d254Sdrh if( sqlite3_value_type(argv[1])==SQLITE_NULL 2918198d254Sdrh || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) 2928198d254Sdrh ){ 2938198d254Sdrh return; 2948198d254Sdrh } 295f764e6fcSdrh p0type = sqlite3_value_type(argv[0]); 2964adc4cb9Sdrh p1 = sqlite3_value_int(argv[1]); 297f764e6fcSdrh if( p0type==SQLITE_BLOB ){ 298f764e6fcSdrh len = sqlite3_value_bytes(argv[0]); 299f764e6fcSdrh z = sqlite3_value_blob(argv[0]); 300f764e6fcSdrh if( z==0 ) return; 3011f0feef8Sdrh assert( len==sqlite3_value_bytes(argv[0]) ); 302f764e6fcSdrh }else{ 3034f26d6c4Sdrh z = sqlite3_value_text(argv[0]); 3040bce8354Sdrh if( z==0 ) return; 3054a919118Sdrh len = 0; 3064adc4cb9Sdrh if( p1<0 ){ 3074a919118Sdrh for(z2=z; *z2; len++){ 3084a919118Sdrh SQLITE_SKIP_UTF8(z2); 3094a919118Sdrh } 310f764e6fcSdrh } 3114adc4cb9Sdrh } 312883ad049Sdrh #ifdef SQLITE_SUBSTR_COMPATIBILITY 313883ad049Sdrh /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as 314883ad049Sdrh ** as substr(X,1,N) - it returns the first N characters of X. This 315883ad049Sdrh ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] 316883ad049Sdrh ** from 2009-02-02 for compatibility of applications that exploited the 317883ad049Sdrh ** old buggy behavior. */ 318883ad049Sdrh if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ 319883ad049Sdrh #endif 32064f31519Sdrh if( argc==3 ){ 32151ad0ecdSdanielk1977 p2 = sqlite3_value_int(argv[2]); 32265595cd6Sdrh if( p2<0 ){ 32365595cd6Sdrh p2 = -p2; 32465595cd6Sdrh negP2 = 1; 32565595cd6Sdrh } 32664f31519Sdrh }else{ 327bb4957f8Sdrh p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; 32864f31519Sdrh } 3290bce8354Sdrh if( p1<0 ){ 33089425d5eSdrh p1 += len; 331653bc759Sdrh if( p1<0 ){ 332653bc759Sdrh p2 += p1; 33365595cd6Sdrh if( p2<0 ) p2 = 0; 334653bc759Sdrh p1 = 0; 335653bc759Sdrh } 3360bce8354Sdrh }else if( p1>0 ){ 3370bce8354Sdrh p1--; 33865595cd6Sdrh }else if( p2>0 ){ 33965595cd6Sdrh p2--; 3400bce8354Sdrh } 34165595cd6Sdrh if( negP2 ){ 34265595cd6Sdrh p1 -= p2; 3434e79c594Sdrh if( p1<0 ){ 3444e79c594Sdrh p2 += p1; 3454e79c594Sdrh p1 = 0; 3464e79c594Sdrh } 3474e79c594Sdrh } 34865595cd6Sdrh assert( p1>=0 && p2>=0 ); 349f764e6fcSdrh if( p0type!=SQLITE_BLOB ){ 3504a919118Sdrh while( *z && p1 ){ 3514a919118Sdrh SQLITE_SKIP_UTF8(z); 3524a919118Sdrh p1--; 3530bce8354Sdrh } 3544a919118Sdrh for(z2=z; *z2 && p2; p2--){ 3554a919118Sdrh SQLITE_SKIP_UTF8(z2); 3560bce8354Sdrh } 357bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, 358bbf483f8Sdrh SQLITE_UTF8); 359f764e6fcSdrh }else{ 3604adc4cb9Sdrh if( p1+p2>len ){ 3614adc4cb9Sdrh p2 = len-p1; 3624adc4cb9Sdrh if( p2<0 ) p2 = 0; 3634adc4cb9Sdrh } 364bbf483f8Sdrh sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); 365f764e6fcSdrh } 3660bce8354Sdrh } 3670bce8354Sdrh 3680bce8354Sdrh /* 3690bce8354Sdrh ** Implementation of the round() function 3700bce8354Sdrh */ 371fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 3720ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 37351ad0ecdSdanielk1977 int n = 0; 3740bce8354Sdrh double r; 37550d654daSdrh char *zBuf; 3760bce8354Sdrh assert( argc==1 || argc==2 ); 37751ad0ecdSdanielk1977 if( argc==2 ){ 3789c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 37951ad0ecdSdanielk1977 n = sqlite3_value_int(argv[1]); 3800bce8354Sdrh if( n>30 ) n = 30; 3810bce8354Sdrh if( n<0 ) n = 0; 38251ad0ecdSdanielk1977 } 383d589a92aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 3844f26d6c4Sdrh r = sqlite3_value_double(argv[0]); 385147e176aSshaneh /* If Y==0 and X will fit in a 64-bit int, 386147e176aSshaneh ** handle the rounding directly, 387147e176aSshaneh ** otherwise use printf. 388147e176aSshaneh */ 389147e176aSshaneh if( n==0 && r>=0 && r<LARGEST_INT64-1 ){ 390147e176aSshaneh r = (double)((sqlite_int64)(r+0.5)); 391147e176aSshaneh }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){ 392147e176aSshaneh r = -(double)((sqlite_int64)((-r)+0.5)); 393147e176aSshaneh }else{ 39450d654daSdrh zBuf = sqlite3_mprintf("%.*f",n,r); 39550d654daSdrh if( zBuf==0 ){ 39650d654daSdrh sqlite3_result_error_nomem(context); 397147e176aSshaneh return; 398147e176aSshaneh } 3999339da1fSdrh sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); 40050d654daSdrh sqlite3_free(zBuf); 4010bce8354Sdrh } 402147e176aSshaneh sqlite3_result_double(context, r); 40350d654daSdrh } 404fbd60f82Sshane #endif 405dc04c583Sdrh 40626783a58Sdanielk1977 /* 407f3cdcdccSdrh ** Allocate nByte bytes of space using sqlite3Malloc(). If the 40826783a58Sdanielk1977 ** allocation fails, call sqlite3_result_error_nomem() to notify 40927e62dbeSdrh ** the database handle that malloc() has failed and return NULL. 41027e62dbeSdrh ** If nByte is larger than the maximum string or blob length, then 41127e62dbeSdrh ** raise an SQLITE_TOOBIG exception and return NULL. 41226783a58Sdanielk1977 */ 413b1a6c3c1Sdrh static void *contextMalloc(sqlite3_context *context, i64 nByte){ 414bb4957f8Sdrh char *z; 41527e62dbeSdrh sqlite3 *db = sqlite3_context_db_handle(context); 416ef31c6aaSdrh assert( nByte>0 ); 41727e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); 41827e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); 41927e62dbeSdrh if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 420bb4957f8Sdrh sqlite3_result_error_toobig(context); 421bb4957f8Sdrh z = 0; 422bb4957f8Sdrh }else{ 423da4ca9d1Sdrh z = sqlite3Malloc(nByte); 424ef31c6aaSdrh if( !z ){ 425a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 426a1644fd8Sdanielk1977 } 427bb4957f8Sdrh } 428a1644fd8Sdanielk1977 return z; 429a1644fd8Sdanielk1977 } 430a1644fd8Sdanielk1977 431dc04c583Sdrh /* 432dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 433dc04c583Sdrh */ 4340ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 4357a521cfbSdrh char *z1; 4367a521cfbSdrh const char *z2; 4379310ef23Sdrh int i, n; 4381d34fdecSdrh UNUSED_PARAMETER(argc); 4397a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4401f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4411f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4421f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4437a521cfbSdrh if( z2 ){ 444b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4457a521cfbSdrh if( z1 ){ 446df901d34Sdrh for(i=0; i<n; i++){ 447df901d34Sdrh z1[i] = (char)sqlite3Toupper(z2[i]); 448dc04c583Sdrh } 449df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4507a521cfbSdrh } 4517a521cfbSdrh } 452dc04c583Sdrh } 4530ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 454df901d34Sdrh char *z1; 4557a521cfbSdrh const char *z2; 4569310ef23Sdrh int i, n; 4571d34fdecSdrh UNUSED_PARAMETER(argc); 4587a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4591f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4601f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4611f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4627a521cfbSdrh if( z2 ){ 463b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4647a521cfbSdrh if( z1 ){ 465df901d34Sdrh for(i=0; i<n; i++){ 466df901d34Sdrh z1[i] = sqlite3Tolower(z2[i]); 467dc04c583Sdrh } 468df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4697a521cfbSdrh } 4707a521cfbSdrh } 471dc04c583Sdrh } 472dc04c583Sdrh 473ae6bb957Sdrh /* 474cca9f3d2Sdrh ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented 475cca9f3d2Sdrh ** as VDBE code so that unused argument values do not have to be computed. 476cca9f3d2Sdrh ** However, we still need some kind of function implementation for this 477cca9f3d2Sdrh ** routines in the function table. The noopFunc macro provides this. 478cca9f3d2Sdrh ** noopFunc will never be called so it doesn't matter what the implementation 479cca9f3d2Sdrh ** is. We might as well use the "version()" function as a substitute. 480ae6bb957Sdrh */ 481cca9f3d2Sdrh #define noopFunc versionFunc /* Substitute function - never called */ 4823212e182Sdrh 4833212e182Sdrh /* 484f9ffac96Sdrh ** Implementation of random(). Return a random integer. 485f9ffac96Sdrh */ 486f9b596ebSdrh static void randomFunc( 487f9b596ebSdrh sqlite3_context *context, 48862c14b34Sdanielk1977 int NotUsed, 48962c14b34Sdanielk1977 sqlite3_value **NotUsed2 490f9b596ebSdrh ){ 49152fc849aSdrh sqlite_int64 r; 49262c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 4932fa1868fSdrh sqlite3_randomness(sizeof(r), &r); 4943034e3d3Sdrh if( r<0 ){ 4953034e3d3Sdrh /* We need to prevent a random number of 0x8000000000000000 4963034e3d3Sdrh ** (or -9223372036854775808) since when you do abs() of that 4973034e3d3Sdrh ** number of you get the same value back again. To do this 4983034e3d3Sdrh ** in a way that is testable, mask the sign bit off of negative 4993034e3d3Sdrh ** values, resulting in a positive value. Then take the 5003034e3d3Sdrh ** 2s complement of that positive value. The end result can 5013034e3d3Sdrh ** therefore be no less than -9223372036854775807. 5023034e3d3Sdrh */ 503af8001bfSdrh r = -(r & LARGEST_INT64); 5043034e3d3Sdrh } 50552fc849aSdrh sqlite3_result_int64(context, r); 506f9ffac96Sdrh } 507f9ffac96Sdrh 508f9ffac96Sdrh /* 509137c728fSdrh ** Implementation of randomblob(N). Return a random blob 510137c728fSdrh ** that is N bytes long. 51163cf66f0Sdrh */ 512137c728fSdrh static void randomBlob( 51363cf66f0Sdrh sqlite3_context *context, 51463cf66f0Sdrh int argc, 51563cf66f0Sdrh sqlite3_value **argv 51663cf66f0Sdrh ){ 5173cb79202Sdrh sqlite3_int64 n; 518137c728fSdrh unsigned char *p; 51963cf66f0Sdrh assert( argc==1 ); 520f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 5213cb79202Sdrh n = sqlite3_value_int64(argv[0]); 522023ae03aSdrh if( n<1 ){ 523023ae03aSdrh n = 1; 524023ae03aSdrh } 525a1644fd8Sdanielk1977 p = contextMalloc(context, n); 52602d85836Sdrh if( p ){ 5272fa1868fSdrh sqlite3_randomness(n, p); 52817435752Sdrh sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 52902d85836Sdrh } 53063cf66f0Sdrh } 53163cf66f0Sdrh 53263cf66f0Sdrh /* 5336ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 53424b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 5356ed41ad7Sdrh */ 53651ad0ecdSdanielk1977 static void last_insert_rowid( 5370ae8b831Sdanielk1977 sqlite3_context *context, 53862c14b34Sdanielk1977 int NotUsed, 53962c14b34Sdanielk1977 sqlite3_value **NotUsed2 54051ad0ecdSdanielk1977 ){ 541fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 54262c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 543ab2f1f95Sdrh /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a 544ab2f1f95Sdrh ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface 545ab2f1f95Sdrh ** function. */ 546f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 5476ed41ad7Sdrh } 5486ed41ad7Sdrh 549f146a776Srdc /* 550ab2f1f95Sdrh ** Implementation of the changes() SQL function. 551ab2f1f95Sdrh ** 552ab2f1f95Sdrh ** IMP: R-62073-11209 The changes() SQL function is a wrapper 553ab2f1f95Sdrh ** around the sqlite3_changes() C/C++ function and hence follows the same 554ab2f1f95Sdrh ** rules for counting changes. 555f146a776Srdc */ 556b28af71aSdanielk1977 static void changes( 557f9b596ebSdrh sqlite3_context *context, 55862c14b34Sdanielk1977 int NotUsed, 55962c14b34Sdanielk1977 sqlite3_value **NotUsed2 560f9b596ebSdrh ){ 561fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 56262c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 563f4479501Sdrh sqlite3_result_int(context, sqlite3_changes(db)); 564b0c374ffSrdc } 565f146a776Srdc 566f146a776Srdc /* 567b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 568b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function. 569f146a776Srdc */ 570b28af71aSdanielk1977 static void total_changes( 5710ae8b831Sdanielk1977 sqlite3_context *context, 57262c14b34Sdanielk1977 int NotUsed, 57362c14b34Sdanielk1977 sqlite3_value **NotUsed2 57451ad0ecdSdanielk1977 ){ 575fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 57662c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 577ab2f1f95Sdrh /* IMP: R-52756-41993 This function is a wrapper around the 578ab2f1f95Sdrh ** sqlite3_total_changes() C/C++ interface. */ 579b28af71aSdanielk1977 sqlite3_result_int(context, sqlite3_total_changes(db)); 580b0c374ffSrdc } 581b0c374ffSrdc 5826ed41ad7Sdrh /* 5834e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 584d02eb1fdSdanielk1977 */ 5854e5ffc5fSdrh struct compareInfo { 58607e83472Sdrh u8 matchAll; /* "*" or "%" */ 58707e83472Sdrh u8 matchOne; /* "?" or "_" */ 58807e83472Sdrh u8 matchSet; /* "[" or 0 */ 58907e83472Sdrh u8 noCase; /* true to ignore case differences */ 590d02eb1fdSdanielk1977 }; 59155ef4d97Sdrh 592b9175aedSdrh /* 593b9175aedSdrh ** For LIKE and GLOB matching on EBCDIC machines, assume that every 594b0870486Sdrh ** character is exactly one byte in size. Also, provde the Utf8Read() 595b0870486Sdrh ** macro for fast reading of the next character in the common case where 596b0870486Sdrh ** the next character is ASCII. 597b9175aedSdrh */ 598b9175aedSdrh #if defined(SQLITE_EBCDIC) 59942610961Sdrh # define sqlite3Utf8Read(A) (*((*A)++)) 600b0870486Sdrh # define Utf8Read(A) (*(A++)) 601b9175aedSdrh #else 602b0870486Sdrh # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) 603b9175aedSdrh #endif 604b9175aedSdrh 6054e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 60670031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 60770031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 60855ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 60970031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 61070031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 61155ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 612d02eb1fdSdanielk1977 613d02eb1fdSdanielk1977 /* 614698a01caSdrh ** Possible error returns from patternMatch() 615698a01caSdrh */ 616698a01caSdrh #define SQLITE_MATCH 0 617698a01caSdrh #define SQLITE_NOMATCH 1 618698a01caSdrh #define SQLITE_NOWILDCARDMATCH 2 619698a01caSdrh 620698a01caSdrh /* 621698a01caSdrh ** Compare two UTF-8 strings for equality where the first string is 622698a01caSdrh ** a GLOB or LIKE expression. Return values: 623698a01caSdrh ** 624698a01caSdrh ** SQLITE_MATCH: Match 625698a01caSdrh ** SQLITE_NOMATCH: No match 626698a01caSdrh ** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards. 6270ac65892Sdrh ** 6284e5ffc5fSdrh ** Globbing rules: 6290ac65892Sdrh ** 6304e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 631d02eb1fdSdanielk1977 ** 6324e5ffc5fSdrh ** '?' Matches exactly one character. 6334e5ffc5fSdrh ** 6344e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 6354e5ffc5fSdrh ** characters. 6364e5ffc5fSdrh ** 6374e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 6384e5ffc5fSdrh ** 6394e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 6404e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 6414e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 6424e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 6434e5ffc5fSdrh ** it the last character in the list. 6444e5ffc5fSdrh ** 6459fdfdc89Sdrh ** Like matching rules: 6469fdfdc89Sdrh ** 6479fdfdc89Sdrh ** '%' Matches any sequence of zero or more characters 6489fdfdc89Sdrh ** 6499fdfdc89Sdrh *** '_' Matches any one character 6509fdfdc89Sdrh ** 6519fdfdc89Sdrh ** Ec Where E is the "esc" character and c is any other 6529fdfdc89Sdrh ** character, including '%', '_', and esc, match exactly c. 6539fdfdc89Sdrh ** 654b0870486Sdrh ** The comments within this routine usually assume glob matching. 6559fdfdc89Sdrh ** 6564e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 6570ac65892Sdrh */ 6587c6303c0Sdanielk1977 static int patternCompare( 6594e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 6604e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 6617c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 662698a01caSdrh u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */ 66351ad0ecdSdanielk1977 ){ 6649fdfdc89Sdrh u32 c, c2; /* Next pattern and input string chars */ 6659fdfdc89Sdrh u32 matchOne = pInfo->matchOne; /* "?" or "_" */ 6669fdfdc89Sdrh u32 matchAll = pInfo->matchAll; /* "*" or "%" */ 6679fdfdc89Sdrh u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ 6689fdfdc89Sdrh const u8 *zEscaped = 0; /* One past the last escaped input char */ 669d02eb1fdSdanielk1977 670b0870486Sdrh while( (c = Utf8Read(zPattern))!=0 ){ 6719fdfdc89Sdrh if( c==matchAll ){ /* Match "*" */ 6729fdfdc89Sdrh /* Skip over multiple "*" characters in the pattern. If there 6739fdfdc89Sdrh ** are also "?" characters, skip those as well, but consume a 6749fdfdc89Sdrh ** single character of the input string for each "?" skipped */ 675b0870486Sdrh while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ 67642610961Sdrh if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ 677698a01caSdrh return SQLITE_NOWILDCARDMATCH; 678d02eb1fdSdanielk1977 } 6794e5ffc5fSdrh } 68066150956Sdrh if( c==0 ){ 681698a01caSdrh return SQLITE_MATCH; /* "*" at the end of the pattern matches */ 68288b3322fSdrh }else if( c==matchOther ){ 68307e83472Sdrh if( pInfo->matchSet==0 ){ 68442610961Sdrh c = sqlite3Utf8Read(&zPattern); 685698a01caSdrh if( c==0 ) return SQLITE_NOWILDCARDMATCH; 68688b3322fSdrh }else{ 6879fdfdc89Sdrh /* "[...]" immediately follows the "*". We have to do a slow 6889fdfdc89Sdrh ** recursive search in this case, but it is an unusual case. */ 68988b3322fSdrh assert( matchOther<0x80 ); /* '[' is a single-byte character */ 690698a01caSdrh while( *zString ){ 691698a01caSdrh int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther); 692698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 6934a919118Sdrh SQLITE_SKIP_UTF8(zString); 6944e5ffc5fSdrh } 695698a01caSdrh return SQLITE_NOWILDCARDMATCH; 69666150956Sdrh } 69788b3322fSdrh } 6989fdfdc89Sdrh 6999fdfdc89Sdrh /* At this point variable c contains the first character of the 7009fdfdc89Sdrh ** pattern string past the "*". Search in the input string for the 7011a4a7376Sdan ** first matching character and recursively continue the match from 7029fdfdc89Sdrh ** that point. 7039fdfdc89Sdrh ** 7049fdfdc89Sdrh ** For a case-insensitive search, set variable cx to be the same as 7059fdfdc89Sdrh ** c but in the other case and search the input string for either 7069fdfdc89Sdrh ** c or cx. 7079fdfdc89Sdrh */ 7089fdfdc89Sdrh if( c<=0x80 ){ 709eba21f9eSdrh char zStop[3]; 710698a01caSdrh int bMatch; 7119fdfdc89Sdrh if( noCase ){ 712eba21f9eSdrh zStop[0] = sqlite3Toupper(c); 713eba21f9eSdrh zStop[1] = sqlite3Tolower(c); 714eba21f9eSdrh zStop[2] = 0; 715ad7dd425Sdanielk1977 }else{ 716eba21f9eSdrh zStop[0] = c; 717eba21f9eSdrh zStop[1] = 0; 71866150956Sdrh } 719eba21f9eSdrh while(1){ 720eba21f9eSdrh zString += strcspn((const char*)zString, zStop); 721eba21f9eSdrh if( zString[0]==0 ) break; 722eba21f9eSdrh zString++; 723698a01caSdrh bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 724698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 7254e5ffc5fSdrh } 72688b3322fSdrh }else{ 727698a01caSdrh int bMatch; 728b0870486Sdrh while( (c2 = Utf8Read(zString))!=0 ){ 7299fdfdc89Sdrh if( c2!=c ) continue; 730698a01caSdrh bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 731698a01caSdrh if( bMatch!=SQLITE_NOMATCH ) return bMatch; 73266150956Sdrh } 73388b3322fSdrh } 734698a01caSdrh return SQLITE_NOWILDCARDMATCH; 7359fdfdc89Sdrh } 73688b3322fSdrh if( c==matchOther ){ 73707e83472Sdrh if( pInfo->matchSet==0 ){ 73888b3322fSdrh c = sqlite3Utf8Read(&zPattern); 739698a01caSdrh if( c==0 ) return SQLITE_NOMATCH; 7409fdfdc89Sdrh zEscaped = zPattern; 74188b3322fSdrh }else{ 7421aa4f3e5Sdrh u32 prior_c = 0; 7439fdfdc89Sdrh int seen = 0; 7449fdfdc89Sdrh int invert = 0; 74542610961Sdrh c = sqlite3Utf8Read(&zString); 746698a01caSdrh if( c==0 ) return SQLITE_NOMATCH; 74742610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 74866150956Sdrh if( c2=='^' ){ 74966150956Sdrh invert = 1; 75042610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 75166150956Sdrh } 7524e5ffc5fSdrh if( c2==']' ){ 7534e5ffc5fSdrh if( c==']' ) seen = 1; 75442610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7554e5ffc5fSdrh } 75666150956Sdrh while( c2 && c2!=']' ){ 75766150956Sdrh if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ 75842610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7594e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 7604e5ffc5fSdrh prior_c = 0; 76166150956Sdrh }else{ 76266150956Sdrh if( c==c2 ){ 7634e5ffc5fSdrh seen = 1; 76466150956Sdrh } 7654e5ffc5fSdrh prior_c = c2; 766d02eb1fdSdanielk1977 } 76742610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 768d02eb1fdSdanielk1977 } 76966150956Sdrh if( c2==0 || (seen ^ invert)==0 ){ 770698a01caSdrh return SQLITE_NOMATCH; 77166150956Sdrh } 77288b3322fSdrh continue; 773328d913cSdrh } 77488b3322fSdrh } 775b0870486Sdrh c2 = Utf8Read(zString); 77688b3322fSdrh if( c==c2 ) continue; 777c80937a5Sdrh if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){ 7789fdfdc89Sdrh continue; 7799fdfdc89Sdrh } 7809fdfdc89Sdrh if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; 781698a01caSdrh return SQLITE_NOMATCH; 7820ac65892Sdrh } 783698a01caSdrh return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH; 7844e5ffc5fSdrh } 7854e5ffc5fSdrh 78655ef4d97Sdrh /* 787698a01caSdrh ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and 788698a01caSdrh ** non-zero if there is no match. 78956282a5bSdrh */ 79056282a5bSdrh int sqlite3_strglob(const char *zGlobPattern, const char *zString){ 791698a01caSdrh return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); 79256282a5bSdrh } 79356282a5bSdrh 79456282a5bSdrh /* 795698a01caSdrh ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for 796698a01caSdrh ** a miss - like strcmp(). 7978b4a94adSdrh */ 7988b4a94adSdrh int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ 799698a01caSdrh return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); 8008b4a94adSdrh } 8018b4a94adSdrh 8028b4a94adSdrh /* 80355ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 80455ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 80555ef4d97Sdrh ** only. 80655ef4d97Sdrh */ 80755ef4d97Sdrh #ifdef SQLITE_TEST 80855ef4d97Sdrh int sqlite3_like_count = 0; 80955ef4d97Sdrh #endif 81055ef4d97Sdrh 8113f6b0874Sdanielk1977 8123f6b0874Sdanielk1977 /* 8133f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 8143f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 8153f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 8163f6b0874Sdanielk1977 ** 8173f6b0874Sdanielk1977 ** A LIKE B 8183f6b0874Sdanielk1977 ** 8193f6b0874Sdanielk1977 ** is implemented as like(B,A). 8203f6b0874Sdanielk1977 ** 82155ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 82255ef4d97Sdrh ** the GLOB operator. 8233f6b0874Sdanielk1977 */ 8243f6b0874Sdanielk1977 static void likeFunc( 8253f6b0874Sdanielk1977 sqlite3_context *context, 8263f6b0874Sdanielk1977 int argc, 8273f6b0874Sdanielk1977 sqlite3_value **argv 8283f6b0874Sdanielk1977 ){ 829beb818d1Sdrh const unsigned char *zA, *zB; 83007e83472Sdrh u32 escape; 83127e62dbeSdrh int nPat; 832bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 83307e83472Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 834beb818d1Sdrh 83541d2e66eSdrh #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS 83641d2e66eSdrh if( sqlite3_value_type(argv[0])==SQLITE_BLOB 83741d2e66eSdrh || sqlite3_value_type(argv[1])==SQLITE_BLOB 83841d2e66eSdrh ){ 83941d2e66eSdrh #ifdef SQLITE_TEST 84041d2e66eSdrh sqlite3_like_count++; 84141d2e66eSdrh #endif 84241d2e66eSdrh sqlite3_result_int(context, 0); 84341d2e66eSdrh return; 84441d2e66eSdrh } 84541d2e66eSdrh #endif 8461f0feef8Sdrh 847beb818d1Sdrh /* Limit the length of the LIKE or GLOB pattern to avoid problems 848beb818d1Sdrh ** of deep recursion and N*N behavior in patternCompare(). 849beb818d1Sdrh */ 85027e62dbeSdrh nPat = sqlite3_value_bytes(argv[0]); 85127e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); 85227e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); 85327e62dbeSdrh if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ 854beb818d1Sdrh sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); 855beb818d1Sdrh return; 856beb818d1Sdrh } 8577c6303c0Sdanielk1977 if( argc==3 ){ 8587c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 8597c6303c0Sdanielk1977 ** Otherwise, return an error. 8607c6303c0Sdanielk1977 */ 8617c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 8627a521cfbSdrh if( zEsc==0 ) return; 863ee85813cSdrh if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ 8647c6303c0Sdanielk1977 sqlite3_result_error(context, 8657c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 8667c6303c0Sdanielk1977 return; 8677c6303c0Sdanielk1977 } 86842610961Sdrh escape = sqlite3Utf8Read(&zEsc); 86907e83472Sdrh }else{ 87007e83472Sdrh escape = pInfo->matchSet; 8717c6303c0Sdanielk1977 } 872cf833239Sdrh zB = sqlite3_value_text(argv[0]); 873cf833239Sdrh zA = sqlite3_value_text(argv[1]); 8743f6b0874Sdanielk1977 if( zA && zB ){ 87555ef4d97Sdrh #ifdef SQLITE_TEST 87655ef4d97Sdrh sqlite3_like_count++; 87755ef4d97Sdrh #endif 878f49759bfSdrh sqlite3_result_int(context, 879f49759bfSdrh patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH); 88051ad0ecdSdanielk1977 } 8818912d106Sdrh } 8828912d106Sdrh 8838912d106Sdrh /* 8848912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 8858912d106Sdrh ** argument if the arguments are different. The result is NULL if the 8868912d106Sdrh ** arguments are equal to each other. 8878912d106Sdrh */ 888f9b596ebSdrh static void nullifFunc( 889f9b596ebSdrh sqlite3_context *context, 89062c14b34Sdanielk1977 int NotUsed, 891f9b596ebSdrh sqlite3_value **argv 892f9b596ebSdrh ){ 893dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 89462c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 895dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 896f4479501Sdrh sqlite3_result_value(context, argv[0]); 8978912d106Sdrh } 8980ac65892Sdrh } 8990ac65892Sdrh 900647cb0e1Sdrh /* 90147baebc2Sdrh ** Implementation of the sqlite_version() function. The result is the version 902647cb0e1Sdrh ** of the SQLite library that is running. 903647cb0e1Sdrh */ 904f9b596ebSdrh static void versionFunc( 905f9b596ebSdrh sqlite3_context *context, 90662c14b34Sdanielk1977 int NotUsed, 90762c14b34Sdanielk1977 sqlite3_value **NotUsed2 908f9b596ebSdrh ){ 90962c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 910ab2f1f95Sdrh /* IMP: R-48699-48617 This function is an SQL wrapper around the 911ab2f1f95Sdrh ** sqlite3_libversion() C-interface. */ 912ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); 913647cb0e1Sdrh } 914647cb0e1Sdrh 91547baebc2Sdrh /* 91647baebc2Sdrh ** Implementation of the sqlite_source_id() function. The result is a string 91747baebc2Sdrh ** that identifies the particular version of the source code used to build 91847baebc2Sdrh ** SQLite. 91947baebc2Sdrh */ 92047baebc2Sdrh static void sourceidFunc( 92147baebc2Sdrh sqlite3_context *context, 92247baebc2Sdrh int NotUsed, 92347baebc2Sdrh sqlite3_value **NotUsed2 92447baebc2Sdrh ){ 92547baebc2Sdrh UNUSED_PARAMETER2(NotUsed, NotUsed2); 926ab2f1f95Sdrh /* IMP: R-24470-31136 This function is an SQL wrapper around the 927ab2f1f95Sdrh ** sqlite3_sourceid() C interface. */ 928ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); 92947baebc2Sdrh } 93047baebc2Sdrh 931bdea6d13Sshaneh /* 9323ca84ef6Sdrh ** Implementation of the sqlite_log() function. This is a wrapper around 9333ca84ef6Sdrh ** sqlite3_log(). The return value is NULL. The function exists purely for 9343ca84ef6Sdrh ** its side-effects. 9353ca84ef6Sdrh */ 936840561f2Sdrh static void errlogFunc( 9373ca84ef6Sdrh sqlite3_context *context, 9383ca84ef6Sdrh int argc, 9393ca84ef6Sdrh sqlite3_value **argv 9403ca84ef6Sdrh ){ 9413ca84ef6Sdrh UNUSED_PARAMETER(argc); 9423ca84ef6Sdrh UNUSED_PARAMETER(context); 9433ca84ef6Sdrh sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); 9443ca84ef6Sdrh } 9453ca84ef6Sdrh 9463ca84ef6Sdrh /* 947dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_used() function. 948dc97a8cdSshaneh ** The result is an integer that identifies if the compiler option 949dc97a8cdSshaneh ** was used to build SQLite. 950bdea6d13Sshaneh */ 951dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 952dc97a8cdSshaneh static void compileoptionusedFunc( 953bdea6d13Sshaneh sqlite3_context *context, 954dc97a8cdSshaneh int argc, 955dc97a8cdSshaneh sqlite3_value **argv 956bdea6d13Sshaneh ){ 957dc97a8cdSshaneh const char *zOptName; 958dc97a8cdSshaneh assert( argc==1 ); 959dc97a8cdSshaneh UNUSED_PARAMETER(argc); 960a3e414cdSdrh /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL 961a3e414cdSdrh ** function is a wrapper around the sqlite3_compileoption_used() C/C++ 962a3e414cdSdrh ** function. 963a3e414cdSdrh */ 964264a2d4dSdrh if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 965dc97a8cdSshaneh sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); 966bdea6d13Sshaneh } 967dc97a8cdSshaneh } 968dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 969dc97a8cdSshaneh 970dc97a8cdSshaneh /* 971dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_get() function. 972dc97a8cdSshaneh ** The result is a string that identifies the compiler options 973dc97a8cdSshaneh ** used to build SQLite. 974dc97a8cdSshaneh */ 975dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 976dc97a8cdSshaneh static void compileoptiongetFunc( 977dc97a8cdSshaneh sqlite3_context *context, 978dc97a8cdSshaneh int argc, 979dc97a8cdSshaneh sqlite3_value **argv 980dc97a8cdSshaneh ){ 981dc97a8cdSshaneh int n; 982dc97a8cdSshaneh assert( argc==1 ); 983dc97a8cdSshaneh UNUSED_PARAMETER(argc); 984a3e414cdSdrh /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function 985a3e414cdSdrh ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. 986a3e414cdSdrh */ 987dc97a8cdSshaneh n = sqlite3_value_int(argv[0]); 988dc97a8cdSshaneh sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); 989dc97a8cdSshaneh } 990dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 991bdea6d13Sshaneh 992137c728fSdrh /* Array for converting from half-bytes (nybbles) into ASCII hex 993137c728fSdrh ** digits. */ 994137c728fSdrh static const char hexdigits[] = { 995137c728fSdrh '0', '1', '2', '3', '4', '5', '6', '7', 996137c728fSdrh '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 997137c728fSdrh }; 998d641d646Sdanielk1977 99947394703Sdrh /* 100047394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 100147394703Sdrh ** argument. If the argument is numeric, the return value is the same as 100247394703Sdrh ** the argument. If the argument is NULL, the return value is the string 100347394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 100447394703Sdrh ** single-quote escapes. 100547394703Sdrh */ 10060ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1007a0df4ccfSdrh assert( argc==1 ); 10081d34fdecSdrh UNUSED_PARAMETER(argc); 1009f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 10109c054830Sdrh case SQLITE_FLOAT: { 101172b3fbc7Sdrh double r1, r2; 101272b3fbc7Sdrh char zBuf[50]; 10132b434a7eSmistachkin r1 = sqlite3_value_double(argv[0]); 101472b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); 101572b3fbc7Sdrh sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); 101672b3fbc7Sdrh if( r1!=r2 ){ 101772b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); 101872b3fbc7Sdrh } 101972b3fbc7Sdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 102072b3fbc7Sdrh break; 102172b3fbc7Sdrh } 102272b3fbc7Sdrh case SQLITE_INTEGER: { 1023f4479501Sdrh sqlite3_result_value(context, argv[0]); 1024f9b596ebSdrh break; 1025f9b596ebSdrh } 10263f41e976Sdanielk1977 case SQLITE_BLOB: { 10273f41e976Sdanielk1977 char *zText = 0; 10283f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 10291f0feef8Sdrh int nBlob = sqlite3_value_bytes(argv[0]); 10301f0feef8Sdrh assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1031b1a6c3c1Sdrh zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 1032a1644fd8Sdanielk1977 if( zText ){ 10333f41e976Sdanielk1977 int i; 10343f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 10353f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 10363f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 10373f41e976Sdanielk1977 } 10383f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 10393f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 10403f41e976Sdanielk1977 zText[0] = 'X'; 10413f41e976Sdanielk1977 zText[1] = '\''; 1042d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 104317435752Sdrh sqlite3_free(zText); 10443f41e976Sdanielk1977 } 10453f41e976Sdanielk1977 break; 10463f41e976Sdanielk1977 } 10479c054830Sdrh case SQLITE_TEXT: { 1048023ae03aSdrh int i,j; 1049023ae03aSdrh u64 n; 10502646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 105147394703Sdrh char *z; 1052f9b596ebSdrh 10537a521cfbSdrh if( zArg==0 ) return; 1054023ae03aSdrh for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 1055b1a6c3c1Sdrh z = contextMalloc(context, ((i64)i)+((i64)n)+3); 1056a1644fd8Sdanielk1977 if( z ){ 105747394703Sdrh z[0] = '\''; 105851ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 105951ad0ecdSdanielk1977 z[j++] = zArg[i]; 106051ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 106147394703Sdrh z[j++] = '\''; 106247394703Sdrh } 106347394703Sdrh } 106447394703Sdrh z[j++] = '\''; 106547394703Sdrh z[j] = 0; 1066a1644fd8Sdanielk1977 sqlite3_result_text(context, z, j, sqlite3_free); 1067a1644fd8Sdanielk1977 } 1068a0df4ccfSdrh break; 1069a0df4ccfSdrh } 1070a0df4ccfSdrh default: { 1071a0df4ccfSdrh assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 1072a0df4ccfSdrh sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 1073a0df4ccfSdrh break; 107447394703Sdrh } 107547394703Sdrh } 1076f9b596ebSdrh } 107747394703Sdrh 1078137c728fSdrh /* 1079d495d8c9Sdrh ** The unicode() function. Return the integer unicode code-point value 1080d495d8c9Sdrh ** for the first character of the input string. 1081d495d8c9Sdrh */ 1082d495d8c9Sdrh static void unicodeFunc( 1083d495d8c9Sdrh sqlite3_context *context, 1084d495d8c9Sdrh int argc, 1085d495d8c9Sdrh sqlite3_value **argv 1086d495d8c9Sdrh ){ 1087d495d8c9Sdrh const unsigned char *z = sqlite3_value_text(argv[0]); 10881d59d036Sdrh (void)argc; 1089d495d8c9Sdrh if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); 1090d495d8c9Sdrh } 1091d495d8c9Sdrh 1092d495d8c9Sdrh /* 1093d495d8c9Sdrh ** The char() function takes zero or more arguments, each of which is 1094d495d8c9Sdrh ** an integer. It constructs a string where each character of the string 1095d495d8c9Sdrh ** is the unicode character for the corresponding integer argument. 1096d495d8c9Sdrh */ 1097d495d8c9Sdrh static void charFunc( 1098d495d8c9Sdrh sqlite3_context *context, 1099d495d8c9Sdrh int argc, 1100d495d8c9Sdrh sqlite3_value **argv 1101d495d8c9Sdrh ){ 1102d495d8c9Sdrh unsigned char *z, *zOut; 1103d495d8c9Sdrh int i; 1104f3cdcdccSdrh zOut = z = sqlite3_malloc64( argc*4+1 ); 1105d495d8c9Sdrh if( z==0 ){ 1106d495d8c9Sdrh sqlite3_result_error_nomem(context); 1107d495d8c9Sdrh return; 1108d495d8c9Sdrh } 1109d495d8c9Sdrh for(i=0; i<argc; i++){ 1110c9545442Smistachkin sqlite3_int64 x; 1111d495d8c9Sdrh unsigned c; 1112d495d8c9Sdrh x = sqlite3_value_int64(argv[i]); 1113d495d8c9Sdrh if( x<0 || x>0x10ffff ) x = 0xfffd; 1114d495d8c9Sdrh c = (unsigned)(x & 0x1fffff); 1115fe7a5d11Sdrh if( c<0x00080 ){ 1116fe7a5d11Sdrh *zOut++ = (u8)(c&0xFF); 1117fe7a5d11Sdrh }else if( c<0x00800 ){ 1118fe7a5d11Sdrh *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); 1119fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1120fe7a5d11Sdrh }else if( c<0x10000 ){ 1121fe7a5d11Sdrh *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); 1122fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1123fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1124d495d8c9Sdrh }else{ 1125fe7a5d11Sdrh *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); 1126fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); 1127fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1128fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1129fe7a5d11Sdrh } \ 1130d495d8c9Sdrh } 1131bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); 1132d495d8c9Sdrh } 1133d495d8c9Sdrh 1134d495d8c9Sdrh /* 1135137c728fSdrh ** The hex() function. Interpret the argument as a blob. Return 1136137c728fSdrh ** a hexadecimal rendering as text. 1137137c728fSdrh */ 1138137c728fSdrh static void hexFunc( 1139137c728fSdrh sqlite3_context *context, 1140137c728fSdrh int argc, 1141137c728fSdrh sqlite3_value **argv 1142137c728fSdrh ){ 1143137c728fSdrh int i, n; 1144137c728fSdrh const unsigned char *pBlob; 1145137c728fSdrh char *zHex, *z; 1146137c728fSdrh assert( argc==1 ); 1147f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 11481f0feef8Sdrh pBlob = sqlite3_value_blob(argv[0]); 1149137c728fSdrh n = sqlite3_value_bytes(argv[0]); 11501f0feef8Sdrh assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1151b1a6c3c1Sdrh z = zHex = contextMalloc(context, ((i64)n)*2 + 1); 1152a1644fd8Sdanielk1977 if( zHex ){ 1153137c728fSdrh for(i=0; i<n; i++, pBlob++){ 1154137c728fSdrh unsigned char c = *pBlob; 1155137c728fSdrh *(z++) = hexdigits[(c>>4)&0xf]; 1156137c728fSdrh *(z++) = hexdigits[c&0xf]; 1157137c728fSdrh } 1158137c728fSdrh *z = 0; 1159137c728fSdrh sqlite3_result_text(context, zHex, n*2, sqlite3_free); 1160137c728fSdrh } 1161a1644fd8Sdanielk1977 } 1162137c728fSdrh 116326b6d90dSdrh /* 11648cff382eSdrh ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 11658cff382eSdrh */ 11668cff382eSdrh static void zeroblobFunc( 11678cff382eSdrh sqlite3_context *context, 11688cff382eSdrh int argc, 11698cff382eSdrh sqlite3_value **argv 11708cff382eSdrh ){ 117198640a3fSdrh i64 n; 1172a4d5ae8fSdan int rc; 11738cff382eSdrh assert( argc==1 ); 1174f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 117598640a3fSdrh n = sqlite3_value_int64(argv[0]); 117653e66c3cSdrh if( n<0 ) n = 0; 1177a4d5ae8fSdan rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ 1178a4d5ae8fSdan if( rc ){ 1179a4d5ae8fSdan sqlite3_result_error_code(context, rc); 11808cff382eSdrh } 118198640a3fSdrh } 11828cff382eSdrh 11838cff382eSdrh /* 118426b6d90dSdrh ** The replace() function. Three arguments are all strings: call 118526b6d90dSdrh ** them A, B, and C. The result is also a string which is derived 1186f7b5496eSdrh ** from A by replacing every occurrence of B with C. The match 118726b6d90dSdrh ** must be exact. Collating sequences are not used. 118826b6d90dSdrh */ 118926b6d90dSdrh static void replaceFunc( 119026b6d90dSdrh sqlite3_context *context, 119126b6d90dSdrh int argc, 119226b6d90dSdrh sqlite3_value **argv 119326b6d90dSdrh ){ 119426b6d90dSdrh const unsigned char *zStr; /* The input string A */ 119526b6d90dSdrh const unsigned char *zPattern; /* The pattern string B */ 119626b6d90dSdrh const unsigned char *zRep; /* The replacement string C */ 119726b6d90dSdrh unsigned char *zOut; /* The output */ 119826b6d90dSdrh int nStr; /* Size of zStr */ 119926b6d90dSdrh int nPattern; /* Size of zPattern */ 120026b6d90dSdrh int nRep; /* Size of zRep */ 12012e6400baSdrh i64 nOut; /* Maximum size of zOut */ 120226b6d90dSdrh int loopLimit; /* Last zStr[] that might match zPattern[] */ 120326b6d90dSdrh int i, j; /* Loop counters */ 1204f3139520Sdrh unsigned cntExpand; /* Number zOut expansions */ 1205f3139520Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 120626b6d90dSdrh 120726b6d90dSdrh assert( argc==3 ); 1208f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 120926b6d90dSdrh zStr = sqlite3_value_text(argv[0]); 12107a521cfbSdrh if( zStr==0 ) return; 12111f0feef8Sdrh nStr = sqlite3_value_bytes(argv[0]); 12121f0feef8Sdrh assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ 121326b6d90dSdrh zPattern = sqlite3_value_text(argv[1]); 1214a605fe8dSdrh if( zPattern==0 ){ 12152333606eSdrh assert( sqlite3_value_type(argv[1])==SQLITE_NULL 12162333606eSdrh || sqlite3_context_db_handle(context)->mallocFailed ); 1217a605fe8dSdrh return; 1218a605fe8dSdrh } 1219a605fe8dSdrh if( zPattern[0]==0 ){ 1220a605fe8dSdrh assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); 1221a605fe8dSdrh sqlite3_result_value(context, argv[0]); 1222a605fe8dSdrh return; 1223a605fe8dSdrh } 12241f0feef8Sdrh nPattern = sqlite3_value_bytes(argv[1]); 12251f0feef8Sdrh assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ 122626b6d90dSdrh zRep = sqlite3_value_text(argv[2]); 12277a521cfbSdrh if( zRep==0 ) return; 12281f0feef8Sdrh nRep = sqlite3_value_bytes(argv[2]); 12291f0feef8Sdrh assert( zRep==sqlite3_value_text(argv[2]) ); 12302e6400baSdrh nOut = nStr + 1; 12312e6400baSdrh assert( nOut<SQLITE_MAX_LENGTH ); 1232b1a6c3c1Sdrh zOut = contextMalloc(context, (i64)nOut); 12332e6400baSdrh if( zOut==0 ){ 12342e6400baSdrh return; 123526b6d90dSdrh } 123626b6d90dSdrh loopLimit = nStr - nPattern; 1237f3139520Sdrh cntExpand = 0; 123826b6d90dSdrh for(i=j=0; i<=loopLimit; i++){ 123926b6d90dSdrh if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 124026b6d90dSdrh zOut[j++] = zStr[i]; 124126b6d90dSdrh }else{ 1242f3139520Sdrh if( nRep>nPattern ){ 12432e6400baSdrh nOut += nRep - nPattern; 1244c86d82f2Sdrh testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); 1245c86d82f2Sdrh testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); 124627e62dbeSdrh if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1247a0206bc8Sdrh sqlite3_result_error_toobig(context); 1248b975598eSdrh sqlite3_free(zOut); 124917374e8fSdanielk1977 return; 125017374e8fSdanielk1977 } 1251f3139520Sdrh cntExpand++; 1252f3139520Sdrh if( (cntExpand&(cntExpand-1))==0 ){ 1253f3139520Sdrh /* Grow the size of the output buffer only on substitutions 1254f3139520Sdrh ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ 1255f3139520Sdrh u8 *zOld; 12564a50aac5Sdrh zOld = zOut; 1257f3139520Sdrh zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1)); 12582e6400baSdrh if( zOut==0 ){ 1259a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 1260b975598eSdrh sqlite3_free(zOld); 12612e6400baSdrh return; 12622e6400baSdrh } 1263f3139520Sdrh } 1264f3139520Sdrh } 126526b6d90dSdrh memcpy(&zOut[j], zRep, nRep); 126626b6d90dSdrh j += nRep; 126726b6d90dSdrh i += nPattern-1; 126826b6d90dSdrh } 126926b6d90dSdrh } 1270f3139520Sdrh assert( j+nStr-i+1<=nOut ); 127126b6d90dSdrh memcpy(&zOut[j], &zStr[i], nStr-i); 127226b6d90dSdrh j += nStr - i; 127326b6d90dSdrh assert( j<=nOut ); 127426b6d90dSdrh zOut[j] = 0; 127526b6d90dSdrh sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 127626b6d90dSdrh } 127726b6d90dSdrh 1278309b3386Sdrh /* 1279309b3386Sdrh ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 1280309b3386Sdrh ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 1281309b3386Sdrh */ 1282309b3386Sdrh static void trimFunc( 1283309b3386Sdrh sqlite3_context *context, 1284309b3386Sdrh int argc, 1285309b3386Sdrh sqlite3_value **argv 1286309b3386Sdrh ){ 1287309b3386Sdrh const unsigned char *zIn; /* Input string */ 1288309b3386Sdrh const unsigned char *zCharSet; /* Set of characters to trim */ 1289309b3386Sdrh int nIn; /* Number of bytes in input */ 12907209c697Sdrh int flags; /* 1: trimleft 2: trimright 3: trim */ 1291d1e3a616Sdrh int i; /* Loop counter */ 12921bd10f8aSdrh unsigned char *aLen = 0; /* Length of each character in zCharSet */ 12931bd10f8aSdrh unsigned char **azChar = 0; /* Individual characters in zCharSet */ 1294d1e3a616Sdrh int nChar; /* Number of characters in zCharSet */ 1295d1e3a616Sdrh 1296309b3386Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1297309b3386Sdrh return; 1298309b3386Sdrh } 1299309b3386Sdrh zIn = sqlite3_value_text(argv[0]); 13007a521cfbSdrh if( zIn==0 ) return; 13011f0feef8Sdrh nIn = sqlite3_value_bytes(argv[0]); 13021f0feef8Sdrh assert( zIn==sqlite3_value_text(argv[0]) ); 1303309b3386Sdrh if( argc==1 ){ 1304d1e3a616Sdrh static const unsigned char lenOne[] = { 1 }; 1305a4de4532Sdanielk1977 static unsigned char * const azOne[] = { (u8*)" " }; 1306d1e3a616Sdrh nChar = 1; 13078cff382eSdrh aLen = (u8*)lenOne; 1308bc67da48Sdanielk1977 azChar = (unsigned char **)azOne; 1309d1e3a616Sdrh zCharSet = 0; 13107a521cfbSdrh }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 1311309b3386Sdrh return; 1312d1e3a616Sdrh }else{ 1313d1e3a616Sdrh const unsigned char *z; 1314d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 13154a919118Sdrh SQLITE_SKIP_UTF8(z); 1316309b3386Sdrh } 1317d1e3a616Sdrh if( nChar>0 ){ 1318b1a6c3c1Sdrh azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); 1319d1e3a616Sdrh if( azChar==0 ){ 1320d1e3a616Sdrh return; 1321d1e3a616Sdrh } 1322d1e3a616Sdrh aLen = (unsigned char*)&azChar[nChar]; 1323d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 1324bc67da48Sdanielk1977 azChar[nChar] = (unsigned char *)z; 13254a919118Sdrh SQLITE_SKIP_UTF8(z); 13263abbd39aSdrh aLen[nChar] = (u8)(z - azChar[nChar]); 1327d1e3a616Sdrh } 1328d1e3a616Sdrh } 1329d1e3a616Sdrh } 1330d1e3a616Sdrh if( nChar>0 ){ 13311fc4129dSshane flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); 1332309b3386Sdrh if( flags & 1 ){ 1333d1e3a616Sdrh while( nIn>0 ){ 13341bd10f8aSdrh int len = 0; 1335d1e3a616Sdrh for(i=0; i<nChar; i++){ 1336d1e3a616Sdrh len = aLen[i]; 133727e62dbeSdrh if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break; 1338d1e3a616Sdrh } 1339d1e3a616Sdrh if( i>=nChar ) break; 1340d1e3a616Sdrh zIn += len; 1341d1e3a616Sdrh nIn -= len; 1342309b3386Sdrh } 1343309b3386Sdrh } 1344309b3386Sdrh if( flags & 2 ){ 1345d1e3a616Sdrh while( nIn>0 ){ 13461bd10f8aSdrh int len = 0; 1347d1e3a616Sdrh for(i=0; i<nChar; i++){ 1348d1e3a616Sdrh len = aLen[i]; 1349d1e3a616Sdrh if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 1350309b3386Sdrh } 1351d1e3a616Sdrh if( i>=nChar ) break; 1352d1e3a616Sdrh nIn -= len; 1353d1e3a616Sdrh } 1354d1e3a616Sdrh } 1355d1e3a616Sdrh if( zCharSet ){ 1356d1e3a616Sdrh sqlite3_free(azChar); 1357309b3386Sdrh } 1358309b3386Sdrh } 1359309b3386Sdrh sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 1360309b3386Sdrh } 136126b6d90dSdrh 1362a4de4532Sdanielk1977 1363cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 1364cc15313cSdrh /* 1365cc15313cSdrh ** The "unknown" function is automatically substituted in place of 1366cc15313cSdrh ** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN 1367cc15313cSdrh ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used. 1368cc15313cSdrh ** When the "sqlite3" command-line shell is built using this functionality, 1369cc15313cSdrh ** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries 1370cc15313cSdrh ** involving application-defined functions to be examined in a generic 1371cc15313cSdrh ** sqlite3 shell. 1372cc15313cSdrh */ 1373cc15313cSdrh static void unknownFunc( 1374cc15313cSdrh sqlite3_context *context, 1375cc15313cSdrh int argc, 1376cc15313cSdrh sqlite3_value **argv 1377cc15313cSdrh ){ 1378cc15313cSdrh /* no-op */ 1379cc15313cSdrh } 1380cc15313cSdrh #endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/ 1381cc15313cSdrh 1382cc15313cSdrh 13832ba3cccfSdrh /* IMP: R-25361-16150 This function is omitted from SQLite by default. It 13842ba3cccfSdrh ** is only available if the SQLITE_SOUNDEX compile-time option is used 13852ba3cccfSdrh ** when SQLite is built. 13862ba3cccfSdrh */ 1387d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1388d24cc427Sdrh /* 1389d24cc427Sdrh ** Compute the soundex encoding of a word. 13902ba3cccfSdrh ** 13912ba3cccfSdrh ** IMP: R-59782-00072 The soundex(X) function returns a string that is the 13922ba3cccfSdrh ** soundex encoding of the string X. 1393d24cc427Sdrh */ 1394137c728fSdrh static void soundexFunc( 1395137c728fSdrh sqlite3_context *context, 1396137c728fSdrh int argc, 1397137c728fSdrh sqlite3_value **argv 1398137c728fSdrh ){ 1399d24cc427Sdrh char zResult[8]; 14004c755c0fSdrh const u8 *zIn; 1401d24cc427Sdrh int i, j; 1402d24cc427Sdrh static const unsigned char iCode[] = { 1403d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1404d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1405d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1406d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1407d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1408d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1409d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1410d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1411d24cc427Sdrh }; 1412d24cc427Sdrh assert( argc==1 ); 14134c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 1414bdf67e0eSdrh if( zIn==0 ) zIn = (u8*)""; 1415dc86e2b2Sdrh for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} 1416d24cc427Sdrh if( zIn[i] ){ 1417bdf67e0eSdrh u8 prevcode = iCode[zIn[i]&0x7f]; 141878ca0e7eSdanielk1977 zResult[0] = sqlite3Toupper(zIn[i]); 1419d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 1420d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 1421d24cc427Sdrh if( code>0 ){ 1422bdf67e0eSdrh if( code!=prevcode ){ 1423bdf67e0eSdrh prevcode = code; 1424d24cc427Sdrh zResult[j++] = code + '0'; 1425d24cc427Sdrh } 1426bdf67e0eSdrh }else{ 1427bdf67e0eSdrh prevcode = 0; 1428bdf67e0eSdrh } 1429d24cc427Sdrh } 1430d24cc427Sdrh while( j<4 ){ 1431d24cc427Sdrh zResult[j++] = '0'; 1432d24cc427Sdrh } 1433d24cc427Sdrh zResult[j] = 0; 1434d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 1435d24cc427Sdrh }else{ 14362ba3cccfSdrh /* IMP: R-64894-50321 The string "?000" is returned if the argument 14372ba3cccfSdrh ** is NULL or contains no ASCII alphabetic characters. */ 1438d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 1439d24cc427Sdrh } 1440d24cc427Sdrh } 14412ba3cccfSdrh #endif /* SQLITE_SOUNDEX */ 1442d24cc427Sdrh 1443fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 1444fdb83b2fSdrh /* 1445fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL. 1446fdb83b2fSdrh */ 1447fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 144865fd59f7Sdanielk1977 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 14497a521cfbSdrh const char *zProc; 1450fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1451fdb83b2fSdrh char *zErrMsg = 0; 1452fdb83b2fSdrh 1453191dd061Sdrh /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc 14541a55dedfSdrh ** flag is set. See the sqlite3_enable_load_extension() API. 14551a55dedfSdrh */ 1456f602a161Sdrh if( (db->flags & SQLITE_LoadExtFunc)==0 ){ 1457f602a161Sdrh sqlite3_result_error(context, "not authorized", -1); 1458f602a161Sdrh return; 1459f602a161Sdrh } 14601a55dedfSdrh 1461fdb83b2fSdrh if( argc==2 ){ 146265fd59f7Sdanielk1977 zProc = (const char *)sqlite3_value_text(argv[1]); 14637a521cfbSdrh }else{ 14647a521cfbSdrh zProc = 0; 1465fdb83b2fSdrh } 14667a521cfbSdrh if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 1467fdb83b2fSdrh sqlite3_result_error(context, zErrMsg, -1); 1468fdb83b2fSdrh sqlite3_free(zErrMsg); 1469fdb83b2fSdrh } 1470fdb83b2fSdrh } 1471fdb83b2fSdrh #endif 1472fdb83b2fSdrh 147301427a62Sdanielk1977 14740ac65892Sdrh /* 1475d3a149efSdrh ** An instance of the following structure holds the context of a 1476dd5baa95Sdrh ** sum() or avg() aggregate computation. 1477dd5baa95Sdrh */ 1478dd5baa95Sdrh typedef struct SumCtx SumCtx; 1479dd5baa95Sdrh struct SumCtx { 14808c08e861Sdrh double rSum; /* Floating point sum */ 14818c08e861Sdrh i64 iSum; /* Integer sum */ 1482cf85a51cSdrh i64 cnt; /* Number of elements summed */ 14838c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 14848c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 1485dd5baa95Sdrh }; 1486dd5baa95Sdrh 1487dd5baa95Sdrh /* 1488a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 1489a97fdd3bSdrh ** 1490a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 1491a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 1492a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 1493a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 1494c806d857Sdrh ** value. TOTAL never fails, but SUM might through an exception if 1495c806d857Sdrh ** it overflows an integer. 1496dd5baa95Sdrh */ 14970ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1498dd5baa95Sdrh SumCtx *p; 14993d1d95e6Sdrh int type; 15003f219f46Sdrh assert( argc==1 ); 1501f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 15024f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 150329d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 15043d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 1505739105c7Sdrh p->cnt++; 150629d72108Sdrh if( type==SQLITE_INTEGER ){ 15078c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 15088c08e861Sdrh p->rSum += v; 1509158b9cb9Sdrh if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ 1510a546ef21Sdrh p->approx = p->overflow = 1; 151129d72108Sdrh } 151229d72108Sdrh }else{ 15138c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 151429d72108Sdrh p->approx = 1; 15153f219f46Sdrh } 1516739105c7Sdrh } 1517dd5baa95Sdrh } 151867a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1519c3a20c19Sdan static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){ 1520c3a20c19Sdan SumCtx *p; 1521c3a20c19Sdan int type; 1522c3a20c19Sdan assert( argc==1 ); 1523c3a20c19Sdan UNUSED_PARAMETER(argc); 1524c3a20c19Sdan p = sqlite3_aggregate_context(context, sizeof(*p)); 1525c3a20c19Sdan type = sqlite3_value_numeric_type(argv[0]); 1526fd4b7285Sdrh /* p is always non-NULL because sumStep() will have been called first 1527fd4b7285Sdrh ** to initialize it */ 1528fd4b7285Sdrh if( ALWAYS(p) && type!=SQLITE_NULL ){ 1529a546ef21Sdrh assert( p->cnt>0 ); 1530c3a20c19Sdan p->cnt--; 1531a546ef21Sdrh assert( type==SQLITE_INTEGER || p->approx ); 1532a546ef21Sdrh if( type==SQLITE_INTEGER && p->approx==0 ){ 1533c3a20c19Sdan i64 v = sqlite3_value_int64(argv[0]); 1534c3a20c19Sdan p->rSum -= v; 1535a546ef21Sdrh p->iSum -= v; 1536c3a20c19Sdan }else{ 1537d736829eSdan p->rSum -= sqlite3_value_double(argv[0]); 1538c3a20c19Sdan } 1539c3a20c19Sdan } 1540c3a20c19Sdan } 154167a9b8edSdan #else 154267a9b8edSdan # define sumInverse 0 154367a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 15440ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 1545dd5baa95Sdrh SumCtx *p; 1546abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1547c2bd913aSdrh if( p && p->cnt>0 ){ 15488c08e861Sdrh if( p->overflow ){ 15498c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 15508c08e861Sdrh }else if( p->approx ){ 15518c08e861Sdrh sqlite3_result_double(context, p->rSum); 1552c2bd913aSdrh }else{ 15538c08e861Sdrh sqlite3_result_int64(context, p->iSum); 15543d1d95e6Sdrh } 1555dd5baa95Sdrh } 1556c2bd913aSdrh } 15570ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 1558dd5baa95Sdrh SumCtx *p; 1559abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1560739105c7Sdrh if( p && p->cnt>0 ){ 15618c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 1562dd5baa95Sdrh } 1563dd5baa95Sdrh } 1564a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 1565a97fdd3bSdrh SumCtx *p; 1566a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 1567fbd60f82Sshane /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 1568fbd60f82Sshane sqlite3_result_double(context, p ? p->rSum : (double)0); 1569a97fdd3bSdrh } 1570dd5baa95Sdrh 1571dd5baa95Sdrh /* 15720bce8354Sdrh ** The following structure keeps track of state information for the 15730bce8354Sdrh ** count() aggregate function. 15740bce8354Sdrh */ 15750bce8354Sdrh typedef struct CountCtx CountCtx; 15760bce8354Sdrh struct CountCtx { 1577fc6ad39cSdrh i64 n; 15787262ca94Sdan #ifdef SQLITE_DEBUG 15797262ca94Sdan int bInverse; /* True if xInverse() ever called */ 15807262ca94Sdan #endif 15810bce8354Sdrh }; 1582dd5baa95Sdrh 15830bce8354Sdrh /* 15840bce8354Sdrh ** Routines to implement the count() aggregate function. 15850bce8354Sdrh */ 15860ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 15870bce8354Sdrh CountCtx *p; 15884f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 15899c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 15900bce8354Sdrh p->n++; 15910bce8354Sdrh } 15922e79c3d5Sdrh 1593d3264c7cSdrh #ifndef SQLITE_OMIT_DEPRECATED 15942e79c3d5Sdrh /* The sqlite3_aggregate_count() function is deprecated. But just to make 15952e79c3d5Sdrh ** sure it still operates correctly, verify that its count agrees with our 15962e79c3d5Sdrh ** internal count when using count(*) and when the total count can be 15972e79c3d5Sdrh ** expressed as a 32-bit integer. */ 15987262ca94Sdan assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse 15992e79c3d5Sdrh || p->n==sqlite3_aggregate_count(context) ); 1600d3264c7cSdrh #endif 16010bce8354Sdrh } 16020ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 16030bce8354Sdrh CountCtx *p; 1604abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1605fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 16060bce8354Sdrh } 16077262ca94Sdan #ifndef SQLITE_OMIT_WINDOWFUNC 16087262ca94Sdan static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){ 16097262ca94Sdan CountCtx *p; 16107262ca94Sdan p = sqlite3_aggregate_context(ctx, sizeof(*p)); 1611fd4b7285Sdrh /* p is always non-NULL since countStep() will have been called first */ 1612fd4b7285Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){ 16137262ca94Sdan p->n--; 16147262ca94Sdan #ifdef SQLITE_DEBUG 16157262ca94Sdan p->bInverse = 1; 16167262ca94Sdan #endif 16177262ca94Sdan } 16187262ca94Sdan } 16196b4b8820Sdan #else 16206b4b8820Sdan # define countInverse 0 16216b4b8820Sdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 16220bce8354Sdrh 16230bce8354Sdrh /* 16240bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 16250bce8354Sdrh */ 162662c14b34Sdanielk1977 static void minmaxStep( 162762c14b34Sdanielk1977 sqlite3_context *context, 162862c14b34Sdanielk1977 int NotUsed, 162962c14b34Sdanielk1977 sqlite3_value **argv 163062c14b34Sdanielk1977 ){ 163188208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 16329eb516c0Sdrh Mem *pBest; 163362c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 16349eb516c0Sdrh 16359eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 16363aeab9e4Sdanielk1977 if( !pBest ) return; 1637268380caSdrh 16386fb2b54cSdan if( sqlite3_value_type(pArg)==SQLITE_NULL ){ 163994a6d998Sdrh if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); 164094a6d998Sdrh }else if( pBest->flags ){ 16419eb516c0Sdrh int max; 16429eb516c0Sdrh int cmp; 1643dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 16447e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 16457e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 16467e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 16477e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 16487e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 16497e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 16507e18c259Sdanielk1977 ** aggregate, or 0 for min(). 16517e18c259Sdanielk1977 */ 1652309b3386Sdrh max = sqlite3_user_data(context)!=0; 1653dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 165488208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 1655b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 16567a95789cSdrh }else{ 16577a95789cSdrh sqlite3SkipAccumulatorLoad(context); 165888208050Sdanielk1977 } 16590bce8354Sdrh }else{ 1660035e563bSdrh pBest->db = sqlite3_context_db_handle(context); 1661b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 16620bce8354Sdrh } 16630bce8354Sdrh } 16646fb2b54cSdan static void minMaxValueFinalize(sqlite3_context *context, int bValue){ 166588208050Sdanielk1977 sqlite3_value *pRes; 1666abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1667abfcea25Sdrh if( pRes ){ 166894a6d998Sdrh if( pRes->flags ){ 1669f4479501Sdrh sqlite3_result_value(context, pRes); 16700bce8354Sdrh } 16716fb2b54cSdan if( bValue==0 ) sqlite3VdbeMemRelease(pRes); 16720bce8354Sdrh } 1673abfcea25Sdrh } 167467a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 16756fb2b54cSdan static void minMaxValue(sqlite3_context *context){ 1676c7bf5716Sdrh minMaxValueFinalize(context, 1); 16776fb2b54cSdan } 167867a9b8edSdan #else 167967a9b8edSdan # define minMaxValue 0 168067a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 16816fb2b54cSdan static void minMaxFinalize(sqlite3_context *context){ 1682c7bf5716Sdrh minMaxValueFinalize(context, 0); 16836fb2b54cSdan } 1684dd5baa95Sdrh 1685b0689696Sdrh /* 1686b0689696Sdrh ** group_concat(EXPR, ?SEPARATOR?) 1687b0689696Sdrh */ 1688b0689696Sdrh static void groupConcatStep( 1689b0689696Sdrh sqlite3_context *context, 1690b0689696Sdrh int argc, 1691b0689696Sdrh sqlite3_value **argv 1692b0689696Sdrh ){ 1693b0689696Sdrh const char *zVal; 1694ade86483Sdrh StrAccum *pAccum; 1695b0689696Sdrh const char *zSep; 169607d3117aSdrh int nVal, nSep; 169707d3117aSdrh assert( argc==1 || argc==2 ); 169807d3117aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1699ade86483Sdrh pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1700ade86483Sdrh 1701ade86483Sdrh if( pAccum ){ 1702bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1703e3bf632cSdan int firstTerm = pAccum->mxAlloc==0; 1704bb4957f8Sdrh pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 17058bfd7190Sdrh if( !firstTerm ){ 170607d3117aSdrh if( argc==2 ){ 170707d3117aSdrh zSep = (char*)sqlite3_value_text(argv[1]); 170807d3117aSdrh nSep = sqlite3_value_bytes(argv[1]); 1709b0689696Sdrh }else{ 1710b0689696Sdrh zSep = ","; 1711ade86483Sdrh nSep = 1; 1712b0689696Sdrh } 17130cdbe1aeSdrh if( zSep ) sqlite3_str_append(pAccum, zSep, nSep); 1714b0689696Sdrh } 171507d3117aSdrh zVal = (char*)sqlite3_value_text(argv[0]); 171607d3117aSdrh nVal = sqlite3_value_bytes(argv[0]); 17170cdbe1aeSdrh if( zVal ) sqlite3_str_append(pAccum, zVal, nVal); 1718b0689696Sdrh } 1719b0689696Sdrh } 172067a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 172103854d2eSdan static void groupConcatInverse( 172203854d2eSdan sqlite3_context *context, 172303854d2eSdan int argc, 172403854d2eSdan sqlite3_value **argv 172503854d2eSdan ){ 172603854d2eSdan int n; 172703854d2eSdan StrAccum *pAccum; 1728c7bf5716Sdrh assert( argc==1 || argc==2 ); 172903854d2eSdan if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 173003854d2eSdan pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1731fd4b7285Sdrh /* pAccum is always non-NULL since groupConcatStep() will have always 1732fd4b7285Sdrh ** run frist to initialize it */ 1733fd4b7285Sdrh if( ALWAYS(pAccum) ){ 173403854d2eSdan n = sqlite3_value_bytes(argv[0]); 173503854d2eSdan if( argc==2 ){ 173603854d2eSdan n += sqlite3_value_bytes(argv[1]); 1737683b0fffSdan }else{ 1738683b0fffSdan n++; 173903854d2eSdan } 1740c7bf5716Sdrh if( n>=(int)pAccum->nChar ){ 174103854d2eSdan pAccum->nChar = 0; 174203854d2eSdan }else{ 174303854d2eSdan pAccum->nChar -= n; 174403854d2eSdan memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar); 174503854d2eSdan } 1746e3bf632cSdan if( pAccum->nChar==0 ) pAccum->mxAlloc = 0; 174703854d2eSdan } 174803854d2eSdan } 174967a9b8edSdan #else 175067a9b8edSdan # define groupConcatInverse 0 175167a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 1752b0689696Sdrh static void groupConcatFinalize(sqlite3_context *context){ 1753ade86483Sdrh StrAccum *pAccum; 1754ade86483Sdrh pAccum = sqlite3_aggregate_context(context, 0); 1755ade86483Sdrh if( pAccum ){ 17560cdbe1aeSdrh if( pAccum->accError==SQLITE_TOOBIG ){ 1757ade86483Sdrh sqlite3_result_error_toobig(context); 17580cdbe1aeSdrh }else if( pAccum->accError==SQLITE_NOMEM ){ 1759ade86483Sdrh sqlite3_result_error_nomem(context); 1760ade86483Sdrh }else{ 1761ade86483Sdrh sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 1762ade86483Sdrh sqlite3_free); 1763b0689696Sdrh } 1764b0689696Sdrh } 1765ade86483Sdrh } 176667a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC 1767e2f781b9Sdan static void groupConcatValue(sqlite3_context *context){ 1768e2f781b9Sdan sqlite3_str *pAccum; 1769e2f781b9Sdan pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0); 1770e2f781b9Sdan if( pAccum ){ 1771e2f781b9Sdan if( pAccum->accError==SQLITE_TOOBIG ){ 1772e2f781b9Sdan sqlite3_result_error_toobig(context); 1773e2f781b9Sdan }else if( pAccum->accError==SQLITE_NOMEM ){ 1774e2f781b9Sdan sqlite3_result_error_nomem(context); 1775e2f781b9Sdan }else{ 1776e2f781b9Sdan const char *zText = sqlite3_str_value(pAccum); 1777e2f781b9Sdan sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 1778e2f781b9Sdan } 1779e2f781b9Sdan } 1780e2f781b9Sdan } 178167a9b8edSdan #else 178267a9b8edSdan # define groupConcatValue 0 178367a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */ 17844e5ffc5fSdrh 1785d3a149efSdrh /* 1786a4741840Sdrh ** This routine does per-connection function registration. Most 1787a4741840Sdrh ** of the built-in functions above are part of the global function set. 1788a4741840Sdrh ** This routine only deals with those that are not global. 1789dc04c583Sdrh */ 179080738d9cSdrh void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ 1791832a58a6Sdanielk1977 int rc = sqlite3_overload_function(db, "MATCH", 2); 1792832a58a6Sdanielk1977 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 1793832a58a6Sdanielk1977 if( rc==SQLITE_NOMEM ){ 17944a642b60Sdrh sqlite3OomFault(db); 1795832a58a6Sdanielk1977 } 1796832a58a6Sdanielk1977 } 179755ef4d97Sdrh 179855ef4d97Sdrh /* 179908652b5eSdrh ** Re-register the built-in LIKE functions. The caseSensitive 180055ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 180108652b5eSdrh ** sensitive. 180255ef4d97Sdrh */ 180355ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 180455ef4d97Sdrh struct compareInfo *pInfo; 1805*ea5c040fSdrh int flags; 180655ef4d97Sdrh if( caseSensitive ){ 180755ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 1808*ea5c040fSdrh flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; 180955ef4d97Sdrh }else{ 181055ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 1811*ea5c040fSdrh flags = SQLITE_FUNC_LIKE; 181255ef4d97Sdrh } 1813660af939Sdan sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1814660af939Sdan sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); 1815*ea5c040fSdrh sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags; 1816*ea5c040fSdrh sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags; 181755ef4d97Sdrh } 181855ef4d97Sdrh 181955ef4d97Sdrh /* 182055ef4d97Sdrh ** pExpr points to an expression which implements a function. If 182155ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 18221d42ea71Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and the 18231d42ea71Sdrh ** escape character and then return TRUE. If the function is not a 18241d42ea71Sdrh ** LIKE-style function then return FALSE. 18251d42ea71Sdrh ** 18261d42ea71Sdrh ** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE 18271d42ea71Sdrh ** operator if c is a string literal that is exactly one byte in length. 18281d42ea71Sdrh ** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is 18291d42ea71Sdrh ** no ESCAPE clause. 183016897072Sdrh ** 183116897072Sdrh ** *pIsNocase is set to true if uppercase and lowercase are equivalent for 183216897072Sdrh ** the function (default for LIKE). If the function makes the distinction 183316897072Sdrh ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to 183416897072Sdrh ** false. 183555ef4d97Sdrh */ 1836d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 183755ef4d97Sdrh FuncDef *pDef; 18381d42ea71Sdrh int nExpr; 18391d42ea71Sdrh if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){ 184055ef4d97Sdrh return 0; 184155ef4d97Sdrh } 18426ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 18431d42ea71Sdrh nExpr = pExpr->x.pList->nExpr; 18441d42ea71Sdrh pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0); 1845d36e1041Sdrh if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ 184655ef4d97Sdrh return 0; 184755ef4d97Sdrh } 18481d42ea71Sdrh if( nExpr<3 ){ 18491d42ea71Sdrh aWc[3] = 0; 18501d42ea71Sdrh }else{ 18511d42ea71Sdrh Expr *pEscape = pExpr->x.pList->a[2].pExpr; 18521d42ea71Sdrh char *zEscape; 18531d42ea71Sdrh if( pEscape->op!=TK_STRING ) return 0; 18541d42ea71Sdrh zEscape = pEscape->u.zToken; 18551d42ea71Sdrh if( zEscape[0]==0 || zEscape[1]!=0 ) return 0; 18561d42ea71Sdrh aWc[3] = zEscape[0]; 18571d42ea71Sdrh } 185855ef4d97Sdrh 185955ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 186055ef4d97Sdrh ** the first three statements in the compareInfo structure. The 186155ef4d97Sdrh ** asserts() that follow verify that assumption 186255ef4d97Sdrh */ 186355ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 186455ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 186555ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 186655ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1867d36e1041Sdrh *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; 186855ef4d97Sdrh return 1; 1869dc04c583Sdrh } 18708c0a791aSdanielk1977 187193ce741bSdanielk1977 /* 187260ec914cSpeter.d.reid ** All of the FuncDef structures in the aBuiltinFunc[] array above 187393ce741bSdanielk1977 ** to the global function hash table. This occurs at start-time (as 187493ce741bSdanielk1977 ** a consequence of calling sqlite3_initialize()). 187593ce741bSdanielk1977 ** 187693ce741bSdanielk1977 ** After this routine runs 187793ce741bSdanielk1977 */ 187880738d9cSdrh void sqlite3RegisterBuiltinFunctions(void){ 18798c0a791aSdanielk1977 /* 1880777c5386Sdrh ** The following array holds FuncDef structures for all of the functions 1881777c5386Sdrh ** defined in this file. 18828c0a791aSdanielk1977 ** 1883777c5386Sdrh ** The array cannot be constant since changes are made to the 1884777c5386Sdrh ** FuncDef.pHash elements at start-time. The elements of this array 1885777c5386Sdrh ** are read-only after initialization is complete. 188680738d9cSdrh ** 188780738d9cSdrh ** For peak efficiency, put the most frequently used function last. 18888c0a791aSdanielk1977 */ 188980738d9cSdrh static FuncDef aBuiltinFunc[] = { 189080738d9cSdrh #ifdef SQLITE_SOUNDEX 189180738d9cSdrh FUNCTION(soundex, 1, 0, 0, soundexFunc ), 189280738d9cSdrh #endif 189380738d9cSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 189480738d9cSdrh VFUNCTION(load_extension, 1, 0, 0, loadExt ), 189580738d9cSdrh VFUNCTION(load_extension, 2, 0, 0, loadExt ), 189680738d9cSdrh #endif 189780738d9cSdrh #if SQLITE_USER_AUTHENTICATION 189880738d9cSdrh FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), 189980738d9cSdrh #endif 190080738d9cSdrh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 190180738d9cSdrh DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), 190280738d9cSdrh DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), 190380738d9cSdrh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 190480738d9cSdrh FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 190580738d9cSdrh FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 190680738d9cSdrh FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 190754240751Sdrh #ifdef SQLITE_DEBUG 1908a1a523a5Sdrh FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY), 190954240751Sdrh #endif 1910092457b1Sdrh #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 191135100fb1Sdrh FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET| 19122fc865c1Sdrh SQLITE_FUNC_TYPEOF), 1913092457b1Sdrh #endif 19148c0a791aSdanielk1977 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 19158c0a791aSdanielk1977 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 19168c0a791aSdanielk1977 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 19178c0a791aSdanielk1977 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 19188c0a791aSdanielk1977 FUNCTION(trim, 1, 3, 0, trimFunc ), 19198c0a791aSdanielk1977 FUNCTION(trim, 2, 3, 0, trimFunc ), 19208c0a791aSdanielk1977 FUNCTION(min, -1, 0, 1, minmaxFunc ), 19218c0a791aSdanielk1977 FUNCTION(min, 0, 0, 1, 0 ), 19226fb2b54cSdan WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 19239588ad95Sdrh SQLITE_FUNC_MINMAX ), 19248c0a791aSdanielk1977 FUNCTION(max, -1, 1, 1, minmaxFunc ), 19258c0a791aSdanielk1977 FUNCTION(max, 0, 1, 1, 0 ), 19266fb2b54cSdan WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, 19279588ad95Sdrh SQLITE_FUNC_MINMAX ), 1928a748fdccSdrh FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 1929a748fdccSdrh FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 1930d55e0729Sdrh FUNCTION(instr, 2, 0, 0, instrFunc ), 1931a5c1416dSdrh FUNCTION(printf, -1, 0, 0, printfFunc ), 1932d495d8c9Sdrh FUNCTION(unicode, 1, 0, 0, unicodeFunc ), 1933d495d8c9Sdrh FUNCTION(char, -1, 0, 0, charFunc ), 19348c0a791aSdanielk1977 FUNCTION(abs, 1, 0, 0, absFunc ), 1935fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 19368c0a791aSdanielk1977 FUNCTION(round, 1, 0, 0, roundFunc ), 19378c0a791aSdanielk1977 FUNCTION(round, 2, 0, 0, roundFunc ), 1938fbd60f82Sshane #endif 19398c0a791aSdanielk1977 FUNCTION(upper, 1, 0, 0, upperFunc ), 19408c0a791aSdanielk1977 FUNCTION(lower, 1, 0, 0, lowerFunc ), 19418c0a791aSdanielk1977 FUNCTION(hex, 1, 0, 0, hexFunc ), 1942cca9f3d2Sdrh FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 1943b1fba286Sdrh VFUNCTION(random, 0, 0, 0, randomFunc ), 1944b1fba286Sdrh VFUNCTION(randomblob, 1, 0, 0, randomBlob ), 19458c0a791aSdanielk1977 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 194603bf26d9Sdrh DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 194703bf26d9Sdrh DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), 1948840561f2Sdrh FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), 19498c0a791aSdanielk1977 FUNCTION(quote, 1, 0, 0, quoteFunc ), 1950b1fba286Sdrh VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 1951b1fba286Sdrh VFUNCTION(changes, 0, 0, 0, changes ), 1952b1fba286Sdrh VFUNCTION(total_changes, 0, 0, 0, total_changes ), 19538c0a791aSdanielk1977 FUNCTION(replace, 3, 0, 0, replaceFunc ), 19548c0a791aSdanielk1977 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 195580738d9cSdrh FUNCTION(substr, 2, 0, 0, substrFunc ), 195680738d9cSdrh FUNCTION(substr, 3, 0, 0, substrFunc ), 19576fb2b54cSdan WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0), 19586fb2b54cSdan WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0), 19596fb2b54cSdan WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0), 19607262ca94Sdan WAGGREGATE(count, 0,0,0, countStep, 19617262ca94Sdan countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ), 19627262ca94Sdan WAGGREGATE(count, 1,0,0, countStep, 19637262ca94Sdan countFinalize, countFinalize, countInverse, 0 ), 196403854d2eSdan WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, 19656fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 196603854d2eSdan WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, 19676fb2b54cSdan groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), 19688c0a791aSdanielk1977 19698c0a791aSdanielk1977 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 19708c0a791aSdanielk1977 #ifdef SQLITE_CASE_SENSITIVE_LIKE 19718c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 19728c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 19738c0a791aSdanielk1977 #else 19748c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 19758c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 19768c0a791aSdanielk1977 #endif 1977cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 1978cc15313cSdrh FUNCTION(unknown, -1, 0, 0, unknownFunc ), 1979cc15313cSdrh #endif 198080738d9cSdrh FUNCTION(coalesce, 1, 0, 0, 0 ), 198180738d9cSdrh FUNCTION(coalesce, 0, 0, 0, 0 ), 198280738d9cSdrh FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 19838c0a791aSdanielk1977 }; 1984545f587fSdrh #ifndef SQLITE_OMIT_ALTERTABLE 1985545f587fSdrh sqlite3AlterFunctions(); 1986545f587fSdrh #endif 1987dfa552f4Sdan sqlite3WindowFunctions(); 19880106e378Sdan #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) 19890106e378Sdan sqlite3AnalyzeFunctions(); 19900106e378Sdan #endif 199180738d9cSdrh sqlite3RegisterDateTimeFunctions(); 199280738d9cSdrh sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); 199380738d9cSdrh 199480738d9cSdrh #if 0 /* Enable to print out how the built-in functions are hashed */ 199580738d9cSdrh { 199680738d9cSdrh int i; 199780738d9cSdrh FuncDef *p; 199880738d9cSdrh for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ 199980738d9cSdrh printf("FUNC-HASH %02d:", i); 200080738d9cSdrh for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){ 200180738d9cSdrh int n = sqlite3Strlen30(p->zName); 200280738d9cSdrh int h = p->zName[0] + n; 200380738d9cSdrh printf(" %s(%d)", p->zName, h); 200480738d9cSdrh } 200580738d9cSdrh printf("\n"); 200680738d9cSdrh } 200780738d9cSdrh } 200880738d9cSdrh #endif 200970a8ca3cSdrh } 2010