blob: 688f50cff1b76ac7ae7c5aa413a3a465130785d1 [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**
drhbbd82df2004-02-11 09:46:30 +000019** $Id: func.c,v 1.39 2004/02/11 09:46: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*/
31static void minFunc(sqlite_func *context, int argc, const char **argv){
32 const char *zBest;
33 int i;
34
drh89425d52002-02-28 03:04:48 +000035 if( argc==0 ) return;
drh0bce8352002-02-28 00:41:10 +000036 zBest = argv[0];
drh8912d102002-05-26 21:34:58 +000037 if( zBest==0 ) return;
drh0bce8352002-02-28 00:41:10 +000038 for(i=1; i<argc; i++){
drh8912d102002-05-26 21:34:58 +000039 if( argv[i]==0 ) return;
drh0bce8352002-02-28 00:41:10 +000040 if( sqliteCompare(argv[i], zBest)<0 ){
41 zBest = argv[i];
42 }
43 }
44 sqlite_set_result_string(context, zBest, -1);
45}
46static void maxFunc(sqlite_func *context, int argc, const char **argv){
47 const char *zBest;
48 int i;
49
drh89425d52002-02-28 03:04:48 +000050 if( argc==0 ) return;
drh0bce8352002-02-28 00:41:10 +000051 zBest = argv[0];
drh8912d102002-05-26 21:34:58 +000052 if( zBest==0 ) return;
drh0bce8352002-02-28 00:41:10 +000053 for(i=1; i<argc; i++){
drh8912d102002-05-26 21:34:58 +000054 if( argv[i]==0 ) return;
drh0bce8352002-02-28 00:41:10 +000055 if( sqliteCompare(argv[i], zBest)>0 ){
56 zBest = argv[i];
57 }
58 }
59 sqlite_set_result_string(context, zBest, -1);
60}
61
62/*
63** Implementation of the length() function
64*/
65static void lengthFunc(sqlite_func *context, int argc, const char **argv){
66 const char *z;
67 int len;
68
69 assert( argc==1 );
70 z = argv[0];
drh8912d102002-05-26 21:34:58 +000071 if( z==0 ) return;
drh0bce8352002-02-28 00:41:10 +000072#ifdef SQLITE_UTF8
drh8912d102002-05-26 21:34:58 +000073 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +000074#else
drh8912d102002-05-26 21:34:58 +000075 len = strlen(z);
drh0bce8352002-02-28 00:41:10 +000076#endif
drh0bce8352002-02-28 00:41:10 +000077 sqlite_set_result_int(context, len);
78}
79
80/*
81** Implementation of the abs() function
82*/
83static void absFunc(sqlite_func *context, int argc, const char **argv){
84 const char *z;
85 assert( argc==1 );
86 z = argv[0];
drh8912d102002-05-26 21:34:58 +000087 if( z==0 ) return;
88 if( z[0]=='-' && isdigit(z[1]) ) z++;
drh0bce8352002-02-28 00:41:10 +000089 sqlite_set_result_string(context, z, -1);
90}
91
92/*
93** Implementation of the substr() function
94*/
95static void substrFunc(sqlite_func *context, int argc, const char **argv){
96 const char *z;
97#ifdef SQLITE_UTF8
98 const char *z2;
99 int i;
100#endif
101 int p1, p2, len;
102 assert( argc==3 );
103 z = argv[0];
104 if( z==0 ) return;
105 p1 = atoi(argv[1]?argv[1]:0);
106 p2 = atoi(argv[2]?argv[2]:0);
107#ifdef SQLITE_UTF8
drh47c8a672002-02-28 04:00:12 +0000108 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000109#else
110 len = strlen(z);
111#endif
112 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000113 p1 += len;
drh653bc752002-02-28 03:31:10 +0000114 if( p1<0 ){
115 p2 += p1;
116 p1 = 0;
117 }
drh0bce8352002-02-28 00:41:10 +0000118 }else if( p1>0 ){
119 p1--;
120 }
121 if( p1+p2>len ){
122 p2 = len-p1;
123 }
124#ifdef SQLITE_UTF8
drh77396302004-01-02 13:17:48 +0000125 for(i=0; i<p1 && z[i]; 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++; }
drh77396302004-01-02 13:17:48 +0000129 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000130 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000131 }
drh47c8a672002-02-28 04:00:12 +0000132 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh0bce8352002-02-28 00:41:10 +0000133#endif
drh653bc752002-02-28 03:31:10 +0000134 if( p2<0 ) p2 = 0;
drh0bce8352002-02-28 00:41:10 +0000135 sqlite_set_result_string(context, &z[p1], p2);
136}
137
138/*
139** Implementation of the round() function
140*/
141static void roundFunc(sqlite_func *context, int argc, const char **argv){
142 int n;
143 double r;
144 char zBuf[100];
145 assert( argc==1 || argc==2 );
drh8912d102002-05-26 21:34:58 +0000146 if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
147 n = argc==2 ? atoi(argv[1]) : 0;
drh0bce8352002-02-28 00:41:10 +0000148 if( n>30 ) n = 30;
149 if( n<0 ) n = 0;
drh93a5c6b2003-12-23 02:17:35 +0000150 r = sqliteAtoF(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000151 sprintf(zBuf,"%.*f",n,r);
152 sqlite_set_result_string(context, zBuf, -1);
153}
drhdc04c582002-02-24 01:55:15 +0000154
155/*
156** Implementation of the upper() and lower() SQL functions.
157*/
drh1350b032002-02-27 19:00:20 +0000158static void upperFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000159 char *z;
160 int i;
161 if( argc<1 || argv[0]==0 ) return;
162 z = sqlite_set_result_string(context, argv[0], -1);
163 if( z==0 ) return;
164 for(i=0; z[i]; i++){
165 if( islower(z[i]) ) z[i] = toupper(z[i]);
166 }
167}
drh1350b032002-02-27 19:00:20 +0000168static void lowerFunc(sqlite_func *context, int argc, const char **argv){
drhdc04c582002-02-24 01:55:15 +0000169 char *z;
170 int i;
171 if( argc<1 || argv[0]==0 ) return;
172 z = sqlite_set_result_string(context, argv[0], -1);
173 if( z==0 ) return;
174 for(i=0; z[i]; i++){
175 if( isupper(z[i]) ) z[i] = tolower(z[i]);
176 }
177}
178
179/*
drhfbc99082002-02-28 03:14:18 +0000180** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000181** All three do the same thing. They return the first non-NULL
182** argument.
drh3212e182002-02-28 00:46:26 +0000183*/
184static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
drhfbc99082002-02-28 03:14:18 +0000185 int i;
186 for(i=0; i<argc; i++){
187 if( argv[i] ){
188 sqlite_set_result_string(context, argv[i], -1);
189 break;
190 }
191 }
drh3212e182002-02-28 00:46:26 +0000192}
193
194/*
drhf9ffac92002-03-02 19:00:31 +0000195** Implementation of random(). Return a random integer.
196*/
197static void randomFunc(sqlite_func *context, int argc, const char **argv){
drhbbd82df2004-02-11 09:46:30 +0000198 int r;
199 sqliteRandomness(sizeof(r), &r);
200 sqlite_set_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000201}
202
203/*
drh6ed41ad2002-04-06 14:10:47 +0000204** Implementation of the last_insert_rowid() SQL function. The return
205** value is the same as the sqlite_last_insert_rowid() API function.
206*/
drh0ac65892002-04-20 14:24:41 +0000207static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
drh6ed41ad2002-04-06 14:10:47 +0000208 sqlite *db = sqlite_user_data(context);
209 sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
210}
211
212/*
drh0ac65892002-04-20 14:24:41 +0000213** Implementation of the like() SQL function. This function implements
214** the build-in LIKE operator. The first argument to the function is the
215** string and the second argument is the pattern. So, the SQL statements:
216**
217** A LIKE B
218**
219** is implemented as like(A,B).
220*/
221static void likeFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000222 if( argv[0]==0 || argv[1]==0 ) return;
drhec1bd0b2003-08-26 11:41:27 +0000223 sqlite_set_result_int(context,
224 sqliteLikeCompare((const unsigned char*)argv[0],
225 (const unsigned char*)argv[1]));
drh0ac65892002-04-20 14:24:41 +0000226}
227
228/*
229** Implementation of the glob() SQL function. This function implements
230** the build-in GLOB operator. The first argument to the function is the
231** string and the second argument is the pattern. So, the SQL statements:
232**
233** A GLOB B
234**
235** is implemented as glob(A,B).
236*/
237static void globFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000238 if( argv[0]==0 || argv[1]==0 ) return;
drhec1bd0b2003-08-26 11:41:27 +0000239 sqlite_set_result_int(context,
240 sqliteGlobCompare((const unsigned char*)argv[0],
241 (const unsigned char*)argv[1]));
drh8912d102002-05-26 21:34:58 +0000242}
243
244/*
245** Implementation of the NULLIF(x,y) function. The result is the first
246** argument if the arguments are different. The result is NULL if the
247** arguments are equal to each other.
248*/
249static void nullifFunc(sqlite_func *context, int argc, const char **argv){
250 if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
251 sqlite_set_result_string(context, argv[0], -1);
252 }
drh0ac65892002-04-20 14:24:41 +0000253}
254
drh647cb0e2002-11-04 19:32:25 +0000255/*
256** Implementation of the VERSION(*) function. The result is the version
257** of the SQLite library that is running.
258*/
259static void versionFunc(sqlite_func *context, int argc, const char **argv){
260 sqlite_set_result_string(context, sqlite_version, -1);
261}
262
drh47394702003-08-20 01:03:33 +0000263/*
264** EXPERIMENTAL - This is not an official function. The interface may
265** change. This function may disappear. Do not write code that depends
266** on this function.
267**
268** Implementation of the QUOTE() function. This function takes a single
269** argument. If the argument is numeric, the return value is the same as
270** the argument. If the argument is NULL, the return value is the string
271** "NULL". Otherwise, the argument is enclosed in single quotes with
272** single-quote escapes.
273*/
274static void quoteFunc(sqlite_func *context, int argc, const char **argv){
275 if( argc<1 ) return;
276 if( argv[0]==0 ){
277 sqlite_set_result_string(context, "NULL", 4);
278 }else if( sqliteIsNumber(argv[0]) ){
279 sqlite_set_result_string(context, argv[0], -1);
280 }else{
281 int i,j,n;
282 char *z;
283 for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
284 z = sqliteMalloc( i+n+3 );
285 if( z==0 ) return;
286 z[0] = '\'';
287 for(i=0, j=1; argv[0][i]; i++){
288 z[j++] = argv[0][i];
289 if( argv[0][i]=='\'' ){
290 z[j++] = '\'';
291 }
292 }
293 z[j++] = '\'';
294 z[j] = 0;
295 sqlite_set_result_string(context, z, j);
296 sqliteFree(z);
297 }
298}
299
drhd24cc422003-03-27 12:51:24 +0000300#ifdef SQLITE_SOUNDEX
301/*
302** Compute the soundex encoding of a word.
303*/
304static void soundexFunc(sqlite_func *context, int argc, const char **argv){
305 char zResult[8];
306 const char *zIn;
307 int i, j;
308 static const unsigned char iCode[] = {
309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
311 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
313 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
314 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
315 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
316 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
317 };
318 assert( argc==1 );
319 zIn = argv[0];
320 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
321 if( zIn[i] ){
322 zResult[0] = toupper(zIn[i]);
323 for(j=1; j<4 && zIn[i]; i++){
324 int code = iCode[zIn[i]&0x7f];
325 if( code>0 ){
326 zResult[j++] = code + '0';
327 }
328 }
329 while( j<4 ){
330 zResult[j++] = '0';
331 }
332 zResult[j] = 0;
333 sqlite_set_result_string(context, zResult, 4);
334 }else{
drh937dd842003-06-28 16:20:22 +0000335 sqlite_set_result_string(context, "?000", 4);
drhd24cc422003-03-27 12:51:24 +0000336 }
337}
338#endif
339
drh193a6b42002-07-07 16:52:46 +0000340#ifdef SQLITE_TEST
341/*
342** This function generates a string of random characters. Used for
343** generating test data.
344*/
345static void randStr(sqlite_func *context, int argc, const char **argv){
drhbbd82df2004-02-11 09:46:30 +0000346 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000347 "abcdefghijklmnopqrstuvwxyz"
348 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
349 "0123456789"
350 ".-!,:*^+=_|?/<> ";
351 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000352 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000353 if( argc>=1 ){
354 iMin = atoi(argv[0]);
355 if( iMin<0 ) iMin = 0;
356 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
357 }else{
358 iMin = 1;
359 }
360 if( argc>=2 ){
361 iMax = atoi(argv[1]);
362 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000363 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000364 }else{
365 iMax = 50;
366 }
367 n = iMin;
368 if( iMax>iMin ){
drhbbd82df2004-02-11 09:46:30 +0000369 sqliteRandomness(sizeof(r), &r);
370 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000371 n += r%(iMax + 1 - iMin);
372 }
drh1dba7272004-01-16 13:58:18 +0000373 assert( n<sizeof(zBuf) );
drhbbd82df2004-02-11 09:46:30 +0000374 sqliteRandomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000375 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000376 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000377 }
378 zBuf[n] = 0;
379 sqlite_set_result_string(context, zBuf, n);
380}
381#endif
382
drh0ac65892002-04-20 14:24:41 +0000383/*
drhd3a149e2002-02-24 17:12:53 +0000384** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000385** sum() or avg() aggregate computation.
386*/
387typedef struct SumCtx SumCtx;
388struct SumCtx {
389 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000390 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000391};
392
393/*
394** Routines used to compute the sum or average.
395*/
396static void sumStep(sqlite_func *context, int argc, const char **argv){
397 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000398 if( argc<1 ) return;
399 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000400 if( p && argv[0] ){
drh93a5c6b2003-12-23 02:17:35 +0000401 p->sum += sqliteAtoF(argv[0]);
drh739105c2002-05-29 23:22:23 +0000402 p->cnt++;
403 }
drhdd5baa92002-02-27 19:50:59 +0000404}
405static void sumFinalize(sqlite_func *context){
406 SumCtx *p;
407 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000408 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000409}
410static void avgFinalize(sqlite_func *context){
411 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000412 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000413 if( p && p->cnt>0 ){
414 sqlite_set_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000415 }
416}
417
418/*
419** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000420** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000421*/
422typedef struct StdDevCtx StdDevCtx;
423struct StdDevCtx {
424 double sum; /* Sum of terms */
425 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000426 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000427};
428
drhef2daf52002-03-04 02:26:15 +0000429#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000430/*
431** Routines used to compute the standard deviation as an aggregate.
432*/
drh1350b032002-02-27 19:00:20 +0000433static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000434 StdDevCtx *p;
435 double x;
drh1350b032002-02-27 19:00:20 +0000436 if( argc<1 ) return;
437 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000438 if( p && argv[0] ){
drh93a5c6b2003-12-23 02:17:35 +0000439 x = sqliteAtoF(argv[0]);
drh739105c2002-05-29 23:22:23 +0000440 p->sum += x;
441 p->sum2 += x*x;
442 p->cnt++;
443 }
drhd3a149e2002-02-24 17:12:53 +0000444}
drh1350b032002-02-27 19:00:20 +0000445static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000446 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000447 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000448 if( p && p->cnt>1 ){
449 double rCnt = cnt;
drhd3a149e2002-02-24 17:12:53 +0000450 sqlite_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000451 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000452 }
drhd3a149e2002-02-24 17:12:53 +0000453}
drhef2daf52002-03-04 02:26:15 +0000454#endif
drhd3a149e2002-02-24 17:12:53 +0000455
drh0bce8352002-02-28 00:41:10 +0000456/*
457** The following structure keeps track of state information for the
458** count() aggregate function.
459*/
460typedef struct CountCtx CountCtx;
461struct CountCtx {
462 int n;
463};
drhdd5baa92002-02-27 19:50:59 +0000464
drh0bce8352002-02-28 00:41:10 +0000465/*
466** Routines to implement the count() aggregate function.
467*/
468static void countStep(sqlite_func *context, int argc, const char **argv){
469 CountCtx *p;
470 p = sqlite_aggregate_context(context, sizeof(*p));
471 if( (argc==0 || argv[0]) && p ){
472 p->n++;
473 }
474}
475static void countFinalize(sqlite_func *context){
476 CountCtx *p;
477 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000478 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000479}
480
481/*
482** This function tracks state information for the min() and max()
483** aggregate functions.
484*/
485typedef struct MinMaxCtx MinMaxCtx;
486struct MinMaxCtx {
487 char *z; /* The best so far */
488 char zBuf[28]; /* Space that can be used for storage */
489};
490
491/*
492** Routines to implement min() and max() aggregate functions.
493*/
494static void minStep(sqlite_func *context, int argc, const char **argv){
495 MinMaxCtx *p;
496 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000497 if( p==0 || argc<1 || argv[0]==0 ) return;
drhf570f012002-05-31 15:51:25 +0000498 if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){
drh8912d102002-05-26 21:34:58 +0000499 int len;
drh00706be2004-01-30 14:49:16 +0000500 if( !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000501 sqliteFree(p->z);
502 }
drh8912d102002-05-26 21:34:58 +0000503 len = strlen(argv[0]);
drh00706be2004-01-30 14:49:16 +0000504 if( len < sizeof(p->zBuf)-1 ){
505 p->z = &p->zBuf[1];
506 p->zBuf[0] = 1;
drh0bce8352002-02-28 00:41:10 +0000507 }else{
drh8912d102002-05-26 21:34:58 +0000508 p->z = sqliteMalloc( len+1 );
drh00706be2004-01-30 14:49:16 +0000509 p->zBuf[0] = 0;
drh8912d102002-05-26 21:34:58 +0000510 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000511 }
drh8912d102002-05-26 21:34:58 +0000512 strcpy(p->z, argv[0]);
drh0bce8352002-02-28 00:41:10 +0000513 }
514}
515static void maxStep(sqlite_func *context, int argc, const char **argv){
516 MinMaxCtx *p;
517 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000518 if( p==0 || argc<1 || argv[0]==0 ) return;
drhf570f012002-05-31 15:51:25 +0000519 if( p->z==0 || sqliteCompare(argv[0],p->z)>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;
555 int nArg;
drhc9b84a12002-06-20 11:36:48 +0000556 int dataType;
drh0bce8352002-02-28 00:41:10 +0000557 void (*xFunc)(sqlite_func*,int,const char**);
558 } aFuncs[] = {
drhc9b84a12002-06-20 11:36:48 +0000559 { "min", -1, SQLITE_ARGS, minFunc },
560 { "min", 0, 0, 0 },
561 { "max", -1, SQLITE_ARGS, maxFunc },
562 { "max", 0, 0, 0 },
563 { "length", 1, SQLITE_NUMERIC, lengthFunc },
564 { "substr", 3, SQLITE_TEXT, substrFunc },
565 { "abs", 1, SQLITE_NUMERIC, absFunc },
566 { "round", 1, SQLITE_NUMERIC, roundFunc },
567 { "round", 2, SQLITE_NUMERIC, roundFunc },
568 { "upper", 1, SQLITE_TEXT, upperFunc },
569 { "lower", 1, SQLITE_TEXT, lowerFunc },
570 { "coalesce", -1, SQLITE_ARGS, ifnullFunc },
571 { "coalesce", 0, 0, 0 },
572 { "coalesce", 1, 0, 0 },
573 { "ifnull", 2, SQLITE_ARGS, ifnullFunc },
574 { "random", -1, SQLITE_NUMERIC, randomFunc },
575 { "like", 2, SQLITE_NUMERIC, likeFunc },
576 { "glob", 2, SQLITE_NUMERIC, globFunc },
577 { "nullif", 2, SQLITE_ARGS, nullifFunc },
drh647cb0e2002-11-04 19:32:25 +0000578 { "sqlite_version",0,SQLITE_TEXT, versionFunc},
drh47394702003-08-20 01:03:33 +0000579 { "quote", 1, SQLITE_ARGS, quoteFunc },
drhd24cc422003-03-27 12:51:24 +0000580#ifdef SQLITE_SOUNDEX
581 { "soundex", 1, SQLITE_TEXT, soundexFunc},
582#endif
drh193a6b42002-07-07 16:52:46 +0000583#ifdef SQLITE_TEST
584 { "randstr", 2, SQLITE_TEXT, randStr },
585#endif
drh0bce8352002-02-28 00:41:10 +0000586 };
587 static struct {
588 char *zName;
589 int nArg;
drhc9b84a12002-06-20 11:36:48 +0000590 int dataType;
drh0bce8352002-02-28 00:41:10 +0000591 void (*xStep)(sqlite_func*,int,const char**);
592 void (*xFinalize)(sqlite_func*);
593 } aAggs[] = {
drhc9b84a12002-06-20 11:36:48 +0000594 { "min", 1, 0, minStep, minMaxFinalize },
595 { "max", 1, 0, maxStep, minMaxFinalize },
596 { "sum", 1, SQLITE_NUMERIC, sumStep, sumFinalize },
597 { "avg", 1, SQLITE_NUMERIC, sumStep, avgFinalize },
598 { "count", 0, SQLITE_NUMERIC, countStep, countFinalize },
599 { "count", 1, SQLITE_NUMERIC, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000600#if 0
drhc9b84a12002-06-20 11:36:48 +0000601 { "stddev", 1, SQLITE_NUMERIC, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000602#endif
drh0bce8352002-02-28 00:41:10 +0000603 };
604 int i;
605
606 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
607 sqlite_create_function(db, aFuncs[i].zName,
608 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
drhc9b84a12002-06-20 11:36:48 +0000609 if( aFuncs[i].xFunc ){
610 sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
611 }
drh0bce8352002-02-28 00:41:10 +0000612 }
drh6ed41ad2002-04-06 14:10:47 +0000613 sqlite_create_function(db, "last_insert_rowid", 0,
614 last_insert_rowid, db);
drhc9b84a12002-06-20 11:36:48 +0000615 sqlite_function_type(db, "last_insert_rowid", SQLITE_NUMERIC);
drh0bce8352002-02-28 00:41:10 +0000616 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
617 sqlite_create_aggregate(db, aAggs[i].zName,
618 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
drhc9b84a12002-06-20 11:36:48 +0000619 sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
drh0bce8352002-02-28 00:41:10 +0000620 }
drh7014aff2003-11-01 01:53:53 +0000621 sqliteRegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000622}