1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/func.c Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,1509 @@
1.4 +/*
1.5 +** 2002 February 23
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** This file contains the C functions that implement various SQL
1.16 +** functions of SQLite.
1.17 +**
1.18 +** There is only one exported symbol in this file - the function
1.19 +** sqliteRegisterBuildinFunctions() found at the bottom of the file.
1.20 +** All other code has file scope.
1.21 +**
1.22 +** $Id$
1.23 +*/
1.24 +#include "sqliteInt.h"
1.25 +#include <ctype.h>
1.26 +#include <stdlib.h>
1.27 +#include <assert.h>
1.28 +#include "vdbeInt.h"
1.29 +
1.30 +
1.31 +/*
1.32 +** Return the collating function associated with a function.
1.33 +*/
1.34 +static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
1.35 + return context->pColl;
1.36 +}
1.37 +
1.38 +/*
1.39 +** Implementation of the non-aggregate min() and max() functions
1.40 +*/
1.41 +static void minmaxFunc(
1.42 + sqlite3_context *context,
1.43 + int argc,
1.44 + sqlite3_value **argv
1.45 +){
1.46 + int i;
1.47 + int mask; /* 0 for min() or 0xffffffff for max() */
1.48 + int iBest;
1.49 + CollSeq *pColl;
1.50 +
1.51 + if( argc==0 ) return;
1.52 + mask = sqlite3_user_data(context)==0 ? 0 : -1;
1.53 + pColl = sqlite3GetFuncCollSeq(context);
1.54 + assert( pColl );
1.55 + assert( mask==-1 || mask==0 );
1.56 + iBest = 0;
1.57 + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1.58 + for(i=1; i<argc; i++){
1.59 + if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
1.60 + if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
1.61 + iBest = i;
1.62 + }
1.63 + }
1.64 + sqlite3_result_value(context, argv[iBest]);
1.65 +}
1.66 +
1.67 +/*
1.68 +** Return the type of the argument.
1.69 +*/
1.70 +static void typeofFunc(
1.71 + sqlite3_context *context,
1.72 + int argc,
1.73 + sqlite3_value **argv
1.74 +){
1.75 + const char *z = 0;
1.76 + switch( sqlite3_value_type(argv[0]) ){
1.77 + case SQLITE_NULL: z = "null"; break;
1.78 + case SQLITE_INTEGER: z = "integer"; break;
1.79 + case SQLITE_TEXT: z = "text"; break;
1.80 + case SQLITE_FLOAT: z = "real"; break;
1.81 + case SQLITE_BLOB: z = "blob"; break;
1.82 + }
1.83 + sqlite3_result_text(context, z, -1, SQLITE_STATIC);
1.84 +}
1.85 +
1.86 +
1.87 +/*
1.88 +** Implementation of the length() function
1.89 +*/
1.90 +static void lengthFunc(
1.91 + sqlite3_context *context,
1.92 + int argc,
1.93 + sqlite3_value **argv
1.94 +){
1.95 + int len;
1.96 +
1.97 + assert( argc==1 );
1.98 + switch( sqlite3_value_type(argv[0]) ){
1.99 + case SQLITE_BLOB:
1.100 + case SQLITE_INTEGER:
1.101 + case SQLITE_FLOAT: {
1.102 + sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
1.103 + break;
1.104 + }
1.105 + case SQLITE_TEXT: {
1.106 + const unsigned char *z = sqlite3_value_text(argv[0]);
1.107 + if( z==0 ) return;
1.108 + len = 0;
1.109 + while( *z ){
1.110 + len++;
1.111 + SQLITE_SKIP_UTF8(z);
1.112 + }
1.113 + sqlite3_result_int(context, len);
1.114 + break;
1.115 + }
1.116 + default: {
1.117 + sqlite3_result_null(context);
1.118 + break;
1.119 + }
1.120 + }
1.121 +}
1.122 +
1.123 +/*
1.124 +** Implementation of the abs() function
1.125 +*/
1.126 +static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1.127 + assert( argc==1 );
1.128 + switch( sqlite3_value_type(argv[0]) ){
1.129 + case SQLITE_INTEGER: {
1.130 + i64 iVal = sqlite3_value_int64(argv[0]);
1.131 + if( iVal<0 ){
1.132 + if( (iVal<<1)==0 ){
1.133 + sqlite3_result_error(context, "integer overflow", -1);
1.134 + return;
1.135 + }
1.136 + iVal = -iVal;
1.137 + }
1.138 + sqlite3_result_int64(context, iVal);
1.139 + break;
1.140 + }
1.141 + case SQLITE_NULL: {
1.142 + sqlite3_result_null(context);
1.143 + break;
1.144 + }
1.145 + default: {
1.146 + double rVal = sqlite3_value_double(argv[0]);
1.147 + if( rVal<0 ) rVal = -rVal;
1.148 + sqlite3_result_double(context, rVal);
1.149 + break;
1.150 + }
1.151 + }
1.152 +}
1.153 +
1.154 +/*
1.155 +** Implementation of the substr() function.
1.156 +**
1.157 +** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
1.158 +** p1 is 1-indexed. So substr(x,1,1) returns the first character
1.159 +** of x. If x is text, then we actually count UTF-8 characters.
1.160 +** If x is a blob, then we count bytes.
1.161 +**
1.162 +** If p1 is negative, then we begin abs(p1) from the end of x[].
1.163 +*/
1.164 +static void substrFunc(
1.165 + sqlite3_context *context,
1.166 + int argc,
1.167 + sqlite3_value **argv
1.168 +){
1.169 + const unsigned char *z;
1.170 + const unsigned char *z2;
1.171 + int len;
1.172 + int p0type;
1.173 + i64 p1, p2;
1.174 +
1.175 + assert( argc==3 );
1.176 + p0type = sqlite3_value_type(argv[0]);
1.177 + if( p0type==SQLITE_BLOB ){
1.178 + len = sqlite3_value_bytes(argv[0]);
1.179 + z = sqlite3_value_blob(argv[0]);
1.180 + if( z==0 ) return;
1.181 + assert( len==sqlite3_value_bytes(argv[0]) );
1.182 + }else{
1.183 + z = sqlite3_value_text(argv[0]);
1.184 + if( z==0 ) return;
1.185 + len = 0;
1.186 + for(z2=z; *z2; len++){
1.187 + SQLITE_SKIP_UTF8(z2);
1.188 + }
1.189 + }
1.190 + p1 = sqlite3_value_int(argv[1]);
1.191 + p2 = sqlite3_value_int(argv[2]);
1.192 + if( p1<0 ){
1.193 + p1 += len;
1.194 + if( p1<0 ){
1.195 + p2 += p1;
1.196 + p1 = 0;
1.197 + }
1.198 + }else if( p1>0 ){
1.199 + p1--;
1.200 + }
1.201 + if( p1+p2>len ){
1.202 + p2 = len-p1;
1.203 + }
1.204 + if( p0type!=SQLITE_BLOB ){
1.205 + while( *z && p1 ){
1.206 + SQLITE_SKIP_UTF8(z);
1.207 + p1--;
1.208 + }
1.209 + for(z2=z; *z2 && p2; p2--){
1.210 + SQLITE_SKIP_UTF8(z2);
1.211 + }
1.212 + sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT);
1.213 + }else{
1.214 + if( p2<0 ) p2 = 0;
1.215 + sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
1.216 + }
1.217 +}
1.218 +
1.219 +/*
1.220 +** Implementation of the round() function
1.221 +*/
1.222 +static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1.223 + int n = 0;
1.224 + double r;
1.225 + char zBuf[500]; /* larger than the %f representation of the largest double */
1.226 + assert( argc==1 || argc==2 );
1.227 + if( argc==2 ){
1.228 + if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
1.229 + n = sqlite3_value_int(argv[1]);
1.230 + if( n>30 ) n = 30;
1.231 + if( n<0 ) n = 0;
1.232 + }
1.233 + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1.234 + r = sqlite3_value_double(argv[0]);
1.235 + sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
1.236 + sqlite3AtoF(zBuf, &r);
1.237 + sqlite3_result_double(context, r);
1.238 +}
1.239 +
1.240 +/*
1.241 +** Allocate nByte bytes of space using sqlite3_malloc(). If the
1.242 +** allocation fails, call sqlite3_result_error_nomem() to notify
1.243 +** the database handle that malloc() has failed.
1.244 +*/
1.245 +static void *contextMalloc(sqlite3_context *context, int nByte){
1.246 + char *z = sqlite3_malloc(nByte);
1.247 + if( !z && nByte>0 ){
1.248 + sqlite3_result_error_nomem(context);
1.249 + }
1.250 + return z;
1.251 +}
1.252 +
1.253 +/*
1.254 +** Implementation of the upper() and lower() SQL functions.
1.255 +*/
1.256 +static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1.257 + char *z1;
1.258 + const char *z2;
1.259 + int i, n;
1.260 + if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
1.261 + z2 = (char*)sqlite3_value_text(argv[0]);
1.262 + n = sqlite3_value_bytes(argv[0]);
1.263 + /* Verify that the call to _bytes() does not invalidate the _text() pointer */
1.264 + assert( z2==(char*)sqlite3_value_text(argv[0]) );
1.265 + if( z2 ){
1.266 + z1 = contextMalloc(context, n+1);
1.267 + if( z1 ){
1.268 + memcpy(z1, z2, n+1);
1.269 + for(i=0; z1[i]; i++){
1.270 + z1[i] = toupper(z1[i]);
1.271 + }
1.272 + sqlite3_result_text(context, z1, -1, sqlite3_free);
1.273 + }
1.274 + }
1.275 +}
1.276 +static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1.277 + char *z1;
1.278 + const char *z2;
1.279 + int i, n;
1.280 + if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
1.281 + z2 = (char*)sqlite3_value_text(argv[0]);
1.282 + n = sqlite3_value_bytes(argv[0]);
1.283 + /* Verify that the call to _bytes() does not invalidate the _text() pointer */
1.284 + assert( z2==(char*)sqlite3_value_text(argv[0]) );
1.285 + if( z2 ){
1.286 + z1 = contextMalloc(context, n+1);
1.287 + if( z1 ){
1.288 + memcpy(z1, z2, n+1);
1.289 + for(i=0; z1[i]; i++){
1.290 + z1[i] = tolower(z1[i]);
1.291 + }
1.292 + sqlite3_result_text(context, z1, -1, sqlite3_free);
1.293 + }
1.294 + }
1.295 +}
1.296 +
1.297 +/*
1.298 +** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
1.299 +** All three do the same thing. They return the first non-NULL
1.300 +** argument.
1.301 +*/
1.302 +static void ifnullFunc(
1.303 + sqlite3_context *context,
1.304 + int argc,
1.305 + sqlite3_value **argv
1.306 +){
1.307 + int i;
1.308 + for(i=0; i<argc; i++){
1.309 + if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
1.310 + sqlite3_result_value(context, argv[i]);
1.311 + break;
1.312 + }
1.313 + }
1.314 +}
1.315 +
1.316 +/*
1.317 +** Implementation of random(). Return a random integer.
1.318 +*/
1.319 +static void randomFunc(
1.320 + sqlite3_context *context,
1.321 + int argc,
1.322 + sqlite3_value **argv
1.323 +){
1.324 + sqlite_int64 r;
1.325 + sqlite3Randomness(sizeof(r), &r);
1.326 + if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */
1.327 + /* can always do abs() of the result */
1.328 + sqlite3_result_int64(context, r);
1.329 +}
1.330 +
1.331 +/*
1.332 +** Implementation of randomblob(N). Return a random blob
1.333 +** that is N bytes long.
1.334 +*/
1.335 +static void randomBlob(
1.336 + sqlite3_context *context,
1.337 + int argc,
1.338 + sqlite3_value **argv
1.339 +){
1.340 + int n;
1.341 + unsigned char *p;
1.342 + assert( argc==1 );
1.343 + n = sqlite3_value_int(argv[0]);
1.344 + if( n<1 ){
1.345 + n = 1;
1.346 + }
1.347 + if( n>SQLITE_MAX_LENGTH ){
1.348 + sqlite3_result_error_toobig(context);
1.349 + return;
1.350 + }
1.351 + p = contextMalloc(context, n);
1.352 + if( p ){
1.353 + sqlite3Randomness(n, p);
1.354 + sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
1.355 + }
1.356 +}
1.357 +
1.358 +/*
1.359 +** Implementation of the last_insert_rowid() SQL function. The return
1.360 +** value is the same as the sqlite3_last_insert_rowid() API function.
1.361 +*/
1.362 +static void last_insert_rowid(
1.363 + sqlite3_context *context,
1.364 + int arg,
1.365 + sqlite3_value **argv
1.366 +){
1.367 + sqlite3 *db = sqlite3_user_data(context);
1.368 + sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
1.369 +}
1.370 +
1.371 +/*
1.372 +** Implementation of the changes() SQL function. The return value is the
1.373 +** same as the sqlite3_changes() API function.
1.374 +*/
1.375 +static void changes(
1.376 + sqlite3_context *context,
1.377 + int arg,
1.378 + sqlite3_value **argv
1.379 +){
1.380 + sqlite3 *db = sqlite3_user_data(context);
1.381 + sqlite3_result_int(context, sqlite3_changes(db));
1.382 +}
1.383 +
1.384 +/*
1.385 +** Implementation of the total_changes() SQL function. The return value is
1.386 +** the same as the sqlite3_total_changes() API function.
1.387 +*/
1.388 +static void total_changes(
1.389 + sqlite3_context *context,
1.390 + int arg,
1.391 + sqlite3_value **argv
1.392 +){
1.393 + sqlite3 *db = sqlite3_user_data(context);
1.394 + sqlite3_result_int(context, sqlite3_total_changes(db));
1.395 +}
1.396 +
1.397 +/*
1.398 +** A structure defining how to do GLOB-style comparisons.
1.399 +*/
1.400 +struct compareInfo {
1.401 + u8 matchAll;
1.402 + u8 matchOne;
1.403 + u8 matchSet;
1.404 + u8 noCase;
1.405 +};
1.406 +
1.407 +static const struct compareInfo globInfo = { '*', '?', '[', 0 };
1.408 +/* The correct SQL-92 behavior is for the LIKE operator to ignore
1.409 +** case. Thus 'a' LIKE 'A' would be true. */
1.410 +static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
1.411 +/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
1.412 +** is case sensitive causing 'a' LIKE 'A' to be false */
1.413 +static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
1.414 +
1.415 +/*
1.416 +** Compare two UTF-8 strings for equality where the first string can
1.417 +** potentially be a "glob" expression. Return true (1) if they
1.418 +** are the same and false (0) if they are different.
1.419 +**
1.420 +** Globbing rules:
1.421 +**
1.422 +** '*' Matches any sequence of zero or more characters.
1.423 +**
1.424 +** '?' Matches exactly one character.
1.425 +**
1.426 +** [...] Matches one character from the enclosed list of
1.427 +** characters.
1.428 +**
1.429 +** [^...] Matches one character not in the enclosed list.
1.430 +**
1.431 +** With the [...] and [^...] matching, a ']' character can be included
1.432 +** in the list by making it the first character after '[' or '^'. A
1.433 +** range of characters can be specified using '-'. Example:
1.434 +** "[a-z]" matches any single lower-case letter. To match a '-', make
1.435 +** it the last character in the list.
1.436 +**
1.437 +** This routine is usually quick, but can be N**2 in the worst case.
1.438 +**
1.439 +** Hints: to match '*' or '?', put them in "[]". Like this:
1.440 +**
1.441 +** abc[*]xyz Matches "abc*xyz" only
1.442 +*/
1.443 +static int patternCompare(
1.444 + const u8 *zPattern, /* The glob pattern */
1.445 + const u8 *zString, /* The string to compare against the glob */
1.446 + const struct compareInfo *pInfo, /* Information about how to do the compare */
1.447 + const int esc /* The escape character */
1.448 +){
1.449 + int c, c2;
1.450 + int invert;
1.451 + int seen;
1.452 + u8 matchOne = pInfo->matchOne;
1.453 + u8 matchAll = pInfo->matchAll;
1.454 + u8 matchSet = pInfo->matchSet;
1.455 + u8 noCase = pInfo->noCase;
1.456 + int prevEscape = 0; /* True if the previous character was 'escape' */
1.457 +
1.458 + while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){
1.459 + if( !prevEscape && c==matchAll ){
1.460 + while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll
1.461 + || c == matchOne ){
1.462 + if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){
1.463 + return 0;
1.464 + }
1.465 + }
1.466 + if( c==0 ){
1.467 + return 1;
1.468 + }else if( c==esc ){
1.469 + c = sqlite3Utf8Read(zPattern, 0, &zPattern);
1.470 + if( c==0 ){
1.471 + return 0;
1.472 + }
1.473 + }else if( c==matchSet ){
1.474 + assert( esc==0 ); /* This is GLOB, not LIKE */
1.475 + assert( matchSet<0x80 ); /* '[' is a single-byte character */
1.476 + while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
1.477 + SQLITE_SKIP_UTF8(zString);
1.478 + }
1.479 + return *zString!=0;
1.480 + }
1.481 + while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){
1.482 + if( noCase ){
1.483 + c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2;
1.484 + c = c<0x80 ? sqlite3UpperToLower[c] : c;
1.485 + while( c2 != 0 && c2 != c ){
1.486 + c2 = sqlite3Utf8Read(zString, 0, &zString);
1.487 + if( c2<0x80 ) c2 = sqlite3UpperToLower[c2];
1.488 + }
1.489 + }else{
1.490 + while( c2 != 0 && c2 != c ){
1.491 + c2 = sqlite3Utf8Read(zString, 0, &zString);
1.492 + }
1.493 + }
1.494 + if( c2==0 ) return 0;
1.495 + if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
1.496 + }
1.497 + return 0;
1.498 + }else if( !prevEscape && c==matchOne ){
1.499 + if( sqlite3Utf8Read(zString, 0, &zString)==0 ){
1.500 + return 0;
1.501 + }
1.502 + }else if( c==matchSet ){
1.503 + int prior_c = 0;
1.504 + assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
1.505 + seen = 0;
1.506 + invert = 0;
1.507 + c = sqlite3Utf8Read(zString, 0, &zString);
1.508 + if( c==0 ) return 0;
1.509 + c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
1.510 + if( c2=='^' ){
1.511 + invert = 1;
1.512 + c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
1.513 + }
1.514 + if( c2==']' ){
1.515 + if( c==']' ) seen = 1;
1.516 + c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
1.517 + }
1.518 + while( c2 && c2!=']' ){
1.519 + if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
1.520 + c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
1.521 + if( c>=prior_c && c<=c2 ) seen = 1;
1.522 + prior_c = 0;
1.523 + }else{
1.524 + if( c==c2 ){
1.525 + seen = 1;
1.526 + }
1.527 + prior_c = c2;
1.528 + }
1.529 + c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
1.530 + }
1.531 + if( c2==0 || (seen ^ invert)==0 ){
1.532 + return 0;
1.533 + }
1.534 + }else if( esc==c && !prevEscape ){
1.535 + prevEscape = 1;
1.536 + }else{
1.537 + c2 = sqlite3Utf8Read(zString, 0, &zString);
1.538 + if( noCase ){
1.539 + c = c<0x80 ? sqlite3UpperToLower[c] : c;
1.540 + c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2;
1.541 + }
1.542 + if( c!=c2 ){
1.543 + return 0;
1.544 + }
1.545 + prevEscape = 0;
1.546 + }
1.547 + }
1.548 + return *zString==0;
1.549 +}
1.550 +
1.551 +/*
1.552 +** Count the number of times that the LIKE operator (or GLOB which is
1.553 +** just a variation of LIKE) gets called. This is used for testing
1.554 +** only.
1.555 +*/
1.556 +#ifdef SQLITE_TEST
1.557 +int sqlite3_like_count = 0;
1.558 +#endif
1.559 +
1.560 +
1.561 +/*
1.562 +** Implementation of the like() SQL function. This function implements
1.563 +** the build-in LIKE operator. The first argument to the function is the
1.564 +** pattern and the second argument is the string. So, the SQL statements:
1.565 +**
1.566 +** A LIKE B
1.567 +**
1.568 +** is implemented as like(B,A).
1.569 +**
1.570 +** This same function (with a different compareInfo structure) computes
1.571 +** the GLOB operator.
1.572 +*/
1.573 +static void likeFunc(
1.574 + sqlite3_context *context,
1.575 + int argc,
1.576 + sqlite3_value **argv
1.577 +){
1.578 + const unsigned char *zA, *zB;
1.579 + int escape = 0;
1.580 +
1.581 + zB = sqlite3_value_text(argv[0]);
1.582 + zA = sqlite3_value_text(argv[1]);
1.583 +
1.584 + /* Limit the length of the LIKE or GLOB pattern to avoid problems
1.585 + ** of deep recursion and N*N behavior in patternCompare().
1.586 + */
1.587 + if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
1.588 + sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
1.589 + return;
1.590 + }
1.591 + assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
1.592 +
1.593 + if( argc==3 ){
1.594 + /* The escape character string must consist of a single UTF-8 character.
1.595 + ** Otherwise, return an error.
1.596 + */
1.597 + const unsigned char *zEsc = sqlite3_value_text(argv[2]);
1.598 + if( zEsc==0 ) return;
1.599 + if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
1.600 + sqlite3_result_error(context,
1.601 + "ESCAPE expression must be a single character", -1);
1.602 + return;
1.603 + }
1.604 + escape = sqlite3Utf8Read(zEsc, 0, &zEsc);
1.605 + }
1.606 + if( zA && zB ){
1.607 + struct compareInfo *pInfo = sqlite3_user_data(context);
1.608 +#ifdef SQLITE_TEST
1.609 + sqlite3_like_count++;
1.610 +#endif
1.611 +
1.612 + sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
1.613 + }
1.614 +}
1.615 +
1.616 +/*
1.617 +** Implementation of the NULLIF(x,y) function. The result is the first
1.618 +** argument if the arguments are different. The result is NULL if the
1.619 +** arguments are equal to each other.
1.620 +*/
1.621 +static void nullifFunc(
1.622 + sqlite3_context *context,
1.623 + int argc,
1.624 + sqlite3_value **argv
1.625 +){
1.626 + CollSeq *pColl = sqlite3GetFuncCollSeq(context);
1.627 + if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
1.628 + sqlite3_result_value(context, argv[0]);
1.629 + }
1.630 +}
1.631 +
1.632 +/*
1.633 +** Implementation of the VERSION(*) function. The result is the version
1.634 +** of the SQLite library that is running.
1.635 +*/
1.636 +static void versionFunc(
1.637 + sqlite3_context *context,
1.638 + int argc,
1.639 + sqlite3_value **argv
1.640 +){
1.641 + sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
1.642 +}
1.643 +
1.644 +/* Array for converting from half-bytes (nybbles) into ASCII hex
1.645 +** digits. */
1.646 +static const char hexdigits[] = {
1.647 + '0', '1', '2', '3', '4', '5', '6', '7',
1.648 + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
1.649 +};
1.650 +
1.651 +/*
1.652 +** EXPERIMENTAL - This is not an official function. The interface may
1.653 +** change. This function may disappear. Do not write code that depends
1.654 +** on this function.
1.655 +**
1.656 +** Implementation of the QUOTE() function. This function takes a single
1.657 +** argument. If the argument is numeric, the return value is the same as
1.658 +** the argument. If the argument is NULL, the return value is the string
1.659 +** "NULL". Otherwise, the argument is enclosed in single quotes with
1.660 +** single-quote escapes.
1.661 +*/
1.662 +static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1.663 + if( argc<1 ) return;
1.664 + switch( sqlite3_value_type(argv[0]) ){
1.665 + case SQLITE_NULL: {
1.666 + sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
1.667 + break;
1.668 + }
1.669 + case SQLITE_INTEGER:
1.670 + case SQLITE_FLOAT: {
1.671 + sqlite3_result_value(context, argv[0]);
1.672 + break;
1.673 + }
1.674 + case SQLITE_BLOB: {
1.675 + char *zText = 0;
1.676 + char const *zBlob = sqlite3_value_blob(argv[0]);
1.677 + int nBlob = sqlite3_value_bytes(argv[0]);
1.678 + assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
1.679 +
1.680 + if( 2*nBlob+4>SQLITE_MAX_LENGTH ){
1.681 + sqlite3_result_error_toobig(context);
1.682 + return;
1.683 + }
1.684 + zText = (char *)contextMalloc(context, (2*nBlob)+4);
1.685 + if( zText ){
1.686 + int i;
1.687 + for(i=0; i<nBlob; i++){
1.688 + zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
1.689 + zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
1.690 + }
1.691 + zText[(nBlob*2)+2] = '\'';
1.692 + zText[(nBlob*2)+3] = '\0';
1.693 + zText[0] = 'X';
1.694 + zText[1] = '\'';
1.695 + sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
1.696 + sqlite3_free(zText);
1.697 + }
1.698 + break;
1.699 + }
1.700 + case SQLITE_TEXT: {
1.701 + int i,j;
1.702 + u64 n;
1.703 + const unsigned char *zArg = sqlite3_value_text(argv[0]);
1.704 + char *z;
1.705 +
1.706 + if( zArg==0 ) return;
1.707 + for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
1.708 + if( i+n+3>SQLITE_MAX_LENGTH ){
1.709 + sqlite3_result_error_toobig(context);
1.710 + return;
1.711 + }
1.712 + z = contextMalloc(context, i+n+3);
1.713 + if( z ){
1.714 + z[0] = '\'';
1.715 + for(i=0, j=1; zArg[i]; i++){
1.716 + z[j++] = zArg[i];
1.717 + if( zArg[i]=='\'' ){
1.718 + z[j++] = '\'';
1.719 + }
1.720 + }
1.721 + z[j++] = '\'';
1.722 + z[j] = 0;
1.723 + sqlite3_result_text(context, z, j, sqlite3_free);
1.724 + }
1.725 + }
1.726 + }
1.727 +}
1.728 +
1.729 +/*
1.730 +** The hex() function. Interpret the argument as a blob. Return
1.731 +** a hexadecimal rendering as text.
1.732 +*/
1.733 +static void hexFunc(
1.734 + sqlite3_context *context,
1.735 + int argc,
1.736 + sqlite3_value **argv
1.737 +){
1.738 + int i, n;
1.739 + const unsigned char *pBlob;
1.740 + char *zHex, *z;
1.741 + assert( argc==1 );
1.742 + pBlob = sqlite3_value_blob(argv[0]);
1.743 + n = sqlite3_value_bytes(argv[0]);
1.744 + if( n*2+1>SQLITE_MAX_LENGTH ){
1.745 + sqlite3_result_error_toobig(context);
1.746 + return;
1.747 + }
1.748 + assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
1.749 + z = zHex = contextMalloc(context, n*2 + 1);
1.750 + if( zHex ){
1.751 + for(i=0; i<n; i++, pBlob++){
1.752 + unsigned char c = *pBlob;
1.753 + *(z++) = hexdigits[(c>>4)&0xf];
1.754 + *(z++) = hexdigits[c&0xf];
1.755 + }
1.756 + *z = 0;
1.757 + sqlite3_result_text(context, zHex, n*2, sqlite3_free);
1.758 + }
1.759 +}
1.760 +
1.761 +/*
1.762 +** The zeroblob(N) function returns a zero-filled blob of size N bytes.
1.763 +*/
1.764 +static void zeroblobFunc(
1.765 + sqlite3_context *context,
1.766 + int argc,
1.767 + sqlite3_value **argv
1.768 +){
1.769 + i64 n;
1.770 + assert( argc==1 );
1.771 + n = sqlite3_value_int64(argv[0]);
1.772 + if( n>SQLITE_MAX_LENGTH ){
1.773 + sqlite3_result_error_toobig(context);
1.774 + }else{
1.775 + sqlite3_result_zeroblob(context, n);
1.776 + }
1.777 +}
1.778 +
1.779 +/*
1.780 +** The replace() function. Three arguments are all strings: call
1.781 +** them A, B, and C. The result is also a string which is derived
1.782 +** from A by replacing every occurance of B with C. The match
1.783 +** must be exact. Collating sequences are not used.
1.784 +*/
1.785 +static void replaceFunc(
1.786 + sqlite3_context *context,
1.787 + int argc,
1.788 + sqlite3_value **argv
1.789 +){
1.790 + const unsigned char *zStr; /* The input string A */
1.791 + const unsigned char *zPattern; /* The pattern string B */
1.792 + const unsigned char *zRep; /* The replacement string C */
1.793 + unsigned char *zOut; /* The output */
1.794 + int nStr; /* Size of zStr */
1.795 + int nPattern; /* Size of zPattern */
1.796 + int nRep; /* Size of zRep */
1.797 + i64 nOut; /* Maximum size of zOut */
1.798 + int loopLimit; /* Last zStr[] that might match zPattern[] */
1.799 + int i, j; /* Loop counters */
1.800 +
1.801 + assert( argc==3 );
1.802 + zStr = sqlite3_value_text(argv[0]);
1.803 + if( zStr==0 ) return;
1.804 + nStr = sqlite3_value_bytes(argv[0]);
1.805 + assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
1.806 + zPattern = sqlite3_value_text(argv[1]);
1.807 + if( zPattern==0 || zPattern[0]==0 ) return;
1.808 + nPattern = sqlite3_value_bytes(argv[1]);
1.809 + assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
1.810 + zRep = sqlite3_value_text(argv[2]);
1.811 + if( zRep==0 ) return;
1.812 + nRep = sqlite3_value_bytes(argv[2]);
1.813 + assert( zRep==sqlite3_value_text(argv[2]) );
1.814 + nOut = nStr + 1;
1.815 + assert( nOut<SQLITE_MAX_LENGTH );
1.816 + zOut = contextMalloc(context, (int)nOut);
1.817 + if( zOut==0 ){
1.818 + return;
1.819 + }
1.820 + loopLimit = nStr - nPattern;
1.821 + for(i=j=0; i<=loopLimit; i++){
1.822 + if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
1.823 + zOut[j++] = zStr[i];
1.824 + }else{
1.825 + u8 *zOld;
1.826 + nOut += nRep - nPattern;
1.827 + if( nOut>=SQLITE_MAX_LENGTH ){
1.828 + sqlite3_result_error_toobig(context);
1.829 + sqlite3_free(zOut);
1.830 + return;
1.831 + }
1.832 + zOld = zOut;
1.833 + zOut = sqlite3_realloc(zOut, (int)nOut);
1.834 + if( zOut==0 ){
1.835 + sqlite3_result_error_nomem(context);
1.836 + sqlite3_free(zOld);
1.837 + return;
1.838 + }
1.839 + memcpy(&zOut[j], zRep, nRep);
1.840 + j += nRep;
1.841 + i += nPattern-1;
1.842 + }
1.843 + }
1.844 + assert( j+nStr-i+1==nOut );
1.845 + memcpy(&zOut[j], &zStr[i], nStr-i);
1.846 + j += nStr - i;
1.847 + assert( j<=nOut );
1.848 + zOut[j] = 0;
1.849 + sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
1.850 +}
1.851 +
1.852 +/*
1.853 +** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
1.854 +** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
1.855 +*/
1.856 +static void trimFunc(
1.857 + sqlite3_context *context,
1.858 + int argc,
1.859 + sqlite3_value **argv
1.860 +){
1.861 + const unsigned char *zIn; /* Input string */
1.862 + const unsigned char *zCharSet; /* Set of characters to trim */
1.863 + int nIn; /* Number of bytes in input */
1.864 + int flags; /* 1: trimleft 2: trimright 3: trim */
1.865 + int i; /* Loop counter */
1.866 + unsigned char *aLen; /* Length of each character in zCharSet */
1.867 + const unsigned char **azChar; /* Individual characters in zCharSet */
1.868 + int nChar; /* Number of characters in zCharSet */
1.869 +
1.870 + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1.871 + return;
1.872 + }
1.873 + zIn = sqlite3_value_text(argv[0]);
1.874 + if( zIn==0 ) return;
1.875 + nIn = sqlite3_value_bytes(argv[0]);
1.876 + assert( zIn==sqlite3_value_text(argv[0]) );
1.877 + if( argc==1 ){
1.878 + static const unsigned char lenOne[] = { 1 };
1.879 + static const unsigned char *azOne[] = { (u8*)" " };
1.880 + nChar = 1;
1.881 + aLen = (u8*)lenOne;
1.882 + azChar = azOne;
1.883 + zCharSet = 0;
1.884 + }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
1.885 + return;
1.886 + }else{
1.887 + const unsigned char *z;
1.888 + for(z=zCharSet, nChar=0; *z; nChar++){
1.889 + SQLITE_SKIP_UTF8(z);
1.890 + }
1.891 + if( nChar>0 ){
1.892 + azChar = contextMalloc(context, nChar*(sizeof(char*)+1));
1.893 + if( azChar==0 ){
1.894 + return;
1.895 + }
1.896 + aLen = (unsigned char*)&azChar[nChar];
1.897 + for(z=zCharSet, nChar=0; *z; nChar++){
1.898 + azChar[nChar] = z;
1.899 + SQLITE_SKIP_UTF8(z);
1.900 + aLen[nChar] = z - azChar[nChar];
1.901 + }
1.902 + }
1.903 + }
1.904 + if( nChar>0 ){
1.905 + flags = (int)sqlite3_user_data(context);
1.906 + if( flags & 1 ){
1.907 + while( nIn>0 ){
1.908 + int len;
1.909 + for(i=0; i<nChar; i++){
1.910 + len = aLen[i];
1.911 + if( memcmp(zIn, azChar[i], len)==0 ) break;
1.912 + }
1.913 + if( i>=nChar ) break;
1.914 + zIn += len;
1.915 + nIn -= len;
1.916 + }
1.917 + }
1.918 + if( flags & 2 ){
1.919 + while( nIn>0 ){
1.920 + int len;
1.921 + for(i=0; i<nChar; i++){
1.922 + len = aLen[i];
1.923 + if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
1.924 + }
1.925 + if( i>=nChar ) break;
1.926 + nIn -= len;
1.927 + }
1.928 + }
1.929 + if( zCharSet ){
1.930 + sqlite3_free(azChar);
1.931 + }
1.932 + }
1.933 + sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
1.934 +}
1.935 +
1.936 +#ifdef SQLITE_SOUNDEX
1.937 +/*
1.938 +** Compute the soundex encoding of a word.
1.939 +*/
1.940 +static void soundexFunc(
1.941 + sqlite3_context *context,
1.942 + int argc,
1.943 + sqlite3_value **argv
1.944 +){
1.945 + char zResult[8];
1.946 + const u8 *zIn;
1.947 + int i, j;
1.948 + static const unsigned char iCode[] = {
1.949 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1.950 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1.951 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1.952 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1.953 + 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1.954 + 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1.955 + 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1.956 + 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1.957 + };
1.958 + assert( argc==1 );
1.959 + zIn = (u8*)sqlite3_value_text(argv[0]);
1.960 + if( zIn==0 ) zIn = (u8*)"";
1.961 + for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
1.962 + if( zIn[i] ){
1.963 + u8 prevcode = iCode[zIn[i]&0x7f];
1.964 + zResult[0] = toupper(zIn[i]);
1.965 + for(j=1; j<4 && zIn[i]; i++){
1.966 + int code = iCode[zIn[i]&0x7f];
1.967 + if( code>0 ){
1.968 + if( code!=prevcode ){
1.969 + prevcode = code;
1.970 + zResult[j++] = code + '0';
1.971 + }
1.972 + }else{
1.973 + prevcode = 0;
1.974 + }
1.975 + }
1.976 + while( j<4 ){
1.977 + zResult[j++] = '0';
1.978 + }
1.979 + zResult[j] = 0;
1.980 + sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
1.981 + }else{
1.982 + sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
1.983 + }
1.984 +}
1.985 +#endif
1.986 +
1.987 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
1.988 +/*
1.989 +** A function that loads a shared-library extension then returns NULL.
1.990 +*/
1.991 +static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
1.992 + const char *zFile = (const char *)sqlite3_value_text(argv[0]);
1.993 + const char *zProc;
1.994 + sqlite3 *db = sqlite3_user_data(context);
1.995 + char *zErrMsg = 0;
1.996 +
1.997 + if( argc==2 ){
1.998 + zProc = (const char *)sqlite3_value_text(argv[1]);
1.999 + }else{
1.1000 + zProc = 0;
1.1001 + }
1.1002 + if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
1.1003 + sqlite3_result_error(context, zErrMsg, -1);
1.1004 + sqlite3_free(zErrMsg);
1.1005 + }
1.1006 +}
1.1007 +#endif
1.1008 +
1.1009 +#ifdef SQLITE_TEST
1.1010 +/*
1.1011 +** This function generates a string of random characters. Used for
1.1012 +** generating test data.
1.1013 +*/
1.1014 +static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
1.1015 + static const unsigned char zSrc[] =
1.1016 + "abcdefghijklmnopqrstuvwxyz"
1.1017 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1.1018 + "0123456789"
1.1019 + ".-!,:*^+=_|?/<> ";
1.1020 + int iMin, iMax, n, r, i;
1.1021 + unsigned char zBuf[1000];
1.1022 +
1.1023 + /* It used to be possible to call randstr() with any number of arguments,
1.1024 + ** but now it is registered with SQLite as requiring exactly 2.
1.1025 + */
1.1026 + assert(argc==2);
1.1027 +
1.1028 + iMin = sqlite3_value_int(argv[0]);
1.1029 + if( iMin<0 ) iMin = 0;
1.1030 + if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
1.1031 + iMax = sqlite3_value_int(argv[1]);
1.1032 + if( iMax<iMin ) iMax = iMin;
1.1033 + if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
1.1034 + n = iMin;
1.1035 + if( iMax>iMin ){
1.1036 + sqlite3Randomness(sizeof(r), &r);
1.1037 + r &= 0x7fffffff;
1.1038 + n += r%(iMax + 1 - iMin);
1.1039 + }
1.1040 + assert( n<sizeof(zBuf) );
1.1041 + sqlite3Randomness(n, zBuf);
1.1042 + for(i=0; i<n; i++){
1.1043 + zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
1.1044 + }
1.1045 + zBuf[n] = 0;
1.1046 + sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
1.1047 +}
1.1048 +#endif /* SQLITE_TEST */
1.1049 +
1.1050 +#ifdef SQLITE_TEST
1.1051 +/*
1.1052 +** The following two SQL functions are used to test returning a text
1.1053 +** result with a destructor. Function 'test_destructor' takes one argument
1.1054 +** and returns the same argument interpreted as TEXT. A destructor is
1.1055 +** passed with the sqlite3_result_text() call.
1.1056 +**
1.1057 +** SQL function 'test_destructor_count' returns the number of outstanding
1.1058 +** allocations made by 'test_destructor';
1.1059 +**
1.1060 +** WARNING: Not threadsafe.
1.1061 +*/
1.1062 +static int test_destructor_count_var = 0;
1.1063 +static void destructor(void *p){
1.1064 + char *zVal = (char *)p;
1.1065 + assert(zVal);
1.1066 + zVal--;
1.1067 + sqlite3_free(zVal);
1.1068 + test_destructor_count_var--;
1.1069 +}
1.1070 +static void test_destructor(
1.1071 + sqlite3_context *pCtx,
1.1072 + int nArg,
1.1073 + sqlite3_value **argv
1.1074 +){
1.1075 + char *zVal;
1.1076 + int len;
1.1077 + sqlite3 *db = sqlite3_user_data(pCtx);
1.1078 +
1.1079 + test_destructor_count_var++;
1.1080 + assert( nArg==1 );
1.1081 + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1.1082 + len = sqlite3ValueBytes(argv[0], ENC(db));
1.1083 + zVal = contextMalloc(pCtx, len+3);
1.1084 + if( !zVal ){
1.1085 + return;
1.1086 + }
1.1087 + zVal[len+1] = 0;
1.1088 + zVal[len+2] = 0;
1.1089 + zVal++;
1.1090 + memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
1.1091 + if( ENC(db)==SQLITE_UTF8 ){
1.1092 + sqlite3_result_text(pCtx, zVal, -1, destructor);
1.1093 +#ifndef SQLITE_OMIT_UTF16
1.1094 + }else if( ENC(db)==SQLITE_UTF16LE ){
1.1095 + sqlite3_result_text16le(pCtx, zVal, -1, destructor);
1.1096 + }else{
1.1097 + sqlite3_result_text16be(pCtx, zVal, -1, destructor);
1.1098 +#endif /* SQLITE_OMIT_UTF16 */
1.1099 + }
1.1100 +}
1.1101 +static void test_destructor_count(
1.1102 + sqlite3_context *pCtx,
1.1103 + int nArg,
1.1104 + sqlite3_value **argv
1.1105 +){
1.1106 + sqlite3_result_int(pCtx, test_destructor_count_var);
1.1107 +}
1.1108 +#endif /* SQLITE_TEST */
1.1109 +
1.1110 +#ifdef SQLITE_TEST
1.1111 +/*
1.1112 +** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
1.1113 +** interface.
1.1114 +**
1.1115 +** The test_auxdata() SQL function attempts to register each of its arguments
1.1116 +** as auxiliary data. If there are no prior registrations of aux data for
1.1117 +** that argument (meaning the argument is not a constant or this is its first
1.1118 +** call) then the result for that argument is 0. If there is a prior
1.1119 +** registration, the result for that argument is 1. The overall result
1.1120 +** is the individual argument results separated by spaces.
1.1121 +*/
1.1122 +static void free_test_auxdata(void *p) {sqlite3_free(p);}
1.1123 +static void test_auxdata(
1.1124 + sqlite3_context *pCtx,
1.1125 + int nArg,
1.1126 + sqlite3_value **argv
1.1127 +){
1.1128 + int i;
1.1129 + char *zRet = contextMalloc(pCtx, nArg*2);
1.1130 + if( !zRet ) return;
1.1131 + memset(zRet, 0, nArg*2);
1.1132 + for(i=0; i<nArg; i++){
1.1133 + char const *z = (char*)sqlite3_value_text(argv[i]);
1.1134 + if( z ){
1.1135 + char *zAux = sqlite3_get_auxdata(pCtx, i);
1.1136 + if( zAux ){
1.1137 + zRet[i*2] = '1';
1.1138 + if( strcmp(zAux, z) ){
1.1139 + sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
1.1140 + return;
1.1141 + }
1.1142 + }else {
1.1143 + zRet[i*2] = '0';
1.1144 + }
1.1145 +
1.1146 + zAux = contextMalloc(pCtx, strlen(z)+1);
1.1147 + if( zAux ){
1.1148 + strcpy(zAux, z);
1.1149 + sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
1.1150 + }
1.1151 + zRet[i*2+1] = ' ';
1.1152 + }
1.1153 + }
1.1154 + sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
1.1155 +}
1.1156 +#endif /* SQLITE_TEST */
1.1157 +
1.1158 +#ifdef SQLITE_TEST
1.1159 +/*
1.1160 +** A function to test error reporting from user functions. This function
1.1161 +** returns a copy of it's first argument as an error.
1.1162 +*/
1.1163 +static void test_error(
1.1164 + sqlite3_context *pCtx,
1.1165 + int nArg,
1.1166 + sqlite3_value **argv
1.1167 +){
1.1168 + sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
1.1169 +}
1.1170 +#endif /* SQLITE_TEST */
1.1171 +
1.1172 +/*
1.1173 +** An instance of the following structure holds the context of a
1.1174 +** sum() or avg() aggregate computation.
1.1175 +*/
1.1176 +typedef struct SumCtx SumCtx;
1.1177 +struct SumCtx {
1.1178 + double rSum; /* Floating point sum */
1.1179 + i64 iSum; /* Integer sum */
1.1180 + i64 cnt; /* Number of elements summed */
1.1181 + u8 overflow; /* True if integer overflow seen */
1.1182 + u8 approx; /* True if non-integer value was input to the sum */
1.1183 +};
1.1184 +
1.1185 +/*
1.1186 +** Routines used to compute the sum, average, and total.
1.1187 +**
1.1188 +** The SUM() function follows the (broken) SQL standard which means
1.1189 +** that it returns NULL if it sums over no inputs. TOTAL returns
1.1190 +** 0.0 in that case. In addition, TOTAL always returns a float where
1.1191 +** SUM might return an integer if it never encounters a floating point
1.1192 +** value. TOTAL never fails, but SUM might through an exception if
1.1193 +** it overflows an integer.
1.1194 +*/
1.1195 +static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1.1196 + SumCtx *p;
1.1197 + int type;
1.1198 + assert( argc==1 );
1.1199 + p = sqlite3_aggregate_context(context, sizeof(*p));
1.1200 + type = sqlite3_value_numeric_type(argv[0]);
1.1201 + if( p && type!=SQLITE_NULL ){
1.1202 + p->cnt++;
1.1203 + if( type==SQLITE_INTEGER ){
1.1204 + i64 v = sqlite3_value_int64(argv[0]);
1.1205 + p->rSum += v;
1.1206 + if( (p->approx|p->overflow)==0 ){
1.1207 + i64 iNewSum = p->iSum + v;
1.1208 + int s1 = p->iSum >> (sizeof(i64)*8-1);
1.1209 + int s2 = v >> (sizeof(i64)*8-1);
1.1210 + int s3 = iNewSum >> (sizeof(i64)*8-1);
1.1211 + p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
1.1212 + p->iSum = iNewSum;
1.1213 + }
1.1214 + }else{
1.1215 + p->rSum += sqlite3_value_double(argv[0]);
1.1216 + p->approx = 1;
1.1217 + }
1.1218 + }
1.1219 +}
1.1220 +static void sumFinalize(sqlite3_context *context){
1.1221 + SumCtx *p;
1.1222 + p = sqlite3_aggregate_context(context, 0);
1.1223 + if( p && p->cnt>0 ){
1.1224 + if( p->overflow ){
1.1225 + sqlite3_result_error(context,"integer overflow",-1);
1.1226 + }else if( p->approx ){
1.1227 + sqlite3_result_double(context, p->rSum);
1.1228 + }else{
1.1229 + sqlite3_result_int64(context, p->iSum);
1.1230 + }
1.1231 + }
1.1232 +}
1.1233 +static void avgFinalize(sqlite3_context *context){
1.1234 + SumCtx *p;
1.1235 + p = sqlite3_aggregate_context(context, 0);
1.1236 + if( p && p->cnt>0 ){
1.1237 + sqlite3_result_double(context, p->rSum/(double)p->cnt);
1.1238 + }
1.1239 +}
1.1240 +static void totalFinalize(sqlite3_context *context){
1.1241 + SumCtx *p;
1.1242 + p = sqlite3_aggregate_context(context, 0);
1.1243 + sqlite3_result_double(context, p ? p->rSum : 0.0);
1.1244 +}
1.1245 +
1.1246 +/*
1.1247 +** The following structure keeps track of state information for the
1.1248 +** count() aggregate function.
1.1249 +*/
1.1250 +typedef struct CountCtx CountCtx;
1.1251 +struct CountCtx {
1.1252 + i64 n;
1.1253 +};
1.1254 +
1.1255 +/*
1.1256 +** Routines to implement the count() aggregate function.
1.1257 +*/
1.1258 +static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1.1259 + CountCtx *p;
1.1260 + p = sqlite3_aggregate_context(context, sizeof(*p));
1.1261 + if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
1.1262 + p->n++;
1.1263 + }
1.1264 +}
1.1265 +static void countFinalize(sqlite3_context *context){
1.1266 + CountCtx *p;
1.1267 + p = sqlite3_aggregate_context(context, 0);
1.1268 + sqlite3_result_int64(context, p ? p->n : 0);
1.1269 +}
1.1270 +
1.1271 +/*
1.1272 +** Routines to implement min() and max() aggregate functions.
1.1273 +*/
1.1274 +static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1.1275 + Mem *pArg = (Mem *)argv[0];
1.1276 + Mem *pBest;
1.1277 +
1.1278 + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1.1279 + pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
1.1280 + if( !pBest ) return;
1.1281 +
1.1282 + if( pBest->flags ){
1.1283 + int max;
1.1284 + int cmp;
1.1285 + CollSeq *pColl = sqlite3GetFuncCollSeq(context);
1.1286 + /* This step function is used for both the min() and max() aggregates,
1.1287 + ** the only difference between the two being that the sense of the
1.1288 + ** comparison is inverted. For the max() aggregate, the
1.1289 + ** sqlite3_user_data() function returns (void *)-1. For min() it
1.1290 + ** returns (void *)db, where db is the sqlite3* database pointer.
1.1291 + ** Therefore the next statement sets variable 'max' to 1 for the max()
1.1292 + ** aggregate, or 0 for min().
1.1293 + */
1.1294 + max = sqlite3_user_data(context)!=0;
1.1295 + cmp = sqlite3MemCompare(pBest, pArg, pColl);
1.1296 + if( (max && cmp<0) || (!max && cmp>0) ){
1.1297 + sqlite3VdbeMemCopy(pBest, pArg);
1.1298 + }
1.1299 + }else{
1.1300 + sqlite3VdbeMemCopy(pBest, pArg);
1.1301 + }
1.1302 +}
1.1303 +static void minMaxFinalize(sqlite3_context *context){
1.1304 + sqlite3_value *pRes;
1.1305 + pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
1.1306 + if( pRes ){
1.1307 + if( pRes->flags ){
1.1308 + sqlite3_result_value(context, pRes);
1.1309 + }
1.1310 + sqlite3VdbeMemRelease(pRes);
1.1311 + }
1.1312 +}
1.1313 +
1.1314 +
1.1315 +/*
1.1316 +** This function registered all of the above C functions as SQL
1.1317 +** functions. This should be the only routine in this file with
1.1318 +** external linkage.
1.1319 +*/
1.1320 +void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
1.1321 + static const struct {
1.1322 + char *zName;
1.1323 + signed char nArg;
1.1324 + u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */
1.1325 + u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
1.1326 + u8 needCollSeq;
1.1327 + void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
1.1328 + } aFuncs[] = {
1.1329 + { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
1.1330 + { "min", 0, 0, SQLITE_UTF8, 1, 0 },
1.1331 + { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc },
1.1332 + { "max", 0, 1, SQLITE_UTF8, 1, 0 },
1.1333 + { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
1.1334 + { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
1.1335 + { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
1.1336 + { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
1.1337 + { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
1.1338 + { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
1.1339 + { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
1.1340 + { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
1.1341 + { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
1.1342 + { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
1.1343 + { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
1.1344 + { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc },
1.1345 + { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
1.1346 + { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
1.1347 + { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob },
1.1348 + { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
1.1349 + { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
1.1350 + { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
1.1351 + { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid },
1.1352 + { "changes", 0, 0xff, SQLITE_UTF8, 0, changes },
1.1353 + { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes },
1.1354 + { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc },
1.1355 + { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc },
1.1356 + { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc },
1.1357 + { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc },
1.1358 + { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc },
1.1359 + { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc },
1.1360 + { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc },
1.1361 + { "zeroblob", 1, 0, SQLITE_UTF8, 0, zeroblobFunc },
1.1362 +#ifdef SQLITE_SOUNDEX
1.1363 + { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
1.1364 +#endif
1.1365 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
1.1366 + { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt },
1.1367 + { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt },
1.1368 +#endif
1.1369 +#ifdef SQLITE_TEST
1.1370 + { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
1.1371 + { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor},
1.1372 + { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
1.1373 + { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
1.1374 + { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
1.1375 +#endif
1.1376 + };
1.1377 + static const struct {
1.1378 + char *zName;
1.1379 + signed char nArg;
1.1380 + u8 argType;
1.1381 + u8 needCollSeq;
1.1382 + void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1.1383 + void (*xFinalize)(sqlite3_context*);
1.1384 + } aAggs[] = {
1.1385 + { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1.1386 + { "max", 1, 1, 1, minmaxStep, minMaxFinalize },
1.1387 + { "sum", 1, 0, 0, sumStep, sumFinalize },
1.1388 + { "total", 1, 0, 0, sumStep, totalFinalize },
1.1389 + { "avg", 1, 0, 0, sumStep, avgFinalize },
1.1390 + { "count", 0, 0, 0, countStep, countFinalize },
1.1391 + { "count", 1, 0, 0, countStep, countFinalize },
1.1392 + };
1.1393 + int i;
1.1394 +
1.1395 + for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1.1396 + void *pArg;
1.1397 + u8 argType = aFuncs[i].argType;
1.1398 + if( argType==0xff ){
1.1399 + pArg = db;
1.1400 + }else{
1.1401 + pArg = (void*)(int)argType;
1.1402 + }
1.1403 + sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
1.1404 + aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
1.1405 + if( aFuncs[i].needCollSeq ){
1.1406 + FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1.1407 + strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1.1408 + if( pFunc && aFuncs[i].needCollSeq ){
1.1409 + pFunc->needCollSeq = 1;
1.1410 + }
1.1411 + }
1.1412 + }
1.1413 +#ifndef SQLITE_OMIT_ALTERTABLE
1.1414 + sqlite3AlterFunctions(db);
1.1415 +#endif
1.1416 +#ifndef SQLITE_OMIT_PARSER
1.1417 + sqlite3AttachFunctions(db);
1.1418 +#endif
1.1419 + for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
1.1420 + void *pArg = (void*)(int)aAggs[i].argType;
1.1421 + sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
1.1422 + pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
1.1423 + if( aAggs[i].needCollSeq ){
1.1424 + FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
1.1425 + strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
1.1426 + if( pFunc && aAggs[i].needCollSeq ){
1.1427 + pFunc->needCollSeq = 1;
1.1428 + }
1.1429 + }
1.1430 + }
1.1431 + sqlite3RegisterDateTimeFunctions(db);
1.1432 + if( !db->mallocFailed ){
1.1433 + int rc = sqlite3_overload_function(db, "MATCH", 2);
1.1434 + assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
1.1435 + if( rc==SQLITE_NOMEM ){
1.1436 + db->mallocFailed = 1;
1.1437 + }
1.1438 + }
1.1439 +#ifdef SQLITE_SSE
1.1440 + (void)sqlite3SseFunctions(db);
1.1441 +#endif
1.1442 +#ifdef SQLITE_CASE_SENSITIVE_LIKE
1.1443 + sqlite3RegisterLikeFunctions(db, 1);
1.1444 +#else
1.1445 + sqlite3RegisterLikeFunctions(db, 0);
1.1446 +#endif
1.1447 +}
1.1448 +
1.1449 +/*
1.1450 +** Set the LIKEOPT flag on the 2-argument function with the given name.
1.1451 +*/
1.1452 +static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
1.1453 + FuncDef *pDef;
1.1454 + pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
1.1455 + if( pDef ){
1.1456 + pDef->flags = flagVal;
1.1457 + }
1.1458 +}
1.1459 +
1.1460 +/*
1.1461 +** Register the built-in LIKE and GLOB functions. The caseSensitive
1.1462 +** parameter determines whether or not the LIKE operator is case
1.1463 +** sensitive. GLOB is always case sensitive.
1.1464 +*/
1.1465 +void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
1.1466 + struct compareInfo *pInfo;
1.1467 + if( caseSensitive ){
1.1468 + pInfo = (struct compareInfo*)&likeInfoAlt;
1.1469 + }else{
1.1470 + pInfo = (struct compareInfo*)&likeInfoNorm;
1.1471 + }
1.1472 + sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1.1473 + sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1.1474 + sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
1.1475 + (struct compareInfo*)&globInfo, likeFunc, 0,0);
1.1476 + setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1.1477 + setLikeOptFlag(db, "like",
1.1478 + caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
1.1479 +}
1.1480 +
1.1481 +/*
1.1482 +** pExpr points to an expression which implements a function. If
1.1483 +** it is appropriate to apply the LIKE optimization to that function
1.1484 +** then set aWc[0] through aWc[2] to the wildcard characters and
1.1485 +** return TRUE. If the function is not a LIKE-style function then
1.1486 +** return FALSE.
1.1487 +*/
1.1488 +int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
1.1489 + FuncDef *pDef;
1.1490 + if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){
1.1491 + return 0;
1.1492 + }
1.1493 + if( pExpr->pList->nExpr!=2 ){
1.1494 + return 0;
1.1495 + }
1.1496 + pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
1.1497 + SQLITE_UTF8, 0);
1.1498 + if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
1.1499 + return 0;
1.1500 + }
1.1501 +
1.1502 + /* The memcpy() statement assumes that the wildcard characters are
1.1503 + ** the first three statements in the compareInfo structure. The
1.1504 + ** asserts() that follow verify that assumption
1.1505 + */
1.1506 + memcpy(aWc, pDef->pUserData, 3);
1.1507 + assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
1.1508 + assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
1.1509 + assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
1.1510 + *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
1.1511 + return 1;
1.1512 +}