blob: fd581f8a168ec04b141b384302305ecc23c8e3d1 [file] [log] [blame]
drhdc04c582002-02-24 01:55:15 +00001/*
2** 2002 February 23
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains the C functions that implement various SQL
13** functions of SQLite.
14**
15** There is only one exported symbol in this file - the function
16** sqliteRegisterBuildinFunctions() found at the bottom of the file.
17** All other code has file scope.
18**
drh268380c2004-02-25 13:47:31 +000019** $Id: func.c,v 1.42 2004/02/25 13:47:32 drh Exp $
drhdc04c582002-02-24 01:55:15 +000020*/
21#include <ctype.h>
drhd3a149e2002-02-24 17:12:53 +000022#include <math.h>
23#include <stdlib.h>
drh0bce8352002-02-28 00:41:10 +000024#include <assert.h>
25#include "sqliteInt.h"
drh771d8c32003-08-09 21:32:28 +000026#include "os.h"
drh0bce8352002-02-28 00:41:10 +000027
28/*
29** Implementation of the non-aggregate min() and max() functions
30*/
drh268380c2004-02-25 13:47:31 +000031static void minmaxFunc(sqlite_func *context, int argc, const char **argv){
drh0bce8352002-02-28 00:41:10 +000032 const char *zBest;
33 int i;
drh268380c2004-02-25 13:47:31 +000034 int (*xCompare)(const char*, const char*);
35 int mask; /* 0 for min() or 0xffffffff for max() */
drh0bce8352002-02-28 00:41:10 +000036
drh89425d52002-02-28 03:04:48 +000037 if( argc==0 ) return;
drh268380c2004-02-25 13:47:31 +000038 mask = (int)sqlite_user_data(context);
drh0bce8352002-02-28 00:41:10 +000039 zBest = argv[0];
drh8912d102002-05-26 21:34:58 +000040 if( zBest==0 ) return;
drh268380c2004-02-25 13:47:31 +000041 if( argv[1][0]=='n' ){
42 xCompare = sqliteCompare;
43 }else{
44 xCompare = strcmp;
45 }
46 for(i=2; i<argc; i+=2){
drh8912d102002-05-26 21:34:58 +000047 if( argv[i]==0 ) return;
drh268380c2004-02-25 13:47:31 +000048 if( (xCompare(argv[i], zBest)^mask)<0 ){
drh0bce8352002-02-28 00:41:10 +000049 zBest = argv[i];
50 }
51 }
52 sqlite_set_result_string(context, zBest, -1);
53}
drh0bce8352002-02-28 00:41:10 +000054
drh268380c2004-02-25 13:47:31 +000055/*
56** Return the type of the argument.
57*/
58static void typeofFunc(sqlite_func *context, int argc, const char **argv){
59 assert( argc==2 );
60 sqlite_set_result_string(context, argv[1], -1);
drh0bce8352002-02-28 00:41:10 +000061}
62
63/*
64** Implementation of the length() function
65*/
66static void lengthFunc(sqlite_func *context, int argc, const char **argv){
67 const char *z;
68 int len;
69
70 assert( argc==1 );
71 z = argv[0];
drh8912d102002-05-26 21:34:58 +000072 if( z==0 ) return;
drh0bce8352002-02-28 00:41:10 +000073#ifdef SQLITE_UTF8
drh8912d102002-05-26 21:34:58 +000074 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +000075#else
drh8912d102002-05-26 21:34:58 +000076 len = strlen(z);
drh0bce8352002-02-28 00:41:10 +000077#endif
drh0bce8352002-02-28 00:41:10 +000078 sqlite_set_result_int(context, len);
79}
80
81/*
82** Implementation of the abs() function
83*/
84static void absFunc(sqlite_func *context, int argc, const char **argv){
85 const char *z;
86 assert( argc==1 );
87 z = argv[0];
drh8912d102002-05-26 21:34:58 +000088 if( z==0 ) return;
89 if( z[0]=='-' && isdigit(z[1]) ) z++;
drh0bce8352002-02-28 00:41:10 +000090 sqlite_set_result_string(context, z, -1);
91}
92
93/*
94** Implementation of the substr() function
95*/
96static void substrFunc(sqlite_func *context, int argc, const char **argv){
97 const char *z;
98#ifdef SQLITE_UTF8
99 const char *z2;
100 int i;
101#endif
102 int p1, p2, len;
103 assert( argc==3 );
104 z = argv[0];
105 if( z==0 ) return;
106 p1 = atoi(argv[1]?argv[1]:0);
107 p2 = atoi(argv[2]?argv[2]:0);
108#ifdef SQLITE_UTF8
drh47c8a672002-02-28 04:00:12 +0000109 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000110#else
111 len = strlen(z);
112#endif
113 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000114 p1 += len;
drh653bc752002-02-28 03:31:10 +0000115 if( p1<0 ){
116 p2 += p1;
117 p1 = 0;
118 }
drh0bce8352002-02-28 00:41:10 +0000119 }else if( p1>0 ){
120 p1--;
121 }
122 if( p1+p2>len ){
123 p2 = len-p1;
124 }
125#ifdef SQLITE_UTF8
drh77396302004-01-02 13:17:48 +0000126 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000127 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000128 }
drh47c8a672002-02-28 04:00:12 +0000129 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000130 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000131 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000132 }
drh47c8a672002-02-28 04:00:12 +0000133 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh0bce8352002-02-28 00:41:10 +0000134#endif
drh653bc752002-02-28 03:31:10 +0000135 if( p2<0 ) p2 = 0;
drh0bce8352002-02-28 00:41:10 +0000136 sqlite_set_result_string(context, &z[p1], p2);
137}
138
139/*
140** Implementation of the round() function
141*/
142static void roundFunc(sqlite_func *context, int argc, const char **argv){
143 int n;
144 double r;
145 char zBuf[100];
146 assert( argc==1 || argc==2 );
drh8912d102002-05-26 21:34:58 +0000147 if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
148 n = argc==2 ? atoi(argv[1]) : 0;
drh0bce8352002-02-28 00:41:10 +0000149 if( n>30 ) n = 30;
150 if( n<0 ) n = 0;
drheb9a9e82004-02-22 17:49:32 +0000151 r = sqliteAtoF(argv[0], 0);
drh0bce8352002-02-28 00:41:10 +0000152 sprintf(zBuf,"%.*f",n,r);
153 sqlite_set_result_string(context, zBuf, -1);
154}
drhdc04c582002-02-24 01:55:15 +0000155
156/*
157** Implementation of the upper() and lower() SQL functions.
158*/
drh1350b032002-02-27 19:00:20 +0000159static void upperFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000160 char *z;
161 int i;
162 if( argc<1 || argv[0]==0 ) return;
163 z = sqlite_set_result_string(context, argv[0], -1);
164 if( z==0 ) return;
165 for(i=0; z[i]; i++){
166 if( islower(z[i]) ) z[i] = toupper(z[i]);
167 }
168}
drh1350b032002-02-27 19:00:20 +0000169static void lowerFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000170 char *z;
171 int i;
172 if( argc<1 || argv[0]==0 ) return;
173 z = sqlite_set_result_string(context, argv[0], -1);
174 if( z==0 ) return;
175 for(i=0; z[i]; i++){
176 if( isupper(z[i]) ) z[i] = tolower(z[i]);
177 }
178}
179
180/*
drhfbc99082002-02-28 03:14:18 +0000181** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000182** All three do the same thing. They return the first non-NULL
183** argument.
drh3212e182002-02-28 00:46:26 +0000184*/
185static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
drhfbc99082002-02-28 03:14:18 +0000186 int i;
187 for(i=0; i<argc; i++){
188 if( argv[i] ){
189 sqlite_set_result_string(context, argv[i], -1);
190 break;
191 }
192 }
drh3212e182002-02-28 00:46:26 +0000193}
194
195/*
drhf9ffac92002-03-02 19:00:31 +0000196** Implementation of random(). Return a random integer.
197*/
198static void randomFunc(sqlite_func *context, int argc, const char **argv){
drhbbd82df2004-02-11 09:46:30 +0000199 int r;
200 sqliteRandomness(sizeof(r), &r);
201 sqlite_set_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000202}
203
204/*
drh6ed41ad2002-04-06 14:10:47 +0000205** Implementation of the last_insert_rowid() SQL function. The return
206** value is the same as the sqlite_last_insert_rowid() API function.
207*/
drh0ac65892002-04-20 14:24:41 +0000208static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
drh6ed41ad2002-04-06 14:10:47 +0000209 sqlite *db = sqlite_user_data(context);
210 sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
211}
212
rdcb0c374f2004-02-20 22:53:38 +0000213static void change_count(sqlite_func *context, int arg, const char **argv){
214 sqlite *db = sqlite_user_data(context);
215 sqlite_set_result_int(context, sqlite_changes(db));
216}
217static void last_statement_change_count(sqlite_func *context, int arg,
218 const char **argv){
219 sqlite *db = sqlite_user_data(context);
220 sqlite_set_result_int(context, sqlite_last_statement_changes(db));
221}
222
drh6ed41ad2002-04-06 14:10:47 +0000223/*
drh0ac65892002-04-20 14:24:41 +0000224** Implementation of the like() SQL function. This function implements
225** the build-in LIKE operator. The first argument to the function is the
226** string and the second argument is the pattern. So, the SQL statements:
227**
228** A LIKE B
229**
230** is implemented as like(A,B).
231*/
232static void likeFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000233 if( argv[0]==0 || argv[1]==0 ) return;
drhec1bd0b2003-08-26 11:41:27 +0000234 sqlite_set_result_int(context,
235 sqliteLikeCompare((const unsigned char*)argv[0],
236 (const unsigned char*)argv[1]));
drh0ac65892002-04-20 14:24:41 +0000237}
238
239/*
240** Implementation of the glob() SQL function. This function implements
241** the build-in GLOB operator. The first argument to the function is the
242** string and the second argument is the pattern. So, the SQL statements:
243**
244** A GLOB B
245**
246** is implemented as glob(A,B).
247*/
248static void globFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000249 if( argv[0]==0 || argv[1]==0 ) return;
drhec1bd0b2003-08-26 11:41:27 +0000250 sqlite_set_result_int(context,
251 sqliteGlobCompare((const unsigned char*)argv[0],
252 (const unsigned char*)argv[1]));
drh8912d102002-05-26 21:34:58 +0000253}
254
255/*
256** Implementation of the NULLIF(x,y) function. The result is the first
257** argument if the arguments are different. The result is NULL if the
258** arguments are equal to each other.
259*/
260static void nullifFunc(sqlite_func *context, int argc, const char **argv){
261 if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
262 sqlite_set_result_string(context, argv[0], -1);
263 }
drh0ac65892002-04-20 14:24:41 +0000264}
265
drh647cb0e2002-11-04 19:32:25 +0000266/*
267** Implementation of the VERSION(*) function. The result is the version
268** of the SQLite library that is running.
269*/
270static void versionFunc(sqlite_func *context, int argc, const char **argv){
271 sqlite_set_result_string(context, sqlite_version, -1);
272}
273
drh47394702003-08-20 01:03:33 +0000274/*
275** EXPERIMENTAL - This is not an official function. The interface may
276** change. This function may disappear. Do not write code that depends
277** on this function.
278**
279** Implementation of the QUOTE() function. This function takes a single
280** argument. If the argument is numeric, the return value is the same as
281** the argument. If the argument is NULL, the return value is the string
282** "NULL". Otherwise, the argument is enclosed in single quotes with
283** single-quote escapes.
284*/
285static void quoteFunc(sqlite_func *context, int argc, const char **argv){
286 if( argc<1 ) return;
287 if( argv[0]==0 ){
288 sqlite_set_result_string(context, "NULL", 4);
289 }else if( sqliteIsNumber(argv[0]) ){
290 sqlite_set_result_string(context, argv[0], -1);
291 }else{
292 int i,j,n;
293 char *z;
294 for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
295 z = sqliteMalloc( i+n+3 );
296 if( z==0 ) return;
297 z[0] = '\'';
298 for(i=0, j=1; argv[0][i]; i++){
299 z[j++] = argv[0][i];
300 if( argv[0][i]=='\'' ){
301 z[j++] = '\'';
302 }
303 }
304 z[j++] = '\'';
305 z[j] = 0;
306 sqlite_set_result_string(context, z, j);
307 sqliteFree(z);
308 }
309}
310
drhd24cc422003-03-27 12:51:24 +0000311#ifdef SQLITE_SOUNDEX
312/*
313** Compute the soundex encoding of a word.
314*/
315static void soundexFunc(sqlite_func *context, int argc, const char **argv){
316 char zResult[8];
317 const char *zIn;
318 int i, j;
319 static const unsigned char iCode[] = {
320 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
321 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
322 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
323 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
324 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
325 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
326 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
327 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
328 };
329 assert( argc==1 );
330 zIn = argv[0];
331 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
332 if( zIn[i] ){
333 zResult[0] = toupper(zIn[i]);
334 for(j=1; j<4 && zIn[i]; i++){
335 int code = iCode[zIn[i]&0x7f];
336 if( code>0 ){
337 zResult[j++] = code + '0';
338 }
339 }
340 while( j<4 ){
341 zResult[j++] = '0';
342 }
343 zResult[j] = 0;
344 sqlite_set_result_string(context, zResult, 4);
345 }else{
drh937dd842003-06-28 16:20:22 +0000346 sqlite_set_result_string(context, "?000", 4);
drhd24cc422003-03-27 12:51:24 +0000347 }
348}
349#endif
350
drh193a6b42002-07-07 16:52:46 +0000351#ifdef SQLITE_TEST
352/*
353** This function generates a string of random characters. Used for
354** generating test data.
355*/
356static void randStr(sqlite_func *context, int argc, const char **argv){
drhbbd82df2004-02-11 09:46:30 +0000357 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000358 "abcdefghijklmnopqrstuvwxyz"
359 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
360 "0123456789"
361 ".-!,:*^+=_|?/<> ";
362 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000363 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000364 if( argc>=1 ){
365 iMin = atoi(argv[0]);
366 if( iMin<0 ) iMin = 0;
367 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
368 }else{
369 iMin = 1;
370 }
371 if( argc>=2 ){
372 iMax = atoi(argv[1]);
373 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000374 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000375 }else{
376 iMax = 50;
377 }
378 n = iMin;
379 if( iMax>iMin ){
drhbbd82df2004-02-11 09:46:30 +0000380 sqliteRandomness(sizeof(r), &r);
381 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000382 n += r%(iMax + 1 - iMin);
383 }
drh1dba7272004-01-16 13:58:18 +0000384 assert( n<sizeof(zBuf) );
drhbbd82df2004-02-11 09:46:30 +0000385 sqliteRandomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000386 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000387 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000388 }
389 zBuf[n] = 0;
390 sqlite_set_result_string(context, zBuf, n);
391}
392#endif
393
drh0ac65892002-04-20 14:24:41 +0000394/*
drhd3a149e2002-02-24 17:12:53 +0000395** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000396** sum() or avg() aggregate computation.
397*/
398typedef struct SumCtx SumCtx;
399struct SumCtx {
400 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000401 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000402};
403
404/*
405** Routines used to compute the sum or average.
406*/
407static void sumStep(sqlite_func *context, int argc, const char **argv){
408 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000409 if( argc<1 ) return;
410 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000411 if( p && argv[0] ){
drheb9a9e82004-02-22 17:49:32 +0000412 p->sum += sqliteAtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000413 p->cnt++;
414 }
drhdd5baa92002-02-27 19:50:59 +0000415}
416static void sumFinalize(sqlite_func *context){
417 SumCtx *p;
418 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000419 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000420}
421static void avgFinalize(sqlite_func *context){
422 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000423 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000424 if( p && p->cnt>0 ){
425 sqlite_set_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000426 }
427}
428
429/*
430** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000431** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000432*/
433typedef struct StdDevCtx StdDevCtx;
434struct StdDevCtx {
435 double sum; /* Sum of terms */
436 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000437 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000438};
439
drhef2daf52002-03-04 02:26:15 +0000440#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000441/*
442** Routines used to compute the standard deviation as an aggregate.
443*/
drh1350b032002-02-27 19:00:20 +0000444static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000445 StdDevCtx *p;
446 double x;
drh1350b032002-02-27 19:00:20 +0000447 if( argc<1 ) return;
448 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000449 if( p && argv[0] ){
drheb9a9e82004-02-22 17:49:32 +0000450 x = sqliteAtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000451 p->sum += x;
452 p->sum2 += x*x;
453 p->cnt++;
454 }
drhd3a149e2002-02-24 17:12:53 +0000455}
drh1350b032002-02-27 19:00:20 +0000456static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000457 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000458 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000459 if( p && p->cnt>1 ){
460 double rCnt = cnt;
drhd3a149e2002-02-24 17:12:53 +0000461 sqlite_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000462 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000463 }
drhd3a149e2002-02-24 17:12:53 +0000464}
drhef2daf52002-03-04 02:26:15 +0000465#endif
drhd3a149e2002-02-24 17:12:53 +0000466
drh0bce8352002-02-28 00:41:10 +0000467/*
468** The following structure keeps track of state information for the
469** count() aggregate function.
470*/
471typedef struct CountCtx CountCtx;
472struct CountCtx {
473 int n;
474};
drhdd5baa92002-02-27 19:50:59 +0000475
drh0bce8352002-02-28 00:41:10 +0000476/*
477** Routines to implement the count() aggregate function.
478*/
479static void countStep(sqlite_func *context, int argc, const char **argv){
480 CountCtx *p;
481 p = sqlite_aggregate_context(context, sizeof(*p));
482 if( (argc==0 || argv[0]) && p ){
483 p->n++;
484 }
485}
486static void countFinalize(sqlite_func *context){
487 CountCtx *p;
488 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000489 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000490}
491
492/*
493** This function tracks state information for the min() and max()
494** aggregate functions.
495*/
496typedef struct MinMaxCtx MinMaxCtx;
497struct MinMaxCtx {
498 char *z; /* The best so far */
499 char zBuf[28]; /* Space that can be used for storage */
500};
501
502/*
503** Routines to implement min() and max() aggregate functions.
504*/
drh268380c2004-02-25 13:47:31 +0000505static void minmaxStep(sqlite_func *context, int argc, const char **argv){
drh0bce8352002-02-28 00:41:10 +0000506 MinMaxCtx *p;
drh268380c2004-02-25 13:47:31 +0000507 int (*xCompare)(const char*, const char*);
508 int mask; /* 0 for min() or 0xffffffff for max() */
509
510 assert( argc==2 );
511 if( argv[1][0]=='n' ){
512 xCompare = sqliteCompare;
513 }else{
514 xCompare = strcmp;
drh0bce8352002-02-28 00:41:10 +0000515 }
drh268380c2004-02-25 13:47:31 +0000516 mask = (int)sqlite_user_data(context);
drh0bce8352002-02-28 00:41:10 +0000517 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000518 if( p==0 || argc<1 || argv[0]==0 ) return;
drh268380c2004-02-25 13:47:31 +0000519 if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){
drh8912d102002-05-26 21:34:58 +0000520 int len;
drh00706be2004-01-30 14:49:16 +0000521 if( !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000522 sqliteFree(p->z);
523 }
drh8912d102002-05-26 21:34:58 +0000524 len = strlen(argv[0]);
drh00706be2004-01-30 14:49:16 +0000525 if( len < sizeof(p->zBuf)-1 ){
526 p->z = &p->zBuf[1];
527 p->zBuf[0] = 1;
drh0bce8352002-02-28 00:41:10 +0000528 }else{
drh8912d102002-05-26 21:34:58 +0000529 p->z = sqliteMalloc( len+1 );
drh00706be2004-01-30 14:49:16 +0000530 p->zBuf[0] = 0;
drh8912d102002-05-26 21:34:58 +0000531 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000532 }
drh8912d102002-05-26 21:34:58 +0000533 strcpy(p->z, argv[0]);
drh0bce8352002-02-28 00:41:10 +0000534 }
535}
536static void minMaxFinalize(sqlite_func *context){
537 MinMaxCtx *p;
538 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000539 if( p && p->z ){
drh0bce8352002-02-28 00:41:10 +0000540 sqlite_set_result_string(context, p->z, strlen(p->z));
541 }
drh00706be2004-01-30 14:49:16 +0000542 if( p && !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000543 sqliteFree(p->z);
544 }
545}
drhdd5baa92002-02-27 19:50:59 +0000546
drhd3a149e2002-02-24 17:12:53 +0000547/*
drha2ed5602002-02-26 23:55:31 +0000548** This function registered all of the above C functions as SQL
549** functions. This should be the only routine in this file with
550** external linkage.
drhdc04c582002-02-24 01:55:15 +0000551*/
drh28f4b682002-06-09 10:14:18 +0000552void sqliteRegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000553 static struct {
554 char *zName;
drh268380c2004-02-25 13:47:31 +0000555 signed char nArg;
556 signed char dataType;
557 u8 argType; /* 0: none. 1: db 2: (-1) */
drh0bce8352002-02-28 00:41:10 +0000558 void (*xFunc)(sqlite_func*,int,const char**);
559 } aFuncs[] = {
drh268380c2004-02-25 13:47:31 +0000560 { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
561 { "min", 0, 0, 0, 0 },
562 { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
563 { "max", 0, 0, 2, 0 },
564 { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
565 { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
566 { "substr", 3, SQLITE_TEXT, 0, substrFunc },
567 { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
568 { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
569 { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
570 { "upper", 1, SQLITE_TEXT, 0, upperFunc },
571 { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
572 { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
573 { "coalesce", 0, 0, 0, 0 },
574 { "coalesce", 1, 0, 0, 0 },
575 { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
576 { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
577 { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
578 { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
579 { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
580 { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc},
581 { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
582 { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
583 { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
584 { "last_statement_change_count",
585 0, SQLITE_NUMERIC, 1, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000586#ifdef SQLITE_SOUNDEX
drh268380c2004-02-25 13:47:31 +0000587 { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000588#endif
drh193a6b42002-07-07 16:52:46 +0000589#ifdef SQLITE_TEST
drh268380c2004-02-25 13:47:31 +0000590 { "randstr", 2, SQLITE_TEXT, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000591#endif
drh0bce8352002-02-28 00:41:10 +0000592 };
593 static struct {
594 char *zName;
drh268380c2004-02-25 13:47:31 +0000595 signed char nArg;
596 signed char dataType;
597 u8 argType;
drh0bce8352002-02-28 00:41:10 +0000598 void (*xStep)(sqlite_func*,int,const char**);
599 void (*xFinalize)(sqlite_func*);
600 } aAggs[] = {
drh268380c2004-02-25 13:47:31 +0000601 { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
602 { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
603 { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
604 { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
605 { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
606 { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000607#if 0
drh268380c2004-02-25 13:47:31 +0000608 { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000609#endif
drh0bce8352002-02-28 00:41:10 +0000610 };
drh268380c2004-02-25 13:47:31 +0000611 static const char *azTypeFuncs[] = { "min", "max", "typeof" };
drh0bce8352002-02-28 00:41:10 +0000612 int i;
613
614 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000615 void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
drh0bce8352002-02-28 00:41:10 +0000616 sqlite_create_function(db, aFuncs[i].zName,
drh268380c2004-02-25 13:47:31 +0000617 aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
drhc9b84a12002-06-20 11:36:48 +0000618 if( aFuncs[i].xFunc ){
619 sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
620 }
drh0bce8352002-02-28 00:41:10 +0000621 }
622 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000623 void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
drh0bce8352002-02-28 00:41:10 +0000624 sqlite_create_aggregate(db, aAggs[i].zName,
drh268380c2004-02-25 13:47:31 +0000625 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
drhc9b84a12002-06-20 11:36:48 +0000626 sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
drh0bce8352002-02-28 00:41:10 +0000627 }
drh268380c2004-02-25 13:47:31 +0000628 for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
629 int n = strlen(azTypeFuncs[i]);
630 FuncDef *p = sqliteHashFind(&db->aFunc, azTypeFuncs[i], n);
631 while( p ){
632 p->includeTypes = 1;
633 p = p->pNext;
634 }
635 }
drh7014aff2003-11-01 01:53:53 +0000636 sqliteRegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000637}