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