00001 /* 00002 ** 2002 February 23 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** This file contains the C functions that implement various SQL 00013 ** functions of SQLite. 00014 ** 00015 ** There is only one exported symbol in this file - the function 00016 ** sqliteRegisterBuildinFunctions() found at the bottom of the file. 00017 ** All other code has file scope. 00018 ** 00019 ** $Id: func.c,v 1.204 2008/10/28 17:52:39 danielk1977 Exp $ 00020 */ 00021 #include "sqliteInt.h" 00022 #include <ctype.h> 00023 #include <stdlib.h> 00024 #include <assert.h> 00025 #include "vdbeInt.h" 00026 00027 /* 00028 ** Return the collating function associated with a function. 00029 */ 00030 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 00031 return context->pColl; 00032 } 00033 00034 /* 00035 ** Implementation of the non-aggregate min() and max() functions 00036 */ 00037 static void minmaxFunc( 00038 sqlite3_context *context, 00039 int argc, 00040 sqlite3_value **argv 00041 ){ 00042 int i; 00043 int mask; /* 0 for min() or 0xffffffff for max() */ 00044 int iBest; 00045 CollSeq *pColl; 00046 00047 if( argc==0 ) return; 00048 mask = sqlite3_user_data(context)==0 ? 0 : -1; 00049 pColl = sqlite3GetFuncCollSeq(context); 00050 assert( pColl ); 00051 assert( mask==-1 || mask==0 ); 00052 iBest = 0; 00053 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 00054 for(i=1; i<argc; i++){ 00055 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 00056 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 00057 iBest = i; 00058 } 00059 } 00060 sqlite3_result_value(context, argv[iBest]); 00061 } 00062 00063 /* 00064 ** Return the type of the argument. 00065 */ 00066 static void typeofFunc( 00067 sqlite3_context *context, 00068 int argc, 00069 sqlite3_value **argv 00070 ){ 00071 const char *z = 0; 00072 switch( sqlite3_value_type(argv[0]) ){ 00073 case SQLITE_NULL: z = "null"; break; 00074 case SQLITE_INTEGER: z = "integer"; break; 00075 case SQLITE_TEXT: z = "text"; break; 00076 case SQLITE_FLOAT: z = "real"; break; 00077 case SQLITE_BLOB: z = "blob"; break; 00078 } 00079 sqlite3_result_text(context, z, -1, SQLITE_STATIC); 00080 } 00081 00082 00083 /* 00084 ** Implementation of the length() function 00085 */ 00086 static void lengthFunc( 00087 sqlite3_context *context, 00088 int argc, 00089 sqlite3_value **argv 00090 ){ 00091 int len; 00092 00093 assert( argc==1 ); 00094 switch( sqlite3_value_type(argv[0]) ){ 00095 case SQLITE_BLOB: 00096 case SQLITE_INTEGER: 00097 case SQLITE_FLOAT: { 00098 sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 00099 break; 00100 } 00101 case SQLITE_TEXT: { 00102 const unsigned char *z = sqlite3_value_text(argv[0]); 00103 if( z==0 ) return; 00104 len = 0; 00105 while( *z ){ 00106 len++; 00107 SQLITE_SKIP_UTF8(z); 00108 } 00109 sqlite3_result_int(context, len); 00110 break; 00111 } 00112 default: { 00113 sqlite3_result_null(context); 00114 break; 00115 } 00116 } 00117 } 00118 00119 /* 00120 ** Implementation of the abs() function 00121 */ 00122 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 00123 assert( argc==1 ); 00124 switch( sqlite3_value_type(argv[0]) ){ 00125 case SQLITE_INTEGER: { 00126 i64 iVal = sqlite3_value_int64(argv[0]); 00127 if( iVal<0 ){ 00128 if( (iVal<<1)==0 ){ 00129 sqlite3_result_error(context, "integer overflow", -1); 00130 return; 00131 } 00132 iVal = -iVal; 00133 } 00134 sqlite3_result_int64(context, iVal); 00135 break; 00136 } 00137 case SQLITE_NULL: { 00138 sqlite3_result_null(context); 00139 break; 00140 } 00141 default: { 00142 double rVal = sqlite3_value_double(argv[0]); 00143 if( rVal<0 ) rVal = -rVal; 00144 sqlite3_result_double(context, rVal); 00145 break; 00146 } 00147 } 00148 } 00149 00150 /* 00151 ** Implementation of the substr() function. 00152 ** 00153 ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. 00154 ** p1 is 1-indexed. So substr(x,1,1) returns the first character 00155 ** of x. If x is text, then we actually count UTF-8 characters. 00156 ** If x is a blob, then we count bytes. 00157 ** 00158 ** If p1 is negative, then we begin abs(p1) from the end of x[]. 00159 */ 00160 static void substrFunc( 00161 sqlite3_context *context, 00162 int argc, 00163 sqlite3_value **argv 00164 ){ 00165 const unsigned char *z; 00166 const unsigned char *z2; 00167 int len; 00168 int p0type; 00169 i64 p1, p2; 00170 00171 assert( argc==3 || argc==2 ); 00172 p0type = sqlite3_value_type(argv[0]); 00173 if( p0type==SQLITE_BLOB ){ 00174 len = sqlite3_value_bytes(argv[0]); 00175 z = sqlite3_value_blob(argv[0]); 00176 if( z==0 ) return; 00177 assert( len==sqlite3_value_bytes(argv[0]) ); 00178 }else{ 00179 z = sqlite3_value_text(argv[0]); 00180 if( z==0 ) return; 00181 len = 0; 00182 for(z2=z; *z2; len++){ 00183 SQLITE_SKIP_UTF8(z2); 00184 } 00185 } 00186 p1 = sqlite3_value_int(argv[1]); 00187 if( argc==3 ){ 00188 p2 = sqlite3_value_int(argv[2]); 00189 }else{ 00190 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; 00191 } 00192 if( p1<0 ){ 00193 p1 += len; 00194 if( p1<0 ){ 00195 p2 += p1; 00196 p1 = 0; 00197 } 00198 }else if( p1>0 ){ 00199 p1--; 00200 } 00201 if( p1+p2>len ){ 00202 p2 = len-p1; 00203 } 00204 if( p0type!=SQLITE_BLOB ){ 00205 while( *z && p1 ){ 00206 SQLITE_SKIP_UTF8(z); 00207 p1--; 00208 } 00209 for(z2=z; *z2 && p2; p2--){ 00210 SQLITE_SKIP_UTF8(z2); 00211 } 00212 sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT); 00213 }else{ 00214 if( p2<0 ) p2 = 0; 00215 sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); 00216 } 00217 } 00218 00219 /* 00220 ** Implementation of the round() function 00221 */ 00222 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 00223 int n = 0; 00224 double r; 00225 char zBuf[500]; /* larger than the %f representation of the largest double */ 00226 assert( argc==1 || argc==2 ); 00227 if( argc==2 ){ 00228 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 00229 n = sqlite3_value_int(argv[1]); 00230 if( n>30 ) n = 30; 00231 if( n<0 ) n = 0; 00232 } 00233 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 00234 r = sqlite3_value_double(argv[0]); 00235 sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); 00236 sqlite3AtoF(zBuf, &r); 00237 sqlite3_result_double(context, r); 00238 } 00239 00240 /* 00241 ** Allocate nByte bytes of space using sqlite3_malloc(). If the 00242 ** allocation fails, call sqlite3_result_error_nomem() to notify 00243 ** the database handle that malloc() has failed. 00244 */ 00245 static void *contextMalloc(sqlite3_context *context, i64 nByte){ 00246 char *z; 00247 if( nByte>sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH] ){ 00248 sqlite3_result_error_toobig(context); 00249 z = 0; 00250 }else{ 00251 z = sqlite3Malloc(nByte); 00252 if( !z && nByte>0 ){ 00253 sqlite3_result_error_nomem(context); 00254 } 00255 } 00256 return z; 00257 } 00258 00259 /* 00260 ** Implementation of the upper() and lower() SQL functions. 00261 */ 00262 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 00263 char *z1; 00264 const char *z2; 00265 int i, n; 00266 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 00267 z2 = (char*)sqlite3_value_text(argv[0]); 00268 n = sqlite3_value_bytes(argv[0]); 00269 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 00270 assert( z2==(char*)sqlite3_value_text(argv[0]) ); 00271 if( z2 ){ 00272 z1 = contextMalloc(context, ((i64)n)+1); 00273 if( z1 ){ 00274 memcpy(z1, z2, n+1); 00275 for(i=0; z1[i]; i++){ 00276 z1[i] = toupper(z1[i]); 00277 } 00278 sqlite3_result_text(context, z1, -1, sqlite3_free); 00279 } 00280 } 00281 } 00282 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 00283 char *z1; 00284 const char *z2; 00285 int i, n; 00286 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 00287 z2 = (char*)sqlite3_value_text(argv[0]); 00288 n = sqlite3_value_bytes(argv[0]); 00289 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ 00290 assert( z2==(char*)sqlite3_value_text(argv[0]) ); 00291 if( z2 ){ 00292 z1 = contextMalloc(context, ((i64)n)+1); 00293 if( z1 ){ 00294 memcpy(z1, z2, n+1); 00295 for(i=0; z1[i]; i++){ 00296 z1[i] = tolower(z1[i]); 00297 } 00298 sqlite3_result_text(context, z1, -1, sqlite3_free); 00299 } 00300 } 00301 } 00302 00303 /* 00304 ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 00305 ** All three do the same thing. They return the first non-NULL 00306 ** argument. 00307 */ 00308 static void ifnullFunc( 00309 sqlite3_context *context, 00310 int argc, 00311 sqlite3_value **argv 00312 ){ 00313 int i; 00314 for(i=0; i<argc; i++){ 00315 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 00316 sqlite3_result_value(context, argv[i]); 00317 break; 00318 } 00319 } 00320 } 00321 00322 /* 00323 ** Implementation of random(). Return a random integer. 00324 */ 00325 static void randomFunc( 00326 sqlite3_context *context, 00327 int argc, 00328 sqlite3_value **argv 00329 ){ 00330 sqlite_int64 r; 00331 sqlite3_randomness(sizeof(r), &r); 00332 if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ 00333 /* can always do abs() of the result */ 00334 sqlite3_result_int64(context, r); 00335 } 00336 00337 /* 00338 ** Implementation of randomblob(N). Return a random blob 00339 ** that is N bytes long. 00340 */ 00341 static void randomBlob( 00342 sqlite3_context *context, 00343 int argc, 00344 sqlite3_value **argv 00345 ){ 00346 int n; 00347 unsigned char *p; 00348 assert( argc==1 ); 00349 n = sqlite3_value_int(argv[0]); 00350 if( n<1 ){ 00351 n = 1; 00352 } 00353 p = contextMalloc(context, n); 00354 if( p ){ 00355 sqlite3_randomness(n, p); 00356 sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 00357 } 00358 } 00359 00360 /* 00361 ** Implementation of the last_insert_rowid() SQL function. The return 00362 ** value is the same as the sqlite3_last_insert_rowid() API function. 00363 */ 00364 static void last_insert_rowid( 00365 sqlite3_context *context, 00366 int arg, 00367 sqlite3_value **argv 00368 ){ 00369 sqlite3 *db = sqlite3_context_db_handle(context); 00370 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 00371 } 00372 00373 /* 00374 ** Implementation of the changes() SQL function. The return value is the 00375 ** same as the sqlite3_changes() API function. 00376 */ 00377 static void changes( 00378 sqlite3_context *context, 00379 int arg, 00380 sqlite3_value **argv 00381 ){ 00382 sqlite3 *db = sqlite3_context_db_handle(context); 00383 sqlite3_result_int(context, sqlite3_changes(db)); 00384 } 00385 00386 /* 00387 ** Implementation of the total_changes() SQL function. The return value is 00388 ** the same as the sqlite3_total_changes() API function. 00389 */ 00390 static void total_changes( 00391 sqlite3_context *context, 00392 int arg, 00393 sqlite3_value **argv 00394 ){ 00395 sqlite3 *db = sqlite3_context_db_handle(context); 00396 sqlite3_result_int(context, sqlite3_total_changes(db)); 00397 } 00398 00399 /* 00400 ** A structure defining how to do GLOB-style comparisons. 00401 */ 00402 struct compareInfo { 00403 u8 matchAll; 00404 u8 matchOne; 00405 u8 matchSet; 00406 u8 noCase; 00407 }; 00408 00409 /* 00410 ** For LIKE and GLOB matching on EBCDIC machines, assume that every 00411 ** character is exactly one byte in size. Also, all characters are 00412 ** able to participate in upper-case-to-lower-case mappings in EBCDIC 00413 ** whereas only characters less than 0x80 do in ASCII. 00414 */ 00415 #if defined(SQLITE_EBCDIC) 00416 # define sqlite3Utf8Read(A,B,C) (*(A++)) 00417 # define GlogUpperToLower(A) A = sqlite3UpperToLower[A] 00418 #else 00419 # define GlogUpperToLower(A) if( A<0x80 ){ A = sqlite3UpperToLower[A]; } 00420 #endif 00421 00422 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 00423 /* The correct SQL-92 behavior is for the LIKE operator to ignore 00424 ** case. Thus 'a' LIKE 'A' would be true. */ 00425 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 00426 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 00427 ** is case sensitive causing 'a' LIKE 'A' to be false */ 00428 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 00429 00430 /* 00431 ** Compare two UTF-8 strings for equality where the first string can 00432 ** potentially be a "glob" expression. Return true (1) if they 00433 ** are the same and false (0) if they are different. 00434 ** 00435 ** Globbing rules: 00436 ** 00437 ** '*' Matches any sequence of zero or more characters. 00438 ** 00439 ** '?' Matches exactly one character. 00440 ** 00441 ** [...] Matches one character from the enclosed list of 00442 ** characters. 00443 ** 00444 ** [^...] Matches one character not in the enclosed list. 00445 ** 00446 ** With the [...] and [^...] matching, a ']' character can be included 00447 ** in the list by making it the first character after '[' or '^'. A 00448 ** range of characters can be specified using '-'. Example: 00449 ** "[a-z]" matches any single lower-case letter. To match a '-', make 00450 ** it the last character in the list. 00451 ** 00452 ** This routine is usually quick, but can be N**2 in the worst case. 00453 ** 00454 ** Hints: to match '*' or '?', put them in "[]". Like this: 00455 ** 00456 ** abc[*]xyz Matches "abc*xyz" only 00457 */ 00458 static int patternCompare( 00459 const u8 *zPattern, /* The glob pattern */ 00460 const u8 *zString, /* The string to compare against the glob */ 00461 const struct compareInfo *pInfo, /* Information about how to do the compare */ 00462 const int esc /* The escape character */ 00463 ){ 00464 int c, c2; 00465 int invert; 00466 int seen; 00467 u8 matchOne = pInfo->matchOne; 00468 u8 matchAll = pInfo->matchAll; 00469 u8 matchSet = pInfo->matchSet; 00470 u8 noCase = pInfo->noCase; 00471 int prevEscape = 0; /* True if the previous character was 'escape' */ 00472 00473 while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){ 00474 if( !prevEscape && c==matchAll ){ 00475 while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll 00476 || c == matchOne ){ 00477 if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){ 00478 return 0; 00479 } 00480 } 00481 if( c==0 ){ 00482 return 1; 00483 }else if( c==esc ){ 00484 c = sqlite3Utf8Read(zPattern, 0, &zPattern); 00485 if( c==0 ){ 00486 return 0; 00487 } 00488 }else if( c==matchSet ){ 00489 assert( esc==0 ); /* This is GLOB, not LIKE */ 00490 assert( matchSet<0x80 ); /* '[' is a single-byte character */ 00491 while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ 00492 SQLITE_SKIP_UTF8(zString); 00493 } 00494 return *zString!=0; 00495 } 00496 while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){ 00497 if( noCase ){ 00498 GlogUpperToLower(c2); 00499 GlogUpperToLower(c); 00500 while( c2 != 0 && c2 != c ){ 00501 c2 = sqlite3Utf8Read(zString, 0, &zString); 00502 GlogUpperToLower(c2); 00503 } 00504 }else{ 00505 while( c2 != 0 && c2 != c ){ 00506 c2 = sqlite3Utf8Read(zString, 0, &zString); 00507 } 00508 } 00509 if( c2==0 ) return 0; 00510 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; 00511 } 00512 return 0; 00513 }else if( !prevEscape && c==matchOne ){ 00514 if( sqlite3Utf8Read(zString, 0, &zString)==0 ){ 00515 return 0; 00516 } 00517 }else if( c==matchSet ){ 00518 int prior_c = 0; 00519 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ 00520 seen = 0; 00521 invert = 0; 00522 c = sqlite3Utf8Read(zString, 0, &zString); 00523 if( c==0 ) return 0; 00524 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); 00525 if( c2=='^' ){ 00526 invert = 1; 00527 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); 00528 } 00529 if( c2==']' ){ 00530 if( c==']' ) seen = 1; 00531 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); 00532 } 00533 while( c2 && c2!=']' ){ 00534 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ 00535 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); 00536 if( c>=prior_c && c<=c2 ) seen = 1; 00537 prior_c = 0; 00538 }else{ 00539 if( c==c2 ){ 00540 seen = 1; 00541 } 00542 prior_c = c2; 00543 } 00544 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); 00545 } 00546 if( c2==0 || (seen ^ invert)==0 ){ 00547 return 0; 00548 } 00549 }else if( esc==c && !prevEscape ){ 00550 prevEscape = 1; 00551 }else{ 00552 c2 = sqlite3Utf8Read(zString, 0, &zString); 00553 if( noCase ){ 00554 GlogUpperToLower(c); 00555 GlogUpperToLower(c2); 00556 } 00557 if( c!=c2 ){ 00558 return 0; 00559 } 00560 prevEscape = 0; 00561 } 00562 } 00563 return *zString==0; 00564 } 00565 00566 /* 00567 ** Count the number of times that the LIKE operator (or GLOB which is 00568 ** just a variation of LIKE) gets called. This is used for testing 00569 ** only. 00570 */ 00571 #ifdef SQLITE_TEST 00572 int sqlite3_like_count = 0; 00573 #endif 00574 00575 00576 /* 00577 ** Implementation of the like() SQL function. This function implements 00578 ** the build-in LIKE operator. The first argument to the function is the 00579 ** pattern and the second argument is the string. So, the SQL statements: 00580 ** 00581 ** A LIKE B 00582 ** 00583 ** is implemented as like(B,A). 00584 ** 00585 ** This same function (with a different compareInfo structure) computes 00586 ** the GLOB operator. 00587 */ 00588 static void likeFunc( 00589 sqlite3_context *context, 00590 int argc, 00591 sqlite3_value **argv 00592 ){ 00593 const unsigned char *zA, *zB; 00594 int escape = 0; 00595 sqlite3 *db = sqlite3_context_db_handle(context); 00596 00597 zB = sqlite3_value_text(argv[0]); 00598 zA = sqlite3_value_text(argv[1]); 00599 00600 /* Limit the length of the LIKE or GLOB pattern to avoid problems 00601 ** of deep recursion and N*N behavior in patternCompare(). 00602 */ 00603 if( sqlite3_value_bytes(argv[0]) > 00604 db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ 00605 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); 00606 return; 00607 } 00608 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ 00609 00610 if( argc==3 ){ 00611 /* The escape character string must consist of a single UTF-8 character. 00612 ** Otherwise, return an error. 00613 */ 00614 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 00615 if( zEsc==0 ) return; 00616 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ 00617 sqlite3_result_error(context, 00618 "ESCAPE expression must be a single character", -1); 00619 return; 00620 } 00621 escape = sqlite3Utf8Read(zEsc, 0, &zEsc); 00622 } 00623 if( zA && zB ){ 00624 struct compareInfo *pInfo = sqlite3_user_data(context); 00625 #ifdef SQLITE_TEST 00626 sqlite3_like_count++; 00627 #endif 00628 00629 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); 00630 } 00631 } 00632 00633 /* 00634 ** Implementation of the NULLIF(x,y) function. The result is the first 00635 ** argument if the arguments are different. The result is NULL if the 00636 ** arguments are equal to each other. 00637 */ 00638 static void nullifFunc( 00639 sqlite3_context *context, 00640 int argc, 00641 sqlite3_value **argv 00642 ){ 00643 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 00644 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 00645 sqlite3_result_value(context, argv[0]); 00646 } 00647 } 00648 00649 /* 00650 ** Implementation of the VERSION(*) function. The result is the version 00651 ** of the SQLite library that is running. 00652 */ 00653 static void versionFunc( 00654 sqlite3_context *context, 00655 int argc, 00656 sqlite3_value **argv 00657 ){ 00658 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); 00659 } 00660 00661 /* Array for converting from half-bytes (nybbles) into ASCII hex 00662 ** digits. */ 00663 static const char hexdigits[] = { 00664 '0', '1', '2', '3', '4', '5', '6', '7', 00665 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 00666 }; 00667 00668 /* 00669 ** EXPERIMENTAL - This is not an official function. The interface may 00670 ** change. This function may disappear. Do not write code that depends 00671 ** on this function. 00672 ** 00673 ** Implementation of the QUOTE() function. This function takes a single 00674 ** argument. If the argument is numeric, the return value is the same as 00675 ** the argument. If the argument is NULL, the return value is the string 00676 ** "NULL". Otherwise, the argument is enclosed in single quotes with 00677 ** single-quote escapes. 00678 */ 00679 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 00680 if( argc<1 ) return; 00681 switch( sqlite3_value_type(argv[0]) ){ 00682 case SQLITE_NULL: { 00683 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 00684 break; 00685 } 00686 case SQLITE_INTEGER: 00687 case SQLITE_FLOAT: { 00688 sqlite3_result_value(context, argv[0]); 00689 break; 00690 } 00691 case SQLITE_BLOB: { 00692 char *zText = 0; 00693 char const *zBlob = sqlite3_value_blob(argv[0]); 00694 int nBlob = sqlite3_value_bytes(argv[0]); 00695 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 00696 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 00697 if( zText ){ 00698 int i; 00699 for(i=0; i<nBlob; i++){ 00700 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 00701 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 00702 } 00703 zText[(nBlob*2)+2] = '\''; 00704 zText[(nBlob*2)+3] = '\0'; 00705 zText[0] = 'X'; 00706 zText[1] = '\''; 00707 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 00708 sqlite3_free(zText); 00709 } 00710 break; 00711 } 00712 case SQLITE_TEXT: { 00713 int i,j; 00714 u64 n; 00715 const unsigned char *zArg = sqlite3_value_text(argv[0]); 00716 char *z; 00717 00718 if( zArg==0 ) return; 00719 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 00720 z = contextMalloc(context, ((i64)i)+((i64)n)+3); 00721 if( z ){ 00722 z[0] = '\''; 00723 for(i=0, j=1; zArg[i]; i++){ 00724 z[j++] = zArg[i]; 00725 if( zArg[i]=='\'' ){ 00726 z[j++] = '\''; 00727 } 00728 } 00729 z[j++] = '\''; 00730 z[j] = 0; 00731 sqlite3_result_text(context, z, j, sqlite3_free); 00732 } 00733 } 00734 } 00735 } 00736 00737 /* 00738 ** The hex() function. Interpret the argument as a blob. Return 00739 ** a hexadecimal rendering as text. 00740 */ 00741 static void hexFunc( 00742 sqlite3_context *context, 00743 int argc, 00744 sqlite3_value **argv 00745 ){ 00746 int i, n; 00747 const unsigned char *pBlob; 00748 char *zHex, *z; 00749 assert( argc==1 ); 00750 pBlob = sqlite3_value_blob(argv[0]); 00751 n = sqlite3_value_bytes(argv[0]); 00752 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ 00753 z = zHex = contextMalloc(context, ((i64)n)*2 + 1); 00754 if( zHex ){ 00755 for(i=0; i<n; i++, pBlob++){ 00756 unsigned char c = *pBlob; 00757 *(z++) = hexdigits[(c>>4)&0xf]; 00758 *(z++) = hexdigits[c&0xf]; 00759 } 00760 *z = 0; 00761 sqlite3_result_text(context, zHex, n*2, sqlite3_free); 00762 } 00763 } 00764 00765 /* 00766 ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 00767 */ 00768 static void zeroblobFunc( 00769 sqlite3_context *context, 00770 int argc, 00771 sqlite3_value **argv 00772 ){ 00773 i64 n; 00774 assert( argc==1 ); 00775 n = sqlite3_value_int64(argv[0]); 00776 if( n>SQLITE_MAX_LENGTH ){ 00777 sqlite3_result_error_toobig(context); 00778 }else{ 00779 sqlite3_result_zeroblob(context, n); 00780 } 00781 } 00782 00783 /* 00784 ** The replace() function. Three arguments are all strings: call 00785 ** them A, B, and C. The result is also a string which is derived 00786 ** from A by replacing every occurance of B with C. The match 00787 ** must be exact. Collating sequences are not used. 00788 */ 00789 static void replaceFunc( 00790 sqlite3_context *context, 00791 int argc, 00792 sqlite3_value **argv 00793 ){ 00794 const unsigned char *zStr; /* The input string A */ 00795 const unsigned char *zPattern; /* The pattern string B */ 00796 const unsigned char *zRep; /* The replacement string C */ 00797 unsigned char *zOut; /* The output */ 00798 int nStr; /* Size of zStr */ 00799 int nPattern; /* Size of zPattern */ 00800 int nRep; /* Size of zRep */ 00801 i64 nOut; /* Maximum size of zOut */ 00802 int loopLimit; /* Last zStr[] that might match zPattern[] */ 00803 int i, j; /* Loop counters */ 00804 00805 assert( argc==3 ); 00806 zStr = sqlite3_value_text(argv[0]); 00807 if( zStr==0 ) return; 00808 nStr = sqlite3_value_bytes(argv[0]); 00809 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ 00810 zPattern = sqlite3_value_text(argv[1]); 00811 if( zPattern==0 || zPattern[0]==0 ) return; 00812 nPattern = sqlite3_value_bytes(argv[1]); 00813 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ 00814 zRep = sqlite3_value_text(argv[2]); 00815 if( zRep==0 ) return; 00816 nRep = sqlite3_value_bytes(argv[2]); 00817 assert( zRep==sqlite3_value_text(argv[2]) ); 00818 nOut = nStr + 1; 00819 assert( nOut<SQLITE_MAX_LENGTH ); 00820 zOut = contextMalloc(context, (i64)nOut); 00821 if( zOut==0 ){ 00822 return; 00823 } 00824 loopLimit = nStr - nPattern; 00825 for(i=j=0; i<=loopLimit; i++){ 00826 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 00827 zOut[j++] = zStr[i]; 00828 }else{ 00829 u8 *zOld; 00830 sqlite3 *db = sqlite3_context_db_handle(context); 00831 nOut += nRep - nPattern; 00832 if( nOut>=db->aLimit[SQLITE_LIMIT_LENGTH] ){ 00833 sqlite3_result_error_toobig(context); 00834 sqlite3DbFree(db, zOut); 00835 return; 00836 } 00837 zOld = zOut; 00838 zOut = sqlite3_realloc(zOut, (int)nOut); 00839 if( zOut==0 ){ 00840 sqlite3_result_error_nomem(context); 00841 sqlite3DbFree(db, zOld); 00842 return; 00843 } 00844 memcpy(&zOut[j], zRep, nRep); 00845 j += nRep; 00846 i += nPattern-1; 00847 } 00848 } 00849 assert( j+nStr-i+1==nOut ); 00850 memcpy(&zOut[j], &zStr[i], nStr-i); 00851 j += nStr - i; 00852 assert( j<=nOut ); 00853 zOut[j] = 0; 00854 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 00855 } 00856 00857 /* 00858 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 00859 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 00860 */ 00861 static void trimFunc( 00862 sqlite3_context *context, 00863 int argc, 00864 sqlite3_value **argv 00865 ){ 00866 const unsigned char *zIn; /* Input string */ 00867 const unsigned char *zCharSet; /* Set of characters to trim */ 00868 int nIn; /* Number of bytes in input */ 00869 int flags; /* 1: trimleft 2: trimright 3: trim */ 00870 int i; /* Loop counter */ 00871 unsigned char *aLen; /* Length of each character in zCharSet */ 00872 unsigned char **azChar; /* Individual characters in zCharSet */ 00873 int nChar; /* Number of characters in zCharSet */ 00874 00875 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 00876 return; 00877 } 00878 zIn = sqlite3_value_text(argv[0]); 00879 if( zIn==0 ) return; 00880 nIn = sqlite3_value_bytes(argv[0]); 00881 assert( zIn==sqlite3_value_text(argv[0]) ); 00882 if( argc==1 ){ 00883 static const unsigned char lenOne[] = { 1 }; 00884 static unsigned char * const azOne[] = { (u8*)" " }; 00885 nChar = 1; 00886 aLen = (u8*)lenOne; 00887 azChar = (unsigned char **)azOne; 00888 zCharSet = 0; 00889 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 00890 return; 00891 }else{ 00892 const unsigned char *z; 00893 for(z=zCharSet, nChar=0; *z; nChar++){ 00894 SQLITE_SKIP_UTF8(z); 00895 } 00896 if( nChar>0 ){ 00897 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); 00898 if( azChar==0 ){ 00899 return; 00900 } 00901 aLen = (unsigned char*)&azChar[nChar]; 00902 for(z=zCharSet, nChar=0; *z; nChar++){ 00903 azChar[nChar] = (unsigned char *)z; 00904 SQLITE_SKIP_UTF8(z); 00905 aLen[nChar] = z - azChar[nChar]; 00906 } 00907 } 00908 } 00909 if( nChar>0 ){ 00910 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); 00911 if( flags & 1 ){ 00912 while( nIn>0 ){ 00913 int len; 00914 for(i=0; i<nChar; i++){ 00915 len = aLen[i]; 00916 if( memcmp(zIn, azChar[i], len)==0 ) break; 00917 } 00918 if( i>=nChar ) break; 00919 zIn += len; 00920 nIn -= len; 00921 } 00922 } 00923 if( flags & 2 ){ 00924 while( nIn>0 ){ 00925 int len; 00926 for(i=0; i<nChar; i++){ 00927 len = aLen[i]; 00928 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 00929 } 00930 if( i>=nChar ) break; 00931 nIn -= len; 00932 } 00933 } 00934 if( zCharSet ){ 00935 sqlite3_free(azChar); 00936 } 00937 } 00938 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 00939 } 00940 00941 00942 #ifdef SQLITE_SOUNDEX 00943 /* 00944 ** Compute the soundex encoding of a word. 00945 */ 00946 static void soundexFunc( 00947 sqlite3_context *context, 00948 int argc, 00949 sqlite3_value **argv 00950 ){ 00951 char zResult[8]; 00952 const u8 *zIn; 00953 int i, j; 00954 static const unsigned char iCode[] = { 00955 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00956 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00957 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00958 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00959 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 00960 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 00961 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 00962 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 00963 }; 00964 assert( argc==1 ); 00965 zIn = (u8*)sqlite3_value_text(argv[0]); 00966 if( zIn==0 ) zIn = (u8*)""; 00967 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} 00968 if( zIn[i] ){ 00969 u8 prevcode = iCode[zIn[i]&0x7f]; 00970 zResult[0] = toupper(zIn[i]); 00971 for(j=1; j<4 && zIn[i]; i++){ 00972 int code = iCode[zIn[i]&0x7f]; 00973 if( code>0 ){ 00974 if( code!=prevcode ){ 00975 prevcode = code; 00976 zResult[j++] = code + '0'; 00977 } 00978 }else{ 00979 prevcode = 0; 00980 } 00981 } 00982 while( j<4 ){ 00983 zResult[j++] = '0'; 00984 } 00985 zResult[j] = 0; 00986 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 00987 }else{ 00988 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 00989 } 00990 } 00991 #endif 00992 00993 #ifndef SQLITE_OMIT_LOAD_EXTENSION 00994 /* 00995 ** A function that loads a shared-library extension then returns NULL. 00996 */ 00997 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 00998 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 00999 const char *zProc; 01000 sqlite3 *db = sqlite3_context_db_handle(context); 01001 char *zErrMsg = 0; 01002 01003 if( argc==2 ){ 01004 zProc = (const char *)sqlite3_value_text(argv[1]); 01005 }else{ 01006 zProc = 0; 01007 } 01008 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 01009 sqlite3_result_error(context, zErrMsg, -1); 01010 sqlite3_free(zErrMsg); 01011 } 01012 } 01013 #endif 01014 01015 01016 /* 01017 ** An instance of the following structure holds the context of a 01018 ** sum() or avg() aggregate computation. 01019 */ 01020 typedef struct SumCtx SumCtx; 01021 struct SumCtx { 01022 double rSum; /* Floating point sum */ 01023 i64 iSum; /* Integer sum */ 01024 i64 cnt; /* Number of elements summed */ 01025 u8 overflow; /* True if integer overflow seen */ 01026 u8 approx; /* True if non-integer value was input to the sum */ 01027 }; 01028 01029 /* 01030 ** Routines used to compute the sum, average, and total. 01031 ** 01032 ** The SUM() function follows the (broken) SQL standard which means 01033 ** that it returns NULL if it sums over no inputs. TOTAL returns 01034 ** 0.0 in that case. In addition, TOTAL always returns a float where 01035 ** SUM might return an integer if it never encounters a floating point 01036 ** value. TOTAL never fails, but SUM might through an exception if 01037 ** it overflows an integer. 01038 */ 01039 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 01040 SumCtx *p; 01041 int type; 01042 assert( argc==1 ); 01043 p = sqlite3_aggregate_context(context, sizeof(*p)); 01044 type = sqlite3_value_numeric_type(argv[0]); 01045 if( p && type!=SQLITE_NULL ){ 01046 p->cnt++; 01047 if( type==SQLITE_INTEGER ){ 01048 i64 v = sqlite3_value_int64(argv[0]); 01049 p->rSum += v; 01050 if( (p->approx|p->overflow)==0 ){ 01051 i64 iNewSum = p->iSum + v; 01052 int s1 = p->iSum >> (sizeof(i64)*8-1); 01053 int s2 = v >> (sizeof(i64)*8-1); 01054 int s3 = iNewSum >> (sizeof(i64)*8-1); 01055 p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); 01056 p->iSum = iNewSum; 01057 } 01058 }else{ 01059 p->rSum += sqlite3_value_double(argv[0]); 01060 p->approx = 1; 01061 } 01062 } 01063 } 01064 static void sumFinalize(sqlite3_context *context){ 01065 SumCtx *p; 01066 p = sqlite3_aggregate_context(context, 0); 01067 if( p && p->cnt>0 ){ 01068 if( p->overflow ){ 01069 sqlite3_result_error(context,"integer overflow",-1); 01070 }else if( p->approx ){ 01071 sqlite3_result_double(context, p->rSum); 01072 }else{ 01073 sqlite3_result_int64(context, p->iSum); 01074 } 01075 } 01076 } 01077 static void avgFinalize(sqlite3_context *context){ 01078 SumCtx *p; 01079 p = sqlite3_aggregate_context(context, 0); 01080 if( p && p->cnt>0 ){ 01081 sqlite3_result_double(context, p->rSum/(double)p->cnt); 01082 } 01083 } 01084 static void totalFinalize(sqlite3_context *context){ 01085 SumCtx *p; 01086 p = sqlite3_aggregate_context(context, 0); 01087 sqlite3_result_double(context, p ? p->rSum : 0.0); 01088 } 01089 01090 /* 01091 ** The following structure keeps track of state information for the 01092 ** count() aggregate function. 01093 */ 01094 typedef struct CountCtx CountCtx; 01095 struct CountCtx { 01096 i64 n; 01097 }; 01098 01099 /* 01100 ** Routines to implement the count() aggregate function. 01101 */ 01102 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 01103 CountCtx *p; 01104 p = sqlite3_aggregate_context(context, sizeof(*p)); 01105 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 01106 p->n++; 01107 } 01108 } 01109 static void countFinalize(sqlite3_context *context){ 01110 CountCtx *p; 01111 p = sqlite3_aggregate_context(context, 0); 01112 sqlite3_result_int64(context, p ? p->n : 0); 01113 } 01114 01115 /* 01116 ** Routines to implement min() and max() aggregate functions. 01117 */ 01118 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 01119 Mem *pArg = (Mem *)argv[0]; 01120 Mem *pBest; 01121 01122 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 01123 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 01124 if( !pBest ) return; 01125 01126 if( pBest->flags ){ 01127 int max; 01128 int cmp; 01129 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 01130 /* This step function is used for both the min() and max() aggregates, 01131 ** the only difference between the two being that the sense of the 01132 ** comparison is inverted. For the max() aggregate, the 01133 ** sqlite3_user_data() function returns (void *)-1. For min() it 01134 ** returns (void *)db, where db is the sqlite3* database pointer. 01135 ** Therefore the next statement sets variable 'max' to 1 for the max() 01136 ** aggregate, or 0 for min(). 01137 */ 01138 max = sqlite3_user_data(context)!=0; 01139 cmp = sqlite3MemCompare(pBest, pArg, pColl); 01140 if( (max && cmp<0) || (!max && cmp>0) ){ 01141 sqlite3VdbeMemCopy(pBest, pArg); 01142 } 01143 }else{ 01144 sqlite3VdbeMemCopy(pBest, pArg); 01145 } 01146 } 01147 static void minMaxFinalize(sqlite3_context *context){ 01148 sqlite3_value *pRes; 01149 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 01150 if( pRes ){ 01151 if( pRes->flags ){ 01152 sqlite3_result_value(context, pRes); 01153 } 01154 sqlite3VdbeMemRelease(pRes); 01155 } 01156 } 01157 01158 /* 01159 ** group_concat(EXPR, ?SEPARATOR?) 01160 */ 01161 static void groupConcatStep( 01162 sqlite3_context *context, 01163 int argc, 01164 sqlite3_value **argv 01165 ){ 01166 const char *zVal; 01167 StrAccum *pAccum; 01168 const char *zSep; 01169 int nVal, nSep, i; 01170 if( argc==0 || sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 01171 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); 01172 01173 if( pAccum ){ 01174 sqlite3 *db = sqlite3_context_db_handle(context); 01175 pAccum->useMalloc = 1; 01176 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; 01177 if( pAccum->nChar ){ 01178 if( argc>1 ){ 01179 zSep = (char*)sqlite3_value_text(argv[argc-1]); 01180 nSep = sqlite3_value_bytes(argv[argc-1]); 01181 }else{ 01182 zSep = ","; 01183 nSep = 1; 01184 } 01185 sqlite3StrAccumAppend(pAccum, zSep, nSep); 01186 } 01187 i = 0; 01188 do{ 01189 zVal = (char*)sqlite3_value_text(argv[i]); 01190 nVal = sqlite3_value_bytes(argv[i]); 01191 sqlite3StrAccumAppend(pAccum, zVal, nVal); 01192 i++; 01193 }while( i<argc-1 ); 01194 } 01195 } 01196 static void groupConcatFinalize(sqlite3_context *context){ 01197 StrAccum *pAccum; 01198 pAccum = sqlite3_aggregate_context(context, 0); 01199 if( pAccum ){ 01200 if( pAccum->tooBig ){ 01201 sqlite3_result_error_toobig(context); 01202 }else if( pAccum->mallocFailed ){ 01203 sqlite3_result_error_nomem(context); 01204 }else{ 01205 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 01206 sqlite3_free); 01207 } 01208 } 01209 } 01210 01211 /* 01212 ** This function registered all of the above C functions as SQL 01213 ** functions. This should be the only routine in this file with 01214 ** external linkage. 01215 */ 01216 void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ 01217 #ifndef SQLITE_OMIT_ALTERTABLE 01218 sqlite3AlterFunctions(db); 01219 #endif 01220 if( !db->mallocFailed ){ 01221 int rc = sqlite3_overload_function(db, "MATCH", 2); 01222 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); 01223 if( rc==SQLITE_NOMEM ){ 01224 db->mallocFailed = 1; 01225 } 01226 } 01227 #ifdef SQLITE_SSE 01228 (void)sqlite3SseFunctions(db); 01229 #endif 01230 } 01231 01232 /* 01233 ** Set the LIKEOPT flag on the 2-argument function with the given name. 01234 */ 01235 static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ 01236 FuncDef *pDef; 01237 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); 01238 if( pDef ){ 01239 pDef->flags = flagVal; 01240 } 01241 } 01242 01243 /* 01244 ** Register the built-in LIKE and GLOB functions. The caseSensitive 01245 ** parameter determines whether or not the LIKE operator is case 01246 ** sensitive. GLOB is always case sensitive. 01247 */ 01248 void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 01249 struct compareInfo *pInfo; 01250 if( caseSensitive ){ 01251 pInfo = (struct compareInfo*)&likeInfoAlt; 01252 }else{ 01253 pInfo = (struct compareInfo*)&likeInfoNorm; 01254 } 01255 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 01256 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 01257 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 01258 (struct compareInfo*)&globInfo, likeFunc, 0,0); 01259 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 01260 setLikeOptFlag(db, "like", 01261 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 01262 } 01263 01264 /* 01265 ** pExpr points to an expression which implements a function. If 01266 ** it is appropriate to apply the LIKE optimization to that function 01267 ** then set aWc[0] through aWc[2] to the wildcard characters and 01268 ** return TRUE. If the function is not a LIKE-style function then 01269 ** return FALSE. 01270 */ 01271 int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 01272 FuncDef *pDef; 01273 if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){ 01274 return 0; 01275 } 01276 if( pExpr->pList->nExpr!=2 ){ 01277 return 0; 01278 } 01279 pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, 01280 SQLITE_UTF8, 0); 01281 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ 01282 return 0; 01283 } 01284 01285 /* The memcpy() statement assumes that the wildcard characters are 01286 ** the first three statements in the compareInfo structure. The 01287 ** asserts() that follow verify that assumption 01288 */ 01289 memcpy(aWc, pDef->pUserData, 3); 01290 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 01291 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 01292 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 01293 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; 01294 return 1; 01295 } 01296 01297 /* 01298 ** All all of the FuncDef structures in the aBuiltinFunc[] array above 01299 ** to the global function hash table. This occurs at start-time (as 01300 ** a consequence of calling sqlite3_initialize()). 01301 ** 01302 ** After this routine runs 01303 */ 01304 void sqlite3RegisterGlobalFunctions(void){ 01305 /* 01306 ** The following array holds FuncDef structures for all of the functions 01307 ** defined in this file. 01308 ** 01309 ** The array cannot be constant since changes are made to the 01310 ** FuncDef.pHash elements at start-time. The elements of this array 01311 ** are read-only after initialization is complete. 01312 */ 01313 static SQLITE_WSD FuncDef aBuiltinFunc[] = { 01314 FUNCTION(ltrim, 1, 1, 0, trimFunc ), 01315 FUNCTION(ltrim, 2, 1, 0, trimFunc ), 01316 FUNCTION(rtrim, 1, 2, 0, trimFunc ), 01317 FUNCTION(rtrim, 2, 2, 0, trimFunc ), 01318 FUNCTION(trim, 1, 3, 0, trimFunc ), 01319 FUNCTION(trim, 2, 3, 0, trimFunc ), 01320 FUNCTION(min, -1, 0, 1, minmaxFunc ), 01321 FUNCTION(min, 0, 0, 1, 0 ), 01322 AGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize ), 01323 FUNCTION(max, -1, 1, 1, minmaxFunc ), 01324 FUNCTION(max, 0, 1, 1, 0 ), 01325 AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ), 01326 FUNCTION(typeof, 1, 0, 0, typeofFunc ), 01327 FUNCTION(length, 1, 0, 0, lengthFunc ), 01328 FUNCTION(substr, 2, 0, 0, substrFunc ), 01329 FUNCTION(substr, 3, 0, 0, substrFunc ), 01330 FUNCTION(abs, 1, 0, 0, absFunc ), 01331 FUNCTION(round, 1, 0, 0, roundFunc ), 01332 FUNCTION(round, 2, 0, 0, roundFunc ), 01333 FUNCTION(upper, 1, 0, 0, upperFunc ), 01334 FUNCTION(lower, 1, 0, 0, lowerFunc ), 01335 FUNCTION(coalesce, 1, 0, 0, 0 ), 01336 FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), 01337 FUNCTION(coalesce, 0, 0, 0, 0 ), 01338 FUNCTION(hex, 1, 0, 0, hexFunc ), 01339 FUNCTION(ifnull, 2, 0, 1, ifnullFunc ), 01340 FUNCTION(random, -1, 0, 0, randomFunc ), 01341 FUNCTION(randomblob, 1, 0, 0, randomBlob ), 01342 FUNCTION(nullif, 2, 0, 1, nullifFunc ), 01343 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), 01344 FUNCTION(quote, 1, 0, 0, quoteFunc ), 01345 FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), 01346 FUNCTION(changes, 0, 0, 0, changes ), 01347 FUNCTION(total_changes, 0, 0, 0, total_changes ), 01348 FUNCTION(replace, 3, 0, 0, replaceFunc ), 01349 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), 01350 #ifdef SQLITE_SOUNDEX 01351 FUNCTION(soundex, 1, 0, 0, soundexFunc ), 01352 #endif 01353 #ifndef SQLITE_OMIT_LOAD_EXTENSION 01354 FUNCTION(load_extension, 1, 0, 0, loadExt ), 01355 FUNCTION(load_extension, 2, 0, 0, loadExt ), 01356 #endif 01357 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), 01358 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), 01359 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), 01360 AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), 01361 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), 01362 AGGREGATE(group_concat, -1, 0, 0, groupConcatStep, groupConcatFinalize), 01363 01364 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 01365 #ifdef SQLITE_CASE_SENSITIVE_LIKE 01366 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 01367 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), 01368 #else 01369 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), 01370 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), 01371 #endif 01372 }; 01373 01374 int i; 01375 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); 01376 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc); 01377 01378 for(i=0; i<ArraySize(aBuiltinFunc); i++){ 01379 sqlite3FuncDefInsert(pHash, &aFunc[i]); 01380 } 01381 sqlite3RegisterDateTimeFunctions(); 01382 }
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:53 2011 by Doxygen 1.6.1