blob: c75a65beea01db88f312d9b6c0c164db3a6cd3f0 [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**
drh6ed41ad2002-04-06 14:10:47 +000019** $Id: func.c,v 1.15 2002/04/06 14:10:47 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];
36 for(i=1; i<argc; i++){
37 if( sqliteCompare(argv[i], zBest)<0 ){
38 zBest = argv[i];
39 }
40 }
41 sqlite_set_result_string(context, zBest, -1);
42}
43static void maxFunc(sqlite_func *context, int argc, const char **argv){
44 const char *zBest;
45 int i;
46
drh89425d52002-02-28 03:04:48 +000047 if( argc==0 ) return;
drh0bce8352002-02-28 00:41:10 +000048 zBest = argv[0];
49 for(i=1; i<argc; i++){
50 if( sqliteCompare(argv[i], zBest)>0 ){
51 zBest = argv[i];
52 }
53 }
54 sqlite_set_result_string(context, zBest, -1);
55}
56
57/*
58** Implementation of the length() function
59*/
60static void lengthFunc(sqlite_func *context, int argc, const char **argv){
61 const char *z;
62 int len;
63
64 assert( argc==1 );
65 z = argv[0];
66 if( z==0 ){
67 len = 0;
68 }else{
69#ifdef SQLITE_UTF8
70 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
71#else
72 len = strlen(z);
73#endif
74 }
75 sqlite_set_result_int(context, len);
76}
77
78/*
79** Implementation of the abs() function
80*/
81static void absFunc(sqlite_func *context, int argc, const char **argv){
82 const char *z;
83 assert( argc==1 );
84 z = argv[0];
85 if( z && z[0]=='-' && isdigit(z[1]) ) z++;
86 sqlite_set_result_string(context, z, -1);
87}
88
89/*
90** Implementation of the substr() function
91*/
92static void substrFunc(sqlite_func *context, int argc, const char **argv){
93 const char *z;
94#ifdef SQLITE_UTF8
95 const char *z2;
96 int i;
97#endif
98 int p1, p2, len;
99 assert( argc==3 );
100 z = argv[0];
101 if( z==0 ) return;
102 p1 = atoi(argv[1]?argv[1]:0);
103 p2 = atoi(argv[2]?argv[2]:0);
104#ifdef SQLITE_UTF8
drh47c8a672002-02-28 04:00:12 +0000105 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000106#else
107 len = strlen(z);
108#endif
109 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000110 p1 += len;
drh653bc752002-02-28 03:31:10 +0000111 if( p1<0 ){
112 p2 += p1;
113 p1 = 0;
114 }
drh0bce8352002-02-28 00:41:10 +0000115 }else if( p1>0 ){
116 p1--;
117 }
118 if( p1+p2>len ){
119 p2 = len-p1;
120 }
121#ifdef SQLITE_UTF8
122 for(i=0; i<p1; i++){
123 assert( z[i] );
drh47c8a672002-02-28 04:00:12 +0000124 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000125 }
drh47c8a672002-02-28 04:00:12 +0000126 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh0bce8352002-02-28 00:41:10 +0000127 for(; i<p1+p2; i++){
128 assert( z[i] );
drh47c8a672002-02-28 04:00:12 +0000129 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000130 }
drh47c8a672002-02-28 04:00:12 +0000131 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh0bce8352002-02-28 00:41:10 +0000132#endif
drh653bc752002-02-28 03:31:10 +0000133 if( p2<0 ) p2 = 0;
drh0bce8352002-02-28 00:41:10 +0000134 sqlite_set_result_string(context, &z[p1], p2);
135}
136
137/*
138** Implementation of the round() function
139*/
140static void roundFunc(sqlite_func *context, int argc, const char **argv){
141 int n;
142 double r;
143 char zBuf[100];
144 assert( argc==1 || argc==2 );
145 n = argc==2 && argv[1] ? atoi(argv[1]) : 0;
146 if( n>30 ) n = 30;
147 if( n<0 ) n = 0;
148 r = argv[0] ? atof(argv[0]) : 0.0;
149 sprintf(zBuf,"%.*f",n,r);
150 sqlite_set_result_string(context, zBuf, -1);
151}
drhdc04c582002-02-24 01:55:15 +0000152
153/*
154** Implementation of the upper() and lower() SQL functions.
155*/
drh1350b032002-02-27 19:00:20 +0000156static void upperFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000157 char *z;
158 int i;
159 if( argc<1 || argv[0]==0 ) return;
160 z = sqlite_set_result_string(context, argv[0], -1);
161 if( z==0 ) return;
162 for(i=0; z[i]; i++){
163 if( islower(z[i]) ) z[i] = toupper(z[i]);
164 }
165}
drh1350b032002-02-27 19:00:20 +0000166static void lowerFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000167 char *z;
168 int i;
169 if( argc<1 || argv[0]==0 ) return;
170 z = sqlite_set_result_string(context, argv[0], -1);
171 if( z==0 ) return;
172 for(i=0; z[i]; i++){
173 if( isupper(z[i]) ) z[i] = tolower(z[i]);
174 }
175}
176
177/*
drhfbc99082002-02-28 03:14:18 +0000178** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
179** All three do the same thing. They return the first argument
180** non-NULL argument.
drh3212e182002-02-28 00:46:26 +0000181*/
182static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
drhfbc99082002-02-28 03:14:18 +0000183 int i;
184 for(i=0; i<argc; i++){
185 if( argv[i] ){
186 sqlite_set_result_string(context, argv[i], -1);
187 break;
188 }
189 }
drh3212e182002-02-28 00:46:26 +0000190}
191
192/*
drhf9ffac92002-03-02 19:00:31 +0000193** Implementation of random(). Return a random integer.
194*/
195static void randomFunc(sqlite_func *context, int argc, const char **argv){
196 sqlite_set_result_int(context, sqliteRandomInteger());
197}
198
199/*
drh6ed41ad2002-04-06 14:10:47 +0000200** Implementation of the last_insert_rowid() SQL function. The return
201** value is the same as the sqlite_last_insert_rowid() API function.
202*/
203static void last_insert_rowid(sqlite_func *context, int arg, char **argv){
204 sqlite *db = sqlite_user_data(context);
205 sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
206}
207
208/*
drhd3a149e2002-02-24 17:12:53 +0000209** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000210** sum() or avg() aggregate computation.
211*/
212typedef struct SumCtx SumCtx;
213struct SumCtx {
214 double sum; /* Sum of terms */
215};
216
217/*
218** Routines used to compute the sum or average.
219*/
220static void sumStep(sqlite_func *context, int argc, const char **argv){
221 SumCtx *p;
222 double x;
223 if( argc<1 ) return;
224 p = sqlite_aggregate_context(context, sizeof(*p));
225 if( p==0 ) return;
226 x = argv[0] ? atof(argv[0]) : 0.0;
227 p->sum += x;
228}
229static void sumFinalize(sqlite_func *context){
230 SumCtx *p;
231 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000232 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000233}
234static void avgFinalize(sqlite_func *context){
235 SumCtx *p;
236 double rN;
237 p = sqlite_aggregate_context(context, sizeof(*p));
238 rN = sqlite_aggregate_count(context);
239 if( p && rN>0.0 ){
240 sqlite_set_result_double(context, p->sum/rN);
241 }
242}
243
244/*
245** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000246** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000247*/
248typedef struct StdDevCtx StdDevCtx;
249struct StdDevCtx {
250 double sum; /* Sum of terms */
251 double sum2; /* Sum of the squares of terms */
drhd3a149e2002-02-24 17:12:53 +0000252};
253
drhef2daf52002-03-04 02:26:15 +0000254#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000255/*
256** Routines used to compute the standard deviation as an aggregate.
257*/
drh1350b032002-02-27 19:00:20 +0000258static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000259 StdDevCtx *p;
260 double x;
drh1350b032002-02-27 19:00:20 +0000261 if( argc<1 ) return;
262 p = sqlite_aggregate_context(context, sizeof(*p));
263 if( p==0 ) return;
drhdd5baa92002-02-27 19:50:59 +0000264 x = argv[0] ? atof(argv[0]) : 0.0;
drhd3a149e2002-02-24 17:12:53 +0000265 p->sum += x;
266 p->sum2 += x*x;
drhd3a149e2002-02-24 17:12:53 +0000267}
drh1350b032002-02-27 19:00:20 +0000268static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000269 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000270 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drhdd5baa92002-02-27 19:50:59 +0000271 if( p && rN>1.0 ){
drhd3a149e2002-02-24 17:12:53 +0000272 sqlite_set_result_double(context,
273 sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
274 }
drhd3a149e2002-02-24 17:12:53 +0000275}
drhef2daf52002-03-04 02:26:15 +0000276#endif
drhd3a149e2002-02-24 17:12:53 +0000277
drh0bce8352002-02-28 00:41:10 +0000278/*
279** The following structure keeps track of state information for the
280** count() aggregate function.
281*/
282typedef struct CountCtx CountCtx;
283struct CountCtx {
284 int n;
285};
drhdd5baa92002-02-27 19:50:59 +0000286
drh0bce8352002-02-28 00:41:10 +0000287/*
288** Routines to implement the count() aggregate function.
289*/
290static void countStep(sqlite_func *context, int argc, const char **argv){
291 CountCtx *p;
292 p = sqlite_aggregate_context(context, sizeof(*p));
293 if( (argc==0 || argv[0]) && p ){
294 p->n++;
295 }
296}
297static void countFinalize(sqlite_func *context){
298 CountCtx *p;
299 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000300 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000301}
302
303/*
304** This function tracks state information for the min() and max()
305** aggregate functions.
306*/
307typedef struct MinMaxCtx MinMaxCtx;
308struct MinMaxCtx {
309 char *z; /* The best so far */
310 char zBuf[28]; /* Space that can be used for storage */
311};
312
313/*
314** Routines to implement min() and max() aggregate functions.
315*/
316static void minStep(sqlite_func *context, int argc, const char **argv){
317 MinMaxCtx *p;
318 p = sqlite_aggregate_context(context, sizeof(*p));
319 if( p==0 || argc<1 ) return;
320 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){
321 if( p->z && p->z!=p->zBuf ){
322 sqliteFree(p->z);
323 }
324 if( argv[0] ){
325 int len = strlen(argv[0]);
326 if( len < sizeof(p->zBuf) ){
327 p->z = p->zBuf;
328 }else{
329 p->z = sqliteMalloc( len+1 );
330 if( p->z==0 ) return;
331 }
332 strcpy(p->z, argv[0]);
333 }else{
334 p->z = 0;
335 }
336 }
337}
338static void maxStep(sqlite_func *context, int argc, const char **argv){
339 MinMaxCtx *p;
340 p = sqlite_aggregate_context(context, sizeof(*p));
341 if( p==0 || argc<1 ) return;
342 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){
343 if( p->z && p->z!=p->zBuf ){
344 sqliteFree(p->z);
345 }
346 if( argv[0] ){
347 int len = strlen(argv[0]);
348 if( len < sizeof(p->zBuf) ){
349 p->z = p->zBuf;
350 }else{
351 p->z = sqliteMalloc( len+1 );
352 if( p->z==0 ) return;
353 }
354 strcpy(p->z, argv[0]);
355 }else{
356 p->z = 0;
357 }
358 }
359}
360static void minMaxFinalize(sqlite_func *context){
361 MinMaxCtx *p;
362 p = sqlite_aggregate_context(context, sizeof(*p));
363 if( p && p->z ){
364 sqlite_set_result_string(context, p->z, strlen(p->z));
365 }
366 if( p && p->z && p->z!=p->zBuf ){
367 sqliteFree(p->z);
368 }
369}
drhdd5baa92002-02-27 19:50:59 +0000370
drhd3a149e2002-02-24 17:12:53 +0000371/*
drha2ed5602002-02-26 23:55:31 +0000372** This function registered all of the above C functions as SQL
373** functions. This should be the only routine in this file with
374** external linkage.
drhdc04c582002-02-24 01:55:15 +0000375*/
376void sqliteRegisterBuildinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000377 static struct {
378 char *zName;
379 int nArg;
380 void (*xFunc)(sqlite_func*,int,const char**);
381 } aFuncs[] = {
drhfbc99082002-02-28 03:14:18 +0000382 { "min", -1, minFunc },
383 { "min", 0, 0 },
384 { "max", -1, maxFunc },
385 { "max", 0, 0 },
386 { "length", 1, lengthFunc },
387 { "substr", 3, substrFunc },
388 { "abs", 1, absFunc },
389 { "round", 1, roundFunc },
390 { "round", 2, roundFunc },
391 { "upper", 1, upperFunc },
392 { "lower", 1, lowerFunc },
393 { "coalesce", -1, ifnullFunc },
394 { "coalesce", 0, 0 },
395 { "coalesce", 1, 0 },
drhf9ffac92002-03-02 19:00:31 +0000396 { "random", -1, randomFunc },
drh0bce8352002-02-28 00:41:10 +0000397 };
398 static struct {
399 char *zName;
400 int nArg;
401 void (*xStep)(sqlite_func*,int,const char**);
402 void (*xFinalize)(sqlite_func*);
403 } aAggs[] = {
404 { "min", 1, minStep, minMaxFinalize },
405 { "max", 1, maxStep, minMaxFinalize },
406 { "sum", 1, sumStep, sumFinalize },
407 { "avg", 1, sumStep, avgFinalize },
408 { "count", 0, countStep, countFinalize },
409 { "count", 1, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000410#if 0
drh0bce8352002-02-28 00:41:10 +0000411 { "stddev", 1, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000412#endif
drh0bce8352002-02-28 00:41:10 +0000413 };
414 int i;
415
416 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
417 sqlite_create_function(db, aFuncs[i].zName,
418 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
419 }
drh6ed41ad2002-04-06 14:10:47 +0000420 sqlite_create_function(db, "last_insert_rowid", 0,
421 last_insert_rowid, db);
drh0bce8352002-02-28 00:41:10 +0000422 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
423 sqlite_create_aggregate(db, aAggs[i].zName,
424 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
425 }
drhdc04c582002-02-24 01:55:15 +0000426}