blob: 2f30619de2be37c8eddbcb83a5367a8bf9acac2c [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**
rdcb0c374f2004-02-20 22:53:38 +000019** $Id: func.c,v 1.40 2004/02/20 22:53:39 rdc 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
rdcb0c374f2004-02-20 22:53:38 +0000212static void change_count(sqlite_func *context, int arg, const char **argv){
213 sqlite *db = sqlite_user_data(context);
214 sqlite_set_result_int(context, sqlite_changes(db));
215}
216static void last_statement_change_count(sqlite_func *context, int arg,
217 const char **argv){
218 sqlite *db = sqlite_user_data(context);
219 sqlite_set_result_int(context, sqlite_last_statement_changes(db));
220}
221
drh6ed41ad2002-04-06 14:10:47 +0000222/*
drh0ac65892002-04-20 14:24:41 +0000223** Implementation of the like() SQL function. This function implements
224** the build-in LIKE operator. The first argument to the function is the
225** string and the second argument is the pattern. So, the SQL statements:
226**
227** A LIKE B
228**
229** is implemented as like(A,B).
230*/
231static void likeFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000232 if( argv[0]==0 || argv[1]==0 ) return;
drhec1bd0b2003-08-26 11:41:27 +0000233 sqlite_set_result_int(context,
234 sqliteLikeCompare((const unsigned char*)argv[0],
235 (const unsigned char*)argv[1]));
drh0ac65892002-04-20 14:24:41 +0000236}
237
238/*
239** Implementation of the glob() SQL function. This function implements
240** the build-in GLOB operator. The first argument to the function is the
241** string and the second argument is the pattern. So, the SQL statements:
242**
243** A GLOB B
244**
245** is implemented as glob(A,B).
246*/
247static void globFunc(sqlite_func *context, int arg, const char **argv){
drh8912d102002-05-26 21:34:58 +0000248 if( argv[0]==0 || argv[1]==0 ) return;
drhec1bd0b2003-08-26 11:41:27 +0000249 sqlite_set_result_int(context,
250 sqliteGlobCompare((const unsigned char*)argv[0],
251 (const unsigned char*)argv[1]));
drh8912d102002-05-26 21:34:58 +0000252}
253
254/*
255** Implementation of the NULLIF(x,y) function. The result is the first
256** argument if the arguments are different. The result is NULL if the
257** arguments are equal to each other.
258*/
259static void nullifFunc(sqlite_func *context, int argc, const char **argv){
260 if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
261 sqlite_set_result_string(context, argv[0], -1);
262 }
drh0ac65892002-04-20 14:24:41 +0000263}
264
drh647cb0e2002-11-04 19:32:25 +0000265/*
266** Implementation of the VERSION(*) function. The result is the version
267** of the SQLite library that is running.
268*/
269static void versionFunc(sqlite_func *context, int argc, const char **argv){
270 sqlite_set_result_string(context, sqlite_version, -1);
271}
272
drh47394702003-08-20 01:03:33 +0000273/*
274** EXPERIMENTAL - This is not an official function. The interface may
275** change. This function may disappear. Do not write code that depends
276** on this function.
277**
278** Implementation of the QUOTE() function. This function takes a single
279** argument. If the argument is numeric, the return value is the same as
280** the argument. If the argument is NULL, the return value is the string
281** "NULL". Otherwise, the argument is enclosed in single quotes with
282** single-quote escapes.
283*/
284static void quoteFunc(sqlite_func *context, int argc, const char **argv){
285 if( argc<1 ) return;
286 if( argv[0]==0 ){
287 sqlite_set_result_string(context, "NULL", 4);
288 }else if( sqliteIsNumber(argv[0]) ){
289 sqlite_set_result_string(context, argv[0], -1);
290 }else{
291 int i,j,n;
292 char *z;
293 for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
294 z = sqliteMalloc( i+n+3 );
295 if( z==0 ) return;
296 z[0] = '\'';
297 for(i=0, j=1; argv[0][i]; i++){
298 z[j++] = argv[0][i];
299 if( argv[0][i]=='\'' ){
300 z[j++] = '\'';
301 }
302 }
303 z[j++] = '\'';
304 z[j] = 0;
305 sqlite_set_result_string(context, z, j);
306 sqliteFree(z);
307 }
308}
309
drhd24cc422003-03-27 12:51:24 +0000310#ifdef SQLITE_SOUNDEX
311/*
312** Compute the soundex encoding of a word.
313*/
314static void soundexFunc(sqlite_func *context, int argc, const char **argv){
315 char zResult[8];
316 const char *zIn;
317 int i, j;
318 static const unsigned char iCode[] = {
319 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
320 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
321 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
322 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
323 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
324 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
325 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
326 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
327 };
328 assert( argc==1 );
329 zIn = argv[0];
330 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
331 if( zIn[i] ){
332 zResult[0] = toupper(zIn[i]);
333 for(j=1; j<4 && zIn[i]; i++){
334 int code = iCode[zIn[i]&0x7f];
335 if( code>0 ){
336 zResult[j++] = code + '0';
337 }
338 }
339 while( j<4 ){
340 zResult[j++] = '0';
341 }
342 zResult[j] = 0;
343 sqlite_set_result_string(context, zResult, 4);
344 }else{
drh937dd842003-06-28 16:20:22 +0000345 sqlite_set_result_string(context, "?000", 4);
drhd24cc422003-03-27 12:51:24 +0000346 }
347}
348#endif
349
drh193a6b42002-07-07 16:52:46 +0000350#ifdef SQLITE_TEST
351/*
352** This function generates a string of random characters. Used for
353** generating test data.
354*/
355static void randStr(sqlite_func *context, int argc, const char **argv){
drhbbd82df2004-02-11 09:46:30 +0000356 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000357 "abcdefghijklmnopqrstuvwxyz"
358 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
359 "0123456789"
360 ".-!,:*^+=_|?/<> ";
361 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000362 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000363 if( argc>=1 ){
364 iMin = atoi(argv[0]);
365 if( iMin<0 ) iMin = 0;
366 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
367 }else{
368 iMin = 1;
369 }
370 if( argc>=2 ){
371 iMax = atoi(argv[1]);
372 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000373 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000374 }else{
375 iMax = 50;
376 }
377 n = iMin;
378 if( iMax>iMin ){
drhbbd82df2004-02-11 09:46:30 +0000379 sqliteRandomness(sizeof(r), &r);
380 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000381 n += r%(iMax + 1 - iMin);
382 }
drh1dba7272004-01-16 13:58:18 +0000383 assert( n<sizeof(zBuf) );
drhbbd82df2004-02-11 09:46:30 +0000384 sqliteRandomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000385 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000386 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000387 }
388 zBuf[n] = 0;
389 sqlite_set_result_string(context, zBuf, n);
390}
391#endif
392
drh0ac65892002-04-20 14:24:41 +0000393/*
drhd3a149e2002-02-24 17:12:53 +0000394** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000395** sum() or avg() aggregate computation.
396*/
397typedef struct SumCtx SumCtx;
398struct SumCtx {
399 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000400 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000401};
402
403/*
404** Routines used to compute the sum or average.
405*/
406static void sumStep(sqlite_func *context, int argc, const char **argv){
407 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000408 if( argc<1 ) return;
409 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000410 if( p && argv[0] ){
drh93a5c6b2003-12-23 02:17:35 +0000411 p->sum += sqliteAtoF(argv[0]);
drh739105c2002-05-29 23:22:23 +0000412 p->cnt++;
413 }
drhdd5baa92002-02-27 19:50:59 +0000414}
415static void sumFinalize(sqlite_func *context){
416 SumCtx *p;
417 p = sqlite_aggregate_context(context, sizeof(*p));
drh89425d52002-02-28 03:04:48 +0000418 sqlite_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000419}
420static void avgFinalize(sqlite_func *context){
421 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000422 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000423 if( p && p->cnt>0 ){
424 sqlite_set_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000425 }
426}
427
428/*
429** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000430** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000431*/
432typedef struct StdDevCtx StdDevCtx;
433struct StdDevCtx {
434 double sum; /* Sum of terms */
435 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000436 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000437};
438
drhef2daf52002-03-04 02:26:15 +0000439#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000440/*
441** Routines used to compute the standard deviation as an aggregate.
442*/
drh1350b032002-02-27 19:00:20 +0000443static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000444 StdDevCtx *p;
445 double x;
drh1350b032002-02-27 19:00:20 +0000446 if( argc<1 ) return;
447 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000448 if( p && argv[0] ){
drh93a5c6b2003-12-23 02:17:35 +0000449 x = sqliteAtoF(argv[0]);
drh739105c2002-05-29 23:22:23 +0000450 p->sum += x;
451 p->sum2 += x*x;
452 p->cnt++;
453 }
drhd3a149e2002-02-24 17:12:53 +0000454}
drh1350b032002-02-27 19:00:20 +0000455static void stdDevFinalize(sqlite_func *context){
drhdd5baa92002-02-27 19:50:59 +0000456 double rN = sqlite_aggregate_count(context);
drh1350b032002-02-27 19:00:20 +0000457 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000458 if( p && p->cnt>1 ){
459 double rCnt = cnt;
drhd3a149e2002-02-24 17:12:53 +0000460 sqlite_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000461 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000462 }
drhd3a149e2002-02-24 17:12:53 +0000463}
drhef2daf52002-03-04 02:26:15 +0000464#endif
drhd3a149e2002-02-24 17:12:53 +0000465
drh0bce8352002-02-28 00:41:10 +0000466/*
467** The following structure keeps track of state information for the
468** count() aggregate function.
469*/
470typedef struct CountCtx CountCtx;
471struct CountCtx {
472 int n;
473};
drhdd5baa92002-02-27 19:50:59 +0000474
drh0bce8352002-02-28 00:41:10 +0000475/*
476** Routines to implement the count() aggregate function.
477*/
478static void countStep(sqlite_func *context, int argc, const char **argv){
479 CountCtx *p;
480 p = sqlite_aggregate_context(context, sizeof(*p));
481 if( (argc==0 || argv[0]) && p ){
482 p->n++;
483 }
484}
485static void countFinalize(sqlite_func *context){
486 CountCtx *p;
487 p = sqlite_aggregate_context(context, sizeof(*p));
drhf55f25f2002-02-28 01:46:11 +0000488 sqlite_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000489}
490
491/*
492** This function tracks state information for the min() and max()
493** aggregate functions.
494*/
495typedef struct MinMaxCtx MinMaxCtx;
496struct MinMaxCtx {
497 char *z; /* The best so far */
498 char zBuf[28]; /* Space that can be used for storage */
499};
500
501/*
502** Routines to implement min() and max() aggregate functions.
503*/
504static void minStep(sqlite_func *context, int argc, const char **argv){
505 MinMaxCtx *p;
506 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000507 if( p==0 || argc<1 || argv[0]==0 ) return;
drhf570f012002-05-31 15:51:25 +0000508 if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){
drh8912d102002-05-26 21:34:58 +0000509 int len;
drh00706be2004-01-30 14:49:16 +0000510 if( !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000511 sqliteFree(p->z);
512 }
drh8912d102002-05-26 21:34:58 +0000513 len = strlen(argv[0]);
drh00706be2004-01-30 14:49:16 +0000514 if( len < sizeof(p->zBuf)-1 ){
515 p->z = &p->zBuf[1];
516 p->zBuf[0] = 1;
drh0bce8352002-02-28 00:41:10 +0000517 }else{
drh8912d102002-05-26 21:34:58 +0000518 p->z = sqliteMalloc( len+1 );
drh00706be2004-01-30 14:49:16 +0000519 p->zBuf[0] = 0;
drh8912d102002-05-26 21:34:58 +0000520 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000521 }
drh8912d102002-05-26 21:34:58 +0000522 strcpy(p->z, argv[0]);
drh0bce8352002-02-28 00:41:10 +0000523 }
524}
525static void maxStep(sqlite_func *context, int argc, const char **argv){
526 MinMaxCtx *p;
527 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000528 if( p==0 || argc<1 || argv[0]==0 ) return;
drhf570f012002-05-31 15:51:25 +0000529 if( p->z==0 || sqliteCompare(argv[0],p->z)>0 ){
drh8912d102002-05-26 21:34:58 +0000530 int len;
drh00706be2004-01-30 14:49:16 +0000531 if( !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000532 sqliteFree(p->z);
533 }
drh8912d102002-05-26 21:34:58 +0000534 len = strlen(argv[0]);
drh00706be2004-01-30 14:49:16 +0000535 if( len < sizeof(p->zBuf)-1 ){
536 p->z = &p->zBuf[1];
537 p->zBuf[0] = 1;
drh0bce8352002-02-28 00:41:10 +0000538 }else{
drh8912d102002-05-26 21:34:58 +0000539 p->z = sqliteMalloc( len+1 );
drh00706be2004-01-30 14:49:16 +0000540 p->zBuf[0] = 0;
drh8912d102002-05-26 21:34:58 +0000541 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000542 }
drh8912d102002-05-26 21:34:58 +0000543 strcpy(p->z, argv[0]);
drh0bce8352002-02-28 00:41:10 +0000544 }
545}
546static void minMaxFinalize(sqlite_func *context){
547 MinMaxCtx *p;
548 p = sqlite_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000549 if( p && p->z ){
drh0bce8352002-02-28 00:41:10 +0000550 sqlite_set_result_string(context, p->z, strlen(p->z));
551 }
drh00706be2004-01-30 14:49:16 +0000552 if( p && !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000553 sqliteFree(p->z);
554 }
555}
drhdd5baa92002-02-27 19:50:59 +0000556
drhd3a149e2002-02-24 17:12:53 +0000557/*
drha2ed5602002-02-26 23:55:31 +0000558** This function registered all of the above C functions as SQL
559** functions. This should be the only routine in this file with
560** external linkage.
drhdc04c582002-02-24 01:55:15 +0000561*/
drh28f4b682002-06-09 10:14:18 +0000562void sqliteRegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000563 static struct {
564 char *zName;
565 int nArg;
drhc9b84a12002-06-20 11:36:48 +0000566 int dataType;
drh0bce8352002-02-28 00:41:10 +0000567 void (*xFunc)(sqlite_func*,int,const char**);
568 } aFuncs[] = {
drhc9b84a12002-06-20 11:36:48 +0000569 { "min", -1, SQLITE_ARGS, minFunc },
570 { "min", 0, 0, 0 },
571 { "max", -1, SQLITE_ARGS, maxFunc },
572 { "max", 0, 0, 0 },
573 { "length", 1, SQLITE_NUMERIC, lengthFunc },
574 { "substr", 3, SQLITE_TEXT, substrFunc },
575 { "abs", 1, SQLITE_NUMERIC, absFunc },
576 { "round", 1, SQLITE_NUMERIC, roundFunc },
577 { "round", 2, SQLITE_NUMERIC, roundFunc },
578 { "upper", 1, SQLITE_TEXT, upperFunc },
579 { "lower", 1, SQLITE_TEXT, lowerFunc },
580 { "coalesce", -1, SQLITE_ARGS, ifnullFunc },
581 { "coalesce", 0, 0, 0 },
582 { "coalesce", 1, 0, 0 },
583 { "ifnull", 2, SQLITE_ARGS, ifnullFunc },
584 { "random", -1, SQLITE_NUMERIC, randomFunc },
585 { "like", 2, SQLITE_NUMERIC, likeFunc },
586 { "glob", 2, SQLITE_NUMERIC, globFunc },
587 { "nullif", 2, SQLITE_ARGS, nullifFunc },
drh647cb0e2002-11-04 19:32:25 +0000588 { "sqlite_version",0,SQLITE_TEXT, versionFunc},
drh47394702003-08-20 01:03:33 +0000589 { "quote", 1, SQLITE_ARGS, quoteFunc },
drhd24cc422003-03-27 12:51:24 +0000590#ifdef SQLITE_SOUNDEX
591 { "soundex", 1, SQLITE_TEXT, soundexFunc},
592#endif
drh193a6b42002-07-07 16:52:46 +0000593#ifdef SQLITE_TEST
594 { "randstr", 2, SQLITE_TEXT, randStr },
595#endif
drh0bce8352002-02-28 00:41:10 +0000596 };
597 static struct {
598 char *zName;
599 int nArg;
drhc9b84a12002-06-20 11:36:48 +0000600 int dataType;
drh0bce8352002-02-28 00:41:10 +0000601 void (*xStep)(sqlite_func*,int,const char**);
602 void (*xFinalize)(sqlite_func*);
603 } aAggs[] = {
drhc9b84a12002-06-20 11:36:48 +0000604 { "min", 1, 0, minStep, minMaxFinalize },
605 { "max", 1, 0, maxStep, minMaxFinalize },
606 { "sum", 1, SQLITE_NUMERIC, sumStep, sumFinalize },
607 { "avg", 1, SQLITE_NUMERIC, sumStep, avgFinalize },
608 { "count", 0, SQLITE_NUMERIC, countStep, countFinalize },
609 { "count", 1, SQLITE_NUMERIC, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000610#if 0
drhc9b84a12002-06-20 11:36:48 +0000611 { "stddev", 1, SQLITE_NUMERIC, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000612#endif
drh0bce8352002-02-28 00:41:10 +0000613 };
614 int i;
615
616 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
617 sqlite_create_function(db, aFuncs[i].zName,
618 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
drhc9b84a12002-06-20 11:36:48 +0000619 if( aFuncs[i].xFunc ){
620 sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
621 }
drh0bce8352002-02-28 00:41:10 +0000622 }
drh6ed41ad2002-04-06 14:10:47 +0000623 sqlite_create_function(db, "last_insert_rowid", 0,
624 last_insert_rowid, db);
drhc9b84a12002-06-20 11:36:48 +0000625 sqlite_function_type(db, "last_insert_rowid", SQLITE_NUMERIC);
rdcb0c374f2004-02-20 22:53:38 +0000626 sqlite_create_function(db, "change_count", 0, change_count, db);
627 sqlite_function_type(db, "change_count", SQLITE_NUMERIC);
628 sqlite_create_function(db, "last_statement_change_count", 0,
629 last_statement_change_count, db);
630 sqlite_function_type(db, "last_statement_change_count", SQLITE_NUMERIC);
631
drh0bce8352002-02-28 00:41:10 +0000632 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
633 sqlite_create_aggregate(db, aAggs[i].zName,
634 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
drhc9b84a12002-06-20 11:36:48 +0000635 sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
drh0bce8352002-02-28 00:41:10 +0000636 }
drh7014aff2003-11-01 01:53:53 +0000637 sqliteRegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000638}