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