blob: cf523e3b8df2663c8e8cf78a00f1d3c0266a56f9 [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**
drh8912d102002-05-26 21:34:58 +000019** $Id: func.c,v 1.17 2002/05/26 21:34:58 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"
26
27/*
28** Implementation of the non-aggregate min() and max() functions
29*/
30static void minFunc(sqlite_func *context, int argc, const char **argv){
31 const char *zBest;
32 int i;
33
drh89425d52002-02-28 03:04:48 +000034 if( argc==0 ) return;
drh0bce8352002-02-28 00:41:10 +000035 zBest = argv[0];
drh8912d102002-05-26 21:34:58 +000036 if( zBest==0 ) return;
drh0bce8352002-02-28 00:41:10 +000037 for(i=1; i<argc; i++){
drh8912d102002-05-26 21:34:58 +000038 if( argv[i]==0 ) return;
drh0bce8352002-02-28 00:41:10 +000039 if( sqliteCompare(argv[i], zBest)<0 ){
40 zBest = argv[i];
41 }
42 }
43 sqlite_set_result_string(context, zBest, -1);
44}
45static void maxFunc(sqlite_func *context, int argc, const char **argv){
46 const char *zBest;
47 int i;
48
drh89425d52002-02-28 03:04:48 +000049 if( argc==0 ) return;
drh0bce8352002-02-28 00:41:10 +000050 zBest = argv[0];
drh8912d102002-05-26 21:34:58 +000051 if( zBest==0 ) return;
drh0bce8352002-02-28 00:41:10 +000052 for(i=1; i<argc; i++){
drh8912d102002-05-26 21:34:58 +000053 if( argv[i]==0 ) return;
drh0bce8352002-02-28 00:41:10 +000054 if( sqliteCompare(argv[i], zBest)>0 ){
55 zBest = argv[i];
56 }
57 }
58 sqlite_set_result_string(context, zBest, -1);
59}
60
61/*
62** Implementation of the length() function
63*/
64static void lengthFunc(sqlite_func *context, int argc, const char **argv){
65 const char *z;
66 int len;
67
68 assert( argc==1 );
69 z = argv[0];
drh8912d102002-05-26 21:34:58 +000070 if( z==0 ) return;
drh0bce8352002-02-28 00:41:10 +000071#ifdef SQLITE_UTF8
drh8912d102002-05-26 21:34:58 +000072 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +000073#else
drh8912d102002-05-26 21:34:58 +000074 len = strlen(z);
drh0bce8352002-02-28 00:41:10 +000075#endif
drh0bce8352002-02-28 00:41:10 +000076 sqlite_set_result_int(context, len);
77}
78
79/*
80** Implementation of the abs() function
81*/
82static void absFunc(sqlite_func *context, int argc, const char **argv){
83 const char *z;
84 assert( argc==1 );
85 z = argv[0];
drh8912d102002-05-26 21:34:58 +000086 if( z==0 ) return;
87 if( z[0]=='-' && isdigit(z[1]) ) z++;
drh0bce8352002-02-28 00:41:10 +000088 sqlite_set_result_string(context, z, -1);
89}
90
91/*
92** Implementation of the substr() function
93*/
94static void substrFunc(sqlite_func *context, int argc, const char **argv){
95 const char *z;
96#ifdef SQLITE_UTF8
97 const char *z2;
98 int i;
99#endif
100 int p1, p2, len;
101 assert( argc==3 );
102 z = argv[0];
103 if( z==0 ) return;
104 p1 = atoi(argv[1]?argv[1]:0);
105 p2 = atoi(argv[2]?argv[2]:0);
106#ifdef SQLITE_UTF8
drh47c8a672002-02-28 04:00:12 +0000107 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000108#else
109 len = strlen(z);
110#endif
111 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000112 p1 += len;
drh653bc752002-02-28 03:31:10 +0000113 if( p1<0 ){
114 p2 += p1;
115 p1 = 0;
116 }
drh0bce8352002-02-28 00:41:10 +0000117 }else if( p1>0 ){
118 p1--;
119 }
120 if( p1+p2>len ){
121 p2 = len-p1;
122 }
123#ifdef SQLITE_UTF8
124 for(i=0; i<p1; i++){
125 assert( z[i] );
drh47c8a672002-02-28 04:00:12 +0000126 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000127 }
drh47c8a672002-02-28 04:00:12 +0000128 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh0bce8352002-02-28 00:41:10 +0000129 for(; i<p1+p2; i++){
130 assert( z[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;
drh8912d102002-05-26 21:34:58 +0000151 r = atof(argv[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.
182** All three do the same thing. They return the first argument
183** non-NULL 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){
199 sqlite_set_result_int(context, sqliteRandomInteger());
200}
201
202/*
drh6ed41ad2002-04-06 14:10:47 +0000203** Implementation of the last_insert_rowid() SQL function. The return
204** value is the same as the sqlite_last_insert_rowid() API function.
205*/
drh0ac65892002-04-20 14:24:41 +0000206static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
drh6ed41ad2002-04-06 14:10:47 +0000207 sqlite *db = sqlite_user_data(context);
208 sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
209}
210
211/*
drh0ac65892002-04-20 14:24:41 +0000212** Implementation of the like() SQL function. This function implements
213** the build-in LIKE operator. The first argument to the function is the
214** string and the second argument is the pattern. So, the SQL statements:
215**
216** A LIKE B
217**
218** is implemented as like(A,B).
219*/
220static void likeFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000221 if( argv[0]==0 || argv[1]==0 ) return;
222 sqlite_set_result_int(context, sqliteLikeCompare(argv[0], argv[1]));
drh0ac65892002-04-20 14:24:41 +0000223}
224
225/*
226** Implementation of the glob() SQL function. This function implements
227** the build-in GLOB operator. The first argument to the function is the
228** string and the second argument is the pattern. So, the SQL statements:
229**
230** A GLOB B
231**
232** is implemented as glob(A,B).
233*/
234static void globFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000235 if( argv[0]==0 || argv[1]==0 ) return;
236 sqlite_set_result_int(context, sqliteGlobCompare(argv[0], argv[1]));
237}
238
239/*
240** Implementation of the NULLIF(x,y) function. The result is the first
241** argument if the arguments are different. The result is NULL if the
242** arguments are equal to each other.
243*/
244static void nullifFunc(sqlite_func *context, int argc, const char **argv){
245 if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
246 sqlite_set_result_string(context, argv[0], -1);
247 }
drh0ac65892002-04-20 14:24:41 +0000248}
249
250/*
drhd3a149e2002-02-24 17:12:53 +0000251** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000252** sum() or avg() aggregate computation.
253*/
254typedef struct SumCtx SumCtx;
255struct SumCtx {
256 double sum; /* Sum of terms */
257};
258
259/*
260** Routines used to compute the sum or average.
261*/
262static void sumStep(sqlite_func *context, int argc, const char **argv){
263 SumCtx *p;
264 double x;
265 if( argc<1 ) return;
266 p = sqlite_aggregate_context(context, sizeof(*p));
267 if( p==0 ) return;
268 x = argv[0] ? atof(argv[0]) : 0.0;
269 p->sum += x;
270}
271static void sumFinalize(sqlite_func *context){
272 SumCtx *p;
273 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000274 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000275}
276static void avgFinalize(sqlite_func *context){
277 SumCtx *p;
278 double rN;
279 p = sqlite_aggregate_context(context, sizeof(*p));
280 rN = sqlite_aggregate_count(context);
281 if( p && rN>0.0 ){
282 sqlite_set_result_double(context, p->sum/rN);
283 }
284}
285
286/*
287** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000288** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000289*/
290typedef struct StdDevCtx StdDevCtx;
291struct StdDevCtx {
292 double sum; /* Sum of terms */
293 double sum2; /* Sum of the squares of terms */
drhd3a149e2002-02-24 17:12:53 +0000294};
295
drhef2daf52002-03-04 02:26:15 +0000296#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000297/*
298** Routines used to compute the standard deviation as an aggregate.
299*/
drh1350b032002-02-27 19:00:20 +0000300static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000301 StdDevCtx *p;
302 double x;
drh1350b032002-02-27 19:00:20 +0000303 if( argc<1 ) return;
304 p = sqlite_aggregate_context(context, sizeof(*p));
305 if( p==0 ) return;
drhdd5baa92002-02-27 19:50:59 +0000306 x = argv[0] ? atof(argv[0]) : 0.0;
drhd3a149e2002-02-24 17:12:53 +0000307 p->sum += x;
308 p->sum2 += x*x;
drhd3a149e2002-02-24 17:12:53 +0000309}
drh1350b032002-02-27 19:00:20 +0000310static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000311 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000312 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drhdd5baa92002-02-27 19:50:59 +0000313 if( p && rN>1.0 ){
drhd3a149e2002-02-24 17:12:53 +0000314 sqlite_set_result_double(context,
315 sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
316 }
drhd3a149e2002-02-24 17:12:53 +0000317}
drhef2daf52002-03-04 02:26:15 +0000318#endif
drhd3a149e2002-02-24 17:12:53 +0000319
drh0bce8352002-02-28 00:41:10 +0000320/*
321** The following structure keeps track of state information for the
322** count() aggregate function.
323*/
324typedef struct CountCtx CountCtx;
325struct CountCtx {
326 int n;
327};
drhdd5baa92002-02-27 19:50:59 +0000328
drh0bce8352002-02-28 00:41:10 +0000329/*
330** Routines to implement the count() aggregate function.
331*/
332static void countStep(sqlite_func *context, int argc, const char **argv){
333 CountCtx *p;
334 p = sqlite_aggregate_context(context, sizeof(*p));
335 if( (argc==0 || argv[0]) && p ){
336 p->n++;
337 }
338}
339static void countFinalize(sqlite_func *context){
340 CountCtx *p;
341 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000342 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000343}
344
345/*
346** This function tracks state information for the min() and max()
347** aggregate functions.
348*/
349typedef struct MinMaxCtx MinMaxCtx;
350struct MinMaxCtx {
drh8912d102002-05-26 21:34:58 +0000351 int isNull; /* True if the result should be NULL */
drh0bce8352002-02-28 00:41:10 +0000352 char *z; /* The best so far */
353 char zBuf[28]; /* Space that can be used for storage */
354};
355
356/*
357** Routines to implement min() and max() aggregate functions.
358*/
359static void minStep(sqlite_func *context, int argc, const char **argv){
360 MinMaxCtx *p;
361 p = sqlite_aggregate_context(context, sizeof(*p));
362 if( p==0 || argc<1 ) return;
drh8912d102002-05-26 21:34:58 +0000363 if( argv[0]==0 || p->isNull ){
364 p->isNull = 1;
365 return;
366 }
drh0bce8352002-02-28 00:41:10 +0000367 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){
drh8912d102002-05-26 21:34:58 +0000368 int len;
drh0bce8352002-02-28 00:41:10 +0000369 if( p->z && p->z!=p->zBuf ){
370 sqliteFree(p->z);
371 }
drh8912d102002-05-26 21:34:58 +0000372 len = strlen(argv[0]);
373 if( len < sizeof(p->zBuf) ){
374 p->z = p->zBuf;
drh0bce8352002-02-28 00:41:10 +0000375 }else{
drh8912d102002-05-26 21:34:58 +0000376 p->z = sqliteMalloc( len+1 );
377 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000378 }
drh8912d102002-05-26 21:34:58 +0000379 strcpy(p->z, argv[0]);
drh0bce8352002-02-28 00:41:10 +0000380 }
381}
382static void maxStep(sqlite_func *context, int argc, const char **argv){
383 MinMaxCtx *p;
384 p = sqlite_aggregate_context(context, sizeof(*p));
385 if( p==0 || argc<1 ) return;
drh8912d102002-05-26 21:34:58 +0000386 if( argv[0]==0 || p->isNull ){
387 p->isNull = 1;
388 return;
389 }
drh0bce8352002-02-28 00:41:10 +0000390 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){
drh8912d102002-05-26 21:34:58 +0000391 int len;
drh0bce8352002-02-28 00:41:10 +0000392 if( p->z && p->z!=p->zBuf ){
393 sqliteFree(p->z);
394 }
drh8912d102002-05-26 21:34:58 +0000395 len = strlen(argv[0]);
396 if( len < sizeof(p->zBuf) ){
397 p->z = p->zBuf;
drh0bce8352002-02-28 00:41:10 +0000398 }else{
drh8912d102002-05-26 21:34:58 +0000399 p->z = sqliteMalloc( len+1 );
400 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000401 }
drh8912d102002-05-26 21:34:58 +0000402 strcpy(p->z, argv[0]);
drh0bce8352002-02-28 00:41:10 +0000403 }
404}
405static void minMaxFinalize(sqlite_func *context){
406 MinMaxCtx *p;
407 p = sqlite_aggregate_context(context, sizeof(*p));
drh8912d102002-05-26 21:34:58 +0000408 if( p && p->z && !p->isNull ){
drh0bce8352002-02-28 00:41:10 +0000409 sqlite_set_result_string(context, p->z, strlen(p->z));
410 }
411 if( p && p->z && p->z!=p->zBuf ){
412 sqliteFree(p->z);
413 }
414}
drhdd5baa92002-02-27 19:50:59 +0000415
drhd3a149e2002-02-24 17:12:53 +0000416/*
drha2ed5602002-02-26 23:55:31 +0000417** This function registered all of the above C functions as SQL
418** functions. This should be the only routine in this file with
419** external linkage.
drhdc04c582002-02-24 01:55:15 +0000420*/
421void sqliteRegisterBuildinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000422 static struct {
423 char *zName;
424 int nArg;
425 void (*xFunc)(sqlite_func*,int,const char**);
426 } aFuncs[] = {
drhfbc99082002-02-28 03:14:18 +0000427 { "min", -1, minFunc },
428 { "min", 0, 0 },
429 { "max", -1, maxFunc },
430 { "max", 0, 0 },
431 { "length", 1, lengthFunc },
432 { "substr", 3, substrFunc },
433 { "abs", 1, absFunc },
434 { "round", 1, roundFunc },
435 { "round", 2, roundFunc },
436 { "upper", 1, upperFunc },
437 { "lower", 1, lowerFunc },
438 { "coalesce", -1, ifnullFunc },
439 { "coalesce", 0, 0 },
440 { "coalesce", 1, 0 },
drhf9ffac92002-03-02 19:00:31 +0000441 { "random", -1, randomFunc },
drh0ac65892002-04-20 14:24:41 +0000442 { "like", 2, likeFunc },
443 { "glob", 2, globFunc },
drh8912d102002-05-26 21:34:58 +0000444 { "nullif", 2, nullifFunc },
drh0bce8352002-02-28 00:41:10 +0000445 };
446 static struct {
447 char *zName;
448 int nArg;
449 void (*xStep)(sqlite_func*,int,const char**);
450 void (*xFinalize)(sqlite_func*);
451 } aAggs[] = {
452 { "min", 1, minStep, minMaxFinalize },
453 { "max", 1, maxStep, minMaxFinalize },
454 { "sum", 1, sumStep, sumFinalize },
455 { "avg", 1, sumStep, avgFinalize },
456 { "count", 0, countStep, countFinalize },
457 { "count", 1, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000458#if 0
drh0bce8352002-02-28 00:41:10 +0000459 { "stddev", 1, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000460#endif
drh0bce8352002-02-28 00:41:10 +0000461 };
462 int i;
463
464 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
465 sqlite_create_function(db, aFuncs[i].zName,
466 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
467 }
drh6ed41ad2002-04-06 14:10:47 +0000468 sqlite_create_function(db, "last_insert_rowid", 0,
469 last_insert_rowid, db);
drh0bce8352002-02-28 00:41:10 +0000470 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
471 sqlite_create_aggregate(db, aAggs[i].zName,
472 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
473 }
drhdc04c582002-02-24 01:55:15 +0000474}