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