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){ 387a95789cSdrh context->skipFlag = 1; 397a95789cSdrh } 407a95789cSdrh 417a95789cSdrh /* 420bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions 430bce8354Sdrh */ 44f9b596ebSdrh static void minmaxFunc( 45f9b596ebSdrh sqlite3_context *context, 46f9b596ebSdrh int argc, 47f9b596ebSdrh sqlite3_value **argv 48f9b596ebSdrh ){ 490bce8354Sdrh int i; 50268380caSdrh int mask; /* 0 for min() or 0xffffffff for max() */ 51f9b596ebSdrh int iBest; 52dc1bdc4fSdanielk1977 CollSeq *pColl; 530bce8354Sdrh 5465595cd6Sdrh assert( argc>1 ); 55c44af71cSdrh mask = sqlite3_user_data(context)==0 ? 0 : -1; 56dc1bdc4fSdanielk1977 pColl = sqlite3GetFuncCollSeq(context); 57dc1bdc4fSdanielk1977 assert( pColl ); 58c572ef7fSdanielk1977 assert( mask==-1 || mask==0 ); 59f9b596ebSdrh iBest = 0; 609c054830Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 61f9b596ebSdrh for(i=1; i<argc; i++){ 629c054830Sdrh if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 63dc1bdc4fSdanielk1977 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 6465595cd6Sdrh testcase( mask==0 ); 65f9b596ebSdrh iBest = i; 660bce8354Sdrh } 670bce8354Sdrh } 68f4479501Sdrh sqlite3_result_value(context, argv[iBest]); 690bce8354Sdrh } 700bce8354Sdrh 71268380caSdrh /* 72268380caSdrh ** Return the type of the argument. 73268380caSdrh */ 74f9b596ebSdrh static void typeofFunc( 75f9b596ebSdrh sqlite3_context *context, 7662c14b34Sdanielk1977 int NotUsed, 77f9b596ebSdrh sqlite3_value **argv 78f9b596ebSdrh ){ 7935bb9d02Sdanielk1977 const char *z = 0; 8062c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 8135bb9d02Sdanielk1977 switch( sqlite3_value_type(argv[0]) ){ 829c054830Sdrh case SQLITE_INTEGER: z = "integer"; break; 839c054830Sdrh case SQLITE_TEXT: z = "text"; break; 849c054830Sdrh case SQLITE_FLOAT: z = "real"; break; 859c054830Sdrh case SQLITE_BLOB: z = "blob"; break; 8665595cd6Sdrh default: z = "null"; break; 8735bb9d02Sdanielk1977 } 88d8123366Sdanielk1977 sqlite3_result_text(context, z, -1, SQLITE_STATIC); 890bce8354Sdrh } 900bce8354Sdrh 915708d2deSdrh 925708d2deSdrh /* 930bce8354Sdrh ** Implementation of the length() function 940bce8354Sdrh */ 95f9b596ebSdrh static void lengthFunc( 96f9b596ebSdrh sqlite3_context *context, 97f9b596ebSdrh int argc, 98f9b596ebSdrh sqlite3_value **argv 99f9b596ebSdrh ){ 1000bce8354Sdrh int len; 1010bce8354Sdrh 1020bce8354Sdrh assert( argc==1 ); 103f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 104f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1059c054830Sdrh case SQLITE_BLOB: 1069c054830Sdrh case SQLITE_INTEGER: 1079c054830Sdrh case SQLITE_FLOAT: { 108f4479501Sdrh sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 109f9b596ebSdrh break; 110f9b596ebSdrh } 1119c054830Sdrh case SQLITE_TEXT: { 1122646da7eSdrh const unsigned char *z = sqlite3_value_text(argv[0]); 1137a521cfbSdrh if( z==0 ) return; 1144a919118Sdrh len = 0; 1154a919118Sdrh while( *z ){ 1164a919118Sdrh len++; 1174a919118Sdrh SQLITE_SKIP_UTF8(z); 1184a919118Sdrh } 119f4479501Sdrh sqlite3_result_int(context, len); 120f9b596ebSdrh break; 121f9b596ebSdrh } 122f9b596ebSdrh default: { 123f9b596ebSdrh sqlite3_result_null(context); 124f9b596ebSdrh break; 125f9b596ebSdrh } 126f9b596ebSdrh } 1270bce8354Sdrh } 1280bce8354Sdrh 1290bce8354Sdrh /* 1302ba3cccfSdrh ** Implementation of the abs() function. 1312ba3cccfSdrh ** 1322ba3cccfSdrh ** IMP: R-23979-26855 The abs(X) function returns the absolute value of 1332ba3cccfSdrh ** the numeric argument X. 1340bce8354Sdrh */ 1350ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1360bce8354Sdrh assert( argc==1 ); 137f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 138f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1399c054830Sdrh case SQLITE_INTEGER: { 140f93bbbeaSdanielk1977 i64 iVal = sqlite3_value_int64(argv[0]); 14152fc849aSdrh if( iVal<0 ){ 142693e6719Sdrh if( iVal==SMALLEST_INT64 ){ 143eb091cdfSdrh /* IMP: R-31676-45509 If X is the integer -9223372036854775808 144eb091cdfSdrh ** then abs(X) throws an integer overflow error since there is no 1452ba3cccfSdrh ** equivalent positive 64-bit two complement value. */ 14652fc849aSdrh sqlite3_result_error(context, "integer overflow", -1); 14752fc849aSdrh return; 14852fc849aSdrh } 14952fc849aSdrh iVal = -iVal; 15052fc849aSdrh } 151f93bbbeaSdanielk1977 sqlite3_result_int64(context, iVal); 152f9b596ebSdrh break; 153f9b596ebSdrh } 1549c054830Sdrh case SQLITE_NULL: { 1552ba3cccfSdrh /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ 156f9b596ebSdrh sqlite3_result_null(context); 157f9b596ebSdrh break; 158f9b596ebSdrh } 159f9b596ebSdrh default: { 1602ba3cccfSdrh /* Because sqlite3_value_double() returns 0.0 if the argument is not 1612ba3cccfSdrh ** something that can be converted into a number, we have: 162643091f0Sdrh ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob 163643091f0Sdrh ** that cannot be converted to a numeric value. 1642ba3cccfSdrh */ 165f93bbbeaSdanielk1977 double rVal = sqlite3_value_double(argv[0]); 16652fc849aSdrh if( rVal<0 ) rVal = -rVal; 167f93bbbeaSdanielk1977 sqlite3_result_double(context, rVal); 168f9b596ebSdrh break; 169f9b596ebSdrh } 170f9b596ebSdrh } 1710bce8354Sdrh } 1720bce8354Sdrh 1730bce8354Sdrh /* 174d55e0729Sdrh ** Implementation of the instr() function. 175d55e0729Sdrh ** 176d55e0729Sdrh ** instr(haystack,needle) finds the first occurrence of needle 177d55e0729Sdrh ** in haystack and returns the number of previous characters plus 1, 178d55e0729Sdrh ** or 0 if needle does not occur within haystack. 179d55e0729Sdrh ** 180d55e0729Sdrh ** If both haystack and needle are BLOBs, then the result is one more than 181d55e0729Sdrh ** the number of bytes in haystack prior to the first occurrence of needle, 182d55e0729Sdrh ** or 0 if needle never occurs in haystack. 183d55e0729Sdrh */ 184d55e0729Sdrh static void instrFunc( 185d55e0729Sdrh sqlite3_context *context, 186d55e0729Sdrh int argc, 187d55e0729Sdrh sqlite3_value **argv 188d55e0729Sdrh ){ 189d55e0729Sdrh const unsigned char *zHaystack; 190d55e0729Sdrh const unsigned char *zNeedle; 191d55e0729Sdrh int nHaystack; 192d55e0729Sdrh int nNeedle; 193d55e0729Sdrh int typeHaystack, typeNeedle; 194d55e0729Sdrh int N = 1; 195d55e0729Sdrh int isText; 196d55e0729Sdrh 19768c804b9Sdrh UNUSED_PARAMETER(argc); 198d55e0729Sdrh typeHaystack = sqlite3_value_type(argv[0]); 199d55e0729Sdrh typeNeedle = sqlite3_value_type(argv[1]); 200d55e0729Sdrh if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; 201d55e0729Sdrh nHaystack = sqlite3_value_bytes(argv[0]); 202d55e0729Sdrh nNeedle = sqlite3_value_bytes(argv[1]); 203d55e0729Sdrh if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ 204d55e0729Sdrh zHaystack = sqlite3_value_blob(argv[0]); 205d55e0729Sdrh zNeedle = sqlite3_value_blob(argv[1]); 206d55e0729Sdrh isText = 0; 207d55e0729Sdrh }else{ 208d55e0729Sdrh zHaystack = sqlite3_value_text(argv[0]); 209d55e0729Sdrh zNeedle = sqlite3_value_text(argv[1]); 210d55e0729Sdrh isText = 1; 211d55e0729Sdrh } 212d55e0729Sdrh while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){ 213d55e0729Sdrh N++; 214d55e0729Sdrh do{ 215d55e0729Sdrh nHaystack--; 216d55e0729Sdrh zHaystack++; 217d55e0729Sdrh }while( isText && (zHaystack[0]&0xc0)==0x80 ); 218d55e0729Sdrh } 219d55e0729Sdrh if( nNeedle>nHaystack ) N = 0; 220d55e0729Sdrh sqlite3_result_int(context, N); 221d55e0729Sdrh } 222d55e0729Sdrh 223d55e0729Sdrh /* 224a5c1416dSdrh ** Implementation of the printf() function. 225a5c1416dSdrh */ 226a5c1416dSdrh static void printfFunc( 227a5c1416dSdrh sqlite3_context *context, 228a5c1416dSdrh int argc, 229a5c1416dSdrh sqlite3_value **argv 230a5c1416dSdrh ){ 231a5c1416dSdrh PrintfArguments x; 232a5c1416dSdrh StrAccum str; 233a5c1416dSdrh const char *zFormat; 234a5c1416dSdrh int n; 235a5c1416dSdrh 236a5c1416dSdrh if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 237a5c1416dSdrh x.nArg = argc-1; 238a5c1416dSdrh x.nUsed = 0; 239a5c1416dSdrh x.apArg = argv+1; 240a5c1416dSdrh sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH); 241a5c1416dSdrh str.db = sqlite3_context_db_handle(context); 242a5c1416dSdrh sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x); 243a5c1416dSdrh n = str.nChar; 244a5c1416dSdrh sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, 245a5c1416dSdrh SQLITE_DYNAMIC); 246a5c1416dSdrh } 247a5c1416dSdrh } 248a5c1416dSdrh 249a5c1416dSdrh /* 250f764e6fcSdrh ** Implementation of the substr() function. 251f764e6fcSdrh ** 252f764e6fcSdrh ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. 253f764e6fcSdrh ** p1 is 1-indexed. So substr(x,1,1) returns the first character 254f764e6fcSdrh ** of x. If x is text, then we actually count UTF-8 characters. 255f764e6fcSdrh ** If x is a blob, then we count bytes. 256f764e6fcSdrh ** 257f764e6fcSdrh ** If p1 is negative, then we begin abs(p1) from the end of x[]. 258779b8f12Sshaneh ** 259f7b5496eSdrh ** If p2 is negative, return the p2 characters preceding p1. 2600bce8354Sdrh */ 261f9b596ebSdrh static void substrFunc( 262f9b596ebSdrh sqlite3_context *context, 263f9b596ebSdrh int argc, 264f9b596ebSdrh sqlite3_value **argv 265f9b596ebSdrh ){ 2662646da7eSdrh const unsigned char *z; 2672646da7eSdrh const unsigned char *z2; 268023ae03aSdrh int len; 269f764e6fcSdrh int p0type; 270023ae03aSdrh i64 p1, p2; 27165595cd6Sdrh int negP2 = 0; 272f9b596ebSdrh 27364f31519Sdrh assert( argc==3 || argc==2 ); 2748198d254Sdrh if( sqlite3_value_type(argv[1])==SQLITE_NULL 2758198d254Sdrh || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) 2768198d254Sdrh ){ 2778198d254Sdrh return; 2788198d254Sdrh } 279f764e6fcSdrh p0type = sqlite3_value_type(argv[0]); 2804adc4cb9Sdrh p1 = sqlite3_value_int(argv[1]); 281f764e6fcSdrh if( p0type==SQLITE_BLOB ){ 282f764e6fcSdrh len = sqlite3_value_bytes(argv[0]); 283f764e6fcSdrh z = sqlite3_value_blob(argv[0]); 284f764e6fcSdrh if( z==0 ) return; 2851f0feef8Sdrh assert( len==sqlite3_value_bytes(argv[0]) ); 286f764e6fcSdrh }else{ 2874f26d6c4Sdrh z = sqlite3_value_text(argv[0]); 2880bce8354Sdrh if( z==0 ) return; 2894a919118Sdrh len = 0; 2904adc4cb9Sdrh if( p1<0 ){ 2914a919118Sdrh for(z2=z; *z2; len++){ 2924a919118Sdrh SQLITE_SKIP_UTF8(z2); 2934a919118Sdrh } 294f764e6fcSdrh } 2954adc4cb9Sdrh } 296883ad049Sdrh #ifdef SQLITE_SUBSTR_COMPATIBILITY 297883ad049Sdrh /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as 298883ad049Sdrh ** as substr(X,1,N) - it returns the first N characters of X. This 299883ad049Sdrh ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] 300883ad049Sdrh ** from 2009-02-02 for compatibility of applications that exploited the 301883ad049Sdrh ** old buggy behavior. */ 302883ad049Sdrh if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ 303883ad049Sdrh #endif 30464f31519Sdrh if( argc==3 ){ 30551ad0ecdSdanielk1977 p2 = sqlite3_value_int(argv[2]); 30665595cd6Sdrh if( p2<0 ){ 30765595cd6Sdrh p2 = -p2; 30865595cd6Sdrh negP2 = 1; 30965595cd6Sdrh } 31064f31519Sdrh }else{ 311bb4957f8Sdrh p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; 31264f31519Sdrh } 3130bce8354Sdrh if( p1<0 ){ 31489425d5eSdrh p1 += len; 315653bc759Sdrh if( p1<0 ){ 316653bc759Sdrh p2 += p1; 31765595cd6Sdrh if( p2<0 ) p2 = 0; 318653bc759Sdrh p1 = 0; 319653bc759Sdrh } 3200bce8354Sdrh }else if( p1>0 ){ 3210bce8354Sdrh p1--; 32265595cd6Sdrh }else if( p2>0 ){ 32365595cd6Sdrh p2--; 3240bce8354Sdrh } 32565595cd6Sdrh if( negP2 ){ 32665595cd6Sdrh p1 -= p2; 3274e79c594Sdrh if( p1<0 ){ 3284e79c594Sdrh p2 += p1; 3294e79c594Sdrh p1 = 0; 3304e79c594Sdrh } 3314e79c594Sdrh } 33265595cd6Sdrh assert( p1>=0 && p2>=0 ); 333f764e6fcSdrh if( p0type!=SQLITE_BLOB ){ 3344a919118Sdrh while( *z && p1 ){ 3354a919118Sdrh SQLITE_SKIP_UTF8(z); 3364a919118Sdrh p1--; 3370bce8354Sdrh } 3384a919118Sdrh for(z2=z; *z2 && p2; p2--){ 3394a919118Sdrh SQLITE_SKIP_UTF8(z2); 3400bce8354Sdrh } 341bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, 342bbf483f8Sdrh SQLITE_UTF8); 343f764e6fcSdrh }else{ 3444adc4cb9Sdrh if( p1+p2>len ){ 3454adc4cb9Sdrh p2 = len-p1; 3464adc4cb9Sdrh if( p2<0 ) p2 = 0; 3474adc4cb9Sdrh } 348bbf483f8Sdrh sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); 349f764e6fcSdrh } 3500bce8354Sdrh } 3510bce8354Sdrh 3520bce8354Sdrh /* 3530bce8354Sdrh ** Implementation of the round() function 3540bce8354Sdrh */ 355fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 3560ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 35751ad0ecdSdanielk1977 int n = 0; 3580bce8354Sdrh double r; 35950d654daSdrh char *zBuf; 3600bce8354Sdrh assert( argc==1 || argc==2 ); 36151ad0ecdSdanielk1977 if( argc==2 ){ 3629c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 36351ad0ecdSdanielk1977 n = sqlite3_value_int(argv[1]); 3640bce8354Sdrh if( n>30 ) n = 30; 3650bce8354Sdrh if( n<0 ) n = 0; 36651ad0ecdSdanielk1977 } 367d589a92aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 3684f26d6c4Sdrh r = sqlite3_value_double(argv[0]); 369147e176aSshaneh /* If Y==0 and X will fit in a 64-bit int, 370147e176aSshaneh ** handle the rounding directly, 371147e176aSshaneh ** otherwise use printf. 372147e176aSshaneh */ 373147e176aSshaneh if( n==0 && r>=0 && r<LARGEST_INT64-1 ){ 374147e176aSshaneh r = (double)((sqlite_int64)(r+0.5)); 375147e176aSshaneh }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){ 376147e176aSshaneh r = -(double)((sqlite_int64)((-r)+0.5)); 377147e176aSshaneh }else{ 37850d654daSdrh zBuf = sqlite3_mprintf("%.*f",n,r); 37950d654daSdrh if( zBuf==0 ){ 38050d654daSdrh sqlite3_result_error_nomem(context); 381147e176aSshaneh return; 382147e176aSshaneh } 3839339da1fSdrh sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); 38450d654daSdrh sqlite3_free(zBuf); 3850bce8354Sdrh } 386147e176aSshaneh sqlite3_result_double(context, r); 38750d654daSdrh } 388fbd60f82Sshane #endif 389dc04c583Sdrh 39026783a58Sdanielk1977 /* 391*f3cdcdccSdrh ** Allocate nByte bytes of space using sqlite3Malloc(). If the 39226783a58Sdanielk1977 ** allocation fails, call sqlite3_result_error_nomem() to notify 39327e62dbeSdrh ** the database handle that malloc() has failed and return NULL. 39427e62dbeSdrh ** If nByte is larger than the maximum string or blob length, then 39527e62dbeSdrh ** raise an SQLITE_TOOBIG exception and return NULL. 39626783a58Sdanielk1977 */ 397b1a6c3c1Sdrh static void *contextMalloc(sqlite3_context *context, i64 nByte){ 398bb4957f8Sdrh char *z; 39927e62dbeSdrh sqlite3 *db = sqlite3_context_db_handle(context); 400ef31c6aaSdrh assert( nByte>0 ); 40127e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); 40227e62dbeSdrh testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); 40327e62dbeSdrh if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 404bb4957f8Sdrh sqlite3_result_error_toobig(context); 405bb4957f8Sdrh z = 0; 406bb4957f8Sdrh }else{ 407da4ca9d1Sdrh z = sqlite3Malloc(nByte); 408ef31c6aaSdrh if( !z ){ 409a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 410a1644fd8Sdanielk1977 } 411bb4957f8Sdrh } 412a1644fd8Sdanielk1977 return z; 413a1644fd8Sdanielk1977 } 414a1644fd8Sdanielk1977 415dc04c583Sdrh /* 416dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 417dc04c583Sdrh */ 4180ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 4197a521cfbSdrh char *z1; 4207a521cfbSdrh const char *z2; 4219310ef23Sdrh int i, n; 4221d34fdecSdrh UNUSED_PARAMETER(argc); 4237a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4241f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4251f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4261f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4277a521cfbSdrh if( z2 ){ 428b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4297a521cfbSdrh if( z1 ){ 430df901d34Sdrh for(i=0; i<n; i++){ 431df901d34Sdrh z1[i] = (char)sqlite3Toupper(z2[i]); 432dc04c583Sdrh } 433df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4347a521cfbSdrh } 4357a521cfbSdrh } 436dc04c583Sdrh } 4370ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 438df901d34Sdrh char *z1; 4397a521cfbSdrh const char *z2; 4409310ef23Sdrh int i, n; 4411d34fdecSdrh UNUSED_PARAMETER(argc); 4427a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 4431f0feef8Sdrh n = sqlite3_value_bytes(argv[0]); 4441f0feef8Sdrh /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 4451f0feef8Sdrh assert( z2==(char*)sqlite3_value_text(argv[0]) ); 4467a521cfbSdrh if( z2 ){ 447b1a6c3c1Sdrh z1 = contextMalloc(context, ((i64)n)+1); 4487a521cfbSdrh if( z1 ){ 449df901d34Sdrh for(i=0; i<n; i++){ 450df901d34Sdrh z1[i] = sqlite3Tolower(z2[i]); 451dc04c583Sdrh } 452df901d34Sdrh sqlite3_result_text(context, z1, n, sqlite3_free); 4537a521cfbSdrh } 4547a521cfbSdrh } 455dc04c583Sdrh } 456dc04c583Sdrh 457ae6bb957Sdrh /* 458cca9f3d2Sdrh ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented 459cca9f3d2Sdrh ** as VDBE code so that unused argument values do not have to be computed. 460cca9f3d2Sdrh ** However, we still need some kind of function implementation for this 461cca9f3d2Sdrh ** routines in the function table. The noopFunc macro provides this. 462cca9f3d2Sdrh ** noopFunc will never be called so it doesn't matter what the implementation 463cca9f3d2Sdrh ** is. We might as well use the "version()" function as a substitute. 464ae6bb957Sdrh */ 465cca9f3d2Sdrh #define noopFunc versionFunc /* Substitute function - never called */ 4663212e182Sdrh 4673212e182Sdrh /* 468f9ffac96Sdrh ** Implementation of random(). Return a random integer. 469f9ffac96Sdrh */ 470f9b596ebSdrh static void randomFunc( 471f9b596ebSdrh sqlite3_context *context, 47262c14b34Sdanielk1977 int NotUsed, 47362c14b34Sdanielk1977 sqlite3_value **NotUsed2 474f9b596ebSdrh ){ 47552fc849aSdrh sqlite_int64 r; 47662c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 4772fa1868fSdrh sqlite3_randomness(sizeof(r), &r); 4783034e3d3Sdrh if( r<0 ){ 4793034e3d3Sdrh /* We need to prevent a random number of 0x8000000000000000 4803034e3d3Sdrh ** (or -9223372036854775808) since when you do abs() of that 4813034e3d3Sdrh ** number of you get the same value back again. To do this 4823034e3d3Sdrh ** in a way that is testable, mask the sign bit off of negative 4833034e3d3Sdrh ** values, resulting in a positive value. Then take the 4843034e3d3Sdrh ** 2s complement of that positive value. The end result can 4853034e3d3Sdrh ** therefore be no less than -9223372036854775807. 4863034e3d3Sdrh */ 487af8001bfSdrh r = -(r & LARGEST_INT64); 4883034e3d3Sdrh } 48952fc849aSdrh sqlite3_result_int64(context, r); 490f9ffac96Sdrh } 491f9ffac96Sdrh 492f9ffac96Sdrh /* 493137c728fSdrh ** Implementation of randomblob(N). Return a random blob 494137c728fSdrh ** that is N bytes long. 49563cf66f0Sdrh */ 496137c728fSdrh static void randomBlob( 49763cf66f0Sdrh sqlite3_context *context, 49863cf66f0Sdrh int argc, 49963cf66f0Sdrh sqlite3_value **argv 50063cf66f0Sdrh ){ 501137c728fSdrh int n; 502137c728fSdrh unsigned char *p; 50363cf66f0Sdrh assert( argc==1 ); 504f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 50563cf66f0Sdrh n = sqlite3_value_int(argv[0]); 506023ae03aSdrh if( n<1 ){ 507023ae03aSdrh n = 1; 508023ae03aSdrh } 509a1644fd8Sdanielk1977 p = contextMalloc(context, n); 51002d85836Sdrh if( p ){ 5112fa1868fSdrh sqlite3_randomness(n, p); 51217435752Sdrh sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 51302d85836Sdrh } 51463cf66f0Sdrh } 51563cf66f0Sdrh 51663cf66f0Sdrh /* 5176ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 51824b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 5196ed41ad7Sdrh */ 52051ad0ecdSdanielk1977 static void last_insert_rowid( 5210ae8b831Sdanielk1977 sqlite3_context *context, 52262c14b34Sdanielk1977 int NotUsed, 52362c14b34Sdanielk1977 sqlite3_value **NotUsed2 52451ad0ecdSdanielk1977 ){ 525fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 52662c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 527ab2f1f95Sdrh /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a 528ab2f1f95Sdrh ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface 529ab2f1f95Sdrh ** function. */ 530f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 5316ed41ad7Sdrh } 5326ed41ad7Sdrh 533f146a776Srdc /* 534ab2f1f95Sdrh ** Implementation of the changes() SQL function. 535ab2f1f95Sdrh ** 536ab2f1f95Sdrh ** IMP: R-62073-11209 The changes() SQL function is a wrapper 537ab2f1f95Sdrh ** around the sqlite3_changes() C/C++ function and hence follows the same 538ab2f1f95Sdrh ** rules for counting changes. 539f146a776Srdc */ 540b28af71aSdanielk1977 static void changes( 541f9b596ebSdrh sqlite3_context *context, 54262c14b34Sdanielk1977 int NotUsed, 54362c14b34Sdanielk1977 sqlite3_value **NotUsed2 544f9b596ebSdrh ){ 545fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 54662c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 547f4479501Sdrh sqlite3_result_int(context, sqlite3_changes(db)); 548b0c374ffSrdc } 549f146a776Srdc 550f146a776Srdc /* 551b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 552b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function. 553f146a776Srdc */ 554b28af71aSdanielk1977 static void total_changes( 5550ae8b831Sdanielk1977 sqlite3_context *context, 55662c14b34Sdanielk1977 int NotUsed, 55762c14b34Sdanielk1977 sqlite3_value **NotUsed2 55851ad0ecdSdanielk1977 ){ 559fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 56062c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 561ab2f1f95Sdrh /* IMP: R-52756-41993 This function is a wrapper around the 562ab2f1f95Sdrh ** sqlite3_total_changes() C/C++ interface. */ 563b28af71aSdanielk1977 sqlite3_result_int(context, sqlite3_total_changes(db)); 564b0c374ffSrdc } 565b0c374ffSrdc 5666ed41ad7Sdrh /* 5674e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 568d02eb1fdSdanielk1977 */ 5694e5ffc5fSdrh struct compareInfo { 5704e5ffc5fSdrh u8 matchAll; 5714e5ffc5fSdrh u8 matchOne; 5724e5ffc5fSdrh u8 matchSet; 5734e5ffc5fSdrh u8 noCase; 574d02eb1fdSdanielk1977 }; 57555ef4d97Sdrh 576b9175aedSdrh /* 577b9175aedSdrh ** For LIKE and GLOB matching on EBCDIC machines, assume that every 578b9175aedSdrh ** character is exactly one byte in size. Also, all characters are 579b9175aedSdrh ** able to participate in upper-case-to-lower-case mappings in EBCDIC 580b9175aedSdrh ** whereas only characters less than 0x80 do in ASCII. 581b9175aedSdrh */ 582b9175aedSdrh #if defined(SQLITE_EBCDIC) 58342610961Sdrh # define sqlite3Utf8Read(A) (*((*A)++)) 5846e1b1674Sdrh # define GlobUpperToLower(A) A = sqlite3UpperToLower[A] 58588b3322fSdrh # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] 586b9175aedSdrh #else 58788b3322fSdrh # define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; } 58888b3322fSdrh # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] 589b9175aedSdrh #endif 590b9175aedSdrh 5914e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 59270031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 59370031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 59455ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 59570031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 59670031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 59755ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 598d02eb1fdSdanielk1977 599d02eb1fdSdanielk1977 /* 6004e5ffc5fSdrh ** Compare two UTF-8 strings for equality where the first string can 6019fdfdc89Sdrh ** potentially be a "glob" or "like" expression. Return true (1) if they 6024e5ffc5fSdrh ** are the same and false (0) if they are different. 6030ac65892Sdrh ** 6044e5ffc5fSdrh ** Globbing rules: 6050ac65892Sdrh ** 6064e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 607d02eb1fdSdanielk1977 ** 6084e5ffc5fSdrh ** '?' Matches exactly one character. 6094e5ffc5fSdrh ** 6104e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 6114e5ffc5fSdrh ** characters. 6124e5ffc5fSdrh ** 6134e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 6144e5ffc5fSdrh ** 6154e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 6164e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 6174e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 6184e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 6194e5ffc5fSdrh ** it the last character in the list. 6204e5ffc5fSdrh ** 6219fdfdc89Sdrh ** Like matching rules: 6229fdfdc89Sdrh ** 6239fdfdc89Sdrh ** '%' Matches any sequence of zero or more characters 6249fdfdc89Sdrh ** 6259fdfdc89Sdrh *** '_' Matches any one character 6269fdfdc89Sdrh ** 6279fdfdc89Sdrh ** Ec Where E is the "esc" character and c is any other 6289fdfdc89Sdrh ** character, including '%', '_', and esc, match exactly c. 6299fdfdc89Sdrh ** 6309fdfdc89Sdrh ** The comments through this routine usually assume glob matching. 6319fdfdc89Sdrh ** 6324e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 6330ac65892Sdrh */ 6347c6303c0Sdanielk1977 static int patternCompare( 6354e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 6364e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 6377c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 6380a32fa6dSdrh u32 esc /* The escape character */ 63951ad0ecdSdanielk1977 ){ 6409fdfdc89Sdrh u32 c, c2; /* Next pattern and input string chars */ 6419fdfdc89Sdrh u32 matchOne = pInfo->matchOne; /* "?" or "_" */ 6429fdfdc89Sdrh u32 matchAll = pInfo->matchAll; /* "*" or "%" */ 6439fdfdc89Sdrh u32 matchOther; /* "[" or the escape character */ 6449fdfdc89Sdrh u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ 6459fdfdc89Sdrh const u8 *zEscaped = 0; /* One past the last escaped input char */ 646d02eb1fdSdanielk1977 64788b3322fSdrh /* The GLOB operator does not have an ESCAPE clause. And LIKE does not 64888b3322fSdrh ** have the matchSet operator. So we either have to look for one or 64988b3322fSdrh ** the other, never both. Hence the single variable matchOther is used 65088b3322fSdrh ** to store the one we have to look for. 65188b3322fSdrh */ 65288b3322fSdrh matchOther = esc ? esc : pInfo->matchSet; 65388b3322fSdrh 65442610961Sdrh while( (c = sqlite3Utf8Read(&zPattern))!=0 ){ 6559fdfdc89Sdrh if( c==matchAll ){ /* Match "*" */ 6569fdfdc89Sdrh /* Skip over multiple "*" characters in the pattern. If there 6579fdfdc89Sdrh ** are also "?" characters, skip those as well, but consume a 6589fdfdc89Sdrh ** single character of the input string for each "?" skipped */ 65942610961Sdrh while( (c=sqlite3Utf8Read(&zPattern)) == matchAll 66066150956Sdrh || c == matchOne ){ 66142610961Sdrh if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ 66266150956Sdrh return 0; 663d02eb1fdSdanielk1977 } 6644e5ffc5fSdrh } 66566150956Sdrh if( c==0 ){ 6669fdfdc89Sdrh return 1; /* "*" at the end of the pattern matches */ 66788b3322fSdrh }else if( c==matchOther ){ 66888b3322fSdrh if( esc ){ 66942610961Sdrh c = sqlite3Utf8Read(&zPattern); 67088b3322fSdrh if( c==0 ) return 0; 67188b3322fSdrh }else{ 6729fdfdc89Sdrh /* "[...]" immediately follows the "*". We have to do a slow 6739fdfdc89Sdrh ** recursive search in this case, but it is an unusual case. */ 67488b3322fSdrh assert( matchOther<0x80 ); /* '[' is a single-byte character */ 67588b3322fSdrh while( *zString 67688b3322fSdrh && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ 6774a919118Sdrh SQLITE_SKIP_UTF8(zString); 6784e5ffc5fSdrh } 6794e5ffc5fSdrh return *zString!=0; 68066150956Sdrh } 68188b3322fSdrh } 6829fdfdc89Sdrh 6839fdfdc89Sdrh /* At this point variable c contains the first character of the 6849fdfdc89Sdrh ** pattern string past the "*". Search in the input string for the 6859fdfdc89Sdrh ** first matching character and recursively contine the match from 6869fdfdc89Sdrh ** that point. 6879fdfdc89Sdrh ** 6889fdfdc89Sdrh ** For a case-insensitive search, set variable cx to be the same as 6899fdfdc89Sdrh ** c but in the other case and search the input string for either 6909fdfdc89Sdrh ** c or cx. 6919fdfdc89Sdrh */ 6929fdfdc89Sdrh if( c<=0x80 ){ 6939fdfdc89Sdrh u32 cx; 6949fdfdc89Sdrh if( noCase ){ 6959fdfdc89Sdrh cx = sqlite3Toupper(c); 6969fdfdc89Sdrh c = sqlite3Tolower(c); 697ad7dd425Sdanielk1977 }else{ 6989fdfdc89Sdrh cx = c; 69966150956Sdrh } 7009fdfdc89Sdrh while( (c2 = *(zString++))!=0 ){ 7019fdfdc89Sdrh if( c2!=c && c2!=cx ) continue; 70266150956Sdrh if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; 7034e5ffc5fSdrh } 70488b3322fSdrh }else{ 7059fdfdc89Sdrh while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ 7069fdfdc89Sdrh if( c2!=c ) continue; 7079fdfdc89Sdrh if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; 70866150956Sdrh } 70988b3322fSdrh } 7109fdfdc89Sdrh return 0; 7119fdfdc89Sdrh } 71288b3322fSdrh if( c==matchOther ){ 71388b3322fSdrh if( esc ){ 71488b3322fSdrh c = sqlite3Utf8Read(&zPattern); 71588b3322fSdrh if( c==0 ) return 0; 7169fdfdc89Sdrh zEscaped = zPattern; 71788b3322fSdrh }else{ 7181aa4f3e5Sdrh u32 prior_c = 0; 7199fdfdc89Sdrh int seen = 0; 7209fdfdc89Sdrh int invert = 0; 72142610961Sdrh c = sqlite3Utf8Read(&zString); 7224e5ffc5fSdrh if( c==0 ) return 0; 72342610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 72466150956Sdrh if( c2=='^' ){ 72566150956Sdrh invert = 1; 72642610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 72766150956Sdrh } 7284e5ffc5fSdrh if( c2==']' ){ 7294e5ffc5fSdrh if( c==']' ) seen = 1; 73042610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7314e5ffc5fSdrh } 73266150956Sdrh while( c2 && c2!=']' ){ 73366150956Sdrh if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ 73442610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 7354e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 7364e5ffc5fSdrh prior_c = 0; 73766150956Sdrh }else{ 73866150956Sdrh if( c==c2 ){ 7394e5ffc5fSdrh seen = 1; 74066150956Sdrh } 7414e5ffc5fSdrh prior_c = c2; 742d02eb1fdSdanielk1977 } 74342610961Sdrh c2 = sqlite3Utf8Read(&zPattern); 744d02eb1fdSdanielk1977 } 74566150956Sdrh if( c2==0 || (seen ^ invert)==0 ){ 74666150956Sdrh return 0; 74766150956Sdrh } 74888b3322fSdrh continue; 749328d913cSdrh } 75088b3322fSdrh } 75188b3322fSdrh c2 = sqlite3Utf8Read(&zString); 75288b3322fSdrh if( c==c2 ) continue; 7539fdfdc89Sdrh if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ 7549fdfdc89Sdrh continue; 7559fdfdc89Sdrh } 7569fdfdc89Sdrh if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; 7579fdfdc89Sdrh return 0; 7580ac65892Sdrh } 7594e5ffc5fSdrh return *zString==0; 7604e5ffc5fSdrh } 7614e5ffc5fSdrh 76255ef4d97Sdrh /* 76356282a5bSdrh ** The sqlite3_strglob() interface. 76456282a5bSdrh */ 76556282a5bSdrh int sqlite3_strglob(const char *zGlobPattern, const char *zString){ 76656282a5bSdrh return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0; 76756282a5bSdrh } 76856282a5bSdrh 76956282a5bSdrh /* 77055ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 77155ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 77255ef4d97Sdrh ** only. 77355ef4d97Sdrh */ 77455ef4d97Sdrh #ifdef SQLITE_TEST 77555ef4d97Sdrh int sqlite3_like_count = 0; 77655ef4d97Sdrh #endif 77755ef4d97Sdrh 7783f6b0874Sdanielk1977 7793f6b0874Sdanielk1977 /* 7803f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 7813f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 7823f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 7833f6b0874Sdanielk1977 ** 7843f6b0874Sdanielk1977 ** A LIKE B 7853f6b0874Sdanielk1977 ** 7863f6b0874Sdanielk1977 ** is implemented as like(B,A). 7873f6b0874Sdanielk1977 ** 78855ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 78955ef4d97Sdrh ** the GLOB operator. 7903f6b0874Sdanielk1977 */ 7913f6b0874Sdanielk1977 static void likeFunc( 7923f6b0874Sdanielk1977 sqlite3_context *context, 7933f6b0874Sdanielk1977 int argc, 7943f6b0874Sdanielk1977 sqlite3_value **argv 7953f6b0874Sdanielk1977 ){ 796beb818d1Sdrh const unsigned char *zA, *zB; 7970a32fa6dSdrh u32 escape = 0; 79827e62dbeSdrh int nPat; 799bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 800beb818d1Sdrh 8011f0feef8Sdrh zB = sqlite3_value_text(argv[0]); 8021f0feef8Sdrh zA = sqlite3_value_text(argv[1]); 8031f0feef8Sdrh 804beb818d1Sdrh /* Limit the length of the LIKE or GLOB pattern to avoid problems 805beb818d1Sdrh ** of deep recursion and N*N behavior in patternCompare(). 806beb818d1Sdrh */ 80727e62dbeSdrh nPat = sqlite3_value_bytes(argv[0]); 80827e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); 80927e62dbeSdrh testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); 81027e62dbeSdrh if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ 811beb818d1Sdrh sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); 812beb818d1Sdrh return; 813beb818d1Sdrh } 8141f0feef8Sdrh assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ 815beb818d1Sdrh 8167c6303c0Sdanielk1977 if( argc==3 ){ 8177c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 8187c6303c0Sdanielk1977 ** Otherwise, return an error. 8197c6303c0Sdanielk1977 */ 8207c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 8217a521cfbSdrh if( zEsc==0 ) return; 822ee85813cSdrh if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ 8237c6303c0Sdanielk1977 sqlite3_result_error(context, 8247c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 8257c6303c0Sdanielk1977 return; 8267c6303c0Sdanielk1977 } 82742610961Sdrh escape = sqlite3Utf8Read(&zEsc); 8287c6303c0Sdanielk1977 } 8293f6b0874Sdanielk1977 if( zA && zB ){ 83055ef4d97Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 83155ef4d97Sdrh #ifdef SQLITE_TEST 83255ef4d97Sdrh sqlite3_like_count++; 83355ef4d97Sdrh #endif 834beb818d1Sdrh 835b56fe1ffSdanielk1977 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); 83651ad0ecdSdanielk1977 } 8378912d106Sdrh } 8388912d106Sdrh 8398912d106Sdrh /* 8408912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 8418912d106Sdrh ** argument if the arguments are different. The result is NULL if the 8428912d106Sdrh ** arguments are equal to each other. 8438912d106Sdrh */ 844f9b596ebSdrh static void nullifFunc( 845f9b596ebSdrh sqlite3_context *context, 84662c14b34Sdanielk1977 int NotUsed, 847f9b596ebSdrh sqlite3_value **argv 848f9b596ebSdrh ){ 849dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 85062c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 851dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 852f4479501Sdrh sqlite3_result_value(context, argv[0]); 8538912d106Sdrh } 8540ac65892Sdrh } 8550ac65892Sdrh 856647cb0e1Sdrh /* 85747baebc2Sdrh ** Implementation of the sqlite_version() function. The result is the version 858647cb0e1Sdrh ** of the SQLite library that is running. 859647cb0e1Sdrh */ 860f9b596ebSdrh static void versionFunc( 861f9b596ebSdrh sqlite3_context *context, 86262c14b34Sdanielk1977 int NotUsed, 86362c14b34Sdanielk1977 sqlite3_value **NotUsed2 864f9b596ebSdrh ){ 86562c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 866ab2f1f95Sdrh /* IMP: R-48699-48617 This function is an SQL wrapper around the 867ab2f1f95Sdrh ** sqlite3_libversion() C-interface. */ 868ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); 869647cb0e1Sdrh } 870647cb0e1Sdrh 87147baebc2Sdrh /* 87247baebc2Sdrh ** Implementation of the sqlite_source_id() function. The result is a string 87347baebc2Sdrh ** that identifies the particular version of the source code used to build 87447baebc2Sdrh ** SQLite. 87547baebc2Sdrh */ 87647baebc2Sdrh static void sourceidFunc( 87747baebc2Sdrh sqlite3_context *context, 87847baebc2Sdrh int NotUsed, 87947baebc2Sdrh sqlite3_value **NotUsed2 88047baebc2Sdrh ){ 88147baebc2Sdrh UNUSED_PARAMETER2(NotUsed, NotUsed2); 882ab2f1f95Sdrh /* IMP: R-24470-31136 This function is an SQL wrapper around the 883ab2f1f95Sdrh ** sqlite3_sourceid() C interface. */ 884ab2f1f95Sdrh sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); 88547baebc2Sdrh } 88647baebc2Sdrh 887bdea6d13Sshaneh /* 8883ca84ef6Sdrh ** Implementation of the sqlite_log() function. This is a wrapper around 8893ca84ef6Sdrh ** sqlite3_log(). The return value is NULL. The function exists purely for 8903ca84ef6Sdrh ** its side-effects. 8913ca84ef6Sdrh */ 892840561f2Sdrh static void errlogFunc( 8933ca84ef6Sdrh sqlite3_context *context, 8943ca84ef6Sdrh int argc, 8953ca84ef6Sdrh sqlite3_value **argv 8963ca84ef6Sdrh ){ 8973ca84ef6Sdrh UNUSED_PARAMETER(argc); 8983ca84ef6Sdrh UNUSED_PARAMETER(context); 8993ca84ef6Sdrh sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); 9003ca84ef6Sdrh } 9013ca84ef6Sdrh 9023ca84ef6Sdrh /* 903dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_used() function. 904dc97a8cdSshaneh ** The result is an integer that identifies if the compiler option 905dc97a8cdSshaneh ** was used to build SQLite. 906bdea6d13Sshaneh */ 907dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 908dc97a8cdSshaneh static void compileoptionusedFunc( 909bdea6d13Sshaneh sqlite3_context *context, 910dc97a8cdSshaneh int argc, 911dc97a8cdSshaneh sqlite3_value **argv 912bdea6d13Sshaneh ){ 913dc97a8cdSshaneh const char *zOptName; 914dc97a8cdSshaneh assert( argc==1 ); 915dc97a8cdSshaneh UNUSED_PARAMETER(argc); 916a3e414cdSdrh /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL 917a3e414cdSdrh ** function is a wrapper around the sqlite3_compileoption_used() C/C++ 918a3e414cdSdrh ** function. 919a3e414cdSdrh */ 920264a2d4dSdrh if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 921dc97a8cdSshaneh sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); 922bdea6d13Sshaneh } 923dc97a8cdSshaneh } 924dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 925dc97a8cdSshaneh 926dc97a8cdSshaneh /* 927dc97a8cdSshaneh ** Implementation of the sqlite_compileoption_get() function. 928dc97a8cdSshaneh ** The result is a string that identifies the compiler options 929dc97a8cdSshaneh ** used to build SQLite. 930dc97a8cdSshaneh */ 931dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 932dc97a8cdSshaneh static void compileoptiongetFunc( 933dc97a8cdSshaneh sqlite3_context *context, 934dc97a8cdSshaneh int argc, 935dc97a8cdSshaneh sqlite3_value **argv 936dc97a8cdSshaneh ){ 937dc97a8cdSshaneh int n; 938dc97a8cdSshaneh assert( argc==1 ); 939dc97a8cdSshaneh UNUSED_PARAMETER(argc); 940a3e414cdSdrh /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function 941a3e414cdSdrh ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. 942a3e414cdSdrh */ 943dc97a8cdSshaneh n = sqlite3_value_int(argv[0]); 944dc97a8cdSshaneh sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); 945dc97a8cdSshaneh } 946dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 947bdea6d13Sshaneh 948137c728fSdrh /* Array for converting from half-bytes (nybbles) into ASCII hex 949137c728fSdrh ** digits. */ 950137c728fSdrh static const char hexdigits[] = { 951137c728fSdrh '0', '1', '2', '3', '4', '5', '6', '7', 952137c728fSdrh '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 953137c728fSdrh }; 954d641d646Sdanielk1977 95547394703Sdrh /* 95647394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 95747394703Sdrh ** argument. If the argument is numeric, the return value is the same as 95847394703Sdrh ** the argument. If the argument is NULL, the return value is the string 95947394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 96047394703Sdrh ** single-quote escapes. 96147394703Sdrh */ 9620ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 963a0df4ccfSdrh assert( argc==1 ); 9641d34fdecSdrh UNUSED_PARAMETER(argc); 965f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 9669c054830Sdrh case SQLITE_FLOAT: { 96772b3fbc7Sdrh double r1, r2; 96872b3fbc7Sdrh char zBuf[50]; 9692b434a7eSmistachkin r1 = sqlite3_value_double(argv[0]); 97072b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); 97172b3fbc7Sdrh sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); 97272b3fbc7Sdrh if( r1!=r2 ){ 97372b3fbc7Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); 97472b3fbc7Sdrh } 97572b3fbc7Sdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 97672b3fbc7Sdrh break; 97772b3fbc7Sdrh } 97872b3fbc7Sdrh case SQLITE_INTEGER: { 979f4479501Sdrh sqlite3_result_value(context, argv[0]); 980f9b596ebSdrh break; 981f9b596ebSdrh } 9823f41e976Sdanielk1977 case SQLITE_BLOB: { 9833f41e976Sdanielk1977 char *zText = 0; 9843f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 9851f0feef8Sdrh int nBlob = sqlite3_value_bytes(argv[0]); 9861f0feef8Sdrh assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 987b1a6c3c1Sdrh zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 988a1644fd8Sdanielk1977 if( zText ){ 9893f41e976Sdanielk1977 int i; 9903f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 9913f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 9923f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 9933f41e976Sdanielk1977 } 9943f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 9953f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 9963f41e976Sdanielk1977 zText[0] = 'X'; 9973f41e976Sdanielk1977 zText[1] = '\''; 998d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 99917435752Sdrh sqlite3_free(zText); 10003f41e976Sdanielk1977 } 10013f41e976Sdanielk1977 break; 10023f41e976Sdanielk1977 } 10039c054830Sdrh case SQLITE_TEXT: { 1004023ae03aSdrh int i,j; 1005023ae03aSdrh u64 n; 10062646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 100747394703Sdrh char *z; 1008f9b596ebSdrh 10097a521cfbSdrh if( zArg==0 ) return; 1010023ae03aSdrh for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 1011b1a6c3c1Sdrh z = contextMalloc(context, ((i64)i)+((i64)n)+3); 1012a1644fd8Sdanielk1977 if( z ){ 101347394703Sdrh z[0] = '\''; 101451ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 101551ad0ecdSdanielk1977 z[j++] = zArg[i]; 101651ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 101747394703Sdrh z[j++] = '\''; 101847394703Sdrh } 101947394703Sdrh } 102047394703Sdrh z[j++] = '\''; 102147394703Sdrh z[j] = 0; 1022a1644fd8Sdanielk1977 sqlite3_result_text(context, z, j, sqlite3_free); 1023a1644fd8Sdanielk1977 } 1024a0df4ccfSdrh break; 1025a0df4ccfSdrh } 1026a0df4ccfSdrh default: { 1027a0df4ccfSdrh assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 1028a0df4ccfSdrh sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 1029a0df4ccfSdrh break; 103047394703Sdrh } 103147394703Sdrh } 1032f9b596ebSdrh } 103347394703Sdrh 1034137c728fSdrh /* 1035d495d8c9Sdrh ** The unicode() function. Return the integer unicode code-point value 1036d495d8c9Sdrh ** for the first character of the input string. 1037d495d8c9Sdrh */ 1038d495d8c9Sdrh static void unicodeFunc( 1039d495d8c9Sdrh sqlite3_context *context, 1040d495d8c9Sdrh int argc, 1041d495d8c9Sdrh sqlite3_value **argv 1042d495d8c9Sdrh ){ 1043d495d8c9Sdrh const unsigned char *z = sqlite3_value_text(argv[0]); 10441d59d036Sdrh (void)argc; 1045d495d8c9Sdrh if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); 1046d495d8c9Sdrh } 1047d495d8c9Sdrh 1048d495d8c9Sdrh /* 1049d495d8c9Sdrh ** The char() function takes zero or more arguments, each of which is 1050d495d8c9Sdrh ** an integer. It constructs a string where each character of the string 1051d495d8c9Sdrh ** is the unicode character for the corresponding integer argument. 1052d495d8c9Sdrh */ 1053d495d8c9Sdrh static void charFunc( 1054d495d8c9Sdrh sqlite3_context *context, 1055d495d8c9Sdrh int argc, 1056d495d8c9Sdrh sqlite3_value **argv 1057d495d8c9Sdrh ){ 1058d495d8c9Sdrh unsigned char *z, *zOut; 1059d495d8c9Sdrh int i; 1060*f3cdcdccSdrh zOut = z = sqlite3_malloc64( argc*4+1 ); 1061d495d8c9Sdrh if( z==0 ){ 1062d495d8c9Sdrh sqlite3_result_error_nomem(context); 1063d495d8c9Sdrh return; 1064d495d8c9Sdrh } 1065d495d8c9Sdrh for(i=0; i<argc; i++){ 1066c9545442Smistachkin sqlite3_int64 x; 1067d495d8c9Sdrh unsigned c; 1068d495d8c9Sdrh x = sqlite3_value_int64(argv[i]); 1069d495d8c9Sdrh if( x<0 || x>0x10ffff ) x = 0xfffd; 1070d495d8c9Sdrh c = (unsigned)(x & 0x1fffff); 1071fe7a5d11Sdrh if( c<0x00080 ){ 1072fe7a5d11Sdrh *zOut++ = (u8)(c&0xFF); 1073fe7a5d11Sdrh }else if( c<0x00800 ){ 1074fe7a5d11Sdrh *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); 1075fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1076fe7a5d11Sdrh }else if( c<0x10000 ){ 1077fe7a5d11Sdrh *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); 1078fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1079fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1080d495d8c9Sdrh }else{ 1081fe7a5d11Sdrh *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); 1082fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); 1083fe7a5d11Sdrh *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 1084fe7a5d11Sdrh *zOut++ = 0x80 + (u8)(c & 0x3F); 1085fe7a5d11Sdrh } \ 1086d495d8c9Sdrh } 1087bbf483f8Sdrh sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); 1088d495d8c9Sdrh } 1089d495d8c9Sdrh 1090d495d8c9Sdrh /* 1091137c728fSdrh ** The hex() function. Interpret the argument as a blob. Return 1092137c728fSdrh ** a hexadecimal rendering as text. 1093137c728fSdrh */ 1094137c728fSdrh static void hexFunc( 1095137c728fSdrh sqlite3_context *context, 1096137c728fSdrh int argc, 1097137c728fSdrh sqlite3_value **argv 1098137c728fSdrh ){ 1099137c728fSdrh int i, n; 1100137c728fSdrh const unsigned char *pBlob; 1101137c728fSdrh char *zHex, *z; 1102137c728fSdrh assert( argc==1 ); 1103f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 11041f0feef8Sdrh pBlob = sqlite3_value_blob(argv[0]); 1105137c728fSdrh n = sqlite3_value_bytes(argv[0]); 11061f0feef8Sdrh assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 1107b1a6c3c1Sdrh z = zHex = contextMalloc(context, ((i64)n)*2 + 1); 1108a1644fd8Sdanielk1977 if( zHex ){ 1109137c728fSdrh for(i=0; i<n; i++, pBlob++){ 1110137c728fSdrh unsigned char c = *pBlob; 1111137c728fSdrh *(z++) = hexdigits[(c>>4)&0xf]; 1112137c728fSdrh *(z++) = hexdigits[c&0xf]; 1113137c728fSdrh } 1114137c728fSdrh *z = 0; 1115137c728fSdrh sqlite3_result_text(context, zHex, n*2, sqlite3_free); 1116137c728fSdrh } 1117a1644fd8Sdanielk1977 } 1118137c728fSdrh 111926b6d90dSdrh /* 11208cff382eSdrh ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 11218cff382eSdrh */ 11228cff382eSdrh static void zeroblobFunc( 11238cff382eSdrh sqlite3_context *context, 11248cff382eSdrh int argc, 11258cff382eSdrh sqlite3_value **argv 11268cff382eSdrh ){ 112798640a3fSdrh i64 n; 112827e62dbeSdrh sqlite3 *db = sqlite3_context_db_handle(context); 11298cff382eSdrh assert( argc==1 ); 1130f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 113198640a3fSdrh n = sqlite3_value_int64(argv[0]); 113227e62dbeSdrh testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] ); 113327e62dbeSdrh testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); 113427e62dbeSdrh if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 113598640a3fSdrh sqlite3_result_error_toobig(context); 113698640a3fSdrh }else{ 1137ab2f1f95Sdrh sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */ 11388cff382eSdrh } 113998640a3fSdrh } 11408cff382eSdrh 11418cff382eSdrh /* 114226b6d90dSdrh ** The replace() function. Three arguments are all strings: call 114326b6d90dSdrh ** them A, B, and C. The result is also a string which is derived 1144f7b5496eSdrh ** from A by replacing every occurrence of B with C. The match 114526b6d90dSdrh ** must be exact. Collating sequences are not used. 114626b6d90dSdrh */ 114726b6d90dSdrh static void replaceFunc( 114826b6d90dSdrh sqlite3_context *context, 114926b6d90dSdrh int argc, 115026b6d90dSdrh sqlite3_value **argv 115126b6d90dSdrh ){ 115226b6d90dSdrh const unsigned char *zStr; /* The input string A */ 115326b6d90dSdrh const unsigned char *zPattern; /* The pattern string B */ 115426b6d90dSdrh const unsigned char *zRep; /* The replacement string C */ 115526b6d90dSdrh unsigned char *zOut; /* The output */ 115626b6d90dSdrh int nStr; /* Size of zStr */ 115726b6d90dSdrh int nPattern; /* Size of zPattern */ 115826b6d90dSdrh int nRep; /* Size of zRep */ 11592e6400baSdrh i64 nOut; /* Maximum size of zOut */ 116026b6d90dSdrh int loopLimit; /* Last zStr[] that might match zPattern[] */ 116126b6d90dSdrh int i, j; /* Loop counters */ 116226b6d90dSdrh 116326b6d90dSdrh assert( argc==3 ); 1164f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 116526b6d90dSdrh zStr = sqlite3_value_text(argv[0]); 11667a521cfbSdrh if( zStr==0 ) return; 11671f0feef8Sdrh nStr = sqlite3_value_bytes(argv[0]); 11681f0feef8Sdrh assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ 116926b6d90dSdrh zPattern = sqlite3_value_text(argv[1]); 1170a605fe8dSdrh if( zPattern==0 ){ 11712333606eSdrh assert( sqlite3_value_type(argv[1])==SQLITE_NULL 11722333606eSdrh || sqlite3_context_db_handle(context)->mallocFailed ); 1173a605fe8dSdrh return; 1174a605fe8dSdrh } 1175a605fe8dSdrh if( zPattern[0]==0 ){ 1176a605fe8dSdrh assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); 1177a605fe8dSdrh sqlite3_result_value(context, argv[0]); 1178a605fe8dSdrh return; 1179a605fe8dSdrh } 11801f0feef8Sdrh nPattern = sqlite3_value_bytes(argv[1]); 11811f0feef8Sdrh assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ 118226b6d90dSdrh zRep = sqlite3_value_text(argv[2]); 11837a521cfbSdrh if( zRep==0 ) return; 11841f0feef8Sdrh nRep = sqlite3_value_bytes(argv[2]); 11851f0feef8Sdrh assert( zRep==sqlite3_value_text(argv[2]) ); 11862e6400baSdrh nOut = nStr + 1; 11872e6400baSdrh assert( nOut<SQLITE_MAX_LENGTH ); 1188b1a6c3c1Sdrh zOut = contextMalloc(context, (i64)nOut); 11892e6400baSdrh if( zOut==0 ){ 11902e6400baSdrh return; 119126b6d90dSdrh } 119226b6d90dSdrh loopLimit = nStr - nPattern; 119326b6d90dSdrh for(i=j=0; i<=loopLimit; i++){ 119426b6d90dSdrh if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 119526b6d90dSdrh zOut[j++] = zStr[i]; 119626b6d90dSdrh }else{ 11974a50aac5Sdrh u8 *zOld; 1198bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 11992e6400baSdrh nOut += nRep - nPattern; 120027e62dbeSdrh testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); 120127e62dbeSdrh testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); 120227e62dbeSdrh if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1203a0206bc8Sdrh sqlite3_result_error_toobig(context); 1204b975598eSdrh sqlite3_free(zOut); 120517374e8fSdanielk1977 return; 120617374e8fSdanielk1977 } 12074a50aac5Sdrh zOld = zOut; 1208*f3cdcdccSdrh zOut = sqlite3_realloc64(zOut, (int)nOut); 12092e6400baSdrh if( zOut==0 ){ 1210a1644fd8Sdanielk1977 sqlite3_result_error_nomem(context); 1211b975598eSdrh sqlite3_free(zOld); 12122e6400baSdrh return; 12132e6400baSdrh } 121426b6d90dSdrh memcpy(&zOut[j], zRep, nRep); 121526b6d90dSdrh j += nRep; 121626b6d90dSdrh i += nPattern-1; 121726b6d90dSdrh } 121826b6d90dSdrh } 12192e6400baSdrh assert( j+nStr-i+1==nOut ); 122026b6d90dSdrh memcpy(&zOut[j], &zStr[i], nStr-i); 122126b6d90dSdrh j += nStr - i; 122226b6d90dSdrh assert( j<=nOut ); 122326b6d90dSdrh zOut[j] = 0; 122426b6d90dSdrh sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 122526b6d90dSdrh } 122626b6d90dSdrh 1227309b3386Sdrh /* 1228309b3386Sdrh ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 1229309b3386Sdrh ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 1230309b3386Sdrh */ 1231309b3386Sdrh static void trimFunc( 1232309b3386Sdrh sqlite3_context *context, 1233309b3386Sdrh int argc, 1234309b3386Sdrh sqlite3_value **argv 1235309b3386Sdrh ){ 1236309b3386Sdrh const unsigned char *zIn; /* Input string */ 1237309b3386Sdrh const unsigned char *zCharSet; /* Set of characters to trim */ 1238309b3386Sdrh int nIn; /* Number of bytes in input */ 12397209c697Sdrh int flags; /* 1: trimleft 2: trimright 3: trim */ 1240d1e3a616Sdrh int i; /* Loop counter */ 12411bd10f8aSdrh unsigned char *aLen = 0; /* Length of each character in zCharSet */ 12421bd10f8aSdrh unsigned char **azChar = 0; /* Individual characters in zCharSet */ 1243d1e3a616Sdrh int nChar; /* Number of characters in zCharSet */ 1244d1e3a616Sdrh 1245309b3386Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 1246309b3386Sdrh return; 1247309b3386Sdrh } 1248309b3386Sdrh zIn = sqlite3_value_text(argv[0]); 12497a521cfbSdrh if( zIn==0 ) return; 12501f0feef8Sdrh nIn = sqlite3_value_bytes(argv[0]); 12511f0feef8Sdrh assert( zIn==sqlite3_value_text(argv[0]) ); 1252309b3386Sdrh if( argc==1 ){ 1253d1e3a616Sdrh static const unsigned char lenOne[] = { 1 }; 1254a4de4532Sdanielk1977 static unsigned char * const azOne[] = { (u8*)" " }; 1255d1e3a616Sdrh nChar = 1; 12568cff382eSdrh aLen = (u8*)lenOne; 1257bc67da48Sdanielk1977 azChar = (unsigned char **)azOne; 1258d1e3a616Sdrh zCharSet = 0; 12597a521cfbSdrh }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 1260309b3386Sdrh return; 1261d1e3a616Sdrh }else{ 1262d1e3a616Sdrh const unsigned char *z; 1263d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 12644a919118Sdrh SQLITE_SKIP_UTF8(z); 1265309b3386Sdrh } 1266d1e3a616Sdrh if( nChar>0 ){ 1267b1a6c3c1Sdrh azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); 1268d1e3a616Sdrh if( azChar==0 ){ 1269d1e3a616Sdrh return; 1270d1e3a616Sdrh } 1271d1e3a616Sdrh aLen = (unsigned char*)&azChar[nChar]; 1272d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 1273bc67da48Sdanielk1977 azChar[nChar] = (unsigned char *)z; 12744a919118Sdrh SQLITE_SKIP_UTF8(z); 12753abbd39aSdrh aLen[nChar] = (u8)(z - azChar[nChar]); 1276d1e3a616Sdrh } 1277d1e3a616Sdrh } 1278d1e3a616Sdrh } 1279d1e3a616Sdrh if( nChar>0 ){ 12801fc4129dSshane flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); 1281309b3386Sdrh if( flags & 1 ){ 1282d1e3a616Sdrh while( nIn>0 ){ 12831bd10f8aSdrh int len = 0; 1284d1e3a616Sdrh for(i=0; i<nChar; i++){ 1285d1e3a616Sdrh len = aLen[i]; 128627e62dbeSdrh if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break; 1287d1e3a616Sdrh } 1288d1e3a616Sdrh if( i>=nChar ) break; 1289d1e3a616Sdrh zIn += len; 1290d1e3a616Sdrh nIn -= len; 1291309b3386Sdrh } 1292309b3386Sdrh } 1293309b3386Sdrh if( flags & 2 ){ 1294d1e3a616Sdrh while( nIn>0 ){ 12951bd10f8aSdrh int len = 0; 1296d1e3a616Sdrh for(i=0; i<nChar; i++){ 1297d1e3a616Sdrh len = aLen[i]; 1298d1e3a616Sdrh if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 1299309b3386Sdrh } 1300d1e3a616Sdrh if( i>=nChar ) break; 1301d1e3a616Sdrh nIn -= len; 1302d1e3a616Sdrh } 1303d1e3a616Sdrh } 1304d1e3a616Sdrh if( zCharSet ){ 1305d1e3a616Sdrh sqlite3_free(azChar); 1306309b3386Sdrh } 1307309b3386Sdrh } 1308309b3386Sdrh sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 1309309b3386Sdrh } 131026b6d90dSdrh 1311a4de4532Sdanielk1977 13122ba3cccfSdrh /* IMP: R-25361-16150 This function is omitted from SQLite by default. It 13132ba3cccfSdrh ** is only available if the SQLITE_SOUNDEX compile-time option is used 13142ba3cccfSdrh ** when SQLite is built. 13152ba3cccfSdrh */ 1316d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1317d24cc427Sdrh /* 1318d24cc427Sdrh ** Compute the soundex encoding of a word. 13192ba3cccfSdrh ** 13202ba3cccfSdrh ** IMP: R-59782-00072 The soundex(X) function returns a string that is the 13212ba3cccfSdrh ** soundex encoding of the string X. 1322d24cc427Sdrh */ 1323137c728fSdrh static void soundexFunc( 1324137c728fSdrh sqlite3_context *context, 1325137c728fSdrh int argc, 1326137c728fSdrh sqlite3_value **argv 1327137c728fSdrh ){ 1328d24cc427Sdrh char zResult[8]; 13294c755c0fSdrh const u8 *zIn; 1330d24cc427Sdrh int i, j; 1331d24cc427Sdrh static const unsigned char iCode[] = { 1332d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1333d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1334d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1335d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1336d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1337d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1338d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1339d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1340d24cc427Sdrh }; 1341d24cc427Sdrh assert( argc==1 ); 13424c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 1343bdf67e0eSdrh if( zIn==0 ) zIn = (u8*)""; 1344dc86e2b2Sdrh for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} 1345d24cc427Sdrh if( zIn[i] ){ 1346bdf67e0eSdrh u8 prevcode = iCode[zIn[i]&0x7f]; 134778ca0e7eSdanielk1977 zResult[0] = sqlite3Toupper(zIn[i]); 1348d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 1349d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 1350d24cc427Sdrh if( code>0 ){ 1351bdf67e0eSdrh if( code!=prevcode ){ 1352bdf67e0eSdrh prevcode = code; 1353d24cc427Sdrh zResult[j++] = code + '0'; 1354d24cc427Sdrh } 1355bdf67e0eSdrh }else{ 1356bdf67e0eSdrh prevcode = 0; 1357bdf67e0eSdrh } 1358d24cc427Sdrh } 1359d24cc427Sdrh while( j<4 ){ 1360d24cc427Sdrh zResult[j++] = '0'; 1361d24cc427Sdrh } 1362d24cc427Sdrh zResult[j] = 0; 1363d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 1364d24cc427Sdrh }else{ 13652ba3cccfSdrh /* IMP: R-64894-50321 The string "?000" is returned if the argument 13662ba3cccfSdrh ** is NULL or contains no ASCII alphabetic characters. */ 1367d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 1368d24cc427Sdrh } 1369d24cc427Sdrh } 13702ba3cccfSdrh #endif /* SQLITE_SOUNDEX */ 1371d24cc427Sdrh 1372fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 1373fdb83b2fSdrh /* 1374fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL. 1375fdb83b2fSdrh */ 1376fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 137765fd59f7Sdanielk1977 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 13787a521cfbSdrh const char *zProc; 1379fa4a4b91Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 1380fdb83b2fSdrh char *zErrMsg = 0; 1381fdb83b2fSdrh 1382fdb83b2fSdrh if( argc==2 ){ 138365fd59f7Sdanielk1977 zProc = (const char *)sqlite3_value_text(argv[1]); 13847a521cfbSdrh }else{ 13857a521cfbSdrh zProc = 0; 1386fdb83b2fSdrh } 13877a521cfbSdrh if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 1388fdb83b2fSdrh sqlite3_result_error(context, zErrMsg, -1); 1389fdb83b2fSdrh sqlite3_free(zErrMsg); 1390fdb83b2fSdrh } 1391fdb83b2fSdrh } 1392fdb83b2fSdrh #endif 1393fdb83b2fSdrh 139401427a62Sdanielk1977 13950ac65892Sdrh /* 1396d3a149efSdrh ** An instance of the following structure holds the context of a 1397dd5baa95Sdrh ** sum() or avg() aggregate computation. 1398dd5baa95Sdrh */ 1399dd5baa95Sdrh typedef struct SumCtx SumCtx; 1400dd5baa95Sdrh struct SumCtx { 14018c08e861Sdrh double rSum; /* Floating point sum */ 14028c08e861Sdrh i64 iSum; /* Integer sum */ 1403cf85a51cSdrh i64 cnt; /* Number of elements summed */ 14048c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 14058c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 1406dd5baa95Sdrh }; 1407dd5baa95Sdrh 1408dd5baa95Sdrh /* 1409a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 1410a97fdd3bSdrh ** 1411a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 1412a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 1413a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 1414a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 1415c806d857Sdrh ** value. TOTAL never fails, but SUM might through an exception if 1416c806d857Sdrh ** it overflows an integer. 1417dd5baa95Sdrh */ 14180ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1419dd5baa95Sdrh SumCtx *p; 14203d1d95e6Sdrh int type; 14213f219f46Sdrh assert( argc==1 ); 1422f3d3c27aSdanielk1977 UNUSED_PARAMETER(argc); 14234f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 142429d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 14253d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 1426739105c7Sdrh p->cnt++; 142729d72108Sdrh if( type==SQLITE_INTEGER ){ 14288c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 14298c08e861Sdrh p->rSum += v; 1430158b9cb9Sdrh if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ 1431158b9cb9Sdrh p->overflow = 1; 143229d72108Sdrh } 143329d72108Sdrh }else{ 14348c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 143529d72108Sdrh p->approx = 1; 14363f219f46Sdrh } 1437739105c7Sdrh } 1438dd5baa95Sdrh } 14390ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 1440dd5baa95Sdrh SumCtx *p; 1441abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1442c2bd913aSdrh if( p && p->cnt>0 ){ 14438c08e861Sdrh if( p->overflow ){ 14448c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 14458c08e861Sdrh }else if( p->approx ){ 14468c08e861Sdrh sqlite3_result_double(context, p->rSum); 1447c2bd913aSdrh }else{ 14488c08e861Sdrh sqlite3_result_int64(context, p->iSum); 14493d1d95e6Sdrh } 1450dd5baa95Sdrh } 1451c2bd913aSdrh } 14520ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 1453dd5baa95Sdrh SumCtx *p; 1454abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1455739105c7Sdrh if( p && p->cnt>0 ){ 14568c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 1457dd5baa95Sdrh } 1458dd5baa95Sdrh } 1459a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 1460a97fdd3bSdrh SumCtx *p; 1461a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 1462fbd60f82Sshane /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 1463fbd60f82Sshane sqlite3_result_double(context, p ? p->rSum : (double)0); 1464a97fdd3bSdrh } 1465dd5baa95Sdrh 1466dd5baa95Sdrh /* 14670bce8354Sdrh ** The following structure keeps track of state information for the 14680bce8354Sdrh ** count() aggregate function. 14690bce8354Sdrh */ 14700bce8354Sdrh typedef struct CountCtx CountCtx; 14710bce8354Sdrh struct CountCtx { 1472fc6ad39cSdrh i64 n; 14730bce8354Sdrh }; 1474dd5baa95Sdrh 14750bce8354Sdrh /* 14760bce8354Sdrh ** Routines to implement the count() aggregate function. 14770bce8354Sdrh */ 14780ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 14790bce8354Sdrh CountCtx *p; 14804f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 14819c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 14820bce8354Sdrh p->n++; 14830bce8354Sdrh } 14842e79c3d5Sdrh 1485d3264c7cSdrh #ifndef SQLITE_OMIT_DEPRECATED 14862e79c3d5Sdrh /* The sqlite3_aggregate_count() function is deprecated. But just to make 14872e79c3d5Sdrh ** sure it still operates correctly, verify that its count agrees with our 14882e79c3d5Sdrh ** internal count when using count(*) and when the total count can be 14892e79c3d5Sdrh ** expressed as a 32-bit integer. */ 14902e79c3d5Sdrh assert( argc==1 || p==0 || p->n>0x7fffffff 14912e79c3d5Sdrh || p->n==sqlite3_aggregate_count(context) ); 1492d3264c7cSdrh #endif 14930bce8354Sdrh } 14940ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 14950bce8354Sdrh CountCtx *p; 1496abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1497fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 14980bce8354Sdrh } 14990bce8354Sdrh 15000bce8354Sdrh /* 15010bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 15020bce8354Sdrh */ 150362c14b34Sdanielk1977 static void minmaxStep( 150462c14b34Sdanielk1977 sqlite3_context *context, 150562c14b34Sdanielk1977 int NotUsed, 150662c14b34Sdanielk1977 sqlite3_value **argv 150762c14b34Sdanielk1977 ){ 150888208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 15099eb516c0Sdrh Mem *pBest; 151062c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 15119eb516c0Sdrh 15129eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 15133aeab9e4Sdanielk1977 if( !pBest ) return; 1514268380caSdrh 151594a6d998Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 151694a6d998Sdrh if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); 151794a6d998Sdrh }else if( pBest->flags ){ 15189eb516c0Sdrh int max; 15199eb516c0Sdrh int cmp; 1520dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 15217e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 15227e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 15237e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 15247e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 15257e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 15267e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 15277e18c259Sdanielk1977 ** aggregate, or 0 for min(). 15287e18c259Sdanielk1977 */ 1529309b3386Sdrh max = sqlite3_user_data(context)!=0; 1530dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 153188208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 1532b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 15337a95789cSdrh }else{ 15347a95789cSdrh sqlite3SkipAccumulatorLoad(context); 153588208050Sdanielk1977 } 15360bce8354Sdrh }else{ 1537035e563bSdrh pBest->db = sqlite3_context_db_handle(context); 1538b21c8cd4Sdrh sqlite3VdbeMemCopy(pBest, pArg); 15390bce8354Sdrh } 15400bce8354Sdrh } 15410ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){ 154288208050Sdanielk1977 sqlite3_value *pRes; 1543abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1544abfcea25Sdrh if( pRes ){ 154594a6d998Sdrh if( pRes->flags ){ 1546f4479501Sdrh sqlite3_result_value(context, pRes); 15470bce8354Sdrh } 1548b20e56b4Sdanielk1977 sqlite3VdbeMemRelease(pRes); 15490bce8354Sdrh } 1550abfcea25Sdrh } 1551dd5baa95Sdrh 1552b0689696Sdrh /* 1553b0689696Sdrh ** group_concat(EXPR, ?SEPARATOR?) 1554b0689696Sdrh */ 1555b0689696Sdrh static void groupConcatStep( 1556b0689696Sdrh sqlite3_context *context, 1557b0689696Sdrh int argc, 1558b0689696Sdrh sqlite3_value **argv 1559b0689696Sdrh ){ 1560b0689696Sdrh const char *zVal; 1561ade86483Sdrh StrAccum *pAccum; 1562b0689696Sdrh const char *zSep; 156307d3117aSdrh int nVal, nSep; 156407d3117aSdrh assert( argc==1 || argc==2 ); 156507d3117aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 1566ade86483Sdrh pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 1567ade86483Sdrh 1568ade86483Sdrh if( pAccum ){ 1569bb4957f8Sdrh sqlite3 *db = sqlite3_context_db_handle(context); 15708bfd7190Sdrh int firstTerm = pAccum->useMalloc==0; 1571b975598eSdrh pAccum->useMalloc = 2; 1572bb4957f8Sdrh pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 15738bfd7190Sdrh if( !firstTerm ){ 157407d3117aSdrh if( argc==2 ){ 157507d3117aSdrh zSep = (char*)sqlite3_value_text(argv[1]); 157607d3117aSdrh nSep = sqlite3_value_bytes(argv[1]); 1577b0689696Sdrh }else{ 1578b0689696Sdrh zSep = ","; 1579ade86483Sdrh nSep = 1; 1580b0689696Sdrh } 1581a9ab481fSdrh if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); 1582b0689696Sdrh } 158307d3117aSdrh zVal = (char*)sqlite3_value_text(argv[0]); 158407d3117aSdrh nVal = sqlite3_value_bytes(argv[0]); 15858009c9b4Sdrh if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal); 1586b0689696Sdrh } 1587b0689696Sdrh } 1588b0689696Sdrh static void groupConcatFinalize(sqlite3_context *context){ 1589ade86483Sdrh StrAccum *pAccum; 1590ade86483Sdrh pAccum = sqlite3_aggregate_context(context, 0); 1591ade86483Sdrh if( pAccum ){ 1592b49bc86aSdrh if( pAccum->accError==STRACCUM_TOOBIG ){ 1593ade86483Sdrh sqlite3_result_error_toobig(context); 1594b49bc86aSdrh }else if( pAccum->accError==STRACCUM_NOMEM ){ 1595ade86483Sdrh sqlite3_result_error_nomem(context); 1596ade86483Sdrh }else{ 1597ade86483Sdrh sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 1598ade86483Sdrh sqlite3_free); 1599b0689696Sdrh } 1600b0689696Sdrh } 1601ade86483Sdrh } 16024e5ffc5fSdrh 1603d3a149efSdrh /* 1604a4741840Sdrh ** This routine does per-connection function registration. Most 1605a4741840Sdrh ** of the built-in functions above are part of the global function set. 1606a4741840Sdrh ** This routine only deals with those that are not global. 1607dc04c583Sdrh */ 16089bb575fdSdrh void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ 1609832a58a6Sdanielk1977 int rc = sqlite3_overload_function(db, "MATCH", 2); 1610832a58a6Sdanielk1977 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 1611832a58a6Sdanielk1977 if( rc==SQLITE_NOMEM ){ 16121e536953Sdanielk1977 db->mallocFailed = 1; 1613832a58a6Sdanielk1977 } 1614832a58a6Sdanielk1977 } 161555ef4d97Sdrh 161655ef4d97Sdrh /* 161755ef4d97Sdrh ** Set the LIKEOPT flag on the 2-argument function with the given name. 161855ef4d97Sdrh */ 16191bd10f8aSdrh static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ 162055ef4d97Sdrh FuncDef *pDef; 1621ea678832Sdrh pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName), 1622ea678832Sdrh 2, SQLITE_UTF8, 0); 1623d27135adSdrh if( ALWAYS(pDef) ){ 1624d36e1041Sdrh pDef->funcFlags |= flagVal; 162555ef4d97Sdrh } 162655ef4d97Sdrh } 162755ef4d97Sdrh 162855ef4d97Sdrh /* 162955ef4d97Sdrh ** Register the built-in LIKE and GLOB functions. The caseSensitive 163055ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 163155ef4d97Sdrh ** sensitive. GLOB is always case sensitive. 163255ef4d97Sdrh */ 163355ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 163455ef4d97Sdrh struct compareInfo *pInfo; 163555ef4d97Sdrh if( caseSensitive ){ 163655ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 163755ef4d97Sdrh }else{ 163855ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 163955ef4d97Sdrh } 1640901e994bSdrh sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); 1641901e994bSdrh sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); 1642901e994bSdrh sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 1643d2199f0fSdan (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); 1644d64fe2f3Sdrh setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 1645d64fe2f3Sdrh setLikeOptFlag(db, "like", 1646d64fe2f3Sdrh caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 164755ef4d97Sdrh } 164855ef4d97Sdrh 164955ef4d97Sdrh /* 165055ef4d97Sdrh ** pExpr points to an expression which implements a function. If 165155ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 165255ef4d97Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and 165355ef4d97Sdrh ** return TRUE. If the function is not a LIKE-style function then 165455ef4d97Sdrh ** return FALSE. 165516897072Sdrh ** 165616897072Sdrh ** *pIsNocase is set to true if uppercase and lowercase are equivalent for 165716897072Sdrh ** the function (default for LIKE). If the function makes the distinction 165816897072Sdrh ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to 165916897072Sdrh ** false. 166055ef4d97Sdrh */ 1661d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 166255ef4d97Sdrh FuncDef *pDef; 16636ab3a2ecSdanielk1977 if( pExpr->op!=TK_FUNCTION 16646ab3a2ecSdanielk1977 || !pExpr->x.pList 16656ab3a2ecSdanielk1977 || pExpr->x.pList->nExpr!=2 16666ab3a2ecSdanielk1977 ){ 166755ef4d97Sdrh return 0; 166855ef4d97Sdrh } 16696ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 167033e619fcSdrh pDef = sqlite3FindFunction(db, pExpr->u.zToken, 167133e619fcSdrh sqlite3Strlen30(pExpr->u.zToken), 1672b7916a78Sdrh 2, SQLITE_UTF8, 0); 1673d36e1041Sdrh if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ 167455ef4d97Sdrh return 0; 167555ef4d97Sdrh } 167655ef4d97Sdrh 167755ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 167855ef4d97Sdrh ** the first three statements in the compareInfo structure. The 167955ef4d97Sdrh ** asserts() that follow verify that assumption 168055ef4d97Sdrh */ 168155ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 168255ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 168355ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 168455ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1685d36e1041Sdrh *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; 168655ef4d97Sdrh return 1; 1687dc04c583Sdrh } 16888c0a791aSdanielk1977 168993ce741bSdanielk1977 /* 169060ec914cSpeter.d.reid ** All of the FuncDef structures in the aBuiltinFunc[] array above 169193ce741bSdanielk1977 ** to the global function hash table. This occurs at start-time (as 169293ce741bSdanielk1977 ** a consequence of calling sqlite3_initialize()). 169393ce741bSdanielk1977 ** 169493ce741bSdanielk1977 ** After this routine runs 169593ce741bSdanielk1977 */ 169693ce741bSdanielk1977 void sqlite3RegisterGlobalFunctions(void){ 16978c0a791aSdanielk1977 /* 1698777c5386Sdrh ** The following array holds FuncDef structures for all of the functions 1699777c5386Sdrh ** defined in this file. 17008c0a791aSdanielk1977 ** 1701777c5386Sdrh ** The array cannot be constant since changes are made to the 1702777c5386Sdrh ** FuncDef.pHash elements at start-time. The elements of this array 1703777c5386Sdrh ** are read-only after initialization is complete. 17048c0a791aSdanielk1977 */ 170593ce741bSdanielk1977 static SQLITE_WSD FuncDef aBuiltinFunc[] = { 17068c0a791aSdanielk1977 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 17078c0a791aSdanielk1977 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 17088c0a791aSdanielk1977 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 17098c0a791aSdanielk1977 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 17108c0a791aSdanielk1977 FUNCTION(trim, 1, 3, 0, trimFunc ), 17118c0a791aSdanielk1977 FUNCTION(trim, 2, 3, 0, trimFunc ), 17128c0a791aSdanielk1977 FUNCTION(min, -1, 0, 1, minmaxFunc ), 17138c0a791aSdanielk1977 FUNCTION(min, 0, 0, 1, 0 ), 17149588ad95Sdrh AGGREGATE2(min, 1, 0, 1, minmaxStep, minMaxFinalize, 17159588ad95Sdrh SQLITE_FUNC_MINMAX ), 17168c0a791aSdanielk1977 FUNCTION(max, -1, 1, 1, minmaxFunc ), 17178c0a791aSdanielk1977 FUNCTION(max, 0, 1, 1, 0 ), 17189588ad95Sdrh AGGREGATE2(max, 1, 1, 1, minmaxStep, minMaxFinalize, 17199588ad95Sdrh SQLITE_FUNC_MINMAX ), 1720a748fdccSdrh FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 1721a748fdccSdrh FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 1722d55e0729Sdrh FUNCTION(instr, 2, 0, 0, instrFunc ), 17238c0a791aSdanielk1977 FUNCTION(substr, 2, 0, 0, substrFunc ), 17248c0a791aSdanielk1977 FUNCTION(substr, 3, 0, 0, substrFunc ), 1725a5c1416dSdrh FUNCTION(printf, -1, 0, 0, printfFunc ), 1726d495d8c9Sdrh FUNCTION(unicode, 1, 0, 0, unicodeFunc ), 1727d495d8c9Sdrh FUNCTION(char, -1, 0, 0, charFunc ), 17288c0a791aSdanielk1977 FUNCTION(abs, 1, 0, 0, absFunc ), 1729fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 17308c0a791aSdanielk1977 FUNCTION(round, 1, 0, 0, roundFunc ), 17318c0a791aSdanielk1977 FUNCTION(round, 2, 0, 0, roundFunc ), 1732fbd60f82Sshane #endif 17338c0a791aSdanielk1977 FUNCTION(upper, 1, 0, 0, upperFunc ), 17348c0a791aSdanielk1977 FUNCTION(lower, 1, 0, 0, lowerFunc ), 17358c0a791aSdanielk1977 FUNCTION(coalesce, 1, 0, 0, 0 ), 17368c0a791aSdanielk1977 FUNCTION(coalesce, 0, 0, 0, 0 ), 1737cca9f3d2Sdrh FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 17388c0a791aSdanielk1977 FUNCTION(hex, 1, 0, 0, hexFunc ), 1739cca9f3d2Sdrh FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 1740cca9f3d2Sdrh FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 1741aae0f9e4Sdrh FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 174203202a97Sdrh FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 1743b1fba286Sdrh VFUNCTION(random, 0, 0, 0, randomFunc ), 1744b1fba286Sdrh VFUNCTION(randomblob, 1, 0, 0, randomBlob ), 17458c0a791aSdanielk1977 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 17468c0a791aSdanielk1977 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 174747baebc2Sdrh FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), 1748840561f2Sdrh FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), 1749f442e33eSdrh #if SQLITE_USER_AUTHENTICATION 1750f442e33eSdrh FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), 1751f442e33eSdrh #endif 1752dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 17538bb76d39Sdrh FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), 17548bb76d39Sdrh FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), 1755dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 17568c0a791aSdanielk1977 FUNCTION(quote, 1, 0, 0, quoteFunc ), 1757b1fba286Sdrh VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 1758b1fba286Sdrh VFUNCTION(changes, 0, 0, 0, changes ), 1759b1fba286Sdrh VFUNCTION(total_changes, 0, 0, 0, total_changes ), 17608c0a791aSdanielk1977 FUNCTION(replace, 3, 0, 0, replaceFunc ), 17618c0a791aSdanielk1977 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 17628c0a791aSdanielk1977 #ifdef SQLITE_SOUNDEX 17638c0a791aSdanielk1977 FUNCTION(soundex, 1, 0, 0, soundexFunc ), 17648c0a791aSdanielk1977 #endif 17658c0a791aSdanielk1977 #ifndef SQLITE_OMIT_LOAD_EXTENSION 17668c0a791aSdanielk1977 FUNCTION(load_extension, 1, 0, 0, loadExt ), 17678c0a791aSdanielk1977 FUNCTION(load_extension, 2, 0, 0, loadExt ), 17688c0a791aSdanielk1977 #endif 17698c0a791aSdanielk1977 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), 17708c0a791aSdanielk1977 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), 17718c0a791aSdanielk1977 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), 17729588ad95Sdrh AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, 17739588ad95Sdrh SQLITE_FUNC_COUNT ), 17748c0a791aSdanielk1977 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), 177507d3117aSdrh AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), 177607d3117aSdrh AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), 17778c0a791aSdanielk1977 17788c0a791aSdanielk1977 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 17798c0a791aSdanielk1977 #ifdef SQLITE_CASE_SENSITIVE_LIKE 17808c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 17818c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 17828c0a791aSdanielk1977 #else 17838c0a791aSdanielk1977 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 17848c0a791aSdanielk1977 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 17858c0a791aSdanielk1977 #endif 17868c0a791aSdanielk1977 }; 17878c0a791aSdanielk1977 178870a8ca3cSdrh int i; 1789075c23afSdanielk1977 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); 1790106cee54Sdrh FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc); 179193ce741bSdanielk1977 179293ce741bSdanielk1977 for(i=0; i<ArraySize(aBuiltinFunc); i++){ 179393ce741bSdanielk1977 sqlite3FuncDefInsert(pHash, &aFunc[i]); 179470a8ca3cSdrh } 1795777c5386Sdrh sqlite3RegisterDateTimeFunctions(); 1796545f587fSdrh #ifndef SQLITE_OMIT_ALTERTABLE 1797545f587fSdrh sqlite3AlterFunctions(); 1798545f587fSdrh #endif 17990106e378Sdan #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) 18000106e378Sdan sqlite3AnalyzeFunctions(); 18010106e378Sdan #endif 180270a8ca3cSdrh } 1803