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*4adee20fSdanielk1977 ** $Id: func.c,v 1.44 2004/05/08 08:23:25 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 */ 31268380caSdrh static void minmaxFunc(sqlite_func *context, int argc, const char **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() */ 360bce8354Sdrh 3789425d5eSdrh if( argc==0 ) return; 38268380caSdrh mask = (int)sqlite_user_data(context); 390bce8354Sdrh zBest = argv[0]; 408912d106Sdrh if( zBest==0 ) return; 41268380caSdrh if( argv[1][0]=='n' ){ 42*4adee20fSdanielk1977 xCompare = sqlite3Compare; 43268380caSdrh }else{ 44268380caSdrh xCompare = strcmp; 45268380caSdrh } 46268380caSdrh for(i=2; i<argc; i+=2){ 478912d106Sdrh if( argv[i]==0 ) return; 48268380caSdrh if( (xCompare(argv[i], zBest)^mask)<0 ){ 490bce8354Sdrh zBest = argv[i]; 500bce8354Sdrh } 510bce8354Sdrh } 520bce8354Sdrh sqlite_set_result_string(context, zBest, -1); 530bce8354Sdrh } 540bce8354Sdrh 55268380caSdrh /* 56268380caSdrh ** Return the type of the argument. 57268380caSdrh */ 58268380caSdrh static void typeofFunc(sqlite_func *context, int argc, const char **argv){ 59268380caSdrh assert( argc==2 ); 60268380caSdrh sqlite_set_result_string(context, argv[1], -1); 610bce8354Sdrh } 620bce8354Sdrh 630bce8354Sdrh /* 640bce8354Sdrh ** Implementation of the length() function 650bce8354Sdrh */ 660bce8354Sdrh static void lengthFunc(sqlite_func *context, int argc, const char **argv){ 670bce8354Sdrh const char *z; 680bce8354Sdrh int len; 690bce8354Sdrh 700bce8354Sdrh assert( argc==1 ); 710bce8354Sdrh z = argv[0]; 728912d106Sdrh if( z==0 ) return; 730bce8354Sdrh #ifdef SQLITE_UTF8 740bce8354Sdrh for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } 750bce8354Sdrh #else 760bce8354Sdrh len = strlen(z); 770bce8354Sdrh #endif 780bce8354Sdrh sqlite_set_result_int(context, len); 790bce8354Sdrh } 800bce8354Sdrh 810bce8354Sdrh /* 820bce8354Sdrh ** Implementation of the abs() function 830bce8354Sdrh */ 840bce8354Sdrh static void absFunc(sqlite_func *context, int argc, const char **argv){ 850bce8354Sdrh const char *z; 860bce8354Sdrh assert( argc==1 ); 870bce8354Sdrh z = argv[0]; 888912d106Sdrh if( z==0 ) return; 898912d106Sdrh if( z[0]=='-' && isdigit(z[1]) ) z++; 900bce8354Sdrh sqlite_set_result_string(context, z, -1); 910bce8354Sdrh } 920bce8354Sdrh 930bce8354Sdrh /* 940bce8354Sdrh ** Implementation of the substr() function 950bce8354Sdrh */ 960bce8354Sdrh static void substrFunc(sqlite_func *context, int argc, const char **argv){ 970bce8354Sdrh const char *z; 980bce8354Sdrh #ifdef SQLITE_UTF8 990bce8354Sdrh const char *z2; 1000bce8354Sdrh int i; 1010bce8354Sdrh #endif 1020bce8354Sdrh int p1, p2, len; 1030bce8354Sdrh assert( argc==3 ); 1040bce8354Sdrh z = argv[0]; 1050bce8354Sdrh if( z==0 ) return; 1060bce8354Sdrh p1 = atoi(argv[1]?argv[1]:0); 1070bce8354Sdrh p2 = atoi(argv[2]?argv[2]:0); 1080bce8354Sdrh #ifdef SQLITE_UTF8 10947c8a679Sdrh for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } 1100bce8354Sdrh #else 1110bce8354Sdrh len = strlen(z); 1120bce8354Sdrh #endif 1130bce8354Sdrh if( p1<0 ){ 11489425d5eSdrh p1 += len; 115653bc759Sdrh if( p1<0 ){ 116653bc759Sdrh p2 += p1; 117653bc759Sdrh p1 = 0; 118653bc759Sdrh } 1190bce8354Sdrh }else if( p1>0 ){ 1200bce8354Sdrh p1--; 1210bce8354Sdrh } 1220bce8354Sdrh if( p1+p2>len ){ 1230bce8354Sdrh p2 = len-p1; 1240bce8354Sdrh } 1250bce8354Sdrh #ifdef SQLITE_UTF8 12677396304Sdrh for(i=0; i<p1 && z[i]; i++){ 12747c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p1++; 1280bce8354Sdrh } 12947c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } 13077396304Sdrh for(; i<p1+p2 && z[i]; i++){ 13147c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p2++; 1320bce8354Sdrh } 13347c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } 1340bce8354Sdrh #endif 135653bc759Sdrh if( p2<0 ) p2 = 0; 1360bce8354Sdrh sqlite_set_result_string(context, &z[p1], p2); 1370bce8354Sdrh } 1380bce8354Sdrh 1390bce8354Sdrh /* 1400bce8354Sdrh ** Implementation of the round() function 1410bce8354Sdrh */ 1420bce8354Sdrh static void roundFunc(sqlite_func *context, int argc, const char **argv){ 1430bce8354Sdrh int n; 1440bce8354Sdrh double r; 1450bce8354Sdrh char zBuf[100]; 1460bce8354Sdrh assert( argc==1 || argc==2 ); 1478912d106Sdrh if( argv[0]==0 || (argc==2 && argv[1]==0) ) return; 1488912d106Sdrh n = argc==2 ? atoi(argv[1]) : 0; 1490bce8354Sdrh if( n>30 ) n = 30; 1500bce8354Sdrh if( n<0 ) n = 0; 151*4adee20fSdanielk1977 r = sqlite3AtoF(argv[0], 0); 1520bce8354Sdrh sprintf(zBuf,"%.*f",n,r); 1530bce8354Sdrh sqlite_set_result_string(context, zBuf, -1); 1540bce8354Sdrh } 155dc04c583Sdrh 156dc04c583Sdrh /* 157dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 158dc04c583Sdrh */ 1591350b030Sdrh static void upperFunc(sqlite_func *context, int argc, const char **argv){ 160dc04c583Sdrh char *z; 161dc04c583Sdrh int i; 162dc04c583Sdrh if( argc<1 || argv[0]==0 ) return; 163dc04c583Sdrh z = sqlite_set_result_string(context, argv[0], -1); 164dc04c583Sdrh if( z==0 ) return; 165dc04c583Sdrh for(i=0; z[i]; i++){ 166dc04c583Sdrh if( islower(z[i]) ) z[i] = toupper(z[i]); 167dc04c583Sdrh } 168dc04c583Sdrh } 1691350b030Sdrh static void lowerFunc(sqlite_func *context, int argc, const char **argv){ 170dc04c583Sdrh char *z; 171dc04c583Sdrh int i; 172dc04c583Sdrh if( argc<1 || argv[0]==0 ) return; 173dc04c583Sdrh z = sqlite_set_result_string(context, argv[0], -1); 174dc04c583Sdrh if( z==0 ) return; 175dc04c583Sdrh for(i=0; z[i]; i++){ 176dc04c583Sdrh if( isupper(z[i]) ) z[i] = tolower(z[i]); 177dc04c583Sdrh } 178dc04c583Sdrh } 179dc04c583Sdrh 180dc04c583Sdrh /* 181fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 182b6c9e6e6Sjplyon ** All three do the same thing. They return the first non-NULL 183b6c9e6e6Sjplyon ** argument. 1843212e182Sdrh */ 1853212e182Sdrh static void ifnullFunc(sqlite_func *context, int argc, const char **argv){ 186fbc99082Sdrh int i; 187fbc99082Sdrh for(i=0; i<argc; i++){ 188fbc99082Sdrh if( argv[i] ){ 189fbc99082Sdrh sqlite_set_result_string(context, argv[i], -1); 190fbc99082Sdrh break; 191fbc99082Sdrh } 192fbc99082Sdrh } 1933212e182Sdrh } 1943212e182Sdrh 1953212e182Sdrh /* 196f9ffac96Sdrh ** Implementation of random(). Return a random integer. 197f9ffac96Sdrh */ 198f9ffac96Sdrh static void randomFunc(sqlite_func *context, int argc, const char **argv){ 199bbd82df6Sdrh int r; 200*4adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 201bbd82df6Sdrh sqlite_set_result_int(context, r); 202f9ffac96Sdrh } 203f9ffac96Sdrh 204f9ffac96Sdrh /* 2056ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 2066ed41ad7Sdrh ** value is the same as the sqlite_last_insert_rowid() API function. 2076ed41ad7Sdrh */ 2080ac65892Sdrh static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){ 2096ed41ad7Sdrh sqlite *db = sqlite_user_data(context); 2106ed41ad7Sdrh sqlite_set_result_int(context, sqlite_last_insert_rowid(db)); 2116ed41ad7Sdrh } 2126ed41ad7Sdrh 213f146a776Srdc /* 214f146a776Srdc ** Implementation of the change_count() SQL function. The return 215f146a776Srdc ** value is the same as the sqlite_changes() API function. 216f146a776Srdc */ 217b0c374ffSrdc static void change_count(sqlite_func *context, int arg, const char **argv){ 218b0c374ffSrdc sqlite *db = sqlite_user_data(context); 219b0c374ffSrdc sqlite_set_result_int(context, sqlite_changes(db)); 220b0c374ffSrdc } 221f146a776Srdc 222f146a776Srdc /* 223f146a776Srdc ** Implementation of the last_statement_change_count() SQL function. The 224f146a776Srdc ** return value is the same as the sqlite_last_statement_changes() API function. 225f146a776Srdc */ 226b0c374ffSrdc static void last_statement_change_count(sqlite_func *context, int arg, 227b0c374ffSrdc const char **argv){ 228b0c374ffSrdc sqlite *db = sqlite_user_data(context); 229b0c374ffSrdc sqlite_set_result_int(context, sqlite_last_statement_changes(db)); 230b0c374ffSrdc } 231b0c374ffSrdc 2326ed41ad7Sdrh /* 2330ac65892Sdrh ** Implementation of the like() SQL function. This function implements 2340ac65892Sdrh ** the build-in LIKE operator. The first argument to the function is the 2350ac65892Sdrh ** string and the second argument is the pattern. So, the SQL statements: 2360ac65892Sdrh ** 2370ac65892Sdrh ** A LIKE B 2380ac65892Sdrh ** 2390ac65892Sdrh ** is implemented as like(A,B). 2400ac65892Sdrh */ 2410ac65892Sdrh static void likeFunc(sqlite_func *context, int arg, const char **argv){ 2428912d106Sdrh if( argv[0]==0 || argv[1]==0 ) return; 243ec1bd0bdSdrh sqlite_set_result_int(context, 244*4adee20fSdanielk1977 sqlite3LikeCompare((const unsigned char*)argv[0], 245ec1bd0bdSdrh (const unsigned char*)argv[1])); 2460ac65892Sdrh } 2470ac65892Sdrh 2480ac65892Sdrh /* 2490ac65892Sdrh ** Implementation of the glob() SQL function. This function implements 2500ac65892Sdrh ** the build-in GLOB operator. The first argument to the function is the 2510ac65892Sdrh ** string and the second argument is the pattern. So, the SQL statements: 2520ac65892Sdrh ** 2530ac65892Sdrh ** A GLOB B 2540ac65892Sdrh ** 2550ac65892Sdrh ** is implemented as glob(A,B). 2560ac65892Sdrh */ 2570ac65892Sdrh static void globFunc(sqlite_func *context, int arg, const char **argv){ 2588912d106Sdrh if( argv[0]==0 || argv[1]==0 ) return; 259ec1bd0bdSdrh sqlite_set_result_int(context, 260*4adee20fSdanielk1977 sqlite3GlobCompare((const unsigned char*)argv[0], 261ec1bd0bdSdrh (const unsigned char*)argv[1])); 2628912d106Sdrh } 2638912d106Sdrh 2648912d106Sdrh /* 2658912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 2668912d106Sdrh ** argument if the arguments are different. The result is NULL if the 2678912d106Sdrh ** arguments are equal to each other. 2688912d106Sdrh */ 2698912d106Sdrh static void nullifFunc(sqlite_func *context, int argc, const char **argv){ 270*4adee20fSdanielk1977 if( argv[0]!=0 && sqlite3Compare(argv[0],argv[1])!=0 ){ 2718912d106Sdrh sqlite_set_result_string(context, argv[0], -1); 2728912d106Sdrh } 2730ac65892Sdrh } 2740ac65892Sdrh 275647cb0e1Sdrh /* 276647cb0e1Sdrh ** Implementation of the VERSION(*) function. The result is the version 277647cb0e1Sdrh ** of the SQLite library that is running. 278647cb0e1Sdrh */ 279647cb0e1Sdrh static void versionFunc(sqlite_func *context, int argc, const char **argv){ 280647cb0e1Sdrh sqlite_set_result_string(context, sqlite_version, -1); 281647cb0e1Sdrh } 282647cb0e1Sdrh 28347394703Sdrh /* 28447394703Sdrh ** EXPERIMENTAL - This is not an official function. The interface may 28547394703Sdrh ** change. This function may disappear. Do not write code that depends 28647394703Sdrh ** on this function. 28747394703Sdrh ** 28847394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 28947394703Sdrh ** argument. If the argument is numeric, the return value is the same as 29047394703Sdrh ** the argument. If the argument is NULL, the return value is the string 29147394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 29247394703Sdrh ** single-quote escapes. 29347394703Sdrh */ 29447394703Sdrh static void quoteFunc(sqlite_func *context, int argc, const char **argv){ 29547394703Sdrh if( argc<1 ) return; 29647394703Sdrh if( argv[0]==0 ){ 29747394703Sdrh sqlite_set_result_string(context, "NULL", 4); 298*4adee20fSdanielk1977 }else if( sqlite3IsNumber(argv[0]) ){ 29947394703Sdrh sqlite_set_result_string(context, argv[0], -1); 30047394703Sdrh }else{ 30147394703Sdrh int i,j,n; 30247394703Sdrh char *z; 30347394703Sdrh for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; } 30447394703Sdrh z = sqliteMalloc( i+n+3 ); 30547394703Sdrh if( z==0 ) return; 30647394703Sdrh z[0] = '\''; 30747394703Sdrh for(i=0, j=1; argv[0][i]; i++){ 30847394703Sdrh z[j++] = argv[0][i]; 30947394703Sdrh if( argv[0][i]=='\'' ){ 31047394703Sdrh z[j++] = '\''; 31147394703Sdrh } 31247394703Sdrh } 31347394703Sdrh z[j++] = '\''; 31447394703Sdrh z[j] = 0; 31547394703Sdrh sqlite_set_result_string(context, z, j); 31647394703Sdrh sqliteFree(z); 31747394703Sdrh } 31847394703Sdrh } 31947394703Sdrh 320d24cc427Sdrh #ifdef SQLITE_SOUNDEX 321d24cc427Sdrh /* 322d24cc427Sdrh ** Compute the soundex encoding of a word. 323d24cc427Sdrh */ 324d24cc427Sdrh static void soundexFunc(sqlite_func *context, int argc, const char **argv){ 325d24cc427Sdrh char zResult[8]; 326d24cc427Sdrh const char *zIn; 327d24cc427Sdrh int i, j; 328d24cc427Sdrh static const unsigned char iCode[] = { 329d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 330d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 331d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 332d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 333d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 334d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 335d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 336d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 337d24cc427Sdrh }; 338d24cc427Sdrh assert( argc==1 ); 339d24cc427Sdrh zIn = argv[0]; 340d24cc427Sdrh for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} 341d24cc427Sdrh if( zIn[i] ){ 342d24cc427Sdrh zResult[0] = toupper(zIn[i]); 343d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 344d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 345d24cc427Sdrh if( code>0 ){ 346d24cc427Sdrh zResult[j++] = code + '0'; 347d24cc427Sdrh } 348d24cc427Sdrh } 349d24cc427Sdrh while( j<4 ){ 350d24cc427Sdrh zResult[j++] = '0'; 351d24cc427Sdrh } 352d24cc427Sdrh zResult[j] = 0; 353d24cc427Sdrh sqlite_set_result_string(context, zResult, 4); 354d24cc427Sdrh }else{ 355937dd84dSdrh sqlite_set_result_string(context, "?000", 4); 356d24cc427Sdrh } 357d24cc427Sdrh } 358d24cc427Sdrh #endif 359d24cc427Sdrh 360193a6b41Sdrh #ifdef SQLITE_TEST 361193a6b41Sdrh /* 362193a6b41Sdrh ** This function generates a string of random characters. Used for 363193a6b41Sdrh ** generating test data. 364193a6b41Sdrh */ 365193a6b41Sdrh static void randStr(sqlite_func *context, int argc, const char **argv){ 366bbd82df6Sdrh static const unsigned char zSrc[] = 367193a6b41Sdrh "abcdefghijklmnopqrstuvwxyz" 368193a6b41Sdrh "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 369193a6b41Sdrh "0123456789" 370193a6b41Sdrh ".-!,:*^+=_|?/<> "; 371193a6b41Sdrh int iMin, iMax, n, r, i; 372bbd82df6Sdrh unsigned char zBuf[1000]; 373193a6b41Sdrh if( argc>=1 ){ 374193a6b41Sdrh iMin = atoi(argv[0]); 375193a6b41Sdrh if( iMin<0 ) iMin = 0; 376193a6b41Sdrh if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 377193a6b41Sdrh }else{ 378193a6b41Sdrh iMin = 1; 379193a6b41Sdrh } 380193a6b41Sdrh if( argc>=2 ){ 381193a6b41Sdrh iMax = atoi(argv[1]); 382193a6b41Sdrh if( iMax<iMin ) iMax = iMin; 3831dba7279Sdrh if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 384193a6b41Sdrh }else{ 385193a6b41Sdrh iMax = 50; 386193a6b41Sdrh } 387193a6b41Sdrh n = iMin; 388193a6b41Sdrh if( iMax>iMin ){ 389*4adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 390bbd82df6Sdrh r &= 0x7fffffff; 391193a6b41Sdrh n += r%(iMax + 1 - iMin); 392193a6b41Sdrh } 3931dba7279Sdrh assert( n<sizeof(zBuf) ); 394*4adee20fSdanielk1977 sqlite3Randomness(n, zBuf); 395193a6b41Sdrh for(i=0; i<n; i++){ 396bbd82df6Sdrh zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 397193a6b41Sdrh } 398193a6b41Sdrh zBuf[n] = 0; 399193a6b41Sdrh sqlite_set_result_string(context, zBuf, n); 400193a6b41Sdrh } 401193a6b41Sdrh #endif 402193a6b41Sdrh 4030ac65892Sdrh /* 404d3a149efSdrh ** An instance of the following structure holds the context of a 405dd5baa95Sdrh ** sum() or avg() aggregate computation. 406dd5baa95Sdrh */ 407dd5baa95Sdrh typedef struct SumCtx SumCtx; 408dd5baa95Sdrh struct SumCtx { 409dd5baa95Sdrh double sum; /* Sum of terms */ 410739105c7Sdrh int cnt; /* Number of elements summed */ 411dd5baa95Sdrh }; 412dd5baa95Sdrh 413dd5baa95Sdrh /* 414dd5baa95Sdrh ** Routines used to compute the sum or average. 415dd5baa95Sdrh */ 416dd5baa95Sdrh static void sumStep(sqlite_func *context, int argc, const char **argv){ 417dd5baa95Sdrh SumCtx *p; 418dd5baa95Sdrh if( argc<1 ) return; 419dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 420739105c7Sdrh if( p && argv[0] ){ 421*4adee20fSdanielk1977 p->sum += sqlite3AtoF(argv[0], 0); 422739105c7Sdrh p->cnt++; 423739105c7Sdrh } 424dd5baa95Sdrh } 425dd5baa95Sdrh static void sumFinalize(sqlite_func *context){ 426dd5baa95Sdrh SumCtx *p; 427dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 42889425d5eSdrh sqlite_set_result_double(context, p ? p->sum : 0.0); 429dd5baa95Sdrh } 430dd5baa95Sdrh static void avgFinalize(sqlite_func *context){ 431dd5baa95Sdrh SumCtx *p; 432dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 433739105c7Sdrh if( p && p->cnt>0 ){ 434739105c7Sdrh sqlite_set_result_double(context, p->sum/(double)p->cnt); 435dd5baa95Sdrh } 436dd5baa95Sdrh } 437dd5baa95Sdrh 438dd5baa95Sdrh /* 439dd5baa95Sdrh ** An instance of the following structure holds the context of a 440a2ed5601Sdrh ** variance or standard deviation computation. 441d3a149efSdrh */ 442d3a149efSdrh typedef struct StdDevCtx StdDevCtx; 443d3a149efSdrh struct StdDevCtx { 444d3a149efSdrh double sum; /* Sum of terms */ 445d3a149efSdrh double sum2; /* Sum of the squares of terms */ 446739105c7Sdrh int cnt; /* Number of terms counted */ 447d3a149efSdrh }; 448d3a149efSdrh 449ef2daf54Sdrh #if 0 /* Omit because math library is required */ 450d3a149efSdrh /* 451d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate. 452d3a149efSdrh */ 4531350b030Sdrh static void stdDevStep(sqlite_func *context, int argc, const char **argv){ 454d3a149efSdrh StdDevCtx *p; 455d3a149efSdrh double x; 4561350b030Sdrh if( argc<1 ) return; 4571350b030Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 458739105c7Sdrh if( p && argv[0] ){ 459*4adee20fSdanielk1977 x = sqlite3AtoF(argv[0], 0); 460d3a149efSdrh p->sum += x; 461d3a149efSdrh p->sum2 += x*x; 462739105c7Sdrh p->cnt++; 463739105c7Sdrh } 464d3a149efSdrh } 4651350b030Sdrh static void stdDevFinalize(sqlite_func *context){ 466dd5baa95Sdrh double rN = sqlite_aggregate_count(context); 4671350b030Sdrh StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p)); 468739105c7Sdrh if( p && p->cnt>1 ){ 469739105c7Sdrh double rCnt = cnt; 470d3a149efSdrh sqlite_set_result_double(context, 471739105c7Sdrh sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0))); 472d3a149efSdrh } 473d3a149efSdrh } 474ef2daf54Sdrh #endif 475d3a149efSdrh 4760bce8354Sdrh /* 4770bce8354Sdrh ** The following structure keeps track of state information for the 4780bce8354Sdrh ** count() aggregate function. 4790bce8354Sdrh */ 4800bce8354Sdrh typedef struct CountCtx CountCtx; 4810bce8354Sdrh struct CountCtx { 4820bce8354Sdrh int n; 4830bce8354Sdrh }; 484dd5baa95Sdrh 4850bce8354Sdrh /* 4860bce8354Sdrh ** Routines to implement the count() aggregate function. 4870bce8354Sdrh */ 4880bce8354Sdrh static void countStep(sqlite_func *context, int argc, const char **argv){ 4890bce8354Sdrh CountCtx *p; 4900bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 4910bce8354Sdrh if( (argc==0 || argv[0]) && p ){ 4920bce8354Sdrh p->n++; 4930bce8354Sdrh } 4940bce8354Sdrh } 4950bce8354Sdrh static void countFinalize(sqlite_func *context){ 4960bce8354Sdrh CountCtx *p; 4970bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 498f55f25f0Sdrh sqlite_set_result_int(context, p ? p->n : 0); 4990bce8354Sdrh } 5000bce8354Sdrh 5010bce8354Sdrh /* 5020bce8354Sdrh ** This function tracks state information for the min() and max() 5030bce8354Sdrh ** aggregate functions. 5040bce8354Sdrh */ 5050bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx; 5060bce8354Sdrh struct MinMaxCtx { 5070bce8354Sdrh char *z; /* The best so far */ 5080bce8354Sdrh char zBuf[28]; /* Space that can be used for storage */ 5090bce8354Sdrh }; 5100bce8354Sdrh 5110bce8354Sdrh /* 5120bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 5130bce8354Sdrh */ 514268380caSdrh static void minmaxStep(sqlite_func *context, int argc, const char **argv){ 5150bce8354Sdrh MinMaxCtx *p; 516268380caSdrh int (*xCompare)(const char*, const char*); 517268380caSdrh int mask; /* 0 for min() or 0xffffffff for max() */ 518268380caSdrh 519268380caSdrh assert( argc==2 ); 520268380caSdrh if( argv[1][0]=='n' ){ 521*4adee20fSdanielk1977 xCompare = sqlite3Compare; 5220bce8354Sdrh }else{ 523268380caSdrh xCompare = strcmp; 5240bce8354Sdrh } 525268380caSdrh mask = (int)sqlite_user_data(context); 5260bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 527739105c7Sdrh if( p==0 || argc<1 || argv[0]==0 ) return; 528268380caSdrh if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){ 5298912d106Sdrh int len; 53000706be3Sdrh if( !p->zBuf[0] ){ 5310bce8354Sdrh sqliteFree(p->z); 5320bce8354Sdrh } 5338912d106Sdrh len = strlen(argv[0]); 53400706be3Sdrh if( len < sizeof(p->zBuf)-1 ){ 53500706be3Sdrh p->z = &p->zBuf[1]; 53600706be3Sdrh p->zBuf[0] = 1; 5370bce8354Sdrh }else{ 5380bce8354Sdrh p->z = sqliteMalloc( len+1 ); 53900706be3Sdrh p->zBuf[0] = 0; 5400bce8354Sdrh if( p->z==0 ) return; 5410bce8354Sdrh } 5420bce8354Sdrh strcpy(p->z, argv[0]); 5430bce8354Sdrh } 5440bce8354Sdrh } 5450bce8354Sdrh static void minMaxFinalize(sqlite_func *context){ 5460bce8354Sdrh MinMaxCtx *p; 5470bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 548739105c7Sdrh if( p && p->z ){ 5490bce8354Sdrh sqlite_set_result_string(context, p->z, strlen(p->z)); 5500bce8354Sdrh } 55100706be3Sdrh if( p && !p->zBuf[0] ){ 5520bce8354Sdrh sqliteFree(p->z); 5530bce8354Sdrh } 5540bce8354Sdrh } 555dd5baa95Sdrh 556d3a149efSdrh /* 557a2ed5601Sdrh ** This function registered all of the above C functions as SQL 558a2ed5601Sdrh ** functions. This should be the only routine in this file with 559a2ed5601Sdrh ** external linkage. 560dc04c583Sdrh */ 561*4adee20fSdanielk1977 void sqlite3RegisterBuiltinFunctions(sqlite *db){ 5620bce8354Sdrh static struct { 5630bce8354Sdrh char *zName; 564268380caSdrh signed char nArg; 565268380caSdrh signed char dataType; 566268380caSdrh u8 argType; /* 0: none. 1: db 2: (-1) */ 5670bce8354Sdrh void (*xFunc)(sqlite_func*,int,const char**); 5680bce8354Sdrh } aFuncs[] = { 569268380caSdrh { "min", -1, SQLITE_ARGS, 0, minmaxFunc }, 570268380caSdrh { "min", 0, 0, 0, 0 }, 571268380caSdrh { "max", -1, SQLITE_ARGS, 2, minmaxFunc }, 572268380caSdrh { "max", 0, 0, 2, 0 }, 573268380caSdrh { "typeof", 1, SQLITE_TEXT, 0, typeofFunc }, 574268380caSdrh { "length", 1, SQLITE_NUMERIC, 0, lengthFunc }, 575268380caSdrh { "substr", 3, SQLITE_TEXT, 0, substrFunc }, 576268380caSdrh { "abs", 1, SQLITE_NUMERIC, 0, absFunc }, 577268380caSdrh { "round", 1, SQLITE_NUMERIC, 0, roundFunc }, 578268380caSdrh { "round", 2, SQLITE_NUMERIC, 0, roundFunc }, 579268380caSdrh { "upper", 1, SQLITE_TEXT, 0, upperFunc }, 580268380caSdrh { "lower", 1, SQLITE_TEXT, 0, lowerFunc }, 581268380caSdrh { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc }, 582268380caSdrh { "coalesce", 0, 0, 0, 0 }, 583268380caSdrh { "coalesce", 1, 0, 0, 0 }, 584268380caSdrh { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc }, 585268380caSdrh { "random", -1, SQLITE_NUMERIC, 0, randomFunc }, 586268380caSdrh { "like", 2, SQLITE_NUMERIC, 0, likeFunc }, 587268380caSdrh { "glob", 2, SQLITE_NUMERIC, 0, globFunc }, 588268380caSdrh { "nullif", 2, SQLITE_ARGS, 0, nullifFunc }, 589268380caSdrh { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc}, 590268380caSdrh { "quote", 1, SQLITE_ARGS, 0, quoteFunc }, 591268380caSdrh { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid }, 592268380caSdrh { "change_count", 0, SQLITE_NUMERIC, 1, change_count }, 593268380caSdrh { "last_statement_change_count", 594268380caSdrh 0, SQLITE_NUMERIC, 1, last_statement_change_count }, 595d24cc427Sdrh #ifdef SQLITE_SOUNDEX 596268380caSdrh { "soundex", 1, SQLITE_TEXT, 0, soundexFunc}, 597d24cc427Sdrh #endif 598193a6b41Sdrh #ifdef SQLITE_TEST 599268380caSdrh { "randstr", 2, SQLITE_TEXT, 0, randStr }, 600193a6b41Sdrh #endif 6010bce8354Sdrh }; 6020bce8354Sdrh static struct { 6030bce8354Sdrh char *zName; 604268380caSdrh signed char nArg; 605268380caSdrh signed char dataType; 606268380caSdrh u8 argType; 6070bce8354Sdrh void (*xStep)(sqlite_func*,int,const char**); 6080bce8354Sdrh void (*xFinalize)(sqlite_func*); 6090bce8354Sdrh } aAggs[] = { 610268380caSdrh { "min", 1, 0, 0, minmaxStep, minMaxFinalize }, 611268380caSdrh { "max", 1, 0, 2, minmaxStep, minMaxFinalize }, 612268380caSdrh { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize }, 613268380caSdrh { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize }, 614268380caSdrh { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize }, 615268380caSdrh { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize }, 616ef2daf54Sdrh #if 0 617268380caSdrh { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize }, 618ef2daf54Sdrh #endif 6190bce8354Sdrh }; 620268380caSdrh static const char *azTypeFuncs[] = { "min", "max", "typeof" }; 6210bce8354Sdrh int i; 6220bce8354Sdrh 6230bce8354Sdrh for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 624268380caSdrh void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db; 6250bce8354Sdrh sqlite_create_function(db, aFuncs[i].zName, 626268380caSdrh aFuncs[i].nArg, aFuncs[i].xFunc, pArg); 627c9b84a1fSdrh if( aFuncs[i].xFunc ){ 628c9b84a1fSdrh sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType); 629c9b84a1fSdrh } 6300bce8354Sdrh } 6310bce8354Sdrh for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 632268380caSdrh void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db; 6330bce8354Sdrh sqlite_create_aggregate(db, aAggs[i].zName, 634268380caSdrh aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg); 635c9b84a1fSdrh sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType); 6360bce8354Sdrh } 637268380caSdrh for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){ 638268380caSdrh int n = strlen(azTypeFuncs[i]); 639*4adee20fSdanielk1977 FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n); 640268380caSdrh while( p ){ 641268380caSdrh p->includeTypes = 1; 642268380caSdrh p = p->pNext; 643268380caSdrh } 644268380caSdrh } 645*4adee20fSdanielk1977 sqlite3RegisterDateTimeFunctions(db); 646dc04c583Sdrh } 647*4adee20fSdanielk1977 648*4adee20fSdanielk1977 649*4adee20fSdanielk1977 650