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