000001 /* 000002 ** 2002 February 23 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** This file contains the C-language implementations for many of the SQL 000013 ** functions of SQLite. (Some function, and in particular the date and 000014 ** time functions, are implemented separately.) 000015 */ 000016 #include "sqliteInt.h" 000017 #include <stdlib.h> 000018 #include <assert.h> 000019 #include "vdbeInt.h" 000020 000021 /* 000022 ** Return the collating function associated with a function. 000023 */ 000024 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 000025 VdbeOp *pOp; 000026 assert( context->pVdbe!=0 ); 000027 pOp = &context->pVdbe->aOp[context->iOp-1]; 000028 assert( pOp->opcode==OP_CollSeq ); 000029 assert( pOp->p4type==P4_COLLSEQ ); 000030 return pOp->p4.pColl; 000031 } 000032 000033 /* 000034 ** Indicate that the accumulator load should be skipped on this 000035 ** iteration of the aggregate loop. 000036 */ 000037 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ 000038 context->skipFlag = 1; 000039 } 000040 000041 /* 000042 ** Implementation of the non-aggregate min() and max() functions 000043 */ 000044 static void minmaxFunc( 000045 sqlite3_context *context, 000046 int argc, 000047 sqlite3_value **argv 000048 ){ 000049 int i; 000050 int mask; /* 0 for min() or 0xffffffff for max() */ 000051 int iBest; 000052 CollSeq *pColl; 000053 000054 assert( argc>1 ); 000055 mask = sqlite3_user_data(context)==0 ? 0 : -1; 000056 pColl = sqlite3GetFuncCollSeq(context); 000057 assert( pColl ); 000058 assert( mask==-1 || mask==0 ); 000059 iBest = 0; 000060 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 000061 for(i=1; i<argc; i++){ 000062 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 000063 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 000064 testcase( mask==0 ); 000065 iBest = i; 000066 } 000067 } 000068 sqlite3_result_value(context, argv[iBest]); 000069 } 000070 000071 /* 000072 ** Return the type of the argument. 000073 */ 000074 static void typeofFunc( 000075 sqlite3_context *context, 000076 int NotUsed, 000077 sqlite3_value **argv 000078 ){ 000079 const char *z = 0; 000080 UNUSED_PARAMETER(NotUsed); 000081 switch( sqlite3_value_type(argv[0]) ){ 000082 case SQLITE_INTEGER: z = "integer"; break; 000083 case SQLITE_TEXT: z = "text"; break; 000084 case SQLITE_FLOAT: z = "real"; break; 000085 case SQLITE_BLOB: z = "blob"; break; 000086 default: z = "null"; break; 000087 } 000088 sqlite3_result_text(context, z, -1, SQLITE_STATIC); 000089 } 000090 000091 000092 /* 000093 ** Implementation of the length() function 000094 */ 000095 static void lengthFunc( 000096 sqlite3_context *context, 000097 int argc, 000098 sqlite3_value **argv 000099 ){ 000100 int len; 000101 000102 assert( argc==1 ); 000103 UNUSED_PARAMETER(argc); 000104 switch( sqlite3_value_type(argv[0]) ){ 000105 case SQLITE_BLOB: 000106 case SQLITE_INTEGER: 000107 case SQLITE_FLOAT: { 000108 sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 000109 break; 000110 } 000111 case SQLITE_TEXT: { 000112 const unsigned char *z = sqlite3_value_text(argv[0]); 000113 if( z==0 ) return; 000114 len = 0; 000115 while( *z ){ 000116 len++; 000117 SQLITE_SKIP_UTF8(z); 000118 } 000119 sqlite3_result_int(context, len); 000120 break; 000121 } 000122 default: { 000123 sqlite3_result_null(context); 000124 break; 000125 } 000126 } 000127 } 000128 000129 /* 000130 ** Implementation of the abs() function. 000131 ** 000132 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of 000133 ** the numeric argument X. 000134 */ 000135 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 000136 assert( argc==1 ); 000137 UNUSED_PARAMETER(argc); 000138 switch( sqlite3_value_type(argv[0]) ){ 000139 case SQLITE_INTEGER: { 000140 i64 iVal = sqlite3_value_int64(argv[0]); 000141 if( iVal<0 ){ 000142 if( iVal==SMALLEST_INT64 ){ 000143 /* IMP: R-31676-45509 If X is the integer -9223372036854775808 000144 ** then abs(X) throws an integer overflow error since there is no 000145 ** equivalent positive 64-bit two complement value. */ 000146 sqlite3_result_error(context, "integer overflow", -1); 000147 return; 000148 } 000149 iVal = -iVal; 000150 } 000151 sqlite3_result_int64(context, iVal); 000152 break; 000153 } 000154 case SQLITE_NULL: { 000155 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ 000156 sqlite3_result_null(context); 000157 break; 000158 } 000159 default: { 000160 /* Because sqlite3_value_double() returns 0.0 if the argument is not 000161 ** something that can be converted into a number, we have: 000162 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob 000163 ** that cannot be converted to a numeric value. 000164 */ 000165 double rVal = sqlite3_value_double(argv[0]); 000166 if( rVal<0 ) rVal = -rVal; 000167 sqlite3_result_double(context, rVal); 000168 break; 000169 } 000170 } 000171 } 000172 000173 /* 000174 ** Implementation of the instr() function. 000175 ** 000176 ** instr(haystack,needle) finds the first occurrence of needle 000177 ** in haystack and returns the number of previous characters plus 1, 000178 ** or 0 if needle does not occur within haystack. 000179 ** 000180 ** If both haystack and needle are BLOBs, then the result is one more than 000181 ** the number of bytes in haystack prior to the first occurrence of needle, 000182 ** or 0 if needle never occurs in haystack. 000183 */ 000184 static void instrFunc( 000185 sqlite3_context *context, 000186 int argc, 000187 sqlite3_value **argv 000188 ){ 000189 const unsigned char *zHaystack; 000190 const unsigned char *zNeedle; 000191 int nHaystack; 000192 int nNeedle; 000193 int typeHaystack, typeNeedle; 000194 int N = 1; 000195 int isText; 000196 000197 UNUSED_PARAMETER(argc); 000198 typeHaystack = sqlite3_value_type(argv[0]); 000199 typeNeedle = sqlite3_value_type(argv[1]); 000200 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; 000201 nHaystack = sqlite3_value_bytes(argv[0]); 000202 nNeedle = sqlite3_value_bytes(argv[1]); 000203 if( nNeedle>0 ){ 000204 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ 000205 zHaystack = sqlite3_value_blob(argv[0]); 000206 zNeedle = sqlite3_value_blob(argv[1]); 000207 assert( zNeedle!=0 ); 000208 assert( zHaystack!=0 || nHaystack==0 ); 000209 isText = 0; 000210 }else{ 000211 zHaystack = sqlite3_value_text(argv[0]); 000212 zNeedle = sqlite3_value_text(argv[1]); 000213 isText = 1; 000214 if( zHaystack==0 || zNeedle==0 ) return; 000215 } 000216 while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){ 000217 N++; 000218 do{ 000219 nHaystack--; 000220 zHaystack++; 000221 }while( isText && (zHaystack[0]&0xc0)==0x80 ); 000222 } 000223 if( nNeedle>nHaystack ) N = 0; 000224 } 000225 sqlite3_result_int(context, N); 000226 } 000227 000228 /* 000229 ** Implementation of the printf() function. 000230 */ 000231 static void printfFunc( 000232 sqlite3_context *context, 000233 int argc, 000234 sqlite3_value **argv 000235 ){ 000236 PrintfArguments x; 000237 StrAccum str; 000238 const char *zFormat; 000239 int n; 000240 sqlite3 *db = sqlite3_context_db_handle(context); 000241 000242 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 000243 x.nArg = argc-1; 000244 x.nUsed = 0; 000245 x.apArg = argv+1; 000246 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); 000247 str.printfFlags = SQLITE_PRINTF_SQLFUNC; 000248 sqlite3XPrintf(&str, zFormat, &x); 000249 n = str.nChar; 000250 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, 000251 SQLITE_DYNAMIC); 000252 } 000253 } 000254 000255 /* 000256 ** Implementation of the substr() function. 000257 ** 000258 ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. 000259 ** p1 is 1-indexed. So substr(x,1,1) returns the first character 000260 ** of x. If x is text, then we actually count UTF-8 characters. 000261 ** If x is a blob, then we count bytes. 000262 ** 000263 ** If p1 is negative, then we begin abs(p1) from the end of x[]. 000264 ** 000265 ** If p2 is negative, return the p2 characters preceding p1. 000266 */ 000267 static void substrFunc( 000268 sqlite3_context *context, 000269 int argc, 000270 sqlite3_value **argv 000271 ){ 000272 const unsigned char *z; 000273 const unsigned char *z2; 000274 int len; 000275 int p0type; 000276 i64 p1, p2; 000277 int negP2 = 0; 000278 000279 assert( argc==3 || argc==2 ); 000280 if( sqlite3_value_type(argv[1])==SQLITE_NULL 000281 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) 000282 ){ 000283 return; 000284 } 000285 p0type = sqlite3_value_type(argv[0]); 000286 p1 = sqlite3_value_int(argv[1]); 000287 if( p0type==SQLITE_BLOB ){ 000288 len = sqlite3_value_bytes(argv[0]); 000289 z = sqlite3_value_blob(argv[0]); 000290 if( z==0 ) return; 000291 assert( len==sqlite3_value_bytes(argv[0]) ); 000292 }else{ 000293 z = sqlite3_value_text(argv[0]); 000294 if( z==0 ) return; 000295 len = 0; 000296 if( p1<0 ){ 000297 for(z2=z; *z2; len++){ 000298 SQLITE_SKIP_UTF8(z2); 000299 } 000300 } 000301 } 000302 #ifdef SQLITE_SUBSTR_COMPATIBILITY 000303 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as 000304 ** as substr(X,1,N) - it returns the first N characters of X. This 000305 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] 000306 ** from 2009-02-02 for compatibility of applications that exploited the 000307 ** old buggy behavior. */ 000308 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ 000309 #endif 000310 if( argc==3 ){ 000311 p2 = sqlite3_value_int(argv[2]); 000312 if( p2<0 ){ 000313 p2 = -p2; 000314 negP2 = 1; 000315 } 000316 }else{ 000317 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; 000318 } 000319 if( p1<0 ){ 000320 p1 += len; 000321 if( p1<0 ){ 000322 p2 += p1; 000323 if( p2<0 ) p2 = 0; 000324 p1 = 0; 000325 } 000326 }else if( p1>0 ){ 000327 p1--; 000328 }else if( p2>0 ){ 000329 p2--; 000330 } 000331 if( negP2 ){ 000332 p1 -= p2; 000333 if( p1<0 ){ 000334 p2 += p1; 000335 p1 = 0; 000336 } 000337 } 000338 assert( p1>=0 && p2>=0 ); 000339 if( p0type!=SQLITE_BLOB ){ 000340 while( *z && p1 ){ 000341 SQLITE_SKIP_UTF8(z); 000342 p1--; 000343 } 000344 for(z2=z; *z2 && p2; p2--){ 000345 SQLITE_SKIP_UTF8(z2); 000346 } 000347 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, 000348 SQLITE_UTF8); 000349 }else{ 000350 if( p1+p2>len ){ 000351 p2 = len-p1; 000352 if( p2<0 ) p2 = 0; 000353 } 000354 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); 000355 } 000356 } 000357 000358 /* 000359 ** Implementation of the round() function 000360 */ 000361 #ifndef SQLITE_OMIT_FLOATING_POINT 000362 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 000363 int n = 0; 000364 double r; 000365 char *zBuf; 000366 assert( argc==1 || argc==2 ); 000367 if( argc==2 ){ 000368 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 000369 n = sqlite3_value_int(argv[1]); 000370 if( n>30 ) n = 30; 000371 if( n<0 ) n = 0; 000372 } 000373 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 000374 r = sqlite3_value_double(argv[0]); 000375 /* If Y==0 and X will fit in a 64-bit int, 000376 ** handle the rounding directly, 000377 ** otherwise use printf. 000378 */ 000379 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){ 000380 r = (double)((sqlite_int64)(r+0.5)); 000381 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){ 000382 r = -(double)((sqlite_int64)((-r)+0.5)); 000383 }else{ 000384 zBuf = sqlite3_mprintf("%.*f",n,r); 000385 if( zBuf==0 ){ 000386 sqlite3_result_error_nomem(context); 000387 return; 000388 } 000389 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); 000390 sqlite3_free(zBuf); 000391 } 000392 sqlite3_result_double(context, r); 000393 } 000394 #endif 000395 000396 /* 000397 ** Allocate nByte bytes of space using sqlite3Malloc(). If the 000398 ** allocation fails, call sqlite3_result_error_nomem() to notify 000399 ** the database handle that malloc() has failed and return NULL. 000400 ** If nByte is larger than the maximum string or blob length, then 000401 ** raise an SQLITE_TOOBIG exception and return NULL. 000402 */ 000403 static void *contextMalloc(sqlite3_context *context, i64 nByte){ 000404 char *z; 000405 sqlite3 *db = sqlite3_context_db_handle(context); 000406 assert( nByte>0 ); 000407 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); 000408 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); 000409 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 000410 sqlite3_result_error_toobig(context); 000411 z = 0; 000412 }else{ 000413 z = sqlite3Malloc(nByte); 000414 if( !z ){ 000415 sqlite3_result_error_nomem(context); 000416 } 000417 } 000418 return z; 000419 } 000420 000421 /* 000422 ** Implementation of the upper() and lower() SQL functions. 000423 */ 000424 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 000425 char *z1; 000426 const char *z2; 000427 int i, n; 000428 UNUSED_PARAMETER(argc); 000429 z2 = (char*)sqlite3_value_text(argv[0]); 000430 n = sqlite3_value_bytes(argv[0]); 000431 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 000432 assert( z2==(char*)sqlite3_value_text(argv[0]) ); 000433 if( z2 ){ 000434 z1 = contextMalloc(context, ((i64)n)+1); 000435 if( z1 ){ 000436 for(i=0; i<n; i++){ 000437 z1[i] = (char)sqlite3Toupper(z2[i]); 000438 } 000439 sqlite3_result_text(context, z1, n, sqlite3_free); 000440 } 000441 } 000442 } 000443 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 000444 char *z1; 000445 const char *z2; 000446 int i, n; 000447 UNUSED_PARAMETER(argc); 000448 z2 = (char*)sqlite3_value_text(argv[0]); 000449 n = sqlite3_value_bytes(argv[0]); 000450 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 000451 assert( z2==(char*)sqlite3_value_text(argv[0]) ); 000452 if( z2 ){ 000453 z1 = contextMalloc(context, ((i64)n)+1); 000454 if( z1 ){ 000455 for(i=0; i<n; i++){ 000456 z1[i] = sqlite3Tolower(z2[i]); 000457 } 000458 sqlite3_result_text(context, z1, n, sqlite3_free); 000459 } 000460 } 000461 } 000462 000463 /* 000464 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented 000465 ** as VDBE code so that unused argument values do not have to be computed. 000466 ** However, we still need some kind of function implementation for this 000467 ** routines in the function table. The noopFunc macro provides this. 000468 ** noopFunc will never be called so it doesn't matter what the implementation 000469 ** is. We might as well use the "version()" function as a substitute. 000470 */ 000471 #define noopFunc versionFunc /* Substitute function - never called */ 000472 000473 /* 000474 ** Implementation of random(). Return a random integer. 000475 */ 000476 static void randomFunc( 000477 sqlite3_context *context, 000478 int NotUsed, 000479 sqlite3_value **NotUsed2 000480 ){ 000481 sqlite_int64 r; 000482 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000483 sqlite3_randomness(sizeof(r), &r); 000484 if( r<0 ){ 000485 /* We need to prevent a random number of 0x8000000000000000 000486 ** (or -9223372036854775808) since when you do abs() of that 000487 ** number of you get the same value back again. To do this 000488 ** in a way that is testable, mask the sign bit off of negative 000489 ** values, resulting in a positive value. Then take the 000490 ** 2s complement of that positive value. The end result can 000491 ** therefore be no less than -9223372036854775807. 000492 */ 000493 r = -(r & LARGEST_INT64); 000494 } 000495 sqlite3_result_int64(context, r); 000496 } 000497 000498 /* 000499 ** Implementation of randomblob(N). Return a random blob 000500 ** that is N bytes long. 000501 */ 000502 static void randomBlob( 000503 sqlite3_context *context, 000504 int argc, 000505 sqlite3_value **argv 000506 ){ 000507 int n; 000508 unsigned char *p; 000509 assert( argc==1 ); 000510 UNUSED_PARAMETER(argc); 000511 n = sqlite3_value_int(argv[0]); 000512 if( n<1 ){ 000513 n = 1; 000514 } 000515 p = contextMalloc(context, n); 000516 if( p ){ 000517 sqlite3_randomness(n, p); 000518 sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 000519 } 000520 } 000521 000522 /* 000523 ** Implementation of the last_insert_rowid() SQL function. The return 000524 ** value is the same as the sqlite3_last_insert_rowid() API function. 000525 */ 000526 static void last_insert_rowid( 000527 sqlite3_context *context, 000528 int NotUsed, 000529 sqlite3_value **NotUsed2 000530 ){ 000531 sqlite3 *db = sqlite3_context_db_handle(context); 000532 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000533 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a 000534 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface 000535 ** function. */ 000536 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 000537 } 000538 000539 /* 000540 ** Implementation of the changes() SQL function. 000541 ** 000542 ** IMP: R-62073-11209 The changes() SQL function is a wrapper 000543 ** around the sqlite3_changes() C/C++ function and hence follows the same 000544 ** rules for counting changes. 000545 */ 000546 static void changes( 000547 sqlite3_context *context, 000548 int NotUsed, 000549 sqlite3_value **NotUsed2 000550 ){ 000551 sqlite3 *db = sqlite3_context_db_handle(context); 000552 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000553 sqlite3_result_int(context, sqlite3_changes(db)); 000554 } 000555 000556 /* 000557 ** Implementation of the total_changes() SQL function. The return value is 000558 ** the same as the sqlite3_total_changes() API function. 000559 */ 000560 static void total_changes( 000561 sqlite3_context *context, 000562 int NotUsed, 000563 sqlite3_value **NotUsed2 000564 ){ 000565 sqlite3 *db = sqlite3_context_db_handle(context); 000566 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000567 /* IMP: R-52756-41993 This function is a wrapper around the 000568 ** sqlite3_total_changes() C/C++ interface. */ 000569 sqlite3_result_int(context, sqlite3_total_changes(db)); 000570 } 000571 000572 /* 000573 ** A structure defining how to do GLOB-style comparisons. 000574 */ 000575 struct compareInfo { 000576 u8 matchAll; /* "*" or "%" */ 000577 u8 matchOne; /* "?" or "_" */ 000578 u8 matchSet; /* "[" or 0 */ 000579 u8 noCase; /* true to ignore case differences */ 000580 }; 000581 000582 /* 000583 ** For LIKE and GLOB matching on EBCDIC machines, assume that every 000584 ** character is exactly one byte in size. Also, provde the Utf8Read() 000585 ** macro for fast reading of the next character in the common case where 000586 ** the next character is ASCII. 000587 */ 000588 #if defined(SQLITE_EBCDIC) 000589 # define sqlite3Utf8Read(A) (*((*A)++)) 000590 # define Utf8Read(A) (*(A++)) 000591 #else 000592 # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) 000593 #endif 000594 000595 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 000596 /* The correct SQL-92 behavior is for the LIKE operator to ignore 000597 ** case. Thus 'a' LIKE 'A' would be true. */ 000598 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 000599 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 000600 ** is case sensitive causing 'a' LIKE 'A' to be false */ 000601 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 000602 000603 /* 000604 ** Possible error returns from patternMatch() 000605 */ 000606 #define SQLITE_MATCH 0 000607 #define SQLITE_NOMATCH 1 000608 #define SQLITE_NOWILDCARDMATCH 2 000609 000610 /* 000611 ** Compare two UTF-8 strings for equality where the first string is 000612 ** a GLOB or LIKE expression. Return values: 000613 ** 000614 ** SQLITE_MATCH: Match 000615 ** SQLITE_NOMATCH: No match 000616 ** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards. 000617 ** 000618 ** Globbing rules: 000619 ** 000620 ** '*' Matches any sequence of zero or more characters. 000621 ** 000622 ** '?' Matches exactly one character. 000623 ** 000624 ** [...] Matches one character from the enclosed list of 000625 ** characters. 000626 ** 000627 ** [^...] Matches one character not in the enclosed list. 000628 ** 000629 ** With the [...] and [^...] matching, a ']' character can be included 000630 ** in the list by making it the first character after '[' or '^'. A 000631 ** range of characters can be specified using '-'. Example: 000632 ** "[a-z]" matches any single lower-case letter. To match a '-', make 000633 ** it the last character in the list. 000634 ** 000635 ** Like matching rules: 000636 ** 000637 ** '%' Matches any sequence of zero or more characters 000638 ** 000639 *** '_' Matches any one character 000640 ** 000641 ** Ec Where E is the "esc" character and c is any other 000642 ** character, including '%', '_', and esc, match exactly c. 000643 ** 000644 ** The comments within this routine usually assume glob matching. 000645 ** 000646 ** This routine is usually quick, but can be N**2 in the worst case. 000647 */ 000648 static int patternCompare( 000649 const u8 *zPattern, /* The glob pattern */ 000650 const u8 *zString, /* The string to compare against the glob */ 000651 const struct compareInfo *pInfo, /* Information about how to do the compare */ 000652 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */ 000653 ){ 000654 u32 c, c2; /* Next pattern and input string chars */ 000655 u32 matchOne = pInfo->matchOne; /* "?" or "_" */ 000656 u32 matchAll = pInfo->matchAll; /* "*" or "%" */ 000657 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ 000658 const u8 *zEscaped = 0; /* One past the last escaped input char */ 000659 000660 while( (c = Utf8Read(zPattern))!=0 ){ 000661 if( c==matchAll ){ /* Match "*" */ 000662 /* Skip over multiple "*" characters in the pattern. If there 000663 ** are also "?" characters, skip those as well, but consume a 000664 ** single character of the input string for each "?" skipped */ 000665 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ 000666 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ 000667 return SQLITE_NOWILDCARDMATCH; 000668 } 000669 } 000670 if( c==0 ){ 000671 return SQLITE_MATCH; /* "*" at the end of the pattern matches */ 000672 }else if( c==matchOther ){ 000673 if( pInfo->matchSet==0 ){ 000674 c = sqlite3Utf8Read(&zPattern); 000675 if( c==0 ) return SQLITE_NOWILDCARDMATCH; 000676 }else{ 000677 /* "[...]" immediately follows the "*". We have to do a slow 000678 ** recursive search in this case, but it is an unusual case. */ 000679 assert( matchOther<0x80 ); /* '[' is a single-byte character */ 000680 while( *zString ){ 000681 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther); 000682 if( bMatch!=SQLITE_NOMATCH ) return bMatch; 000683 SQLITE_SKIP_UTF8(zString); 000684 } 000685 return SQLITE_NOWILDCARDMATCH; 000686 } 000687 } 000688 000689 /* At this point variable c contains the first character of the 000690 ** pattern string past the "*". Search in the input string for the 000691 ** first matching character and recursively continue the match from 000692 ** that point. 000693 ** 000694 ** For a case-insensitive search, set variable cx to be the same as 000695 ** c but in the other case and search the input string for either 000696 ** c or cx. 000697 */ 000698 if( c<=0x80 ){ 000699 u32 cx; 000700 int bMatch; 000701 if( noCase ){ 000702 cx = sqlite3Toupper(c); 000703 c = sqlite3Tolower(c); 000704 }else{ 000705 cx = c; 000706 } 000707 while( (c2 = *(zString++))!=0 ){ 000708 if( c2!=c && c2!=cx ) continue; 000709 bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 000710 if( bMatch!=SQLITE_NOMATCH ) return bMatch; 000711 } 000712 }else{ 000713 int bMatch; 000714 while( (c2 = Utf8Read(zString))!=0 ){ 000715 if( c2!=c ) continue; 000716 bMatch = patternCompare(zPattern,zString,pInfo,matchOther); 000717 if( bMatch!=SQLITE_NOMATCH ) return bMatch; 000718 } 000719 } 000720 return SQLITE_NOWILDCARDMATCH; 000721 } 000722 if( c==matchOther ){ 000723 if( pInfo->matchSet==0 ){ 000724 c = sqlite3Utf8Read(&zPattern); 000725 if( c==0 ) return SQLITE_NOMATCH; 000726 zEscaped = zPattern; 000727 }else{ 000728 u32 prior_c = 0; 000729 int seen = 0; 000730 int invert = 0; 000731 c = sqlite3Utf8Read(&zString); 000732 if( c==0 ) return SQLITE_NOMATCH; 000733 c2 = sqlite3Utf8Read(&zPattern); 000734 if( c2=='^' ){ 000735 invert = 1; 000736 c2 = sqlite3Utf8Read(&zPattern); 000737 } 000738 if( c2==']' ){ 000739 if( c==']' ) seen = 1; 000740 c2 = sqlite3Utf8Read(&zPattern); 000741 } 000742 while( c2 && c2!=']' ){ 000743 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ 000744 c2 = sqlite3Utf8Read(&zPattern); 000745 if( c>=prior_c && c<=c2 ) seen = 1; 000746 prior_c = 0; 000747 }else{ 000748 if( c==c2 ){ 000749 seen = 1; 000750 } 000751 prior_c = c2; 000752 } 000753 c2 = sqlite3Utf8Read(&zPattern); 000754 } 000755 if( c2==0 || (seen ^ invert)==0 ){ 000756 return SQLITE_NOMATCH; 000757 } 000758 continue; 000759 } 000760 } 000761 c2 = Utf8Read(zString); 000762 if( c==c2 ) continue; 000763 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){ 000764 continue; 000765 } 000766 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; 000767 return SQLITE_NOMATCH; 000768 } 000769 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH; 000770 } 000771 000772 /* 000773 ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and 000774 ** non-zero if there is no match. 000775 */ 000776 int sqlite3_strglob(const char *zGlobPattern, const char *zString){ 000777 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); 000778 } 000779 000780 /* 000781 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for 000782 ** a miss - like strcmp(). 000783 */ 000784 int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ 000785 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); 000786 } 000787 000788 /* 000789 ** Count the number of times that the LIKE operator (or GLOB which is 000790 ** just a variation of LIKE) gets called. This is used for testing 000791 ** only. 000792 */ 000793 #ifdef SQLITE_TEST 000794 int sqlite3_like_count = 0; 000795 #endif 000796 000797 000798 /* 000799 ** Implementation of the like() SQL function. This function implements 000800 ** the build-in LIKE operator. The first argument to the function is the 000801 ** pattern and the second argument is the string. So, the SQL statements: 000802 ** 000803 ** A LIKE B 000804 ** 000805 ** is implemented as like(B,A). 000806 ** 000807 ** This same function (with a different compareInfo structure) computes 000808 ** the GLOB operator. 000809 */ 000810 static void likeFunc( 000811 sqlite3_context *context, 000812 int argc, 000813 sqlite3_value **argv 000814 ){ 000815 const unsigned char *zA, *zB; 000816 u32 escape; 000817 int nPat; 000818 sqlite3 *db = sqlite3_context_db_handle(context); 000819 struct compareInfo *pInfo = sqlite3_user_data(context); 000820 000821 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS 000822 if( sqlite3_value_type(argv[0])==SQLITE_BLOB 000823 || sqlite3_value_type(argv[1])==SQLITE_BLOB 000824 ){ 000825 #ifdef SQLITE_TEST 000826 sqlite3_like_count++; 000827 #endif 000828 sqlite3_result_int(context, 0); 000829 return; 000830 } 000831 #endif 000832 zB = sqlite3_value_text(argv[0]); 000833 zA = sqlite3_value_text(argv[1]); 000834 000835 /* Limit the length of the LIKE or GLOB pattern to avoid problems 000836 ** of deep recursion and N*N behavior in patternCompare(). 000837 */ 000838 nPat = sqlite3_value_bytes(argv[0]); 000839 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); 000840 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); 000841 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ 000842 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); 000843 return; 000844 } 000845 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ 000846 000847 if( argc==3 ){ 000848 /* The escape character string must consist of a single UTF-8 character. 000849 ** Otherwise, return an error. 000850 */ 000851 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 000852 if( zEsc==0 ) return; 000853 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ 000854 sqlite3_result_error(context, 000855 "ESCAPE expression must be a single character", -1); 000856 return; 000857 } 000858 escape = sqlite3Utf8Read(&zEsc); 000859 }else{ 000860 escape = pInfo->matchSet; 000861 } 000862 if( zA && zB ){ 000863 #ifdef SQLITE_TEST 000864 sqlite3_like_count++; 000865 #endif 000866 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH); 000867 } 000868 } 000869 000870 /* 000871 ** Implementation of the NULLIF(x,y) function. The result is the first 000872 ** argument if the arguments are different. The result is NULL if the 000873 ** arguments are equal to each other. 000874 */ 000875 static void nullifFunc( 000876 sqlite3_context *context, 000877 int NotUsed, 000878 sqlite3_value **argv 000879 ){ 000880 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 000881 UNUSED_PARAMETER(NotUsed); 000882 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 000883 sqlite3_result_value(context, argv[0]); 000884 } 000885 } 000886 000887 /* 000888 ** Implementation of the sqlite_version() function. The result is the version 000889 ** of the SQLite library that is running. 000890 */ 000891 static void versionFunc( 000892 sqlite3_context *context, 000893 int NotUsed, 000894 sqlite3_value **NotUsed2 000895 ){ 000896 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000897 /* IMP: R-48699-48617 This function is an SQL wrapper around the 000898 ** sqlite3_libversion() C-interface. */ 000899 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); 000900 } 000901 000902 /* 000903 ** Implementation of the sqlite_source_id() function. The result is a string 000904 ** that identifies the particular version of the source code used to build 000905 ** SQLite. 000906 */ 000907 static void sourceidFunc( 000908 sqlite3_context *context, 000909 int NotUsed, 000910 sqlite3_value **NotUsed2 000911 ){ 000912 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000913 /* IMP: R-24470-31136 This function is an SQL wrapper around the 000914 ** sqlite3_sourceid() C interface. */ 000915 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); 000916 } 000917 000918 /* 000919 ** Implementation of the sqlite_log() function. This is a wrapper around 000920 ** sqlite3_log(). The return value is NULL. The function exists purely for 000921 ** its side-effects. 000922 */ 000923 static void errlogFunc( 000924 sqlite3_context *context, 000925 int argc, 000926 sqlite3_value **argv 000927 ){ 000928 UNUSED_PARAMETER(argc); 000929 UNUSED_PARAMETER(context); 000930 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); 000931 } 000932 000933 /* 000934 ** Implementation of the sqlite_compileoption_used() function. 000935 ** The result is an integer that identifies if the compiler option 000936 ** was used to build SQLite. 000937 */ 000938 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 000939 static void compileoptionusedFunc( 000940 sqlite3_context *context, 000941 int argc, 000942 sqlite3_value **argv 000943 ){ 000944 const char *zOptName; 000945 assert( argc==1 ); 000946 UNUSED_PARAMETER(argc); 000947 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL 000948 ** function is a wrapper around the sqlite3_compileoption_used() C/C++ 000949 ** function. 000950 */ 000951 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ 000952 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); 000953 } 000954 } 000955 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 000956 000957 /* 000958 ** Implementation of the sqlite_compileoption_get() function. 000959 ** The result is a string that identifies the compiler options 000960 ** used to build SQLite. 000961 */ 000962 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 000963 static void compileoptiongetFunc( 000964 sqlite3_context *context, 000965 int argc, 000966 sqlite3_value **argv 000967 ){ 000968 int n; 000969 assert( argc==1 ); 000970 UNUSED_PARAMETER(argc); 000971 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function 000972 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. 000973 */ 000974 n = sqlite3_value_int(argv[0]); 000975 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); 000976 } 000977 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 000978 000979 /* Array for converting from half-bytes (nybbles) into ASCII hex 000980 ** digits. */ 000981 static const char hexdigits[] = { 000982 '0', '1', '2', '3', '4', '5', '6', '7', 000983 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 000984 }; 000985 000986 /* 000987 ** Implementation of the QUOTE() function. This function takes a single 000988 ** argument. If the argument is numeric, the return value is the same as 000989 ** the argument. If the argument is NULL, the return value is the string 000990 ** "NULL". Otherwise, the argument is enclosed in single quotes with 000991 ** single-quote escapes. 000992 */ 000993 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 000994 assert( argc==1 ); 000995 UNUSED_PARAMETER(argc); 000996 switch( sqlite3_value_type(argv[0]) ){ 000997 case SQLITE_FLOAT: { 000998 double r1, r2; 000999 char zBuf[50]; 001000 r1 = sqlite3_value_double(argv[0]); 001001 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); 001002 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); 001003 if( r1!=r2 ){ 001004 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); 001005 } 001006 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 001007 break; 001008 } 001009 case SQLITE_INTEGER: { 001010 sqlite3_result_value(context, argv[0]); 001011 break; 001012 } 001013 case SQLITE_BLOB: { 001014 char *zText = 0; 001015 char const *zBlob = sqlite3_value_blob(argv[0]); 001016 int nBlob = sqlite3_value_bytes(argv[0]); 001017 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 001018 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 001019 if( zText ){ 001020 int i; 001021 for(i=0; i<nBlob; i++){ 001022 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 001023 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 001024 } 001025 zText[(nBlob*2)+2] = '\''; 001026 zText[(nBlob*2)+3] = '\0'; 001027 zText[0] = 'X'; 001028 zText[1] = '\''; 001029 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 001030 sqlite3_free(zText); 001031 } 001032 break; 001033 } 001034 case SQLITE_TEXT: { 001035 int i,j; 001036 u64 n; 001037 const unsigned char *zArg = sqlite3_value_text(argv[0]); 001038 char *z; 001039 001040 if( zArg==0 ) return; 001041 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 001042 z = contextMalloc(context, ((i64)i)+((i64)n)+3); 001043 if( z ){ 001044 z[0] = '\''; 001045 for(i=0, j=1; zArg[i]; i++){ 001046 z[j++] = zArg[i]; 001047 if( zArg[i]=='\'' ){ 001048 z[j++] = '\''; 001049 } 001050 } 001051 z[j++] = '\''; 001052 z[j] = 0; 001053 sqlite3_result_text(context, z, j, sqlite3_free); 001054 } 001055 break; 001056 } 001057 default: { 001058 assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 001059 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 001060 break; 001061 } 001062 } 001063 } 001064 001065 /* 001066 ** The unicode() function. Return the integer unicode code-point value 001067 ** for the first character of the input string. 001068 */ 001069 static void unicodeFunc( 001070 sqlite3_context *context, 001071 int argc, 001072 sqlite3_value **argv 001073 ){ 001074 const unsigned char *z = sqlite3_value_text(argv[0]); 001075 (void)argc; 001076 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); 001077 } 001078 001079 /* 001080 ** The char() function takes zero or more arguments, each of which is 001081 ** an integer. It constructs a string where each character of the string 001082 ** is the unicode character for the corresponding integer argument. 001083 */ 001084 static void charFunc( 001085 sqlite3_context *context, 001086 int argc, 001087 sqlite3_value **argv 001088 ){ 001089 unsigned char *z, *zOut; 001090 int i; 001091 zOut = z = sqlite3_malloc64( argc*4+1 ); 001092 if( z==0 ){ 001093 sqlite3_result_error_nomem(context); 001094 return; 001095 } 001096 for(i=0; i<argc; i++){ 001097 sqlite3_int64 x; 001098 unsigned c; 001099 x = sqlite3_value_int64(argv[i]); 001100 if( x<0 || x>0x10ffff ) x = 0xfffd; 001101 c = (unsigned)(x & 0x1fffff); 001102 if( c<0x00080 ){ 001103 *zOut++ = (u8)(c&0xFF); 001104 }else if( c<0x00800 ){ 001105 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); 001106 *zOut++ = 0x80 + (u8)(c & 0x3F); 001107 }else if( c<0x10000 ){ 001108 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); 001109 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 001110 *zOut++ = 0x80 + (u8)(c & 0x3F); 001111 }else{ 001112 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); 001113 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); 001114 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); 001115 *zOut++ = 0x80 + (u8)(c & 0x3F); 001116 } \ 001117 } 001118 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); 001119 } 001120 001121 /* 001122 ** The hex() function. Interpret the argument as a blob. Return 001123 ** a hexadecimal rendering as text. 001124 */ 001125 static void hexFunc( 001126 sqlite3_context *context, 001127 int argc, 001128 sqlite3_value **argv 001129 ){ 001130 int i, n; 001131 const unsigned char *pBlob; 001132 char *zHex, *z; 001133 assert( argc==1 ); 001134 UNUSED_PARAMETER(argc); 001135 pBlob = sqlite3_value_blob(argv[0]); 001136 n = sqlite3_value_bytes(argv[0]); 001137 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 001138 z = zHex = contextMalloc(context, ((i64)n)*2 + 1); 001139 if( zHex ){ 001140 for(i=0; i<n; i++, pBlob++){ 001141 unsigned char c = *pBlob; 001142 *(z++) = hexdigits[(c>>4)&0xf]; 001143 *(z++) = hexdigits[c&0xf]; 001144 } 001145 *z = 0; 001146 sqlite3_result_text(context, zHex, n*2, sqlite3_free); 001147 } 001148 } 001149 001150 /* 001151 ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 001152 */ 001153 static void zeroblobFunc( 001154 sqlite3_context *context, 001155 int argc, 001156 sqlite3_value **argv 001157 ){ 001158 i64 n; 001159 int rc; 001160 assert( argc==1 ); 001161 UNUSED_PARAMETER(argc); 001162 n = sqlite3_value_int64(argv[0]); 001163 if( n<0 ) n = 0; 001164 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ 001165 if( rc ){ 001166 sqlite3_result_error_code(context, rc); 001167 } 001168 } 001169 001170 /* 001171 ** The replace() function. Three arguments are all strings: call 001172 ** them A, B, and C. The result is also a string which is derived 001173 ** from A by replacing every occurrence of B with C. The match 001174 ** must be exact. Collating sequences are not used. 001175 */ 001176 static void replaceFunc( 001177 sqlite3_context *context, 001178 int argc, 001179 sqlite3_value **argv 001180 ){ 001181 const unsigned char *zStr; /* The input string A */ 001182 const unsigned char *zPattern; /* The pattern string B */ 001183 const unsigned char *zRep; /* The replacement string C */ 001184 unsigned char *zOut; /* The output */ 001185 int nStr; /* Size of zStr */ 001186 int nPattern; /* Size of zPattern */ 001187 int nRep; /* Size of zRep */ 001188 i64 nOut; /* Maximum size of zOut */ 001189 int loopLimit; /* Last zStr[] that might match zPattern[] */ 001190 int i, j; /* Loop counters */ 001191 001192 assert( argc==3 ); 001193 UNUSED_PARAMETER(argc); 001194 zStr = sqlite3_value_text(argv[0]); 001195 if( zStr==0 ) return; 001196 nStr = sqlite3_value_bytes(argv[0]); 001197 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ 001198 zPattern = sqlite3_value_text(argv[1]); 001199 if( zPattern==0 ){ 001200 assert( sqlite3_value_type(argv[1])==SQLITE_NULL 001201 || sqlite3_context_db_handle(context)->mallocFailed ); 001202 return; 001203 } 001204 if( zPattern[0]==0 ){ 001205 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); 001206 sqlite3_result_value(context, argv[0]); 001207 return; 001208 } 001209 nPattern = sqlite3_value_bytes(argv[1]); 001210 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ 001211 zRep = sqlite3_value_text(argv[2]); 001212 if( zRep==0 ) return; 001213 nRep = sqlite3_value_bytes(argv[2]); 001214 assert( zRep==sqlite3_value_text(argv[2]) ); 001215 nOut = nStr + 1; 001216 assert( nOut<SQLITE_MAX_LENGTH ); 001217 zOut = contextMalloc(context, (i64)nOut); 001218 if( zOut==0 ){ 001219 return; 001220 } 001221 loopLimit = nStr - nPattern; 001222 for(i=j=0; i<=loopLimit; i++){ 001223 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 001224 zOut[j++] = zStr[i]; 001225 }else{ 001226 u8 *zOld; 001227 sqlite3 *db = sqlite3_context_db_handle(context); 001228 nOut += nRep - nPattern; 001229 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); 001230 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); 001231 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001232 sqlite3_result_error_toobig(context); 001233 sqlite3_free(zOut); 001234 return; 001235 } 001236 zOld = zOut; 001237 zOut = sqlite3_realloc64(zOut, (int)nOut); 001238 if( zOut==0 ){ 001239 sqlite3_result_error_nomem(context); 001240 sqlite3_free(zOld); 001241 return; 001242 } 001243 memcpy(&zOut[j], zRep, nRep); 001244 j += nRep; 001245 i += nPattern-1; 001246 } 001247 } 001248 assert( j+nStr-i+1==nOut ); 001249 memcpy(&zOut[j], &zStr[i], nStr-i); 001250 j += nStr - i; 001251 assert( j<=nOut ); 001252 zOut[j] = 0; 001253 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 001254 } 001255 001256 /* 001257 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 001258 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 001259 */ 001260 static void trimFunc( 001261 sqlite3_context *context, 001262 int argc, 001263 sqlite3_value **argv 001264 ){ 001265 const unsigned char *zIn; /* Input string */ 001266 const unsigned char *zCharSet; /* Set of characters to trim */ 001267 int nIn; /* Number of bytes in input */ 001268 int flags; /* 1: trimleft 2: trimright 3: trim */ 001269 int i; /* Loop counter */ 001270 unsigned char *aLen = 0; /* Length of each character in zCharSet */ 001271 unsigned char **azChar = 0; /* Individual characters in zCharSet */ 001272 int nChar; /* Number of characters in zCharSet */ 001273 001274 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 001275 return; 001276 } 001277 zIn = sqlite3_value_text(argv[0]); 001278 if( zIn==0 ) return; 001279 nIn = sqlite3_value_bytes(argv[0]); 001280 assert( zIn==sqlite3_value_text(argv[0]) ); 001281 if( argc==1 ){ 001282 static const unsigned char lenOne[] = { 1 }; 001283 static unsigned char * const azOne[] = { (u8*)" " }; 001284 nChar = 1; 001285 aLen = (u8*)lenOne; 001286 azChar = (unsigned char **)azOne; 001287 zCharSet = 0; 001288 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 001289 return; 001290 }else{ 001291 const unsigned char *z; 001292 for(z=zCharSet, nChar=0; *z; nChar++){ 001293 SQLITE_SKIP_UTF8(z); 001294 } 001295 if( nChar>0 ){ 001296 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); 001297 if( azChar==0 ){ 001298 return; 001299 } 001300 aLen = (unsigned char*)&azChar[nChar]; 001301 for(z=zCharSet, nChar=0; *z; nChar++){ 001302 azChar[nChar] = (unsigned char *)z; 001303 SQLITE_SKIP_UTF8(z); 001304 aLen[nChar] = (u8)(z - azChar[nChar]); 001305 } 001306 } 001307 } 001308 if( nChar>0 ){ 001309 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); 001310 if( flags & 1 ){ 001311 while( nIn>0 ){ 001312 int len = 0; 001313 for(i=0; i<nChar; i++){ 001314 len = aLen[i]; 001315 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break; 001316 } 001317 if( i>=nChar ) break; 001318 zIn += len; 001319 nIn -= len; 001320 } 001321 } 001322 if( flags & 2 ){ 001323 while( nIn>0 ){ 001324 int len = 0; 001325 for(i=0; i<nChar; i++){ 001326 len = aLen[i]; 001327 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 001328 } 001329 if( i>=nChar ) break; 001330 nIn -= len; 001331 } 001332 } 001333 if( zCharSet ){ 001334 sqlite3_free(azChar); 001335 } 001336 } 001337 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 001338 } 001339 001340 001341 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 001342 /* 001343 ** The "unknown" function is automatically substituted in place of 001344 ** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN 001345 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used. 001346 ** When the "sqlite3" command-line shell is built using this functionality, 001347 ** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries 001348 ** involving application-defined functions to be examined in a generic 001349 ** sqlite3 shell. 001350 */ 001351 static void unknownFunc( 001352 sqlite3_context *context, 001353 int argc, 001354 sqlite3_value **argv 001355 ){ 001356 /* no-op */ 001357 } 001358 #endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/ 001359 001360 001361 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It 001362 ** is only available if the SQLITE_SOUNDEX compile-time option is used 001363 ** when SQLite is built. 001364 */ 001365 #ifdef SQLITE_SOUNDEX 001366 /* 001367 ** Compute the soundex encoding of a word. 001368 ** 001369 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the 001370 ** soundex encoding of the string X. 001371 */ 001372 static void soundexFunc( 001373 sqlite3_context *context, 001374 int argc, 001375 sqlite3_value **argv 001376 ){ 001377 char zResult[8]; 001378 const u8 *zIn; 001379 int i, j; 001380 static const unsigned char iCode[] = { 001381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 001382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 001383 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 001384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 001385 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 001386 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 001387 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 001388 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 001389 }; 001390 assert( argc==1 ); 001391 zIn = (u8*)sqlite3_value_text(argv[0]); 001392 if( zIn==0 ) zIn = (u8*)""; 001393 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} 001394 if( zIn[i] ){ 001395 u8 prevcode = iCode[zIn[i]&0x7f]; 001396 zResult[0] = sqlite3Toupper(zIn[i]); 001397 for(j=1; j<4 && zIn[i]; i++){ 001398 int code = iCode[zIn[i]&0x7f]; 001399 if( code>0 ){ 001400 if( code!=prevcode ){ 001401 prevcode = code; 001402 zResult[j++] = code + '0'; 001403 } 001404 }else{ 001405 prevcode = 0; 001406 } 001407 } 001408 while( j<4 ){ 001409 zResult[j++] = '0'; 001410 } 001411 zResult[j] = 0; 001412 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 001413 }else{ 001414 /* IMP: R-64894-50321 The string "?000" is returned if the argument 001415 ** is NULL or contains no ASCII alphabetic characters. */ 001416 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 001417 } 001418 } 001419 #endif /* SQLITE_SOUNDEX */ 001420 001421 #ifndef SQLITE_OMIT_LOAD_EXTENSION 001422 /* 001423 ** A function that loads a shared-library extension then returns NULL. 001424 */ 001425 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 001426 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 001427 const char *zProc; 001428 sqlite3 *db = sqlite3_context_db_handle(context); 001429 char *zErrMsg = 0; 001430 001431 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc 001432 ** flag is set. See the sqlite3_enable_load_extension() API. 001433 */ 001434 if( (db->flags & SQLITE_LoadExtFunc)==0 ){ 001435 sqlite3_result_error(context, "not authorized", -1); 001436 return; 001437 } 001438 001439 if( argc==2 ){ 001440 zProc = (const char *)sqlite3_value_text(argv[1]); 001441 }else{ 001442 zProc = 0; 001443 } 001444 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 001445 sqlite3_result_error(context, zErrMsg, -1); 001446 sqlite3_free(zErrMsg); 001447 } 001448 } 001449 #endif 001450 001451 001452 /* 001453 ** An instance of the following structure holds the context of a 001454 ** sum() or avg() aggregate computation. 001455 */ 001456 typedef struct SumCtx SumCtx; 001457 struct SumCtx { 001458 double rSum; /* Floating point sum */ 001459 i64 iSum; /* Integer sum */ 001460 i64 cnt; /* Number of elements summed */ 001461 u8 overflow; /* True if integer overflow seen */ 001462 u8 approx; /* True if non-integer value was input to the sum */ 001463 }; 001464 001465 /* 001466 ** Routines used to compute the sum, average, and total. 001467 ** 001468 ** The SUM() function follows the (broken) SQL standard which means 001469 ** that it returns NULL if it sums over no inputs. TOTAL returns 001470 ** 0.0 in that case. In addition, TOTAL always returns a float where 001471 ** SUM might return an integer if it never encounters a floating point 001472 ** value. TOTAL never fails, but SUM might through an exception if 001473 ** it overflows an integer. 001474 */ 001475 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 001476 SumCtx *p; 001477 int type; 001478 assert( argc==1 ); 001479 UNUSED_PARAMETER(argc); 001480 p = sqlite3_aggregate_context(context, sizeof(*p)); 001481 type = sqlite3_value_numeric_type(argv[0]); 001482 if( p && type!=SQLITE_NULL ){ 001483 p->cnt++; 001484 if( type==SQLITE_INTEGER ){ 001485 i64 v = sqlite3_value_int64(argv[0]); 001486 p->rSum += v; 001487 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ 001488 p->overflow = 1; 001489 } 001490 }else{ 001491 p->rSum += sqlite3_value_double(argv[0]); 001492 p->approx = 1; 001493 } 001494 } 001495 } 001496 static void sumFinalize(sqlite3_context *context){ 001497 SumCtx *p; 001498 p = sqlite3_aggregate_context(context, 0); 001499 if( p && p->cnt>0 ){ 001500 if( p->overflow ){ 001501 sqlite3_result_error(context,"integer overflow",-1); 001502 }else if( p->approx ){ 001503 sqlite3_result_double(context, p->rSum); 001504 }else{ 001505 sqlite3_result_int64(context, p->iSum); 001506 } 001507 } 001508 } 001509 static void avgFinalize(sqlite3_context *context){ 001510 SumCtx *p; 001511 p = sqlite3_aggregate_context(context, 0); 001512 if( p && p->cnt>0 ){ 001513 sqlite3_result_double(context, p->rSum/(double)p->cnt); 001514 } 001515 } 001516 static void totalFinalize(sqlite3_context *context){ 001517 SumCtx *p; 001518 p = sqlite3_aggregate_context(context, 0); 001519 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 001520 sqlite3_result_double(context, p ? p->rSum : (double)0); 001521 } 001522 001523 /* 001524 ** The following structure keeps track of state information for the 001525 ** count() aggregate function. 001526 */ 001527 typedef struct CountCtx CountCtx; 001528 struct CountCtx { 001529 i64 n; 001530 }; 001531 001532 /* 001533 ** Routines to implement the count() aggregate function. 001534 */ 001535 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 001536 CountCtx *p; 001537 p = sqlite3_aggregate_context(context, sizeof(*p)); 001538 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 001539 p->n++; 001540 } 001541 001542 #ifndef SQLITE_OMIT_DEPRECATED 001543 /* The sqlite3_aggregate_count() function is deprecated. But just to make 001544 ** sure it still operates correctly, verify that its count agrees with our 001545 ** internal count when using count(*) and when the total count can be 001546 ** expressed as a 32-bit integer. */ 001547 assert( argc==1 || p==0 || p->n>0x7fffffff 001548 || p->n==sqlite3_aggregate_count(context) ); 001549 #endif 001550 } 001551 static void countFinalize(sqlite3_context *context){ 001552 CountCtx *p; 001553 p = sqlite3_aggregate_context(context, 0); 001554 sqlite3_result_int64(context, p ? p->n : 0); 001555 } 001556 001557 /* 001558 ** Routines to implement min() and max() aggregate functions. 001559 */ 001560 static void minmaxStep( 001561 sqlite3_context *context, 001562 int NotUsed, 001563 sqlite3_value **argv 001564 ){ 001565 Mem *pArg = (Mem *)argv[0]; 001566 Mem *pBest; 001567 UNUSED_PARAMETER(NotUsed); 001568 001569 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 001570 if( !pBest ) return; 001571 001572 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 001573 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); 001574 }else if( pBest->flags ){ 001575 int max; 001576 int cmp; 001577 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 001578 /* This step function is used for both the min() and max() aggregates, 001579 ** the only difference between the two being that the sense of the 001580 ** comparison is inverted. For the max() aggregate, the 001581 ** sqlite3_user_data() function returns (void *)-1. For min() it 001582 ** returns (void *)db, where db is the sqlite3* database pointer. 001583 ** Therefore the next statement sets variable 'max' to 1 for the max() 001584 ** aggregate, or 0 for min(). 001585 */ 001586 max = sqlite3_user_data(context)!=0; 001587 cmp = sqlite3MemCompare(pBest, pArg, pColl); 001588 if( (max && cmp<0) || (!max && cmp>0) ){ 001589 sqlite3VdbeMemCopy(pBest, pArg); 001590 }else{ 001591 sqlite3SkipAccumulatorLoad(context); 001592 } 001593 }else{ 001594 pBest->db = sqlite3_context_db_handle(context); 001595 sqlite3VdbeMemCopy(pBest, pArg); 001596 } 001597 } 001598 static void minMaxFinalize(sqlite3_context *context){ 001599 sqlite3_value *pRes; 001600 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 001601 if( pRes ){ 001602 if( pRes->flags ){ 001603 sqlite3_result_value(context, pRes); 001604 } 001605 sqlite3VdbeMemRelease(pRes); 001606 } 001607 } 001608 001609 /* 001610 ** group_concat(EXPR, ?SEPARATOR?) 001611 */ 001612 static void groupConcatStep( 001613 sqlite3_context *context, 001614 int argc, 001615 sqlite3_value **argv 001616 ){ 001617 const char *zVal; 001618 StrAccum *pAccum; 001619 const char *zSep; 001620 int nVal, nSep; 001621 assert( argc==1 || argc==2 ); 001622 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 001623 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 001624 001625 if( pAccum ){ 001626 sqlite3 *db = sqlite3_context_db_handle(context); 001627 int firstTerm = pAccum->mxAlloc==0; 001628 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 001629 if( !firstTerm ){ 001630 if( argc==2 ){ 001631 zSep = (char*)sqlite3_value_text(argv[1]); 001632 nSep = sqlite3_value_bytes(argv[1]); 001633 }else{ 001634 zSep = ","; 001635 nSep = 1; 001636 } 001637 if( zSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); 001638 } 001639 zVal = (char*)sqlite3_value_text(argv[0]); 001640 nVal = sqlite3_value_bytes(argv[0]); 001641 if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal); 001642 } 001643 } 001644 static void groupConcatFinalize(sqlite3_context *context){ 001645 StrAccum *pAccum; 001646 pAccum = sqlite3_aggregate_context(context, 0); 001647 if( pAccum ){ 001648 if( pAccum->accError==STRACCUM_TOOBIG ){ 001649 sqlite3_result_error_toobig(context); 001650 }else if( pAccum->accError==STRACCUM_NOMEM ){ 001651 sqlite3_result_error_nomem(context); 001652 }else{ 001653 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 001654 sqlite3_free); 001655 } 001656 } 001657 } 001658 001659 /* 001660 ** This routine does per-connection function registration. Most 001661 ** of the built-in functions above are part of the global function set. 001662 ** This routine only deals with those that are not global. 001663 */ 001664 void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ 001665 int rc = sqlite3_overload_function(db, "MATCH", 2); 001666 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 001667 if( rc==SQLITE_NOMEM ){ 001668 sqlite3OomFault(db); 001669 } 001670 } 001671 001672 /* 001673 ** Set the LIKEOPT flag on the 2-argument function with the given name. 001674 */ 001675 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ 001676 FuncDef *pDef; 001677 pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0); 001678 if( ALWAYS(pDef) ){ 001679 pDef->funcFlags |= flagVal; 001680 } 001681 } 001682 001683 /* 001684 ** Register the built-in LIKE and GLOB functions. The caseSensitive 001685 ** parameter determines whether or not the LIKE operator is case 001686 ** sensitive. GLOB is always case sensitive. 001687 */ 001688 void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 001689 struct compareInfo *pInfo; 001690 if( caseSensitive ){ 001691 pInfo = (struct compareInfo*)&likeInfoAlt; 001692 }else{ 001693 pInfo = (struct compareInfo*)&likeInfoNorm; 001694 } 001695 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); 001696 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); 001697 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 001698 (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); 001699 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 001700 setLikeOptFlag(db, "like", 001701 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 001702 } 001703 001704 /* 001705 ** pExpr points to an expression which implements a function. If 001706 ** it is appropriate to apply the LIKE optimization to that function 001707 ** then set aWc[0] through aWc[2] to the wildcard characters and 001708 ** return TRUE. If the function is not a LIKE-style function then 001709 ** return FALSE. 001710 ** 001711 ** *pIsNocase is set to true if uppercase and lowercase are equivalent for 001712 ** the function (default for LIKE). If the function makes the distinction 001713 ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to 001714 ** false. 001715 */ 001716 int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 001717 FuncDef *pDef; 001718 if( pExpr->op!=TK_FUNCTION 001719 || !pExpr->x.pList 001720 || pExpr->x.pList->nExpr!=2 001721 ){ 001722 return 0; 001723 } 001724 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 001725 pDef = sqlite3FindFunction(db, pExpr->u.zToken, 2, SQLITE_UTF8, 0); 001726 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ 001727 return 0; 001728 } 001729 001730 /* The memcpy() statement assumes that the wildcard characters are 001731 ** the first three statements in the compareInfo structure. The 001732 ** asserts() that follow verify that assumption 001733 */ 001734 memcpy(aWc, pDef->pUserData, 3); 001735 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 001736 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 001737 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 001738 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; 001739 return 1; 001740 } 001741 001742 /* 001743 ** All of the FuncDef structures in the aBuiltinFunc[] array above 001744 ** to the global function hash table. This occurs at start-time (as 001745 ** a consequence of calling sqlite3_initialize()). 001746 ** 001747 ** After this routine runs 001748 */ 001749 void sqlite3RegisterBuiltinFunctions(void){ 001750 /* 001751 ** The following array holds FuncDef structures for all of the functions 001752 ** defined in this file. 001753 ** 001754 ** The array cannot be constant since changes are made to the 001755 ** FuncDef.pHash elements at start-time. The elements of this array 001756 ** are read-only after initialization is complete. 001757 ** 001758 ** For peak efficiency, put the most frequently used function last. 001759 */ 001760 static FuncDef aBuiltinFunc[] = { 001761 #ifdef SQLITE_SOUNDEX 001762 FUNCTION(soundex, 1, 0, 0, soundexFunc ), 001763 #endif 001764 #ifndef SQLITE_OMIT_LOAD_EXTENSION 001765 VFUNCTION(load_extension, 1, 0, 0, loadExt ), 001766 VFUNCTION(load_extension, 2, 0, 0, loadExt ), 001767 #endif 001768 #if SQLITE_USER_AUTHENTICATION 001769 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), 001770 #endif 001771 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 001772 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), 001773 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), 001774 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 001775 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 001776 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 001777 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), 001778 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 001779 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 001780 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 001781 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 001782 FUNCTION(trim, 1, 3, 0, trimFunc ), 001783 FUNCTION(trim, 2, 3, 0, trimFunc ), 001784 FUNCTION(min, -1, 0, 1, minmaxFunc ), 001785 FUNCTION(min, 0, 0, 1, 0 ), 001786 AGGREGATE2(min, 1, 0, 1, minmaxStep, minMaxFinalize, 001787 SQLITE_FUNC_MINMAX ), 001788 FUNCTION(max, -1, 1, 1, minmaxFunc ), 001789 FUNCTION(max, 0, 1, 1, 0 ), 001790 AGGREGATE2(max, 1, 1, 1, minmaxStep, minMaxFinalize, 001791 SQLITE_FUNC_MINMAX ), 001792 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 001793 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 001794 FUNCTION(instr, 2, 0, 0, instrFunc ), 001795 FUNCTION(printf, -1, 0, 0, printfFunc ), 001796 FUNCTION(unicode, 1, 0, 0, unicodeFunc ), 001797 FUNCTION(char, -1, 0, 0, charFunc ), 001798 FUNCTION(abs, 1, 0, 0, absFunc ), 001799 #ifndef SQLITE_OMIT_FLOATING_POINT 001800 FUNCTION(round, 1, 0, 0, roundFunc ), 001801 FUNCTION(round, 2, 0, 0, roundFunc ), 001802 #endif 001803 FUNCTION(upper, 1, 0, 0, upperFunc ), 001804 FUNCTION(lower, 1, 0, 0, lowerFunc ), 001805 FUNCTION(hex, 1, 0, 0, hexFunc ), 001806 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 001807 VFUNCTION(random, 0, 0, 0, randomFunc ), 001808 VFUNCTION(randomblob, 1, 0, 0, randomBlob ), 001809 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 001810 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 001811 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), 001812 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), 001813 FUNCTION(quote, 1, 0, 0, quoteFunc ), 001814 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 001815 VFUNCTION(changes, 0, 0, 0, changes ), 001816 VFUNCTION(total_changes, 0, 0, 0, total_changes ), 001817 FUNCTION(replace, 3, 0, 0, replaceFunc ), 001818 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 001819 FUNCTION(substr, 2, 0, 0, substrFunc ), 001820 FUNCTION(substr, 3, 0, 0, substrFunc ), 001821 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), 001822 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), 001823 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), 001824 AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, 001825 SQLITE_FUNC_COUNT ), 001826 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), 001827 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), 001828 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), 001829 001830 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 001831 #ifdef SQLITE_CASE_SENSITIVE_LIKE 001832 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 001833 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 001834 #else 001835 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 001836 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 001837 #endif 001838 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 001839 FUNCTION(unknown, -1, 0, 0, unknownFunc ), 001840 #endif 001841 FUNCTION(coalesce, 1, 0, 0, 0 ), 001842 FUNCTION(coalesce, 0, 0, 0, 0 ), 001843 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), 001844 }; 001845 #ifndef SQLITE_OMIT_ALTERTABLE 001846 sqlite3AlterFunctions(); 001847 #endif 001848 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) 001849 sqlite3AnalyzeFunctions(); 001850 #endif 001851 sqlite3RegisterDateTimeFunctions(); 001852 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); 001853 001854 #if 0 /* Enable to print out how the built-in functions are hashed */ 001855 { 001856 int i; 001857 FuncDef *p; 001858 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ 001859 printf("FUNC-HASH %02d:", i); 001860 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){ 001861 int n = sqlite3Strlen30(p->zName); 001862 int h = p->zName[0] + n; 001863 printf(" %s(%d)", p->zName, h); 001864 } 001865 printf("\n"); 001866 } 001867 } 001868 #endif 001869 }