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 | 771d8c3 | 2003-08-09 21:32:28 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.27 2003/08/09 21:32:28 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" |
drh | 771d8c3 | 2003-08-09 21:32:28 +0000 | [diff] [blame^] | 26 | #include "os.h" |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | ** Implementation of the non-aggregate min() and max() functions |
| 30 | */ |
| 31 | static void minFunc(sqlite_func *context, int argc, const char **argv){ |
| 32 | const char *zBest; |
| 33 | int i; |
| 34 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 35 | if( argc==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 36 | zBest = argv[0]; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 37 | if( zBest==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 38 | for(i=1; i<argc; i++){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 39 | if( argv[i]==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 40 | if( sqliteCompare(argv[i], zBest)<0 ){ |
| 41 | zBest = argv[i]; |
| 42 | } |
| 43 | } |
| 44 | sqlite_set_result_string(context, zBest, -1); |
| 45 | } |
| 46 | static void maxFunc(sqlite_func *context, int argc, const char **argv){ |
| 47 | const char *zBest; |
| 48 | int i; |
| 49 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 50 | if( argc==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 51 | zBest = argv[0]; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 52 | if( zBest==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 53 | for(i=1; i<argc; i++){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 54 | if( argv[i]==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 55 | if( sqliteCompare(argv[i], zBest)>0 ){ |
| 56 | zBest = argv[i]; |
| 57 | } |
| 58 | } |
| 59 | sqlite_set_result_string(context, zBest, -1); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | ** Implementation of the length() function |
| 64 | */ |
| 65 | static void lengthFunc(sqlite_func *context, int argc, const char **argv){ |
| 66 | const char *z; |
| 67 | int len; |
| 68 | |
| 69 | assert( argc==1 ); |
| 70 | z = argv[0]; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 71 | if( z==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 72 | #ifdef SQLITE_UTF8 |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 73 | for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 74 | #else |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 75 | len = strlen(z); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 76 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 77 | sqlite_set_result_int(context, len); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | ** Implementation of the abs() function |
| 82 | */ |
| 83 | static void absFunc(sqlite_func *context, int argc, const char **argv){ |
| 84 | const char *z; |
| 85 | assert( argc==1 ); |
| 86 | z = argv[0]; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 87 | if( z==0 ) return; |
| 88 | if( z[0]=='-' && isdigit(z[1]) ) z++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 89 | sqlite_set_result_string(context, z, -1); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | ** Implementation of the substr() function |
| 94 | */ |
| 95 | static void substrFunc(sqlite_func *context, int argc, const char **argv){ |
| 96 | const char *z; |
| 97 | #ifdef SQLITE_UTF8 |
| 98 | const char *z2; |
| 99 | int i; |
| 100 | #endif |
| 101 | int p1, p2, len; |
| 102 | assert( argc==3 ); |
| 103 | z = argv[0]; |
| 104 | if( z==0 ) return; |
| 105 | p1 = atoi(argv[1]?argv[1]:0); |
| 106 | p2 = atoi(argv[2]?argv[2]:0); |
| 107 | #ifdef SQLITE_UTF8 |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 108 | for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 109 | #else |
| 110 | len = strlen(z); |
| 111 | #endif |
| 112 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 113 | p1 += len; |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 114 | if( p1<0 ){ |
| 115 | p2 += p1; |
| 116 | p1 = 0; |
| 117 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 118 | }else if( p1>0 ){ |
| 119 | p1--; |
| 120 | } |
| 121 | if( p1+p2>len ){ |
| 122 | p2 = len-p1; |
| 123 | } |
| 124 | #ifdef SQLITE_UTF8 |
| 125 | for(i=0; i<p1; i++){ |
| 126 | assert( z[i] ); |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 127 | if( (z[i]&0xc0)==0x80 ) p1++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 128 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 129 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 130 | for(; i<p1+p2; i++){ |
| 131 | assert( z[i] ); |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 132 | if( (z[i]&0xc0)==0x80 ) p2++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 133 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 134 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 135 | #endif |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 136 | if( p2<0 ) p2 = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 137 | sqlite_set_result_string(context, &z[p1], p2); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | ** Implementation of the round() function |
| 142 | */ |
| 143 | static void roundFunc(sqlite_func *context, int argc, const char **argv){ |
| 144 | int n; |
| 145 | double r; |
| 146 | char zBuf[100]; |
| 147 | assert( argc==1 || argc==2 ); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 148 | if( argv[0]==0 || (argc==2 && argv[1]==0) ) return; |
| 149 | n = argc==2 ? atoi(argv[1]) : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 150 | if( n>30 ) n = 30; |
| 151 | if( n<0 ) n = 0; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 152 | r = atof(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 153 | sprintf(zBuf,"%.*f",n,r); |
| 154 | sqlite_set_result_string(context, zBuf, -1); |
| 155 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | ** Implementation of the upper() and lower() SQL functions. |
| 159 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 160 | static void upperFunc(sqlite_func *context, int argc, const char **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 161 | char *z; |
| 162 | int i; |
| 163 | if( argc<1 || argv[0]==0 ) return; |
| 164 | z = sqlite_set_result_string(context, argv[0], -1); |
| 165 | if( z==0 ) return; |
| 166 | for(i=0; z[i]; i++){ |
| 167 | if( islower(z[i]) ) z[i] = toupper(z[i]); |
| 168 | } |
| 169 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 170 | static void lowerFunc(sqlite_func *context, int argc, const char **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 171 | char *z; |
| 172 | int i; |
| 173 | if( argc<1 || argv[0]==0 ) return; |
| 174 | z = sqlite_set_result_string(context, argv[0], -1); |
| 175 | if( z==0 ) return; |
| 176 | for(i=0; z[i]; i++){ |
| 177 | if( isupper(z[i]) ) z[i] = tolower(z[i]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 182 | ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. |
| 183 | ** All three do the same thing. They return the first argument |
| 184 | ** non-NULL argument. |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 185 | */ |
| 186 | static void ifnullFunc(sqlite_func *context, int argc, const char **argv){ |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 187 | int i; |
| 188 | for(i=0; i<argc; i++){ |
| 189 | if( argv[i] ){ |
| 190 | sqlite_set_result_string(context, argv[i], -1); |
| 191 | break; |
| 192 | } |
| 193 | } |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 197 | ** Implementation of random(). Return a random integer. |
| 198 | */ |
| 199 | static void randomFunc(sqlite_func *context, int argc, const char **argv){ |
| 200 | sqlite_set_result_int(context, sqliteRandomInteger()); |
| 201 | } |
| 202 | |
| 203 | /* |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 204 | ** Implementation of the last_insert_rowid() SQL function. The return |
| 205 | ** value is the same as the sqlite_last_insert_rowid() API function. |
| 206 | */ |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 207 | static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){ |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 208 | sqlite *db = sqlite_user_data(context); |
| 209 | sqlite_set_result_int(context, sqlite_last_insert_rowid(db)); |
| 210 | } |
| 211 | |
| 212 | /* |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 213 | ** Implementation of the like() SQL function. This function implements |
| 214 | ** the build-in LIKE operator. The first argument to the function is the |
| 215 | ** string and the second argument is the pattern. So, the SQL statements: |
| 216 | ** |
| 217 | ** A LIKE B |
| 218 | ** |
| 219 | ** is implemented as like(A,B). |
| 220 | */ |
| 221 | static void likeFunc(sqlite_func *context, int arg, const char **argv){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 222 | if( argv[0]==0 || argv[1]==0 ) return; |
| 223 | sqlite_set_result_int(context, sqliteLikeCompare(argv[0], argv[1])); |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | ** Implementation of the glob() SQL function. This function implements |
| 228 | ** the build-in GLOB operator. The first argument to the function is the |
| 229 | ** string and the second argument is the pattern. So, the SQL statements: |
| 230 | ** |
| 231 | ** A GLOB B |
| 232 | ** |
| 233 | ** is implemented as glob(A,B). |
| 234 | */ |
| 235 | static void globFunc(sqlite_func *context, int arg, const char **argv){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 236 | if( argv[0]==0 || argv[1]==0 ) return; |
| 237 | sqlite_set_result_int(context, sqliteGlobCompare(argv[0], argv[1])); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 242 | ** argument if the arguments are different. The result is NULL if the |
| 243 | ** arguments are equal to each other. |
| 244 | */ |
| 245 | static void nullifFunc(sqlite_func *context, int argc, const char **argv){ |
| 246 | if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){ |
| 247 | sqlite_set_result_string(context, argv[0], -1); |
| 248 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 249 | } |
| 250 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 251 | /* |
| 252 | ** Implementation of the VERSION(*) function. The result is the version |
| 253 | ** of the SQLite library that is running. |
| 254 | */ |
| 255 | static void versionFunc(sqlite_func *context, int argc, const char **argv){ |
| 256 | sqlite_set_result_string(context, sqlite_version, -1); |
| 257 | } |
| 258 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 259 | #ifdef SQLITE_SOUNDEX |
| 260 | /* |
| 261 | ** Compute the soundex encoding of a word. |
| 262 | */ |
| 263 | static void soundexFunc(sqlite_func *context, int argc, const char **argv){ |
| 264 | char zResult[8]; |
| 265 | const char *zIn; |
| 266 | int i, j; |
| 267 | static const unsigned char iCode[] = { |
| 268 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 269 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 270 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 273 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 274 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 275 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 276 | }; |
| 277 | assert( argc==1 ); |
| 278 | zIn = argv[0]; |
| 279 | for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} |
| 280 | if( zIn[i] ){ |
| 281 | zResult[0] = toupper(zIn[i]); |
| 282 | for(j=1; j<4 && zIn[i]; i++){ |
| 283 | int code = iCode[zIn[i]&0x7f]; |
| 284 | if( code>0 ){ |
| 285 | zResult[j++] = code + '0'; |
| 286 | } |
| 287 | } |
| 288 | while( j<4 ){ |
| 289 | zResult[j++] = '0'; |
| 290 | } |
| 291 | zResult[j] = 0; |
| 292 | sqlite_set_result_string(context, zResult, 4); |
| 293 | }else{ |
drh | 937dd84 | 2003-06-28 16:20:22 +0000 | [diff] [blame] | 294 | sqlite_set_result_string(context, "?000", 4); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | #endif |
| 298 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 299 | #ifdef SQLITE_TEST |
| 300 | /* |
| 301 | ** This function generates a string of random characters. Used for |
| 302 | ** generating test data. |
| 303 | */ |
| 304 | static void randStr(sqlite_func *context, int argc, const char **argv){ |
| 305 | static const char zSrc[] = |
| 306 | "abcdefghijklmnopqrstuvwxyz" |
| 307 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 308 | "0123456789" |
| 309 | ".-!,:*^+=_|?/<> "; |
| 310 | int iMin, iMax, n, r, i; |
| 311 | char zBuf[1000]; |
| 312 | if( argc>=1 ){ |
| 313 | iMin = atoi(argv[0]); |
| 314 | if( iMin<0 ) iMin = 0; |
| 315 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 316 | }else{ |
| 317 | iMin = 1; |
| 318 | } |
| 319 | if( argc>=2 ){ |
| 320 | iMax = atoi(argv[1]); |
| 321 | if( iMax<iMin ) iMax = iMin; |
| 322 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf); |
| 323 | }else{ |
| 324 | iMax = 50; |
| 325 | } |
| 326 | n = iMin; |
| 327 | if( iMax>iMin ){ |
drh | 3958196 | 2003-05-13 01:52:31 +0000 | [diff] [blame] | 328 | r = sqliteRandomInteger() & 0x7fffffff; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 329 | n += r%(iMax + 1 - iMin); |
| 330 | } |
| 331 | r = 0; |
| 332 | for(i=0; i<n; i++){ |
| 333 | r = (r + sqliteRandomByte())% (sizeof(zSrc)-1); |
| 334 | zBuf[i] = zSrc[r]; |
| 335 | } |
| 336 | zBuf[n] = 0; |
| 337 | sqlite_set_result_string(context, zBuf, n); |
| 338 | } |
| 339 | #endif |
| 340 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 341 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 342 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 343 | ** sum() or avg() aggregate computation. |
| 344 | */ |
| 345 | typedef struct SumCtx SumCtx; |
| 346 | struct SumCtx { |
| 347 | double sum; /* Sum of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 348 | int cnt; /* Number of elements summed */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | /* |
| 352 | ** Routines used to compute the sum or average. |
| 353 | */ |
| 354 | static void sumStep(sqlite_func *context, int argc, const char **argv){ |
| 355 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 356 | if( argc<1 ) return; |
| 357 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 358 | if( p && argv[0] ){ |
| 359 | p->sum += atof(argv[0]); |
| 360 | p->cnt++; |
| 361 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 362 | } |
| 363 | static void sumFinalize(sqlite_func *context){ |
| 364 | SumCtx *p; |
| 365 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 366 | sqlite_set_result_double(context, p ? p->sum : 0.0); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 367 | } |
| 368 | static void avgFinalize(sqlite_func *context){ |
| 369 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 370 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 371 | if( p && p->cnt>0 ){ |
| 372 | sqlite_set_result_double(context, p->sum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | ** An instance of the following structure holds the context of a |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 378 | ** variance or standard deviation computation. |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 379 | */ |
| 380 | typedef struct StdDevCtx StdDevCtx; |
| 381 | struct StdDevCtx { |
| 382 | double sum; /* Sum of terms */ |
| 383 | double sum2; /* Sum of the squares of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 384 | int cnt; /* Number of terms counted */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 387 | #if 0 /* Omit because math library is required */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 388 | /* |
| 389 | ** Routines used to compute the standard deviation as an aggregate. |
| 390 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 391 | static void stdDevStep(sqlite_func *context, int argc, const char **argv){ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 392 | StdDevCtx *p; |
| 393 | double x; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 394 | if( argc<1 ) return; |
| 395 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 396 | if( p && argv[0] ){ |
| 397 | x = atof(argv[0]); |
| 398 | p->sum += x; |
| 399 | p->sum2 += x*x; |
| 400 | p->cnt++; |
| 401 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 402 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 403 | static void stdDevFinalize(sqlite_func *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 404 | double rN = sqlite_aggregate_count(context); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 405 | StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 406 | if( p && p->cnt>1 ){ |
| 407 | double rCnt = cnt; |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 408 | sqlite_set_result_double(context, |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 409 | sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0))); |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 410 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 411 | } |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 412 | #endif |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 413 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 414 | /* |
| 415 | ** The following structure keeps track of state information for the |
| 416 | ** count() aggregate function. |
| 417 | */ |
| 418 | typedef struct CountCtx CountCtx; |
| 419 | struct CountCtx { |
| 420 | int n; |
| 421 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 422 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 423 | /* |
| 424 | ** Routines to implement the count() aggregate function. |
| 425 | */ |
| 426 | static void countStep(sqlite_func *context, int argc, const char **argv){ |
| 427 | CountCtx *p; |
| 428 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 429 | if( (argc==0 || argv[0]) && p ){ |
| 430 | p->n++; |
| 431 | } |
| 432 | } |
| 433 | static void countFinalize(sqlite_func *context){ |
| 434 | CountCtx *p; |
| 435 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 436 | sqlite_set_result_int(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* |
| 440 | ** This function tracks state information for the min() and max() |
| 441 | ** aggregate functions. |
| 442 | */ |
| 443 | typedef struct MinMaxCtx MinMaxCtx; |
| 444 | struct MinMaxCtx { |
| 445 | char *z; /* The best so far */ |
| 446 | char zBuf[28]; /* Space that can be used for storage */ |
| 447 | }; |
| 448 | |
| 449 | /* |
| 450 | ** Routines to implement min() and max() aggregate functions. |
| 451 | */ |
| 452 | static void minStep(sqlite_func *context, int argc, const char **argv){ |
| 453 | MinMaxCtx *p; |
| 454 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 455 | if( p==0 || argc<1 || argv[0]==0 ) return; |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 456 | if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 457 | int len; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 458 | if( p->z && p->z!=p->zBuf ){ |
| 459 | sqliteFree(p->z); |
| 460 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 461 | len = strlen(argv[0]); |
| 462 | if( len < sizeof(p->zBuf) ){ |
| 463 | p->z = p->zBuf; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 464 | }else{ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 465 | p->z = sqliteMalloc( len+1 ); |
| 466 | if( p->z==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 467 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 468 | strcpy(p->z, argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | static void maxStep(sqlite_func *context, int argc, const char **argv){ |
| 472 | MinMaxCtx *p; |
| 473 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 474 | if( p==0 || argc<1 || argv[0]==0 ) return; |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 475 | if( p->z==0 || sqliteCompare(argv[0],p->z)>0 ){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 476 | int len; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 477 | if( p->z && p->z!=p->zBuf ){ |
| 478 | sqliteFree(p->z); |
| 479 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 480 | len = strlen(argv[0]); |
| 481 | if( len < sizeof(p->zBuf) ){ |
| 482 | p->z = p->zBuf; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 483 | }else{ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 484 | p->z = sqliteMalloc( len+1 ); |
| 485 | if( p->z==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 486 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 487 | strcpy(p->z, argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | static void minMaxFinalize(sqlite_func *context){ |
| 491 | MinMaxCtx *p; |
| 492 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 493 | if( p && p->z ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 494 | sqlite_set_result_string(context, p->z, strlen(p->z)); |
| 495 | } |
| 496 | if( p && p->z && p->z!=p->zBuf ){ |
| 497 | sqliteFree(p->z); |
| 498 | } |
| 499 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 500 | |
drh | 771d8c3 | 2003-08-09 21:32:28 +0000 | [diff] [blame^] | 501 | /**************************************************************************** |
| 502 | ** Time and date functions. |
| 503 | ** |
| 504 | ** SQLite processes all times and dates as Julian Day numbers. The |
| 505 | ** dates and times are stored as the number of days since noon |
| 506 | ** in Greenwich on January 01, 4713 B.C. (a.k.a -4713-01-01 12:00:00) |
| 507 | ** This implement requires years to be expressed as a 4-digit number |
| 508 | ** which means that only dates between 0000-01-01 and 9999-12-31 can |
| 509 | ** be represented, even though julian day numbers allow a much wider |
| 510 | ** range of dates. |
| 511 | ** |
| 512 | ** The Gregorian calendar system is used for all dates and times, |
| 513 | ** even those that predate the Gregorian calendar. Historians often |
| 514 | ** use the Julian calendar for dates prior to 1582-10-15 and for some |
| 515 | ** dates afterwards, depending on locale. Beware of this difference. |
| 516 | ** |
| 517 | ** The conversion algorithms are implemented based on descriptions |
| 518 | ** in the following text: |
| 519 | ** |
| 520 | ** Jean Meeus |
| 521 | ** Astronomical Algorithms, 2nd Edition, 1998 |
| 522 | ** ISBM 0-943396-61-1 |
| 523 | ** Willmann-Bell, Inc |
| 524 | ** Richmond, Virginia (USA) |
| 525 | */ |
| 526 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 527 | |
| 528 | /* |
| 529 | ** Convert N digits from zDate into an integer. Return |
| 530 | ** -1 if zDate does not begin with N digits. |
| 531 | */ |
| 532 | static int getDigits(const char *zDate, int N){ |
| 533 | int val = 0; |
| 534 | while( N-- ){ |
| 535 | if( !isdigit(*zDate) ) return -1; |
| 536 | val = val*10 + *zDate - '0'; |
| 537 | zDate++; |
| 538 | } |
| 539 | return val; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | ** Parse dates of the form HH:MM:SS or HH:MM. Store the |
| 544 | ** result (in days) in *prJD. |
| 545 | ** |
| 546 | ** Return 1 if there is a parsing error and 0 on success. |
| 547 | */ |
| 548 | static int parseHhMmSs(const char *zDate, double *prJD){ |
| 549 | int h, m, s; |
| 550 | h = getDigits(zDate, 2); |
| 551 | if( h<0 || zDate[2]!=':' ) return 1; |
| 552 | zDate += 3; |
| 553 | m = getDigits(zDate, 2); |
| 554 | if( m<0 || m>59 ) return 1; |
| 555 | zDate += 2; |
| 556 | if( *zDate==':' ){ |
| 557 | s = getDigits(&zDate[1], 2); |
| 558 | if( s<0 || s>59 ) return 1; |
| 559 | zDate += 3; |
| 560 | }else{ |
| 561 | s = 0; |
| 562 | } |
| 563 | while( isspace(*zDate) ){ zDate++; } |
| 564 | *prJD = (h*3600.0 + m*60.0 + s)/86400.0; |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | ** Parse dates of the form |
| 570 | ** |
| 571 | ** YYYY-MM-DD HH:MM:SS |
| 572 | ** YYYY-MM-DD HH:MM |
| 573 | ** YYYY-MM-DD |
| 574 | ** |
| 575 | ** Write the result as a julian day number in *prJD. Return 0 |
| 576 | ** on success and 1 if the input string is not a well-formed |
| 577 | ** date. |
| 578 | */ |
| 579 | static int parseYyyyMmDd(const char *zDate, double *prJD){ |
| 580 | int Y, M, D; |
| 581 | double rTime; |
| 582 | int A, B, X1, X2; |
| 583 | |
| 584 | Y = getDigits(zDate, 4); |
| 585 | if( Y<0 || zDate[4]!='-' ) return 1; |
| 586 | zDate += 5; |
| 587 | M = getDigits(zDate, 2); |
| 588 | if( M<=0 || M>12 || zDate[2]!='-' ) return 1; |
| 589 | zDate += 3; |
| 590 | D = getDigits(zDate, 2); |
| 591 | if( D<=0 || D>31 ) return 1; |
| 592 | zDate += 2; |
| 593 | while( isspace(*zDate) ){ zDate++; } |
| 594 | if( isdigit(*zDate) ){ |
| 595 | if( parseHhMmSs(zDate, &rTime) ) return 1; |
| 596 | }else if( *zDate==0 ){ |
| 597 | rTime = 0.0; |
| 598 | }else{ |
| 599 | return 1; |
| 600 | } |
| 601 | |
| 602 | /* The year, month, and day are now stored in Y, M, and D. Convert |
| 603 | ** these into the Julian Day number. See Meeus page 61. |
| 604 | */ |
| 605 | if( M<=2 ){ |
| 606 | Y--; |
| 607 | M += 12; |
| 608 | } |
| 609 | A = Y/100; |
| 610 | B = 2 - A + (A/4); |
| 611 | X1 = 365.25*(Y+4716); |
| 612 | X2 = 30.6001*(M+1); |
| 613 | *prJD = X1 + X2 + D + B - 1524.5 + rTime; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | ** Attempt to parse the given string into a Julian Day Number. Return |
| 619 | ** the number of errors. |
| 620 | ** |
| 621 | ** The following are acceptable forms for the input string: |
| 622 | ** |
| 623 | ** YYYY-MM-DD |
| 624 | ** YYYY-MM-DD HH:MM |
| 625 | ** YYYY-MM-DD HH:MM:SS |
| 626 | ** HH:MM |
| 627 | ** HH:MM:SS |
| 628 | ** DDDD.DD |
| 629 | ** now |
| 630 | */ |
| 631 | static int parseDateOrTime(const char *zDate, double *prJD){ |
| 632 | int i; |
| 633 | for(i=0; isdigit(zDate[i]); i++){} |
| 634 | if( i==4 && zDate[i]=='-' ){ |
| 635 | return parseYyyyMmDd(zDate, prJD); |
| 636 | }else if( i==2 && zDate[i]==':' ){ |
| 637 | return parseHhMmSs(zDate, prJD); |
| 638 | }else if( i==0 && sqliteStrICmp(zDate,"now")==0 ){ |
| 639 | return sqliteOsCurrentTime(prJD); |
| 640 | }else if( sqliteIsNumber(zDate) ){ |
| 641 | *prJD = atof(zDate); |
| 642 | return 0; |
| 643 | } |
| 644 | return 1; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | ** Break up a julian day number into year, month, day, and seconds. |
| 649 | ** This function assume the Gregorian calendar - even for dates prior |
| 650 | ** to the invention of the Gregorian calendar in 1582. |
| 651 | ** |
| 652 | ** See Meeus page 63. |
| 653 | */ |
| 654 | static void decomposeDate(double JD, int *pY, int *pM, int *pD, int *pS){ |
| 655 | int Z, A, B, C, D, E, X1; |
| 656 | Z = JD + 0.5; |
| 657 | A = (Z - 1867216.25)/36524.25; |
| 658 | A = Z + 1 + A - (A/4); |
| 659 | B = A + 1524; |
| 660 | C = (B - 122.1)/365.25; |
| 661 | D = 365.25*C; |
| 662 | E = (B-D)/30.6001; |
| 663 | X1 = 30.6001*E; |
| 664 | *pD = B - D - X1; |
| 665 | *pM = E<14 ? E-1 : E-13; |
| 666 | *pY = *pD>2 ? C - 4716 : C - 4715; |
| 667 | *pS = (JD + 0.5 - Z)*86400.0; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | ** Check to see that all arguments are valid date strings. If any is |
| 672 | ** not a valid date string, return 0. If all are valid, return 1. |
| 673 | ** Write into *prJD the sum of the julian day numbers for all date |
| 674 | ** strings. |
| 675 | */ |
| 676 | static int isDate( |
| 677 | sqlite_func *context, |
| 678 | int argc, |
| 679 | const char **argv, |
| 680 | double *prJD |
| 681 | ){ |
| 682 | double r; |
| 683 | int i; |
| 684 | *prJD = 0.0; |
| 685 | for(i=0; i<argc; i++){ |
| 686 | if( argv[i]==0 ) return 0; |
| 687 | if( parseDateOrTime(argv[i], &r) ) return 0; |
| 688 | *prJD += r; |
| 689 | } |
| 690 | return 1; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | ** The following routines implement the various date and time functions |
| 695 | ** of SQLite. |
| 696 | */ |
| 697 | static void juliandayFunc(sqlite_func *context, int argc, const char **argv){ |
| 698 | double JD; |
| 699 | if( isDate(context, argc, argv, &JD) ){ |
| 700 | sqlite_set_result_double(context, JD); |
| 701 | } |
| 702 | } |
| 703 | static void timestampFunc(sqlite_func *context, int argc, const char **argv){ |
| 704 | double JD; |
| 705 | if( isDate(context, argc, argv, &JD) ){ |
| 706 | int Y, M, D, h, m, s; |
| 707 | char zBuf[100]; |
| 708 | decomposeDate(JD, &Y, &M, &D, &s); |
| 709 | h = s/3600; |
| 710 | s -= h*3600; |
| 711 | m = s/60; |
| 712 | s -= m*60; |
| 713 | sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d", Y, M, D, h, m, s); |
| 714 | sqlite_set_result_string(context, zBuf, -1); |
| 715 | } |
| 716 | } |
| 717 | static void timeFunc(sqlite_func *context, int argc, const char **argv){ |
| 718 | double JD; |
| 719 | if( isDate(context, argc, argv, &JD) ){ |
| 720 | int Y, M, D, h, m, s; |
| 721 | char zBuf[100]; |
| 722 | decomposeDate(JD, &Y, &M, &D, &s); |
| 723 | h = s/3600; |
| 724 | s -= h*3600; |
| 725 | m = s/60; |
| 726 | s -= m*60; |
| 727 | sprintf(zBuf, "%02d:%02d:%02d", h, m, s); |
| 728 | sqlite_set_result_string(context, zBuf, -1); |
| 729 | } |
| 730 | } |
| 731 | static void dateFunc(sqlite_func *context, int argc, const char **argv){ |
| 732 | double JD; |
| 733 | if( isDate(context, argc, argv, &JD) ){ |
| 734 | int Y, M, D, s; |
| 735 | char zBuf[100]; |
| 736 | decomposeDate(JD, &Y, &M, &D, &s); |
| 737 | sprintf(zBuf, "%04d-%02d-%02d", Y, M, D); |
| 738 | sqlite_set_result_string(context, zBuf, -1); |
| 739 | } |
| 740 | } |
| 741 | static void yearFunc(sqlite_func *context, int argc, const char **argv){ |
| 742 | double JD; |
| 743 | if( isDate(context, argc, argv, &JD) ){ |
| 744 | int Y, M, D, s; |
| 745 | decomposeDate(JD, &Y, &M, &D, &s); |
| 746 | sqlite_set_result_int(context, Y); |
| 747 | } |
| 748 | } |
| 749 | static void monthFunc(sqlite_func *context, int argc, const char **argv){ |
| 750 | double JD; |
| 751 | if( isDate(context, argc, argv, &JD) ){ |
| 752 | int Y, M, D, s; |
| 753 | decomposeDate(JD, &Y, &M, &D, &s); |
| 754 | sqlite_set_result_int(context, M); |
| 755 | } |
| 756 | } |
| 757 | static void dayofweekFunc(sqlite_func *context, int argc, const char **argv){ |
| 758 | double JD; |
| 759 | if( isDate(context, argc, argv, &JD) ){ |
| 760 | int Z = JD + 1.5; |
| 761 | sqlite_set_result_int(context, Z % 7); |
| 762 | } |
| 763 | } |
| 764 | static void dayofmonthFunc(sqlite_func *context, int argc, const char **argv){ |
| 765 | double JD; |
| 766 | if( isDate(context, argc, argv, &JD) ){ |
| 767 | int Y, M, D, s; |
| 768 | decomposeDate(JD, &Y, &M, &D, &s); |
| 769 | sqlite_set_result_int(context, D); |
| 770 | } |
| 771 | } |
| 772 | static void secondFunc(sqlite_func *context, int argc, const char **argv){ |
| 773 | double JD; |
| 774 | if( isDate(context, argc, argv, &JD) ){ |
| 775 | int Y, M, D, h, m, s; |
| 776 | decomposeDate(JD, &Y, &M, &D, &s); |
| 777 | h = s/3600; |
| 778 | s -= h*3600; |
| 779 | m = s/60; |
| 780 | s -= m*60; |
| 781 | sqlite_set_result_int(context, s); |
| 782 | } |
| 783 | } |
| 784 | static void minuteFunc(sqlite_func *context, int argc, const char **argv){ |
| 785 | double JD; |
| 786 | if( isDate(context, argc, argv, &JD) ){ |
| 787 | int Y, M, D, h, m, s; |
| 788 | decomposeDate(JD, &Y, &M, &D, &s); |
| 789 | h = s/3600; |
| 790 | s -= h*3600; |
| 791 | m = s/60; |
| 792 | sqlite_set_result_int(context, m); |
| 793 | } |
| 794 | } |
| 795 | static void hourFunc(sqlite_func *context, int argc, const char **argv){ |
| 796 | double JD; |
| 797 | if( isDate(context, argc, argv, &JD) ){ |
| 798 | int Y, M, D, h, s; |
| 799 | decomposeDate(JD, &Y, &M, &D, &s); |
| 800 | h = s/3600; |
| 801 | sqlite_set_result_int(context, h); |
| 802 | } |
| 803 | } |
| 804 | #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ |
| 805 | /***************************************************************************/ |
| 806 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 807 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 808 | ** This function registered all of the above C functions as SQL |
| 809 | ** functions. This should be the only routine in this file with |
| 810 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 811 | */ |
drh | 28f4b68 | 2002-06-09 10:14:18 +0000 | [diff] [blame] | 812 | void sqliteRegisterBuiltinFunctions(sqlite *db){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 813 | static struct { |
| 814 | char *zName; |
| 815 | int nArg; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 816 | int dataType; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 817 | void (*xFunc)(sqlite_func*,int,const char**); |
| 818 | } aFuncs[] = { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 819 | { "min", -1, SQLITE_ARGS, minFunc }, |
| 820 | { "min", 0, 0, 0 }, |
| 821 | { "max", -1, SQLITE_ARGS, maxFunc }, |
| 822 | { "max", 0, 0, 0 }, |
| 823 | { "length", 1, SQLITE_NUMERIC, lengthFunc }, |
| 824 | { "substr", 3, SQLITE_TEXT, substrFunc }, |
| 825 | { "abs", 1, SQLITE_NUMERIC, absFunc }, |
| 826 | { "round", 1, SQLITE_NUMERIC, roundFunc }, |
| 827 | { "round", 2, SQLITE_NUMERIC, roundFunc }, |
| 828 | { "upper", 1, SQLITE_TEXT, upperFunc }, |
| 829 | { "lower", 1, SQLITE_TEXT, lowerFunc }, |
| 830 | { "coalesce", -1, SQLITE_ARGS, ifnullFunc }, |
| 831 | { "coalesce", 0, 0, 0 }, |
| 832 | { "coalesce", 1, 0, 0 }, |
| 833 | { "ifnull", 2, SQLITE_ARGS, ifnullFunc }, |
| 834 | { "random", -1, SQLITE_NUMERIC, randomFunc }, |
| 835 | { "like", 2, SQLITE_NUMERIC, likeFunc }, |
| 836 | { "glob", 2, SQLITE_NUMERIC, globFunc }, |
| 837 | { "nullif", 2, SQLITE_ARGS, nullifFunc }, |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 838 | { "sqlite_version",0,SQLITE_TEXT, versionFunc}, |
drh | 771d8c3 | 2003-08-09 21:32:28 +0000 | [diff] [blame^] | 839 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 840 | { "julianday", -1, SQLITE_NUMERIC, juliandayFunc }, |
| 841 | { "timestamp", -1, SQLITE_TEXT, timestampFunc }, |
| 842 | { "time", -1, SQLITE_TEXT, timeFunc }, |
| 843 | { "date", -1, SQLITE_TEXT, dateFunc }, |
| 844 | { "year", -1, SQLITE_NUMERIC, yearFunc }, |
| 845 | { "month", -1, SQLITE_NUMERIC, monthFunc }, |
| 846 | { "dayofmonth",-1, SQLITE_NUMERIC, dayofmonthFunc }, |
| 847 | { "dayofweek", -1, SQLITE_NUMERIC, dayofweekFunc }, |
| 848 | { "hour", -1, SQLITE_NUMERIC, hourFunc }, |
| 849 | { "minute", -1, SQLITE_NUMERIC, minuteFunc }, |
| 850 | { "second", -1, SQLITE_NUMERIC, secondFunc }, |
| 851 | #endif |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 852 | #ifdef SQLITE_SOUNDEX |
| 853 | { "soundex", 1, SQLITE_TEXT, soundexFunc}, |
| 854 | #endif |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 855 | #ifdef SQLITE_TEST |
| 856 | { "randstr", 2, SQLITE_TEXT, randStr }, |
| 857 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 858 | }; |
| 859 | static struct { |
| 860 | char *zName; |
| 861 | int nArg; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 862 | int dataType; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 863 | void (*xStep)(sqlite_func*,int,const char**); |
| 864 | void (*xFinalize)(sqlite_func*); |
| 865 | } aAggs[] = { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 866 | { "min", 1, 0, minStep, minMaxFinalize }, |
| 867 | { "max", 1, 0, maxStep, minMaxFinalize }, |
| 868 | { "sum", 1, SQLITE_NUMERIC, sumStep, sumFinalize }, |
| 869 | { "avg", 1, SQLITE_NUMERIC, sumStep, avgFinalize }, |
| 870 | { "count", 0, SQLITE_NUMERIC, countStep, countFinalize }, |
| 871 | { "count", 1, SQLITE_NUMERIC, countStep, countFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 872 | #if 0 |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 873 | { "stddev", 1, SQLITE_NUMERIC, stdDevStep, stdDevFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 874 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 875 | }; |
| 876 | int i; |
| 877 | |
| 878 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
| 879 | sqlite_create_function(db, aFuncs[i].zName, |
| 880 | aFuncs[i].nArg, aFuncs[i].xFunc, 0); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 881 | if( aFuncs[i].xFunc ){ |
| 882 | sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType); |
| 883 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 884 | } |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 885 | sqlite_create_function(db, "last_insert_rowid", 0, |
| 886 | last_insert_rowid, db); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 887 | sqlite_function_type(db, "last_insert_rowid", SQLITE_NUMERIC); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 888 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
| 889 | sqlite_create_aggregate(db, aAggs[i].zName, |
| 890 | aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 891 | sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 892 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 893 | } |