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