xref: /sqlite-3.40.0/src/func.c (revision 35bb9d02)
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*35bb9d02Sdanielk1977 ** $Id: func.c,v 1.51 2004/05/24 12:55:55 danielk1977 Exp $
20dc04c583Sdrh */
21dc04c583Sdrh #include <ctype.h>
22d3a149efSdrh #include <math.h>
23d3a149efSdrh #include <stdlib.h>
240bce8354Sdrh #include <assert.h>
250bce8354Sdrh #include "sqliteInt.h"
26771d8c3bSdrh #include "os.h"
270bce8354Sdrh 
280bce8354Sdrh /*
290bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions
300bce8354Sdrh */
3151ad0ecdSdanielk1977 static void minmaxFunc(sqlite_func *context, int argc, sqlite3_value **argv){
320bce8354Sdrh   const char *zBest;
330bce8354Sdrh   int i;
34268380caSdrh   int (*xCompare)(const char*, const char*);
35268380caSdrh   int mask;    /* 0 for min() or 0xffffffff for max() */
3651ad0ecdSdanielk1977   const char *zArg;
370bce8354Sdrh 
3889425d5eSdrh   if( argc==0 ) return;
3924b03fd0Sdanielk1977   mask = (int)sqlite3_user_data(context);
4051ad0ecdSdanielk1977   zBest = sqlite3_value_data(argv[0]);
418912d106Sdrh   if( zBest==0 ) return;
4251ad0ecdSdanielk1977   zArg = sqlite3_value_data(argv[1]);
4351ad0ecdSdanielk1977   if( zArg[0]=='n' ){
444adee20fSdanielk1977     xCompare = sqlite3Compare;
45268380caSdrh   }else{
46268380caSdrh     xCompare = strcmp;
47268380caSdrh   }
48268380caSdrh   for(i=2; i<argc; i+=2){
4951ad0ecdSdanielk1977     zArg = sqlite3_value_data(argv[i]);
5051ad0ecdSdanielk1977     if( zArg==0 ) return;
5151ad0ecdSdanielk1977     if( (xCompare(zArg, zBest)^mask)<0 ){
5251ad0ecdSdanielk1977       zBest = zArg;
530bce8354Sdrh     }
540bce8354Sdrh   }
5524b03fd0Sdanielk1977   sqlite3_set_result_string(context, zBest, -1);
560bce8354Sdrh }
570bce8354Sdrh 
58268380caSdrh /*
59268380caSdrh ** Return the type of the argument.
60268380caSdrh */
6151ad0ecdSdanielk1977 static void typeofFunc(sqlite_func *context, int argc, sqlite3_value **argv){
62*35bb9d02Sdanielk1977   const char *z = 0;
63268380caSdrh   assert( argc==2 );
64*35bb9d02Sdanielk1977   switch( sqlite3_value_type(argv[0]) ){
65*35bb9d02Sdanielk1977     case SQLITE3_NULL: z = "null" ; break;
66*35bb9d02Sdanielk1977     case SQLITE3_INTEGER: z = "integer" ; break;
67*35bb9d02Sdanielk1977     case SQLITE3_TEXT: z = "text" ; break;
68*35bb9d02Sdanielk1977     case SQLITE3_FLOAT: z = "real" ; break;
69*35bb9d02Sdanielk1977     case SQLITE3_BLOB: z = "blob" ; break;
70*35bb9d02Sdanielk1977   }
71*35bb9d02Sdanielk1977   sqlite3_set_result_string(context, z, -1);
720bce8354Sdrh }
730bce8354Sdrh 
740bce8354Sdrh /*
750bce8354Sdrh ** Implementation of the length() function
760bce8354Sdrh */
7751ad0ecdSdanielk1977 static void lengthFunc(sqlite_func *context, int argc, sqlite3_value **argv){
780bce8354Sdrh   const char *z;
790bce8354Sdrh   int len;
800bce8354Sdrh 
810bce8354Sdrh   assert( argc==1 );
8251ad0ecdSdanielk1977   z = sqlite3_value_data(argv[0]);
838912d106Sdrh   if( z==0 ) return;
840bce8354Sdrh #ifdef SQLITE_UTF8
850bce8354Sdrh   for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
860bce8354Sdrh #else
870bce8354Sdrh   len = strlen(z);
880bce8354Sdrh #endif
8924b03fd0Sdanielk1977   sqlite3_set_result_int(context, len);
900bce8354Sdrh }
910bce8354Sdrh 
920bce8354Sdrh /*
930bce8354Sdrh ** Implementation of the abs() function
940bce8354Sdrh */
9551ad0ecdSdanielk1977 static void absFunc(sqlite_func *context, int argc, sqlite3_value **argv){
960bce8354Sdrh   const char *z;
970bce8354Sdrh   assert( argc==1 );
9851ad0ecdSdanielk1977   z = sqlite3_value_data(argv[0]);
998912d106Sdrh   if( z==0 ) return;
1008912d106Sdrh   if( z[0]=='-' && isdigit(z[1]) ) z++;
10124b03fd0Sdanielk1977   sqlite3_set_result_string(context, z, -1);
1020bce8354Sdrh }
1030bce8354Sdrh 
1040bce8354Sdrh /*
1050bce8354Sdrh ** Implementation of the substr() function
1060bce8354Sdrh */
10751ad0ecdSdanielk1977 static void substrFunc(sqlite_func *context, int argc, sqlite3_value **argv){
1080bce8354Sdrh   const char *z;
1090bce8354Sdrh #ifdef SQLITE_UTF8
1100bce8354Sdrh   const char *z2;
1110bce8354Sdrh   int i;
1120bce8354Sdrh #endif
1130bce8354Sdrh   int p1, p2, len;
1140bce8354Sdrh   assert( argc==3 );
11551ad0ecdSdanielk1977   z = sqlite3_value_data(argv[0]);
1160bce8354Sdrh   if( z==0 ) return;
11751ad0ecdSdanielk1977   p1 = sqlite3_value_int(argv[1]);
11851ad0ecdSdanielk1977   p2 = sqlite3_value_int(argv[2]);
1190bce8354Sdrh #ifdef SQLITE_UTF8
12047c8a679Sdrh   for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
1210bce8354Sdrh #else
1220bce8354Sdrh   len = strlen(z);
1230bce8354Sdrh #endif
1240bce8354Sdrh   if( p1<0 ){
12589425d5eSdrh     p1 += len;
126653bc759Sdrh     if( p1<0 ){
127653bc759Sdrh       p2 += p1;
128653bc759Sdrh       p1 = 0;
129653bc759Sdrh     }
1300bce8354Sdrh   }else if( p1>0 ){
1310bce8354Sdrh     p1--;
1320bce8354Sdrh   }
1330bce8354Sdrh   if( p1+p2>len ){
1340bce8354Sdrh     p2 = len-p1;
1350bce8354Sdrh   }
1360bce8354Sdrh #ifdef SQLITE_UTF8
13777396304Sdrh   for(i=0; i<p1 && z[i]; i++){
13847c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p1++;
1390bce8354Sdrh   }
14047c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
14177396304Sdrh   for(; i<p1+p2 && z[i]; i++){
14247c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p2++;
1430bce8354Sdrh   }
14447c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
1450bce8354Sdrh #endif
146653bc759Sdrh   if( p2<0 ) p2 = 0;
14724b03fd0Sdanielk1977   sqlite3_set_result_string(context, &z[p1], p2);
1480bce8354Sdrh }
1490bce8354Sdrh 
1500bce8354Sdrh /*
1510bce8354Sdrh ** Implementation of the round() function
1520bce8354Sdrh */
15351ad0ecdSdanielk1977 static void roundFunc(sqlite_func *context, int argc, sqlite3_value **argv){
15451ad0ecdSdanielk1977   int n = 0;
1550bce8354Sdrh   double r;
1560bce8354Sdrh   char zBuf[100];
1570bce8354Sdrh   assert( argc==1 || argc==2 );
15851ad0ecdSdanielk1977   if( argc==2 ){
15951ad0ecdSdanielk1977     if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
16051ad0ecdSdanielk1977     n = sqlite3_value_int(argv[1]);
1610bce8354Sdrh     if( n>30 ) n = 30;
1620bce8354Sdrh     if( n<0 ) n = 0;
16351ad0ecdSdanielk1977   }
16451ad0ecdSdanielk1977   if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
16551ad0ecdSdanielk1977   r = sqlite3_value_float(argv[0]);
1660bce8354Sdrh   sprintf(zBuf,"%.*f",n,r);
16724b03fd0Sdanielk1977   sqlite3_set_result_string(context, zBuf, -1);
1680bce8354Sdrh }
169dc04c583Sdrh 
170dc04c583Sdrh /*
171dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions.
172dc04c583Sdrh */
17351ad0ecdSdanielk1977 static void upperFunc(sqlite_func *context, int argc, sqlite3_value **argv){
174dc04c583Sdrh   char *z;
175dc04c583Sdrh   int i;
17651ad0ecdSdanielk1977   if( argc<1 ) return;
17751ad0ecdSdanielk1977   z = sqlite3_set_result_string(context, sqlite3_value_data(argv[0]), -1);
178dc04c583Sdrh   if( z==0 ) return;
179dc04c583Sdrh   for(i=0; z[i]; i++){
180dc04c583Sdrh     if( islower(z[i]) ) z[i] = toupper(z[i]);
181dc04c583Sdrh   }
182dc04c583Sdrh }
18351ad0ecdSdanielk1977 static void lowerFunc(sqlite_func *context, int argc, sqlite3_value **argv){
184dc04c583Sdrh   char *z;
185dc04c583Sdrh   int i;
18651ad0ecdSdanielk1977   if( argc<1 ) return;
18751ad0ecdSdanielk1977   z = sqlite3_set_result_string(context, sqlite3_value_data(argv[0]), -1);
188dc04c583Sdrh   if( z==0 ) return;
189dc04c583Sdrh   for(i=0; z[i]; i++){
190dc04c583Sdrh     if( isupper(z[i]) ) z[i] = tolower(z[i]);
191dc04c583Sdrh   }
192dc04c583Sdrh }
193dc04c583Sdrh 
194dc04c583Sdrh /*
195fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
196b6c9e6e6Sjplyon ** All three do the same thing.  They return the first non-NULL
197b6c9e6e6Sjplyon ** argument.
1983212e182Sdrh */
19951ad0ecdSdanielk1977 static void ifnullFunc(sqlite_func *context, int argc, sqlite3_value **argv){
200fbc99082Sdrh   int i;
201fbc99082Sdrh   for(i=0; i<argc; i++){
20251ad0ecdSdanielk1977     if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
20351ad0ecdSdanielk1977       sqlite3_set_result_string(context, sqlite3_value_data(argv[i]), -1);
204fbc99082Sdrh       break;
205fbc99082Sdrh     }
206fbc99082Sdrh   }
2073212e182Sdrh }
2083212e182Sdrh 
2093212e182Sdrh /*
210f9ffac96Sdrh ** Implementation of random().  Return a random integer.
211f9ffac96Sdrh */
21251ad0ecdSdanielk1977 static void randomFunc(sqlite_func *context, int argc, sqlite3_value **argv){
213bbd82df6Sdrh   int r;
2144adee20fSdanielk1977   sqlite3Randomness(sizeof(r), &r);
21524b03fd0Sdanielk1977   sqlite3_set_result_int(context, r);
216f9ffac96Sdrh }
217f9ffac96Sdrh 
218f9ffac96Sdrh /*
2196ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function.  The return
22024b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function.
2216ed41ad7Sdrh */
22251ad0ecdSdanielk1977 static void last_insert_rowid(
22351ad0ecdSdanielk1977   sqlite_func *context,
22451ad0ecdSdanielk1977   int arg,
22551ad0ecdSdanielk1977   sqlite3_value **argv
22651ad0ecdSdanielk1977 ){
22724b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
22824b03fd0Sdanielk1977   sqlite3_set_result_int(context, sqlite3_last_insert_rowid(db));
2296ed41ad7Sdrh }
2306ed41ad7Sdrh 
231f146a776Srdc /*
232f146a776Srdc ** Implementation of the change_count() SQL function.  The return
23324b03fd0Sdanielk1977 ** value is the same as the sqlite3_changes() API function.
234f146a776Srdc */
23551ad0ecdSdanielk1977 static void change_count(sqlite_func *context, int arg, sqlite3_value **argv){
23624b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
23724b03fd0Sdanielk1977   sqlite3_set_result_int(context, sqlite3_changes(db));
238b0c374ffSrdc }
239f146a776Srdc 
240f146a776Srdc /*
241f146a776Srdc ** Implementation of the last_statement_change_count() SQL function.  The
24251ad0ecdSdanielk1977 ** return value is the same as the sqlite3_last_statement_changes() API
24351ad0ecdSdanielk1977 ** function.
244f146a776Srdc */
24551ad0ecdSdanielk1977 static void last_statement_change_count(
24651ad0ecdSdanielk1977   sqlite_func *context,
24751ad0ecdSdanielk1977   int arg,
24851ad0ecdSdanielk1977   sqlite3_value **argv
24951ad0ecdSdanielk1977 ){
25024b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
25124b03fd0Sdanielk1977   sqlite3_set_result_int(context, sqlite3_last_statement_changes(db));
252b0c374ffSrdc }
253b0c374ffSrdc 
2546ed41ad7Sdrh /*
2550ac65892Sdrh ** Implementation of the like() SQL function.  This function implements
2560ac65892Sdrh ** the build-in LIKE operator.  The first argument to the function is the
2570ac65892Sdrh ** string and the second argument is the pattern.  So, the SQL statements:
2580ac65892Sdrh **
2590ac65892Sdrh **       A LIKE B
2600ac65892Sdrh **
2610ac65892Sdrh ** is implemented as like(A,B).
2620ac65892Sdrh */
26351ad0ecdSdanielk1977 static void likeFunc(
26451ad0ecdSdanielk1977   sqlite_func *context,
26551ad0ecdSdanielk1977   int argc,
26651ad0ecdSdanielk1977   sqlite3_value **argv
26751ad0ecdSdanielk1977 ){
26851ad0ecdSdanielk1977   const unsigned char *zA = sqlite3_value_data(argv[0]);
26951ad0ecdSdanielk1977   const unsigned char *zB = sqlite3_value_data(argv[1]);
27051ad0ecdSdanielk1977   if( zA && zB ){
27151ad0ecdSdanielk1977     sqlite3_set_result_int(context, sqlite3LikeCompare(zA, zB));
27251ad0ecdSdanielk1977   }
2730ac65892Sdrh }
2740ac65892Sdrh 
2750ac65892Sdrh /*
2760ac65892Sdrh ** Implementation of the glob() SQL function.  This function implements
2770ac65892Sdrh ** the build-in GLOB operator.  The first argument to the function is the
2780ac65892Sdrh ** string and the second argument is the pattern.  So, the SQL statements:
2790ac65892Sdrh **
2800ac65892Sdrh **       A GLOB B
2810ac65892Sdrh **
2820ac65892Sdrh ** is implemented as glob(A,B).
2830ac65892Sdrh */
28451ad0ecdSdanielk1977 static void globFunc(sqlite_func *context, int arg, sqlite3_value **argv){
28551ad0ecdSdanielk1977   const unsigned char *zA = sqlite3_value_data(argv[0]);
28651ad0ecdSdanielk1977   const unsigned char *zB = sqlite3_value_data(argv[1]);
28751ad0ecdSdanielk1977   if( zA && zB ){
28851ad0ecdSdanielk1977     sqlite3_set_result_int(context, sqlite3GlobCompare(zA, zB));
28951ad0ecdSdanielk1977   }
2908912d106Sdrh }
2918912d106Sdrh 
2928912d106Sdrh /*
2938912d106Sdrh ** Implementation of the NULLIF(x,y) function.  The result is the first
2948912d106Sdrh ** argument if the arguments are different.  The result is NULL if the
2958912d106Sdrh ** arguments are equal to each other.
2968912d106Sdrh */
29751ad0ecdSdanielk1977 static void nullifFunc(sqlite_func *context, int argc, sqlite3_value **argv){
29851ad0ecdSdanielk1977   const unsigned char *zX = sqlite3_value_data(argv[0]);
29951ad0ecdSdanielk1977   const unsigned char *zY = sqlite3_value_data(argv[1]);
30051ad0ecdSdanielk1977   if( zX!=0 && sqlite3Compare(zX, zY)!=0 ){
30151ad0ecdSdanielk1977     sqlite3_set_result_string(context, zX, -1);
3028912d106Sdrh   }
3030ac65892Sdrh }
3040ac65892Sdrh 
305647cb0e1Sdrh /*
306647cb0e1Sdrh ** Implementation of the VERSION(*) function.  The result is the version
307647cb0e1Sdrh ** of the SQLite library that is running.
308647cb0e1Sdrh */
30951ad0ecdSdanielk1977 static void versionFunc(sqlite_func *context, int argc, sqlite3_value **argv){
31024b03fd0Sdanielk1977   sqlite3_set_result_string(context, sqlite3_version, -1);
311647cb0e1Sdrh }
312647cb0e1Sdrh 
31347394703Sdrh /*
31447394703Sdrh ** EXPERIMENTAL - This is not an official function.  The interface may
31547394703Sdrh ** change.  This function may disappear.  Do not write code that depends
31647394703Sdrh ** on this function.
31747394703Sdrh **
31847394703Sdrh ** Implementation of the QUOTE() function.  This function takes a single
31947394703Sdrh ** argument.  If the argument is numeric, the return value is the same as
32047394703Sdrh ** the argument.  If the argument is NULL, the return value is the string
32147394703Sdrh ** "NULL".  Otherwise, the argument is enclosed in single quotes with
32247394703Sdrh ** single-quote escapes.
32347394703Sdrh */
32451ad0ecdSdanielk1977 static void quoteFunc(sqlite_func *context, int argc, sqlite3_value **argv){
32551ad0ecdSdanielk1977   const char *zArg = sqlite3_value_data(argv[0]);
32647394703Sdrh   if( argc<1 ) return;
32751ad0ecdSdanielk1977   if( zArg==0 ){
32824b03fd0Sdanielk1977     sqlite3_set_result_string(context, "NULL", 4);
32951ad0ecdSdanielk1977   }else if( sqlite3IsNumber(zArg, 0, TEXT_Utf8) ){
33051ad0ecdSdanielk1977     sqlite3_set_result_string(context, zArg, -1);
33147394703Sdrh   }else{
33247394703Sdrh     int i,j,n;
33347394703Sdrh     char *z;
33451ad0ecdSdanielk1977     for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
33547394703Sdrh     z = sqliteMalloc( i+n+3 );
33647394703Sdrh     if( z==0 ) return;
33747394703Sdrh     z[0] = '\'';
33851ad0ecdSdanielk1977     for(i=0, j=1; zArg[i]; i++){
33951ad0ecdSdanielk1977       z[j++] = zArg[i];
34051ad0ecdSdanielk1977       if( zArg[i]=='\'' ){
34147394703Sdrh         z[j++] = '\'';
34247394703Sdrh       }
34347394703Sdrh     }
34447394703Sdrh     z[j++] = '\'';
34547394703Sdrh     z[j] = 0;
34624b03fd0Sdanielk1977     sqlite3_set_result_string(context, z, j);
34747394703Sdrh     sqliteFree(z);
34847394703Sdrh   }
34947394703Sdrh }
35047394703Sdrh 
351d24cc427Sdrh #ifdef SQLITE_SOUNDEX
352d24cc427Sdrh /*
353d24cc427Sdrh ** Compute the soundex encoding of a word.
354d24cc427Sdrh */
35551ad0ecdSdanielk1977 static void soundexFunc(sqlite_func *context, int argc, sqlite3_value **argv){
356d24cc427Sdrh   char zResult[8];
357d24cc427Sdrh   const char *zIn;
358d24cc427Sdrh   int i, j;
359d24cc427Sdrh   static const unsigned char iCode[] = {
360d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
361d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
362d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
363d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
364d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
365d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
366d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
367d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
368d24cc427Sdrh   };
369d24cc427Sdrh   assert( argc==1 );
37051ad0ecdSdanielk1977   zIn = sqlite3_value_data(argv[0]);
371d24cc427Sdrh   for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
372d24cc427Sdrh   if( zIn[i] ){
373d24cc427Sdrh     zResult[0] = toupper(zIn[i]);
374d24cc427Sdrh     for(j=1; j<4 && zIn[i]; i++){
375d24cc427Sdrh       int code = iCode[zIn[i]&0x7f];
376d24cc427Sdrh       if( code>0 ){
377d24cc427Sdrh         zResult[j++] = code + '0';
378d24cc427Sdrh       }
379d24cc427Sdrh     }
380d24cc427Sdrh     while( j<4 ){
381d24cc427Sdrh       zResult[j++] = '0';
382d24cc427Sdrh     }
383d24cc427Sdrh     zResult[j] = 0;
38424b03fd0Sdanielk1977     sqlite3_set_result_string(context, zResult, 4);
385d24cc427Sdrh   }else{
38624b03fd0Sdanielk1977     sqlite3_set_result_string(context, "?000", 4);
387d24cc427Sdrh   }
388d24cc427Sdrh }
389d24cc427Sdrh #endif
390d24cc427Sdrh 
391193a6b41Sdrh #ifdef SQLITE_TEST
392193a6b41Sdrh /*
393193a6b41Sdrh ** This function generates a string of random characters.  Used for
394193a6b41Sdrh ** generating test data.
395193a6b41Sdrh */
39651ad0ecdSdanielk1977 static void randStr(sqlite_func *context, int argc, sqlite3_value **argv){
397bbd82df6Sdrh   static const unsigned char zSrc[] =
398193a6b41Sdrh      "abcdefghijklmnopqrstuvwxyz"
399193a6b41Sdrh      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
400193a6b41Sdrh      "0123456789"
401193a6b41Sdrh      ".-!,:*^+=_|?/<> ";
402193a6b41Sdrh   int iMin, iMax, n, r, i;
403bbd82df6Sdrh   unsigned char zBuf[1000];
404193a6b41Sdrh   if( argc>=1 ){
40551ad0ecdSdanielk1977     iMin = atoi(sqlite3_value_data(argv[0]));
406193a6b41Sdrh     if( iMin<0 ) iMin = 0;
407193a6b41Sdrh     if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
408193a6b41Sdrh   }else{
409193a6b41Sdrh     iMin = 1;
410193a6b41Sdrh   }
411193a6b41Sdrh   if( argc>=2 ){
41251ad0ecdSdanielk1977     iMax = atoi(sqlite3_value_data(argv[1]));
413193a6b41Sdrh     if( iMax<iMin ) iMax = iMin;
4141dba7279Sdrh     if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
415193a6b41Sdrh   }else{
416193a6b41Sdrh     iMax = 50;
417193a6b41Sdrh   }
418193a6b41Sdrh   n = iMin;
419193a6b41Sdrh   if( iMax>iMin ){
4204adee20fSdanielk1977     sqlite3Randomness(sizeof(r), &r);
421bbd82df6Sdrh     r &= 0x7fffffff;
422193a6b41Sdrh     n += r%(iMax + 1 - iMin);
423193a6b41Sdrh   }
4241dba7279Sdrh   assert( n<sizeof(zBuf) );
4254adee20fSdanielk1977   sqlite3Randomness(n, zBuf);
426193a6b41Sdrh   for(i=0; i<n; i++){
427bbd82df6Sdrh     zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
428193a6b41Sdrh   }
429193a6b41Sdrh   zBuf[n] = 0;
43024b03fd0Sdanielk1977   sqlite3_set_result_string(context, zBuf, n);
431193a6b41Sdrh }
432193a6b41Sdrh #endif
433193a6b41Sdrh 
4340ac65892Sdrh /*
435d3a149efSdrh ** An instance of the following structure holds the context of a
436dd5baa95Sdrh ** sum() or avg() aggregate computation.
437dd5baa95Sdrh */
438dd5baa95Sdrh typedef struct SumCtx SumCtx;
439dd5baa95Sdrh struct SumCtx {
440dd5baa95Sdrh   double sum;     /* Sum of terms */
441739105c7Sdrh   int cnt;        /* Number of elements summed */
442dd5baa95Sdrh };
443dd5baa95Sdrh 
444dd5baa95Sdrh /*
445dd5baa95Sdrh ** Routines used to compute the sum or average.
446dd5baa95Sdrh */
447dd5baa95Sdrh static void sumStep(sqlite_func *context, int argc, const char **argv){
448dd5baa95Sdrh   SumCtx *p;
449dd5baa95Sdrh   if( argc<1 ) return;
45024b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
451739105c7Sdrh   if( p && argv[0] ){
4524adee20fSdanielk1977     p->sum += sqlite3AtoF(argv[0], 0);
453739105c7Sdrh     p->cnt++;
454739105c7Sdrh   }
455dd5baa95Sdrh }
456dd5baa95Sdrh static void sumFinalize(sqlite_func *context){
457dd5baa95Sdrh   SumCtx *p;
45824b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
45924b03fd0Sdanielk1977   sqlite3_set_result_double(context, p ? p->sum : 0.0);
460dd5baa95Sdrh }
461dd5baa95Sdrh static void avgFinalize(sqlite_func *context){
462dd5baa95Sdrh   SumCtx *p;
46324b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
464739105c7Sdrh   if( p && p->cnt>0 ){
46524b03fd0Sdanielk1977     sqlite3_set_result_double(context, p->sum/(double)p->cnt);
466dd5baa95Sdrh   }
467dd5baa95Sdrh }
468dd5baa95Sdrh 
469dd5baa95Sdrh /*
470dd5baa95Sdrh ** An instance of the following structure holds the context of a
471a2ed5601Sdrh ** variance or standard deviation computation.
472d3a149efSdrh */
473d3a149efSdrh typedef struct StdDevCtx StdDevCtx;
474d3a149efSdrh struct StdDevCtx {
475d3a149efSdrh   double sum;     /* Sum of terms */
476d3a149efSdrh   double sum2;    /* Sum of the squares of terms */
477739105c7Sdrh   int cnt;        /* Number of terms counted */
478d3a149efSdrh };
479d3a149efSdrh 
480ef2daf54Sdrh #if 0   /* Omit because math library is required */
481d3a149efSdrh /*
482d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate.
483d3a149efSdrh */
4841350b030Sdrh static void stdDevStep(sqlite_func *context, int argc, const char **argv){
485d3a149efSdrh   StdDevCtx *p;
486d3a149efSdrh   double x;
4871350b030Sdrh   if( argc<1 ) return;
48824b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
489739105c7Sdrh   if( p && argv[0] ){
4904adee20fSdanielk1977     x = sqlite3AtoF(argv[0], 0);
491d3a149efSdrh     p->sum += x;
492d3a149efSdrh     p->sum2 += x*x;
493739105c7Sdrh     p->cnt++;
494739105c7Sdrh   }
495d3a149efSdrh }
4961350b030Sdrh static void stdDevFinalize(sqlite_func *context){
49724b03fd0Sdanielk1977   double rN = sqlite3_aggregate_count(context);
49824b03fd0Sdanielk1977   StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
499739105c7Sdrh   if( p && p->cnt>1 ){
500739105c7Sdrh     double rCnt = cnt;
50124b03fd0Sdanielk1977     sqlite3_set_result_double(context,
502739105c7Sdrh        sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
503d3a149efSdrh   }
504d3a149efSdrh }
505ef2daf54Sdrh #endif
506d3a149efSdrh 
5070bce8354Sdrh /*
5080bce8354Sdrh ** The following structure keeps track of state information for the
5090bce8354Sdrh ** count() aggregate function.
5100bce8354Sdrh */
5110bce8354Sdrh typedef struct CountCtx CountCtx;
5120bce8354Sdrh struct CountCtx {
5130bce8354Sdrh   int n;
5140bce8354Sdrh };
515dd5baa95Sdrh 
5160bce8354Sdrh /*
5170bce8354Sdrh ** Routines to implement the count() aggregate function.
5180bce8354Sdrh */
5190bce8354Sdrh static void countStep(sqlite_func *context, int argc, const char **argv){
5200bce8354Sdrh   CountCtx *p;
52124b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
5220bce8354Sdrh   if( (argc==0 || argv[0]) && p ){
5230bce8354Sdrh     p->n++;
5240bce8354Sdrh   }
5250bce8354Sdrh }
5260bce8354Sdrh static void countFinalize(sqlite_func *context){
5270bce8354Sdrh   CountCtx *p;
52824b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
52924b03fd0Sdanielk1977   sqlite3_set_result_int(context, p ? p->n : 0);
5300bce8354Sdrh }
5310bce8354Sdrh 
5320bce8354Sdrh /*
5330bce8354Sdrh ** This function tracks state information for the min() and max()
5340bce8354Sdrh ** aggregate functions.
5350bce8354Sdrh */
5360bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx;
5370bce8354Sdrh struct MinMaxCtx {
5380bce8354Sdrh   char *z;         /* The best so far */
5390bce8354Sdrh   char zBuf[28];   /* Space that can be used for storage */
5400bce8354Sdrh };
5410bce8354Sdrh 
5420bce8354Sdrh /*
5430bce8354Sdrh ** Routines to implement min() and max() aggregate functions.
5440bce8354Sdrh */
545268380caSdrh static void minmaxStep(sqlite_func *context, int argc, const char **argv){
5460bce8354Sdrh   MinMaxCtx *p;
547268380caSdrh   int (*xCompare)(const char*, const char*);
548268380caSdrh   int mask;    /* 0 for min() or 0xffffffff for max() */
549268380caSdrh 
550268380caSdrh   assert( argc==2 );
551268380caSdrh   if( argv[1][0]=='n' ){
5524adee20fSdanielk1977     xCompare = sqlite3Compare;
5530bce8354Sdrh   }else{
554268380caSdrh     xCompare = strcmp;
5550bce8354Sdrh   }
55624b03fd0Sdanielk1977   mask = (int)sqlite3_user_data(context);
55724b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
558739105c7Sdrh   if( p==0 || argc<1 || argv[0]==0 ) return;
559268380caSdrh   if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){
5608912d106Sdrh     int len;
56100706be3Sdrh     if( !p->zBuf[0] ){
5620bce8354Sdrh       sqliteFree(p->z);
5630bce8354Sdrh     }
5648912d106Sdrh     len = strlen(argv[0]);
56500706be3Sdrh     if( len < sizeof(p->zBuf)-1 ){
56600706be3Sdrh       p->z = &p->zBuf[1];
56700706be3Sdrh       p->zBuf[0] = 1;
5680bce8354Sdrh     }else{
5690bce8354Sdrh       p->z = sqliteMalloc( len+1 );
57000706be3Sdrh       p->zBuf[0] = 0;
5710bce8354Sdrh       if( p->z==0 ) return;
5720bce8354Sdrh     }
5730bce8354Sdrh     strcpy(p->z, argv[0]);
5740bce8354Sdrh   }
5750bce8354Sdrh }
5760bce8354Sdrh static void minMaxFinalize(sqlite_func *context){
5770bce8354Sdrh   MinMaxCtx *p;
57824b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
579739105c7Sdrh   if( p && p->z ){
58024b03fd0Sdanielk1977     sqlite3_set_result_string(context, p->z, strlen(p->z));
5810bce8354Sdrh   }
58200706be3Sdrh   if( p && !p->zBuf[0] ){
5830bce8354Sdrh     sqliteFree(p->z);
5840bce8354Sdrh   }
5850bce8354Sdrh }
586dd5baa95Sdrh 
587d3a149efSdrh /*
588a2ed5601Sdrh ** This function registered all of the above C functions as SQL
589a2ed5601Sdrh ** functions.  This should be the only routine in this file with
590a2ed5601Sdrh ** external linkage.
591dc04c583Sdrh */
5924adee20fSdanielk1977 void sqlite3RegisterBuiltinFunctions(sqlite *db){
5930bce8354Sdrh   static struct {
5940bce8354Sdrh      char *zName;
595268380caSdrh      signed char nArg;
596268380caSdrh      signed char dataType;
597268380caSdrh      u8 argType;               /* 0: none.  1: db  2: (-1) */
59851ad0ecdSdanielk1977      void (*xFunc)(sqlite_func*,int,sqlite3_value **);
5990bce8354Sdrh   } aFuncs[] = {
600268380caSdrh     { "min",       -1, SQLITE_ARGS,    0, minmaxFunc },
601268380caSdrh     { "min",        0, 0,              0, 0          },
602268380caSdrh     { "max",       -1, SQLITE_ARGS,    2, minmaxFunc },
603268380caSdrh     { "max",        0, 0,              2, 0          },
604268380caSdrh     { "typeof",     1, SQLITE_TEXT,    0, typeofFunc },
605a37cdde0Sdanielk1977     { "classof",    1, SQLITE_TEXT,    0, typeofFunc }, /* FIX ME: hack */
606268380caSdrh     { "length",     1, SQLITE_NUMERIC, 0, lengthFunc },
607268380caSdrh     { "substr",     3, SQLITE_TEXT,    0, substrFunc },
608268380caSdrh     { "abs",        1, SQLITE_NUMERIC, 0, absFunc    },
609268380caSdrh     { "round",      1, SQLITE_NUMERIC, 0, roundFunc  },
610268380caSdrh     { "round",      2, SQLITE_NUMERIC, 0, roundFunc  },
611268380caSdrh     { "upper",      1, SQLITE_TEXT,    0, upperFunc  },
612268380caSdrh     { "lower",      1, SQLITE_TEXT,    0, lowerFunc  },
613268380caSdrh     { "coalesce",  -1, SQLITE_ARGS,    0, ifnullFunc },
614268380caSdrh     { "coalesce",   0, 0,              0, 0          },
615268380caSdrh     { "coalesce",   1, 0,              0, 0          },
616268380caSdrh     { "ifnull",     2, SQLITE_ARGS,    0, ifnullFunc },
617268380caSdrh     { "random",    -1, SQLITE_NUMERIC, 0, randomFunc },
618268380caSdrh     { "like",       2, SQLITE_NUMERIC, 0, likeFunc   },
619268380caSdrh     { "glob",       2, SQLITE_NUMERIC, 0, globFunc   },
620268380caSdrh     { "nullif",     2, SQLITE_ARGS,    0, nullifFunc },
62196fc5fe6Sdanielk1977     { "sqlite_version",0,SQLITE_TEXT,  0, versionFunc},
622268380caSdrh     { "quote",      1, SQLITE_ARGS,    0, quoteFunc  },
623268380caSdrh     { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
624268380caSdrh     { "change_count",      0, SQLITE_NUMERIC, 1, change_count      },
625268380caSdrh     { "last_statement_change_count",
626268380caSdrh                            0, SQLITE_NUMERIC, 1, last_statement_change_count },
627d24cc427Sdrh #ifdef SQLITE_SOUNDEX
628268380caSdrh     { "soundex",    1, SQLITE_TEXT,    0, soundexFunc},
629d24cc427Sdrh #endif
630193a6b41Sdrh #ifdef SQLITE_TEST
631268380caSdrh     { "randstr",    2, SQLITE_TEXT,    0, randStr    },
632193a6b41Sdrh #endif
6330bce8354Sdrh   };
6340bce8354Sdrh   static struct {
6350bce8354Sdrh     char *zName;
636268380caSdrh     signed char nArg;
637268380caSdrh     signed char dataType;
638268380caSdrh     u8 argType;
6390bce8354Sdrh     void (*xStep)(sqlite_func*,int,const char**);
6400bce8354Sdrh     void (*xFinalize)(sqlite_func*);
6410bce8354Sdrh   } aAggs[] = {
642268380caSdrh     { "min",    1, 0,              0, minmaxStep,   minMaxFinalize },
643268380caSdrh     { "max",    1, 0,              2, minmaxStep,   minMaxFinalize },
644268380caSdrh     { "sum",    1, SQLITE_NUMERIC, 0, sumStep,      sumFinalize    },
645268380caSdrh     { "avg",    1, SQLITE_NUMERIC, 0, sumStep,      avgFinalize    },
646268380caSdrh     { "count",  0, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
647268380caSdrh     { "count",  1, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
648ef2daf54Sdrh #if 0
649268380caSdrh     { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep,   stdDevFinalize },
650ef2daf54Sdrh #endif
6510bce8354Sdrh   };
652268380caSdrh   static const char *azTypeFuncs[] = { "min", "max", "typeof" };
6530bce8354Sdrh   int i;
6540bce8354Sdrh 
6550bce8354Sdrh   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
656268380caSdrh     void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
65724b03fd0Sdanielk1977     sqlite3_create_function(db, aFuncs[i].zName,
658268380caSdrh            aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
659c9b84a1fSdrh     if( aFuncs[i].xFunc ){
66024b03fd0Sdanielk1977       sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
661c9b84a1fSdrh     }
6620bce8354Sdrh   }
6630bce8354Sdrh   for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
664268380caSdrh     void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
66524b03fd0Sdanielk1977     sqlite3_create_aggregate(db, aAggs[i].zName,
666268380caSdrh            aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
66724b03fd0Sdanielk1977     sqlite3_function_type(db, aAggs[i].zName, aAggs[i].dataType);
6680bce8354Sdrh   }
66951ad0ecdSdanielk1977 
670268380caSdrh   for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
671268380caSdrh     int n = strlen(azTypeFuncs[i]);
6724adee20fSdanielk1977     FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n);
673268380caSdrh     while( p ){
674268380caSdrh       p->includeTypes = 1;
675268380caSdrh       p = p->pNext;
676268380caSdrh     }
677268380caSdrh   }
6784adee20fSdanielk1977   sqlite3RegisterDateTimeFunctions(db);
679dc04c583Sdrh }
6804adee20fSdanielk1977 
6814adee20fSdanielk1977 
6824adee20fSdanielk1977 
683