sl@0: /*
sl@0: ** 2002 February 23
sl@0: **
sl@0: ** The author disclaims copyright to this source code.  In place of
sl@0: ** a legal notice, here is a blessing:
sl@0: **
sl@0: **    May you do good and not evil.
sl@0: **    May you find forgiveness for yourself and forgive others.
sl@0: **    May you share freely, never taking more than you give.
sl@0: **
sl@0: *************************************************************************
sl@0: ** This file contains the C functions that implement various SQL
sl@0: ** functions of SQLite.  
sl@0: **
sl@0: ** There is only one exported symbol in this file - the function
sl@0: ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
sl@0: ** All other code has file scope.
sl@0: **
sl@0: ** $Id: func.c,v 1.203 2008/09/03 17:11:16 drh Exp $
sl@0: */
sl@0: #include "sqliteInt.h"
sl@0: #include <ctype.h>
sl@0: #include <stdlib.h>
sl@0: #include <assert.h>
sl@0: #include "vdbeInt.h"
sl@0: 
sl@0: /*
sl@0: ** Return the collating function associated with a function.
sl@0: */
sl@0: static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
sl@0:   return context->pColl;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the non-aggregate min() and max() functions
sl@0: */
sl@0: static void minmaxFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   int i;
sl@0:   int mask;    /* 0 for min() or 0xffffffff for max() */
sl@0:   int iBest;
sl@0:   CollSeq *pColl;
sl@0: 
sl@0:   if( argc==0 ) return;
sl@0:   mask = sqlite3_user_data(context)==0 ? 0 : -1;
sl@0:   pColl = sqlite3GetFuncCollSeq(context);
sl@0:   assert( pColl );
sl@0:   assert( mask==-1 || mask==0 );
sl@0:   iBest = 0;
sl@0:   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
sl@0:   for(i=1; i<argc; i++){
sl@0:     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
sl@0:     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
sl@0:       iBest = i;
sl@0:     }
sl@0:   }
sl@0:   sqlite3_result_value(context, argv[iBest]);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the type of the argument.
sl@0: */
sl@0: static void typeofFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   const char *z = 0;
sl@0:   switch( sqlite3_value_type(argv[0]) ){
sl@0:     case SQLITE_NULL:    z = "null";    break;
sl@0:     case SQLITE_INTEGER: z = "integer"; break;
sl@0:     case SQLITE_TEXT:    z = "text";    break;
sl@0:     case SQLITE_FLOAT:   z = "real";    break;
sl@0:     case SQLITE_BLOB:    z = "blob";    break;
sl@0:   }
sl@0:   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Implementation of the length() function
sl@0: */
sl@0: static void lengthFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   int len;
sl@0: 
sl@0:   assert( argc==1 );
sl@0:   switch( sqlite3_value_type(argv[0]) ){
sl@0:     case SQLITE_BLOB:
sl@0:     case SQLITE_INTEGER:
sl@0:     case SQLITE_FLOAT: {
sl@0:       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
sl@0:       break;
sl@0:     }
sl@0:     case SQLITE_TEXT: {
sl@0:       const unsigned char *z = sqlite3_value_text(argv[0]);
sl@0:       if( z==0 ) return;
sl@0:       len = 0;
sl@0:       while( *z ){
sl@0:         len++;
sl@0:         SQLITE_SKIP_UTF8(z);
sl@0:       }
sl@0:       sqlite3_result_int(context, len);
sl@0:       break;
sl@0:     }
sl@0:     default: {
sl@0:       sqlite3_result_null(context);
sl@0:       break;
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the abs() function
sl@0: */
sl@0: static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   assert( argc==1 );
sl@0:   switch( sqlite3_value_type(argv[0]) ){
sl@0:     case SQLITE_INTEGER: {
sl@0:       i64 iVal = sqlite3_value_int64(argv[0]);
sl@0:       if( iVal<0 ){
sl@0:         if( (iVal<<1)==0 ){
sl@0:           sqlite3_result_error(context, "integer overflow", -1);
sl@0:           return;
sl@0:         }
sl@0:         iVal = -iVal;
sl@0:       } 
sl@0:       sqlite3_result_int64(context, iVal);
sl@0:       break;
sl@0:     }
sl@0:     case SQLITE_NULL: {
sl@0:       sqlite3_result_null(context);
sl@0:       break;
sl@0:     }
sl@0:     default: {
sl@0:       double rVal = sqlite3_value_double(argv[0]);
sl@0:       if( rVal<0 ) rVal = -rVal;
sl@0:       sqlite3_result_double(context, rVal);
sl@0:       break;
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the substr() function.
sl@0: **
sl@0: ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
sl@0: ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
sl@0: ** of x.  If x is text, then we actually count UTF-8 characters.
sl@0: ** If x is a blob, then we count bytes.
sl@0: **
sl@0: ** If p1 is negative, then we begin abs(p1) from the end of x[].
sl@0: */
sl@0: static void substrFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   const unsigned char *z;
sl@0:   const unsigned char *z2;
sl@0:   int len;
sl@0:   int p0type;
sl@0:   i64 p1, p2;
sl@0: 
sl@0:   assert( argc==3 || argc==2 );
sl@0:   p0type = sqlite3_value_type(argv[0]);
sl@0:   if( p0type==SQLITE_BLOB ){
sl@0:     len = sqlite3_value_bytes(argv[0]);
sl@0:     z = sqlite3_value_blob(argv[0]);
sl@0:     if( z==0 ) return;
sl@0:     assert( len==sqlite3_value_bytes(argv[0]) );
sl@0:   }else{
sl@0:     z = sqlite3_value_text(argv[0]);
sl@0:     if( z==0 ) return;
sl@0:     len = 0;
sl@0:     for(z2=z; *z2; len++){
sl@0:       SQLITE_SKIP_UTF8(z2);
sl@0:     }
sl@0:   }
sl@0:   p1 = sqlite3_value_int(argv[1]);
sl@0:   if( argc==3 ){
sl@0:     p2 = sqlite3_value_int(argv[2]);
sl@0:   }else{
sl@0:     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
sl@0:   }
sl@0:   if( p1<0 ){
sl@0:     p1 += len;
sl@0:     if( p1<0 ){
sl@0:       p2 += p1;
sl@0:       p1 = 0;
sl@0:     }
sl@0:   }else if( p1>0 ){
sl@0:     p1--;
sl@0:   }
sl@0:   if( p1+p2>len ){
sl@0:     p2 = len-p1;
sl@0:   }
sl@0:   if( p0type!=SQLITE_BLOB ){
sl@0:     while( *z && p1 ){
sl@0:       SQLITE_SKIP_UTF8(z);
sl@0:       p1--;
sl@0:     }
sl@0:     for(z2=z; *z2 && p2; p2--){
sl@0:       SQLITE_SKIP_UTF8(z2);
sl@0:     }
sl@0:     sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT);
sl@0:   }else{
sl@0:     if( p2<0 ) p2 = 0;
sl@0:     sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the round() function
sl@0: */
sl@0: static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   int n = 0;
sl@0:   double r;
sl@0:   char zBuf[500];  /* larger than the %f representation of the largest double */
sl@0:   assert( argc==1 || argc==2 );
sl@0:   if( argc==2 ){
sl@0:     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
sl@0:     n = sqlite3_value_int(argv[1]);
sl@0:     if( n>30 ) n = 30;
sl@0:     if( n<0 ) n = 0;
sl@0:   }
sl@0:   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
sl@0:   r = sqlite3_value_double(argv[0]);
sl@0:   sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
sl@0:   sqlite3AtoF(zBuf, &r);
sl@0:   sqlite3_result_double(context, r);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Allocate nByte bytes of space using sqlite3_malloc(). If the
sl@0: ** allocation fails, call sqlite3_result_error_nomem() to notify
sl@0: ** the database handle that malloc() has failed.
sl@0: */
sl@0: static void *contextMalloc(sqlite3_context *context, i64 nByte){
sl@0:   char *z;
sl@0:   if( nByte>sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH] ){
sl@0:     sqlite3_result_error_toobig(context);
sl@0:     z = 0;
sl@0:   }else{
sl@0:     z = sqlite3Malloc(nByte);
sl@0:     if( !z && nByte>0 ){
sl@0:       sqlite3_result_error_nomem(context);
sl@0:     }
sl@0:   }
sl@0:   return z;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the upper() and lower() SQL functions.
sl@0: */
sl@0: static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   char *z1;
sl@0:   const char *z2;
sl@0:   int i, n;
sl@0:   if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
sl@0:   z2 = (char*)sqlite3_value_text(argv[0]);
sl@0:   n = sqlite3_value_bytes(argv[0]);
sl@0:   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
sl@0:   assert( z2==(char*)sqlite3_value_text(argv[0]) );
sl@0:   if( z2 ){
sl@0:     z1 = contextMalloc(context, ((i64)n)+1);
sl@0:     if( z1 ){
sl@0:       memcpy(z1, z2, n+1);
sl@0:       for(i=0; z1[i]; i++){
sl@0:         z1[i] = toupper(z1[i]);
sl@0:       }
sl@0:       sqlite3_result_text(context, z1, -1, sqlite3_free);
sl@0:     }
sl@0:   }
sl@0: }
sl@0: static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   char *z1;
sl@0:   const char *z2;
sl@0:   int i, n;
sl@0:   if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
sl@0:   z2 = (char*)sqlite3_value_text(argv[0]);
sl@0:   n = sqlite3_value_bytes(argv[0]);
sl@0:   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
sl@0:   assert( z2==(char*)sqlite3_value_text(argv[0]) );
sl@0:   if( z2 ){
sl@0:     z1 = contextMalloc(context, ((i64)n)+1);
sl@0:     if( z1 ){
sl@0:       memcpy(z1, z2, n+1);
sl@0:       for(i=0; z1[i]; i++){
sl@0:         z1[i] = tolower(z1[i]);
sl@0:       }
sl@0:       sqlite3_result_text(context, z1, -1, sqlite3_free);
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
sl@0: ** All three do the same thing.  They return the first non-NULL
sl@0: ** argument.
sl@0: */
sl@0: static void ifnullFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   int i;
sl@0:   for(i=0; i<argc; i++){
sl@0:     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
sl@0:       sqlite3_result_value(context, argv[i]);
sl@0:       break;
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of random().  Return a random integer.  
sl@0: */
sl@0: static void randomFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   sqlite_int64 r;
sl@0:   sqlite3_randomness(sizeof(r), &r);
sl@0:   if( (r<<1)==0 ) r = 0;  /* Prevent 0x8000.... as the result so that we */
sl@0:                           /* can always do abs() of the result */
sl@0:   sqlite3_result_int64(context, r);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of randomblob(N).  Return a random blob
sl@0: ** that is N bytes long.
sl@0: */
sl@0: static void randomBlob(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   int n;
sl@0:   unsigned char *p;
sl@0:   assert( argc==1 );
sl@0:   n = sqlite3_value_int(argv[0]);
sl@0:   if( n<1 ){
sl@0:     n = 1;
sl@0:   }
sl@0:   p = contextMalloc(context, n);
sl@0:   if( p ){
sl@0:     sqlite3_randomness(n, p);
sl@0:     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the last_insert_rowid() SQL function.  The return
sl@0: ** value is the same as the sqlite3_last_insert_rowid() API function.
sl@0: */
sl@0: static void last_insert_rowid(
sl@0:   sqlite3_context *context, 
sl@0:   int arg, 
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   sqlite3 *db = sqlite3_context_db_handle(context);
sl@0:   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the changes() SQL function.  The return value is the
sl@0: ** same as the sqlite3_changes() API function.
sl@0: */
sl@0: static void changes(
sl@0:   sqlite3_context *context,
sl@0:   int arg,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   sqlite3 *db = sqlite3_context_db_handle(context);
sl@0:   sqlite3_result_int(context, sqlite3_changes(db));
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the total_changes() SQL function.  The return value is
sl@0: ** the same as the sqlite3_total_changes() API function.
sl@0: */
sl@0: static void total_changes(
sl@0:   sqlite3_context *context,
sl@0:   int arg,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   sqlite3 *db = sqlite3_context_db_handle(context);
sl@0:   sqlite3_result_int(context, sqlite3_total_changes(db));
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** A structure defining how to do GLOB-style comparisons.
sl@0: */
sl@0: struct compareInfo {
sl@0:   u8 matchAll;
sl@0:   u8 matchOne;
sl@0:   u8 matchSet;
sl@0:   u8 noCase;
sl@0: };
sl@0: 
sl@0: /*
sl@0: ** For LIKE and GLOB matching on EBCDIC machines, assume that every
sl@0: ** character is exactly one byte in size.  Also, all characters are
sl@0: ** able to participate in upper-case-to-lower-case mappings in EBCDIC
sl@0: ** whereas only characters less than 0x80 do in ASCII.
sl@0: */
sl@0: #if defined(SQLITE_EBCDIC)
sl@0: # define sqlite3Utf8Read(A,B,C)  (*(A++))
sl@0: # define GlogUpperToLower(A)     A = sqlite3UpperToLower[A]
sl@0: #else
sl@0: # define GlogUpperToLower(A)     if( A<0x80 ){ A = sqlite3UpperToLower[A]; }
sl@0: #endif
sl@0: 
sl@0: static const struct compareInfo globInfo = { '*', '?', '[', 0 };
sl@0: /* The correct SQL-92 behavior is for the LIKE operator to ignore
sl@0: ** case.  Thus  'a' LIKE 'A' would be true. */
sl@0: static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
sl@0: /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
sl@0: ** is case sensitive causing 'a' LIKE 'A' to be false */
sl@0: static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
sl@0: 
sl@0: /*
sl@0: ** Compare two UTF-8 strings for equality where the first string can
sl@0: ** potentially be a "glob" expression.  Return true (1) if they
sl@0: ** are the same and false (0) if they are different.
sl@0: **
sl@0: ** Globbing rules:
sl@0: **
sl@0: **      '*'       Matches any sequence of zero or more characters.
sl@0: **
sl@0: **      '?'       Matches exactly one character.
sl@0: **
sl@0: **     [...]      Matches one character from the enclosed list of
sl@0: **                characters.
sl@0: **
sl@0: **     [^...]     Matches one character not in the enclosed list.
sl@0: **
sl@0: ** With the [...] and [^...] matching, a ']' character can be included
sl@0: ** in the list by making it the first character after '[' or '^'.  A
sl@0: ** range of characters can be specified using '-'.  Example:
sl@0: ** "[a-z]" matches any single lower-case letter.  To match a '-', make
sl@0: ** it the last character in the list.
sl@0: **
sl@0: ** This routine is usually quick, but can be N**2 in the worst case.
sl@0: **
sl@0: ** Hints: to match '*' or '?', put them in "[]".  Like this:
sl@0: **
sl@0: **         abc[*]xyz        Matches "abc*xyz" only
sl@0: */
sl@0: static int patternCompare(
sl@0:   const u8 *zPattern,              /* The glob pattern */
sl@0:   const u8 *zString,               /* The string to compare against the glob */
sl@0:   const struct compareInfo *pInfo, /* Information about how to do the compare */
sl@0:   const int esc                    /* The escape character */
sl@0: ){
sl@0:   int c, c2;
sl@0:   int invert;
sl@0:   int seen;
sl@0:   u8 matchOne = pInfo->matchOne;
sl@0:   u8 matchAll = pInfo->matchAll;
sl@0:   u8 matchSet = pInfo->matchSet;
sl@0:   u8 noCase = pInfo->noCase; 
sl@0:   int prevEscape = 0;     /* True if the previous character was 'escape' */
sl@0: 
sl@0:   while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){
sl@0:     if( !prevEscape && c==matchAll ){
sl@0:       while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll
sl@0:                || c == matchOne ){
sl@0:         if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){
sl@0:           return 0;
sl@0:         }
sl@0:       }
sl@0:       if( c==0 ){
sl@0:         return 1;
sl@0:       }else if( c==esc ){
sl@0:         c = sqlite3Utf8Read(zPattern, 0, &zPattern);
sl@0:         if( c==0 ){
sl@0:           return 0;
sl@0:         }
sl@0:       }else if( c==matchSet ){
sl@0:         assert( esc==0 );         /* This is GLOB, not LIKE */
sl@0:         assert( matchSet<0x80 );  /* '[' is a single-byte character */
sl@0:         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
sl@0:           SQLITE_SKIP_UTF8(zString);
sl@0:         }
sl@0:         return *zString!=0;
sl@0:       }
sl@0:       while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){
sl@0:         if( noCase ){
sl@0:           GlogUpperToLower(c2);
sl@0:           GlogUpperToLower(c);
sl@0:           while( c2 != 0 && c2 != c ){
sl@0:             c2 = sqlite3Utf8Read(zString, 0, &zString);
sl@0:             GlogUpperToLower(c2);
sl@0:           }
sl@0:         }else{
sl@0:           while( c2 != 0 && c2 != c ){
sl@0:             c2 = sqlite3Utf8Read(zString, 0, &zString);
sl@0:           }
sl@0:         }
sl@0:         if( c2==0 ) return 0;
sl@0:         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
sl@0:       }
sl@0:       return 0;
sl@0:     }else if( !prevEscape && c==matchOne ){
sl@0:       if( sqlite3Utf8Read(zString, 0, &zString)==0 ){
sl@0:         return 0;
sl@0:       }
sl@0:     }else if( c==matchSet ){
sl@0:       int prior_c = 0;
sl@0:       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
sl@0:       seen = 0;
sl@0:       invert = 0;
sl@0:       c = sqlite3Utf8Read(zString, 0, &zString);
sl@0:       if( c==0 ) return 0;
sl@0:       c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
sl@0:       if( c2=='^' ){
sl@0:         invert = 1;
sl@0:         c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
sl@0:       }
sl@0:       if( c2==']' ){
sl@0:         if( c==']' ) seen = 1;
sl@0:         c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
sl@0:       }
sl@0:       while( c2 && c2!=']' ){
sl@0:         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
sl@0:           c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
sl@0:           if( c>=prior_c && c<=c2 ) seen = 1;
sl@0:           prior_c = 0;
sl@0:         }else{
sl@0:           if( c==c2 ){
sl@0:             seen = 1;
sl@0:           }
sl@0:           prior_c = c2;
sl@0:         }
sl@0:         c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
sl@0:       }
sl@0:       if( c2==0 || (seen ^ invert)==0 ){
sl@0:         return 0;
sl@0:       }
sl@0:     }else if( esc==c && !prevEscape ){
sl@0:       prevEscape = 1;
sl@0:     }else{
sl@0:       c2 = sqlite3Utf8Read(zString, 0, &zString);
sl@0:       if( noCase ){
sl@0:         GlogUpperToLower(c);
sl@0:         GlogUpperToLower(c2);
sl@0:       }
sl@0:       if( c!=c2 ){
sl@0:         return 0;
sl@0:       }
sl@0:       prevEscape = 0;
sl@0:     }
sl@0:   }
sl@0:   return *zString==0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Count the number of times that the LIKE operator (or GLOB which is
sl@0: ** just a variation of LIKE) gets called.  This is used for testing
sl@0: ** only.
sl@0: */
sl@0: #ifdef SQLITE_TEST
sl@0: int sqlite3_like_count = 0;
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Implementation of the like() SQL function.  This function implements
sl@0: ** the build-in LIKE operator.  The first argument to the function is the
sl@0: ** pattern and the second argument is the string.  So, the SQL statements:
sl@0: **
sl@0: **       A LIKE B
sl@0: **
sl@0: ** is implemented as like(B,A).
sl@0: **
sl@0: ** This same function (with a different compareInfo structure) computes
sl@0: ** the GLOB operator.
sl@0: */
sl@0: void likeFunc(
sl@0:   sqlite3_context *context, 
sl@0:   int argc, 
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   const unsigned char *zA, *zB;
sl@0:   int escape = 0;
sl@0:   sqlite3 *db = sqlite3_context_db_handle(context);
sl@0: 
sl@0:   zB = sqlite3_value_text(argv[0]);
sl@0:   zA = sqlite3_value_text(argv[1]);
sl@0: 
sl@0:   /* Limit the length of the LIKE or GLOB pattern to avoid problems
sl@0:   ** of deep recursion and N*N behavior in patternCompare().
sl@0:   */
sl@0:   if( sqlite3_value_bytes(argv[0]) >
sl@0:         db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
sl@0:     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
sl@0:     return;
sl@0:   }
sl@0:   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
sl@0: 
sl@0:   if( argc==3 ){
sl@0:     /* The escape character string must consist of a single UTF-8 character.
sl@0:     ** Otherwise, return an error.
sl@0:     */
sl@0:     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
sl@0:     if( zEsc==0 ) return;
sl@0:     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
sl@0:       sqlite3_result_error(context, 
sl@0:           "ESCAPE expression must be a single character", -1);
sl@0:       return;
sl@0:     }
sl@0:     escape = sqlite3Utf8Read(zEsc, 0, &zEsc);
sl@0:   }
sl@0:   if( zA && zB ){
sl@0:     struct compareInfo *pInfo = sqlite3_user_data(context);
sl@0: #ifdef SQLITE_TEST
sl@0:     sqlite3_like_count++;
sl@0: #endif
sl@0:     
sl@0:     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the NULLIF(x,y) function.  The result is the first
sl@0: ** argument if the arguments are different.  The result is NULL if the
sl@0: ** arguments are equal to each other.
sl@0: */
sl@0: static void nullifFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
sl@0:   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
sl@0:     sqlite3_result_value(context, argv[0]);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the VERSION(*) function.  The result is the version
sl@0: ** of the SQLite library that is running.
sl@0: */
sl@0: static void versionFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
sl@0: }
sl@0: 
sl@0: /* Array for converting from half-bytes (nybbles) into ASCII hex
sl@0: ** digits. */
sl@0: static const char hexdigits[] = {
sl@0:   '0', '1', '2', '3', '4', '5', '6', '7',
sl@0:   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
sl@0: };
sl@0: 
sl@0: /*
sl@0: ** EXPERIMENTAL - This is not an official function.  The interface may
sl@0: ** change.  This function may disappear.  Do not write code that depends
sl@0: ** on this function.
sl@0: **
sl@0: ** Implementation of the QUOTE() function.  This function takes a single
sl@0: ** argument.  If the argument is numeric, the return value is the same as
sl@0: ** the argument.  If the argument is NULL, the return value is the string
sl@0: ** "NULL".  Otherwise, the argument is enclosed in single quotes with
sl@0: ** single-quote escapes.
sl@0: */
sl@0: static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   if( argc<1 ) return;
sl@0:   switch( sqlite3_value_type(argv[0]) ){
sl@0:     case SQLITE_NULL: {
sl@0:       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
sl@0:       break;
sl@0:     }
sl@0:     case SQLITE_INTEGER:
sl@0:     case SQLITE_FLOAT: {
sl@0:       sqlite3_result_value(context, argv[0]);
sl@0:       break;
sl@0:     }
sl@0:     case SQLITE_BLOB: {
sl@0:       char *zText = 0;
sl@0:       char const *zBlob = sqlite3_value_blob(argv[0]);
sl@0:       int nBlob = sqlite3_value_bytes(argv[0]);
sl@0:       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
sl@0:       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
sl@0:       if( zText ){
sl@0:         int i;
sl@0:         for(i=0; i<nBlob; i++){
sl@0:           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
sl@0:           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
sl@0:         }
sl@0:         zText[(nBlob*2)+2] = '\'';
sl@0:         zText[(nBlob*2)+3] = '\0';
sl@0:         zText[0] = 'X';
sl@0:         zText[1] = '\'';
sl@0:         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
sl@0:         sqlite3_free(zText);
sl@0:       }
sl@0:       break;
sl@0:     }
sl@0:     case SQLITE_TEXT: {
sl@0:       int i,j;
sl@0:       u64 n;
sl@0:       const unsigned char *zArg = sqlite3_value_text(argv[0]);
sl@0:       char *z;
sl@0: 
sl@0:       if( zArg==0 ) return;
sl@0:       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
sl@0:       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
sl@0:       if( z ){
sl@0:         z[0] = '\'';
sl@0:         for(i=0, j=1; zArg[i]; i++){
sl@0:           z[j++] = zArg[i];
sl@0:           if( zArg[i]=='\'' ){
sl@0:             z[j++] = '\'';
sl@0:           }
sl@0:         }
sl@0:         z[j++] = '\'';
sl@0:         z[j] = 0;
sl@0:         sqlite3_result_text(context, z, j, sqlite3_free);
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The hex() function.  Interpret the argument as a blob.  Return
sl@0: ** a hexadecimal rendering as text.
sl@0: */
sl@0: static void hexFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   int i, n;
sl@0:   const unsigned char *pBlob;
sl@0:   char *zHex, *z;
sl@0:   assert( argc==1 );
sl@0:   pBlob = sqlite3_value_blob(argv[0]);
sl@0:   n = sqlite3_value_bytes(argv[0]);
sl@0:   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
sl@0:   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
sl@0:   if( zHex ){
sl@0:     for(i=0; i<n; i++, pBlob++){
sl@0:       unsigned char c = *pBlob;
sl@0:       *(z++) = hexdigits[(c>>4)&0xf];
sl@0:       *(z++) = hexdigits[c&0xf];
sl@0:     }
sl@0:     *z = 0;
sl@0:     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
sl@0: */
sl@0: static void zeroblobFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   i64 n;
sl@0:   assert( argc==1 );
sl@0:   n = sqlite3_value_int64(argv[0]);
sl@0:   if( n>SQLITE_MAX_LENGTH ){
sl@0:     sqlite3_result_error_toobig(context);
sl@0:   }else{
sl@0:     sqlite3_result_zeroblob(context, n);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The replace() function.  Three arguments are all strings: call
sl@0: ** them A, B, and C. The result is also a string which is derived
sl@0: ** from A by replacing every occurance of B with C.  The match
sl@0: ** must be exact.  Collating sequences are not used.
sl@0: */
sl@0: static void replaceFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   const unsigned char *zStr;        /* The input string A */
sl@0:   const unsigned char *zPattern;    /* The pattern string B */
sl@0:   const unsigned char *zRep;        /* The replacement string C */
sl@0:   unsigned char *zOut;              /* The output */
sl@0:   int nStr;                /* Size of zStr */
sl@0:   int nPattern;            /* Size of zPattern */
sl@0:   int nRep;                /* Size of zRep */
sl@0:   i64 nOut;                /* Maximum size of zOut */
sl@0:   int loopLimit;           /* Last zStr[] that might match zPattern[] */
sl@0:   int i, j;                /* Loop counters */
sl@0: 
sl@0:   assert( argc==3 );
sl@0:   zStr = sqlite3_value_text(argv[0]);
sl@0:   if( zStr==0 ) return;
sl@0:   nStr = sqlite3_value_bytes(argv[0]);
sl@0:   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
sl@0:   zPattern = sqlite3_value_text(argv[1]);
sl@0:   if( zPattern==0 || zPattern[0]==0 ) return;
sl@0:   nPattern = sqlite3_value_bytes(argv[1]);
sl@0:   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
sl@0:   zRep = sqlite3_value_text(argv[2]);
sl@0:   if( zRep==0 ) return;
sl@0:   nRep = sqlite3_value_bytes(argv[2]);
sl@0:   assert( zRep==sqlite3_value_text(argv[2]) );
sl@0:   nOut = nStr + 1;
sl@0:   assert( nOut<SQLITE_MAX_LENGTH );
sl@0:   zOut = contextMalloc(context, (i64)nOut);
sl@0:   if( zOut==0 ){
sl@0:     return;
sl@0:   }
sl@0:   loopLimit = nStr - nPattern;  
sl@0:   for(i=j=0; i<=loopLimit; i++){
sl@0:     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
sl@0:       zOut[j++] = zStr[i];
sl@0:     }else{
sl@0:       u8 *zOld;
sl@0:       sqlite3 *db = sqlite3_context_db_handle(context);
sl@0:       nOut += nRep - nPattern;
sl@0:       if( nOut>=db->aLimit[SQLITE_LIMIT_LENGTH] ){
sl@0:         sqlite3_result_error_toobig(context);
sl@0:         sqlite3DbFree(db, zOut);
sl@0:         return;
sl@0:       }
sl@0:       zOld = zOut;
sl@0:       zOut = sqlite3_realloc(zOut, (int)nOut);
sl@0:       if( zOut==0 ){
sl@0:         sqlite3_result_error_nomem(context);
sl@0:         sqlite3DbFree(db, zOld);
sl@0:         return;
sl@0:       }
sl@0:       memcpy(&zOut[j], zRep, nRep);
sl@0:       j += nRep;
sl@0:       i += nPattern-1;
sl@0:     }
sl@0:   }
sl@0:   assert( j+nStr-i+1==nOut );
sl@0:   memcpy(&zOut[j], &zStr[i], nStr-i);
sl@0:   j += nStr - i;
sl@0:   assert( j<=nOut );
sl@0:   zOut[j] = 0;
sl@0:   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
sl@0: ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
sl@0: */
sl@0: static void trimFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   const unsigned char *zIn;         /* Input string */
sl@0:   const unsigned char *zCharSet;    /* Set of characters to trim */
sl@0:   int nIn;                          /* Number of bytes in input */
sl@0:   int flags;                        /* 1: trimleft  2: trimright  3: trim */
sl@0:   int i;                            /* Loop counter */
sl@0:   unsigned char *aLen;              /* Length of each character in zCharSet */
sl@0:   unsigned char **azChar;           /* Individual characters in zCharSet */
sl@0:   int nChar;                        /* Number of characters in zCharSet */
sl@0: 
sl@0:   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
sl@0:     return;
sl@0:   }
sl@0:   zIn = sqlite3_value_text(argv[0]);
sl@0:   if( zIn==0 ) return;
sl@0:   nIn = sqlite3_value_bytes(argv[0]);
sl@0:   assert( zIn==sqlite3_value_text(argv[0]) );
sl@0:   if( argc==1 ){
sl@0:     static const unsigned char lenOne[] = { 1 };
sl@0:     static unsigned char * const azOne[] = { (u8*)" " };
sl@0:     nChar = 1;
sl@0:     aLen = (u8*)lenOne;
sl@0:     azChar = (unsigned char **)azOne;
sl@0:     zCharSet = 0;
sl@0:   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
sl@0:     return;
sl@0:   }else{
sl@0:     const unsigned char *z;
sl@0:     for(z=zCharSet, nChar=0; *z; nChar++){
sl@0:       SQLITE_SKIP_UTF8(z);
sl@0:     }
sl@0:     if( nChar>0 ){
sl@0:       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
sl@0:       if( azChar==0 ){
sl@0:         return;
sl@0:       }
sl@0:       aLen = (unsigned char*)&azChar[nChar];
sl@0:       for(z=zCharSet, nChar=0; *z; nChar++){
sl@0:         azChar[nChar] = (unsigned char *)z;
sl@0:         SQLITE_SKIP_UTF8(z);
sl@0:         aLen[nChar] = z - azChar[nChar];
sl@0:       }
sl@0:     }
sl@0:   }
sl@0:   if( nChar>0 ){
sl@0:     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
sl@0:     if( flags & 1 ){
sl@0:       while( nIn>0 ){
sl@0:         int len;
sl@0:         for(i=0; i<nChar; i++){
sl@0:           len = aLen[i];
sl@0:           if( memcmp(zIn, azChar[i], len)==0 ) break;
sl@0:         }
sl@0:         if( i>=nChar ) break;
sl@0:         zIn += len;
sl@0:         nIn -= len;
sl@0:       }
sl@0:     }
sl@0:     if( flags & 2 ){
sl@0:       while( nIn>0 ){
sl@0:         int len;
sl@0:         for(i=0; i<nChar; i++){
sl@0:           len = aLen[i];
sl@0:           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
sl@0:         }
sl@0:         if( i>=nChar ) break;
sl@0:         nIn -= len;
sl@0:       }
sl@0:     }
sl@0:     if( zCharSet ){
sl@0:       sqlite3_free(azChar);
sl@0:     }
sl@0:   }
sl@0:   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
sl@0: }
sl@0: 
sl@0: 
sl@0: #ifdef SQLITE_SOUNDEX
sl@0: /*
sl@0: ** Compute the soundex encoding of a word.
sl@0: */
sl@0: static void soundexFunc(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   char zResult[8];
sl@0:   const u8 *zIn;
sl@0:   int i, j;
sl@0:   static const unsigned char iCode[] = {
sl@0:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0:     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
sl@0:     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
sl@0:     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
sl@0:     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
sl@0:   };
sl@0:   assert( argc==1 );
sl@0:   zIn = (u8*)sqlite3_value_text(argv[0]);
sl@0:   if( zIn==0 ) zIn = (u8*)"";
sl@0:   for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
sl@0:   if( zIn[i] ){
sl@0:     u8 prevcode = iCode[zIn[i]&0x7f];
sl@0:     zResult[0] = toupper(zIn[i]);
sl@0:     for(j=1; j<4 && zIn[i]; i++){
sl@0:       int code = iCode[zIn[i]&0x7f];
sl@0:       if( code>0 ){
sl@0:         if( code!=prevcode ){
sl@0:           prevcode = code;
sl@0:           zResult[j++] = code + '0';
sl@0:         }
sl@0:       }else{
sl@0:         prevcode = 0;
sl@0:       }
sl@0:     }
sl@0:     while( j<4 ){
sl@0:       zResult[j++] = '0';
sl@0:     }
sl@0:     zResult[j] = 0;
sl@0:     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
sl@0:   }else{
sl@0:     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
sl@0:   }
sl@0: }
sl@0: #endif
sl@0: 
sl@0: #ifndef SQLITE_OMIT_LOAD_EXTENSION
sl@0: /*
sl@0: ** A function that loads a shared-library extension then returns NULL.
sl@0: */
sl@0: static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
sl@0:   const char *zProc;
sl@0:   sqlite3 *db = sqlite3_context_db_handle(context);
sl@0:   char *zErrMsg = 0;
sl@0: 
sl@0:   if( argc==2 ){
sl@0:     zProc = (const char *)sqlite3_value_text(argv[1]);
sl@0:   }else{
sl@0:     zProc = 0;
sl@0:   }
sl@0:   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
sl@0:     sqlite3_result_error(context, zErrMsg, -1);
sl@0:     sqlite3_free(zErrMsg);
sl@0:   }
sl@0: }
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** An instance of the following structure holds the context of a
sl@0: ** sum() or avg() aggregate computation.
sl@0: */
sl@0: typedef struct SumCtx SumCtx;
sl@0: struct SumCtx {
sl@0:   double rSum;      /* Floating point sum */
sl@0:   i64 iSum;         /* Integer sum */   
sl@0:   i64 cnt;          /* Number of elements summed */
sl@0:   u8 overflow;      /* True if integer overflow seen */
sl@0:   u8 approx;        /* True if non-integer value was input to the sum */
sl@0: };
sl@0: 
sl@0: /*
sl@0: ** Routines used to compute the sum, average, and total.
sl@0: **
sl@0: ** The SUM() function follows the (broken) SQL standard which means
sl@0: ** that it returns NULL if it sums over no inputs.  TOTAL returns
sl@0: ** 0.0 in that case.  In addition, TOTAL always returns a float where
sl@0: ** SUM might return an integer if it never encounters a floating point
sl@0: ** value.  TOTAL never fails, but SUM might through an exception if
sl@0: ** it overflows an integer.
sl@0: */
sl@0: static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   SumCtx *p;
sl@0:   int type;
sl@0:   assert( argc==1 );
sl@0:   p = sqlite3_aggregate_context(context, sizeof(*p));
sl@0:   type = sqlite3_value_numeric_type(argv[0]);
sl@0:   if( p && type!=SQLITE_NULL ){
sl@0:     p->cnt++;
sl@0:     if( type==SQLITE_INTEGER ){
sl@0:       i64 v = sqlite3_value_int64(argv[0]);
sl@0:       p->rSum += v;
sl@0:       if( (p->approx|p->overflow)==0 ){
sl@0:         i64 iNewSum = p->iSum + v;
sl@0:         int s1 = p->iSum >> (sizeof(i64)*8-1);
sl@0:         int s2 = v       >> (sizeof(i64)*8-1);
sl@0:         int s3 = iNewSum >> (sizeof(i64)*8-1);
sl@0:         p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
sl@0:         p->iSum = iNewSum;
sl@0:       }
sl@0:     }else{
sl@0:       p->rSum += sqlite3_value_double(argv[0]);
sl@0:       p->approx = 1;
sl@0:     }
sl@0:   }
sl@0: }
sl@0: static void sumFinalize(sqlite3_context *context){
sl@0:   SumCtx *p;
sl@0:   p = sqlite3_aggregate_context(context, 0);
sl@0:   if( p && p->cnt>0 ){
sl@0:     if( p->overflow ){
sl@0:       sqlite3_result_error(context,"integer overflow",-1);
sl@0:     }else if( p->approx ){
sl@0:       sqlite3_result_double(context, p->rSum);
sl@0:     }else{
sl@0:       sqlite3_result_int64(context, p->iSum);
sl@0:     }
sl@0:   }
sl@0: }
sl@0: static void avgFinalize(sqlite3_context *context){
sl@0:   SumCtx *p;
sl@0:   p = sqlite3_aggregate_context(context, 0);
sl@0:   if( p && p->cnt>0 ){
sl@0:     sqlite3_result_double(context, p->rSum/(double)p->cnt);
sl@0:   }
sl@0: }
sl@0: static void totalFinalize(sqlite3_context *context){
sl@0:   SumCtx *p;
sl@0:   p = sqlite3_aggregate_context(context, 0);
sl@0:   sqlite3_result_double(context, p ? p->rSum : 0.0);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The following structure keeps track of state information for the
sl@0: ** count() aggregate function.
sl@0: */
sl@0: typedef struct CountCtx CountCtx;
sl@0: struct CountCtx {
sl@0:   i64 n;
sl@0: };
sl@0: 
sl@0: /*
sl@0: ** Routines to implement the count() aggregate function.
sl@0: */
sl@0: static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   CountCtx *p;
sl@0:   p = sqlite3_aggregate_context(context, sizeof(*p));
sl@0:   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
sl@0:     p->n++;
sl@0:   }
sl@0: }   
sl@0: static void countFinalize(sqlite3_context *context){
sl@0:   CountCtx *p;
sl@0:   p = sqlite3_aggregate_context(context, 0);
sl@0:   sqlite3_result_int64(context, p ? p->n : 0);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Routines to implement min() and max() aggregate functions.
sl@0: */
sl@0: static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
sl@0:   Mem *pArg  = (Mem *)argv[0];
sl@0:   Mem *pBest;
sl@0: 
sl@0:   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
sl@0:   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
sl@0:   if( !pBest ) return;
sl@0: 
sl@0:   if( pBest->flags ){
sl@0:     int max;
sl@0:     int cmp;
sl@0:     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
sl@0:     /* This step function is used for both the min() and max() aggregates,
sl@0:     ** the only difference between the two being that the sense of the
sl@0:     ** comparison is inverted. For the max() aggregate, the
sl@0:     ** sqlite3_user_data() function returns (void *)-1. For min() it
sl@0:     ** returns (void *)db, where db is the sqlite3* database pointer.
sl@0:     ** Therefore the next statement sets variable 'max' to 1 for the max()
sl@0:     ** aggregate, or 0 for min().
sl@0:     */
sl@0:     max = sqlite3_user_data(context)!=0;
sl@0:     cmp = sqlite3MemCompare(pBest, pArg, pColl);
sl@0:     if( (max && cmp<0) || (!max && cmp>0) ){
sl@0:       sqlite3VdbeMemCopy(pBest, pArg);
sl@0:     }
sl@0:   }else{
sl@0:     sqlite3VdbeMemCopy(pBest, pArg);
sl@0:   }
sl@0: }
sl@0: static void minMaxFinalize(sqlite3_context *context){
sl@0:   sqlite3_value *pRes;
sl@0:   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
sl@0:   if( pRes ){
sl@0:     if( pRes->flags ){
sl@0:       sqlite3_result_value(context, pRes);
sl@0:     }
sl@0:     sqlite3VdbeMemRelease(pRes);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** group_concat(EXPR, ?SEPARATOR?)
sl@0: */
sl@0: static void groupConcatStep(
sl@0:   sqlite3_context *context,
sl@0:   int argc,
sl@0:   sqlite3_value **argv
sl@0: ){
sl@0:   const char *zVal;
sl@0:   StrAccum *pAccum;
sl@0:   const char *zSep;
sl@0:   int nVal, nSep, i;
sl@0:   if( argc==0 || sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
sl@0:   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
sl@0: 
sl@0:   if( pAccum ){
sl@0:     sqlite3 *db = sqlite3_context_db_handle(context);
sl@0:     pAccum->useMalloc = 1;
sl@0:     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
sl@0:     if( pAccum->nChar ){
sl@0:       if( argc>1 ){
sl@0:         zSep = (char*)sqlite3_value_text(argv[argc-1]);
sl@0:         nSep = sqlite3_value_bytes(argv[argc-1]);
sl@0:       }else{
sl@0:         zSep = ",";
sl@0:         nSep = 1;
sl@0:       }
sl@0:       sqlite3StrAccumAppend(pAccum, zSep, nSep);
sl@0:     }
sl@0:     i = 0;
sl@0:     do{
sl@0:       zVal = (char*)sqlite3_value_text(argv[i]);
sl@0:       nVal = sqlite3_value_bytes(argv[i]);
sl@0:       sqlite3StrAccumAppend(pAccum, zVal, nVal);
sl@0:       i++;
sl@0:     }while( i<argc-1 );
sl@0:   }
sl@0: }
sl@0: static void groupConcatFinalize(sqlite3_context *context){
sl@0:   StrAccum *pAccum;
sl@0:   pAccum = sqlite3_aggregate_context(context, 0);
sl@0:   if( pAccum ){
sl@0:     if( pAccum->tooBig ){
sl@0:       sqlite3_result_error_toobig(context);
sl@0:     }else if( pAccum->mallocFailed ){
sl@0:       sqlite3_result_error_nomem(context);
sl@0:     }else{    
sl@0:       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
sl@0:                           sqlite3_free);
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This function registered all of the above C functions as SQL
sl@0: ** functions.  This should be the only routine in this file with
sl@0: ** external linkage.
sl@0: */
sl@0: void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
sl@0: #ifndef SQLITE_OMIT_ALTERTABLE
sl@0:   sqlite3AlterFunctions(db);
sl@0: #endif
sl@0: #ifndef SQLITE_OMIT_PARSER
sl@0:   sqlite3AttachFunctions(db);
sl@0: #endif
sl@0:   if( !db->mallocFailed ){
sl@0:     int rc = sqlite3_overload_function(db, "MATCH", 2);
sl@0:     assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
sl@0:     if( rc==SQLITE_NOMEM ){
sl@0:       db->mallocFailed = 1;
sl@0:     }
sl@0:   }
sl@0: #ifdef SQLITE_SSE
sl@0:   (void)sqlite3SseFunctions(db);
sl@0: #endif
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Set the LIKEOPT flag on the 2-argument function with the given name.
sl@0: */
sl@0: static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
sl@0:   FuncDef *pDef;
sl@0:   pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
sl@0:   if( pDef ){
sl@0:     pDef->flags = flagVal;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Register the built-in LIKE and GLOB functions.  The caseSensitive
sl@0: ** parameter determines whether or not the LIKE operator is case
sl@0: ** sensitive.  GLOB is always case sensitive.
sl@0: */
sl@0: void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
sl@0:   struct compareInfo *pInfo;
sl@0:   if( caseSensitive ){
sl@0:     pInfo = (struct compareInfo*)&likeInfoAlt;
sl@0:   }else{
sl@0:     pInfo = (struct compareInfo*)&likeInfoNorm;
sl@0:   }
sl@0:   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
sl@0:   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
sl@0:   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
sl@0:       (struct compareInfo*)&globInfo, likeFunc, 0,0);
sl@0:   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
sl@0:   setLikeOptFlag(db, "like", 
sl@0:       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** pExpr points to an expression which implements a function.  If
sl@0: ** it is appropriate to apply the LIKE optimization to that function
sl@0: ** then set aWc[0] through aWc[2] to the wildcard characters and
sl@0: ** return TRUE.  If the function is not a LIKE-style function then
sl@0: ** return FALSE.
sl@0: */
sl@0: int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
sl@0:   FuncDef *pDef;
sl@0:   if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){
sl@0:     return 0;
sl@0:   }
sl@0:   if( pExpr->pList->nExpr!=2 ){
sl@0:     return 0;
sl@0:   }
sl@0:   pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
sl@0:                              SQLITE_UTF8, 0);
sl@0:   if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
sl@0:     return 0;
sl@0:   }
sl@0: 
sl@0:   /* The memcpy() statement assumes that the wildcard characters are
sl@0:   ** the first three statements in the compareInfo structure.  The
sl@0:   ** asserts() that follow verify that assumption
sl@0:   */
sl@0:   memcpy(aWc, pDef->pUserData, 3);
sl@0:   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
sl@0:   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
sl@0:   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
sl@0:   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
sl@0:   return 1;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** All all of the FuncDef structures in the aBuiltinFunc[] array above
sl@0: ** to the global function hash table.  This occurs at start-time (as
sl@0: ** a consequence of calling sqlite3_initialize()).
sl@0: **
sl@0: ** After this routine runs
sl@0: */
sl@0: void sqlite3RegisterGlobalFunctions(void){
sl@0:   /*
sl@0:   ** The following array holds FuncDef structures for all of the functions
sl@0:   ** defined in this file.
sl@0:   **
sl@0:   ** The array cannot be constant since changes are made to the
sl@0:   ** FuncDef.pHash elements at start-time.  The elements of this array
sl@0:   ** are read-only after initialization is complete.
sl@0:   */
sl@0:   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
sl@0:     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
sl@0:     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
sl@0:     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
sl@0:     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
sl@0:     FUNCTION(trim,               1, 3, 0, trimFunc         ),
sl@0:     FUNCTION(trim,               2, 3, 0, trimFunc         ),
sl@0:     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
sl@0:     FUNCTION(min,                0, 0, 1, 0                ),
sl@0:     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
sl@0:     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
sl@0:     FUNCTION(max,                0, 1, 1, 0                ),
sl@0:     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
sl@0:     FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
sl@0:     FUNCTION(length,             1, 0, 0, lengthFunc       ),
sl@0:     FUNCTION(substr,             2, 0, 0, substrFunc       ),
sl@0:     FUNCTION(substr,             3, 0, 0, substrFunc       ),
sl@0:     FUNCTION(abs,                1, 0, 0, absFunc          ),
sl@0:     FUNCTION(round,              1, 0, 0, roundFunc        ),
sl@0:     FUNCTION(round,              2, 0, 0, roundFunc        ),
sl@0:     FUNCTION(upper,              1, 0, 0, upperFunc        ),
sl@0:     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
sl@0:     FUNCTION(coalesce,           1, 0, 0, 0                ),
sl@0:     FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ),
sl@0:     FUNCTION(coalesce,           0, 0, 0, 0                ),
sl@0:     FUNCTION(hex,                1, 0, 0, hexFunc          ),
sl@0:     FUNCTION(ifnull,             2, 0, 1, ifnullFunc       ),
sl@0:     FUNCTION(random,            -1, 0, 0, randomFunc       ),
sl@0:     FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
sl@0:     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
sl@0:     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
sl@0:     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
sl@0:     FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
sl@0:     FUNCTION(changes,            0, 0, 0, changes          ),
sl@0:     FUNCTION(total_changes,      0, 0, 0, total_changes    ),
sl@0:     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
sl@0:     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
sl@0:   #ifdef SQLITE_SOUNDEX
sl@0:     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
sl@0:   #endif
sl@0:   #ifndef SQLITE_OMIT_LOAD_EXTENSION
sl@0:     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
sl@0:     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
sl@0:   #endif
sl@0:     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
sl@0:     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
sl@0:     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
sl@0:     AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ),
sl@0:     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
sl@0:     AGGREGATE(group_concat,     -1, 0, 0, groupConcatStep, groupConcatFinalize),
sl@0:   
sl@0:     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
sl@0:   #ifdef SQLITE_CASE_SENSITIVE_LIKE
sl@0:     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
sl@0:     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
sl@0:   #else
sl@0:     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
sl@0:     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
sl@0:   #endif
sl@0:   };
sl@0: 
sl@0:   int i;
sl@0:   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
sl@0:   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
sl@0: 
sl@0:   for(i=0; i<ArraySize(aBuiltinFunc); i++){
sl@0:     sqlite3FuncDefInsert(pHash, &aFunc[i]);
sl@0:   }
sl@0:   sqlite3RegisterDateTimeFunctions();
sl@0: }
sl@0: 
sl@0: int sqlite3RegisterInternalUtf8Like(sqlite3 *db){
sl@0:   int rc;
sl@0:   void *pCtx = (void *)&likeInfoNorm;
sl@0:   rc = sqlite3_create_function(db, "like", 2, SQLITE_UTF8, pCtx, likeFunc, 0, 0);
sl@0:   if( rc!=SQLITE_OK ) return rc;
sl@0:   rc = sqlite3_create_function(db, "like", 3, SQLITE_UTF8, pCtx, likeFunc, 0, 0);
sl@0:   if( rc!=SQLITE_OK ) return rc;
sl@0:   setLikeOptFlag(db, "like", SQLITE_FUNC_LIKE);
sl@0:   return SQLITE_OK;
sl@0: }