PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - pdo_sqlite/sqlite/src - func.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 445
Code covered: 11.2 % Executed lines: 50
Legend: not executed executed

       1                 : /*
       2                 : ** 2002 February 23
       3                 : **
       4                 : ** The author disclaims copyright to this source code.  In place of
       5                 : ** a legal notice, here is a blessing:
       6                 : **
       7                 : **    May you do good and not evil.
       8                 : **    May you find forgiveness for yourself and forgive others.
       9                 : **    May you share freely, never taking more than you give.
      10                 : **
      11                 : *************************************************************************
      12                 : ** This file contains the C functions that implement various SQL
      13                 : ** functions of SQLite.  
      14                 : **
      15                 : ** There is only one exported symbol in this file - the function
      16                 : ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
      17                 : ** All other code has file scope.
      18                 : **
      19                 : ** $Id$
      20                 : */
      21                 : #include "sqliteInt.h"
      22                 : #include <ctype.h>
      23                 : #include <stdlib.h>
      24                 : #include <assert.h>
      25                 : #include "vdbeInt.h"
      26                 : #include "os.h"
      27                 : 
      28                 : /*
      29                 : ** Return the collating function associated with a function.
      30                 : */
      31               0 : static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
      32               0 :   return context->pColl;
      33                 : }
      34                 : 
      35                 : /*
      36                 : ** Implementation of the non-aggregate min() and max() functions
      37                 : */
      38                 : static void minmaxFunc(
      39                 :   sqlite3_context *context,
      40                 :   int argc,
      41                 :   sqlite3_value **argv
      42               0 : ){
      43                 :   int i;
      44                 :   int mask;    /* 0 for min() or 0xffffffff for max() */
      45                 :   int iBest;
      46                 :   CollSeq *pColl;
      47                 : 
      48               0 :   if( argc==0 ) return;
      49               0 :   mask = sqlite3_user_data(context)==0 ? 0 : -1;
      50               0 :   pColl = sqlite3GetFuncCollSeq(context);
      51                 :   assert( pColl );
      52                 :   assert( mask==-1 || mask==0 );
      53               0 :   iBest = 0;
      54               0 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
      55               0 :   for(i=1; i<argc; i++){
      56               0 :     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
      57               0 :     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
      58               0 :       iBest = i;
      59                 :     }
      60                 :   }
      61               0 :   sqlite3_result_value(context, argv[iBest]);
      62                 : }
      63                 : 
      64                 : /*
      65                 : ** Return the type of the argument.
      66                 : */
      67                 : static void typeofFunc(
      68                 :   sqlite3_context *context,
      69                 :   int argc,
      70                 :   sqlite3_value **argv
      71               0 : ){
      72               0 :   const char *z = 0;
      73               0 :   switch( sqlite3_value_type(argv[0]) ){
      74               0 :     case SQLITE_NULL:    z = "null";    break;
      75               0 :     case SQLITE_INTEGER: z = "integer"; break;
      76               0 :     case SQLITE_TEXT:    z = "text";    break;
      77               0 :     case SQLITE_FLOAT:   z = "real";    break;
      78               0 :     case SQLITE_BLOB:    z = "blob";    break;
      79                 :   }
      80               0 :   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
      81               0 : }
      82                 : 
      83                 : 
      84                 : /*
      85                 : ** Implementation of the length() function
      86                 : */
      87                 : static void lengthFunc(
      88                 :   sqlite3_context *context,
      89                 :   int argc,
      90                 :   sqlite3_value **argv
      91               0 : ){
      92                 :   int len;
      93                 : 
      94                 :   assert( argc==1 );
      95               0 :   switch( sqlite3_value_type(argv[0]) ){
      96                 :     case SQLITE_BLOB:
      97                 :     case SQLITE_INTEGER:
      98                 :     case SQLITE_FLOAT: {
      99               0 :       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
     100               0 :       break;
     101                 :     }
     102                 :     case SQLITE_TEXT: {
     103               0 :       const unsigned char *z = sqlite3_value_text(argv[0]);
     104               0 :       for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
     105               0 :       sqlite3_result_int(context, len);
     106               0 :       break;
     107                 :     }
     108                 :     default: {
     109               0 :       sqlite3_result_null(context);
     110                 :       break;
     111                 :     }
     112                 :   }
     113               0 : }
     114                 : 
     115                 : /*
     116                 : ** Implementation of the abs() function
     117                 : */
     118               0 : static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
     119                 :   assert( argc==1 );
     120               0 :   switch( sqlite3_value_type(argv[0]) ){
     121                 :     case SQLITE_INTEGER: {
     122               0 :       i64 iVal = sqlite3_value_int64(argv[0]);
     123               0 :       if( iVal<0 ){
     124               0 :         if( (iVal<<1)==0 ){
     125               0 :           sqlite3_result_error(context, "integer overflow", -1);
     126               0 :           return;
     127                 :         }
     128               0 :         iVal = -iVal;
     129                 :       } 
     130               0 :       sqlite3_result_int64(context, iVal);
     131               0 :       break;
     132                 :     }
     133                 :     case SQLITE_NULL: {
     134               0 :       sqlite3_result_null(context);
     135               0 :       break;
     136                 :     }
     137                 :     default: {
     138               0 :       double rVal = sqlite3_value_double(argv[0]);
     139               0 :       if( rVal<0 ) rVal = -rVal;
     140               0 :       sqlite3_result_double(context, rVal);
     141                 :       break;
     142                 :     }
     143                 :   }
     144                 : }
     145                 : 
     146                 : /*
     147                 : ** Implementation of the substr() function
     148                 : */
     149                 : static void substrFunc(
     150                 :   sqlite3_context *context,
     151                 :   int argc,
     152                 :   sqlite3_value **argv
     153               0 : ){
     154                 :   const unsigned char *z;
     155                 :   const unsigned char *z2;
     156                 :   int i;
     157                 :   int p1, p2, len;
     158                 : 
     159                 :   assert( argc==3 );
     160               0 :   z = sqlite3_value_text(argv[0]);
     161               0 :   if( z==0 ) return;
     162               0 :   p1 = sqlite3_value_int(argv[1]);
     163               0 :   p2 = sqlite3_value_int(argv[2]);
     164               0 :   for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
     165               0 :   if( p1<0 ){
     166               0 :     p1 += len;
     167               0 :     if( p1<0 ){
     168               0 :       p2 += p1;
     169               0 :       p1 = 0;
     170                 :     }
     171               0 :   }else if( p1>0 ){
     172               0 :     p1--;
     173                 :   }
     174               0 :   if( p1+p2>len ){
     175               0 :     p2 = len-p1;
     176                 :   }
     177               0 :   for(i=0; i<p1 && z[i]; i++){
     178               0 :     if( (z[i]&0xc0)==0x80 ) p1++;
     179                 :   }
     180               0 :   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
     181               0 :   for(; i<p1+p2 && z[i]; i++){
     182               0 :     if( (z[i]&0xc0)==0x80 ) p2++;
     183                 :   }
     184               0 :   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
     185               0 :   if( p2<0 ) p2 = 0;
     186               0 :   sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
     187                 : }
     188                 : 
     189                 : /*
     190                 : ** Implementation of the round() function
     191                 : */
     192               0 : static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
     193               0 :   int n = 0;
     194                 :   double r;
     195                 :   char zBuf[500];  /* larger than the %f representation of the largest double */
     196                 :   assert( argc==1 || argc==2 );
     197               0 :   if( argc==2 ){
     198               0 :     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
     199               0 :     n = sqlite3_value_int(argv[1]);
     200               0 :     if( n>30 ) n = 30;
     201               0 :     if( n<0 ) n = 0;
     202                 :   }
     203               0 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
     204               0 :   r = sqlite3_value_double(argv[0]);
     205               0 :   sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
     206               0 :   sqlite3AtoF(zBuf, &r);
     207               0 :   sqlite3_result_double(context, r);
     208                 : }
     209                 : 
     210                 : /*
     211                 : ** Implementation of the upper() and lower() SQL functions.
     212                 : */
     213               0 : static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
     214                 :   unsigned char *z;
     215                 :   int i;
     216               0 :   if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
     217               0 :   z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
     218               0 :   if( z==0 ) return;
     219               0 :   strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
     220               0 :   for(i=0; z[i]; i++){
     221               0 :     z[i] = toupper(z[i]);
     222                 :   }
     223               0 :   sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
     224               0 :   sqliteFree(z);
     225                 : }
     226               0 : static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
     227                 :   unsigned char *z;
     228                 :   int i;
     229               0 :   if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
     230               0 :   z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
     231               0 :   if( z==0 ) return;
     232               0 :   strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
     233               0 :   for(i=0; z[i]; i++){
     234               0 :     z[i] = tolower(z[i]);
     235                 :   }
     236               0 :   sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
     237               0 :   sqliteFree(z);
     238                 : }
     239                 : 
     240                 : /*
     241                 : ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
     242                 : ** All three do the same thing.  They return the first non-NULL
     243                 : ** argument.
     244                 : */
     245                 : static void ifnullFunc(
     246                 :   sqlite3_context *context,
     247                 :   int argc,
     248                 :   sqlite3_value **argv
     249               0 : ){
     250                 :   int i;
     251               0 :   for(i=0; i<argc; i++){
     252               0 :     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
     253               0 :       sqlite3_result_value(context, argv[i]);
     254               0 :       break;
     255                 :     }
     256                 :   }
     257               0 : }
     258                 : 
     259                 : /*
     260                 : ** Implementation of random().  Return a random integer.  
     261                 : */
     262                 : static void randomFunc(
     263                 :   sqlite3_context *context,
     264                 :   int argc,
     265                 :   sqlite3_value **argv
     266               0 : ){
     267                 :   sqlite_int64 r;
     268               0 :   sqlite3Randomness(sizeof(r), &r);
     269               0 :   if( (r<<1)==0 ) r = 0;  /* Prevent 0x8000.... as the result so that we */
     270                 :                           /* can always do abs() of the result */
     271               0 :   sqlite3_result_int64(context, r);
     272               0 : }
     273                 : 
     274                 : /*
     275                 : ** Implementation of randomblob(N).  Return a random blob
     276                 : ** that is N bytes long.
     277                 : */
     278                 : static void randomBlob(
     279                 :   sqlite3_context *context,
     280                 :   int argc,
     281                 :   sqlite3_value **argv
     282               0 : ){
     283                 :   int n;
     284                 :   unsigned char *p;
     285                 :   assert( argc==1 );
     286               0 :   n = sqlite3_value_int(argv[0]);
     287               0 :   if( n<1 ) n = 1;
     288               0 :   p = sqlite3_malloc(n);
     289               0 :   sqlite3Randomness(n, p);
     290               0 :   sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
     291               0 : }
     292                 : 
     293                 : /*
     294                 : ** Implementation of the last_insert_rowid() SQL function.  The return
     295                 : ** value is the same as the sqlite3_last_insert_rowid() API function.
     296                 : */
     297                 : static void last_insert_rowid(
     298                 :   sqlite3_context *context, 
     299                 :   int arg, 
     300                 :   sqlite3_value **argv
     301               0 : ){
     302               0 :   sqlite3 *db = sqlite3_user_data(context);
     303               0 :   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
     304               0 : }
     305                 : 
     306                 : /*
     307                 : ** Implementation of the changes() SQL function.  The return value is the
     308                 : ** same as the sqlite3_changes() API function.
     309                 : */
     310                 : static void changes(
     311                 :   sqlite3_context *context,
     312                 :   int arg,
     313                 :   sqlite3_value **argv
     314               0 : ){
     315               0 :   sqlite3 *db = sqlite3_user_data(context);
     316               0 :   sqlite3_result_int(context, sqlite3_changes(db));
     317               0 : }
     318                 : 
     319                 : /*
     320                 : ** Implementation of the total_changes() SQL function.  The return value is
     321                 : ** the same as the sqlite3_total_changes() API function.
     322                 : */
     323                 : static void total_changes(
     324                 :   sqlite3_context *context,
     325                 :   int arg,
     326                 :   sqlite3_value **argv
     327               0 : ){
     328               0 :   sqlite3 *db = sqlite3_user_data(context);
     329               0 :   sqlite3_result_int(context, sqlite3_total_changes(db));
     330               0 : }
     331                 : 
     332                 : /*
     333                 : ** A structure defining how to do GLOB-style comparisons.
     334                 : */
     335                 : struct compareInfo {
     336                 :   u8 matchAll;
     337                 :   u8 matchOne;
     338                 :   u8 matchSet;
     339                 :   u8 noCase;
     340                 : };
     341                 : 
     342                 : static const struct compareInfo globInfo = { '*', '?', '[', 0 };
     343                 : /* The correct SQL-92 behavior is for the LIKE operator to ignore
     344                 : ** case.  Thus  'a' LIKE 'A' would be true. */
     345                 : static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
     346                 : /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
     347                 : ** is case sensitive causing 'a' LIKE 'A' to be false */
     348                 : static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
     349                 : 
     350                 : /*
     351                 : ** X is a pointer to the first byte of a UTF-8 character.  Increment
     352                 : ** X so that it points to the next character.  This only works right
     353                 : ** if X points to a well-formed UTF-8 string.
     354                 : */
     355                 : #define sqliteNextChar(X)  while( (0xc0&*++(X))==0x80 ){}
     356                 : #define sqliteCharVal(X)   sqlite3ReadUtf8(X)
     357                 : 
     358                 : 
     359                 : /*
     360                 : ** Compare two UTF-8 strings for equality where the first string can
     361                 : ** potentially be a "glob" expression.  Return true (1) if they
     362                 : ** are the same and false (0) if they are different.
     363                 : **
     364                 : ** Globbing rules:
     365                 : **
     366                 : **      '*'       Matches any sequence of zero or more characters.
     367                 : **
     368                 : **      '?'       Matches exactly one character.
     369                 : **
     370                 : **     [...]      Matches one character from the enclosed list of
     371                 : **                characters.
     372                 : **
     373                 : **     [^...]     Matches one character not in the enclosed list.
     374                 : **
     375                 : ** With the [...] and [^...] matching, a ']' character can be included
     376                 : ** in the list by making it the first character after '[' or '^'.  A
     377                 : ** range of characters can be specified using '-'.  Example:
     378                 : ** "[a-z]" matches any single lower-case letter.  To match a '-', make
     379                 : ** it the last character in the list.
     380                 : **
     381                 : ** This routine is usually quick, but can be N**2 in the worst case.
     382                 : **
     383                 : ** Hints: to match '*' or '?', put them in "[]".  Like this:
     384                 : **
     385                 : **         abc[*]xyz        Matches "abc*xyz" only
     386                 : */
     387                 : static int patternCompare(
     388                 :   const u8 *zPattern,              /* The glob pattern */
     389                 :   const u8 *zString,               /* The string to compare against the glob */
     390                 :   const struct compareInfo *pInfo, /* Information about how to do the compare */
     391                 :   const int esc                    /* The escape character */
     392               0 : ){
     393                 :   register int c;
     394                 :   int invert;
     395                 :   int seen;
     396                 :   int c2;
     397               0 :   u8 matchOne = pInfo->matchOne;
     398               0 :   u8 matchAll = pInfo->matchAll;
     399               0 :   u8 matchSet = pInfo->matchSet;
     400               0 :   u8 noCase = pInfo->noCase; 
     401               0 :   int prevEscape = 0;     /* True if the previous character was 'escape' */
     402                 : 
     403               0 :   while( (c = *zPattern)!=0 ){
     404               0 :     if( !prevEscape && c==matchAll ){
     405               0 :       while( (c=zPattern[1]) == matchAll || c == matchOne ){
     406               0 :         if( c==matchOne ){
     407               0 :           if( *zString==0 ) return 0;
     408               0 :           sqliteNextChar(zString);
     409                 :         }
     410               0 :         zPattern++;
     411                 :       }
     412               0 :       if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
     413               0 :         u8 const *zTemp = &zPattern[1];
     414               0 :         sqliteNextChar(zTemp);
     415               0 :         c = *zTemp;
     416                 :       }
     417               0 :       if( c==0 ) return 1;
     418               0 :       if( c==matchSet ){
     419                 :         assert( esc==0 );   /* This is GLOB, not LIKE */
     420               0 :         while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
     421               0 :           sqliteNextChar(zString);
     422                 :         }
     423               0 :         return *zString!=0;
     424                 :       }else{
     425               0 :         while( (c2 = *zString)!=0 ){
     426               0 :           if( noCase ){
     427               0 :             c2 = sqlite3UpperToLower[c2];
     428               0 :             c = sqlite3UpperToLower[c];
     429               0 :             while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
     430                 :           }else{
     431               0 :             while( c2 != 0 && c2 != c ){ c2 = *++zString; }
     432                 :           }
     433               0 :           if( c2==0 ) return 0;
     434               0 :           if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
     435               0 :           sqliteNextChar(zString);
     436                 :         }
     437               0 :         return 0;
     438                 :       }
     439               0 :     }else if( !prevEscape && c==matchOne ){
     440               0 :       if( *zString==0 ) return 0;
     441               0 :       sqliteNextChar(zString);
     442               0 :       zPattern++;
     443               0 :     }else if( c==matchSet ){
     444               0 :       int prior_c = 0;
     445                 :       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
     446               0 :       seen = 0;
     447               0 :       invert = 0;
     448               0 :       c = sqliteCharVal(zString);
     449               0 :       if( c==0 ) return 0;
     450               0 :       c2 = *++zPattern;
     451               0 :       if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
     452               0 :       if( c2==']' ){
     453               0 :         if( c==']' ) seen = 1;
     454               0 :         c2 = *++zPattern;
     455                 :       }
     456               0 :       while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
     457               0 :         if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
     458               0 :           zPattern++;
     459               0 :           c2 = sqliteCharVal(zPattern);
     460               0 :           if( c>=prior_c && c<=c2 ) seen = 1;
     461               0 :           prior_c = 0;
     462               0 :         }else if( c==c2 ){
     463               0 :           seen = 1;
     464               0 :           prior_c = c2;
     465                 :         }else{
     466               0 :           prior_c = c2;
     467                 :         }
     468               0 :         sqliteNextChar(zPattern);
     469                 :       }
     470               0 :       if( c2==0 || (seen ^ invert)==0 ) return 0;
     471               0 :       sqliteNextChar(zString);
     472               0 :       zPattern++;
     473               0 :     }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
     474               0 :       prevEscape = 1;
     475               0 :       sqliteNextChar(zPattern);
     476                 :     }else{
     477               0 :       if( noCase ){
     478               0 :         if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
     479                 :       }else{
     480               0 :         if( c != *zString ) return 0;
     481                 :       }
     482               0 :       zPattern++;
     483               0 :       zString++;
     484               0 :       prevEscape = 0;
     485                 :     }
     486                 :   }
     487               0 :   return *zString==0;
     488                 : }
     489                 : 
     490                 : /*
     491                 : ** Count the number of times that the LIKE operator (or GLOB which is
     492                 : ** just a variation of LIKE) gets called.  This is used for testing
     493                 : ** only.
     494                 : */
     495                 : #ifdef SQLITE_TEST
     496                 : int sqlite3_like_count = 0;
     497                 : #endif
     498                 : 
     499                 : 
     500                 : /*
     501                 : ** Implementation of the like() SQL function.  This function implements
     502                 : ** the build-in LIKE operator.  The first argument to the function is the
     503                 : ** pattern and the second argument is the string.  So, the SQL statements:
     504                 : **
     505                 : **       A LIKE B
     506                 : **
     507                 : ** is implemented as like(B,A).
     508                 : **
     509                 : ** This same function (with a different compareInfo structure) computes
     510                 : ** the GLOB operator.
     511                 : */
     512                 : static void likeFunc(
     513                 :   sqlite3_context *context, 
     514                 :   int argc, 
     515                 :   sqlite3_value **argv
     516               0 : ){
     517               0 :   const unsigned char *zA = sqlite3_value_text(argv[0]);
     518               0 :   const unsigned char *zB = sqlite3_value_text(argv[1]);
     519               0 :   int escape = 0;
     520               0 :   if( argc==3 ){
     521                 :     /* The escape character string must consist of a single UTF-8 character.
     522                 :     ** Otherwise, return an error.
     523                 :     */
     524               0 :     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
     525               0 :     if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){
     526               0 :       sqlite3_result_error(context, 
     527                 :           "ESCAPE expression must be a single character", -1);
     528               0 :       return;
     529                 :     }
     530               0 :     escape = sqlite3ReadUtf8(zEsc);
     531                 :   }
     532               0 :   if( zA && zB ){
     533               0 :     struct compareInfo *pInfo = sqlite3_user_data(context);
     534                 : #ifdef SQLITE_TEST
     535                 :     sqlite3_like_count++;
     536                 : #endif
     537               0 :     sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape));
     538                 :   }
     539                 : }
     540                 : 
     541                 : /*
     542                 : ** Implementation of the NULLIF(x,y) function.  The result is the first
     543                 : ** argument if the arguments are different.  The result is NULL if the
     544                 : ** arguments are equal to each other.
     545                 : */
     546                 : static void nullifFunc(
     547                 :   sqlite3_context *context,
     548                 :   int argc,
     549                 :   sqlite3_value **argv
     550               0 : ){
     551               0 :   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
     552               0 :   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
     553               0 :     sqlite3_result_value(context, argv[0]);
     554                 :   }
     555               0 : }
     556                 : 
     557                 : /*
     558                 : ** Implementation of the VERSION(*) function.  The result is the version
     559                 : ** of the SQLite library that is running.
     560                 : */
     561                 : static void versionFunc(
     562                 :   sqlite3_context *context,
     563                 :   int argc,
     564                 :   sqlite3_value **argv
     565               0 : ){
     566               0 :   sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
     567               0 : }
     568                 : 
     569                 : /* Array for converting from half-bytes (nybbles) into ASCII hex
     570                 : ** digits. */
     571                 : static const char hexdigits[] = {
     572                 :   '0', '1', '2', '3', '4', '5', '6', '7',
     573                 :   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
     574                 : };
     575                 : 
     576                 : /*
     577                 : ** EXPERIMENTAL - This is not an official function.  The interface may
     578                 : ** change.  This function may disappear.  Do not write code that depends
     579                 : ** on this function.
     580                 : **
     581                 : ** Implementation of the QUOTE() function.  This function takes a single
     582                 : ** argument.  If the argument is numeric, the return value is the same as
     583                 : ** the argument.  If the argument is NULL, the return value is the string
     584                 : ** "NULL".  Otherwise, the argument is enclosed in single quotes with
     585                 : ** single-quote escapes.
     586                 : */
     587               0 : static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
     588               0 :   if( argc<1 ) return;
     589               0 :   switch( sqlite3_value_type(argv[0]) ){
     590                 :     case SQLITE_NULL: {
     591               0 :       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
     592               0 :       break;
     593                 :     }
     594                 :     case SQLITE_INTEGER:
     595                 :     case SQLITE_FLOAT: {
     596               0 :       sqlite3_result_value(context, argv[0]);
     597               0 :       break;
     598                 :     }
     599                 :     case SQLITE_BLOB: {
     600               0 :       char *zText = 0;
     601               0 :       int nBlob = sqlite3_value_bytes(argv[0]);
     602               0 :       char const *zBlob = sqlite3_value_blob(argv[0]);
     603                 : 
     604               0 :       zText = (char *)sqliteMalloc((2*nBlob)+4); 
     605               0 :       if( !zText ){
     606               0 :         sqlite3_result_error(context, "out of memory", -1);
     607                 :       }else{
     608                 :         int i;
     609               0 :         for(i=0; i<nBlob; i++){
     610               0 :           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
     611               0 :           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
     612                 :         }
     613               0 :         zText[(nBlob*2)+2] = '\'';
     614               0 :         zText[(nBlob*2)+3] = '\0';
     615               0 :         zText[0] = 'X';
     616               0 :         zText[1] = '\'';
     617               0 :         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
     618               0 :         sqliteFree(zText);
     619                 :       }
     620               0 :       break;
     621                 :     }
     622                 :     case SQLITE_TEXT: {
     623                 :       int i,j,n;
     624               0 :       const unsigned char *zArg = sqlite3_value_text(argv[0]);
     625                 :       char *z;
     626                 : 
     627               0 :       for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
     628               0 :       z = sqliteMalloc( i+n+3 );
     629               0 :       if( z==0 ) return;
     630               0 :       z[0] = '\'';
     631               0 :       for(i=0, j=1; zArg[i]; i++){
     632               0 :         z[j++] = zArg[i];
     633               0 :         if( zArg[i]=='\'' ){
     634               0 :           z[j++] = '\'';
     635                 :         }
     636                 :       }
     637               0 :       z[j++] = '\'';
     638               0 :       z[j] = 0;
     639               0 :       sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
     640               0 :       sqliteFree(z);
     641                 :     }
     642                 :   }
     643                 : }
     644                 : 
     645                 : /*
     646                 : ** The hex() function.  Interpret the argument as a blob.  Return
     647                 : ** a hexadecimal rendering as text.
     648                 : */
     649                 : static void hexFunc(
     650                 :   sqlite3_context *context,
     651                 :   int argc,
     652                 :   sqlite3_value **argv
     653               0 : ){
     654                 :   int i, n;
     655                 :   const unsigned char *pBlob;
     656                 :   char *zHex, *z;
     657                 :   assert( argc==1 );
     658               0 :   n = sqlite3_value_bytes(argv[0]);
     659               0 :   pBlob = sqlite3_value_blob(argv[0]);
     660               0 :   z = zHex = sqlite3_malloc(n*2 + 1);
     661               0 :   if( zHex==0 ) return;
     662               0 :   for(i=0; i<n; i++, pBlob++){
     663               0 :     unsigned char c = *pBlob;
     664               0 :     *(z++) = hexdigits[(c>>4)&0xf];
     665               0 :     *(z++) = hexdigits[c&0xf];
     666                 :   }
     667               0 :   *z = 0;
     668               0 :   sqlite3_result_text(context, zHex, n*2, sqlite3_free);
     669                 : }
     670                 : 
     671                 : /*
     672                 : ** The replace() function.  Three arguments are all strings: call
     673                 : ** them A, B, and C. The result is also a string which is derived
     674                 : ** from A by replacing every occurance of B with C.  The match
     675                 : ** must be exact.  Collating sequences are not used.
     676                 : */
     677                 : static void replaceFunc(
     678                 :   sqlite3_context *context,
     679                 :   int argc,
     680                 :   sqlite3_value **argv
     681               0 : ){
     682                 :   const unsigned char *zStr;        /* The input string A */
     683                 :   const unsigned char *zPattern;    /* The pattern string B */
     684                 :   const unsigned char *zRep;        /* The replacement string C */
     685                 :   unsigned char *zOut;              /* The output */
     686                 :   int nStr;                /* Size of zStr */
     687                 :   int nPattern;            /* Size of zPattern */
     688                 :   int nRep;                /* Size of zRep */
     689                 :   int nOut;                /* Maximum size of zOut */
     690                 :   int loopLimit;           /* Last zStr[] that might match zPattern[] */
     691                 :   int i, j;                /* Loop counters */
     692                 : 
     693                 :   assert( argc==3 );
     694               0 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ||
     695                 :       sqlite3_value_type(argv[1])==SQLITE_NULL ||
     696                 :       sqlite3_value_type(argv[2])==SQLITE_NULL ){
     697               0 :     return;
     698                 :   }
     699               0 :   zStr = sqlite3_value_text(argv[0]);
     700               0 :   nStr = sqlite3_value_bytes(argv[0]);
     701               0 :   zPattern = sqlite3_value_text(argv[1]);
     702               0 :   nPattern = sqlite3_value_bytes(argv[1]);
     703               0 :   zRep = sqlite3_value_text(argv[2]);
     704               0 :   nRep = sqlite3_value_bytes(argv[2]);
     705               0 :   if( nPattern>=nRep ){
     706               0 :     nOut = nStr;
     707                 :   }else{
     708               0 :     nOut = (nStr/nPattern + 1)*nRep;
     709                 :   }
     710               0 :   zOut = sqlite3_malloc(nOut+1);
     711               0 :   if( zOut==0 ) return;
     712               0 :   loopLimit = nStr - nPattern;  
     713               0 :   for(i=j=0; i<=loopLimit; i++){
     714               0 :     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
     715               0 :       zOut[j++] = zStr[i];
     716                 :     }else{
     717               0 :       memcpy(&zOut[j], zRep, nRep);
     718               0 :       j += nRep;
     719               0 :       i += nPattern-1;
     720                 :     }
     721                 :   }
     722               0 :   memcpy(&zOut[j], &zStr[i], nStr-i);
     723               0 :   j += nStr - i;
     724                 :   assert( j<=nOut );
     725               0 :   zOut[j] = 0;
     726               0 :   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
     727                 : }
     728                 : 
     729                 : /*
     730                 : ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
     731                 : ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
     732                 : */
     733                 : static void trimFunc(
     734                 :   sqlite3_context *context,
     735                 :   int argc,
     736                 :   sqlite3_value **argv
     737               0 : ){
     738                 :   const unsigned char *zIn;         /* Input string */
     739                 :   const unsigned char *zCharSet;    /* Set of characters to trim */
     740                 :   int nIn;                          /* Number of bytes in input */
     741                 :   int flags;
     742                 :   int i;
     743                 :   unsigned char cFirst, cNext;
     744               0 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
     745               0 :     return;
     746                 :   }
     747               0 :   zIn = sqlite3_value_text(argv[0]);
     748               0 :   nIn = sqlite3_value_bytes(argv[0]);
     749               0 :   if( argc==1 ){
     750                 :     static const unsigned char zSpace[] = " ";
     751               0 :     zCharSet = zSpace;
     752               0 :   }else if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
     753               0 :     return;
     754                 :   }else{
     755               0 :     zCharSet = sqlite3_value_text(argv[1]);
     756                 :   }
     757               0 :   cFirst = zCharSet[0];
     758               0 :   if( cFirst ){
     759               0 :     flags = (int)sqlite3_user_data(context);
     760               0 :     if( flags & 1 ){
     761               0 :       for(; nIn>0; nIn--, zIn++){
     762               0 :         if( cFirst==zIn[0] ) continue;
     763               0 :         for(i=1; zCharSet[i] && zCharSet[i]!=zIn[0]; i++){}
     764               0 :         if( zCharSet[i]==0 ) break;
     765                 :       }
     766                 :     }
     767               0 :     if( flags & 2 ){
     768               0 :       for(; nIn>0; nIn--){
     769               0 :         cNext = zIn[nIn-1];
     770               0 :         if( cFirst==cNext ) continue;
     771               0 :         for(i=1; zCharSet[i] && zCharSet[i]!=cNext; i++){}
     772               0 :         if( zCharSet[i]==0 ) break;
     773                 :       }
     774                 :     }
     775                 :   }
     776               0 :   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
     777                 : }
     778                 : 
     779                 : #ifdef SQLITE_SOUNDEX
     780                 : /*
     781                 : ** Compute the soundex encoding of a word.
     782                 : */
     783                 : static void soundexFunc(
     784                 :   sqlite3_context *context,
     785                 :   int argc,
     786                 :   sqlite3_value **argv
     787                 : ){
     788                 :   char zResult[8];
     789                 :   const u8 *zIn;
     790                 :   int i, j;
     791                 :   static const unsigned char iCode[] = {
     792                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     793                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     794                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     795                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     796                 :     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
     797                 :     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
     798                 :     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
     799                 :     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
     800                 :   };
     801                 :   assert( argc==1 );
     802                 :   zIn = (u8*)sqlite3_value_text(argv[0]);
     803                 :   if( zIn==0 ) zIn = (u8*)"";
     804                 :   for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
     805                 :   if( zIn[i] ){
     806                 :     u8 prevcode = iCode[zIn[i]&0x7f];
     807                 :     zResult[0] = toupper(zIn[i]);
     808                 :     for(j=1; j<4 && zIn[i]; i++){
     809                 :       int code = iCode[zIn[i]&0x7f];
     810                 :       if( code>0 ){
     811                 :         if( code!=prevcode ){
     812                 :           prevcode = code;
     813                 :           zResult[j++] = code + '0';
     814                 :         }
     815                 :       }else{
     816                 :         prevcode = 0;
     817                 :       }
     818                 :     }
     819                 :     while( j<4 ){
     820                 :       zResult[j++] = '0';
     821                 :     }
     822                 :     zResult[j] = 0;
     823                 :     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
     824                 :   }else{
     825                 :     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
     826                 :   }
     827                 : }
     828                 : #endif
     829                 : 
     830                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
     831                 : /*
     832                 : ** A function that loads a shared-library extension then returns NULL.
     833                 : */
     834               0 : static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
     835               0 :   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
     836               0 :   const char *zProc = 0;
     837               0 :   sqlite3 *db = sqlite3_user_data(context);
     838               0 :   char *zErrMsg = 0;
     839                 : 
     840               0 :   if( argc==2 ){
     841               0 :     zProc = (const char *)sqlite3_value_text(argv[1]);
     842                 :   }
     843               0 :   if( sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
     844               0 :     sqlite3_result_error(context, zErrMsg, -1);
     845               0 :     sqlite3_free(zErrMsg);
     846                 :   }
     847               0 : }
     848                 : #endif
     849                 : 
     850                 : #ifdef SQLITE_TEST
     851                 : /*
     852                 : ** This function generates a string of random characters.  Used for
     853                 : ** generating test data.
     854                 : */
     855                 : static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
     856                 :   static const unsigned char zSrc[] = 
     857                 :      "abcdefghijklmnopqrstuvwxyz"
     858                 :      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     859                 :      "0123456789"
     860                 :      ".-!,:*^+=_|?/<> ";
     861                 :   int iMin, iMax, n, r, i;
     862                 :   unsigned char zBuf[1000];
     863                 :   if( argc>=1 ){
     864                 :     iMin = sqlite3_value_int(argv[0]);
     865                 :     if( iMin<0 ) iMin = 0;
     866                 :     if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
     867                 :   }else{
     868                 :     iMin = 1;
     869                 :   }
     870                 :   if( argc>=2 ){
     871                 :     iMax = sqlite3_value_int(argv[1]);
     872                 :     if( iMax<iMin ) iMax = iMin;
     873                 :     if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
     874                 :   }else{
     875                 :     iMax = 50;
     876                 :   }
     877                 :   n = iMin;
     878                 :   if( iMax>iMin ){
     879                 :     sqlite3Randomness(sizeof(r), &r);
     880                 :     r &= 0x7fffffff;
     881                 :     n += r%(iMax + 1 - iMin);
     882                 :   }
     883                 :   assert( n<sizeof(zBuf) );
     884                 :   sqlite3Randomness(n, zBuf);
     885                 :   for(i=0; i<n; i++){
     886                 :     zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
     887                 :   }
     888                 :   zBuf[n] = 0;
     889                 :   sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
     890                 : }
     891                 : #endif /* SQLITE_TEST */
     892                 : 
     893                 : #ifdef SQLITE_TEST
     894                 : /*
     895                 : ** The following two SQL functions are used to test returning a text
     896                 : ** result with a destructor. Function 'test_destructor' takes one argument
     897                 : ** and returns the same argument interpreted as TEXT. A destructor is
     898                 : ** passed with the sqlite3_result_text() call.
     899                 : **
     900                 : ** SQL function 'test_destructor_count' returns the number of outstanding 
     901                 : ** allocations made by 'test_destructor';
     902                 : **
     903                 : ** WARNING: Not threadsafe.
     904                 : */
     905                 : static int test_destructor_count_var = 0;
     906                 : static void destructor(void *p){
     907                 :   char *zVal = (char *)p;
     908                 :   assert(zVal);
     909                 :   zVal--;
     910                 :   sqliteFree(zVal);
     911                 :   test_destructor_count_var--;
     912                 : }
     913                 : static void test_destructor(
     914                 :   sqlite3_context *pCtx, 
     915                 :   int nArg,
     916                 :   sqlite3_value **argv
     917                 : ){
     918                 :   char *zVal;
     919                 :   int len;
     920                 :   sqlite3 *db = sqlite3_user_data(pCtx);
     921                 :  
     922                 :   test_destructor_count_var++;
     923                 :   assert( nArg==1 );
     924                 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
     925                 :   len = sqlite3ValueBytes(argv[0], ENC(db)); 
     926                 :   zVal = sqliteMalloc(len+3);
     927                 :   zVal[len] = 0;
     928                 :   zVal[len-1] = 0;
     929                 :   assert( zVal );
     930                 :   zVal++;
     931                 :   memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
     932                 :   if( ENC(db)==SQLITE_UTF8 ){
     933                 :     sqlite3_result_text(pCtx, zVal, -1, destructor);
     934                 : #ifndef SQLITE_OMIT_UTF16
     935                 :   }else if( ENC(db)==SQLITE_UTF16LE ){
     936                 :     sqlite3_result_text16le(pCtx, zVal, -1, destructor);
     937                 :   }else{
     938                 :     sqlite3_result_text16be(pCtx, zVal, -1, destructor);
     939                 : #endif /* SQLITE_OMIT_UTF16 */
     940                 :   }
     941                 : }
     942                 : static void test_destructor_count(
     943                 :   sqlite3_context *pCtx, 
     944                 :   int nArg,
     945                 :   sqlite3_value **argv
     946                 : ){
     947                 :   sqlite3_result_int(pCtx, test_destructor_count_var);
     948                 : }
     949                 : #endif /* SQLITE_TEST */
     950                 : 
     951                 : #ifdef SQLITE_TEST
     952                 : /*
     953                 : ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
     954                 : ** interface.
     955                 : **
     956                 : ** The test_auxdata() SQL function attempts to register each of its arguments
     957                 : ** as auxiliary data.  If there are no prior registrations of aux data for
     958                 : ** that argument (meaning the argument is not a constant or this is its first
     959                 : ** call) then the result for that argument is 0.  If there is a prior
     960                 : ** registration, the result for that argument is 1.  The overall result
     961                 : ** is the individual argument results separated by spaces.
     962                 : */
     963                 : static void free_test_auxdata(void *p) {sqliteFree(p);}
     964                 : static void test_auxdata(
     965                 :   sqlite3_context *pCtx, 
     966                 :   int nArg,
     967                 :   sqlite3_value **argv
     968                 : ){
     969                 :   int i;
     970                 :   char *zRet = sqliteMalloc(nArg*2);
     971                 :   if( !zRet ) return;
     972                 :   for(i=0; i<nArg; i++){
     973                 :     char const *z = (char*)sqlite3_value_text(argv[i]);
     974                 :     if( z ){
     975                 :       char *zAux = sqlite3_get_auxdata(pCtx, i);
     976                 :       if( zAux ){
     977                 :         zRet[i*2] = '1';
     978                 :         if( strcmp(zAux, z) ){
     979                 :           sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
     980                 :           return;
     981                 :         }
     982                 :       }else{
     983                 :         zRet[i*2] = '0';
     984                 :         zAux = sqliteStrDup(z);
     985                 :         sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
     986                 :       }
     987                 :       zRet[i*2+1] = ' ';
     988                 :     }
     989                 :   }
     990                 :   sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
     991                 : }
     992                 : #endif /* SQLITE_TEST */
     993                 : 
     994                 : #ifdef SQLITE_TEST
     995                 : /*
     996                 : ** A function to test error reporting from user functions. This function
     997                 : ** returns a copy of it's first argument as an error.
     998                 : */
     999                 : static void test_error(
    1000                 :   sqlite3_context *pCtx, 
    1001                 :   int nArg,
    1002                 :   sqlite3_value **argv
    1003                 : ){
    1004                 :   sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
    1005                 : }
    1006                 : #endif /* SQLITE_TEST */
    1007                 : 
    1008                 : /*
    1009                 : ** An instance of the following structure holds the context of a
    1010                 : ** sum() or avg() aggregate computation.
    1011                 : */
    1012                 : typedef struct SumCtx SumCtx;
    1013                 : struct SumCtx {
    1014                 :   double rSum;      /* Floating point sum */
    1015                 :   i64 iSum;         /* Integer sum */   
    1016                 :   i64 cnt;          /* Number of elements summed */
    1017                 :   u8 overflow;      /* True if integer overflow seen */
    1018                 :   u8 approx;        /* True if non-integer value was input to the sum */
    1019                 : };
    1020                 : 
    1021                 : /*
    1022                 : ** Routines used to compute the sum, average, and total.
    1023                 : **
    1024                 : ** The SUM() function follows the (broken) SQL standard which means
    1025                 : ** that it returns NULL if it sums over no inputs.  TOTAL returns
    1026                 : ** 0.0 in that case.  In addition, TOTAL always returns a float where
    1027                 : ** SUM might return an integer if it never encounters a floating point
    1028                 : ** value.  TOTAL never fails, but SUM might through an exception if
    1029                 : ** it overflows an integer.
    1030                 : */
    1031               0 : static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
    1032                 :   SumCtx *p;
    1033                 :   int type;
    1034                 :   assert( argc==1 );
    1035               0 :   p = sqlite3_aggregate_context(context, sizeof(*p));
    1036               0 :   type = sqlite3_value_numeric_type(argv[0]);
    1037               0 :   if( p && type!=SQLITE_NULL ){
    1038               0 :     p->cnt++;
    1039               0 :     if( type==SQLITE_INTEGER ){
    1040               0 :       i64 v = sqlite3_value_int64(argv[0]);
    1041               0 :       p->rSum += v;
    1042               0 :       if( (p->approx|p->overflow)==0 ){
    1043               0 :         i64 iNewSum = p->iSum + v;
    1044               0 :         int s1 = p->iSum >> (sizeof(i64)*8-1);
    1045               0 :         int s2 = v       >> (sizeof(i64)*8-1);
    1046               0 :         int s3 = iNewSum >> (sizeof(i64)*8-1);
    1047               0 :         p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
    1048               0 :         p->iSum = iNewSum;
    1049                 :       }
    1050                 :     }else{
    1051               0 :       p->rSum += sqlite3_value_double(argv[0]);
    1052               0 :       p->approx = 1;
    1053                 :     }
    1054                 :   }
    1055               0 : }
    1056               0 : static void sumFinalize(sqlite3_context *context){
    1057                 :   SumCtx *p;
    1058               0 :   p = sqlite3_aggregate_context(context, 0);
    1059               0 :   if( p && p->cnt>0 ){
    1060               0 :     if( p->overflow ){
    1061               0 :       sqlite3_result_error(context,"integer overflow",-1);
    1062               0 :     }else if( p->approx ){
    1063               0 :       sqlite3_result_double(context, p->rSum);
    1064                 :     }else{
    1065               0 :       sqlite3_result_int64(context, p->iSum);
    1066                 :     }
    1067                 :   }
    1068               0 : }
    1069               0 : static void avgFinalize(sqlite3_context *context){
    1070                 :   SumCtx *p;
    1071               0 :   p = sqlite3_aggregate_context(context, 0);
    1072               0 :   if( p && p->cnt>0 ){
    1073               0 :     sqlite3_result_double(context, p->rSum/(double)p->cnt);
    1074                 :   }
    1075               0 : }
    1076               0 : static void totalFinalize(sqlite3_context *context){
    1077                 :   SumCtx *p;
    1078               0 :   p = sqlite3_aggregate_context(context, 0);
    1079               0 :   sqlite3_result_double(context, p ? p->rSum : 0.0);
    1080               0 : }
    1081                 : 
    1082                 : /*
    1083                 : ** The following structure keeps track of state information for the
    1084                 : ** count() aggregate function.
    1085                 : */
    1086                 : typedef struct CountCtx CountCtx;
    1087                 : struct CountCtx {
    1088                 :   i64 n;
    1089                 : };
    1090                 : 
    1091                 : /*
    1092                 : ** Routines to implement the count() aggregate function.
    1093                 : */
    1094              43 : static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
    1095                 :   CountCtx *p;
    1096              43 :   p = sqlite3_aggregate_context(context, sizeof(*p));
    1097              43 :   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
    1098              43 :     p->n++;
    1099                 :   }
    1100              43 : }   
    1101              14 : static void countFinalize(sqlite3_context *context){
    1102                 :   CountCtx *p;
    1103              14 :   p = sqlite3_aggregate_context(context, 0);
    1104              14 :   sqlite3_result_int64(context, p ? p->n : 0);
    1105              14 : }
    1106                 : 
    1107                 : /*
    1108                 : ** Routines to implement min() and max() aggregate functions.
    1109                 : */
    1110               0 : static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
    1111               0 :   Mem *pArg  = (Mem *)argv[0];
    1112                 :   Mem *pBest;
    1113                 : 
    1114               0 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
    1115               0 :   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
    1116               0 :   if( !pBest ) return;
    1117                 : 
    1118               0 :   if( pBest->flags ){
    1119                 :     int max;
    1120                 :     int cmp;
    1121               0 :     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
    1122                 :     /* This step function is used for both the min() and max() aggregates,
    1123                 :     ** the only difference between the two being that the sense of the
    1124                 :     ** comparison is inverted. For the max() aggregate, the
    1125                 :     ** sqlite3_user_data() function returns (void *)-1. For min() it
    1126                 :     ** returns (void *)db, where db is the sqlite3* database pointer.
    1127                 :     ** Therefore the next statement sets variable 'max' to 1 for the max()
    1128                 :     ** aggregate, or 0 for min().
    1129                 :     */
    1130               0 :     max = sqlite3_user_data(context)!=0;
    1131               0 :     cmp = sqlite3MemCompare(pBest, pArg, pColl);
    1132               0 :     if( (max && cmp<0) || (!max && cmp>0) ){
    1133               0 :       sqlite3VdbeMemCopy(pBest, pArg);
    1134                 :     }
    1135                 :   }else{
    1136               0 :     sqlite3VdbeMemCopy(pBest, pArg);
    1137                 :   }
    1138                 : }
    1139               0 : static void minMaxFinalize(sqlite3_context *context){
    1140                 :   sqlite3_value *pRes;
    1141               0 :   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
    1142               0 :   if( pRes ){
    1143               0 :     if( pRes->flags ){
    1144               0 :       sqlite3_result_value(context, pRes);
    1145                 :     }
    1146               0 :     sqlite3VdbeMemRelease(pRes);
    1147                 :   }
    1148               0 : }
    1149                 : 
    1150                 : 
    1151                 : /*
    1152                 : ** This function registered all of the above C functions as SQL
    1153                 : ** functions.  This should be the only routine in this file with
    1154                 : ** external linkage.
    1155                 : */
    1156             130 : void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
    1157                 :   static const struct {
    1158                 :      char *zName;
    1159                 :      signed char nArg;
    1160                 :      u8 argType;           /* ff: db   1: 0, 2: 1, 3: 2,...  N:  N-1. */
    1161                 :      u8 eTextRep;          /* 1: UTF-16.  0: UTF-8 */
    1162                 :      u8 needCollSeq;
    1163                 :      void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
    1164                 :   } aFuncs[] = {
    1165                 :     { "min",               -1, 0, SQLITE_UTF8,    1, minmaxFunc },
    1166                 :     { "min",                0, 0, SQLITE_UTF8,    1, 0          },
    1167                 :     { "max",               -1, 1, SQLITE_UTF8,    1, minmaxFunc },
    1168                 :     { "max",                0, 1, SQLITE_UTF8,    1, 0          },
    1169                 :     { "typeof",             1, 0, SQLITE_UTF8,    0, typeofFunc },
    1170                 :     { "length",             1, 0, SQLITE_UTF8,    0, lengthFunc },
    1171                 :     { "substr",             3, 0, SQLITE_UTF8,    0, substrFunc },
    1172                 : #ifndef SQLITE_OMIT_UTF16
    1173                 :     { "substr",             3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
    1174                 : #endif
    1175                 :     { "abs",                1, 0, SQLITE_UTF8,    0, absFunc    },
    1176                 :     { "round",              1, 0, SQLITE_UTF8,    0, roundFunc  },
    1177                 :     { "round",              2, 0, SQLITE_UTF8,    0, roundFunc  },
    1178                 :     { "upper",              1, 0, SQLITE_UTF8,    0, upperFunc  },
    1179                 :     { "lower",              1, 0, SQLITE_UTF8,    0, lowerFunc  },
    1180                 :     { "coalesce",          -1, 0, SQLITE_UTF8,    0, ifnullFunc },
    1181                 :     { "coalesce",           0, 0, SQLITE_UTF8,    0, 0          },
    1182                 :     { "coalesce",           1, 0, SQLITE_UTF8,    0, 0          },
    1183                 :     { "hex",                1, 0, SQLITE_UTF8,    0, hexFunc    },
    1184                 :     { "ifnull",             2, 0, SQLITE_UTF8,    1, ifnullFunc },
    1185                 :     { "random",            -1, 0, SQLITE_UTF8,    0, randomFunc },
    1186                 :     { "randomblob",         1, 0, SQLITE_UTF8,    0, randomBlob },
    1187                 :     { "nullif",             2, 0, SQLITE_UTF8,    1, nullifFunc },
    1188                 :     { "sqlite_version",     0, 0, SQLITE_UTF8,    0, versionFunc},
    1189                 :     { "quote",              1, 0, SQLITE_UTF8,    0, quoteFunc  },
    1190                 :     { "last_insert_rowid",  0, 0xff, SQLITE_UTF8, 0, last_insert_rowid },
    1191                 :     { "changes",            0, 0xff, SQLITE_UTF8, 0, changes           },
    1192                 :     { "total_changes",      0, 0xff, SQLITE_UTF8, 0, total_changes     },
    1193                 :     { "replace",            3, 0, SQLITE_UTF8,    0, replaceFunc       },
    1194                 :     { "ltrim",              1, 1, SQLITE_UTF8,    0, trimFunc          },
    1195                 :     { "ltrim",              2, 1, SQLITE_UTF8,    0, trimFunc          },
    1196                 :     { "rtrim",              1, 2, SQLITE_UTF8,    0, trimFunc          },
    1197                 :     { "rtrim",              2, 2, SQLITE_UTF8,    0, trimFunc          },
    1198                 :     { "trim",               1, 3, SQLITE_UTF8,    0, trimFunc          },
    1199                 :     { "trim",               2, 3, SQLITE_UTF8,    0, trimFunc          },
    1200                 : #ifdef SQLITE_SOUNDEX
    1201                 :     { "soundex",            1, 0, SQLITE_UTF8,    0, soundexFunc},
    1202                 : #endif
    1203                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
    1204                 :     { "load_extension",     1, 0xff, SQLITE_UTF8, 0, loadExt },
    1205                 :     { "load_extension",     2, 0xff, SQLITE_UTF8, 0, loadExt },
    1206                 : #endif
    1207                 : #ifdef SQLITE_TEST
    1208                 :     { "randstr",               2, 0,    SQLITE_UTF8, 0, randStr    },
    1209                 :     { "test_destructor",       1, 0xff, SQLITE_UTF8, 0, test_destructor},
    1210                 :     { "test_destructor_count", 0, 0,    SQLITE_UTF8, 0, test_destructor_count},
    1211                 :     { "test_auxdata",         -1, 0,    SQLITE_UTF8, 0, test_auxdata},
    1212                 :     { "test_error",            1, 0,    SQLITE_UTF8, 0, test_error},
    1213                 : #endif
    1214                 :   };
    1215                 :   static const struct {
    1216                 :     char *zName;
    1217                 :     signed char nArg;
    1218                 :     u8 argType;
    1219                 :     u8 needCollSeq;
    1220                 :     void (*xStep)(sqlite3_context*,int,sqlite3_value**);
    1221                 :     void (*xFinalize)(sqlite3_context*);
    1222                 :   } aAggs[] = {
    1223                 :     { "min",    1, 0, 1, minmaxStep,   minMaxFinalize },
    1224                 :     { "max",    1, 1, 1, minmaxStep,   minMaxFinalize },
    1225                 :     { "sum",    1, 0, 0, sumStep,      sumFinalize    },
    1226                 :     { "total",  1, 0, 0, sumStep,      totalFinalize    },
    1227                 :     { "avg",    1, 0, 0, sumStep,      avgFinalize    },
    1228                 :     { "count",  0, 0, 0, countStep,    countFinalize  },
    1229                 :     { "count",  1, 0, 0, countStep,    countFinalize  },
    1230                 :   };
    1231                 :   int i;
    1232                 : 
    1233            4680 :   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
    1234                 :     void *pArg;
    1235            4550 :     u8 argType = aFuncs[i].argType;
    1236            4550 :     if( argType==0xff ){
    1237             650 :       pArg = db;
    1238                 :     }else{
    1239            3900 :       pArg = (void*)(int)argType;
    1240                 :     }
    1241            4550 :     sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
    1242                 :         aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
    1243            4550 :     if( aFuncs[i].needCollSeq ){
    1244                 :       FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 
    1245             780 :           strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
    1246             780 :       if( pFunc && aFuncs[i].needCollSeq ){
    1247             520 :         pFunc->needCollSeq = 1;
    1248                 :       }
    1249                 :     }
    1250                 :   }
    1251                 : #ifndef SQLITE_OMIT_ALTERTABLE
    1252             130 :   sqlite3AlterFunctions(db);
    1253                 : #endif
    1254                 : #ifndef SQLITE_OMIT_PARSER
    1255             130 :   sqlite3AttachFunctions(db);
    1256                 : #endif
    1257            1040 :   for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
    1258             910 :     void *pArg = (void*)(int)aAggs[i].argType;
    1259             910 :     sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 
    1260                 :         pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
    1261             910 :     if( aAggs[i].needCollSeq ){
    1262                 :       FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
    1263             260 :           strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
    1264             260 :       if( pFunc && aAggs[i].needCollSeq ){
    1265             260 :         pFunc->needCollSeq = 1;
    1266                 :       }
    1267                 :     }
    1268                 :   }
    1269             130 :   sqlite3RegisterDateTimeFunctions(db);
    1270             130 :   sqlite3_overload_function(db, "MATCH", 2);
    1271                 : #ifdef SQLITE_SSE
    1272                 :   (void)sqlite3SseFunctions(db);
    1273                 : #endif
    1274                 : #ifdef SQLITE_CASE_SENSITIVE_LIKE
    1275                 :   sqlite3RegisterLikeFunctions(db, 1);
    1276                 : #else
    1277             130 :   sqlite3RegisterLikeFunctions(db, 0);
    1278                 : #endif
    1279             130 : }
    1280                 : 
    1281                 : /*
    1282                 : ** Set the LIKEOPT flag on the 2-argument function with the given name.
    1283                 : */
    1284             260 : static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
    1285                 :   FuncDef *pDef;
    1286             260 :   pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
    1287             260 :   if( pDef ){
    1288             260 :     pDef->flags = flagVal;
    1289                 :   }
    1290             260 : }
    1291                 : 
    1292                 : /*
    1293                 : ** Register the built-in LIKE and GLOB functions.  The caseSensitive
    1294                 : ** parameter determines whether or not the LIKE operator is case
    1295                 : ** sensitive.  GLOB is always case sensitive.
    1296                 : */
    1297             130 : void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
    1298                 :   struct compareInfo *pInfo;
    1299             130 :   if( caseSensitive ){
    1300               0 :     pInfo = (struct compareInfo*)&likeInfoAlt;
    1301                 :   }else{
    1302             130 :     pInfo = (struct compareInfo*)&likeInfoNorm;
    1303                 :   }
    1304             130 :   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
    1305             130 :   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
    1306             130 :   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
    1307                 :       (struct compareInfo*)&globInfo, likeFunc, 0,0);
    1308             130 :   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
    1309             130 :   setLikeOptFlag(db, "like", 
    1310                 :       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
    1311             130 : }
    1312                 : 
    1313                 : /*
    1314                 : ** pExpr points to an expression which implements a function.  If
    1315                 : ** it is appropriate to apply the LIKE optimization to that function
    1316                 : ** then set aWc[0] through aWc[2] to the wildcard characters and
    1317                 : ** return TRUE.  If the function is not a LIKE-style function then
    1318                 : ** return FALSE.
    1319                 : */
    1320             155 : int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
    1321                 :   FuncDef *pDef;
    1322             155 :   if( pExpr->op!=TK_FUNCTION ){
    1323             155 :     return 0;
    1324                 :   }
    1325               0 :   if( pExpr->pList->nExpr!=2 ){
    1326               0 :     return 0;
    1327                 :   }
    1328               0 :   pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
    1329                 :                              SQLITE_UTF8, 0);
    1330               0 :   if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
    1331               0 :     return 0;
    1332                 :   }
    1333                 : 
    1334                 :   /* The memcpy() statement assumes that the wildcard characters are
    1335                 :   ** the first three statements in the compareInfo structure.  The
    1336                 :   ** asserts() that follow verify that assumption
    1337                 :   */
    1338               0 :   memcpy(aWc, pDef->pUserData, 3);
    1339                 :   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
    1340                 :   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
    1341                 :   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
    1342               0 :   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
    1343               0 :   return 1;
    1344                 : }

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:16 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.