blob: b7464146fa66f93620621f87ef602f40815f1f14 [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**
drh89425d52002-02-28 03:04:48 +000019** $Id: func.c,v 1.9 2002/02/28 03:04:48 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
105 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z)!=0x80 ) len++; }
106#else
107 len = strlen(z);
108#endif
109 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000110 p1 += len;
drh0bce8352002-02-28 00:41:10 +0000111 }else if( p1>0 ){
112 p1--;
113 }
114 if( p1+p2>len ){
115 p2 = len-p1;
116 }
117#ifdef SQLITE_UTF8
118 for(i=0; i<p1; i++){
119 assert( z[i] );
120 if( (z[i]&0xc0)!=0x80 ) p1++;
121 }
122 for(; i<p1+p2; i++){
123 assert( z[i] );
124 if( (z[i]&0xc0)!=0x80 ) p2++;
125 }
126#endif
127 sqlite_set_result_string(context, &z[p1], p2);
128}
129
130/*
131** Implementation of the round() function
132*/
133static void roundFunc(sqlite_func *context, int argc, const char **argv){
134 int n;
135 double r;
136 char zBuf[100];
137 assert( argc==1 || argc==2 );
138 n = argc==2 && argv[1] ? atoi(argv[1]) : 0;
139 if( n>30 ) n = 30;
140 if( n<0 ) n = 0;
141 r = argv[0] ? atof(argv[0]) : 0.0;
142 sprintf(zBuf,"%.*f",n,r);
143 sqlite_set_result_string(context, zBuf, -1);
144}
drhdc04c582002-02-24 01:55:15 +0000145
146/*
147** Implementation of the upper() and lower() SQL functions.
148*/
drh1350b032002-02-27 19:00:20 +0000149static void upperFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000150 char *z;
151 int i;
152 if( argc<1 || argv[0]==0 ) return;
153 z = sqlite_set_result_string(context, argv[0], -1);
154 if( z==0 ) return;
155 for(i=0; z[i]; i++){
156 if( islower(z[i]) ) z[i] = toupper(z[i]);
157 }
158}
drh1350b032002-02-27 19:00:20 +0000159static void lowerFunc(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( isupper(z[i]) ) z[i] = tolower(z[i]);
167 }
168}
169
170/*
drh3212e182002-02-28 00:46:26 +0000171** Implementation of the IFNULL() and NVL() functions. (both do the
172** same thing. They return their first argument if it is not NULL or
173** their second argument if the first is NULL.
174*/
175static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
176 const char *z;
177 assert( argc==2 );
178 z = argv[0] ? argv[0] : argv[1];
179 sqlite_set_result_string(context, z, -1);
180}
181
182/*
drhd3a149e2002-02-24 17:12:53 +0000183** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000184** sum() or avg() aggregate computation.
185*/
186typedef struct SumCtx SumCtx;
187struct SumCtx {
188 double sum; /* Sum of terms */
189};
190
191/*
192** Routines used to compute the sum or average.
193*/
194static void sumStep(sqlite_func *context, int argc, const char **argv){
195 SumCtx *p;
196 double x;
197 if( argc<1 ) return;
198 p = sqlite_aggregate_context(context, sizeof(*p));
199 if( p==0 ) return;
200 x = argv[0] ? atof(argv[0]) : 0.0;
201 p->sum += x;
202}
203static void sumFinalize(sqlite_func *context){
204 SumCtx *p;
205 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000206 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000207}
208static void avgFinalize(sqlite_func *context){
209 SumCtx *p;
210 double rN;
211 p = sqlite_aggregate_context(context, sizeof(*p));
212 rN = sqlite_aggregate_count(context);
213 if( p && rN>0.0 ){
214 sqlite_set_result_double(context, p->sum/rN);
215 }
216}
217
218/*
219** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000220** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000221*/
222typedef struct StdDevCtx StdDevCtx;
223struct StdDevCtx {
224 double sum; /* Sum of terms */
225 double sum2; /* Sum of the squares of terms */
drhd3a149e2002-02-24 17:12:53 +0000226};
227
228/*
229** Routines used to compute the standard deviation as an aggregate.
230*/
drh1350b032002-02-27 19:00:20 +0000231static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000232 StdDevCtx *p;
233 double x;
drh1350b032002-02-27 19:00:20 +0000234 if( argc<1 ) return;
235 p = sqlite_aggregate_context(context, sizeof(*p));
236 if( p==0 ) return;
drhdd5baa92002-02-27 19:50:59 +0000237 x = argv[0] ? atof(argv[0]) : 0.0;
drhd3a149e2002-02-24 17:12:53 +0000238 p->sum += x;
239 p->sum2 += x*x;
drhd3a149e2002-02-24 17:12:53 +0000240}
drh1350b032002-02-27 19:00:20 +0000241static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000242 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000243 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drhdd5baa92002-02-27 19:50:59 +0000244 if( p && rN>1.0 ){
drhd3a149e2002-02-24 17:12:53 +0000245 sqlite_set_result_double(context,
246 sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
247 }
drhd3a149e2002-02-24 17:12:53 +0000248}
249
drh0bce8352002-02-28 00:41:10 +0000250/*
251** The following structure keeps track of state information for the
252** count() aggregate function.
253*/
254typedef struct CountCtx CountCtx;
255struct CountCtx {
256 int n;
257};
drhdd5baa92002-02-27 19:50:59 +0000258
drh0bce8352002-02-28 00:41:10 +0000259/*
260** Routines to implement the count() aggregate function.
261*/
262static void countStep(sqlite_func *context, int argc, const char **argv){
263 CountCtx *p;
264 p = sqlite_aggregate_context(context, sizeof(*p));
265 if( (argc==0 || argv[0]) && p ){
266 p->n++;
267 }
268}
269static void countFinalize(sqlite_func *context){
270 CountCtx *p;
271 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000272 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000273}
274
275/*
276** This function tracks state information for the min() and max()
277** aggregate functions.
278*/
279typedef struct MinMaxCtx MinMaxCtx;
280struct MinMaxCtx {
281 char *z; /* The best so far */
282 char zBuf[28]; /* Space that can be used for storage */
283};
284
285/*
286** Routines to implement min() and max() aggregate functions.
287*/
288static void minStep(sqlite_func *context, int argc, const char **argv){
289 MinMaxCtx *p;
290 p = sqlite_aggregate_context(context, sizeof(*p));
291 if( p==0 || argc<1 ) return;
292 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){
293 if( p->z && p->z!=p->zBuf ){
294 sqliteFree(p->z);
295 }
296 if( argv[0] ){
297 int len = strlen(argv[0]);
298 if( len < sizeof(p->zBuf) ){
299 p->z = p->zBuf;
300 }else{
301 p->z = sqliteMalloc( len+1 );
302 if( p->z==0 ) return;
303 }
304 strcpy(p->z, argv[0]);
305 }else{
306 p->z = 0;
307 }
308 }
309}
310static void maxStep(sqlite_func *context, int argc, const char **argv){
311 MinMaxCtx *p;
312 p = sqlite_aggregate_context(context, sizeof(*p));
313 if( p==0 || argc<1 ) return;
314 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){
315 if( p->z && p->z!=p->zBuf ){
316 sqliteFree(p->z);
317 }
318 if( argv[0] ){
319 int len = strlen(argv[0]);
320 if( len < sizeof(p->zBuf) ){
321 p->z = p->zBuf;
322 }else{
323 p->z = sqliteMalloc( len+1 );
324 if( p->z==0 ) return;
325 }
326 strcpy(p->z, argv[0]);
327 }else{
328 p->z = 0;
329 }
330 }
331}
332static void minMaxFinalize(sqlite_func *context){
333 MinMaxCtx *p;
334 p = sqlite_aggregate_context(context, sizeof(*p));
335 if( p && p->z ){
336 sqlite_set_result_string(context, p->z, strlen(p->z));
337 }
338 if( p && p->z && p->z!=p->zBuf ){
339 sqliteFree(p->z);
340 }
341}
drhdd5baa92002-02-27 19:50:59 +0000342
drhd3a149e2002-02-24 17:12:53 +0000343/*
drha2ed5602002-02-26 23:55:31 +0000344** This function registered all of the above C functions as SQL
345** functions. This should be the only routine in this file with
346** external linkage.
drhdc04c582002-02-24 01:55:15 +0000347*/
348void sqliteRegisterBuildinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000349 static struct {
350 char *zName;
351 int nArg;
352 void (*xFunc)(sqlite_func*,int,const char**);
353 } aFuncs[] = {
354 { "min", -1, minFunc },
355 { "max", -1, maxFunc },
356 { "length", 1, lengthFunc },
357 { "substr", 3, substrFunc },
358 { "abs", 1, absFunc },
359 { "round", 1, roundFunc },
360 { "round", 2, roundFunc },
361 { "upper", 1, upperFunc },
362 { "lower", 1, lowerFunc },
drh3212e182002-02-28 00:46:26 +0000363 { "ifnull", 2, ifnullFunc },
364 { "nvl", 2, ifnullFunc },
drh0bce8352002-02-28 00:41:10 +0000365 };
366 static struct {
367 char *zName;
368 int nArg;
369 void (*xStep)(sqlite_func*,int,const char**);
370 void (*xFinalize)(sqlite_func*);
371 } aAggs[] = {
372 { "min", 1, minStep, minMaxFinalize },
373 { "max", 1, maxStep, minMaxFinalize },
374 { "sum", 1, sumStep, sumFinalize },
375 { "avg", 1, sumStep, avgFinalize },
376 { "count", 0, countStep, countFinalize },
377 { "count", 1, countStep, countFinalize },
378 { "stddev", 1, stdDevStep, stdDevFinalize },
379 };
380 int i;
381
382 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
383 sqlite_create_function(db, aFuncs[i].zName,
384 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
385 }
386 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
387 sqlite_create_aggregate(db, aAggs[i].zName,
388 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
389 }
drhdc04c582002-02-24 01:55:15 +0000390}