xref: /sqlite-3.40.0/src/func.c (revision b7481e70)
1dc04c583Sdrh /*
2dc04c583Sdrh ** 2002 February 23
3dc04c583Sdrh **
4dc04c583Sdrh ** The author disclaims copyright to this source code.  In place of
5dc04c583Sdrh ** a legal notice, here is a blessing:
6dc04c583Sdrh **
7dc04c583Sdrh **    May you do good and not evil.
8dc04c583Sdrh **    May you find forgiveness for yourself and forgive others.
9dc04c583Sdrh **    May you share freely, never taking more than you give.
10dc04c583Sdrh **
11dc04c583Sdrh *************************************************************************
12dc04c583Sdrh ** This file contains the C functions that implement various SQL
13dc04c583Sdrh ** functions of SQLite.
14dc04c583Sdrh **
15dc04c583Sdrh ** There is only one exported symbol in this file - the function
16dc04c583Sdrh ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
17dc04c583Sdrh ** All other code has file scope.
18dc04c583Sdrh **
19*b7481e70Sdrh ** $Id: func.c,v 1.134 2006/09/16 21:45:14 drh Exp $
20dc04c583Sdrh */
21b659e9bfSdrh #include "sqliteInt.h"
22dc04c583Sdrh #include <ctype.h>
23b37df7b9Sdrh /* #include <math.h> */
24d3a149efSdrh #include <stdlib.h>
250bce8354Sdrh #include <assert.h>
2688208050Sdanielk1977 #include "vdbeInt.h"
27771d8c3bSdrh #include "os.h"
280bce8354Sdrh 
2955ef4d97Sdrh /*
3055ef4d97Sdrh ** Return the collating function associated with a function.
3155ef4d97Sdrh */
32dc1bdc4fSdanielk1977 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
33dc1bdc4fSdanielk1977   return context->pColl;
34dc1bdc4fSdanielk1977 }
35dc1bdc4fSdanielk1977 
360bce8354Sdrh /*
370bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions
380bce8354Sdrh */
39f9b596ebSdrh static void minmaxFunc(
40f9b596ebSdrh   sqlite3_context *context,
41f9b596ebSdrh   int argc,
42f9b596ebSdrh   sqlite3_value **argv
43f9b596ebSdrh ){
440bce8354Sdrh   int i;
45268380caSdrh   int mask;    /* 0 for min() or 0xffffffff for max() */
46f9b596ebSdrh   int iBest;
47dc1bdc4fSdanielk1977   CollSeq *pColl;
480bce8354Sdrh 
4989425d5eSdrh   if( argc==0 ) return;
50c44af71cSdrh   mask = sqlite3_user_data(context)==0 ? 0 : -1;
51dc1bdc4fSdanielk1977   pColl = sqlite3GetFuncCollSeq(context);
52dc1bdc4fSdanielk1977   assert( pColl );
53c572ef7fSdanielk1977   assert( mask==-1 || mask==0 );
54f9b596ebSdrh   iBest = 0;
559c054830Sdrh   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
56f9b596ebSdrh   for(i=1; i<argc; i++){
579c054830Sdrh     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
58dc1bdc4fSdanielk1977     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
59f9b596ebSdrh       iBest = i;
600bce8354Sdrh     }
610bce8354Sdrh   }
62f4479501Sdrh   sqlite3_result_value(context, argv[iBest]);
630bce8354Sdrh }
640bce8354Sdrh 
65268380caSdrh /*
66268380caSdrh ** Return the type of the argument.
67268380caSdrh */
68f9b596ebSdrh static void typeofFunc(
69f9b596ebSdrh   sqlite3_context *context,
70f9b596ebSdrh   int argc,
71f9b596ebSdrh   sqlite3_value **argv
72f9b596ebSdrh ){
7335bb9d02Sdanielk1977   const char *z = 0;
7435bb9d02Sdanielk1977   switch( sqlite3_value_type(argv[0]) ){
759c054830Sdrh     case SQLITE_NULL:    z = "null";    break;
769c054830Sdrh     case SQLITE_INTEGER: z = "integer"; break;
779c054830Sdrh     case SQLITE_TEXT:    z = "text";    break;
789c054830Sdrh     case SQLITE_FLOAT:   z = "real";    break;
799c054830Sdrh     case SQLITE_BLOB:    z = "blob";    break;
8035bb9d02Sdanielk1977   }
81d8123366Sdanielk1977   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
820bce8354Sdrh }
830bce8354Sdrh 
845708d2deSdrh 
855708d2deSdrh /*
860bce8354Sdrh ** Implementation of the length() function
870bce8354Sdrh */
88f9b596ebSdrh static void lengthFunc(
89f9b596ebSdrh   sqlite3_context *context,
90f9b596ebSdrh   int argc,
91f9b596ebSdrh   sqlite3_value **argv
92f9b596ebSdrh ){
930bce8354Sdrh   int len;
940bce8354Sdrh 
950bce8354Sdrh   assert( argc==1 );
96f9b596ebSdrh   switch( sqlite3_value_type(argv[0]) ){
979c054830Sdrh     case SQLITE_BLOB:
989c054830Sdrh     case SQLITE_INTEGER:
999c054830Sdrh     case SQLITE_FLOAT: {
100f4479501Sdrh       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
101f9b596ebSdrh       break;
102f9b596ebSdrh     }
1039c054830Sdrh     case SQLITE_TEXT: {
1042646da7eSdrh       const unsigned char *z = sqlite3_value_text(argv[0]);
1050bce8354Sdrh       for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
106f4479501Sdrh       sqlite3_result_int(context, len);
107f9b596ebSdrh       break;
108f9b596ebSdrh     }
109f9b596ebSdrh     default: {
110f9b596ebSdrh       sqlite3_result_null(context);
111f9b596ebSdrh       break;
112f9b596ebSdrh     }
113f9b596ebSdrh   }
1140bce8354Sdrh }
1150bce8354Sdrh 
1160bce8354Sdrh /*
1170bce8354Sdrh ** Implementation of the abs() function
1180bce8354Sdrh */
1190ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1200bce8354Sdrh   assert( argc==1 );
121f9b596ebSdrh   switch( sqlite3_value_type(argv[0]) ){
1229c054830Sdrh     case SQLITE_INTEGER: {
123f93bbbeaSdanielk1977       i64 iVal = sqlite3_value_int64(argv[0]);
12452fc849aSdrh       if( iVal<0 ){
12552fc849aSdrh         if( (iVal<<1)==0 ){
12652fc849aSdrh           sqlite3_result_error(context, "integer overflow", -1);
12752fc849aSdrh           return;
12852fc849aSdrh         }
12952fc849aSdrh         iVal = -iVal;
13052fc849aSdrh       }
131f93bbbeaSdanielk1977       sqlite3_result_int64(context, iVal);
132f9b596ebSdrh       break;
133f9b596ebSdrh     }
1349c054830Sdrh     case SQLITE_NULL: {
135f9b596ebSdrh       sqlite3_result_null(context);
136f9b596ebSdrh       break;
137f9b596ebSdrh     }
138f9b596ebSdrh     default: {
139f93bbbeaSdanielk1977       double rVal = sqlite3_value_double(argv[0]);
14052fc849aSdrh       if( rVal<0 ) rVal = -rVal;
141f93bbbeaSdanielk1977       sqlite3_result_double(context, rVal);
142f9b596ebSdrh       break;
143f9b596ebSdrh     }
144f9b596ebSdrh   }
1450bce8354Sdrh }
1460bce8354Sdrh 
1470bce8354Sdrh /*
1480bce8354Sdrh ** Implementation of the substr() function
1490bce8354Sdrh */
150f9b596ebSdrh static void substrFunc(
151f9b596ebSdrh   sqlite3_context *context,
152f9b596ebSdrh   int argc,
153f9b596ebSdrh   sqlite3_value **argv
154f9b596ebSdrh ){
1552646da7eSdrh   const unsigned char *z;
1562646da7eSdrh   const unsigned char *z2;
1570bce8354Sdrh   int i;
1580bce8354Sdrh   int p1, p2, len;
159f9b596ebSdrh 
1600bce8354Sdrh   assert( argc==3 );
1614f26d6c4Sdrh   z = sqlite3_value_text(argv[0]);
1620bce8354Sdrh   if( z==0 ) return;
16351ad0ecdSdanielk1977   p1 = sqlite3_value_int(argv[1]);
16451ad0ecdSdanielk1977   p2 = sqlite3_value_int(argv[2]);
16547c8a679Sdrh   for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
1660bce8354Sdrh   if( p1<0 ){
16789425d5eSdrh     p1 += len;
168653bc759Sdrh     if( p1<0 ){
169653bc759Sdrh       p2 += p1;
170653bc759Sdrh       p1 = 0;
171653bc759Sdrh     }
1720bce8354Sdrh   }else if( p1>0 ){
1730bce8354Sdrh     p1--;
1740bce8354Sdrh   }
1750bce8354Sdrh   if( p1+p2>len ){
1760bce8354Sdrh     p2 = len-p1;
1770bce8354Sdrh   }
17877396304Sdrh   for(i=0; i<p1 && z[i]; i++){
17947c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p1++;
1800bce8354Sdrh   }
18147c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
18277396304Sdrh   for(; i<p1+p2 && z[i]; i++){
18347c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p2++;
1840bce8354Sdrh   }
18547c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
186653bc759Sdrh   if( p2<0 ) p2 = 0;
1872646da7eSdrh   sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
1880bce8354Sdrh }
1890bce8354Sdrh 
1900bce8354Sdrh /*
1910bce8354Sdrh ** Implementation of the round() function
1920bce8354Sdrh */
1930ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
19451ad0ecdSdanielk1977   int n = 0;
1950bce8354Sdrh   double r;
196592ac8cbSdrh   char zBuf[500];  /* larger than the %f representation of the largest double */
1970bce8354Sdrh   assert( argc==1 || argc==2 );
19851ad0ecdSdanielk1977   if( argc==2 ){
1999c054830Sdrh     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
20051ad0ecdSdanielk1977     n = sqlite3_value_int(argv[1]);
2010bce8354Sdrh     if( n>30 ) n = 30;
2020bce8354Sdrh     if( n<0 ) n = 0;
20351ad0ecdSdanielk1977   }
204d589a92aSdrh   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
2054f26d6c4Sdrh   r = sqlite3_value_double(argv[0]);
206e866fcb9Sdrh   sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
207502b962bSdrh   sqlite3AtoF(zBuf, &r);
208502b962bSdrh   sqlite3_result_double(context, r);
2090bce8354Sdrh }
210dc04c583Sdrh 
211dc04c583Sdrh /*
212dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions.
213dc04c583Sdrh */
2140ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2158cd9db00Sdrh   unsigned char *z;
216dc04c583Sdrh   int i;
2179c054830Sdrh   if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
218c572ef7fSdanielk1977   z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
219dc04c583Sdrh   if( z==0 ) return;
2202646da7eSdrh   strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
221dc04c583Sdrh   for(i=0; z[i]; i++){
2224c755c0fSdrh     z[i] = toupper(z[i]);
223dc04c583Sdrh   }
2242646da7eSdrh   sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
2257e18c259Sdanielk1977   sqliteFree(z);
226dc04c583Sdrh }
2270ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2288cd9db00Sdrh   unsigned char *z;
229dc04c583Sdrh   int i;
2309c054830Sdrh   if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
231c572ef7fSdanielk1977   z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
232dc04c583Sdrh   if( z==0 ) return;
2332646da7eSdrh   strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
234dc04c583Sdrh   for(i=0; z[i]; i++){
2354c755c0fSdrh     z[i] = tolower(z[i]);
236dc04c583Sdrh   }
2372646da7eSdrh   sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
2387e18c259Sdanielk1977   sqliteFree(z);
239dc04c583Sdrh }
240dc04c583Sdrh 
241dc04c583Sdrh /*
242fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
243b6c9e6e6Sjplyon ** All three do the same thing.  They return the first non-NULL
244b6c9e6e6Sjplyon ** argument.
2453212e182Sdrh */
246f9b596ebSdrh static void ifnullFunc(
247f9b596ebSdrh   sqlite3_context *context,
248f9b596ebSdrh   int argc,
249f9b596ebSdrh   sqlite3_value **argv
250f9b596ebSdrh ){
251fbc99082Sdrh   int i;
252fbc99082Sdrh   for(i=0; i<argc; i++){
2539c054830Sdrh     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
254f4479501Sdrh       sqlite3_result_value(context, argv[i]);
255fbc99082Sdrh       break;
256fbc99082Sdrh     }
257fbc99082Sdrh   }
2583212e182Sdrh }
2593212e182Sdrh 
2603212e182Sdrh /*
261f9ffac96Sdrh ** Implementation of random().  Return a random integer.
262f9ffac96Sdrh */
263f9b596ebSdrh static void randomFunc(
264f9b596ebSdrh   sqlite3_context *context,
265f9b596ebSdrh   int argc,
266f9b596ebSdrh   sqlite3_value **argv
267f9b596ebSdrh ){
26852fc849aSdrh   sqlite_int64 r;
2694adee20fSdanielk1977   sqlite3Randomness(sizeof(r), &r);
270874abbedSdrh   if( (r<<1)==0 ) r = 0;  /* Prevent 0x8000.... as the result so that we */
271874abbedSdrh                           /* can always do abs() of the result */
27252fc849aSdrh   sqlite3_result_int64(context, r);
273f9ffac96Sdrh }
274f9ffac96Sdrh 
275f9ffac96Sdrh /*
2766ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function.  The return
27724b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function.
2786ed41ad7Sdrh */
27951ad0ecdSdanielk1977 static void last_insert_rowid(
2800ae8b831Sdanielk1977   sqlite3_context *context,
28151ad0ecdSdanielk1977   int arg,
28251ad0ecdSdanielk1977   sqlite3_value **argv
28351ad0ecdSdanielk1977 ){
2849bb575fdSdrh   sqlite3 *db = sqlite3_user_data(context);
285f9b596ebSdrh   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
2866ed41ad7Sdrh }
2876ed41ad7Sdrh 
288f146a776Srdc /*
289b28af71aSdanielk1977 ** Implementation of the changes() SQL function.  The return value is the
290b28af71aSdanielk1977 ** same as the sqlite3_changes() API function.
291f146a776Srdc */
292b28af71aSdanielk1977 static void changes(
293f9b596ebSdrh   sqlite3_context *context,
294f9b596ebSdrh   int arg,
295f9b596ebSdrh   sqlite3_value **argv
296f9b596ebSdrh ){
2979bb575fdSdrh   sqlite3 *db = sqlite3_user_data(context);
298f4479501Sdrh   sqlite3_result_int(context, sqlite3_changes(db));
299b0c374ffSrdc }
300f146a776Srdc 
301f146a776Srdc /*
302b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function.  The return value is
303b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function.
304f146a776Srdc */
305b28af71aSdanielk1977 static void total_changes(
3060ae8b831Sdanielk1977   sqlite3_context *context,
30751ad0ecdSdanielk1977   int arg,
30851ad0ecdSdanielk1977   sqlite3_value **argv
30951ad0ecdSdanielk1977 ){
3109bb575fdSdrh   sqlite3 *db = sqlite3_user_data(context);
311b28af71aSdanielk1977   sqlite3_result_int(context, sqlite3_total_changes(db));
312b0c374ffSrdc }
313b0c374ffSrdc 
3146ed41ad7Sdrh /*
3154e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons.
316d02eb1fdSdanielk1977 */
3174e5ffc5fSdrh struct compareInfo {
3184e5ffc5fSdrh   u8 matchAll;
3194e5ffc5fSdrh   u8 matchOne;
3204e5ffc5fSdrh   u8 matchSet;
3214e5ffc5fSdrh   u8 noCase;
322d02eb1fdSdanielk1977 };
32355ef4d97Sdrh 
3244e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 };
32570031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore
32670031fa3Sdrh ** case.  Thus  'a' LIKE 'A' would be true. */
32755ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
32870031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
32970031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */
33055ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
331d02eb1fdSdanielk1977 
332d02eb1fdSdanielk1977 /*
3334e5ffc5fSdrh ** X is a pointer to the first byte of a UTF-8 character.  Increment
3344e5ffc5fSdrh ** X so that it points to the next character.  This only works right
3354e5ffc5fSdrh ** if X points to a well-formed UTF-8 string.
336d02eb1fdSdanielk1977 */
3374e5ffc5fSdrh #define sqliteNextChar(X)  while( (0xc0&*++(X))==0x80 ){}
3384e5ffc5fSdrh #define sqliteCharVal(X)   sqlite3ReadUtf8(X)
339d02eb1fdSdanielk1977 
340d02eb1fdSdanielk1977 
341d02eb1fdSdanielk1977 /*
3424e5ffc5fSdrh ** Compare two UTF-8 strings for equality where the first string can
3434e5ffc5fSdrh ** potentially be a "glob" expression.  Return true (1) if they
3444e5ffc5fSdrh ** are the same and false (0) if they are different.
3450ac65892Sdrh **
3464e5ffc5fSdrh ** Globbing rules:
3470ac65892Sdrh **
3484e5ffc5fSdrh **      '*'       Matches any sequence of zero or more characters.
349d02eb1fdSdanielk1977 **
3504e5ffc5fSdrh **      '?'       Matches exactly one character.
3514e5ffc5fSdrh **
3524e5ffc5fSdrh **     [...]      Matches one character from the enclosed list of
3534e5ffc5fSdrh **                characters.
3544e5ffc5fSdrh **
3554e5ffc5fSdrh **     [^...]     Matches one character not in the enclosed list.
3564e5ffc5fSdrh **
3574e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included
3584e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'.  A
3594e5ffc5fSdrh ** range of characters can be specified using '-'.  Example:
3604e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter.  To match a '-', make
3614e5ffc5fSdrh ** it the last character in the list.
3624e5ffc5fSdrh **
3634e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case.
3644e5ffc5fSdrh **
3654e5ffc5fSdrh ** Hints: to match '*' or '?', put them in "[]".  Like this:
3664e5ffc5fSdrh **
3674e5ffc5fSdrh **         abc[*]xyz        Matches "abc*xyz" only
3680ac65892Sdrh */
3697c6303c0Sdanielk1977 static int patternCompare(
3704e5ffc5fSdrh   const u8 *zPattern,              /* The glob pattern */
3714e5ffc5fSdrh   const u8 *zString,               /* The string to compare against the glob */
3727c6303c0Sdanielk1977   const struct compareInfo *pInfo, /* Information about how to do the compare */
3737c6303c0Sdanielk1977   const int esc                    /* The escape character */
37451ad0ecdSdanielk1977 ){
375ad7dd425Sdanielk1977   register int c;
3764e5ffc5fSdrh   int invert;
3774e5ffc5fSdrh   int seen;
3784e5ffc5fSdrh   int c2;
3794e5ffc5fSdrh   u8 matchOne = pInfo->matchOne;
3804e5ffc5fSdrh   u8 matchAll = pInfo->matchAll;
3814e5ffc5fSdrh   u8 matchSet = pInfo->matchSet;
3824e5ffc5fSdrh   u8 noCase = pInfo->noCase;
3837c6303c0Sdanielk1977   int prevEscape = 0;     /* True if the previous character was 'escape' */
384d02eb1fdSdanielk1977 
3854e5ffc5fSdrh   while( (c = *zPattern)!=0 ){
3867c6303c0Sdanielk1977     if( !prevEscape && c==matchAll ){
3874e5ffc5fSdrh       while( (c=zPattern[1]) == matchAll || c == matchOne ){
3884e5ffc5fSdrh         if( c==matchOne ){
3894e5ffc5fSdrh           if( *zString==0 ) return 0;
3904e5ffc5fSdrh           sqliteNextChar(zString);
391d02eb1fdSdanielk1977         }
3924e5ffc5fSdrh         zPattern++;
3934e5ffc5fSdrh       }
39420fc0887Sdrh       if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
3957c6303c0Sdanielk1977         u8 const *zTemp = &zPattern[1];
3967c6303c0Sdanielk1977         sqliteNextChar(zTemp);
3977c6303c0Sdanielk1977         c = *zTemp;
3987c6303c0Sdanielk1977       }
3994e5ffc5fSdrh       if( c==0 ) return 1;
4004e5ffc5fSdrh       if( c==matchSet ){
4017c6303c0Sdanielk1977         assert( esc==0 );   /* This is GLOB, not LIKE */
4027c6303c0Sdanielk1977         while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
4034e5ffc5fSdrh           sqliteNextChar(zString);
4044e5ffc5fSdrh         }
4054e5ffc5fSdrh         return *zString!=0;
406d02eb1fdSdanielk1977       }else{
4074e5ffc5fSdrh         while( (c2 = *zString)!=0 ){
4084e5ffc5fSdrh           if( noCase ){
4094e5ffc5fSdrh             c2 = sqlite3UpperToLower[c2];
4104e5ffc5fSdrh             c = sqlite3UpperToLower[c];
4114e5ffc5fSdrh             while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
412ad7dd425Sdanielk1977           }else{
4134e5ffc5fSdrh             while( c2 != 0 && c2 != c ){ c2 = *++zString; }
414ad7dd425Sdanielk1977           }
4154e5ffc5fSdrh           if( c2==0 ) return 0;
4167c6303c0Sdanielk1977           if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
4174e5ffc5fSdrh           sqliteNextChar(zString);
4184e5ffc5fSdrh         }
4194e5ffc5fSdrh         return 0;
4204e5ffc5fSdrh       }
4217c6303c0Sdanielk1977     }else if( !prevEscape && c==matchOne ){
4224e5ffc5fSdrh       if( *zString==0 ) return 0;
4234e5ffc5fSdrh       sqliteNextChar(zString);
4244e5ffc5fSdrh       zPattern++;
4254e5ffc5fSdrh     }else if( c==matchSet ){
4264e5ffc5fSdrh       int prior_c = 0;
4277c6303c0Sdanielk1977       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
4284e5ffc5fSdrh       seen = 0;
4294e5ffc5fSdrh       invert = 0;
4304e5ffc5fSdrh       c = sqliteCharVal(zString);
4314e5ffc5fSdrh       if( c==0 ) return 0;
4324e5ffc5fSdrh       c2 = *++zPattern;
4334e5ffc5fSdrh       if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
4344e5ffc5fSdrh       if( c2==']' ){
4354e5ffc5fSdrh         if( c==']' ) seen = 1;
4364e5ffc5fSdrh         c2 = *++zPattern;
4374e5ffc5fSdrh       }
4384e5ffc5fSdrh       while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
4394e5ffc5fSdrh         if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
4404e5ffc5fSdrh           zPattern++;
4414e5ffc5fSdrh           c2 = sqliteCharVal(zPattern);
4424e5ffc5fSdrh           if( c>=prior_c && c<=c2 ) seen = 1;
4434e5ffc5fSdrh           prior_c = 0;
4444e5ffc5fSdrh         }else if( c==c2 ){
4454e5ffc5fSdrh           seen = 1;
4464e5ffc5fSdrh           prior_c = c2;
447d02eb1fdSdanielk1977         }else{
4484e5ffc5fSdrh           prior_c = c2;
449d02eb1fdSdanielk1977         }
4504e5ffc5fSdrh         sqliteNextChar(zPattern);
451d02eb1fdSdanielk1977       }
4524e5ffc5fSdrh       if( c2==0 || (seen ^ invert)==0 ) return 0;
4534e5ffc5fSdrh       sqliteNextChar(zString);
4544e5ffc5fSdrh       zPattern++;
45520fc0887Sdrh     }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
4567c6303c0Sdanielk1977       prevEscape = 1;
4577c6303c0Sdanielk1977       sqliteNextChar(zPattern);
458d02eb1fdSdanielk1977     }else{
4594e5ffc5fSdrh       if( noCase ){
4604e5ffc5fSdrh         if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
4614e5ffc5fSdrh       }else{
4624e5ffc5fSdrh         if( c != *zString ) return 0;
4634e5ffc5fSdrh       }
4644e5ffc5fSdrh       zPattern++;
4654e5ffc5fSdrh       zString++;
4667c6303c0Sdanielk1977       prevEscape = 0;
46751ad0ecdSdanielk1977     }
4680ac65892Sdrh   }
4694e5ffc5fSdrh   return *zString==0;
4704e5ffc5fSdrh }
4714e5ffc5fSdrh 
47255ef4d97Sdrh /*
47355ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is
47455ef4d97Sdrh ** just a variation of LIKE) gets called.  This is used for testing
47555ef4d97Sdrh ** only.
47655ef4d97Sdrh */
47755ef4d97Sdrh #ifdef SQLITE_TEST
47855ef4d97Sdrh int sqlite3_like_count = 0;
47955ef4d97Sdrh #endif
48055ef4d97Sdrh 
4813f6b0874Sdanielk1977 
4823f6b0874Sdanielk1977 /*
4833f6b0874Sdanielk1977 ** Implementation of the like() SQL function.  This function implements
4843f6b0874Sdanielk1977 ** the build-in LIKE operator.  The first argument to the function is the
4853f6b0874Sdanielk1977 ** pattern and the second argument is the string.  So, the SQL statements:
4863f6b0874Sdanielk1977 **
4873f6b0874Sdanielk1977 **       A LIKE B
4883f6b0874Sdanielk1977 **
4893f6b0874Sdanielk1977 ** is implemented as like(B,A).
4903f6b0874Sdanielk1977 **
49155ef4d97Sdrh ** This same function (with a different compareInfo structure) computes
49255ef4d97Sdrh ** the GLOB operator.
4933f6b0874Sdanielk1977 */
4943f6b0874Sdanielk1977 static void likeFunc(
4953f6b0874Sdanielk1977   sqlite3_context *context,
4963f6b0874Sdanielk1977   int argc,
4973f6b0874Sdanielk1977   sqlite3_value **argv
4983f6b0874Sdanielk1977 ){
4993f6b0874Sdanielk1977   const unsigned char *zA = sqlite3_value_text(argv[0]);
5003f6b0874Sdanielk1977   const unsigned char *zB = sqlite3_value_text(argv[1]);
5017c6303c0Sdanielk1977   int escape = 0;
5027c6303c0Sdanielk1977   if( argc==3 ){
5037c6303c0Sdanielk1977     /* The escape character string must consist of a single UTF-8 character.
5047c6303c0Sdanielk1977     ** Otherwise, return an error.
5057c6303c0Sdanielk1977     */
5067c6303c0Sdanielk1977     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
5072646da7eSdrh     if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){
5087c6303c0Sdanielk1977       sqlite3_result_error(context,
5097c6303c0Sdanielk1977           "ESCAPE expression must be a single character", -1);
5107c6303c0Sdanielk1977       return;
5117c6303c0Sdanielk1977     }
5127c6303c0Sdanielk1977     escape = sqlite3ReadUtf8(zEsc);
5137c6303c0Sdanielk1977   }
5143f6b0874Sdanielk1977   if( zA && zB ){
51555ef4d97Sdrh     struct compareInfo *pInfo = sqlite3_user_data(context);
51655ef4d97Sdrh #ifdef SQLITE_TEST
51755ef4d97Sdrh     sqlite3_like_count++;
51855ef4d97Sdrh #endif
51955ef4d97Sdrh     sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape));
52051ad0ecdSdanielk1977   }
5218912d106Sdrh }
5228912d106Sdrh 
5238912d106Sdrh /*
5248912d106Sdrh ** Implementation of the NULLIF(x,y) function.  The result is the first
5258912d106Sdrh ** argument if the arguments are different.  The result is NULL if the
5268912d106Sdrh ** arguments are equal to each other.
5278912d106Sdrh */
528f9b596ebSdrh static void nullifFunc(
529f9b596ebSdrh   sqlite3_context *context,
530f9b596ebSdrh   int argc,
531f9b596ebSdrh   sqlite3_value **argv
532f9b596ebSdrh ){
533dc1bdc4fSdanielk1977   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
534dc1bdc4fSdanielk1977   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
535f4479501Sdrh     sqlite3_result_value(context, argv[0]);
5368912d106Sdrh   }
5370ac65892Sdrh }
5380ac65892Sdrh 
539647cb0e1Sdrh /*
540647cb0e1Sdrh ** Implementation of the VERSION(*) function.  The result is the version
541647cb0e1Sdrh ** of the SQLite library that is running.
542647cb0e1Sdrh */
543f9b596ebSdrh static void versionFunc(
544f9b596ebSdrh   sqlite3_context *context,
545f9b596ebSdrh   int argc,
546f9b596ebSdrh   sqlite3_value **argv
547f9b596ebSdrh ){
548d8123366Sdanielk1977   sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
549647cb0e1Sdrh }
550647cb0e1Sdrh 
551d641d646Sdanielk1977 
55247394703Sdrh /*
55347394703Sdrh ** EXPERIMENTAL - This is not an official function.  The interface may
55447394703Sdrh ** change.  This function may disappear.  Do not write code that depends
55547394703Sdrh ** on this function.
55647394703Sdrh **
55747394703Sdrh ** Implementation of the QUOTE() function.  This function takes a single
55847394703Sdrh ** argument.  If the argument is numeric, the return value is the same as
55947394703Sdrh ** the argument.  If the argument is NULL, the return value is the string
56047394703Sdrh ** "NULL".  Otherwise, the argument is enclosed in single quotes with
56147394703Sdrh ** single-quote escapes.
56247394703Sdrh */
5630ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
56447394703Sdrh   if( argc<1 ) return;
565f9b596ebSdrh   switch( sqlite3_value_type(argv[0]) ){
5669c054830Sdrh     case SQLITE_NULL: {
567d8123366Sdanielk1977       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
568f9b596ebSdrh       break;
569f9b596ebSdrh     }
5709c054830Sdrh     case SQLITE_INTEGER:
5719c054830Sdrh     case SQLITE_FLOAT: {
572f4479501Sdrh       sqlite3_result_value(context, argv[0]);
573f9b596ebSdrh       break;
574f9b596ebSdrh     }
5753f41e976Sdanielk1977     case SQLITE_BLOB: {
5763f41e976Sdanielk1977       static const char hexdigits[] = {
5773f41e976Sdanielk1977         '0', '1', '2', '3', '4', '5', '6', '7',
5783f41e976Sdanielk1977         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
5793f41e976Sdanielk1977       };
5803f41e976Sdanielk1977       char *zText = 0;
5813f41e976Sdanielk1977       int nBlob = sqlite3_value_bytes(argv[0]);
5823f41e976Sdanielk1977       char const *zBlob = sqlite3_value_blob(argv[0]);
5833f41e976Sdanielk1977 
5843f41e976Sdanielk1977       zText = (char *)sqliteMalloc((2*nBlob)+4);
5853f41e976Sdanielk1977       if( !zText ){
5863f41e976Sdanielk1977         sqlite3_result_error(context, "out of memory", -1);
5873f41e976Sdanielk1977       }else{
5883f41e976Sdanielk1977         int i;
5893f41e976Sdanielk1977         for(i=0; i<nBlob; i++){
5903f41e976Sdanielk1977           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
5913f41e976Sdanielk1977           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
5923f41e976Sdanielk1977         }
5933f41e976Sdanielk1977         zText[(nBlob*2)+2] = '\'';
5943f41e976Sdanielk1977         zText[(nBlob*2)+3] = '\0';
5953f41e976Sdanielk1977         zText[0] = 'X';
5963f41e976Sdanielk1977         zText[1] = '\'';
597d8123366Sdanielk1977         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
5983f41e976Sdanielk1977         sqliteFree(zText);
5993f41e976Sdanielk1977       }
6003f41e976Sdanielk1977       break;
6013f41e976Sdanielk1977     }
6029c054830Sdrh     case SQLITE_TEXT: {
60347394703Sdrh       int i,j,n;
6042646da7eSdrh       const unsigned char *zArg = sqlite3_value_text(argv[0]);
60547394703Sdrh       char *z;
606f9b596ebSdrh 
60751ad0ecdSdanielk1977       for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
60847394703Sdrh       z = sqliteMalloc( i+n+3 );
60947394703Sdrh       if( z==0 ) return;
61047394703Sdrh       z[0] = '\'';
61151ad0ecdSdanielk1977       for(i=0, j=1; zArg[i]; i++){
61251ad0ecdSdanielk1977         z[j++] = zArg[i];
61351ad0ecdSdanielk1977         if( zArg[i]=='\'' ){
61447394703Sdrh           z[j++] = '\'';
61547394703Sdrh         }
61647394703Sdrh       }
61747394703Sdrh       z[j++] = '\'';
61847394703Sdrh       z[j] = 0;
619d8123366Sdanielk1977       sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
62047394703Sdrh       sqliteFree(z);
62147394703Sdrh     }
62247394703Sdrh   }
623f9b596ebSdrh }
62447394703Sdrh 
625d24cc427Sdrh #ifdef SQLITE_SOUNDEX
626d24cc427Sdrh /*
627d24cc427Sdrh ** Compute the soundex encoding of a word.
628d24cc427Sdrh */
6290ae8b831Sdanielk1977 static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
630d24cc427Sdrh   char zResult[8];
6314c755c0fSdrh   const u8 *zIn;
632d24cc427Sdrh   int i, j;
633d24cc427Sdrh   static const unsigned char iCode[] = {
634d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
635d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
636d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
637d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
638d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
639d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
640d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
641d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
642d24cc427Sdrh   };
643d24cc427Sdrh   assert( argc==1 );
6444c755c0fSdrh   zIn = (u8*)sqlite3_value_text(argv[0]);
645bdf67e0eSdrh   if( zIn==0 ) zIn = (u8*)"";
646d24cc427Sdrh   for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
647d24cc427Sdrh   if( zIn[i] ){
648bdf67e0eSdrh     u8 prevcode = iCode[zIn[i]&0x7f];
649d24cc427Sdrh     zResult[0] = toupper(zIn[i]);
650d24cc427Sdrh     for(j=1; j<4 && zIn[i]; i++){
651d24cc427Sdrh       int code = iCode[zIn[i]&0x7f];
652d24cc427Sdrh       if( code>0 ){
653bdf67e0eSdrh         if( code!=prevcode ){
654bdf67e0eSdrh           prevcode = code;
655d24cc427Sdrh           zResult[j++] = code + '0';
656d24cc427Sdrh         }
657bdf67e0eSdrh       }else{
658bdf67e0eSdrh         prevcode = 0;
659bdf67e0eSdrh       }
660d24cc427Sdrh     }
661d24cc427Sdrh     while( j<4 ){
662d24cc427Sdrh       zResult[j++] = '0';
663d24cc427Sdrh     }
664d24cc427Sdrh     zResult[j] = 0;
665d8123366Sdanielk1977     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
666d24cc427Sdrh   }else{
667d8123366Sdanielk1977     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
668d24cc427Sdrh   }
669d24cc427Sdrh }
670d24cc427Sdrh #endif
671d24cc427Sdrh 
672fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION
673fdb83b2fSdrh /*
674fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL.
675fdb83b2fSdrh */
676fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
67765fd59f7Sdanielk1977   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
678fdb83b2fSdrh   const char *zProc = 0;
679fdb83b2fSdrh   sqlite3 *db = sqlite3_user_data(context);
680fdb83b2fSdrh   char *zErrMsg = 0;
681fdb83b2fSdrh 
682fdb83b2fSdrh   if( argc==2 ){
68365fd59f7Sdanielk1977     zProc = (const char *)sqlite3_value_text(argv[1]);
684fdb83b2fSdrh   }
685fdb83b2fSdrh   if( sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
686fdb83b2fSdrh     sqlite3_result_error(context, zErrMsg, -1);
687fdb83b2fSdrh     sqlite3_free(zErrMsg);
688fdb83b2fSdrh   }
689fdb83b2fSdrh }
690fdb83b2fSdrh #endif
691fdb83b2fSdrh 
692193a6b41Sdrh #ifdef SQLITE_TEST
693193a6b41Sdrh /*
694193a6b41Sdrh ** This function generates a string of random characters.  Used for
695193a6b41Sdrh ** generating test data.
696193a6b41Sdrh */
6970ae8b831Sdanielk1977 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
698bbd82df6Sdrh   static const unsigned char zSrc[] =
699193a6b41Sdrh      "abcdefghijklmnopqrstuvwxyz"
700193a6b41Sdrh      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
701193a6b41Sdrh      "0123456789"
702193a6b41Sdrh      ".-!,:*^+=_|?/<> ";
703193a6b41Sdrh   int iMin, iMax, n, r, i;
704bbd82df6Sdrh   unsigned char zBuf[1000];
705193a6b41Sdrh   if( argc>=1 ){
706f9b596ebSdrh     iMin = sqlite3_value_int(argv[0]);
707193a6b41Sdrh     if( iMin<0 ) iMin = 0;
708193a6b41Sdrh     if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
709193a6b41Sdrh   }else{
710193a6b41Sdrh     iMin = 1;
711193a6b41Sdrh   }
712193a6b41Sdrh   if( argc>=2 ){
713f9b596ebSdrh     iMax = sqlite3_value_int(argv[1]);
714193a6b41Sdrh     if( iMax<iMin ) iMax = iMin;
7151dba7279Sdrh     if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
716193a6b41Sdrh   }else{
717193a6b41Sdrh     iMax = 50;
718193a6b41Sdrh   }
719193a6b41Sdrh   n = iMin;
720193a6b41Sdrh   if( iMax>iMin ){
7214adee20fSdanielk1977     sqlite3Randomness(sizeof(r), &r);
722bbd82df6Sdrh     r &= 0x7fffffff;
723193a6b41Sdrh     n += r%(iMax + 1 - iMin);
724193a6b41Sdrh   }
7251dba7279Sdrh   assert( n<sizeof(zBuf) );
7264adee20fSdanielk1977   sqlite3Randomness(n, zBuf);
727193a6b41Sdrh   for(i=0; i<n; i++){
728bbd82df6Sdrh     zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
729193a6b41Sdrh   }
730193a6b41Sdrh   zBuf[n] = 0;
7312646da7eSdrh   sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
732d8123366Sdanielk1977 }
7330e3d7476Sdrh #endif /* SQLITE_TEST */
734d8123366Sdanielk1977 
7350e3d7476Sdrh #ifdef SQLITE_TEST
736d8123366Sdanielk1977 /*
737d8123366Sdanielk1977 ** The following two SQL functions are used to test returning a text
738d8123366Sdanielk1977 ** result with a destructor. Function 'test_destructor' takes one argument
739d8123366Sdanielk1977 ** and returns the same argument interpreted as TEXT. A destructor is
740d8123366Sdanielk1977 ** passed with the sqlite3_result_text() call.
741d8123366Sdanielk1977 **
742d8123366Sdanielk1977 ** SQL function 'test_destructor_count' returns the number of outstanding
743d8123366Sdanielk1977 ** allocations made by 'test_destructor';
744d8123366Sdanielk1977 **
745d8123366Sdanielk1977 ** WARNING: Not threadsafe.
746d8123366Sdanielk1977 */
747d8123366Sdanielk1977 static int test_destructor_count_var = 0;
748d8123366Sdanielk1977 static void destructor(void *p){
749d8123366Sdanielk1977   char *zVal = (char *)p;
750d8123366Sdanielk1977   assert(zVal);
751d8123366Sdanielk1977   zVal--;
752d8123366Sdanielk1977   sqliteFree(zVal);
753d8123366Sdanielk1977   test_destructor_count_var--;
754d8123366Sdanielk1977 }
755d8123366Sdanielk1977 static void test_destructor(
756d8123366Sdanielk1977   sqlite3_context *pCtx,
757d8123366Sdanielk1977   int nArg,
758d8123366Sdanielk1977   sqlite3_value **argv
759d8123366Sdanielk1977 ){
760d8123366Sdanielk1977   char *zVal;
761f4618891Sdanielk1977   int len;
7629bb575fdSdrh   sqlite3 *db = sqlite3_user_data(pCtx);
763f4618891Sdanielk1977 
764d8123366Sdanielk1977   test_destructor_count_var++;
765d8123366Sdanielk1977   assert( nArg==1 );
766d8123366Sdanielk1977   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
76714db2665Sdanielk1977   len = sqlite3ValueBytes(argv[0], ENC(db));
768f4618891Sdanielk1977   zVal = sqliteMalloc(len+3);
769f4618891Sdanielk1977   zVal[len] = 0;
770f4618891Sdanielk1977   zVal[len-1] = 0;
771d8123366Sdanielk1977   assert( zVal );
772d8123366Sdanielk1977   zVal++;
77314db2665Sdanielk1977   memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
77414db2665Sdanielk1977   if( ENC(db)==SQLITE_UTF8 ){
775d8123366Sdanielk1977     sqlite3_result_text(pCtx, zVal, -1, destructor);
7766c62608fSdrh #ifndef SQLITE_OMIT_UTF16
77714db2665Sdanielk1977   }else if( ENC(db)==SQLITE_UTF16LE ){
778f4618891Sdanielk1977     sqlite3_result_text16le(pCtx, zVal, -1, destructor);
779f4618891Sdanielk1977   }else{
780f4618891Sdanielk1977     sqlite3_result_text16be(pCtx, zVal, -1, destructor);
7816c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */
782f4618891Sdanielk1977   }
783d8123366Sdanielk1977 }
784d8123366Sdanielk1977 static void test_destructor_count(
785d8123366Sdanielk1977   sqlite3_context *pCtx,
786d8123366Sdanielk1977   int nArg,
787d8123366Sdanielk1977   sqlite3_value **argv
788d8123366Sdanielk1977 ){
789d8123366Sdanielk1977   sqlite3_result_int(pCtx, test_destructor_count_var);
790193a6b41Sdrh }
7910e3d7476Sdrh #endif /* SQLITE_TEST */
7923f6b0874Sdanielk1977 
7930e3d7476Sdrh #ifdef SQLITE_TEST
7940e3d7476Sdrh /*
7950e3d7476Sdrh ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
7960e3d7476Sdrh ** interface.
7970e3d7476Sdrh **
7980e3d7476Sdrh ** The test_auxdata() SQL function attempts to register each of its arguments
7990e3d7476Sdrh ** as auxiliary data.  If there are no prior registrations of aux data for
8000e3d7476Sdrh ** that argument (meaning the argument is not a constant or this is its first
8010e3d7476Sdrh ** call) then the result for that argument is 0.  If there is a prior
8020e3d7476Sdrh ** registration, the result for that argument is 1.  The overall result
8030e3d7476Sdrh ** is the individual argument results separated by spaces.
8040e3d7476Sdrh */
8053f6b0874Sdanielk1977 static void free_test_auxdata(void *p) {sqliteFree(p);}
8063f6b0874Sdanielk1977 static void test_auxdata(
8073f6b0874Sdanielk1977   sqlite3_context *pCtx,
8083f6b0874Sdanielk1977   int nArg,
8093f6b0874Sdanielk1977   sqlite3_value **argv
8103f6b0874Sdanielk1977 ){
8113f6b0874Sdanielk1977   int i;
8123f6b0874Sdanielk1977   char *zRet = sqliteMalloc(nArg*2);
8133f6b0874Sdanielk1977   if( !zRet ) return;
8143f6b0874Sdanielk1977   for(i=0; i<nArg; i++){
8152646da7eSdrh     char const *z = (char*)sqlite3_value_text(argv[i]);
8163f6b0874Sdanielk1977     if( z ){
8173f6b0874Sdanielk1977       char *zAux = sqlite3_get_auxdata(pCtx, i);
8183f6b0874Sdanielk1977       if( zAux ){
8193f6b0874Sdanielk1977         zRet[i*2] = '1';
8203f6b0874Sdanielk1977         if( strcmp(zAux, z) ){
8213f6b0874Sdanielk1977           sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
8223f6b0874Sdanielk1977           return;
8233f6b0874Sdanielk1977         }
8243f6b0874Sdanielk1977       }else{
8253f6b0874Sdanielk1977         zRet[i*2] = '0';
8263f6b0874Sdanielk1977         zAux = sqliteStrDup(z);
8273f6b0874Sdanielk1977         sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
8283f6b0874Sdanielk1977       }
8293f6b0874Sdanielk1977       zRet[i*2+1] = ' ';
8303f6b0874Sdanielk1977     }
8313f6b0874Sdanielk1977   }
8323f6b0874Sdanielk1977   sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
8333f6b0874Sdanielk1977 }
8340e3d7476Sdrh #endif /* SQLITE_TEST */
835193a6b41Sdrh 
83601427a62Sdanielk1977 #ifdef SQLITE_TEST
83701427a62Sdanielk1977 /*
83801427a62Sdanielk1977 ** A function to test error reporting from user functions. This function
83901427a62Sdanielk1977 ** returns a copy of it's first argument as an error.
84001427a62Sdanielk1977 */
84101427a62Sdanielk1977 static void test_error(
84201427a62Sdanielk1977   sqlite3_context *pCtx,
84301427a62Sdanielk1977   int nArg,
84401427a62Sdanielk1977   sqlite3_value **argv
84501427a62Sdanielk1977 ){
8462646da7eSdrh   sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
84701427a62Sdanielk1977 }
84801427a62Sdanielk1977 #endif /* SQLITE_TEST */
84901427a62Sdanielk1977 
8500ac65892Sdrh /*
851d3a149efSdrh ** An instance of the following structure holds the context of a
852dd5baa95Sdrh ** sum() or avg() aggregate computation.
853dd5baa95Sdrh */
854dd5baa95Sdrh typedef struct SumCtx SumCtx;
855dd5baa95Sdrh struct SumCtx {
8568c08e861Sdrh   double rSum;      /* Floating point sum */
8578c08e861Sdrh   i64 iSum;         /* Integer sum */
858cf85a51cSdrh   i64 cnt;          /* Number of elements summed */
8598c08e861Sdrh   u8 overflow;      /* True if integer overflow seen */
8608c08e861Sdrh   u8 approx;        /* True if non-integer value was input to the sum */
861dd5baa95Sdrh };
862dd5baa95Sdrh 
863dd5baa95Sdrh /*
864a97fdd3bSdrh ** Routines used to compute the sum, average, and total.
865a97fdd3bSdrh **
866a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means
867a97fdd3bSdrh ** that it returns NULL if it sums over no inputs.  TOTAL returns
868a97fdd3bSdrh ** 0.0 in that case.  In addition, TOTAL always returns a float where
869a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point
870c806d857Sdrh ** value.  TOTAL never fails, but SUM might through an exception if
871c806d857Sdrh ** it overflows an integer.
872dd5baa95Sdrh */
8730ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
874dd5baa95Sdrh   SumCtx *p;
8753d1d95e6Sdrh   int type;
8763f219f46Sdrh   assert( argc==1 );
8774f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
87829d72108Sdrh   type = sqlite3_value_numeric_type(argv[0]);
8793d1d95e6Sdrh   if( p && type!=SQLITE_NULL ){
880739105c7Sdrh     p->cnt++;
88129d72108Sdrh     if( type==SQLITE_INTEGER ){
8828c08e861Sdrh       i64 v = sqlite3_value_int64(argv[0]);
8838c08e861Sdrh       p->rSum += v;
8848c08e861Sdrh       if( (p->approx|p->overflow)==0 ){
8858c08e861Sdrh         i64 iNewSum = p->iSum + v;
8868c08e861Sdrh         int s1 = p->iSum >> (sizeof(i64)*8-1);
8878c08e861Sdrh         int s2 = v       >> (sizeof(i64)*8-1);
8888c08e861Sdrh         int s3 = iNewSum >> (sizeof(i64)*8-1);
8898c08e861Sdrh         p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
8908c08e861Sdrh         p->iSum = iNewSum;
89129d72108Sdrh       }
89229d72108Sdrh     }else{
8938c08e861Sdrh       p->rSum += sqlite3_value_double(argv[0]);
89429d72108Sdrh       p->approx = 1;
8953f219f46Sdrh     }
896739105c7Sdrh   }
897dd5baa95Sdrh }
8980ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){
899dd5baa95Sdrh   SumCtx *p;
900abfcea25Sdrh   p = sqlite3_aggregate_context(context, 0);
901c2bd913aSdrh   if( p && p->cnt>0 ){
9028c08e861Sdrh     if( p->overflow ){
9038c08e861Sdrh       sqlite3_result_error(context,"integer overflow",-1);
9048c08e861Sdrh     }else if( p->approx ){
9058c08e861Sdrh       sqlite3_result_double(context, p->rSum);
906c2bd913aSdrh     }else{
9078c08e861Sdrh       sqlite3_result_int64(context, p->iSum);
9083d1d95e6Sdrh     }
909dd5baa95Sdrh   }
910c2bd913aSdrh }
9110ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){
912dd5baa95Sdrh   SumCtx *p;
913abfcea25Sdrh   p = sqlite3_aggregate_context(context, 0);
914739105c7Sdrh   if( p && p->cnt>0 ){
9158c08e861Sdrh     sqlite3_result_double(context, p->rSum/(double)p->cnt);
916dd5baa95Sdrh   }
917dd5baa95Sdrh }
918a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){
919a97fdd3bSdrh   SumCtx *p;
920a97fdd3bSdrh   p = sqlite3_aggregate_context(context, 0);
9218c08e861Sdrh   sqlite3_result_double(context, p ? p->rSum : 0.0);
922a97fdd3bSdrh }
923dd5baa95Sdrh 
924dd5baa95Sdrh /*
9250bce8354Sdrh ** The following structure keeps track of state information for the
9260bce8354Sdrh ** count() aggregate function.
9270bce8354Sdrh */
9280bce8354Sdrh typedef struct CountCtx CountCtx;
9290bce8354Sdrh struct CountCtx {
930fc6ad39cSdrh   i64 n;
9310bce8354Sdrh };
932dd5baa95Sdrh 
9330bce8354Sdrh /*
9340bce8354Sdrh ** Routines to implement the count() aggregate function.
9350bce8354Sdrh */
9360ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
9370bce8354Sdrh   CountCtx *p;
9384f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
9399c054830Sdrh   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
9400bce8354Sdrh     p->n++;
9410bce8354Sdrh   }
9420bce8354Sdrh }
9430ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){
9440bce8354Sdrh   CountCtx *p;
945abfcea25Sdrh   p = sqlite3_aggregate_context(context, 0);
946fc6ad39cSdrh   sqlite3_result_int64(context, p ? p->n : 0);
9470bce8354Sdrh }
9480bce8354Sdrh 
9490bce8354Sdrh /*
9500bce8354Sdrh ** Routines to implement min() and max() aggregate functions.
9510bce8354Sdrh */
9520ae8b831Sdanielk1977 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
95388208050Sdanielk1977   Mem *pArg  = (Mem *)argv[0];
9549eb516c0Sdrh   Mem *pBest;
9559eb516c0Sdrh 
9569eb516c0Sdrh   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
9579eb516c0Sdrh   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
9583aeab9e4Sdanielk1977   if( !pBest ) return;
959268380caSdrh 
96088208050Sdanielk1977   if( pBest->flags ){
9619eb516c0Sdrh     int max;
9629eb516c0Sdrh     int cmp;
963dc1bdc4fSdanielk1977     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
9647e18c259Sdanielk1977     /* This step function is used for both the min() and max() aggregates,
9657e18c259Sdanielk1977     ** the only difference between the two being that the sense of the
9667e18c259Sdanielk1977     ** comparison is inverted. For the max() aggregate, the
9677e18c259Sdanielk1977     ** sqlite3_user_data() function returns (void *)-1. For min() it
9687e18c259Sdanielk1977     ** returns (void *)db, where db is the sqlite3* database pointer.
9697e18c259Sdanielk1977     ** Therefore the next statement sets variable 'max' to 1 for the max()
9707e18c259Sdanielk1977     ** aggregate, or 0 for min().
9717e18c259Sdanielk1977     */
97288208050Sdanielk1977     max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
973dc1bdc4fSdanielk1977     cmp = sqlite3MemCompare(pBest, pArg, pColl);
97488208050Sdanielk1977     if( (max && cmp<0) || (!max && cmp>0) ){
9757e18c259Sdanielk1977       sqlite3VdbeMemCopy(pBest, pArg);
97688208050Sdanielk1977     }
9770bce8354Sdrh   }else{
9787e18c259Sdanielk1977     sqlite3VdbeMemCopy(pBest, pArg);
9790bce8354Sdrh   }
9800bce8354Sdrh }
9810ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){
98288208050Sdanielk1977   sqlite3_value *pRes;
983abfcea25Sdrh   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
984abfcea25Sdrh   if( pRes ){
98588208050Sdanielk1977     if( pRes->flags ){
986f4479501Sdrh       sqlite3_result_value(context, pRes);
9870bce8354Sdrh     }
988b20e56b4Sdanielk1977     sqlite3VdbeMemRelease(pRes);
9890bce8354Sdrh   }
990abfcea25Sdrh }
991dd5baa95Sdrh 
9924e5ffc5fSdrh 
993d3a149efSdrh /*
994a2ed5601Sdrh ** This function registered all of the above C functions as SQL
995a2ed5601Sdrh ** functions.  This should be the only routine in this file with
996a2ed5601Sdrh ** external linkage.
997dc04c583Sdrh */
9989bb575fdSdrh void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
9995719628aSdrh   static const struct {
10000bce8354Sdrh      char *zName;
1001268380caSdrh      signed char nArg;
1002268380caSdrh      u8 argType;           /* 0: none.  1: db  2: (-1) */
1003d02eb1fdSdanielk1977      u8 eTextRep;          /* 1: UTF-16.  0: UTF-8 */
1004dc1bdc4fSdanielk1977      u8 needCollSeq;
10050ae8b831Sdanielk1977      void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
10060bce8354Sdrh   } aFuncs[] = {
1007d8123366Sdanielk1977     { "min",               -1, 0, SQLITE_UTF8,    1, minmaxFunc },
1008d8123366Sdanielk1977     { "min",                0, 0, SQLITE_UTF8,    1, 0          },
1009d8123366Sdanielk1977     { "max",               -1, 2, SQLITE_UTF8,    1, minmaxFunc },
1010d8123366Sdanielk1977     { "max",                0, 2, SQLITE_UTF8,    1, 0          },
1011d8123366Sdanielk1977     { "typeof",             1, 0, SQLITE_UTF8,    0, typeofFunc },
1012d8123366Sdanielk1977     { "length",             1, 0, SQLITE_UTF8,    0, lengthFunc },
1013d8123366Sdanielk1977     { "substr",             3, 0, SQLITE_UTF8,    0, substrFunc },
10146c62608fSdrh #ifndef SQLITE_OMIT_UTF16
1015f4618891Sdanielk1977     { "substr",             3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
10166c62608fSdrh #endif
1017d8123366Sdanielk1977     { "abs",                1, 0, SQLITE_UTF8,    0, absFunc    },
1018d8123366Sdanielk1977     { "round",              1, 0, SQLITE_UTF8,    0, roundFunc  },
1019d8123366Sdanielk1977     { "round",              2, 0, SQLITE_UTF8,    0, roundFunc  },
1020d8123366Sdanielk1977     { "upper",              1, 0, SQLITE_UTF8,    0, upperFunc  },
1021d8123366Sdanielk1977     { "lower",              1, 0, SQLITE_UTF8,    0, lowerFunc  },
1022d8123366Sdanielk1977     { "coalesce",          -1, 0, SQLITE_UTF8,    0, ifnullFunc },
1023d8123366Sdanielk1977     { "coalesce",           0, 0, SQLITE_UTF8,    0, 0          },
1024d8123366Sdanielk1977     { "coalesce",           1, 0, SQLITE_UTF8,    0, 0          },
1025d8123366Sdanielk1977     { "ifnull",             2, 0, SQLITE_UTF8,    1, ifnullFunc },
1026d8123366Sdanielk1977     { "random",            -1, 0, SQLITE_UTF8,    0, randomFunc },
102794a98365Sdrh     { "nullif",             2, 0, SQLITE_UTF8,    1, nullifFunc },
1028d8123366Sdanielk1977     { "sqlite_version",     0, 0, SQLITE_UTF8,    0, versionFunc},
1029d8123366Sdanielk1977     { "quote",              1, 0, SQLITE_UTF8,    0, quoteFunc  },
1030d8123366Sdanielk1977     { "last_insert_rowid",  0, 1, SQLITE_UTF8,    0, last_insert_rowid },
1031b28af71aSdanielk1977     { "changes",            0, 1, SQLITE_UTF8,    0, changes    },
1032b28af71aSdanielk1977     { "total_changes",      0, 1, SQLITE_UTF8,    0, total_changes },
1033d24cc427Sdrh #ifdef SQLITE_SOUNDEX
1034d8123366Sdanielk1977     { "soundex",            1, 0, SQLITE_UTF8, 0, soundexFunc},
1035d24cc427Sdrh #endif
1036fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION
1037fdb83b2fSdrh     { "load_extension",     1, 1, SQLITE_UTF8,    0, loadExt },
1038fdb83b2fSdrh     { "load_extension",     2, 1, SQLITE_UTF8,    0, loadExt },
1039fdb83b2fSdrh #endif
1040193a6b41Sdrh #ifdef SQLITE_TEST
1041d8123366Sdanielk1977     { "randstr",               2, 0, SQLITE_UTF8, 0, randStr    },
1042f4618891Sdanielk1977     { "test_destructor",       1, 1, SQLITE_UTF8, 0, test_destructor},
1043d8123366Sdanielk1977     { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
10443f6b0874Sdanielk1977     { "test_auxdata",         -1, 0, SQLITE_UTF8, 0, test_auxdata},
104501427a62Sdanielk1977     { "test_error",            1, 0, SQLITE_UTF8, 0, test_error},
1046193a6b41Sdrh #endif
10470bce8354Sdrh   };
10485719628aSdrh   static const struct {
10490bce8354Sdrh     char *zName;
1050268380caSdrh     signed char nArg;
1051268380caSdrh     u8 argType;
1052dc1bdc4fSdanielk1977     u8 needCollSeq;
10530ae8b831Sdanielk1977     void (*xStep)(sqlite3_context*,int,sqlite3_value**);
10540ae8b831Sdanielk1977     void (*xFinalize)(sqlite3_context*);
10550bce8354Sdrh   } aAggs[] = {
1056dc1bdc4fSdanielk1977     { "min",    1, 0, 1, minmaxStep,   minMaxFinalize },
1057dc1bdc4fSdanielk1977     { "max",    1, 2, 1, minmaxStep,   minMaxFinalize },
1058dc1bdc4fSdanielk1977     { "sum",    1, 0, 0, sumStep,      sumFinalize    },
1059a97fdd3bSdrh     { "total",  1, 0, 0, sumStep,      totalFinalize    },
1060dc1bdc4fSdanielk1977     { "avg",    1, 0, 0, sumStep,      avgFinalize    },
1061dc1bdc4fSdanielk1977     { "count",  0, 0, 0, countStep,    countFinalize  },
1062dc1bdc4fSdanielk1977     { "count",  1, 0, 0, countStep,    countFinalize  },
10630bce8354Sdrh   };
10640bce8354Sdrh   int i;
10650bce8354Sdrh 
10660bce8354Sdrh   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1067c572ef7fSdanielk1977     void *pArg = 0;
1068c572ef7fSdanielk1977     switch( aFuncs[i].argType ){
1069c572ef7fSdanielk1977       case 1: pArg = db; break;
1070c572ef7fSdanielk1977       case 2: pArg = (void *)(-1); break;
1071c572ef7fSdanielk1977     }
1072771151b6Sdanielk1977     sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
1073f9d64d2cSdanielk1977         aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
1074dc1bdc4fSdanielk1977     if( aFuncs[i].needCollSeq ){
1075dc1bdc4fSdanielk1977       FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1076dc1bdc4fSdanielk1977           strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1077dc1bdc4fSdanielk1977       if( pFunc && aFuncs[i].needCollSeq ){
1078dc1bdc4fSdanielk1977         pFunc->needCollSeq = 1;
1079dc1bdc4fSdanielk1977       }
1080dc1bdc4fSdanielk1977     }
10810bce8354Sdrh   }
10821f01ec1bSdrh #ifndef SQLITE_OMIT_ALTERTABLE
10831f01ec1bSdrh   sqlite3AlterFunctions(db);
10841f01ec1bSdrh #endif
1085198bf391Sdrh #ifndef SQLITE_OMIT_PARSER
1086f744bb56Sdanielk1977   sqlite3AttachFunctions(db);
1087198bf391Sdrh #endif
10880bce8354Sdrh   for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
1089c572ef7fSdanielk1977     void *pArg = 0;
1090c572ef7fSdanielk1977     switch( aAggs[i].argType ){
1091c572ef7fSdanielk1977       case 1: pArg = db; break;
1092c572ef7fSdanielk1977       case 2: pArg = (void *)(-1); break;
1093c572ef7fSdanielk1977     }
1094771151b6Sdanielk1977     sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
1095f9d64d2cSdanielk1977         pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
1096dc1bdc4fSdanielk1977     if( aAggs[i].needCollSeq ){
1097dc1bdc4fSdanielk1977       FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
1098d8123366Sdanielk1977           strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
1099dc1bdc4fSdanielk1977       if( pFunc && aAggs[i].needCollSeq ){
1100dc1bdc4fSdanielk1977         pFunc->needCollSeq = 1;
1101dc1bdc4fSdanielk1977       }
1102dc1bdc4fSdanielk1977     }
1103268380caSdrh   }
11044adee20fSdanielk1977   sqlite3RegisterDateTimeFunctions(db);
1105*b7481e70Sdrh   sqlite3_overload_function(db, "MATCH", 2);
1106fd9e1f31Sdanielk1977 #ifdef SQLITE_SSE
11073752785fSdrh   (void)sqlite3SseFunctions(db);
1108fd9e1f31Sdanielk1977 #endif
110955ef4d97Sdrh #ifdef SQLITE_CASE_SENSITIVE_LIKE
111055ef4d97Sdrh   sqlite3RegisterLikeFunctions(db, 1);
111155ef4d97Sdrh #else
111255ef4d97Sdrh   sqlite3RegisterLikeFunctions(db, 0);
111355ef4d97Sdrh #endif
111455ef4d97Sdrh }
111555ef4d97Sdrh 
111655ef4d97Sdrh /*
111755ef4d97Sdrh ** Set the LIKEOPT flag on the 2-argument function with the given name.
111855ef4d97Sdrh */
1119d64fe2f3Sdrh static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
112055ef4d97Sdrh   FuncDef *pDef;
112155ef4d97Sdrh   pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
112255ef4d97Sdrh   if( pDef ){
1123d64fe2f3Sdrh     pDef->flags = flagVal;
112455ef4d97Sdrh   }
112555ef4d97Sdrh }
112655ef4d97Sdrh 
112755ef4d97Sdrh /*
112855ef4d97Sdrh ** Register the built-in LIKE and GLOB functions.  The caseSensitive
112955ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case
113055ef4d97Sdrh ** sensitive.  GLOB is always case sensitive.
113155ef4d97Sdrh */
113255ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
113355ef4d97Sdrh   struct compareInfo *pInfo;
113455ef4d97Sdrh   if( caseSensitive ){
113555ef4d97Sdrh     pInfo = (struct compareInfo*)&likeInfoAlt;
113655ef4d97Sdrh   }else{
113755ef4d97Sdrh     pInfo = (struct compareInfo*)&likeInfoNorm;
113855ef4d97Sdrh   }
1139771151b6Sdanielk1977   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1140771151b6Sdanielk1977   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1141771151b6Sdanielk1977   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
114255ef4d97Sdrh       (struct compareInfo*)&globInfo, likeFunc, 0,0);
1143d64fe2f3Sdrh   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1144d64fe2f3Sdrh   setLikeOptFlag(db, "like",
1145d64fe2f3Sdrh       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
114655ef4d97Sdrh }
114755ef4d97Sdrh 
114855ef4d97Sdrh /*
114955ef4d97Sdrh ** pExpr points to an expression which implements a function.  If
115055ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function
115155ef4d97Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and
115255ef4d97Sdrh ** return TRUE.  If the function is not a LIKE-style function then
115355ef4d97Sdrh ** return FALSE.
115455ef4d97Sdrh */
1155d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
115655ef4d97Sdrh   FuncDef *pDef;
115755ef4d97Sdrh   if( pExpr->op!=TK_FUNCTION ){
115855ef4d97Sdrh     return 0;
115955ef4d97Sdrh   }
116055ef4d97Sdrh   if( pExpr->pList->nExpr!=2 ){
116155ef4d97Sdrh     return 0;
116255ef4d97Sdrh   }
11632646da7eSdrh   pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
116455ef4d97Sdrh                              SQLITE_UTF8, 0);
1165d64fe2f3Sdrh   if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
116655ef4d97Sdrh     return 0;
116755ef4d97Sdrh   }
116855ef4d97Sdrh 
116955ef4d97Sdrh   /* The memcpy() statement assumes that the wildcard characters are
117055ef4d97Sdrh   ** the first three statements in the compareInfo structure.  The
117155ef4d97Sdrh   ** asserts() that follow verify that assumption
117255ef4d97Sdrh   */
117355ef4d97Sdrh   memcpy(aWc, pDef->pUserData, 3);
117455ef4d97Sdrh   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
117555ef4d97Sdrh   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
117655ef4d97Sdrh   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
1177d64fe2f3Sdrh   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
117855ef4d97Sdrh   return 1;
1179dc04c583Sdrh }
1180