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