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 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.40 2004/02/20 22:53:39 rdc 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 |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 125 | for(i=0; i<p1 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 126 | if( (z[i]&0xc0)==0x80 ) p1++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 127 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 128 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 129 | for(; i<p1+p2 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 130 | if( (z[i]&0xc0)==0x80 ) p2++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 131 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 132 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 133 | #endif |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 134 | if( p2<0 ) p2 = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 135 | sqlite_set_result_string(context, &z[p1], p2); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | ** Implementation of the round() function |
| 140 | */ |
| 141 | static void roundFunc(sqlite_func *context, int argc, const char **argv){ |
| 142 | int n; |
| 143 | double r; |
| 144 | char zBuf[100]; |
| 145 | assert( argc==1 || argc==2 ); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 146 | if( argv[0]==0 || (argc==2 && argv[1]==0) ) return; |
| 147 | n = argc==2 ? atoi(argv[1]) : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 148 | if( n>30 ) n = 30; |
| 149 | if( n<0 ) n = 0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 150 | r = sqliteAtoF(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 151 | sprintf(zBuf,"%.*f",n,r); |
| 152 | sqlite_set_result_string(context, zBuf, -1); |
| 153 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | ** Implementation of the upper() and lower() SQL functions. |
| 157 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 158 | static void upperFunc(sqlite_func *context, int argc, const char **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 159 | char *z; |
| 160 | int i; |
| 161 | if( argc<1 || argv[0]==0 ) return; |
| 162 | z = sqlite_set_result_string(context, argv[0], -1); |
| 163 | if( z==0 ) return; |
| 164 | for(i=0; z[i]; i++){ |
| 165 | if( islower(z[i]) ) z[i] = toupper(z[i]); |
| 166 | } |
| 167 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 168 | static void lowerFunc(sqlite_func *context, int argc, const char **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 169 | char *z; |
| 170 | int i; |
| 171 | if( argc<1 || argv[0]==0 ) return; |
| 172 | z = sqlite_set_result_string(context, argv[0], -1); |
| 173 | if( z==0 ) return; |
| 174 | for(i=0; z[i]; i++){ |
| 175 | if( isupper(z[i]) ) z[i] = tolower(z[i]); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 180 | ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. |
jplyon | b6c9e6e | 2004-01-19 04:53:24 +0000 | [diff] [blame] | 181 | ** All three do the same thing. They return the first non-NULL |
| 182 | ** argument. |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 183 | */ |
| 184 | static void ifnullFunc(sqlite_func *context, int argc, const char **argv){ |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 185 | int i; |
| 186 | for(i=0; i<argc; i++){ |
| 187 | if( argv[i] ){ |
| 188 | sqlite_set_result_string(context, argv[i], -1); |
| 189 | break; |
| 190 | } |
| 191 | } |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 195 | ** Implementation of random(). Return a random integer. |
| 196 | */ |
| 197 | static void randomFunc(sqlite_func *context, int argc, const char **argv){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 198 | int r; |
| 199 | sqliteRandomness(sizeof(r), &r); |
| 200 | sqlite_set_result_int(context, r); |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 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 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame^] | 212 | static void change_count(sqlite_func *context, int arg, const char **argv){ |
| 213 | sqlite *db = sqlite_user_data(context); |
| 214 | sqlite_set_result_int(context, sqlite_changes(db)); |
| 215 | } |
| 216 | static void last_statement_change_count(sqlite_func *context, int arg, |
| 217 | const char **argv){ |
| 218 | sqlite *db = sqlite_user_data(context); |
| 219 | sqlite_set_result_int(context, sqlite_last_statement_changes(db)); |
| 220 | } |
| 221 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 222 | /* |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 223 | ** Implementation of the like() SQL function. This function implements |
| 224 | ** the build-in LIKE operator. The first argument to the function is the |
| 225 | ** string and the second argument is the pattern. So, the SQL statements: |
| 226 | ** |
| 227 | ** A LIKE B |
| 228 | ** |
| 229 | ** is implemented as like(A,B). |
| 230 | */ |
| 231 | static void likeFunc(sqlite_func *context, int arg, const char **argv){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 232 | if( argv[0]==0 || argv[1]==0 ) return; |
drh | ec1bd0b | 2003-08-26 11:41:27 +0000 | [diff] [blame] | 233 | sqlite_set_result_int(context, |
| 234 | sqliteLikeCompare((const unsigned char*)argv[0], |
| 235 | (const unsigned char*)argv[1])); |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | ** Implementation of the glob() SQL function. This function implements |
| 240 | ** the build-in GLOB operator. The first argument to the function is the |
| 241 | ** string and the second argument is the pattern. So, the SQL statements: |
| 242 | ** |
| 243 | ** A GLOB B |
| 244 | ** |
| 245 | ** is implemented as glob(A,B). |
| 246 | */ |
| 247 | static void globFunc(sqlite_func *context, int arg, const char **argv){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 248 | if( argv[0]==0 || argv[1]==0 ) return; |
drh | ec1bd0b | 2003-08-26 11:41:27 +0000 | [diff] [blame] | 249 | sqlite_set_result_int(context, |
| 250 | sqliteGlobCompare((const unsigned char*)argv[0], |
| 251 | (const unsigned char*)argv[1])); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 256 | ** argument if the arguments are different. The result is NULL if the |
| 257 | ** arguments are equal to each other. |
| 258 | */ |
| 259 | static void nullifFunc(sqlite_func *context, int argc, const char **argv){ |
| 260 | if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){ |
| 261 | sqlite_set_result_string(context, argv[0], -1); |
| 262 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 263 | } |
| 264 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 265 | /* |
| 266 | ** Implementation of the VERSION(*) function. The result is the version |
| 267 | ** of the SQLite library that is running. |
| 268 | */ |
| 269 | static void versionFunc(sqlite_func *context, int argc, const char **argv){ |
| 270 | sqlite_set_result_string(context, sqlite_version, -1); |
| 271 | } |
| 272 | |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 273 | /* |
| 274 | ** EXPERIMENTAL - This is not an official function. The interface may |
| 275 | ** change. This function may disappear. Do not write code that depends |
| 276 | ** on this function. |
| 277 | ** |
| 278 | ** Implementation of the QUOTE() function. This function takes a single |
| 279 | ** argument. If the argument is numeric, the return value is the same as |
| 280 | ** the argument. If the argument is NULL, the return value is the string |
| 281 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 282 | ** single-quote escapes. |
| 283 | */ |
| 284 | static void quoteFunc(sqlite_func *context, int argc, const char **argv){ |
| 285 | if( argc<1 ) return; |
| 286 | if( argv[0]==0 ){ |
| 287 | sqlite_set_result_string(context, "NULL", 4); |
| 288 | }else if( sqliteIsNumber(argv[0]) ){ |
| 289 | sqlite_set_result_string(context, argv[0], -1); |
| 290 | }else{ |
| 291 | int i,j,n; |
| 292 | char *z; |
| 293 | for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; } |
| 294 | z = sqliteMalloc( i+n+3 ); |
| 295 | if( z==0 ) return; |
| 296 | z[0] = '\''; |
| 297 | for(i=0, j=1; argv[0][i]; i++){ |
| 298 | z[j++] = argv[0][i]; |
| 299 | if( argv[0][i]=='\'' ){ |
| 300 | z[j++] = '\''; |
| 301 | } |
| 302 | } |
| 303 | z[j++] = '\''; |
| 304 | z[j] = 0; |
| 305 | sqlite_set_result_string(context, z, j); |
| 306 | sqliteFree(z); |
| 307 | } |
| 308 | } |
| 309 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 310 | #ifdef SQLITE_SOUNDEX |
| 311 | /* |
| 312 | ** Compute the soundex encoding of a word. |
| 313 | */ |
| 314 | static void soundexFunc(sqlite_func *context, int argc, const char **argv){ |
| 315 | char zResult[8]; |
| 316 | const char *zIn; |
| 317 | int i, j; |
| 318 | static const unsigned char iCode[] = { |
| 319 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 320 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 321 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 322 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 323 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 324 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 325 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 326 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 327 | }; |
| 328 | assert( argc==1 ); |
| 329 | zIn = argv[0]; |
| 330 | for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} |
| 331 | if( zIn[i] ){ |
| 332 | zResult[0] = toupper(zIn[i]); |
| 333 | for(j=1; j<4 && zIn[i]; i++){ |
| 334 | int code = iCode[zIn[i]&0x7f]; |
| 335 | if( code>0 ){ |
| 336 | zResult[j++] = code + '0'; |
| 337 | } |
| 338 | } |
| 339 | while( j<4 ){ |
| 340 | zResult[j++] = '0'; |
| 341 | } |
| 342 | zResult[j] = 0; |
| 343 | sqlite_set_result_string(context, zResult, 4); |
| 344 | }else{ |
drh | 937dd84 | 2003-06-28 16:20:22 +0000 | [diff] [blame] | 345 | sqlite_set_result_string(context, "?000", 4); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | #endif |
| 349 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 350 | #ifdef SQLITE_TEST |
| 351 | /* |
| 352 | ** This function generates a string of random characters. Used for |
| 353 | ** generating test data. |
| 354 | */ |
| 355 | static void randStr(sqlite_func *context, int argc, const char **argv){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 356 | static const unsigned char zSrc[] = |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 357 | "abcdefghijklmnopqrstuvwxyz" |
| 358 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 359 | "0123456789" |
| 360 | ".-!,:*^+=_|?/<> "; |
| 361 | int iMin, iMax, n, r, i; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 362 | unsigned char zBuf[1000]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 363 | if( argc>=1 ){ |
| 364 | iMin = atoi(argv[0]); |
| 365 | if( iMin<0 ) iMin = 0; |
| 366 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 367 | }else{ |
| 368 | iMin = 1; |
| 369 | } |
| 370 | if( argc>=2 ){ |
| 371 | iMax = atoi(argv[1]); |
| 372 | if( iMax<iMin ) iMax = iMin; |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 373 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 374 | }else{ |
| 375 | iMax = 50; |
| 376 | } |
| 377 | n = iMin; |
| 378 | if( iMax>iMin ){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 379 | sqliteRandomness(sizeof(r), &r); |
| 380 | r &= 0x7fffffff; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 381 | n += r%(iMax + 1 - iMin); |
| 382 | } |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 383 | assert( n<sizeof(zBuf) ); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 384 | sqliteRandomness(n, zBuf); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 385 | for(i=0; i<n; i++){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 386 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 387 | } |
| 388 | zBuf[n] = 0; |
| 389 | sqlite_set_result_string(context, zBuf, n); |
| 390 | } |
| 391 | #endif |
| 392 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 393 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 394 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 395 | ** sum() or avg() aggregate computation. |
| 396 | */ |
| 397 | typedef struct SumCtx SumCtx; |
| 398 | struct SumCtx { |
| 399 | double sum; /* Sum of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 400 | int cnt; /* Number of elements summed */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | /* |
| 404 | ** Routines used to compute the sum or average. |
| 405 | */ |
| 406 | static void sumStep(sqlite_func *context, int argc, const char **argv){ |
| 407 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 408 | if( argc<1 ) return; |
| 409 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 410 | if( p && argv[0] ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 411 | p->sum += sqliteAtoF(argv[0]); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 412 | p->cnt++; |
| 413 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 414 | } |
| 415 | static void sumFinalize(sqlite_func *context){ |
| 416 | SumCtx *p; |
| 417 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 418 | sqlite_set_result_double(context, p ? p->sum : 0.0); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 419 | } |
| 420 | static void avgFinalize(sqlite_func *context){ |
| 421 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 422 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 423 | if( p && p->cnt>0 ){ |
| 424 | sqlite_set_result_double(context, p->sum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | ** An instance of the following structure holds the context of a |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 430 | ** variance or standard deviation computation. |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 431 | */ |
| 432 | typedef struct StdDevCtx StdDevCtx; |
| 433 | struct StdDevCtx { |
| 434 | double sum; /* Sum of terms */ |
| 435 | double sum2; /* Sum of the squares of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 436 | int cnt; /* Number of terms counted */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 439 | #if 0 /* Omit because math library is required */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 440 | /* |
| 441 | ** Routines used to compute the standard deviation as an aggregate. |
| 442 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 443 | static void stdDevStep(sqlite_func *context, int argc, const char **argv){ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 444 | StdDevCtx *p; |
| 445 | double x; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 446 | if( argc<1 ) return; |
| 447 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 448 | if( p && argv[0] ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 449 | x = sqliteAtoF(argv[0]); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 450 | p->sum += x; |
| 451 | p->sum2 += x*x; |
| 452 | p->cnt++; |
| 453 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 454 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 455 | static void stdDevFinalize(sqlite_func *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 456 | double rN = sqlite_aggregate_count(context); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 457 | StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 458 | if( p && p->cnt>1 ){ |
| 459 | double rCnt = cnt; |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 460 | sqlite_set_result_double(context, |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 461 | sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0))); |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 462 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 463 | } |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 464 | #endif |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 465 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 466 | /* |
| 467 | ** The following structure keeps track of state information for the |
| 468 | ** count() aggregate function. |
| 469 | */ |
| 470 | typedef struct CountCtx CountCtx; |
| 471 | struct CountCtx { |
| 472 | int n; |
| 473 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 474 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 475 | /* |
| 476 | ** Routines to implement the count() aggregate function. |
| 477 | */ |
| 478 | static void countStep(sqlite_func *context, int argc, const char **argv){ |
| 479 | CountCtx *p; |
| 480 | p = sqlite_aggregate_context(context, sizeof(*p)); |
| 481 | if( (argc==0 || argv[0]) && p ){ |
| 482 | p->n++; |
| 483 | } |
| 484 | } |
| 485 | static void countFinalize(sqlite_func *context){ |
| 486 | CountCtx *p; |
| 487 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 488 | sqlite_set_result_int(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* |
| 492 | ** This function tracks state information for the min() and max() |
| 493 | ** aggregate functions. |
| 494 | */ |
| 495 | typedef struct MinMaxCtx MinMaxCtx; |
| 496 | struct MinMaxCtx { |
| 497 | char *z; /* The best so far */ |
| 498 | char zBuf[28]; /* Space that can be used for storage */ |
| 499 | }; |
| 500 | |
| 501 | /* |
| 502 | ** Routines to implement min() and max() aggregate functions. |
| 503 | */ |
| 504 | static void minStep(sqlite_func *context, int argc, const char **argv){ |
| 505 | MinMaxCtx *p; |
| 506 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 507 | if( p==0 || argc<1 || argv[0]==0 ) return; |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 508 | if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 509 | int len; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 510 | if( !p->zBuf[0] ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 511 | sqliteFree(p->z); |
| 512 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 513 | len = strlen(argv[0]); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 514 | if( len < sizeof(p->zBuf)-1 ){ |
| 515 | p->z = &p->zBuf[1]; |
| 516 | p->zBuf[0] = 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 517 | }else{ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 518 | p->z = sqliteMalloc( len+1 ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 519 | p->zBuf[0] = 0; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 520 | if( p->z==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 521 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 522 | strcpy(p->z, argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | static void maxStep(sqlite_func *context, int argc, const char **argv){ |
| 526 | MinMaxCtx *p; |
| 527 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 528 | if( p==0 || argc<1 || argv[0]==0 ) return; |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 529 | if( p->z==0 || sqliteCompare(argv[0],p->z)>0 ){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 530 | int len; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 531 | if( !p->zBuf[0] ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 532 | sqliteFree(p->z); |
| 533 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 534 | len = strlen(argv[0]); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 535 | if( len < sizeof(p->zBuf)-1 ){ |
| 536 | p->z = &p->zBuf[1]; |
| 537 | p->zBuf[0] = 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 538 | }else{ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 539 | p->z = sqliteMalloc( len+1 ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 540 | p->zBuf[0] = 0; |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 541 | if( p->z==0 ) return; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 542 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 543 | strcpy(p->z, argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | static void minMaxFinalize(sqlite_func *context){ |
| 547 | MinMaxCtx *p; |
| 548 | p = sqlite_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 549 | if( p && p->z ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 550 | sqlite_set_result_string(context, p->z, strlen(p->z)); |
| 551 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 552 | if( p && !p->zBuf[0] ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 553 | sqliteFree(p->z); |
| 554 | } |
| 555 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 556 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 557 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 558 | ** This function registered all of the above C functions as SQL |
| 559 | ** functions. This should be the only routine in this file with |
| 560 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 561 | */ |
drh | 28f4b68 | 2002-06-09 10:14:18 +0000 | [diff] [blame] | 562 | void sqliteRegisterBuiltinFunctions(sqlite *db){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 563 | static struct { |
| 564 | char *zName; |
| 565 | int nArg; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 566 | int dataType; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 567 | void (*xFunc)(sqlite_func*,int,const char**); |
| 568 | } aFuncs[] = { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 569 | { "min", -1, SQLITE_ARGS, minFunc }, |
| 570 | { "min", 0, 0, 0 }, |
| 571 | { "max", -1, SQLITE_ARGS, maxFunc }, |
| 572 | { "max", 0, 0, 0 }, |
| 573 | { "length", 1, SQLITE_NUMERIC, lengthFunc }, |
| 574 | { "substr", 3, SQLITE_TEXT, substrFunc }, |
| 575 | { "abs", 1, SQLITE_NUMERIC, absFunc }, |
| 576 | { "round", 1, SQLITE_NUMERIC, roundFunc }, |
| 577 | { "round", 2, SQLITE_NUMERIC, roundFunc }, |
| 578 | { "upper", 1, SQLITE_TEXT, upperFunc }, |
| 579 | { "lower", 1, SQLITE_TEXT, lowerFunc }, |
| 580 | { "coalesce", -1, SQLITE_ARGS, ifnullFunc }, |
| 581 | { "coalesce", 0, 0, 0 }, |
| 582 | { "coalesce", 1, 0, 0 }, |
| 583 | { "ifnull", 2, SQLITE_ARGS, ifnullFunc }, |
| 584 | { "random", -1, SQLITE_NUMERIC, randomFunc }, |
| 585 | { "like", 2, SQLITE_NUMERIC, likeFunc }, |
| 586 | { "glob", 2, SQLITE_NUMERIC, globFunc }, |
| 587 | { "nullif", 2, SQLITE_ARGS, nullifFunc }, |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 588 | { "sqlite_version",0,SQLITE_TEXT, versionFunc}, |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 589 | { "quote", 1, SQLITE_ARGS, quoteFunc }, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 590 | #ifdef SQLITE_SOUNDEX |
| 591 | { "soundex", 1, SQLITE_TEXT, soundexFunc}, |
| 592 | #endif |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 593 | #ifdef SQLITE_TEST |
| 594 | { "randstr", 2, SQLITE_TEXT, randStr }, |
| 595 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 596 | }; |
| 597 | static struct { |
| 598 | char *zName; |
| 599 | int nArg; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 600 | int dataType; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 601 | void (*xStep)(sqlite_func*,int,const char**); |
| 602 | void (*xFinalize)(sqlite_func*); |
| 603 | } aAggs[] = { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 604 | { "min", 1, 0, minStep, minMaxFinalize }, |
| 605 | { "max", 1, 0, maxStep, minMaxFinalize }, |
| 606 | { "sum", 1, SQLITE_NUMERIC, sumStep, sumFinalize }, |
| 607 | { "avg", 1, SQLITE_NUMERIC, sumStep, avgFinalize }, |
| 608 | { "count", 0, SQLITE_NUMERIC, countStep, countFinalize }, |
| 609 | { "count", 1, SQLITE_NUMERIC, countStep, countFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 610 | #if 0 |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 611 | { "stddev", 1, SQLITE_NUMERIC, stdDevStep, stdDevFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 612 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 613 | }; |
| 614 | int i; |
| 615 | |
| 616 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
| 617 | sqlite_create_function(db, aFuncs[i].zName, |
| 618 | aFuncs[i].nArg, aFuncs[i].xFunc, 0); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 619 | if( aFuncs[i].xFunc ){ |
| 620 | sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType); |
| 621 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 622 | } |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 623 | sqlite_create_function(db, "last_insert_rowid", 0, |
| 624 | last_insert_rowid, db); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 625 | sqlite_function_type(db, "last_insert_rowid", SQLITE_NUMERIC); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame^] | 626 | sqlite_create_function(db, "change_count", 0, change_count, db); |
| 627 | sqlite_function_type(db, "change_count", SQLITE_NUMERIC); |
| 628 | sqlite_create_function(db, "last_statement_change_count", 0, |
| 629 | last_statement_change_count, db); |
| 630 | sqlite_function_type(db, "last_statement_change_count", SQLITE_NUMERIC); |
| 631 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 632 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
| 633 | sqlite_create_aggregate(db, aAggs[i].zName, |
| 634 | aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 635 | sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 636 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 637 | sqliteRegisterDateTimeFunctions(db); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 638 | } |