| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2002 February 23 | 2 ** 2002 February 23 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains the C functions that implement various SQL | 12 ** This file contains the C-language implementations for many of the SQL |
| 13 ** functions of SQLite. | 13 ** functions of SQLite. (Some function, and in particular the date and |
| 14 ** | 14 ** time functions, are implemented separately.) |
| 15 ** There is only one exported symbol in this file - the function | |
| 16 ** sqliteRegisterBuildinFunctions() found at the bottom of the file. | |
| 17 ** All other code has file scope. | |
| 18 */ | 15 */ |
| 19 #include "sqliteInt.h" | 16 #include "sqliteInt.h" |
| 20 #include <stdlib.h> | 17 #include <stdlib.h> |
| 21 #include <assert.h> | 18 #include <assert.h> |
| 22 #include "vdbeInt.h" | 19 #include "vdbeInt.h" |
| 23 | 20 |
| 24 /* | 21 /* |
| 25 ** Return the collating function associated with a function. | 22 ** Return the collating function associated with a function. |
| 26 */ | 23 */ |
| 27 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ | 24 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 28 return context->pColl; | 25 VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 26 assert( pOp->opcode==OP_CollSeq ); |
| 27 assert( pOp->p4type==P4_COLLSEQ ); |
| 28 return pOp->p4.pColl; |
| 29 } | 29 } |
| 30 | 30 |
| 31 /* | 31 /* |
| 32 ** Indicate that the accumulator load should be skipped on this |
| 33 ** iteration of the aggregate loop. |
| 34 */ |
| 35 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ |
| 36 context->skipFlag = 1; |
| 37 } |
| 38 |
| 39 /* |
| 32 ** Implementation of the non-aggregate min() and max() functions | 40 ** Implementation of the non-aggregate min() and max() functions |
| 33 */ | 41 */ |
| 34 static void minmaxFunc( | 42 static void minmaxFunc( |
| 35 sqlite3_context *context, | 43 sqlite3_context *context, |
| 36 int argc, | 44 int argc, |
| 37 sqlite3_value **argv | 45 sqlite3_value **argv |
| 38 ){ | 46 ){ |
| 39 int i; | 47 int i; |
| 40 int mask; /* 0 for min() or 0xffffffff for max() */ | 48 int mask; /* 0 for min() or 0xffffffff for max() */ |
| 41 int iBest; | 49 int iBest; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of | 130 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of |
| 123 ** the numeric argument X. | 131 ** the numeric argument X. |
| 124 */ | 132 */ |
| 125 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ | 133 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 126 assert( argc==1 ); | 134 assert( argc==1 ); |
| 127 UNUSED_PARAMETER(argc); | 135 UNUSED_PARAMETER(argc); |
| 128 switch( sqlite3_value_type(argv[0]) ){ | 136 switch( sqlite3_value_type(argv[0]) ){ |
| 129 case SQLITE_INTEGER: { | 137 case SQLITE_INTEGER: { |
| 130 i64 iVal = sqlite3_value_int64(argv[0]); | 138 i64 iVal = sqlite3_value_int64(argv[0]); |
| 131 if( iVal<0 ){ | 139 if( iVal<0 ){ |
| 132 if( (iVal<<1)==0 ){ | 140 if( iVal==SMALLEST_INT64 ){ |
| 133 /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then | 141 /* IMP: R-31676-45509 If X is the integer -9223372036854775808 |
| 134 ** abs(X) throws an integer overflow error since there is no | 142 ** then abs(X) throws an integer overflow error since there is no |
| 135 ** equivalent positive 64-bit two complement value. */ | 143 ** equivalent positive 64-bit two complement value. */ |
| 136 sqlite3_result_error(context, "integer overflow", -1); | 144 sqlite3_result_error(context, "integer overflow", -1); |
| 137 return; | 145 return; |
| 138 } | 146 } |
| 139 iVal = -iVal; | 147 iVal = -iVal; |
| 140 } | 148 } |
| 141 sqlite3_result_int64(context, iVal); | 149 sqlite3_result_int64(context, iVal); |
| 142 break; | 150 break; |
| 143 } | 151 } |
| 144 case SQLITE_NULL: { | 152 case SQLITE_NULL: { |
| 145 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ | 153 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ |
| 146 sqlite3_result_null(context); | 154 sqlite3_result_null(context); |
| 147 break; | 155 break; |
| 148 } | 156 } |
| 149 default: { | 157 default: { |
| 150 /* Because sqlite3_value_double() returns 0.0 if the argument is not | 158 /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 151 ** something that can be converted into a number, we have: | 159 ** something that can be converted into a number, we have: |
| 152 ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that | 160 ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that |
| 153 ** cannot be converted to a numeric value. | 161 ** cannot be converted to a numeric value. |
| 154 */ | 162 */ |
| 155 double rVal = sqlite3_value_double(argv[0]); | 163 double rVal = sqlite3_value_double(argv[0]); |
| 156 if( rVal<0 ) rVal = -rVal; | 164 if( rVal<0 ) rVal = -rVal; |
| 157 sqlite3_result_double(context, rVal); | 165 sqlite3_result_double(context, rVal); |
| 158 break; | 166 break; |
| 159 } | 167 } |
| 160 } | 168 } |
| 161 } | 169 } |
| 162 | 170 |
| 163 /* | 171 /* |
| 172 ** Implementation of the instr() function. |
| 173 ** |
| 174 ** instr(haystack,needle) finds the first occurrence of needle |
| 175 ** in haystack and returns the number of previous characters plus 1, |
| 176 ** or 0 if needle does not occur within haystack. |
| 177 ** |
| 178 ** If both haystack and needle are BLOBs, then the result is one more than |
| 179 ** the number of bytes in haystack prior to the first occurrence of needle, |
| 180 ** or 0 if needle never occurs in haystack. |
| 181 */ |
| 182 static void instrFunc( |
| 183 sqlite3_context *context, |
| 184 int argc, |
| 185 sqlite3_value **argv |
| 186 ){ |
| 187 const unsigned char *zHaystack; |
| 188 const unsigned char *zNeedle; |
| 189 int nHaystack; |
| 190 int nNeedle; |
| 191 int typeHaystack, typeNeedle; |
| 192 int N = 1; |
| 193 int isText; |
| 194 |
| 195 UNUSED_PARAMETER(argc); |
| 196 typeHaystack = sqlite3_value_type(argv[0]); |
| 197 typeNeedle = sqlite3_value_type(argv[1]); |
| 198 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; |
| 199 nHaystack = sqlite3_value_bytes(argv[0]); |
| 200 nNeedle = sqlite3_value_bytes(argv[1]); |
| 201 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ |
| 202 zHaystack = sqlite3_value_blob(argv[0]); |
| 203 zNeedle = sqlite3_value_blob(argv[1]); |
| 204 isText = 0; |
| 205 }else{ |
| 206 zHaystack = sqlite3_value_text(argv[0]); |
| 207 zNeedle = sqlite3_value_text(argv[1]); |
| 208 isText = 1; |
| 209 } |
| 210 while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){ |
| 211 N++; |
| 212 do{ |
| 213 nHaystack--; |
| 214 zHaystack++; |
| 215 }while( isText && (zHaystack[0]&0xc0)==0x80 ); |
| 216 } |
| 217 if( nNeedle>nHaystack ) N = 0; |
| 218 sqlite3_result_int(context, N); |
| 219 } |
| 220 |
| 221 /* |
| 222 ** Implementation of the printf() function. |
| 223 */ |
| 224 static void printfFunc( |
| 225 sqlite3_context *context, |
| 226 int argc, |
| 227 sqlite3_value **argv |
| 228 ){ |
| 229 PrintfArguments x; |
| 230 StrAccum str; |
| 231 const char *zFormat; |
| 232 int n; |
| 233 |
| 234 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ |
| 235 x.nArg = argc-1; |
| 236 x.nUsed = 0; |
| 237 x.apArg = argv+1; |
| 238 sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH); |
| 239 str.db = sqlite3_context_db_handle(context); |
| 240 sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x); |
| 241 n = str.nChar; |
| 242 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, |
| 243 SQLITE_DYNAMIC); |
| 244 } |
| 245 } |
| 246 |
| 247 /* |
| 164 ** Implementation of the substr() function. | 248 ** Implementation of the substr() function. |
| 165 ** | 249 ** |
| 166 ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. | 250 ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. |
| 167 ** p1 is 1-indexed. So substr(x,1,1) returns the first character | 251 ** p1 is 1-indexed. So substr(x,1,1) returns the first character |
| 168 ** of x. If x is text, then we actually count UTF-8 characters. | 252 ** of x. If x is text, then we actually count UTF-8 characters. |
| 169 ** If x is a blob, then we count bytes. | 253 ** If x is a blob, then we count bytes. |
| 170 ** | 254 ** |
| 171 ** If p1 is negative, then we begin abs(p1) from the end of x[]. | 255 ** If p1 is negative, then we begin abs(p1) from the end of x[]. |
| 172 ** | 256 ** |
| 173 ** If p2 is negative, return the p2 characters preceeding p1. | 257 ** If p2 is negative, return the p2 characters preceding p1. |
| 174 */ | 258 */ |
| 175 static void substrFunc( | 259 static void substrFunc( |
| 176 sqlite3_context *context, | 260 sqlite3_context *context, |
| 177 int argc, | 261 int argc, |
| 178 sqlite3_value **argv | 262 sqlite3_value **argv |
| 179 ){ | 263 ){ |
| 180 const unsigned char *z; | 264 const unsigned char *z; |
| 181 const unsigned char *z2; | 265 const unsigned char *z2; |
| 182 int len; | 266 int len; |
| 183 int p0type; | 267 int p0type; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 } | 321 } |
| 238 assert( p1>=0 && p2>=0 ); | 322 assert( p1>=0 && p2>=0 ); |
| 239 if( p0type!=SQLITE_BLOB ){ | 323 if( p0type!=SQLITE_BLOB ){ |
| 240 while( *z && p1 ){ | 324 while( *z && p1 ){ |
| 241 SQLITE_SKIP_UTF8(z); | 325 SQLITE_SKIP_UTF8(z); |
| 242 p1--; | 326 p1--; |
| 243 } | 327 } |
| 244 for(z2=z; *z2 && p2; p2--){ | 328 for(z2=z; *z2 && p2; p2--){ |
| 245 SQLITE_SKIP_UTF8(z2); | 329 SQLITE_SKIP_UTF8(z2); |
| 246 } | 330 } |
| 247 sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT); | 331 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, |
| 332 SQLITE_UTF8); |
| 248 }else{ | 333 }else{ |
| 249 if( p1+p2>len ){ | 334 if( p1+p2>len ){ |
| 250 p2 = len-p1; | 335 p2 = len-p1; |
| 251 if( p2<0 ) p2 = 0; | 336 if( p2<0 ) p2 = 0; |
| 252 } | 337 } |
| 253 sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT); | 338 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); |
| 254 } | 339 } |
| 255 } | 340 } |
| 256 | 341 |
| 257 /* | 342 /* |
| 258 ** Implementation of the round() function | 343 ** Implementation of the round() function |
| 259 */ | 344 */ |
| 260 #ifndef SQLITE_OMIT_FLOATING_POINT | 345 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 261 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ | 346 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 262 int n = 0; | 347 int n = 0; |
| 263 double r; | 348 double r; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 static void *contextMalloc(sqlite3_context *context, i64 nByte){ | 387 static void *contextMalloc(sqlite3_context *context, i64 nByte){ |
| 303 char *z; | 388 char *z; |
| 304 sqlite3 *db = sqlite3_context_db_handle(context); | 389 sqlite3 *db = sqlite3_context_db_handle(context); |
| 305 assert( nByte>0 ); | 390 assert( nByte>0 ); |
| 306 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); | 391 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 307 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); | 392 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); |
| 308 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | 393 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 309 sqlite3_result_error_toobig(context); | 394 sqlite3_result_error_toobig(context); |
| 310 z = 0; | 395 z = 0; |
| 311 }else{ | 396 }else{ |
| 312 z = sqlite3Malloc((int)nByte); | 397 z = sqlite3Malloc(nByte); |
| 313 if( !z ){ | 398 if( !z ){ |
| 314 sqlite3_result_error_nomem(context); | 399 sqlite3_result_error_nomem(context); |
| 315 } | 400 } |
| 316 } | 401 } |
| 317 return z; | 402 return z; |
| 318 } | 403 } |
| 319 | 404 |
| 320 /* | 405 /* |
| 321 ** Implementation of the upper() and lower() SQL functions. | 406 ** Implementation of the upper() and lower() SQL functions. |
| 322 */ | 407 */ |
| 323 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ | 408 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 324 char *z1; | 409 char *z1; |
| 325 const char *z2; | 410 const char *z2; |
| 326 int i, n; | 411 int i, n; |
| 327 UNUSED_PARAMETER(argc); | 412 UNUSED_PARAMETER(argc); |
| 328 z2 = (char*)sqlite3_value_text(argv[0]); | 413 z2 = (char*)sqlite3_value_text(argv[0]); |
| 329 n = sqlite3_value_bytes(argv[0]); | 414 n = sqlite3_value_bytes(argv[0]); |
| 330 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ | 415 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 331 assert( z2==(char*)sqlite3_value_text(argv[0]) ); | 416 assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
| 332 if( z2 ){ | 417 if( z2 ){ |
| 333 z1 = contextMalloc(context, ((i64)n)+1); | 418 z1 = contextMalloc(context, ((i64)n)+1); |
| 334 if( z1 ){ | 419 if( z1 ){ |
| 335 memcpy(z1, z2, n+1); | 420 for(i=0; i<n; i++){ |
| 336 for(i=0; z1[i]; i++){ | 421 z1[i] = (char)sqlite3Toupper(z2[i]); |
| 337 z1[i] = (char)sqlite3Toupper(z1[i]); | |
| 338 } | 422 } |
| 339 sqlite3_result_text(context, z1, -1, sqlite3_free); | 423 sqlite3_result_text(context, z1, n, sqlite3_free); |
| 340 } | 424 } |
| 341 } | 425 } |
| 342 } | 426 } |
| 343 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ | 427 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 344 u8 *z1; | 428 char *z1; |
| 345 const char *z2; | 429 const char *z2; |
| 346 int i, n; | 430 int i, n; |
| 347 UNUSED_PARAMETER(argc); | 431 UNUSED_PARAMETER(argc); |
| 348 z2 = (char*)sqlite3_value_text(argv[0]); | 432 z2 = (char*)sqlite3_value_text(argv[0]); |
| 349 n = sqlite3_value_bytes(argv[0]); | 433 n = sqlite3_value_bytes(argv[0]); |
| 350 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ | 434 /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 351 assert( z2==(char*)sqlite3_value_text(argv[0]) ); | 435 assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
| 352 if( z2 ){ | 436 if( z2 ){ |
| 353 z1 = contextMalloc(context, ((i64)n)+1); | 437 z1 = contextMalloc(context, ((i64)n)+1); |
| 354 if( z1 ){ | 438 if( z1 ){ |
| 355 memcpy(z1, z2, n+1); | 439 for(i=0; i<n; i++){ |
| 356 for(i=0; z1[i]; i++){ | 440 z1[i] = sqlite3Tolower(z2[i]); |
| 357 z1[i] = sqlite3Tolower(z1[i]); | |
| 358 } | 441 } |
| 359 sqlite3_result_text(context, (char *)z1, -1, sqlite3_free); | 442 sqlite3_result_text(context, z1, n, sqlite3_free); |
| 360 } | 443 } |
| 361 } | 444 } |
| 362 } | 445 } |
| 363 | 446 |
| 364 | |
| 365 #if 0 /* This function is never used. */ | |
| 366 /* | 447 /* |
| 367 ** The COALESCE() and IFNULL() functions used to be implemented as shown | 448 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented |
| 368 ** here. But now they are implemented as VDBE code so that unused arguments | 449 ** as VDBE code so that unused argument values do not have to be computed. |
| 369 ** do not have to be computed. This legacy implementation is retained as | 450 ** However, we still need some kind of function implementation for this |
| 370 ** comment. | 451 ** routines in the function table. The noopFunc macro provides this. |
| 452 ** noopFunc will never be called so it doesn't matter what the implementation |
| 453 ** is. We might as well use the "version()" function as a substitute. |
| 371 */ | 454 */ |
| 372 /* | 455 #define noopFunc versionFunc /* Substitute function - never called */ |
| 373 ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. | |
| 374 ** All three do the same thing. They return the first non-NULL | |
| 375 ** argument. | |
| 376 */ | |
| 377 static void ifnullFunc( | |
| 378 sqlite3_context *context, | |
| 379 int argc, | |
| 380 sqlite3_value **argv | |
| 381 ){ | |
| 382 int i; | |
| 383 for(i=0; i<argc; i++){ | |
| 384 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ | |
| 385 sqlite3_result_value(context, argv[i]); | |
| 386 break; | |
| 387 } | |
| 388 } | |
| 389 } | |
| 390 #endif /* NOT USED */ | |
| 391 #define ifnullFunc versionFunc /* Substitute function - never called */ | |
| 392 | 456 |
| 393 /* | 457 /* |
| 394 ** Implementation of random(). Return a random integer. | 458 ** Implementation of random(). Return a random integer. |
| 395 */ | 459 */ |
| 396 static void randomFunc( | 460 static void randomFunc( |
| 397 sqlite3_context *context, | 461 sqlite3_context *context, |
| 398 int NotUsed, | 462 int NotUsed, |
| 399 sqlite3_value **NotUsed2 | 463 sqlite3_value **NotUsed2 |
| 400 ){ | 464 ){ |
| 401 sqlite_int64 r; | 465 sqlite_int64 r; |
| 402 UNUSED_PARAMETER2(NotUsed, NotUsed2); | 466 UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 403 sqlite3_randomness(sizeof(r), &r); | 467 sqlite3_randomness(sizeof(r), &r); |
| 404 if( r<0 ){ | 468 if( r<0 ){ |
| 405 /* We need to prevent a random number of 0x8000000000000000 | 469 /* We need to prevent a random number of 0x8000000000000000 |
| 406 ** (or -9223372036854775808) since when you do abs() of that | 470 ** (or -9223372036854775808) since when you do abs() of that |
| 407 ** number of you get the same value back again. To do this | 471 ** number of you get the same value back again. To do this |
| 408 ** in a way that is testable, mask the sign bit off of negative | 472 ** in a way that is testable, mask the sign bit off of negative |
| 409 ** values, resulting in a positive value. Then take the | 473 ** values, resulting in a positive value. Then take the |
| 410 ** 2s complement of that positive value. The end result can | 474 ** 2s complement of that positive value. The end result can |
| 411 ** therefore be no less than -9223372036854775807. | 475 ** therefore be no less than -9223372036854775807. |
| 412 */ | 476 */ |
| 413 r = -(r ^ (((sqlite3_int64)1)<<63)); | 477 r = -(r & LARGEST_INT64); |
| 414 } | 478 } |
| 415 sqlite3_result_int64(context, r); | 479 sqlite3_result_int64(context, r); |
| 416 } | 480 } |
| 417 | 481 |
| 418 /* | 482 /* |
| 419 ** Implementation of randomblob(N). Return a random blob | 483 ** Implementation of randomblob(N). Return a random blob |
| 420 ** that is N bytes long. | 484 ** that is N bytes long. |
| 421 */ | 485 */ |
| 422 static void randomBlob( | 486 static void randomBlob( |
| 423 sqlite3_context *context, | 487 sqlite3_context *context, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 u8 noCase; | 563 u8 noCase; |
| 500 }; | 564 }; |
| 501 | 565 |
| 502 /* | 566 /* |
| 503 ** For LIKE and GLOB matching on EBCDIC machines, assume that every | 567 ** For LIKE and GLOB matching on EBCDIC machines, assume that every |
| 504 ** character is exactly one byte in size. Also, all characters are | 568 ** character is exactly one byte in size. Also, all characters are |
| 505 ** able to participate in upper-case-to-lower-case mappings in EBCDIC | 569 ** able to participate in upper-case-to-lower-case mappings in EBCDIC |
| 506 ** whereas only characters less than 0x80 do in ASCII. | 570 ** whereas only characters less than 0x80 do in ASCII. |
| 507 */ | 571 */ |
| 508 #if defined(SQLITE_EBCDIC) | 572 #if defined(SQLITE_EBCDIC) |
| 509 # define sqlite3Utf8Read(A,C) (*(A++)) | 573 # define sqlite3Utf8Read(A) (*((*A)++)) |
| 510 # define GlogUpperToLower(A) A = sqlite3UpperToLower[A] | 574 # define GlobUpperToLower(A) A = sqlite3UpperToLower[A] |
| 575 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 511 #else | 576 #else |
| 512 # define GlogUpperToLower(A) if( A<0x80 ){ A = sqlite3UpperToLower[A]; } | 577 # define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; } |
| 578 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 513 #endif | 579 #endif |
| 514 | 580 |
| 515 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; | 581 static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 516 /* The correct SQL-92 behavior is for the LIKE operator to ignore | 582 /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 517 ** case. Thus 'a' LIKE 'A' would be true. */ | 583 ** case. Thus 'a' LIKE 'A' would be true. */ |
| 518 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; | 584 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; |
| 519 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator | 585 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator |
| 520 ** is case sensitive causing 'a' LIKE 'A' to be false */ | 586 ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 521 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; | 587 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
| 522 | 588 |
| 523 /* | 589 /* |
| 524 ** Compare two UTF-8 strings for equality where the first string can | 590 ** Compare two UTF-8 strings for equality where the first string can |
| 525 ** potentially be a "glob" expression. Return true (1) if they | 591 ** potentially be a "glob" or "like" expression. Return true (1) if they |
| 526 ** are the same and false (0) if they are different. | 592 ** are the same and false (0) if they are different. |
| 527 ** | 593 ** |
| 528 ** Globbing rules: | 594 ** Globbing rules: |
| 529 ** | 595 ** |
| 530 ** '*' Matches any sequence of zero or more characters. | 596 ** '*' Matches any sequence of zero or more characters. |
| 531 ** | 597 ** |
| 532 ** '?' Matches exactly one character. | 598 ** '?' Matches exactly one character. |
| 533 ** | 599 ** |
| 534 ** [...] Matches one character from the enclosed list of | 600 ** [...] Matches one character from the enclosed list of |
| 535 ** characters. | 601 ** characters. |
| 536 ** | 602 ** |
| 537 ** [^...] Matches one character not in the enclosed list. | 603 ** [^...] Matches one character not in the enclosed list. |
| 538 ** | 604 ** |
| 539 ** With the [...] and [^...] matching, a ']' character can be included | 605 ** With the [...] and [^...] matching, a ']' character can be included |
| 540 ** in the list by making it the first character after '[' or '^'. A | 606 ** in the list by making it the first character after '[' or '^'. A |
| 541 ** range of characters can be specified using '-'. Example: | 607 ** range of characters can be specified using '-'. Example: |
| 542 ** "[a-z]" matches any single lower-case letter. To match a '-', make | 608 ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 543 ** it the last character in the list. | 609 ** it the last character in the list. |
| 544 ** | 610 ** |
| 611 ** Like matching rules: |
| 612 ** |
| 613 ** '%' Matches any sequence of zero or more characters |
| 614 ** |
| 615 *** '_' Matches any one character |
| 616 ** |
| 617 ** Ec Where E is the "esc" character and c is any other |
| 618 ** character, including '%', '_', and esc, match exactly c. |
| 619 ** |
| 620 ** The comments through this routine usually assume glob matching. |
| 621 ** |
| 545 ** This routine is usually quick, but can be N**2 in the worst case. | 622 ** This routine is usually quick, but can be N**2 in the worst case. |
| 546 ** | |
| 547 ** Hints: to match '*' or '?', put them in "[]". Like this: | |
| 548 ** | |
| 549 ** abc[*]xyz Matches "abc*xyz" only | |
| 550 */ | 623 */ |
| 551 static int patternCompare( | 624 static int patternCompare( |
| 552 const u8 *zPattern, /* The glob pattern */ | 625 const u8 *zPattern, /* The glob pattern */ |
| 553 const u8 *zString, /* The string to compare against the glob */ | 626 const u8 *zString, /* The string to compare against the glob */ |
| 554 const struct compareInfo *pInfo, /* Information about how to do the compare */ | 627 const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 555 const int esc /* The escape character */ | 628 u32 esc /* The escape character */ |
| 556 ){ | 629 ){ |
| 557 int c, c2; | 630 u32 c, c2; /* Next pattern and input string chars */ |
| 558 int invert; | 631 u32 matchOne = pInfo->matchOne; /* "?" or "_" */ |
| 559 int seen; | 632 u32 matchAll = pInfo->matchAll; /* "*" or "%" */ |
| 560 u8 matchOne = pInfo->matchOne; | 633 u32 matchOther; /* "[" or the escape character */ |
| 561 u8 matchAll = pInfo->matchAll; | 634 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ |
| 562 u8 matchSet = pInfo->matchSet; | 635 const u8 *zEscaped = 0; /* One past the last escaped input char */ |
| 563 u8 noCase = pInfo->noCase; | 636 |
| 564 int prevEscape = 0; /* True if the previous character was 'escape' */ | 637 /* The GLOB operator does not have an ESCAPE clause. And LIKE does not |
| 638 ** have the matchSet operator. So we either have to look for one or |
| 639 ** the other, never both. Hence the single variable matchOther is used |
| 640 ** to store the one we have to look for. |
| 641 */ |
| 642 matchOther = esc ? esc : pInfo->matchSet; |
| 565 | 643 |
| 566 while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){ | 644 while( (c = sqlite3Utf8Read(&zPattern))!=0 ){ |
| 567 if( !prevEscape && c==matchAll ){ | 645 if( c==matchAll ){ /* Match "*" */ |
| 568 while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll | 646 /* Skip over multiple "*" characters in the pattern. If there |
| 647 ** are also "?" characters, skip those as well, but consume a |
| 648 ** single character of the input string for each "?" skipped */ |
| 649 while( (c=sqlite3Utf8Read(&zPattern)) == matchAll |
| 569 || c == matchOne ){ | 650 || c == matchOne ){ |
| 570 if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){ | 651 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ |
| 571 return 0; | 652 return 0; |
| 572 } | 653 } |
| 573 } | 654 } |
| 574 if( c==0 ){ | 655 if( c==0 ){ |
| 575 return 1; | 656 return 1; /* "*" at the end of the pattern matches */ |
| 576 }else if( c==esc ){ | 657 }else if( c==matchOther ){ |
| 577 c = sqlite3Utf8Read(zPattern, &zPattern); | 658 if( esc ){ |
| 578 if( c==0 ){ | 659 c = sqlite3Utf8Read(&zPattern); |
| 660 if( c==0 ) return 0; |
| 661 }else{ |
| 662 /* "[...]" immediately follows the "*". We have to do a slow |
| 663 ** recursive search in this case, but it is an unusual case. */ |
| 664 assert( matchOther<0x80 ); /* '[' is a single-byte character */ |
| 665 while( *zString |
| 666 && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ |
| 667 SQLITE_SKIP_UTF8(zString); |
| 668 } |
| 669 return *zString!=0; |
| 670 } |
| 671 } |
| 672 |
| 673 /* At this point variable c contains the first character of the |
| 674 ** pattern string past the "*". Search in the input string for the |
| 675 ** first matching character and recursively contine the match from |
| 676 ** that point. |
| 677 ** |
| 678 ** For a case-insensitive search, set variable cx to be the same as |
| 679 ** c but in the other case and search the input string for either |
| 680 ** c or cx. |
| 681 */ |
| 682 if( c<=0x80 ){ |
| 683 u32 cx; |
| 684 if( noCase ){ |
| 685 cx = sqlite3Toupper(c); |
| 686 c = sqlite3Tolower(c); |
| 687 }else{ |
| 688 cx = c; |
| 689 } |
| 690 while( (c2 = *(zString++))!=0 ){ |
| 691 if( c2!=c && c2!=cx ) continue; |
| 692 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 693 } |
| 694 }else{ |
| 695 while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ |
| 696 if( c2!=c ) continue; |
| 697 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 698 } |
| 699 } |
| 700 return 0; |
| 701 } |
| 702 if( c==matchOther ){ |
| 703 if( esc ){ |
| 704 c = sqlite3Utf8Read(&zPattern); |
| 705 if( c==0 ) return 0; |
| 706 zEscaped = zPattern; |
| 707 }else{ |
| 708 u32 prior_c = 0; |
| 709 int seen = 0; |
| 710 int invert = 0; |
| 711 c = sqlite3Utf8Read(&zString); |
| 712 if( c==0 ) return 0; |
| 713 c2 = sqlite3Utf8Read(&zPattern); |
| 714 if( c2=='^' ){ |
| 715 invert = 1; |
| 716 c2 = sqlite3Utf8Read(&zPattern); |
| 717 } |
| 718 if( c2==']' ){ |
| 719 if( c==']' ) seen = 1; |
| 720 c2 = sqlite3Utf8Read(&zPattern); |
| 721 } |
| 722 while( c2 && c2!=']' ){ |
| 723 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 724 c2 = sqlite3Utf8Read(&zPattern); |
| 725 if( c>=prior_c && c<=c2 ) seen = 1; |
| 726 prior_c = 0; |
| 727 }else{ |
| 728 if( c==c2 ){ |
| 729 seen = 1; |
| 730 } |
| 731 prior_c = c2; |
| 732 } |
| 733 c2 = sqlite3Utf8Read(&zPattern); |
| 734 } |
| 735 if( c2==0 || (seen ^ invert)==0 ){ |
| 579 return 0; | 736 return 0; |
| 580 } | 737 } |
| 581 }else if( c==matchSet ){ | 738 continue; |
| 582 assert( esc==0 ); /* This is GLOB, not LIKE */ | |
| 583 assert( matchSet<0x80 ); /* '[' is a single-byte character */ | |
| 584 while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ | |
| 585 SQLITE_SKIP_UTF8(zString); | |
| 586 } | |
| 587 return *zString!=0; | |
| 588 } | 739 } |
| 589 while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){ | |
| 590 if( noCase ){ | |
| 591 GlogUpperToLower(c2); | |
| 592 GlogUpperToLower(c); | |
| 593 while( c2 != 0 && c2 != c ){ | |
| 594 c2 = sqlite3Utf8Read(zString, &zString); | |
| 595 GlogUpperToLower(c2); | |
| 596 } | |
| 597 }else{ | |
| 598 while( c2 != 0 && c2 != c ){ | |
| 599 c2 = sqlite3Utf8Read(zString, &zString); | |
| 600 } | |
| 601 } | |
| 602 if( c2==0 ) return 0; | |
| 603 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; | |
| 604 } | |
| 605 return 0; | |
| 606 }else if( !prevEscape && c==matchOne ){ | |
| 607 if( sqlite3Utf8Read(zString, &zString)==0 ){ | |
| 608 return 0; | |
| 609 } | |
| 610 }else if( c==matchSet ){ | |
| 611 int prior_c = 0; | |
| 612 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ | |
| 613 seen = 0; | |
| 614 invert = 0; | |
| 615 c = sqlite3Utf8Read(zString, &zString); | |
| 616 if( c==0 ) return 0; | |
| 617 c2 = sqlite3Utf8Read(zPattern, &zPattern); | |
| 618 if( c2=='^' ){ | |
| 619 invert = 1; | |
| 620 c2 = sqlite3Utf8Read(zPattern, &zPattern); | |
| 621 } | |
| 622 if( c2==']' ){ | |
| 623 if( c==']' ) seen = 1; | |
| 624 c2 = sqlite3Utf8Read(zPattern, &zPattern); | |
| 625 } | |
| 626 while( c2 && c2!=']' ){ | |
| 627 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ | |
| 628 c2 = sqlite3Utf8Read(zPattern, &zPattern); | |
| 629 if( c>=prior_c && c<=c2 ) seen = 1; | |
| 630 prior_c = 0; | |
| 631 }else{ | |
| 632 if( c==c2 ){ | |
| 633 seen = 1; | |
| 634 } | |
| 635 prior_c = c2; | |
| 636 } | |
| 637 c2 = sqlite3Utf8Read(zPattern, &zPattern); | |
| 638 } | |
| 639 if( c2==0 || (seen ^ invert)==0 ){ | |
| 640 return 0; | |
| 641 } | |
| 642 }else if( esc==c && !prevEscape ){ | |
| 643 prevEscape = 1; | |
| 644 }else{ | |
| 645 c2 = sqlite3Utf8Read(zString, &zString); | |
| 646 if( noCase ){ | |
| 647 GlogUpperToLower(c); | |
| 648 GlogUpperToLower(c2); | |
| 649 } | |
| 650 if( c!=c2 ){ | |
| 651 return 0; | |
| 652 } | |
| 653 prevEscape = 0; | |
| 654 } | 740 } |
| 741 c2 = sqlite3Utf8Read(&zString); |
| 742 if( c==c2 ) continue; |
| 743 if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ |
| 744 continue; |
| 745 } |
| 746 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; |
| 747 return 0; |
| 655 } | 748 } |
| 656 return *zString==0; | 749 return *zString==0; |
| 657 } | 750 } |
| 658 | 751 |
| 659 /* | 752 /* |
| 753 ** The sqlite3_strglob() interface. |
| 754 */ |
| 755 int sqlite3_strglob(const char *zGlobPattern, const char *zString){ |
| 756 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0; |
| 757 } |
| 758 |
| 759 /* |
| 660 ** Count the number of times that the LIKE operator (or GLOB which is | 760 ** Count the number of times that the LIKE operator (or GLOB which is |
| 661 ** just a variation of LIKE) gets called. This is used for testing | 761 ** just a variation of LIKE) gets called. This is used for testing |
| 662 ** only. | 762 ** only. |
| 663 */ | 763 */ |
| 664 #ifdef SQLITE_TEST | 764 #ifdef SQLITE_TEST |
| 665 int sqlite3_like_count = 0; | 765 int sqlite3_like_count = 0; |
| 666 #endif | 766 #endif |
| 667 | 767 |
| 668 | 768 |
| 669 /* | 769 /* |
| 670 ** Implementation of the like() SQL function. This function implements | 770 ** Implementation of the like() SQL function. This function implements |
| 671 ** the build-in LIKE operator. The first argument to the function is the | 771 ** the build-in LIKE operator. The first argument to the function is the |
| 672 ** pattern and the second argument is the string. So, the SQL statements: | 772 ** pattern and the second argument is the string. So, the SQL statements: |
| 673 ** | 773 ** |
| 674 ** A LIKE B | 774 ** A LIKE B |
| 675 ** | 775 ** |
| 676 ** is implemented as like(B,A). | 776 ** is implemented as like(B,A). |
| 677 ** | 777 ** |
| 678 ** This same function (with a different compareInfo structure) computes | 778 ** This same function (with a different compareInfo structure) computes |
| 679 ** the GLOB operator. | 779 ** the GLOB operator. |
| 680 */ | 780 */ |
| 681 static void likeFunc( | 781 static void likeFunc( |
| 682 sqlite3_context *context, | 782 sqlite3_context *context, |
| 683 int argc, | 783 int argc, |
| 684 sqlite3_value **argv | 784 sqlite3_value **argv |
| 685 ){ | 785 ){ |
| 686 const unsigned char *zA, *zB; | 786 const unsigned char *zA, *zB; |
| 687 int escape = 0; | 787 u32 escape = 0; |
| 688 int nPat; | 788 int nPat; |
| 689 sqlite3 *db = sqlite3_context_db_handle(context); | 789 sqlite3 *db = sqlite3_context_db_handle(context); |
| 690 | 790 |
| 691 zB = sqlite3_value_text(argv[0]); | 791 zB = sqlite3_value_text(argv[0]); |
| 692 zA = sqlite3_value_text(argv[1]); | 792 zA = sqlite3_value_text(argv[1]); |
| 693 | 793 |
| 694 /* Limit the length of the LIKE or GLOB pattern to avoid problems | 794 /* Limit the length of the LIKE or GLOB pattern to avoid problems |
| 695 ** of deep recursion and N*N behavior in patternCompare(). | 795 ** of deep recursion and N*N behavior in patternCompare(). |
| 696 */ | 796 */ |
| 697 nPat = sqlite3_value_bytes(argv[0]); | 797 nPat = sqlite3_value_bytes(argv[0]); |
| 698 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); | 798 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); |
| 699 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); | 799 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); |
| 700 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ | 800 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ |
| 701 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); | 801 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); |
| 702 return; | 802 return; |
| 703 } | 803 } |
| 704 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ | 804 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ |
| 705 | 805 |
| 706 if( argc==3 ){ | 806 if( argc==3 ){ |
| 707 /* The escape character string must consist of a single UTF-8 character. | 807 /* The escape character string must consist of a single UTF-8 character. |
| 708 ** Otherwise, return an error. | 808 ** Otherwise, return an error. |
| 709 */ | 809 */ |
| 710 const unsigned char *zEsc = sqlite3_value_text(argv[2]); | 810 const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
| 711 if( zEsc==0 ) return; | 811 if( zEsc==0 ) return; |
| 712 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ | 812 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ |
| 713 sqlite3_result_error(context, | 813 sqlite3_result_error(context, |
| 714 "ESCAPE expression must be a single character", -1); | 814 "ESCAPE expression must be a single character", -1); |
| 715 return; | 815 return; |
| 716 } | 816 } |
| 717 escape = sqlite3Utf8Read(zEsc, &zEsc); | 817 escape = sqlite3Utf8Read(&zEsc); |
| 718 } | 818 } |
| 719 if( zA && zB ){ | 819 if( zA && zB ){ |
| 720 struct compareInfo *pInfo = sqlite3_user_data(context); | 820 struct compareInfo *pInfo = sqlite3_user_data(context); |
| 721 #ifdef SQLITE_TEST | 821 #ifdef SQLITE_TEST |
| 722 sqlite3_like_count++; | 822 sqlite3_like_count++; |
| 723 #endif | 823 #endif |
| 724 | 824 |
| 725 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); | 825 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); |
| 726 } | 826 } |
| 727 } | 827 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 int NotUsed, | 868 int NotUsed, |
| 769 sqlite3_value **NotUsed2 | 869 sqlite3_value **NotUsed2 |
| 770 ){ | 870 ){ |
| 771 UNUSED_PARAMETER2(NotUsed, NotUsed2); | 871 UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 772 /* IMP: R-24470-31136 This function is an SQL wrapper around the | 872 /* IMP: R-24470-31136 This function is an SQL wrapper around the |
| 773 ** sqlite3_sourceid() C interface. */ | 873 ** sqlite3_sourceid() C interface. */ |
| 774 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); | 874 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); |
| 775 } | 875 } |
| 776 | 876 |
| 777 /* | 877 /* |
| 878 ** Implementation of the sqlite_log() function. This is a wrapper around |
| 879 ** sqlite3_log(). The return value is NULL. The function exists purely for |
| 880 ** its side-effects. |
| 881 */ |
| 882 static void errlogFunc( |
| 883 sqlite3_context *context, |
| 884 int argc, |
| 885 sqlite3_value **argv |
| 886 ){ |
| 887 UNUSED_PARAMETER(argc); |
| 888 UNUSED_PARAMETER(context); |
| 889 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); |
| 890 } |
| 891 |
| 892 /* |
| 778 ** Implementation of the sqlite_compileoption_used() function. | 893 ** Implementation of the sqlite_compileoption_used() function. |
| 779 ** The result is an integer that identifies if the compiler option | 894 ** The result is an integer that identifies if the compiler option |
| 780 ** was used to build SQLite. | 895 ** was used to build SQLite. |
| 781 */ | 896 */ |
| 782 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS | 897 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 783 static void compileoptionusedFunc( | 898 static void compileoptionusedFunc( |
| 784 sqlite3_context *context, | 899 sqlite3_context *context, |
| 785 int argc, | 900 int argc, |
| 786 sqlite3_value **argv | 901 sqlite3_value **argv |
| 787 ){ | 902 ){ |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ | 936 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 822 | 937 |
| 823 /* Array for converting from half-bytes (nybbles) into ASCII hex | 938 /* Array for converting from half-bytes (nybbles) into ASCII hex |
| 824 ** digits. */ | 939 ** digits. */ |
| 825 static const char hexdigits[] = { | 940 static const char hexdigits[] = { |
| 826 '0', '1', '2', '3', '4', '5', '6', '7', | 941 '0', '1', '2', '3', '4', '5', '6', '7', |
| 827 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' | 942 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 828 }; | 943 }; |
| 829 | 944 |
| 830 /* | 945 /* |
| 831 ** EXPERIMENTAL - This is not an official function. The interface may | |
| 832 ** change. This function may disappear. Do not write code that depends | |
| 833 ** on this function. | |
| 834 ** | |
| 835 ** Implementation of the QUOTE() function. This function takes a single | 946 ** Implementation of the QUOTE() function. This function takes a single |
| 836 ** argument. If the argument is numeric, the return value is the same as | 947 ** argument. If the argument is numeric, the return value is the same as |
| 837 ** the argument. If the argument is NULL, the return value is the string | 948 ** the argument. If the argument is NULL, the return value is the string |
| 838 ** "NULL". Otherwise, the argument is enclosed in single quotes with | 949 ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 839 ** single-quote escapes. | 950 ** single-quote escapes. |
| 840 */ | 951 */ |
| 841 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ | 952 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 842 assert( argc==1 ); | 953 assert( argc==1 ); |
| 843 UNUSED_PARAMETER(argc); | 954 UNUSED_PARAMETER(argc); |
| 844 switch( sqlite3_value_type(argv[0]) ){ | 955 switch( sqlite3_value_type(argv[0]) ){ |
| 845 case SQLITE_INTEGER: | |
| 846 case SQLITE_FLOAT: { | 956 case SQLITE_FLOAT: { |
| 957 double r1, r2; |
| 958 char zBuf[50]; |
| 959 r1 = sqlite3_value_double(argv[0]); |
| 960 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); |
| 961 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); |
| 962 if( r1!=r2 ){ |
| 963 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); |
| 964 } |
| 965 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 966 break; |
| 967 } |
| 968 case SQLITE_INTEGER: { |
| 847 sqlite3_result_value(context, argv[0]); | 969 sqlite3_result_value(context, argv[0]); |
| 848 break; | 970 break; |
| 849 } | 971 } |
| 850 case SQLITE_BLOB: { | 972 case SQLITE_BLOB: { |
| 851 char *zText = 0; | 973 char *zText = 0; |
| 852 char const *zBlob = sqlite3_value_blob(argv[0]); | 974 char const *zBlob = sqlite3_value_blob(argv[0]); |
| 853 int nBlob = sqlite3_value_bytes(argv[0]); | 975 int nBlob = sqlite3_value_bytes(argv[0]); |
| 854 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ | 976 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ |
| 855 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); | 977 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); |
| 856 if( zText ){ | 978 if( zText ){ |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 } | 1015 } |
| 894 default: { | 1016 default: { |
| 895 assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); | 1017 assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); |
| 896 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); | 1018 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
| 897 break; | 1019 break; |
| 898 } | 1020 } |
| 899 } | 1021 } |
| 900 } | 1022 } |
| 901 | 1023 |
| 902 /* | 1024 /* |
| 1025 ** The unicode() function. Return the integer unicode code-point value |
| 1026 ** for the first character of the input string. |
| 1027 */ |
| 1028 static void unicodeFunc( |
| 1029 sqlite3_context *context, |
| 1030 int argc, |
| 1031 sqlite3_value **argv |
| 1032 ){ |
| 1033 const unsigned char *z = sqlite3_value_text(argv[0]); |
| 1034 (void)argc; |
| 1035 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); |
| 1036 } |
| 1037 |
| 1038 /* |
| 1039 ** The char() function takes zero or more arguments, each of which is |
| 1040 ** an integer. It constructs a string where each character of the string |
| 1041 ** is the unicode character for the corresponding integer argument. |
| 1042 */ |
| 1043 static void charFunc( |
| 1044 sqlite3_context *context, |
| 1045 int argc, |
| 1046 sqlite3_value **argv |
| 1047 ){ |
| 1048 unsigned char *z, *zOut; |
| 1049 int i; |
| 1050 zOut = z = sqlite3_malloc( argc*4+1 ); |
| 1051 if( z==0 ){ |
| 1052 sqlite3_result_error_nomem(context); |
| 1053 return; |
| 1054 } |
| 1055 for(i=0; i<argc; i++){ |
| 1056 sqlite3_int64 x; |
| 1057 unsigned c; |
| 1058 x = sqlite3_value_int64(argv[i]); |
| 1059 if( x<0 || x>0x10ffff ) x = 0xfffd; |
| 1060 c = (unsigned)(x & 0x1fffff); |
| 1061 if( c<0x00080 ){ |
| 1062 *zOut++ = (u8)(c&0xFF); |
| 1063 }else if( c<0x00800 ){ |
| 1064 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); |
| 1065 *zOut++ = 0x80 + (u8)(c & 0x3F); |
| 1066 }else if( c<0x10000 ){ |
| 1067 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); |
| 1068 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); |
| 1069 *zOut++ = 0x80 + (u8)(c & 0x3F); |
| 1070 }else{ |
| 1071 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); |
| 1072 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); |
| 1073 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); |
| 1074 *zOut++ = 0x80 + (u8)(c & 0x3F); |
| 1075 } \ |
| 1076 } |
| 1077 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); |
| 1078 } |
| 1079 |
| 1080 /* |
| 903 ** The hex() function. Interpret the argument as a blob. Return | 1081 ** The hex() function. Interpret the argument as a blob. Return |
| 904 ** a hexadecimal rendering as text. | 1082 ** a hexadecimal rendering as text. |
| 905 */ | 1083 */ |
| 906 static void hexFunc( | 1084 static void hexFunc( |
| 907 sqlite3_context *context, | 1085 sqlite3_context *context, |
| 908 int argc, | 1086 int argc, |
| 909 sqlite3_value **argv | 1087 sqlite3_value **argv |
| 910 ){ | 1088 ){ |
| 911 int i, n; | 1089 int i, n; |
| 912 const unsigned char *pBlob; | 1090 const unsigned char *pBlob; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 946 if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | 1124 if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 947 sqlite3_result_error_toobig(context); | 1125 sqlite3_result_error_toobig(context); |
| 948 }else{ | 1126 }else{ |
| 949 sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */ | 1127 sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */ |
| 950 } | 1128 } |
| 951 } | 1129 } |
| 952 | 1130 |
| 953 /* | 1131 /* |
| 954 ** The replace() function. Three arguments are all strings: call | 1132 ** The replace() function. Three arguments are all strings: call |
| 955 ** them A, B, and C. The result is also a string which is derived | 1133 ** them A, B, and C. The result is also a string which is derived |
| 956 ** from A by replacing every occurance of B with C. The match | 1134 ** from A by replacing every occurrence of B with C. The match |
| 957 ** must be exact. Collating sequences are not used. | 1135 ** must be exact. Collating sequences are not used. |
| 958 */ | 1136 */ |
| 959 static void replaceFunc( | 1137 static void replaceFunc( |
| 960 sqlite3_context *context, | 1138 sqlite3_context *context, |
| 961 int argc, | 1139 int argc, |
| 962 sqlite3_value **argv | 1140 sqlite3_value **argv |
| 963 ){ | 1141 ){ |
| 964 const unsigned char *zStr; /* The input string A */ | 1142 const unsigned char *zStr; /* The input string A */ |
| 965 const unsigned char *zPattern; /* The pattern string B */ | 1143 const unsigned char *zPattern; /* The pattern string B */ |
| 966 const unsigned char *zRep; /* The replacement string C */ | 1144 const unsigned char *zRep; /* The replacement string C */ |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 */ | 1492 */ |
| 1315 static void minmaxStep( | 1493 static void minmaxStep( |
| 1316 sqlite3_context *context, | 1494 sqlite3_context *context, |
| 1317 int NotUsed, | 1495 int NotUsed, |
| 1318 sqlite3_value **argv | 1496 sqlite3_value **argv |
| 1319 ){ | 1497 ){ |
| 1320 Mem *pArg = (Mem *)argv[0]; | 1498 Mem *pArg = (Mem *)argv[0]; |
| 1321 Mem *pBest; | 1499 Mem *pBest; |
| 1322 UNUSED_PARAMETER(NotUsed); | 1500 UNUSED_PARAMETER(NotUsed); |
| 1323 | 1501 |
| 1324 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; | |
| 1325 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); | 1502 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
| 1326 if( !pBest ) return; | 1503 if( !pBest ) return; |
| 1327 | 1504 |
| 1328 if( pBest->flags ){ | 1505 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 1506 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); |
| 1507 }else if( pBest->flags ){ |
| 1329 int max; | 1508 int max; |
| 1330 int cmp; | 1509 int cmp; |
| 1331 CollSeq *pColl = sqlite3GetFuncCollSeq(context); | 1510 CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
| 1332 /* This step function is used for both the min() and max() aggregates, | 1511 /* This step function is used for both the min() and max() aggregates, |
| 1333 ** the only difference between the two being that the sense of the | 1512 ** the only difference between the two being that the sense of the |
| 1334 ** comparison is inverted. For the max() aggregate, the | 1513 ** comparison is inverted. For the max() aggregate, the |
| 1335 ** sqlite3_user_data() function returns (void *)-1. For min() it | 1514 ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 1336 ** returns (void *)db, where db is the sqlite3* database pointer. | 1515 ** returns (void *)db, where db is the sqlite3* database pointer. |
| 1337 ** Therefore the next statement sets variable 'max' to 1 for the max() | 1516 ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 1338 ** aggregate, or 0 for min(). | 1517 ** aggregate, or 0 for min(). |
| 1339 */ | 1518 */ |
| 1340 max = sqlite3_user_data(context)!=0; | 1519 max = sqlite3_user_data(context)!=0; |
| 1341 cmp = sqlite3MemCompare(pBest, pArg, pColl); | 1520 cmp = sqlite3MemCompare(pBest, pArg, pColl); |
| 1342 if( (max && cmp<0) || (!max && cmp>0) ){ | 1521 if( (max && cmp<0) || (!max && cmp>0) ){ |
| 1343 sqlite3VdbeMemCopy(pBest, pArg); | 1522 sqlite3VdbeMemCopy(pBest, pArg); |
| 1523 }else{ |
| 1524 sqlite3SkipAccumulatorLoad(context); |
| 1344 } | 1525 } |
| 1345 }else{ | 1526 }else{ |
| 1527 pBest->db = sqlite3_context_db_handle(context); |
| 1346 sqlite3VdbeMemCopy(pBest, pArg); | 1528 sqlite3VdbeMemCopy(pBest, pArg); |
| 1347 } | 1529 } |
| 1348 } | 1530 } |
| 1349 static void minMaxFinalize(sqlite3_context *context){ | 1531 static void minMaxFinalize(sqlite3_context *context){ |
| 1350 sqlite3_value *pRes; | 1532 sqlite3_value *pRes; |
| 1351 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); | 1533 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); |
| 1352 if( pRes ){ | 1534 if( pRes ){ |
| 1353 if( ALWAYS(pRes->flags) ){ | 1535 if( pRes->flags ){ |
| 1354 sqlite3_result_value(context, pRes); | 1536 sqlite3_result_value(context, pRes); |
| 1355 } | 1537 } |
| 1356 sqlite3VdbeMemRelease(pRes); | 1538 sqlite3VdbeMemRelease(pRes); |
| 1357 } | 1539 } |
| 1358 } | 1540 } |
| 1359 | 1541 |
| 1360 /* | 1542 /* |
| 1361 ** group_concat(EXPR, ?SEPARATOR?) | 1543 ** group_concat(EXPR, ?SEPARATOR?) |
| 1362 */ | 1544 */ |
| 1363 static void groupConcatStep( | 1545 static void groupConcatStep( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1379 pAccum->useMalloc = 2; | 1561 pAccum->useMalloc = 2; |
| 1380 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; | 1562 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 1381 if( !firstTerm ){ | 1563 if( !firstTerm ){ |
| 1382 if( argc==2 ){ | 1564 if( argc==2 ){ |
| 1383 zSep = (char*)sqlite3_value_text(argv[1]); | 1565 zSep = (char*)sqlite3_value_text(argv[1]); |
| 1384 nSep = sqlite3_value_bytes(argv[1]); | 1566 nSep = sqlite3_value_bytes(argv[1]); |
| 1385 }else{ | 1567 }else{ |
| 1386 zSep = ","; | 1568 zSep = ","; |
| 1387 nSep = 1; | 1569 nSep = 1; |
| 1388 } | 1570 } |
| 1389 sqlite3StrAccumAppend(pAccum, zSep, nSep); | 1571 if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); |
| 1390 } | 1572 } |
| 1391 zVal = (char*)sqlite3_value_text(argv[0]); | 1573 zVal = (char*)sqlite3_value_text(argv[0]); |
| 1392 nVal = sqlite3_value_bytes(argv[0]); | 1574 nVal = sqlite3_value_bytes(argv[0]); |
| 1393 sqlite3StrAccumAppend(pAccum, zVal, nVal); | 1575 if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal); |
| 1394 } | 1576 } |
| 1395 } | 1577 } |
| 1396 static void groupConcatFinalize(sqlite3_context *context){ | 1578 static void groupConcatFinalize(sqlite3_context *context){ |
| 1397 StrAccum *pAccum; | 1579 StrAccum *pAccum; |
| 1398 pAccum = sqlite3_aggregate_context(context, 0); | 1580 pAccum = sqlite3_aggregate_context(context, 0); |
| 1399 if( pAccum ){ | 1581 if( pAccum ){ |
| 1400 if( pAccum->tooBig ){ | 1582 if( pAccum->accError==STRACCUM_TOOBIG ){ |
| 1401 sqlite3_result_error_toobig(context); | 1583 sqlite3_result_error_toobig(context); |
| 1402 }else if( pAccum->mallocFailed ){ | 1584 }else if( pAccum->accError==STRACCUM_NOMEM ){ |
| 1403 sqlite3_result_error_nomem(context); | 1585 sqlite3_result_error_nomem(context); |
| 1404 }else{ | 1586 }else{ |
| 1405 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, | 1587 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, |
| 1406 sqlite3_free); | 1588 sqlite3_free); |
| 1407 } | 1589 } |
| 1408 } | 1590 } |
| 1409 } | 1591 } |
| 1410 | 1592 |
| 1411 /* | 1593 /* |
| 1412 ** This routine does per-connection function registration. Most | 1594 ** This routine does per-connection function registration. Most |
| 1413 ** of the built-in functions above are part of the global function set. | 1595 ** of the built-in functions above are part of the global function set. |
| 1414 ** This routine only deals with those that are not global. | 1596 ** This routine only deals with those that are not global. |
| 1415 */ | 1597 */ |
| 1416 void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ | 1598 void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ |
| 1417 int rc = sqlite3_overload_function(db, "MATCH", 2); | 1599 int rc = sqlite3_overload_function(db, "MATCH", 2); |
| 1418 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); | 1600 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); |
| 1419 if( rc==SQLITE_NOMEM ){ | 1601 if( rc==SQLITE_NOMEM ){ |
| 1420 db->mallocFailed = 1; | 1602 db->mallocFailed = 1; |
| 1421 } | 1603 } |
| 1422 } | 1604 } |
| 1423 | 1605 |
| 1424 /* | 1606 /* |
| 1425 ** Set the LIKEOPT flag on the 2-argument function with the given name. | 1607 ** Set the LIKEOPT flag on the 2-argument function with the given name. |
| 1426 */ | 1608 */ |
| 1427 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ | 1609 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ |
| 1428 FuncDef *pDef; | 1610 FuncDef *pDef; |
| 1429 pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName), | 1611 pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName), |
| 1430 2, SQLITE_UTF8, 0); | 1612 2, SQLITE_UTF8, 0); |
| 1431 if( ALWAYS(pDef) ){ | 1613 if( ALWAYS(pDef) ){ |
| 1432 pDef->flags = flagVal; | 1614 pDef->funcFlags |= flagVal; |
| 1433 } | 1615 } |
| 1434 } | 1616 } |
| 1435 | 1617 |
| 1436 /* | 1618 /* |
| 1437 ** Register the built-in LIKE and GLOB functions. The caseSensitive | 1619 ** Register the built-in LIKE and GLOB functions. The caseSensitive |
| 1438 ** parameter determines whether or not the LIKE operator is case | 1620 ** parameter determines whether or not the LIKE operator is case |
| 1439 ** sensitive. GLOB is always case sensitive. | 1621 ** sensitive. GLOB is always case sensitive. |
| 1440 */ | 1622 */ |
| 1441 void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ | 1623 void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ |
| 1442 struct compareInfo *pInfo; | 1624 struct compareInfo *pInfo; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1466 if( pExpr->op!=TK_FUNCTION | 1648 if( pExpr->op!=TK_FUNCTION |
| 1467 || !pExpr->x.pList | 1649 || !pExpr->x.pList |
| 1468 || pExpr->x.pList->nExpr!=2 | 1650 || pExpr->x.pList->nExpr!=2 |
| 1469 ){ | 1651 ){ |
| 1470 return 0; | 1652 return 0; |
| 1471 } | 1653 } |
| 1472 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | 1654 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 1473 pDef = sqlite3FindFunction(db, pExpr->u.zToken, | 1655 pDef = sqlite3FindFunction(db, pExpr->u.zToken, |
| 1474 sqlite3Strlen30(pExpr->u.zToken), | 1656 sqlite3Strlen30(pExpr->u.zToken), |
| 1475 2, SQLITE_UTF8, 0); | 1657 2, SQLITE_UTF8, 0); |
| 1476 if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ | 1658 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ |
| 1477 return 0; | 1659 return 0; |
| 1478 } | 1660 } |
| 1479 | 1661 |
| 1480 /* The memcpy() statement assumes that the wildcard characters are | 1662 /* The memcpy() statement assumes that the wildcard characters are |
| 1481 ** the first three statements in the compareInfo structure. The | 1663 ** the first three statements in the compareInfo structure. The |
| 1482 ** asserts() that follow verify that assumption | 1664 ** asserts() that follow verify that assumption |
| 1483 */ | 1665 */ |
| 1484 memcpy(aWc, pDef->pUserData, 3); | 1666 memcpy(aWc, pDef->pUserData, 3); |
| 1485 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); | 1667 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); |
| 1486 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); | 1668 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); |
| 1487 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); | 1669 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); |
| 1488 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; | 1670 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; |
| 1489 return 1; | 1671 return 1; |
| 1490 } | 1672 } |
| 1491 | 1673 |
| 1492 /* | 1674 /* |
| 1493 ** All all of the FuncDef structures in the aBuiltinFunc[] array above | 1675 ** All of the FuncDef structures in the aBuiltinFunc[] array above |
| 1494 ** to the global function hash table. This occurs at start-time (as | 1676 ** to the global function hash table. This occurs at start-time (as |
| 1495 ** a consequence of calling sqlite3_initialize()). | 1677 ** a consequence of calling sqlite3_initialize()). |
| 1496 ** | 1678 ** |
| 1497 ** After this routine runs | 1679 ** After this routine runs |
| 1498 */ | 1680 */ |
| 1499 void sqlite3RegisterGlobalFunctions(void){ | 1681 void sqlite3RegisterGlobalFunctions(void){ |
| 1500 /* | 1682 /* |
| 1501 ** The following array holds FuncDef structures for all of the functions | 1683 ** The following array holds FuncDef structures for all of the functions |
| 1502 ** defined in this file. | 1684 ** defined in this file. |
| 1503 ** | 1685 ** |
| 1504 ** The array cannot be constant since changes are made to the | 1686 ** The array cannot be constant since changes are made to the |
| 1505 ** FuncDef.pHash elements at start-time. The elements of this array | 1687 ** FuncDef.pHash elements at start-time. The elements of this array |
| 1506 ** are read-only after initialization is complete. | 1688 ** are read-only after initialization is complete. |
| 1507 */ | 1689 */ |
| 1508 static SQLITE_WSD FuncDef aBuiltinFunc[] = { | 1690 static SQLITE_WSD FuncDef aBuiltinFunc[] = { |
| 1509 FUNCTION(ltrim, 1, 1, 0, trimFunc ), | 1691 FUNCTION(ltrim, 1, 1, 0, trimFunc ), |
| 1510 FUNCTION(ltrim, 2, 1, 0, trimFunc ), | 1692 FUNCTION(ltrim, 2, 1, 0, trimFunc ), |
| 1511 FUNCTION(rtrim, 1, 2, 0, trimFunc ), | 1693 FUNCTION(rtrim, 1, 2, 0, trimFunc ), |
| 1512 FUNCTION(rtrim, 2, 2, 0, trimFunc ), | 1694 FUNCTION(rtrim, 2, 2, 0, trimFunc ), |
| 1513 FUNCTION(trim, 1, 3, 0, trimFunc ), | 1695 FUNCTION(trim, 1, 3, 0, trimFunc ), |
| 1514 FUNCTION(trim, 2, 3, 0, trimFunc ), | 1696 FUNCTION(trim, 2, 3, 0, trimFunc ), |
| 1515 FUNCTION(min, -1, 0, 1, minmaxFunc ), | 1697 FUNCTION(min, -1, 0, 1, minmaxFunc ), |
| 1516 FUNCTION(min, 0, 0, 1, 0 ), | 1698 FUNCTION(min, 0, 0, 1, 0 ), |
| 1517 AGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize ), | 1699 AGGREGATE2(min, 1, 0, 1, minmaxStep, minMaxFinalize, |
| 1700 SQLITE_FUNC_MINMAX ), |
| 1518 FUNCTION(max, -1, 1, 1, minmaxFunc ), | 1701 FUNCTION(max, -1, 1, 1, minmaxFunc ), |
| 1519 FUNCTION(max, 0, 1, 1, 0 ), | 1702 FUNCTION(max, 0, 1, 1, 0 ), |
| 1520 AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ), | 1703 AGGREGATE2(max, 1, 1, 1, minmaxStep, minMaxFinalize, |
| 1521 FUNCTION(typeof, 1, 0, 0, typeofFunc ), | 1704 SQLITE_FUNC_MINMAX ), |
| 1522 FUNCTION(length, 1, 0, 0, lengthFunc ), | 1705 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), |
| 1706 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), |
| 1707 FUNCTION(instr, 2, 0, 0, instrFunc ), |
| 1523 FUNCTION(substr, 2, 0, 0, substrFunc ), | 1708 FUNCTION(substr, 2, 0, 0, substrFunc ), |
| 1524 FUNCTION(substr, 3, 0, 0, substrFunc ), | 1709 FUNCTION(substr, 3, 0, 0, substrFunc ), |
| 1710 FUNCTION(printf, -1, 0, 0, printfFunc ), |
| 1711 FUNCTION(unicode, 1, 0, 0, unicodeFunc ), |
| 1712 FUNCTION(char, -1, 0, 0, charFunc ), |
| 1525 FUNCTION(abs, 1, 0, 0, absFunc ), | 1713 FUNCTION(abs, 1, 0, 0, absFunc ), |
| 1526 #ifndef SQLITE_OMIT_FLOATING_POINT | 1714 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1527 FUNCTION(round, 1, 0, 0, roundFunc ), | 1715 FUNCTION(round, 1, 0, 0, roundFunc ), |
| 1528 FUNCTION(round, 2, 0, 0, roundFunc ), | 1716 FUNCTION(round, 2, 0, 0, roundFunc ), |
| 1529 #endif | 1717 #endif |
| 1530 FUNCTION(upper, 1, 0, 0, upperFunc ), | 1718 FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 1531 FUNCTION(lower, 1, 0, 0, lowerFunc ), | 1719 FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 1532 FUNCTION(coalesce, 1, 0, 0, 0 ), | 1720 FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 1533 FUNCTION(coalesce, 0, 0, 0, 0 ), | 1721 FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 1534 /* FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), */ | 1722 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), |
| 1535 {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0}, | |
| 1536 FUNCTION(hex, 1, 0, 0, hexFunc ), | 1723 FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 1537 /* FUNCTION(ifnull, 2, 0, 0, ifnullFunc ), */ | 1724 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE), |
| 1538 {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0}, | 1725 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 1539 FUNCTION(random, 0, 0, 0, randomFunc ), | 1726 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 1540 FUNCTION(randomblob, 1, 0, 0, randomBlob ), | 1727 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 1728 VFUNCTION(random, 0, 0, 0, randomFunc ), |
| 1729 VFUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 1541 FUNCTION(nullif, 2, 0, 1, nullifFunc ), | 1730 FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 1542 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), | 1731 FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 1543 FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), | 1732 FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| 1733 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), |
| 1734 #if SQLITE_USER_AUTHENTICATION |
| 1735 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), |
| 1736 #endif |
| 1544 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS | 1737 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 1545 FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), | 1738 FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), |
| 1546 FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), | 1739 FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), |
| 1547 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ | 1740 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 1548 FUNCTION(quote, 1, 0, 0, quoteFunc ), | 1741 FUNCTION(quote, 1, 0, 0, quoteFunc ), |
| 1549 FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), | 1742 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), |
| 1550 FUNCTION(changes, 0, 0, 0, changes ), | 1743 VFUNCTION(changes, 0, 0, 0, changes ), |
| 1551 FUNCTION(total_changes, 0, 0, 0, total_changes ), | 1744 VFUNCTION(total_changes, 0, 0, 0, total_changes ), |
| 1552 FUNCTION(replace, 3, 0, 0, replaceFunc ), | 1745 FUNCTION(replace, 3, 0, 0, replaceFunc ), |
| 1553 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), | 1746 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), |
| 1554 #ifdef SQLITE_SOUNDEX | 1747 #ifdef SQLITE_SOUNDEX |
| 1555 FUNCTION(soundex, 1, 0, 0, soundexFunc ), | 1748 FUNCTION(soundex, 1, 0, 0, soundexFunc ), |
| 1556 #endif | 1749 #endif |
| 1557 #ifndef SQLITE_OMIT_LOAD_EXTENSION | 1750 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 1558 FUNCTION(load_extension, 1, 0, 0, loadExt ), | 1751 FUNCTION(load_extension, 1, 0, 0, loadExt ), |
| 1559 FUNCTION(load_extension, 2, 0, 0, loadExt ), | 1752 FUNCTION(load_extension, 2, 0, 0, loadExt ), |
| 1560 #endif | 1753 #endif |
| 1561 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), | 1754 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), |
| 1562 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), | 1755 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), |
| 1563 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), | 1756 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), |
| 1564 /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */ | 1757 AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, |
| 1565 {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0}, | 1758 SQLITE_FUNC_COUNT ), |
| 1566 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), | 1759 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), |
| 1567 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), | 1760 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), |
| 1568 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), | 1761 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), |
| 1569 | 1762 |
| 1570 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), | 1763 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 1571 #ifdef SQLITE_CASE_SENSITIVE_LIKE | 1764 #ifdef SQLITE_CASE_SENSITIVE_LIKE |
| 1572 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), | 1765 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 1573 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), | 1766 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 1574 #else | 1767 #else |
| 1575 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), | 1768 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), |
| 1576 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), | 1769 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), |
| 1577 #endif | 1770 #endif |
| 1578 }; | 1771 }; |
| 1579 | 1772 |
| 1580 int i; | 1773 int i; |
| 1581 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); | 1774 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 1582 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc); | 1775 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc); |
| 1583 | 1776 |
| 1584 for(i=0; i<ArraySize(aBuiltinFunc); i++){ | 1777 for(i=0; i<ArraySize(aBuiltinFunc); i++){ |
| 1585 sqlite3FuncDefInsert(pHash, &aFunc[i]); | 1778 sqlite3FuncDefInsert(pHash, &aFunc[i]); |
| 1586 } | 1779 } |
| 1587 sqlite3RegisterDateTimeFunctions(); | 1780 sqlite3RegisterDateTimeFunctions(); |
| 1588 #ifndef SQLITE_OMIT_ALTERTABLE | 1781 #ifndef SQLITE_OMIT_ALTERTABLE |
| 1589 sqlite3AlterFunctions(); | 1782 sqlite3AlterFunctions(); |
| 1590 #endif | 1783 #endif |
| 1784 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4) |
| 1785 sqlite3AnalyzeFunctions(); |
| 1786 #endif |
| 1591 } | 1787 } |
| OLD | NEW |