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