drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2002 February 23 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains the C functions that implement various SQL |
| 13 | ** functions of SQLite. |
| 14 | ** |
| 15 | ** There is only one exported symbol in this file - the function |
| 16 | ** sqliteRegisterBuildinFunctions() found at the bottom of the file. |
| 17 | ** All other code has file scope. |
| 18 | ** |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.9 2002/02/28 03:04:48 drh Exp $ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 20 | */ |
| 21 | #include <ctype.h> |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 22 | #include <math.h> |
| 23 | #include <stdlib.h> |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include "sqliteInt.h" |
| 26 | |
| 27 | /* |
| 28 | ** Implementation of the non-aggregate min() and max() functions |
| 29 | */ |
| 30 | static void minFunc(sqlite_func *context, int argc, const char **argv){ |
| 31 | const char *zBest; |
| 32 | int i; |
| 33 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame^] | 34 | if( argc==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 35 | zBest = argv[0]; |
| 36 | for(i=1; i<argc; i++){ |
| 37 | if( sqliteCompare(argv[i], zBest)<0 ){ |
| 38 | zBest = argv[i]; |
| 39 | } |
| 40 | } |
| 41 | sqlite_set_result_string(context, zBest, -1); |
| 42 | } |
| 43 | static void maxFunc(sqlite_func *context, int argc, const char **argv){ |
| 44 | const char *zBest; |
| 45 | int i; |
| 46 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame^] | 47 | if( argc==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 48 | zBest = argv[0]; |
| 49 | for(i=1; i<argc; i++){ |
| 50 | if( sqliteCompare(argv[i], zBest)>0 ){ |
| 51 | zBest = argv[i]; |
| 52 | } |
| 53 | } |
| 54 | sqlite_set_result_string(context, zBest, -1); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | ** Implementation of the length() function |
| 59 | */ |
| 60 | static void lengthFunc(sqlite_func *context, int argc, const char **argv){ |
| 61 | const char *z; |
| 62 | int len; |
| 63 | |
| 64 | assert( argc==1 ); |
| 65 | z = argv[0]; |
| 66 | if( z==0 ){ |
| 67 | len = 0; |
| 68 | }else{ |
| 69 | #ifdef SQLITE_UTF8 |
| 70 | for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } |
| 71 | #else |
| 72 | len = strlen(z); |
| 73 | #endif |
| 74 | } |
| 75 | sqlite_set_result_int(context, len); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | ** Implementation of the abs() function |
| 80 | */ |
| 81 | static void absFunc(sqlite_func *context, int argc, const char **argv){ |
| 82 | const char *z; |
| 83 | assert( argc==1 ); |
| 84 | z = argv[0]; |
| 85 | if( z && z[0]=='-' && isdigit(z[1]) ) z++; |
| 86 | sqlite_set_result_string(context, z, -1); |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | ** Implementation of the substr() function |
| 91 | */ |
| 92 | static void substrFunc(sqlite_func *context, int argc, const char **argv){ |
| 93 | const char *z; |
| 94 | #ifdef SQLITE_UTF8 |
| 95 | const char *z2; |
| 96 | int i; |
| 97 | #endif |
| 98 | int p1, p2, len; |
| 99 | assert( argc==3 ); |
| 100 | z = argv[0]; |
| 101 | if( z==0 ) return; |
| 102 | p1 = atoi(argv[1]?argv[1]:0); |
| 103 | p2 = atoi(argv[2]?argv[2]:0); |
| 104 | #ifdef SQLITE_UTF8 |
| 105 | for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z)!=0x80 ) len++; } |
| 106 | #else |
| 107 | len = strlen(z); |
| 108 | #endif |
| 109 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame^] | 110 | p1 += len; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 111 | }else if( p1>0 ){ |
| 112 | p1--; |
| 113 | } |
| 114 | if( p1+p2>len ){ |
| 115 | p2 = len-p1; |
| 116 | } |
| 117 | #ifdef SQLITE_UTF8 |
| 118 | for(i=0; i<p1; i++){ |
| 119 | assert( z[i] ); |
| 120 | if( (z[i]&0xc0)!=0x80 ) p1++; |
| 121 | } |
| 122 | for(; i<p1+p2; i++){ |
| 123 | assert( z[i] ); |
| 124 | if( (z[i]&0xc0)!=0x80 ) p2++; |
| 125 | } |
| 126 | #endif |
| 127 | sqlite_set_result_string(context, &z[p1], p2); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** Implementation of the round() function |
| 132 | */ |
| 133 | static void roundFunc(sqlite_func *context, int argc, const char **argv){ |
| 134 | int n; |
| 135 | double r; |
| 136 | char zBuf[100]; |
| 137 | assert( argc==1 || argc==2 ); |
| 138 | n = argc==2 && argv[1] ? atoi(argv[1]) : 0; |
| 139 | if( n>30 ) n = 30; |
| 140 | if( n<0 ) n = 0; |
| 141 | r = argv[0] ? atof(argv[0]) : 0.0; |
| 142 | sprintf(zBuf,"%.*f",n,r); |
| 143 | sqlite_set_result_string(context, zBuf, -1); |
| 144 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | ** Implementation of the upper() and lower() SQL functions. |
| 148 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 149 | static void upperFunc(sqlite_func *context, int argc, const char **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 150 | char *z; |
| 151 | int i; |
| 152 | if( argc<1 || argv[0]==0 ) return; |
| 153 | z = sqlite_set_result_string(context, argv[0], -1); |
| 154 | if( z==0 ) return; |
| 155 | for(i=0; z[i]; i++){ |
| 156 | if( islower(z[i]) ) z[i] = toupper(z[i]); |
| 157 | } |
| 158 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 159 | static void lowerFunc(sqlite_func *context, int argc, const char **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 160 | char *z; |
| 161 | int i; |
| 162 | if( argc<1 || argv[0]==0 ) return; |
| 163 | z = sqlite_set_result_string(context, argv[0], -1); |
| 164 | if( z==0 ) return; |
| 165 | for(i=0; z[i]; i++){ |
| 166 | if( isupper(z[i]) ) z[i] = tolower(z[i]); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 171 | ** Implementation of the IFNULL() and NVL() functions. (both do the |
| 172 | ** same thing. They return their first argument if it is not NULL or |
| 173 | ** their second argument if the first is NULL. |
| 174 | */ |
| 175 | static void ifnullFunc(sqlite_func *context, int argc, const char **argv){ |
| 176 | const char *z; |
| 177 | assert( argc==2 ); |
| 178 | z = argv[0] ? argv[0] : argv[1]; |
| 179 | sqlite_set_result_string(context, z, -1); |
| 180 | } |
| 181 | |
| 182 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 183 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 184 | ** sum() or avg() aggregate computation. |
| 185 | */ |
| 186 | typedef struct SumCtx SumCtx; |
| 187 | struct SumCtx { |
| 188 | double sum; /* Sum of terms */ |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | ** Routines used to compute the sum or average. |
| 193 | */ |
| 194 | static void sumStep(sqlite_func *context, int argc, const char **argv){ |
| 195 | SumCtx *p; |
| 196 | double x; |
| 197 | if( argc<1 ) return; |
| 198 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 199 | if( p==0 ) return; |
| 200 | x = argv[0] ? atof(argv[0]) : 0.0; |
| 201 | p->sum += x; |
| 202 | } |
| 203 | static void sumFinalize(sqlite_func *context){ |
| 204 | SumCtx *p; |
| 205 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame^] | 206 | sqlite_set_result_double(context, p ? p->sum : 0.0); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 207 | } |
| 208 | static void avgFinalize(sqlite_func *context){ |
| 209 | SumCtx *p; |
| 210 | double rN; |
| 211 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 212 | rN = sqlite_aggregate_count(context); |
| 213 | if( p && rN>0.0 ){ |
| 214 | sqlite_set_result_double(context, p->sum/rN); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | ** An instance of the following structure holds the context of a |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 220 | ** variance or standard deviation computation. |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 221 | */ |
| 222 | typedef struct StdDevCtx StdDevCtx; |
| 223 | struct StdDevCtx { |
| 224 | double sum; /* Sum of terms */ |
| 225 | double sum2; /* Sum of the squares of terms */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /* |
| 229 | ** Routines used to compute the standard deviation as an aggregate. |
| 230 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 231 | static void stdDevStep(sqlite_func *context, int argc, const char **argv){ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 232 | StdDevCtx *p; |
| 233 | double x; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 234 | if( argc<1 ) return; |
| 235 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 236 | if( p==0 ) return; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 237 | x = argv[0] ? atof(argv[0]) : 0.0; |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 238 | p->sum += x; |
| 239 | p->sum2 += x*x; |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 240 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 241 | static void stdDevFinalize(sqlite_func *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 242 | double rN = sqlite_aggregate_count(context); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 243 | StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 244 | if( p && rN>1.0 ){ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 245 | sqlite_set_result_double(context, |
| 246 | sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0))); |
| 247 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 248 | } |
| 249 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 250 | /* |
| 251 | ** The following structure keeps track of state information for the |
| 252 | ** count() aggregate function. |
| 253 | */ |
| 254 | typedef struct CountCtx CountCtx; |
| 255 | struct CountCtx { |
| 256 | int n; |
| 257 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 258 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 259 | /* |
| 260 | ** Routines to implement the count() aggregate function. |
| 261 | */ |
| 262 | static void countStep(sqlite_func *context, int argc, const char **argv){ |
| 263 | CountCtx *p; |
| 264 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 265 | if( (argc==0 || argv[0]) && p ){ |
| 266 | p->n++; |
| 267 | } |
| 268 | } |
| 269 | static void countFinalize(sqlite_func *context){ |
| 270 | CountCtx *p; |
| 271 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 272 | sqlite_set_result_int(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | ** This function tracks state information for the min() and max() |
| 277 | ** aggregate functions. |
| 278 | */ |
| 279 | typedef struct MinMaxCtx MinMaxCtx; |
| 280 | struct MinMaxCtx { |
| 281 | char *z; /* The best so far */ |
| 282 | char zBuf[28]; /* Space that can be used for storage */ |
| 283 | }; |
| 284 | |
| 285 | /* |
| 286 | ** Routines to implement min() and max() aggregate functions. |
| 287 | */ |
| 288 | static void minStep(sqlite_func *context, int argc, const char **argv){ |
| 289 | MinMaxCtx *p; |
| 290 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 291 | if( p==0 || argc<1 ) return; |
| 292 | if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){ |
| 293 | if( p->z && p->z!=p->zBuf ){ |
| 294 | sqliteFree(p->z); |
| 295 | } |
| 296 | if( argv[0] ){ |
| 297 | int len = strlen(argv[0]); |
| 298 | if( len < sizeof(p->zBuf) ){ |
| 299 | p->z = p->zBuf; |
| 300 | }else{ |
| 301 | p->z = sqliteMalloc( len+1 ); |
| 302 | if( p->z==0 ) return; |
| 303 | } |
| 304 | strcpy(p->z, argv[0]); |
| 305 | }else{ |
| 306 | p->z = 0; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | static void maxStep(sqlite_func *context, int argc, const char **argv){ |
| 311 | MinMaxCtx *p; |
| 312 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 313 | if( p==0 || argc<1 ) return; |
| 314 | if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){ |
| 315 | if( p->z && p->z!=p->zBuf ){ |
| 316 | sqliteFree(p->z); |
| 317 | } |
| 318 | if( argv[0] ){ |
| 319 | int len = strlen(argv[0]); |
| 320 | if( len < sizeof(p->zBuf) ){ |
| 321 | p->z = p->zBuf; |
| 322 | }else{ |
| 323 | p->z = sqliteMalloc( len+1 ); |
| 324 | if( p->z==0 ) return; |
| 325 | } |
| 326 | strcpy(p->z, argv[0]); |
| 327 | }else{ |
| 328 | p->z = 0; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | static void minMaxFinalize(sqlite_func *context){ |
| 333 | MinMaxCtx *p; |
| 334 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 335 | if( p && p->z ){ |
| 336 | sqlite_set_result_string(context, p->z, strlen(p->z)); |
| 337 | } |
| 338 | if( p && p->z && p->z!=p->zBuf ){ |
| 339 | sqliteFree(p->z); |
| 340 | } |
| 341 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 342 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 343 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 344 | ** This function registered all of the above C functions as SQL |
| 345 | ** functions. This should be the only routine in this file with |
| 346 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 347 | */ |
| 348 | void sqliteRegisterBuildinFunctions(sqlite *db){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 349 | static struct { |
| 350 | char *zName; |
| 351 | int nArg; |
| 352 | void (*xFunc)(sqlite_func*,int,const char**); |
| 353 | } aFuncs[] = { |
| 354 | { "min", -1, minFunc }, |
| 355 | { "max", -1, maxFunc }, |
| 356 | { "length", 1, lengthFunc }, |
| 357 | { "substr", 3, substrFunc }, |
| 358 | { "abs", 1, absFunc }, |
| 359 | { "round", 1, roundFunc }, |
| 360 | { "round", 2, roundFunc }, |
| 361 | { "upper", 1, upperFunc }, |
| 362 | { "lower", 1, lowerFunc }, |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 363 | { "ifnull", 2, ifnullFunc }, |
| 364 | { "nvl", 2, ifnullFunc }, |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 365 | }; |
| 366 | static struct { |
| 367 | char *zName; |
| 368 | int nArg; |
| 369 | void (*xStep)(sqlite_func*,int,const char**); |
| 370 | void (*xFinalize)(sqlite_func*); |
| 371 | } aAggs[] = { |
| 372 | { "min", 1, minStep, minMaxFinalize }, |
| 373 | { "max", 1, maxStep, minMaxFinalize }, |
| 374 | { "sum", 1, sumStep, sumFinalize }, |
| 375 | { "avg", 1, sumStep, avgFinalize }, |
| 376 | { "count", 0, countStep, countFinalize }, |
| 377 | { "count", 1, countStep, countFinalize }, |
| 378 | { "stddev", 1, stdDevStep, stdDevFinalize }, |
| 379 | }; |
| 380 | int i; |
| 381 | |
| 382 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
| 383 | sqlite_create_function(db, aFuncs[i].zName, |
| 384 | aFuncs[i].nArg, aFuncs[i].xFunc, 0); |
| 385 | } |
| 386 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
| 387 | sqlite_create_aggregate(db, aAggs[i].zName, |
| 388 | aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0); |
| 389 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 390 | } |