blob: 007a3db95ac09c2b30657f656011ac9d936e5167 [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**
drh653bc752002-02-28 03:31:10 +000019** $Id: func.c,v 1.11 2002/02/28 03:31:11 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;
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] );
124 if( (z[i]&0xc0)!=0x80 ) p1++;
125 }
126 for(; i<p1+p2; i++){
127 assert( z[i] );
128 if( (z[i]&0xc0)!=0x80 ) p2++;
129 }
130#endif
drh653bc752002-02-28 03:31:10 +0000131 if( p2<0 ) p2 = 0;
drh0bce8352002-02-28 00:41:10 +0000132 sqlite_set_result_string(context, &z[p1], p2);
133}
134
135/*
136** Implementation of the round() function
137*/
138static void roundFunc(sqlite_func *context, int argc, const char **argv){
139 int n;
140 double r;
141 char zBuf[100];
142 assert( argc==1 || argc==2 );
143 n = argc==2 && argv[1] ? atoi(argv[1]) : 0;
144 if( n>30 ) n = 30;
145 if( n<0 ) n = 0;
146 r = argv[0] ? atof(argv[0]) : 0.0;
147 sprintf(zBuf,"%.*f",n,r);
148 sqlite_set_result_string(context, zBuf, -1);
149}
drhdc04c582002-02-24 01:55:15 +0000150
151/*
152** Implementation of the upper() and lower() SQL functions.
153*/
drh1350b032002-02-27 19:00:20 +0000154static void upperFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000155 char *z;
156 int i;
157 if( argc<1 || argv[0]==0 ) return;
158 z = sqlite_set_result_string(context, argv[0], -1);
159 if( z==0 ) return;
160 for(i=0; z[i]; i++){
161 if( islower(z[i]) ) z[i] = toupper(z[i]);
162 }
163}
drh1350b032002-02-27 19:00:20 +0000164static void lowerFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000165 char *z;
166 int i;
167 if( argc<1 || argv[0]==0 ) return;
168 z = sqlite_set_result_string(context, argv[0], -1);
169 if( z==0 ) return;
170 for(i=0; z[i]; i++){
171 if( isupper(z[i]) ) z[i] = tolower(z[i]);
172 }
173}
174
175/*
drhfbc99082002-02-28 03:14:18 +0000176** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
177** All three do the same thing. They return the first argument
178** non-NULL argument.
drh3212e182002-02-28 00:46:26 +0000179*/
180static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
drhfbc99082002-02-28 03:14:18 +0000181 int i;
182 for(i=0; i<argc; i++){
183 if( argv[i] ){
184 sqlite_set_result_string(context, argv[i], -1);
185 break;
186 }
187 }
drh3212e182002-02-28 00:46:26 +0000188}
189
190/*
drhd3a149e2002-02-24 17:12:53 +0000191** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000192** sum() or avg() aggregate computation.
193*/
194typedef struct SumCtx SumCtx;
195struct SumCtx {
196 double sum; /* Sum of terms */
197};
198
199/*
200** Routines used to compute the sum or average.
201*/
202static void sumStep(sqlite_func *context, int argc, const char **argv){
203 SumCtx *p;
204 double x;
205 if( argc<1 ) return;
206 p = sqlite_aggregate_context(context, sizeof(*p));
207 if( p==0 ) return;
208 x = argv[0] ? atof(argv[0]) : 0.0;
209 p->sum += x;
210}
211static void sumFinalize(sqlite_func *context){
212 SumCtx *p;
213 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000214 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000215}
216static void avgFinalize(sqlite_func *context){
217 SumCtx *p;
218 double rN;
219 p = sqlite_aggregate_context(context, sizeof(*p));
220 rN = sqlite_aggregate_count(context);
221 if( p && rN>0.0 ){
222 sqlite_set_result_double(context, p->sum/rN);
223 }
224}
225
226/*
227** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000228** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000229*/
230typedef struct StdDevCtx StdDevCtx;
231struct StdDevCtx {
232 double sum; /* Sum of terms */
233 double sum2; /* Sum of the squares of terms */
drhd3a149e2002-02-24 17:12:53 +0000234};
235
236/*
237** Routines used to compute the standard deviation as an aggregate.
238*/
drh1350b032002-02-27 19:00:20 +0000239static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000240 StdDevCtx *p;
241 double x;
drh1350b032002-02-27 19:00:20 +0000242 if( argc<1 ) return;
243 p = sqlite_aggregate_context(context, sizeof(*p));
244 if( p==0 ) return;
drhdd5baa92002-02-27 19:50:59 +0000245 x = argv[0] ? atof(argv[0]) : 0.0;
drhd3a149e2002-02-24 17:12:53 +0000246 p->sum += x;
247 p->sum2 += x*x;
drhd3a149e2002-02-24 17:12:53 +0000248}
drh1350b032002-02-27 19:00:20 +0000249static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000250 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000251 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drhdd5baa92002-02-27 19:50:59 +0000252 if( p && rN>1.0 ){
drhd3a149e2002-02-24 17:12:53 +0000253 sqlite_set_result_double(context,
254 sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
255 }
drhd3a149e2002-02-24 17:12:53 +0000256}
257
drh0bce8352002-02-28 00:41:10 +0000258/*
259** The following structure keeps track of state information for the
260** count() aggregate function.
261*/
262typedef struct CountCtx CountCtx;
263struct CountCtx {
264 int n;
265};
drhdd5baa92002-02-27 19:50:59 +0000266
drh0bce8352002-02-28 00:41:10 +0000267/*
268** Routines to implement the count() aggregate function.
269*/
270static void countStep(sqlite_func *context, int argc, const char **argv){
271 CountCtx *p;
272 p = sqlite_aggregate_context(context, sizeof(*p));
273 if( (argc==0 || argv[0]) && p ){
274 p->n++;
275 }
276}
277static void countFinalize(sqlite_func *context){
278 CountCtx *p;
279 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000280 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000281}
282
283/*
284** This function tracks state information for the min() and max()
285** aggregate functions.
286*/
287typedef struct MinMaxCtx MinMaxCtx;
288struct MinMaxCtx {
289 char *z; /* The best so far */
290 char zBuf[28]; /* Space that can be used for storage */
291};
292
293/*
294** Routines to implement min() and max() aggregate functions.
295*/
296static void minStep(sqlite_func *context, int argc, const char **argv){
297 MinMaxCtx *p;
298 p = sqlite_aggregate_context(context, sizeof(*p));
299 if( p==0 || argc<1 ) return;
300 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){
301 if( p->z && p->z!=p->zBuf ){
302 sqliteFree(p->z);
303 }
304 if( argv[0] ){
305 int len = strlen(argv[0]);
306 if( len < sizeof(p->zBuf) ){
307 p->z = p->zBuf;
308 }else{
309 p->z = sqliteMalloc( len+1 );
310 if( p->z==0 ) return;
311 }
312 strcpy(p->z, argv[0]);
313 }else{
314 p->z = 0;
315 }
316 }
317}
318static void maxStep(sqlite_func *context, int argc, const char **argv){
319 MinMaxCtx *p;
320 p = sqlite_aggregate_context(context, sizeof(*p));
321 if( p==0 || argc<1 ) return;
322 if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){
323 if( p->z && p->z!=p->zBuf ){
324 sqliteFree(p->z);
325 }
326 if( argv[0] ){
327 int len = strlen(argv[0]);
328 if( len < sizeof(p->zBuf) ){
329 p->z = p->zBuf;
330 }else{
331 p->z = sqliteMalloc( len+1 );
332 if( p->z==0 ) return;
333 }
334 strcpy(p->z, argv[0]);
335 }else{
336 p->z = 0;
337 }
338 }
339}
340static void minMaxFinalize(sqlite_func *context){
341 MinMaxCtx *p;
342 p = sqlite_aggregate_context(context, sizeof(*p));
343 if( p && p->z ){
344 sqlite_set_result_string(context, p->z, strlen(p->z));
345 }
346 if( p && p->z && p->z!=p->zBuf ){
347 sqliteFree(p->z);
348 }
349}
drhdd5baa92002-02-27 19:50:59 +0000350
drhd3a149e2002-02-24 17:12:53 +0000351/*
drha2ed5602002-02-26 23:55:31 +0000352** This function registered all of the above C functions as SQL
353** functions. This should be the only routine in this file with
354** external linkage.
drhdc04c582002-02-24 01:55:15 +0000355*/
356void sqliteRegisterBuildinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000357 static struct {
358 char *zName;
359 int nArg;
360 void (*xFunc)(sqlite_func*,int,const char**);
361 } aFuncs[] = {
drhfbc99082002-02-28 03:14:18 +0000362 { "min", -1, minFunc },
363 { "min", 0, 0 },
364 { "max", -1, maxFunc },
365 { "max", 0, 0 },
366 { "length", 1, lengthFunc },
367 { "substr", 3, substrFunc },
368 { "abs", 1, absFunc },
369 { "round", 1, roundFunc },
370 { "round", 2, roundFunc },
371 { "upper", 1, upperFunc },
372 { "lower", 1, lowerFunc },
373 { "coalesce", -1, ifnullFunc },
374 { "coalesce", 0, 0 },
375 { "coalesce", 1, 0 },
376
drh0bce8352002-02-28 00:41:10 +0000377 };
378 static struct {
379 char *zName;
380 int nArg;
381 void (*xStep)(sqlite_func*,int,const char**);
382 void (*xFinalize)(sqlite_func*);
383 } aAggs[] = {
384 { "min", 1, minStep, minMaxFinalize },
385 { "max", 1, maxStep, minMaxFinalize },
386 { "sum", 1, sumStep, sumFinalize },
387 { "avg", 1, sumStep, avgFinalize },
388 { "count", 0, countStep, countFinalize },
389 { "count", 1, countStep, countFinalize },
390 { "stddev", 1, stdDevStep, stdDevFinalize },
391 };
392 int i;
393
394 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
395 sqlite_create_function(db, aFuncs[i].zName,
396 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
397 }
398 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
399 sqlite_create_aggregate(db, aAggs[i].zName,
400 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
401 }
drhdc04c582002-02-24 01:55:15 +0000402}