xref: /sqlite-3.40.0/src/func.c (revision f93bbbea)
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*f93bbbeaSdanielk1977 ** $Id: func.c,v 1.61 2004/05/27 10:30:53 danielk1977 Exp $
20dc04c583Sdrh */
21dc04c583Sdrh #include <ctype.h>
22d3a149efSdrh #include <math.h>
23d3a149efSdrh #include <stdlib.h>
240bce8354Sdrh #include <assert.h>
250bce8354Sdrh #include "sqliteInt.h"
2688208050Sdanielk1977 #include "vdbeInt.h"
27771d8c3bSdrh #include "os.h"
280bce8354Sdrh 
290bce8354Sdrh /*
300bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions
310bce8354Sdrh */
32f9b596ebSdrh static void minmaxFunc(
33f9b596ebSdrh   sqlite3_context *context,
34f9b596ebSdrh   int argc,
35f9b596ebSdrh   sqlite3_value **argv
36f9b596ebSdrh ){
370bce8354Sdrh   int i;
38268380caSdrh   int mask;    /* 0 for min() or 0xffffffff for max() */
39f9b596ebSdrh   int iBest;
400bce8354Sdrh 
4189425d5eSdrh   if( argc==0 ) return;
4224b03fd0Sdanielk1977   mask = (int)sqlite3_user_data(context);
43c572ef7fSdanielk1977   assert( mask==-1 || mask==0 );
44f9b596ebSdrh   iBest = 0;
45*f93bbbeaSdanielk1977   if( sqlite3_value_type(argv[0])==SQLITE3_NULL ) return;
46f9b596ebSdrh   for(i=1; i<argc; i++){
47*f93bbbeaSdanielk1977     if( sqlite3_value_type(argv[i])==SQLITE3_NULL ) return;
48c572ef7fSdanielk1977     if( (sqlite3MemCompare(argv[iBest], argv[i], 0)^mask)>=0 ){
49f9b596ebSdrh       iBest = i;
500bce8354Sdrh     }
510bce8354Sdrh   }
52f4479501Sdrh   sqlite3_result_value(context, argv[iBest]);
530bce8354Sdrh }
540bce8354Sdrh 
55268380caSdrh /*
56268380caSdrh ** Return the type of the argument.
57268380caSdrh */
58f9b596ebSdrh static void typeofFunc(
59f9b596ebSdrh   sqlite3_context *context,
60f9b596ebSdrh   int argc,
61f9b596ebSdrh   sqlite3_value **argv
62f9b596ebSdrh ){
6335bb9d02Sdanielk1977   const char *z = 0;
6435bb9d02Sdanielk1977   switch( sqlite3_value_type(argv[0]) ){
6535bb9d02Sdanielk1977     case SQLITE3_NULL:    z = "null";    break;
6635bb9d02Sdanielk1977     case SQLITE3_INTEGER: z = "integer"; break;
6735bb9d02Sdanielk1977     case SQLITE3_TEXT:    z = "text";    break;
6835bb9d02Sdanielk1977     case SQLITE3_FLOAT:   z = "real";    break;
6935bb9d02Sdanielk1977     case SQLITE3_BLOB:    z = "blob";    break;
7035bb9d02Sdanielk1977   }
717e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 0);
720bce8354Sdrh }
730bce8354Sdrh 
740bce8354Sdrh /*
750bce8354Sdrh ** Implementation of the length() function
760bce8354Sdrh */
77f9b596ebSdrh static void lengthFunc(
78f9b596ebSdrh   sqlite3_context *context,
79f9b596ebSdrh   int argc,
80f9b596ebSdrh   sqlite3_value **argv
81f9b596ebSdrh ){
820bce8354Sdrh   int len;
830bce8354Sdrh 
840bce8354Sdrh   assert( argc==1 );
85f9b596ebSdrh   switch( sqlite3_value_type(argv[0]) ){
86f9b596ebSdrh     case SQLITE3_BLOB:
87f9b596ebSdrh     case SQLITE3_INTEGER:
88f9b596ebSdrh     case SQLITE3_FLOAT: {
89f4479501Sdrh       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
90f9b596ebSdrh       break;
91f9b596ebSdrh     }
92f9b596ebSdrh     case SQLITE3_TEXT: {
934f26d6c4Sdrh       const char *z = sqlite3_value_text(argv[0]);
940bce8354Sdrh       for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
95f4479501Sdrh       sqlite3_result_int(context, len);
96f9b596ebSdrh       break;
97f9b596ebSdrh     }
98f9b596ebSdrh     default: {
99f9b596ebSdrh       sqlite3_result_null(context);
100f9b596ebSdrh       break;
101f9b596ebSdrh     }
102f9b596ebSdrh   }
1030bce8354Sdrh }
1040bce8354Sdrh 
1050bce8354Sdrh /*
1060bce8354Sdrh ** Implementation of the abs() function
1070bce8354Sdrh */
1080ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1090bce8354Sdrh   assert( argc==1 );
110f9b596ebSdrh   switch( sqlite3_value_type(argv[0]) ){
111f9b596ebSdrh     case SQLITE3_INTEGER: {
112*f93bbbeaSdanielk1977       i64 iVal = sqlite3_value_int64(argv[0]);
113*f93bbbeaSdanielk1977       if( iVal<0 ) iVal = iVal * -1;
114*f93bbbeaSdanielk1977       sqlite3_result_int64(context, iVal);
115f9b596ebSdrh       break;
116f9b596ebSdrh     }
117f9b596ebSdrh     case SQLITE3_NULL: {
118f9b596ebSdrh       sqlite3_result_null(context);
119f9b596ebSdrh       break;
120f9b596ebSdrh     }
121f9b596ebSdrh     default: {
122*f93bbbeaSdanielk1977       double rVal = sqlite3_value_double(argv[0]);
123*f93bbbeaSdanielk1977       if( rVal<0 ) rVal = rVal * -1.0;
124*f93bbbeaSdanielk1977       sqlite3_result_double(context, rVal);
125f9b596ebSdrh       break;
126f9b596ebSdrh     }
127f9b596ebSdrh   }
1280bce8354Sdrh }
1290bce8354Sdrh 
1300bce8354Sdrh /*
1310bce8354Sdrh ** Implementation of the substr() function
1320bce8354Sdrh */
133f9b596ebSdrh static void substrFunc(
134f9b596ebSdrh   sqlite3_context *context,
135f9b596ebSdrh   int argc,
136f9b596ebSdrh   sqlite3_value **argv
137f9b596ebSdrh ){
1380bce8354Sdrh   const char *z;
1390bce8354Sdrh   const char *z2;
1400bce8354Sdrh   int i;
1410bce8354Sdrh   int p1, p2, len;
142f9b596ebSdrh 
1430bce8354Sdrh   assert( argc==3 );
1444f26d6c4Sdrh   z = sqlite3_value_text(argv[0]);
1450bce8354Sdrh   if( z==0 ) return;
14651ad0ecdSdanielk1977   p1 = sqlite3_value_int(argv[1]);
14751ad0ecdSdanielk1977   p2 = sqlite3_value_int(argv[2]);
14847c8a679Sdrh   for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
1490bce8354Sdrh   if( p1<0 ){
15089425d5eSdrh     p1 += len;
151653bc759Sdrh     if( p1<0 ){
152653bc759Sdrh       p2 += p1;
153653bc759Sdrh       p1 = 0;
154653bc759Sdrh     }
1550bce8354Sdrh   }else if( p1>0 ){
1560bce8354Sdrh     p1--;
1570bce8354Sdrh   }
1580bce8354Sdrh   if( p1+p2>len ){
1590bce8354Sdrh     p2 = len-p1;
1600bce8354Sdrh   }
16177396304Sdrh   for(i=0; i<p1 && z[i]; i++){
16247c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p1++;
1630bce8354Sdrh   }
16447c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
16577396304Sdrh   for(; i<p1+p2 && z[i]; i++){
16647c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p2++;
1670bce8354Sdrh   }
16847c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
169653bc759Sdrh   if( p2<0 ) p2 = 0;
1707e18c259Sdanielk1977   sqlite3_result_text(context, &z[p1], p2, 1);
1710bce8354Sdrh }
1720bce8354Sdrh 
1730bce8354Sdrh /*
1740bce8354Sdrh ** Implementation of the round() function
1750bce8354Sdrh */
1760ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
17751ad0ecdSdanielk1977   int n = 0;
1780bce8354Sdrh   double r;
1790bce8354Sdrh   char zBuf[100];
1800bce8354Sdrh   assert( argc==1 || argc==2 );
18151ad0ecdSdanielk1977   if( argc==2 ){
18251ad0ecdSdanielk1977     if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
18351ad0ecdSdanielk1977     n = sqlite3_value_int(argv[1]);
1840bce8354Sdrh     if( n>30 ) n = 30;
1850bce8354Sdrh     if( n<0 ) n = 0;
18651ad0ecdSdanielk1977   }
18751ad0ecdSdanielk1977   if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
1884f26d6c4Sdrh   r = sqlite3_value_double(argv[0]);
1890bce8354Sdrh   sprintf(zBuf,"%.*f",n,r);
1907e18c259Sdanielk1977   sqlite3_result_text(context, zBuf, -1, 1);
1910bce8354Sdrh }
192dc04c583Sdrh 
193dc04c583Sdrh /*
194dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions.
195dc04c583Sdrh */
1960ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
197dc04c583Sdrh   char *z;
198dc04c583Sdrh   int i;
1997e18c259Sdanielk1977   if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
200c572ef7fSdanielk1977   z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
201dc04c583Sdrh   if( z==0 ) return;
2024f26d6c4Sdrh   strcpy(z, sqlite3_value_text(argv[0]));
203dc04c583Sdrh   for(i=0; z[i]; i++){
204dc04c583Sdrh     if( islower(z[i]) ) z[i] = toupper(z[i]);
205dc04c583Sdrh   }
2067e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 1);
2077e18c259Sdanielk1977   sqliteFree(z);
208dc04c583Sdrh }
2090ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
210dc04c583Sdrh   char *z;
211dc04c583Sdrh   int i;
2127e18c259Sdanielk1977   if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
213c572ef7fSdanielk1977   z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
214dc04c583Sdrh   if( z==0 ) return;
2154f26d6c4Sdrh   strcpy(z, sqlite3_value_text(argv[0]));
216dc04c583Sdrh   for(i=0; z[i]; i++){
217dc04c583Sdrh     if( isupper(z[i]) ) z[i] = tolower(z[i]);
218dc04c583Sdrh   }
2197e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 1);
2207e18c259Sdanielk1977   sqliteFree(z);
221dc04c583Sdrh }
222dc04c583Sdrh 
223dc04c583Sdrh /*
224fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
225b6c9e6e6Sjplyon ** All three do the same thing.  They return the first non-NULL
226b6c9e6e6Sjplyon ** argument.
2273212e182Sdrh */
228f9b596ebSdrh static void ifnullFunc(
229f9b596ebSdrh   sqlite3_context *context,
230f9b596ebSdrh   int argc,
231f9b596ebSdrh   sqlite3_value **argv
232f9b596ebSdrh ){
233fbc99082Sdrh   int i;
234fbc99082Sdrh   for(i=0; i<argc; i++){
23551ad0ecdSdanielk1977     if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
236f4479501Sdrh       sqlite3_result_value(context, argv[i]);
237fbc99082Sdrh       break;
238fbc99082Sdrh     }
239fbc99082Sdrh   }
2403212e182Sdrh }
2413212e182Sdrh 
2423212e182Sdrh /*
243f9ffac96Sdrh ** Implementation of random().  Return a random integer.
244f9ffac96Sdrh */
245f9b596ebSdrh static void randomFunc(
246f9b596ebSdrh   sqlite3_context *context,
247f9b596ebSdrh   int argc,
248f9b596ebSdrh   sqlite3_value **argv
249f9b596ebSdrh ){
250bbd82df6Sdrh   int r;
2514adee20fSdanielk1977   sqlite3Randomness(sizeof(r), &r);
252f4479501Sdrh   sqlite3_result_int(context, r);
253f9ffac96Sdrh }
254f9ffac96Sdrh 
255f9ffac96Sdrh /*
2566ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function.  The return
25724b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function.
2586ed41ad7Sdrh */
25951ad0ecdSdanielk1977 static void last_insert_rowid(
2600ae8b831Sdanielk1977   sqlite3_context *context,
26151ad0ecdSdanielk1977   int arg,
26251ad0ecdSdanielk1977   sqlite3_value **argv
26351ad0ecdSdanielk1977 ){
26424b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
265f9b596ebSdrh   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
2666ed41ad7Sdrh }
2676ed41ad7Sdrh 
268f146a776Srdc /*
269f146a776Srdc ** Implementation of the change_count() SQL function.  The return
27024b03fd0Sdanielk1977 ** value is the same as the sqlite3_changes() API function.
271f146a776Srdc */
272f9b596ebSdrh static void change_count(
273f9b596ebSdrh   sqlite3_context *context,
274f9b596ebSdrh   int arg,
275f9b596ebSdrh   sqlite3_value **argv
276f9b596ebSdrh ){
27724b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
278f4479501Sdrh   sqlite3_result_int(context, sqlite3_changes(db));
279b0c374ffSrdc }
280f146a776Srdc 
281f146a776Srdc /*
282f146a776Srdc ** Implementation of the last_statement_change_count() SQL function.  The
28351ad0ecdSdanielk1977 ** return value is the same as the sqlite3_last_statement_changes() API
28451ad0ecdSdanielk1977 ** function.
285f146a776Srdc */
28651ad0ecdSdanielk1977 static void last_statement_change_count(
2870ae8b831Sdanielk1977   sqlite3_context *context,
28851ad0ecdSdanielk1977   int arg,
28951ad0ecdSdanielk1977   sqlite3_value **argv
29051ad0ecdSdanielk1977 ){
29124b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
292f4479501Sdrh   sqlite3_result_int(context, sqlite3_last_statement_changes(db));
293b0c374ffSrdc }
294b0c374ffSrdc 
2956ed41ad7Sdrh /*
2960ac65892Sdrh ** Implementation of the like() SQL function.  This function implements
2970ac65892Sdrh ** the build-in LIKE operator.  The first argument to the function is the
2980ac65892Sdrh ** string and the second argument is the pattern.  So, the SQL statements:
2990ac65892Sdrh **
3000ac65892Sdrh **       A LIKE B
3010ac65892Sdrh **
3020ac65892Sdrh ** is implemented as like(A,B).
3030ac65892Sdrh */
30451ad0ecdSdanielk1977 static void likeFunc(
3050ae8b831Sdanielk1977   sqlite3_context *context,
30651ad0ecdSdanielk1977   int argc,
30751ad0ecdSdanielk1977   sqlite3_value **argv
30851ad0ecdSdanielk1977 ){
3094f26d6c4Sdrh   const unsigned char *zA = sqlite3_value_text(argv[0]);
3104f26d6c4Sdrh   const unsigned char *zB = sqlite3_value_text(argv[1]);
31151ad0ecdSdanielk1977   if( zA && zB ){
312f4479501Sdrh     sqlite3_result_int(context, sqlite3LikeCompare(zA, zB));
31351ad0ecdSdanielk1977   }
3140ac65892Sdrh }
3150ac65892Sdrh 
3160ac65892Sdrh /*
3170ac65892Sdrh ** Implementation of the glob() SQL function.  This function implements
3180ac65892Sdrh ** the build-in GLOB operator.  The first argument to the function is the
3190ac65892Sdrh ** string and the second argument is the pattern.  So, the SQL statements:
3200ac65892Sdrh **
3210ac65892Sdrh **       A GLOB B
3220ac65892Sdrh **
3230ac65892Sdrh ** is implemented as glob(A,B).
3240ac65892Sdrh */
3250ae8b831Sdanielk1977 static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
3264f26d6c4Sdrh   const unsigned char *zA = sqlite3_value_text(argv[0]);
3274f26d6c4Sdrh   const unsigned char *zB = sqlite3_value_text(argv[1]);
32851ad0ecdSdanielk1977   if( zA && zB ){
329f4479501Sdrh     sqlite3_result_int(context, sqlite3GlobCompare(zA, zB));
33051ad0ecdSdanielk1977   }
3318912d106Sdrh }
3328912d106Sdrh 
3338912d106Sdrh /*
3348912d106Sdrh ** Implementation of the NULLIF(x,y) function.  The result is the first
3358912d106Sdrh ** argument if the arguments are different.  The result is NULL if the
3368912d106Sdrh ** arguments are equal to each other.
3378912d106Sdrh */
338f9b596ebSdrh static void nullifFunc(
339f9b596ebSdrh   sqlite3_context *context,
340f9b596ebSdrh   int argc,
341f9b596ebSdrh   sqlite3_value **argv
342f9b596ebSdrh ){
343f9b596ebSdrh   if( sqlite3MemCompare(argv[0], argv[1], 0)!=0 ){
344f4479501Sdrh     sqlite3_result_value(context, argv[0]);
3458912d106Sdrh   }
3460ac65892Sdrh }
3470ac65892Sdrh 
348647cb0e1Sdrh /*
349647cb0e1Sdrh ** Implementation of the VERSION(*) function.  The result is the version
350647cb0e1Sdrh ** of the SQLite library that is running.
351647cb0e1Sdrh */
352f9b596ebSdrh static void versionFunc(
353f9b596ebSdrh   sqlite3_context *context,
354f9b596ebSdrh   int argc,
355f9b596ebSdrh   sqlite3_value **argv
356f9b596ebSdrh ){
3577e18c259Sdanielk1977   sqlite3_result_text(context, sqlite3_version, -1, 0);
358647cb0e1Sdrh }
359647cb0e1Sdrh 
36047394703Sdrh /*
36147394703Sdrh ** EXPERIMENTAL - This is not an official function.  The interface may
36247394703Sdrh ** change.  This function may disappear.  Do not write code that depends
36347394703Sdrh ** on this function.
36447394703Sdrh **
36547394703Sdrh ** Implementation of the QUOTE() function.  This function takes a single
36647394703Sdrh ** argument.  If the argument is numeric, the return value is the same as
36747394703Sdrh ** the argument.  If the argument is NULL, the return value is the string
36847394703Sdrh ** "NULL".  Otherwise, the argument is enclosed in single quotes with
36947394703Sdrh ** single-quote escapes.
37047394703Sdrh */
3710ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
37247394703Sdrh   if( argc<1 ) return;
373f9b596ebSdrh   switch( sqlite3_value_type(argv[0]) ){
374f9b596ebSdrh     case SQLITE3_NULL: {
3757e18c259Sdanielk1977       sqlite3_result_text(context, "NULL", 4, 0);
376f9b596ebSdrh       break;
377f9b596ebSdrh     }
378f9b596ebSdrh     case SQLITE3_INTEGER:
379f9b596ebSdrh     case SQLITE3_FLOAT: {
380f4479501Sdrh       sqlite3_result_value(context, argv[0]);
381f9b596ebSdrh       break;
382f9b596ebSdrh     }
383f9b596ebSdrh     case SQLITE3_BLOB:  /*** FIX ME.  Use a BLOB encoding ***/
384f9b596ebSdrh     case SQLITE3_TEXT: {
38547394703Sdrh       int i,j,n;
3864f26d6c4Sdrh       const char *zArg = sqlite3_value_text(argv[0]);
38747394703Sdrh       char *z;
388f9b596ebSdrh 
38951ad0ecdSdanielk1977       for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
39047394703Sdrh       z = sqliteMalloc( i+n+3 );
39147394703Sdrh       if( z==0 ) return;
39247394703Sdrh       z[0] = '\'';
39351ad0ecdSdanielk1977       for(i=0, j=1; zArg[i]; i++){
39451ad0ecdSdanielk1977         z[j++] = zArg[i];
39551ad0ecdSdanielk1977         if( zArg[i]=='\'' ){
39647394703Sdrh           z[j++] = '\'';
39747394703Sdrh         }
39847394703Sdrh       }
39947394703Sdrh       z[j++] = '\'';
40047394703Sdrh       z[j] = 0;
4017e18c259Sdanielk1977       sqlite3_result_text(context, z, j, 1);
40247394703Sdrh       sqliteFree(z);
40347394703Sdrh     }
40447394703Sdrh   }
405f9b596ebSdrh }
40647394703Sdrh 
407d24cc427Sdrh #ifdef SQLITE_SOUNDEX
408d24cc427Sdrh /*
409d24cc427Sdrh ** Compute the soundex encoding of a word.
410d24cc427Sdrh */
4110ae8b831Sdanielk1977 static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
412d24cc427Sdrh   char zResult[8];
413d24cc427Sdrh   const char *zIn;
414d24cc427Sdrh   int i, j;
415d24cc427Sdrh   static const unsigned char iCode[] = {
416d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
417d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
418d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
419d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
420d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
421d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
422d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
423d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
424d24cc427Sdrh   };
425d24cc427Sdrh   assert( argc==1 );
4264f26d6c4Sdrh   zIn = sqlite3_value_text(argv[0]);
427d24cc427Sdrh   for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
428d24cc427Sdrh   if( zIn[i] ){
429d24cc427Sdrh     zResult[0] = toupper(zIn[i]);
430d24cc427Sdrh     for(j=1; j<4 && zIn[i]; i++){
431d24cc427Sdrh       int code = iCode[zIn[i]&0x7f];
432d24cc427Sdrh       if( code>0 ){
433d24cc427Sdrh         zResult[j++] = code + '0';
434d24cc427Sdrh       }
435d24cc427Sdrh     }
436d24cc427Sdrh     while( j<4 ){
437d24cc427Sdrh       zResult[j++] = '0';
438d24cc427Sdrh     }
439d24cc427Sdrh     zResult[j] = 0;
4407e18c259Sdanielk1977     sqlite3_result_text(context, zResult, 4, 1);
441d24cc427Sdrh   }else{
4427e18c259Sdanielk1977     sqlite3_result_text(context, "?000", 4, 0);
443d24cc427Sdrh   }
444d24cc427Sdrh }
445d24cc427Sdrh #endif
446d24cc427Sdrh 
447193a6b41Sdrh #ifdef SQLITE_TEST
448193a6b41Sdrh /*
449193a6b41Sdrh ** This function generates a string of random characters.  Used for
450193a6b41Sdrh ** generating test data.
451193a6b41Sdrh */
4520ae8b831Sdanielk1977 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
453bbd82df6Sdrh   static const unsigned char zSrc[] =
454193a6b41Sdrh      "abcdefghijklmnopqrstuvwxyz"
455193a6b41Sdrh      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
456193a6b41Sdrh      "0123456789"
457193a6b41Sdrh      ".-!,:*^+=_|?/<> ";
458193a6b41Sdrh   int iMin, iMax, n, r, i;
459bbd82df6Sdrh   unsigned char zBuf[1000];
460193a6b41Sdrh   if( argc>=1 ){
461f9b596ebSdrh     iMin = sqlite3_value_int(argv[0]);
462193a6b41Sdrh     if( iMin<0 ) iMin = 0;
463193a6b41Sdrh     if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
464193a6b41Sdrh   }else{
465193a6b41Sdrh     iMin = 1;
466193a6b41Sdrh   }
467193a6b41Sdrh   if( argc>=2 ){
468f9b596ebSdrh     iMax = sqlite3_value_int(argv[1]);
469193a6b41Sdrh     if( iMax<iMin ) iMax = iMin;
4701dba7279Sdrh     if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
471193a6b41Sdrh   }else{
472193a6b41Sdrh     iMax = 50;
473193a6b41Sdrh   }
474193a6b41Sdrh   n = iMin;
475193a6b41Sdrh   if( iMax>iMin ){
4764adee20fSdanielk1977     sqlite3Randomness(sizeof(r), &r);
477bbd82df6Sdrh     r &= 0x7fffffff;
478193a6b41Sdrh     n += r%(iMax + 1 - iMin);
479193a6b41Sdrh   }
4801dba7279Sdrh   assert( n<sizeof(zBuf) );
4814adee20fSdanielk1977   sqlite3Randomness(n, zBuf);
482193a6b41Sdrh   for(i=0; i<n; i++){
483bbd82df6Sdrh     zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
484193a6b41Sdrh   }
485193a6b41Sdrh   zBuf[n] = 0;
4867e18c259Sdanielk1977   sqlite3_result_text(context, zBuf, n, 1);
487193a6b41Sdrh }
488193a6b41Sdrh #endif
489193a6b41Sdrh 
4900ac65892Sdrh /*
491d3a149efSdrh ** An instance of the following structure holds the context of a
492dd5baa95Sdrh ** sum() or avg() aggregate computation.
493dd5baa95Sdrh */
494dd5baa95Sdrh typedef struct SumCtx SumCtx;
495dd5baa95Sdrh struct SumCtx {
496dd5baa95Sdrh   double sum;     /* Sum of terms */
497739105c7Sdrh   int cnt;        /* Number of elements summed */
498dd5baa95Sdrh };
499dd5baa95Sdrh 
500dd5baa95Sdrh /*
501dd5baa95Sdrh ** Routines used to compute the sum or average.
502dd5baa95Sdrh */
5030ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
504dd5baa95Sdrh   SumCtx *p;
505dd5baa95Sdrh   if( argc<1 ) return;
5064f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
5076ddcca55Sdanielk1977   if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
5084f26d6c4Sdrh     p->sum += sqlite3_value_double(argv[0]);
509739105c7Sdrh     p->cnt++;
510739105c7Sdrh   }
511dd5baa95Sdrh }
5120ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){
513dd5baa95Sdrh   SumCtx *p;
5144f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
5157e18c259Sdanielk1977   sqlite3_result_double(context, p ? p->sum : 0.0);
516dd5baa95Sdrh }
5170ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){
518dd5baa95Sdrh   SumCtx *p;
5194f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
520739105c7Sdrh   if( p && p->cnt>0 ){
5217e18c259Sdanielk1977     sqlite3_result_double(context, p->sum/(double)p->cnt);
522dd5baa95Sdrh   }
523dd5baa95Sdrh }
524dd5baa95Sdrh 
525dd5baa95Sdrh /*
526dd5baa95Sdrh ** An instance of the following structure holds the context of a
527a2ed5601Sdrh ** variance or standard deviation computation.
528d3a149efSdrh */
529d3a149efSdrh typedef struct StdDevCtx StdDevCtx;
530d3a149efSdrh struct StdDevCtx {
531d3a149efSdrh   double sum;     /* Sum of terms */
532d3a149efSdrh   double sum2;    /* Sum of the squares of terms */
533739105c7Sdrh   int cnt;        /* Number of terms counted */
534d3a149efSdrh };
535d3a149efSdrh 
536ef2daf54Sdrh #if 0   /* Omit because math library is required */
537d3a149efSdrh /*
538d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate.
539d3a149efSdrh */
5400ae8b831Sdanielk1977 static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
541d3a149efSdrh   StdDevCtx *p;
542d3a149efSdrh   double x;
5431350b030Sdrh   if( argc<1 ) return;
54424b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
545739105c7Sdrh   if( p && argv[0] ){
5464adee20fSdanielk1977     x = sqlite3AtoF(argv[0], 0);
547d3a149efSdrh     p->sum += x;
548d3a149efSdrh     p->sum2 += x*x;
549739105c7Sdrh     p->cnt++;
550739105c7Sdrh   }
551d3a149efSdrh }
5520ae8b831Sdanielk1977 static void stdDevFinalize(sqlite3_context *context){
55324b03fd0Sdanielk1977   double rN = sqlite3_aggregate_count(context);
55424b03fd0Sdanielk1977   StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
555739105c7Sdrh   if( p && p->cnt>1 ){
556739105c7Sdrh     double rCnt = cnt;
55724b03fd0Sdanielk1977     sqlite3_set_result_double(context,
558739105c7Sdrh        sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
559d3a149efSdrh   }
560d3a149efSdrh }
561ef2daf54Sdrh #endif
562d3a149efSdrh 
5630bce8354Sdrh /*
5640bce8354Sdrh ** The following structure keeps track of state information for the
5650bce8354Sdrh ** count() aggregate function.
5660bce8354Sdrh */
5670bce8354Sdrh typedef struct CountCtx CountCtx;
5680bce8354Sdrh struct CountCtx {
5690bce8354Sdrh   int n;
5700bce8354Sdrh };
571dd5baa95Sdrh 
5720bce8354Sdrh /*
5730bce8354Sdrh ** Routines to implement the count() aggregate function.
5740bce8354Sdrh */
5750ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
5760bce8354Sdrh   CountCtx *p;
5774f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
5786ddcca55Sdanielk1977   if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
5790bce8354Sdrh     p->n++;
5800bce8354Sdrh   }
5810bce8354Sdrh }
5820ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){
5830bce8354Sdrh   CountCtx *p;
5844f26d6c4Sdrh   p = sqlite3_aggregate_context(context, sizeof(*p));
585f4479501Sdrh   sqlite3_result_int(context, p ? p->n : 0);
5860bce8354Sdrh }
5870bce8354Sdrh 
5880bce8354Sdrh /*
5890bce8354Sdrh ** This function tracks state information for the min() and max()
5900bce8354Sdrh ** aggregate functions.
5910bce8354Sdrh */
5920bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx;
5930bce8354Sdrh struct MinMaxCtx {
5940bce8354Sdrh   char *z;         /* The best so far */
5950bce8354Sdrh   char zBuf[28];   /* Space that can be used for storage */
5960bce8354Sdrh };
5970bce8354Sdrh 
5980bce8354Sdrh /*
5990bce8354Sdrh ** Routines to implement min() and max() aggregate functions.
6000bce8354Sdrh */
6010ae8b831Sdanielk1977 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
60288208050Sdanielk1977   int max = 0;
60388208050Sdanielk1977   int cmp = 0;
60488208050Sdanielk1977   Mem *pArg  = (Mem *)argv[0];
6054f26d6c4Sdrh   Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
606268380caSdrh 
60788208050Sdanielk1977   if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
60888208050Sdanielk1977 
60988208050Sdanielk1977   if( pBest->flags ){
6107e18c259Sdanielk1977     /* This step function is used for both the min() and max() aggregates,
6117e18c259Sdanielk1977     ** the only difference between the two being that the sense of the
6127e18c259Sdanielk1977     ** comparison is inverted. For the max() aggregate, the
6137e18c259Sdanielk1977     ** sqlite3_user_data() function returns (void *)-1. For min() it
6147e18c259Sdanielk1977     ** returns (void *)db, where db is the sqlite3* database pointer.
6157e18c259Sdanielk1977     ** Therefore the next statement sets variable 'max' to 1 for the max()
6167e18c259Sdanielk1977     ** aggregate, or 0 for min().
6177e18c259Sdanielk1977     */
61888208050Sdanielk1977     max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
61988208050Sdanielk1977     cmp = sqlite3MemCompare(pBest, pArg, 0);
62088208050Sdanielk1977     if( (max && cmp<0) || (!max && cmp>0) ){
6217e18c259Sdanielk1977       sqlite3VdbeMemCopy(pBest, pArg);
62288208050Sdanielk1977     }
6230bce8354Sdrh   }else{
6247e18c259Sdanielk1977     sqlite3VdbeMemCopy(pBest, pArg);
6250bce8354Sdrh   }
6260bce8354Sdrh }
6270ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){
62888208050Sdanielk1977   sqlite3_value *pRes;
6294f26d6c4Sdrh   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
63088208050Sdanielk1977   if( pRes->flags ){
631f4479501Sdrh     sqlite3_result_value(context, pRes);
6320bce8354Sdrh   }
6330bce8354Sdrh }
634dd5baa95Sdrh 
635d3a149efSdrh /*
636a2ed5601Sdrh ** This function registered all of the above C functions as SQL
637a2ed5601Sdrh ** functions.  This should be the only routine in this file with
638a2ed5601Sdrh ** external linkage.
639dc04c583Sdrh */
6404adee20fSdanielk1977 void sqlite3RegisterBuiltinFunctions(sqlite *db){
6410bce8354Sdrh   static struct {
6420bce8354Sdrh      char *zName;
643268380caSdrh      signed char nArg;
644268380caSdrh      u8 argType;               /* 0: none.  1: db  2: (-1) */
6450ae8b831Sdanielk1977      void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
6460bce8354Sdrh   } aFuncs[] = {
647f9b596ebSdrh     { "min",                        -1, 0, minmaxFunc },
648f9b596ebSdrh     { "min",                         0, 0, 0          },
649f9b596ebSdrh     { "max",                        -1, 2, minmaxFunc },
650f9b596ebSdrh     { "max",                         0, 2, 0          },
651f9b596ebSdrh     { "typeof",                      1, 0, typeofFunc },
652f9b596ebSdrh     { "classof",                     1, 0, typeofFunc }, /* FIX ME: hack */
653f9b596ebSdrh     { "length",                      1, 0, lengthFunc },
654f9b596ebSdrh     { "substr",                      3, 0, substrFunc },
655f9b596ebSdrh     { "abs",                         1, 0, absFunc    },
656f9b596ebSdrh     { "round",                       1, 0, roundFunc  },
657f9b596ebSdrh     { "round",                       2, 0, roundFunc  },
658f9b596ebSdrh     { "upper",                       1, 0, upperFunc  },
659f9b596ebSdrh     { "lower",                       1, 0, lowerFunc  },
660f9b596ebSdrh     { "coalesce",                   -1, 0, ifnullFunc },
661f9b596ebSdrh     { "coalesce",                    0, 0, 0          },
662f9b596ebSdrh     { "coalesce",                    1, 0, 0          },
663f9b596ebSdrh     { "ifnull",                      2, 0, ifnullFunc },
664f9b596ebSdrh     { "random",                     -1, 0, randomFunc },
665f9b596ebSdrh     { "like",                        2, 0, likeFunc   },
666f9b596ebSdrh     { "glob",                        2, 0, globFunc   },
667f9b596ebSdrh     { "nullif",                      2, 0, nullifFunc },
668f9b596ebSdrh     { "sqlite_version",              0, 0, versionFunc},
669f9b596ebSdrh     { "quote",                       1, 0, quoteFunc  },
670f9b596ebSdrh     { "last_insert_rowid",           0, 1, last_insert_rowid },
671f9b596ebSdrh     { "change_count",                0, 1, change_count      },
672f9b596ebSdrh     { "last_statement_change_count", 0, 1, last_statement_change_count },
673d24cc427Sdrh #ifdef SQLITE_SOUNDEX
674f9b596ebSdrh     { "soundex",                     1, 0, soundexFunc},
675d24cc427Sdrh #endif
676193a6b41Sdrh #ifdef SQLITE_TEST
677f9b596ebSdrh     { "randstr",                     2, 0, randStr    },
678193a6b41Sdrh #endif
6790bce8354Sdrh   };
6800bce8354Sdrh   static struct {
6810bce8354Sdrh     char *zName;
682268380caSdrh     signed char nArg;
683268380caSdrh     u8 argType;
6840ae8b831Sdanielk1977     void (*xStep)(sqlite3_context*,int,sqlite3_value**);
6850ae8b831Sdanielk1977     void (*xFinalize)(sqlite3_context*);
6860bce8354Sdrh   } aAggs[] = {
687f9b596ebSdrh     { "min",    1, 0, minmaxStep,   minMaxFinalize },
688f9b596ebSdrh     { "max",    1, 2, minmaxStep,   minMaxFinalize },
689f9b596ebSdrh     { "sum",    1, 0, sumStep,      sumFinalize    },
690f9b596ebSdrh     { "avg",    1, 0, sumStep,      avgFinalize    },
691f9b596ebSdrh     { "count",  0, 0, countStep,    countFinalize  },
692f9b596ebSdrh     { "count",  1, 0, countStep,    countFinalize  },
693ef2daf54Sdrh #if 0
694f9b596ebSdrh     { "stddev", 1, 0, stdDevStep,   stdDevFinalize },
695ef2daf54Sdrh #endif
6960bce8354Sdrh   };
6970bce8354Sdrh   int i;
6980bce8354Sdrh 
6990bce8354Sdrh   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
700c572ef7fSdanielk1977     void *pArg = 0;
701c572ef7fSdanielk1977     switch( aFuncs[i].argType ){
702c572ef7fSdanielk1977       case 1: pArg = db; break;
703c572ef7fSdanielk1977       case 2: pArg = (void *)(-1); break;
704c572ef7fSdanielk1977     }
7056590493dSdanielk1977     sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0,
7066590493dSdanielk1977         pArg, aFuncs[i].xFunc, 0, 0);
7070bce8354Sdrh   }
7080bce8354Sdrh   for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
709c572ef7fSdanielk1977     void *pArg = 0;
710c572ef7fSdanielk1977     switch( aAggs[i].argType ){
711c572ef7fSdanielk1977       case 1: pArg = db; break;
712c572ef7fSdanielk1977       case 2: pArg = (void *)(-1); break;
713c572ef7fSdanielk1977     }
7146590493dSdanielk1977     sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg,
7156590493dSdanielk1977         0, aAggs[i].xStep, aAggs[i].xFinalize);
716268380caSdrh   }
7174adee20fSdanielk1977   sqlite3RegisterDateTimeFunctions(db);
718dc04c583Sdrh }
719