xref: /sqlite-3.40.0/src/func.c (revision 6590493d)
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*6590493dSdanielk1977 ** $Id: func.c,v 1.56 2004/05/26 06:18:37 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 */
320ae8b831Sdanielk1977 static void minmaxFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
330bce8354Sdrh   const char *zBest;
340bce8354Sdrh   int i;
35268380caSdrh   int (*xCompare)(const char*, const char*);
36268380caSdrh   int mask;    /* 0 for min() or 0xffffffff for max() */
3751ad0ecdSdanielk1977   const char *zArg;
380bce8354Sdrh 
3989425d5eSdrh   if( argc==0 ) return;
4024b03fd0Sdanielk1977   mask = (int)sqlite3_user_data(context);
4151ad0ecdSdanielk1977   zBest = sqlite3_value_data(argv[0]);
428912d106Sdrh   if( zBest==0 ) return;
4351ad0ecdSdanielk1977   zArg = sqlite3_value_data(argv[1]);
4451ad0ecdSdanielk1977   if( zArg[0]=='n' ){
454adee20fSdanielk1977     xCompare = sqlite3Compare;
46268380caSdrh   }else{
47268380caSdrh     xCompare = strcmp;
48268380caSdrh   }
49268380caSdrh   for(i=2; i<argc; i+=2){
5051ad0ecdSdanielk1977     zArg = sqlite3_value_data(argv[i]);
5151ad0ecdSdanielk1977     if( zArg==0 ) return;
5251ad0ecdSdanielk1977     if( (xCompare(zArg, zBest)^mask)<0 ){
5351ad0ecdSdanielk1977       zBest = zArg;
540bce8354Sdrh     }
550bce8354Sdrh   }
567e18c259Sdanielk1977   sqlite3_result_text(context, zBest, -1, 1);
570bce8354Sdrh }
580bce8354Sdrh 
59268380caSdrh /*
60268380caSdrh ** Return the type of the argument.
61268380caSdrh */
620ae8b831Sdanielk1977 static void typeofFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
6335bb9d02Sdanielk1977   const char *z = 0;
64268380caSdrh   assert( argc==2 );
6535bb9d02Sdanielk1977   switch( sqlite3_value_type(argv[0]) ){
6635bb9d02Sdanielk1977     case SQLITE3_NULL: z = "null" ; break;
6735bb9d02Sdanielk1977     case SQLITE3_INTEGER: z = "integer" ; break;
6835bb9d02Sdanielk1977     case SQLITE3_TEXT: z = "text" ; break;
6935bb9d02Sdanielk1977     case SQLITE3_FLOAT: z = "real" ; break;
7035bb9d02Sdanielk1977     case SQLITE3_BLOB: z = "blob" ; break;
7135bb9d02Sdanielk1977   }
727e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 0);
730bce8354Sdrh }
740bce8354Sdrh 
750bce8354Sdrh /*
760bce8354Sdrh ** Implementation of the length() function
770bce8354Sdrh */
780ae8b831Sdanielk1977 static void lengthFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
790bce8354Sdrh   const char *z;
800bce8354Sdrh   int len;
810bce8354Sdrh 
820bce8354Sdrh   assert( argc==1 );
8351ad0ecdSdanielk1977   z = sqlite3_value_data(argv[0]);
848912d106Sdrh   if( z==0 ) return;
850bce8354Sdrh #ifdef SQLITE_UTF8
860bce8354Sdrh   for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
870bce8354Sdrh #else
880bce8354Sdrh   len = strlen(z);
890bce8354Sdrh #endif
907e18c259Sdanielk1977   sqlite3_result_int32(context, len);
910bce8354Sdrh }
920bce8354Sdrh 
930bce8354Sdrh /*
940bce8354Sdrh ** Implementation of the abs() function
950bce8354Sdrh */
960ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
970bce8354Sdrh   const char *z;
980bce8354Sdrh   assert( argc==1 );
9951ad0ecdSdanielk1977   z = sqlite3_value_data(argv[0]);
1008912d106Sdrh   if( z==0 ) return;
1018912d106Sdrh   if( z[0]=='-' && isdigit(z[1]) ) z++;
1027e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 1);
1030bce8354Sdrh }
1040bce8354Sdrh 
1050bce8354Sdrh /*
1060bce8354Sdrh ** Implementation of the substr() function
1070bce8354Sdrh */
1080ae8b831Sdanielk1977 static void substrFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1090bce8354Sdrh   const char *z;
1100bce8354Sdrh #ifdef SQLITE_UTF8
1110bce8354Sdrh   const char *z2;
1120bce8354Sdrh   int i;
1130bce8354Sdrh #endif
1140bce8354Sdrh   int p1, p2, len;
1150bce8354Sdrh   assert( argc==3 );
11651ad0ecdSdanielk1977   z = sqlite3_value_data(argv[0]);
1170bce8354Sdrh   if( z==0 ) return;
11851ad0ecdSdanielk1977   p1 = sqlite3_value_int(argv[1]);
11951ad0ecdSdanielk1977   p2 = sqlite3_value_int(argv[2]);
1200bce8354Sdrh #ifdef SQLITE_UTF8
12147c8a679Sdrh   for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
1220bce8354Sdrh #else
1230bce8354Sdrh   len = strlen(z);
1240bce8354Sdrh #endif
1250bce8354Sdrh   if( p1<0 ){
12689425d5eSdrh     p1 += len;
127653bc759Sdrh     if( p1<0 ){
128653bc759Sdrh       p2 += p1;
129653bc759Sdrh       p1 = 0;
130653bc759Sdrh     }
1310bce8354Sdrh   }else if( p1>0 ){
1320bce8354Sdrh     p1--;
1330bce8354Sdrh   }
1340bce8354Sdrh   if( p1+p2>len ){
1350bce8354Sdrh     p2 = len-p1;
1360bce8354Sdrh   }
1370bce8354Sdrh #ifdef SQLITE_UTF8
13877396304Sdrh   for(i=0; i<p1 && z[i]; i++){
13947c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p1++;
1400bce8354Sdrh   }
14147c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
14277396304Sdrh   for(; i<p1+p2 && z[i]; i++){
14347c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p2++;
1440bce8354Sdrh   }
14547c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
1460bce8354Sdrh #endif
147653bc759Sdrh   if( p2<0 ) p2 = 0;
1487e18c259Sdanielk1977   sqlite3_result_text(context, &z[p1], p2, 1);
1490bce8354Sdrh }
1500bce8354Sdrh 
1510bce8354Sdrh /*
1520bce8354Sdrh ** Implementation of the round() function
1530bce8354Sdrh */
1540ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
15551ad0ecdSdanielk1977   int n = 0;
1560bce8354Sdrh   double r;
1570bce8354Sdrh   char zBuf[100];
1580bce8354Sdrh   assert( argc==1 || argc==2 );
15951ad0ecdSdanielk1977   if( argc==2 ){
16051ad0ecdSdanielk1977     if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
16151ad0ecdSdanielk1977     n = sqlite3_value_int(argv[1]);
1620bce8354Sdrh     if( n>30 ) n = 30;
1630bce8354Sdrh     if( n<0 ) n = 0;
16451ad0ecdSdanielk1977   }
16551ad0ecdSdanielk1977   if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
16651ad0ecdSdanielk1977   r = sqlite3_value_float(argv[0]);
1670bce8354Sdrh   sprintf(zBuf,"%.*f",n,r);
1687e18c259Sdanielk1977   sqlite3_result_text(context, zBuf, -1, 1);
1690bce8354Sdrh }
170dc04c583Sdrh 
171dc04c583Sdrh /*
172dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions.
173dc04c583Sdrh */
1740ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
175dc04c583Sdrh   char *z;
176dc04c583Sdrh   int i;
1777e18c259Sdanielk1977   if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
1787e18c259Sdanielk1977   z = sqliteMalloc(sqlite3_value_bytes(argv[0]));
179dc04c583Sdrh   if( z==0 ) return;
1807e18c259Sdanielk1977   strcpy(z, sqlite3_value_data(argv[0]));
181dc04c583Sdrh   for(i=0; z[i]; i++){
182dc04c583Sdrh     if( islower(z[i]) ) z[i] = toupper(z[i]);
183dc04c583Sdrh   }
1847e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 1);
1857e18c259Sdanielk1977   sqliteFree(z);
186dc04c583Sdrh }
1870ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
188dc04c583Sdrh   char *z;
189dc04c583Sdrh   int i;
1907e18c259Sdanielk1977   if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
1917e18c259Sdanielk1977   z = sqliteMalloc(sqlite3_value_bytes(argv[0]));
192dc04c583Sdrh   if( z==0 ) return;
1937e18c259Sdanielk1977   strcpy(z, sqlite3_value_data(argv[0]));
194dc04c583Sdrh   for(i=0; z[i]; i++){
195dc04c583Sdrh     if( isupper(z[i]) ) z[i] = tolower(z[i]);
196dc04c583Sdrh   }
1977e18c259Sdanielk1977   sqlite3_result_text(context, z, -1, 1);
1987e18c259Sdanielk1977   sqliteFree(z);
199dc04c583Sdrh }
200dc04c583Sdrh 
201dc04c583Sdrh /*
202fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
203b6c9e6e6Sjplyon ** All three do the same thing.  They return the first non-NULL
204b6c9e6e6Sjplyon ** argument.
2053212e182Sdrh */
2060ae8b831Sdanielk1977 static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
207fbc99082Sdrh   int i;
208fbc99082Sdrh   for(i=0; i<argc; i++){
20951ad0ecdSdanielk1977     if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
2107e18c259Sdanielk1977       sqlite3_result_text(context, sqlite3_value_data(argv[i]), -1, 1);
211fbc99082Sdrh       break;
212fbc99082Sdrh     }
213fbc99082Sdrh   }
2143212e182Sdrh }
2153212e182Sdrh 
2163212e182Sdrh /*
217f9ffac96Sdrh ** Implementation of random().  Return a random integer.
218f9ffac96Sdrh */
2190ae8b831Sdanielk1977 static void randomFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
220bbd82df6Sdrh   int r;
2214adee20fSdanielk1977   sqlite3Randomness(sizeof(r), &r);
2227e18c259Sdanielk1977   sqlite3_result_int32(context, r);
223f9ffac96Sdrh }
224f9ffac96Sdrh 
225f9ffac96Sdrh /*
2266ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function.  The return
22724b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function.
2286ed41ad7Sdrh */
22951ad0ecdSdanielk1977 static void last_insert_rowid(
2300ae8b831Sdanielk1977   sqlite3_context *context,
23151ad0ecdSdanielk1977   int arg,
23251ad0ecdSdanielk1977   sqlite3_value **argv
23351ad0ecdSdanielk1977 ){
23424b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
2357e18c259Sdanielk1977   sqlite3_result_int32(context, sqlite3_last_insert_rowid(db));
2366ed41ad7Sdrh }
2376ed41ad7Sdrh 
238f146a776Srdc /*
239f146a776Srdc ** Implementation of the change_count() SQL function.  The return
24024b03fd0Sdanielk1977 ** value is the same as the sqlite3_changes() API function.
241f146a776Srdc */
2420ae8b831Sdanielk1977 static void change_count(sqlite3_context *context, int arg, sqlite3_value **argv){
24324b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
2447e18c259Sdanielk1977   sqlite3_result_int32(context, sqlite3_changes(db));
245b0c374ffSrdc }
246f146a776Srdc 
247f146a776Srdc /*
248f146a776Srdc ** Implementation of the last_statement_change_count() SQL function.  The
24951ad0ecdSdanielk1977 ** return value is the same as the sqlite3_last_statement_changes() API
25051ad0ecdSdanielk1977 ** function.
251f146a776Srdc */
25251ad0ecdSdanielk1977 static void last_statement_change_count(
2530ae8b831Sdanielk1977   sqlite3_context *context,
25451ad0ecdSdanielk1977   int arg,
25551ad0ecdSdanielk1977   sqlite3_value **argv
25651ad0ecdSdanielk1977 ){
25724b03fd0Sdanielk1977   sqlite *db = sqlite3_user_data(context);
2587e18c259Sdanielk1977   sqlite3_result_int32(context, sqlite3_last_statement_changes(db));
259b0c374ffSrdc }
260b0c374ffSrdc 
2616ed41ad7Sdrh /*
2620ac65892Sdrh ** Implementation of the like() SQL function.  This function implements
2630ac65892Sdrh ** the build-in LIKE operator.  The first argument to the function is the
2640ac65892Sdrh ** string and the second argument is the pattern.  So, the SQL statements:
2650ac65892Sdrh **
2660ac65892Sdrh **       A LIKE B
2670ac65892Sdrh **
2680ac65892Sdrh ** is implemented as like(A,B).
2690ac65892Sdrh */
27051ad0ecdSdanielk1977 static void likeFunc(
2710ae8b831Sdanielk1977   sqlite3_context *context,
27251ad0ecdSdanielk1977   int argc,
27351ad0ecdSdanielk1977   sqlite3_value **argv
27451ad0ecdSdanielk1977 ){
27551ad0ecdSdanielk1977   const unsigned char *zA = sqlite3_value_data(argv[0]);
27651ad0ecdSdanielk1977   const unsigned char *zB = sqlite3_value_data(argv[1]);
27751ad0ecdSdanielk1977   if( zA && zB ){
2787e18c259Sdanielk1977     sqlite3_result_int32(context, sqlite3LikeCompare(zA, zB));
27951ad0ecdSdanielk1977   }
2800ac65892Sdrh }
2810ac65892Sdrh 
2820ac65892Sdrh /*
2830ac65892Sdrh ** Implementation of the glob() SQL function.  This function implements
2840ac65892Sdrh ** the build-in GLOB operator.  The first argument to the function is the
2850ac65892Sdrh ** string and the second argument is the pattern.  So, the SQL statements:
2860ac65892Sdrh **
2870ac65892Sdrh **       A GLOB B
2880ac65892Sdrh **
2890ac65892Sdrh ** is implemented as glob(A,B).
2900ac65892Sdrh */
2910ae8b831Sdanielk1977 static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
29251ad0ecdSdanielk1977   const unsigned char *zA = sqlite3_value_data(argv[0]);
29351ad0ecdSdanielk1977   const unsigned char *zB = sqlite3_value_data(argv[1]);
29451ad0ecdSdanielk1977   if( zA && zB ){
2957e18c259Sdanielk1977     sqlite3_result_int32(context, sqlite3GlobCompare(zA, zB));
29651ad0ecdSdanielk1977   }
2978912d106Sdrh }
2988912d106Sdrh 
2998912d106Sdrh /*
3008912d106Sdrh ** Implementation of the NULLIF(x,y) function.  The result is the first
3018912d106Sdrh ** argument if the arguments are different.  The result is NULL if the
3028912d106Sdrh ** arguments are equal to each other.
3038912d106Sdrh */
3040ae8b831Sdanielk1977 static void nullifFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
30551ad0ecdSdanielk1977   const unsigned char *zX = sqlite3_value_data(argv[0]);
30651ad0ecdSdanielk1977   const unsigned char *zY = sqlite3_value_data(argv[1]);
30751ad0ecdSdanielk1977   if( zX!=0 && sqlite3Compare(zX, zY)!=0 ){
3087e18c259Sdanielk1977     sqlite3_result_text(context, zX, -1, 1);
3098912d106Sdrh   }
3100ac65892Sdrh }
3110ac65892Sdrh 
312647cb0e1Sdrh /*
313647cb0e1Sdrh ** Implementation of the VERSION(*) function.  The result is the version
314647cb0e1Sdrh ** of the SQLite library that is running.
315647cb0e1Sdrh */
3160ae8b831Sdanielk1977 static void versionFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
3177e18c259Sdanielk1977   sqlite3_result_text(context, sqlite3_version, -1, 0);
318647cb0e1Sdrh }
319647cb0e1Sdrh 
32047394703Sdrh /*
32147394703Sdrh ** EXPERIMENTAL - This is not an official function.  The interface may
32247394703Sdrh ** change.  This function may disappear.  Do not write code that depends
32347394703Sdrh ** on this function.
32447394703Sdrh **
32547394703Sdrh ** Implementation of the QUOTE() function.  This function takes a single
32647394703Sdrh ** argument.  If the argument is numeric, the return value is the same as
32747394703Sdrh ** the argument.  If the argument is NULL, the return value is the string
32847394703Sdrh ** "NULL".  Otherwise, the argument is enclosed in single quotes with
32947394703Sdrh ** single-quote escapes.
33047394703Sdrh */
3310ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
33251ad0ecdSdanielk1977   const char *zArg = sqlite3_value_data(argv[0]);
33347394703Sdrh   if( argc<1 ) return;
33451ad0ecdSdanielk1977   if( zArg==0 ){
3357e18c259Sdanielk1977     sqlite3_result_text(context, "NULL", 4, 0);
33651ad0ecdSdanielk1977   }else if( sqlite3IsNumber(zArg, 0, TEXT_Utf8) ){
3377e18c259Sdanielk1977     sqlite3_result_text(context, zArg, -1, 1);
33847394703Sdrh   }else{
33947394703Sdrh     int i,j,n;
34047394703Sdrh     char *z;
34151ad0ecdSdanielk1977     for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
34247394703Sdrh     z = sqliteMalloc( i+n+3 );
34347394703Sdrh     if( z==0 ) return;
34447394703Sdrh     z[0] = '\'';
34551ad0ecdSdanielk1977     for(i=0, j=1; zArg[i]; i++){
34651ad0ecdSdanielk1977       z[j++] = zArg[i];
34751ad0ecdSdanielk1977       if( zArg[i]=='\'' ){
34847394703Sdrh         z[j++] = '\'';
34947394703Sdrh       }
35047394703Sdrh     }
35147394703Sdrh     z[j++] = '\'';
35247394703Sdrh     z[j] = 0;
3537e18c259Sdanielk1977     sqlite3_result_text(context, z, j, 1);
35447394703Sdrh     sqliteFree(z);
35547394703Sdrh   }
35647394703Sdrh }
35747394703Sdrh 
358d24cc427Sdrh #ifdef SQLITE_SOUNDEX
359d24cc427Sdrh /*
360d24cc427Sdrh ** Compute the soundex encoding of a word.
361d24cc427Sdrh */
3620ae8b831Sdanielk1977 static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
363d24cc427Sdrh   char zResult[8];
364d24cc427Sdrh   const char *zIn;
365d24cc427Sdrh   int i, j;
366d24cc427Sdrh   static const unsigned char iCode[] = {
367d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
368d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
369d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
370d24cc427Sdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
371d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
372d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
373d24cc427Sdrh     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
374d24cc427Sdrh     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
375d24cc427Sdrh   };
376d24cc427Sdrh   assert( argc==1 );
37751ad0ecdSdanielk1977   zIn = sqlite3_value_data(argv[0]);
378d24cc427Sdrh   for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
379d24cc427Sdrh   if( zIn[i] ){
380d24cc427Sdrh     zResult[0] = toupper(zIn[i]);
381d24cc427Sdrh     for(j=1; j<4 && zIn[i]; i++){
382d24cc427Sdrh       int code = iCode[zIn[i]&0x7f];
383d24cc427Sdrh       if( code>0 ){
384d24cc427Sdrh         zResult[j++] = code + '0';
385d24cc427Sdrh       }
386d24cc427Sdrh     }
387d24cc427Sdrh     while( j<4 ){
388d24cc427Sdrh       zResult[j++] = '0';
389d24cc427Sdrh     }
390d24cc427Sdrh     zResult[j] = 0;
3917e18c259Sdanielk1977     sqlite3_result_text(context, zResult, 4, 1);
392d24cc427Sdrh   }else{
3937e18c259Sdanielk1977     sqlite3_result_text(context, "?000", 4, 0);
394d24cc427Sdrh   }
395d24cc427Sdrh }
396d24cc427Sdrh #endif
397d24cc427Sdrh 
398193a6b41Sdrh #ifdef SQLITE_TEST
399193a6b41Sdrh /*
400193a6b41Sdrh ** This function generates a string of random characters.  Used for
401193a6b41Sdrh ** generating test data.
402193a6b41Sdrh */
4030ae8b831Sdanielk1977 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
404bbd82df6Sdrh   static const unsigned char zSrc[] =
405193a6b41Sdrh      "abcdefghijklmnopqrstuvwxyz"
406193a6b41Sdrh      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
407193a6b41Sdrh      "0123456789"
408193a6b41Sdrh      ".-!,:*^+=_|?/<> ";
409193a6b41Sdrh   int iMin, iMax, n, r, i;
410bbd82df6Sdrh   unsigned char zBuf[1000];
411193a6b41Sdrh   if( argc>=1 ){
41251ad0ecdSdanielk1977     iMin = atoi(sqlite3_value_data(argv[0]));
413193a6b41Sdrh     if( iMin<0 ) iMin = 0;
414193a6b41Sdrh     if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
415193a6b41Sdrh   }else{
416193a6b41Sdrh     iMin = 1;
417193a6b41Sdrh   }
418193a6b41Sdrh   if( argc>=2 ){
41951ad0ecdSdanielk1977     iMax = atoi(sqlite3_value_data(argv[1]));
420193a6b41Sdrh     if( iMax<iMin ) iMax = iMin;
4211dba7279Sdrh     if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
422193a6b41Sdrh   }else{
423193a6b41Sdrh     iMax = 50;
424193a6b41Sdrh   }
425193a6b41Sdrh   n = iMin;
426193a6b41Sdrh   if( iMax>iMin ){
4274adee20fSdanielk1977     sqlite3Randomness(sizeof(r), &r);
428bbd82df6Sdrh     r &= 0x7fffffff;
429193a6b41Sdrh     n += r%(iMax + 1 - iMin);
430193a6b41Sdrh   }
4311dba7279Sdrh   assert( n<sizeof(zBuf) );
4324adee20fSdanielk1977   sqlite3Randomness(n, zBuf);
433193a6b41Sdrh   for(i=0; i<n; i++){
434bbd82df6Sdrh     zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
435193a6b41Sdrh   }
436193a6b41Sdrh   zBuf[n] = 0;
4377e18c259Sdanielk1977   sqlite3_result_text(context, zBuf, n, 1);
438193a6b41Sdrh }
439193a6b41Sdrh #endif
440193a6b41Sdrh 
4410ac65892Sdrh /*
442d3a149efSdrh ** An instance of the following structure holds the context of a
443dd5baa95Sdrh ** sum() or avg() aggregate computation.
444dd5baa95Sdrh */
445dd5baa95Sdrh typedef struct SumCtx SumCtx;
446dd5baa95Sdrh struct SumCtx {
447dd5baa95Sdrh   double sum;     /* Sum of terms */
448739105c7Sdrh   int cnt;        /* Number of elements summed */
449dd5baa95Sdrh };
450dd5baa95Sdrh 
451dd5baa95Sdrh /*
452dd5baa95Sdrh ** Routines used to compute the sum or average.
453dd5baa95Sdrh */
4540ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
455dd5baa95Sdrh   SumCtx *p;
456dd5baa95Sdrh   if( argc<1 ) return;
4570ae8b831Sdanielk1977   p = sqlite3_get_context(context, sizeof(*p));
4586ddcca55Sdanielk1977   if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
4596ddcca55Sdanielk1977     p->sum += sqlite3_value_float(argv[0]);
460739105c7Sdrh     p->cnt++;
461739105c7Sdrh   }
462dd5baa95Sdrh }
4630ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){
464dd5baa95Sdrh   SumCtx *p;
4650ae8b831Sdanielk1977   p = sqlite3_get_context(context, sizeof(*p));
4667e18c259Sdanielk1977   sqlite3_result_double(context, p ? p->sum : 0.0);
467dd5baa95Sdrh }
4680ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){
469dd5baa95Sdrh   SumCtx *p;
4700ae8b831Sdanielk1977   p = sqlite3_get_context(context, sizeof(*p));
471739105c7Sdrh   if( p && p->cnt>0 ){
4727e18c259Sdanielk1977     sqlite3_result_double(context, p->sum/(double)p->cnt);
473dd5baa95Sdrh   }
474dd5baa95Sdrh }
475dd5baa95Sdrh 
476dd5baa95Sdrh /*
477dd5baa95Sdrh ** An instance of the following structure holds the context of a
478a2ed5601Sdrh ** variance or standard deviation computation.
479d3a149efSdrh */
480d3a149efSdrh typedef struct StdDevCtx StdDevCtx;
481d3a149efSdrh struct StdDevCtx {
482d3a149efSdrh   double sum;     /* Sum of terms */
483d3a149efSdrh   double sum2;    /* Sum of the squares of terms */
484739105c7Sdrh   int cnt;        /* Number of terms counted */
485d3a149efSdrh };
486d3a149efSdrh 
487ef2daf54Sdrh #if 0   /* Omit because math library is required */
488d3a149efSdrh /*
489d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate.
490d3a149efSdrh */
4910ae8b831Sdanielk1977 static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
492d3a149efSdrh   StdDevCtx *p;
493d3a149efSdrh   double x;
4941350b030Sdrh   if( argc<1 ) return;
49524b03fd0Sdanielk1977   p = sqlite3_aggregate_context(context, sizeof(*p));
496739105c7Sdrh   if( p && argv[0] ){
4974adee20fSdanielk1977     x = sqlite3AtoF(argv[0], 0);
498d3a149efSdrh     p->sum += x;
499d3a149efSdrh     p->sum2 += x*x;
500739105c7Sdrh     p->cnt++;
501739105c7Sdrh   }
502d3a149efSdrh }
5030ae8b831Sdanielk1977 static void stdDevFinalize(sqlite3_context *context){
50424b03fd0Sdanielk1977   double rN = sqlite3_aggregate_count(context);
50524b03fd0Sdanielk1977   StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
506739105c7Sdrh   if( p && p->cnt>1 ){
507739105c7Sdrh     double rCnt = cnt;
50824b03fd0Sdanielk1977     sqlite3_set_result_double(context,
509739105c7Sdrh        sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
510d3a149efSdrh   }
511d3a149efSdrh }
512ef2daf54Sdrh #endif
513d3a149efSdrh 
5140bce8354Sdrh /*
5150bce8354Sdrh ** The following structure keeps track of state information for the
5160bce8354Sdrh ** count() aggregate function.
5170bce8354Sdrh */
5180bce8354Sdrh typedef struct CountCtx CountCtx;
5190bce8354Sdrh struct CountCtx {
5200bce8354Sdrh   int n;
5210bce8354Sdrh };
522dd5baa95Sdrh 
5230bce8354Sdrh /*
5240bce8354Sdrh ** Routines to implement the count() aggregate function.
5250bce8354Sdrh */
5260ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
5270bce8354Sdrh   CountCtx *p;
5280ae8b831Sdanielk1977   p = sqlite3_get_context(context, sizeof(*p));
5296ddcca55Sdanielk1977   if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
5300bce8354Sdrh     p->n++;
5310bce8354Sdrh   }
5320bce8354Sdrh }
5330ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){
5340bce8354Sdrh   CountCtx *p;
5350ae8b831Sdanielk1977   p = sqlite3_get_context(context, sizeof(*p));
5367e18c259Sdanielk1977   sqlite3_result_int32(context, p ? p->n : 0);
5370bce8354Sdrh }
5380bce8354Sdrh 
5390bce8354Sdrh /*
5400bce8354Sdrh ** This function tracks state information for the min() and max()
5410bce8354Sdrh ** aggregate functions.
5420bce8354Sdrh */
5430bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx;
5440bce8354Sdrh struct MinMaxCtx {
5450bce8354Sdrh   char *z;         /* The best so far */
5460bce8354Sdrh   char zBuf[28];   /* Space that can be used for storage */
5470bce8354Sdrh };
5480bce8354Sdrh 
5490bce8354Sdrh /*
5500bce8354Sdrh ** Routines to implement min() and max() aggregate functions.
5510bce8354Sdrh */
5520ae8b831Sdanielk1977 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
55388208050Sdanielk1977   int max = 0;
55488208050Sdanielk1977   int cmp = 0;
55588208050Sdanielk1977   Mem *pArg  = (Mem *)argv[0];
5560ae8b831Sdanielk1977   Mem *pBest = (Mem *)sqlite3_get_context(context, sizeof(*pBest));
557268380caSdrh 
55888208050Sdanielk1977   if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
55988208050Sdanielk1977 
56088208050Sdanielk1977   if( pBest->flags ){
5617e18c259Sdanielk1977     /* This step function is used for both the min() and max() aggregates,
5627e18c259Sdanielk1977     ** the only difference between the two being that the sense of the
5637e18c259Sdanielk1977     ** comparison is inverted. For the max() aggregate, the
5647e18c259Sdanielk1977     ** sqlite3_user_data() function returns (void *)-1. For min() it
5657e18c259Sdanielk1977     ** returns (void *)db, where db is the sqlite3* database pointer.
5667e18c259Sdanielk1977     ** Therefore the next statement sets variable 'max' to 1 for the max()
5677e18c259Sdanielk1977     ** aggregate, or 0 for min().
5687e18c259Sdanielk1977     */
56988208050Sdanielk1977     max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
57088208050Sdanielk1977     cmp = sqlite3MemCompare(pBest, pArg, 0);
57188208050Sdanielk1977     if( (max && cmp<0) || (!max && cmp>0) ){
5727e18c259Sdanielk1977       sqlite3VdbeMemCopy(pBest, pArg);
57388208050Sdanielk1977     }
5740bce8354Sdrh   }else{
5757e18c259Sdanielk1977     sqlite3VdbeMemCopy(pBest, pArg);
5760bce8354Sdrh   }
5770bce8354Sdrh }
5780ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){
57988208050Sdanielk1977   sqlite3_value *pRes;
5800ae8b831Sdanielk1977   pRes = (sqlite3_value *)sqlite3_get_context(context, sizeof(Mem));
58188208050Sdanielk1977 
58288208050Sdanielk1977   if( pRes->flags ){
58388208050Sdanielk1977     switch( sqlite3_value_type(pRes) ){
58488208050Sdanielk1977       case SQLITE3_INTEGER:
5857e18c259Sdanielk1977         sqlite3_result_int32(context, sqlite3_value_int(pRes));
58688208050Sdanielk1977         break;
58788208050Sdanielk1977       case SQLITE3_FLOAT:
5887e18c259Sdanielk1977         sqlite3_result_double(context, sqlite3_value_float(pRes));
58988208050Sdanielk1977       case SQLITE3_TEXT:
59088208050Sdanielk1977       case SQLITE3_BLOB:
5917e18c259Sdanielk1977         sqlite3_result_text(context,
5927e18c259Sdanielk1977             sqlite3_value_data(pRes), sqlite3_value_bytes(pRes), 1);
59388208050Sdanielk1977         break;
59488208050Sdanielk1977       case SQLITE3_NULL:
59588208050Sdanielk1977       default:
59688208050Sdanielk1977         assert(0);
5970bce8354Sdrh     }
5980bce8354Sdrh   }
5990bce8354Sdrh }
600dd5baa95Sdrh 
601d3a149efSdrh /*
602a2ed5601Sdrh ** This function registered all of the above C functions as SQL
603a2ed5601Sdrh ** functions.  This should be the only routine in this file with
604a2ed5601Sdrh ** external linkage.
605dc04c583Sdrh */
6064adee20fSdanielk1977 void sqlite3RegisterBuiltinFunctions(sqlite *db){
6070bce8354Sdrh   static struct {
6080bce8354Sdrh      char *zName;
609268380caSdrh      signed char nArg;
610268380caSdrh      signed char dataType;
611268380caSdrh      u8 argType;               /* 0: none.  1: db  2: (-1) */
6120ae8b831Sdanielk1977      void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
6130bce8354Sdrh   } aFuncs[] = {
614268380caSdrh     { "min",       -1, SQLITE_ARGS,    0, minmaxFunc },
615268380caSdrh     { "min",        0, 0,              0, 0          },
616268380caSdrh     { "max",       -1, SQLITE_ARGS,    2, minmaxFunc },
617268380caSdrh     { "max",        0, 0,              2, 0          },
618268380caSdrh     { "typeof",     1, SQLITE_TEXT,    0, typeofFunc },
619a37cdde0Sdanielk1977     { "classof",    1, SQLITE_TEXT,    0, typeofFunc }, /* FIX ME: hack */
620268380caSdrh     { "length",     1, SQLITE_NUMERIC, 0, lengthFunc },
621268380caSdrh     { "substr",     3, SQLITE_TEXT,    0, substrFunc },
622268380caSdrh     { "abs",        1, SQLITE_NUMERIC, 0, absFunc    },
623268380caSdrh     { "round",      1, SQLITE_NUMERIC, 0, roundFunc  },
624268380caSdrh     { "round",      2, SQLITE_NUMERIC, 0, roundFunc  },
625268380caSdrh     { "upper",      1, SQLITE_TEXT,    0, upperFunc  },
626268380caSdrh     { "lower",      1, SQLITE_TEXT,    0, lowerFunc  },
627268380caSdrh     { "coalesce",  -1, SQLITE_ARGS,    0, ifnullFunc },
628268380caSdrh     { "coalesce",   0, 0,              0, 0          },
629268380caSdrh     { "coalesce",   1, 0,              0, 0          },
630268380caSdrh     { "ifnull",     2, SQLITE_ARGS,    0, ifnullFunc },
631268380caSdrh     { "random",    -1, SQLITE_NUMERIC, 0, randomFunc },
632268380caSdrh     { "like",       2, SQLITE_NUMERIC, 0, likeFunc   },
633268380caSdrh     { "glob",       2, SQLITE_NUMERIC, 0, globFunc   },
634268380caSdrh     { "nullif",     2, SQLITE_ARGS,    0, nullifFunc },
63596fc5fe6Sdanielk1977     { "sqlite_version",0,SQLITE_TEXT,  0, versionFunc},
636268380caSdrh     { "quote",      1, SQLITE_ARGS,    0, quoteFunc  },
637268380caSdrh     { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
638268380caSdrh     { "change_count",      0, SQLITE_NUMERIC, 1, change_count      },
639268380caSdrh     { "last_statement_change_count",
640268380caSdrh                            0, SQLITE_NUMERIC, 1, last_statement_change_count },
641d24cc427Sdrh #ifdef SQLITE_SOUNDEX
642268380caSdrh     { "soundex",    1, SQLITE_TEXT,    0, soundexFunc},
643d24cc427Sdrh #endif
644193a6b41Sdrh #ifdef SQLITE_TEST
645268380caSdrh     { "randstr",    2, SQLITE_TEXT,    0, randStr    },
646193a6b41Sdrh #endif
6470bce8354Sdrh   };
6480bce8354Sdrh   static struct {
6490bce8354Sdrh     char *zName;
650268380caSdrh     signed char nArg;
651268380caSdrh     signed char dataType;
652268380caSdrh     u8 argType;
6530ae8b831Sdanielk1977     void (*xStep)(sqlite3_context*,int,sqlite3_value**);
6540ae8b831Sdanielk1977     void (*xFinalize)(sqlite3_context*);
6550bce8354Sdrh   } aAggs[] = {
656268380caSdrh     { "min",    1, 0,              0, minmaxStep,   minMaxFinalize },
657268380caSdrh     { "max",    1, 0,              2, minmaxStep,   minMaxFinalize },
658268380caSdrh     { "sum",    1, SQLITE_NUMERIC, 0, sumStep,      sumFinalize    },
659268380caSdrh     { "avg",    1, SQLITE_NUMERIC, 0, sumStep,      avgFinalize    },
660268380caSdrh     { "count",  0, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
661268380caSdrh     { "count",  1, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
662ef2daf54Sdrh #if 0
663268380caSdrh     { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep,   stdDevFinalize },
664ef2daf54Sdrh #endif
6650bce8354Sdrh   };
666268380caSdrh   static const char *azTypeFuncs[] = { "min", "max", "typeof" };
6670bce8354Sdrh   int i;
6680bce8354Sdrh 
6690bce8354Sdrh   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
670268380caSdrh     void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
671*6590493dSdanielk1977     sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0,
672*6590493dSdanielk1977         pArg, aFuncs[i].xFunc, 0, 0);
673c9b84a1fSdrh     if( aFuncs[i].xFunc ){
67424b03fd0Sdanielk1977       sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
675c9b84a1fSdrh     }
6760bce8354Sdrh   }
6770bce8354Sdrh   for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
678268380caSdrh     void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
679*6590493dSdanielk1977     sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg,
680*6590493dSdanielk1977         0, aAggs[i].xStep, aAggs[i].xFinalize);
68124b03fd0Sdanielk1977     sqlite3_function_type(db, aAggs[i].zName, aAggs[i].dataType);
6820bce8354Sdrh   }
68351ad0ecdSdanielk1977 
684268380caSdrh   for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
685268380caSdrh     int n = strlen(azTypeFuncs[i]);
6864adee20fSdanielk1977     FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n);
687268380caSdrh     while( p ){
688268380caSdrh       p->includeTypes = 1;
689268380caSdrh       p = p->pNext;
690268380caSdrh     }
691268380caSdrh   }
6924adee20fSdanielk1977   sqlite3RegisterDateTimeFunctions(db);
693dc04c583Sdrh }
6944adee20fSdanielk1977 
6954adee20fSdanielk1977 
6964adee20fSdanielk1977 
697