blob: 23386b467f4e5989f4a56907e96aa68538d4035b [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**
danielk197788208052004-05-25 01:13:20 +000019** $Id: func.c,v 1.53 2004/05/25 01:13:21 danielk1977 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"
danielk197788208052004-05-25 01:13:20 +000026#include "vdbeInt.h"
drh771d8c32003-08-09 21:32:28 +000027#include "os.h"
drh0bce8352002-02-28 00:41:10 +000028
29/*
30** Implementation of the non-aggregate min() and max() functions
31*/
danielk197751ad0ec2004-05-24 12:39:02 +000032static void minmaxFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +000033 const char *zBest;
34 int i;
drh268380c2004-02-25 13:47:31 +000035 int (*xCompare)(const char*, const char*);
36 int mask; /* 0 for min() or 0xffffffff for max() */
danielk197751ad0ec2004-05-24 12:39:02 +000037 const char *zArg;
drh0bce8352002-02-28 00:41:10 +000038
drh89425d52002-02-28 03:04:48 +000039 if( argc==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +000040 mask = (int)sqlite3_user_data(context);
danielk197751ad0ec2004-05-24 12:39:02 +000041 zBest = sqlite3_value_data(argv[0]);
drh8912d102002-05-26 21:34:58 +000042 if( zBest==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +000043 zArg = sqlite3_value_data(argv[1]);
44 if( zArg[0]=='n' ){
danielk19774adee202004-05-08 08:23:19 +000045 xCompare = sqlite3Compare;
drh268380c2004-02-25 13:47:31 +000046 }else{
47 xCompare = strcmp;
48 }
49 for(i=2; i<argc; i+=2){
danielk197751ad0ec2004-05-24 12:39:02 +000050 zArg = sqlite3_value_data(argv[i]);
51 if( zArg==0 ) return;
52 if( (xCompare(zArg, zBest)^mask)<0 ){
53 zBest = zArg;
drh0bce8352002-02-28 00:41:10 +000054 }
55 }
danielk197724b03fd2004-05-10 10:34:34 +000056 sqlite3_set_result_string(context, zBest, -1);
drh0bce8352002-02-28 00:41:10 +000057}
drh0bce8352002-02-28 00:41:10 +000058
drh268380c2004-02-25 13:47:31 +000059/*
60** Return the type of the argument.
61*/
danielk197751ad0ec2004-05-24 12:39:02 +000062static void typeofFunc(sqlite_func *context, int argc, sqlite3_value **argv){
danielk197735bb9d02004-05-24 12:55:54 +000063 const char *z = 0;
drh268380c2004-02-25 13:47:31 +000064 assert( argc==2 );
danielk197735bb9d02004-05-24 12:55:54 +000065 switch( sqlite3_value_type(argv[0]) ){
66 case SQLITE3_NULL: z = "null" ; break;
67 case SQLITE3_INTEGER: z = "integer" ; break;
68 case SQLITE3_TEXT: z = "text" ; break;
69 case SQLITE3_FLOAT: z = "real" ; break;
70 case SQLITE3_BLOB: z = "blob" ; break;
71 }
72 sqlite3_set_result_string(context, z, -1);
drh0bce8352002-02-28 00:41:10 +000073}
74
75/*
76** Implementation of the length() function
77*/
danielk197751ad0ec2004-05-24 12:39:02 +000078static void lengthFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +000079 const char *z;
80 int len;
81
82 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +000083 z = sqlite3_value_data(argv[0]);
drh8912d102002-05-26 21:34:58 +000084 if( z==0 ) return;
drh0bce8352002-02-28 00:41:10 +000085#ifdef SQLITE_UTF8
drh8912d102002-05-26 21:34:58 +000086 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +000087#else
drh8912d102002-05-26 21:34:58 +000088 len = strlen(z);
drh0bce8352002-02-28 00:41:10 +000089#endif
danielk197724b03fd2004-05-10 10:34:34 +000090 sqlite3_set_result_int(context, len);
drh0bce8352002-02-28 00:41:10 +000091}
92
93/*
94** Implementation of the abs() function
95*/
danielk197751ad0ec2004-05-24 12:39:02 +000096static void absFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +000097 const char *z;
98 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +000099 z = sqlite3_value_data(argv[0]);
drh8912d102002-05-26 21:34:58 +0000100 if( z==0 ) return;
101 if( z[0]=='-' && isdigit(z[1]) ) z++;
danielk197724b03fd2004-05-10 10:34:34 +0000102 sqlite3_set_result_string(context, z, -1);
drh0bce8352002-02-28 00:41:10 +0000103}
104
105/*
106** Implementation of the substr() function
107*/
danielk197751ad0ec2004-05-24 12:39:02 +0000108static void substrFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000109 const char *z;
110#ifdef SQLITE_UTF8
111 const char *z2;
112 int i;
113#endif
114 int p1, p2, len;
115 assert( argc==3 );
danielk197751ad0ec2004-05-24 12:39:02 +0000116 z = sqlite3_value_data(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000117 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000118 p1 = sqlite3_value_int(argv[1]);
119 p2 = sqlite3_value_int(argv[2]);
drh0bce8352002-02-28 00:41:10 +0000120#ifdef SQLITE_UTF8
drh47c8a672002-02-28 04:00:12 +0000121 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000122#else
123 len = strlen(z);
124#endif
125 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000126 p1 += len;
drh653bc752002-02-28 03:31:10 +0000127 if( p1<0 ){
128 p2 += p1;
129 p1 = 0;
130 }
drh0bce8352002-02-28 00:41:10 +0000131 }else if( p1>0 ){
132 p1--;
133 }
134 if( p1+p2>len ){
135 p2 = len-p1;
136 }
137#ifdef SQLITE_UTF8
drh77396302004-01-02 13:17:48 +0000138 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000139 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000140 }
drh47c8a672002-02-28 04:00:12 +0000141 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000142 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000143 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000144 }
drh47c8a672002-02-28 04:00:12 +0000145 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh0bce8352002-02-28 00:41:10 +0000146#endif
drh653bc752002-02-28 03:31:10 +0000147 if( p2<0 ) p2 = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000148 sqlite3_set_result_string(context, &z[p1], p2);
drh0bce8352002-02-28 00:41:10 +0000149}
150
151/*
152** Implementation of the round() function
153*/
danielk197751ad0ec2004-05-24 12:39:02 +0000154static void roundFunc(sqlite_func *context, int argc, sqlite3_value **argv){
155 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000156 double r;
157 char zBuf[100];
158 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000159 if( argc==2 ){
160 if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
161 n = sqlite3_value_int(argv[1]);
162 if( n>30 ) n = 30;
163 if( n<0 ) n = 0;
164 }
165 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
166 r = sqlite3_value_float(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000167 sprintf(zBuf,"%.*f",n,r);
danielk197724b03fd2004-05-10 10:34:34 +0000168 sqlite3_set_result_string(context, zBuf, -1);
drh0bce8352002-02-28 00:41:10 +0000169}
drhdc04c582002-02-24 01:55:15 +0000170
171/*
172** Implementation of the upper() and lower() SQL functions.
173*/
danielk197751ad0ec2004-05-24 12:39:02 +0000174static void upperFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000175 char *z;
176 int i;
danielk197751ad0ec2004-05-24 12:39:02 +0000177 if( argc<1 ) return;
178 z = sqlite3_set_result_string(context, sqlite3_value_data(argv[0]), -1);
drhdc04c582002-02-24 01:55:15 +0000179 if( z==0 ) return;
180 for(i=0; z[i]; i++){
181 if( islower(z[i]) ) z[i] = toupper(z[i]);
182 }
183}
danielk197751ad0ec2004-05-24 12:39:02 +0000184static void lowerFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000185 char *z;
186 int i;
danielk197751ad0ec2004-05-24 12:39:02 +0000187 if( argc<1 ) return;
188 z = sqlite3_set_result_string(context, sqlite3_value_data(argv[0]), -1);
drhdc04c582002-02-24 01:55:15 +0000189 if( z==0 ) return;
190 for(i=0; z[i]; i++){
191 if( isupper(z[i]) ) z[i] = tolower(z[i]);
192 }
193}
194
195/*
drhfbc99082002-02-28 03:14:18 +0000196** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000197** All three do the same thing. They return the first non-NULL
198** argument.
drh3212e182002-02-28 00:46:26 +0000199*/
danielk197751ad0ec2004-05-24 12:39:02 +0000200static void ifnullFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhfbc99082002-02-28 03:14:18 +0000201 int i;
202 for(i=0; i<argc; i++){
danielk197751ad0ec2004-05-24 12:39:02 +0000203 if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
204 sqlite3_set_result_string(context, sqlite3_value_data(argv[i]), -1);
drhfbc99082002-02-28 03:14:18 +0000205 break;
206 }
207 }
drh3212e182002-02-28 00:46:26 +0000208}
209
210/*
drhf9ffac92002-03-02 19:00:31 +0000211** Implementation of random(). Return a random integer.
212*/
danielk197751ad0ec2004-05-24 12:39:02 +0000213static void randomFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000214 int r;
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3Randomness(sizeof(r), &r);
danielk197724b03fd2004-05-10 10:34:34 +0000216 sqlite3_set_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000217}
218
219/*
drh6ed41ad2002-04-06 14:10:47 +0000220** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000221** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000222*/
danielk197751ad0ec2004-05-24 12:39:02 +0000223static void last_insert_rowid(
224 sqlite_func *context,
225 int arg,
226 sqlite3_value **argv
227){
danielk197724b03fd2004-05-10 10:34:34 +0000228 sqlite *db = sqlite3_user_data(context);
229 sqlite3_set_result_int(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000230}
231
rdcf146a772004-02-25 22:51:06 +0000232/*
233** Implementation of the change_count() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000234** value is the same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000235*/
danielk197751ad0ec2004-05-24 12:39:02 +0000236static void change_count(sqlite_func *context, int arg, sqlite3_value **argv){
danielk197724b03fd2004-05-10 10:34:34 +0000237 sqlite *db = sqlite3_user_data(context);
238 sqlite3_set_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000239}
rdcf146a772004-02-25 22:51:06 +0000240
241/*
242** Implementation of the last_statement_change_count() SQL function. The
danielk197751ad0ec2004-05-24 12:39:02 +0000243** return value is the same as the sqlite3_last_statement_changes() API
244** function.
rdcf146a772004-02-25 22:51:06 +0000245*/
danielk197751ad0ec2004-05-24 12:39:02 +0000246static void last_statement_change_count(
247 sqlite_func *context,
248 int arg,
249 sqlite3_value **argv
250){
danielk197724b03fd2004-05-10 10:34:34 +0000251 sqlite *db = sqlite3_user_data(context);
252 sqlite3_set_result_int(context, sqlite3_last_statement_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000253}
254
drh6ed41ad2002-04-06 14:10:47 +0000255/*
drh0ac65892002-04-20 14:24:41 +0000256** Implementation of the like() SQL function. This function implements
257** the build-in LIKE operator. The first argument to the function is the
258** string and the second argument is the pattern. So, the SQL statements:
259**
260** A LIKE B
261**
262** is implemented as like(A,B).
263*/
danielk197751ad0ec2004-05-24 12:39:02 +0000264static void likeFunc(
265 sqlite_func *context,
266 int argc,
267 sqlite3_value **argv
268){
269 const unsigned char *zA = sqlite3_value_data(argv[0]);
270 const unsigned char *zB = sqlite3_value_data(argv[1]);
271 if( zA && zB ){
272 sqlite3_set_result_int(context, sqlite3LikeCompare(zA, zB));
273 }
drh0ac65892002-04-20 14:24:41 +0000274}
275
276/*
277** Implementation of the glob() SQL function. This function implements
278** the build-in GLOB operator. The first argument to the function is the
279** string and the second argument is the pattern. So, the SQL statements:
280**
281** A GLOB B
282**
283** is implemented as glob(A,B).
284*/
danielk197751ad0ec2004-05-24 12:39:02 +0000285static void globFunc(sqlite_func *context, int arg, sqlite3_value **argv){
286 const unsigned char *zA = sqlite3_value_data(argv[0]);
287 const unsigned char *zB = sqlite3_value_data(argv[1]);
288 if( zA && zB ){
289 sqlite3_set_result_int(context, sqlite3GlobCompare(zA, zB));
290 }
drh8912d102002-05-26 21:34:58 +0000291}
292
293/*
294** Implementation of the NULLIF(x,y) function. The result is the first
295** argument if the arguments are different. The result is NULL if the
296** arguments are equal to each other.
297*/
danielk197751ad0ec2004-05-24 12:39:02 +0000298static void nullifFunc(sqlite_func *context, int argc, sqlite3_value **argv){
299 const unsigned char *zX = sqlite3_value_data(argv[0]);
300 const unsigned char *zY = sqlite3_value_data(argv[1]);
301 if( zX!=0 && sqlite3Compare(zX, zY)!=0 ){
302 sqlite3_set_result_string(context, zX, -1);
drh8912d102002-05-26 21:34:58 +0000303 }
drh0ac65892002-04-20 14:24:41 +0000304}
305
drh647cb0e2002-11-04 19:32:25 +0000306/*
307** Implementation of the VERSION(*) function. The result is the version
308** of the SQLite library that is running.
309*/
danielk197751ad0ec2004-05-24 12:39:02 +0000310static void versionFunc(sqlite_func *context, int argc, sqlite3_value **argv){
danielk197724b03fd2004-05-10 10:34:34 +0000311 sqlite3_set_result_string(context, sqlite3_version, -1);
drh647cb0e2002-11-04 19:32:25 +0000312}
313
drh47394702003-08-20 01:03:33 +0000314/*
315** EXPERIMENTAL - This is not an official function. The interface may
316** change. This function may disappear. Do not write code that depends
317** on this function.
318**
319** Implementation of the QUOTE() function. This function takes a single
320** argument. If the argument is numeric, the return value is the same as
321** the argument. If the argument is NULL, the return value is the string
322** "NULL". Otherwise, the argument is enclosed in single quotes with
323** single-quote escapes.
324*/
danielk197751ad0ec2004-05-24 12:39:02 +0000325static void quoteFunc(sqlite_func *context, int argc, sqlite3_value **argv){
326 const char *zArg = sqlite3_value_data(argv[0]);
drh47394702003-08-20 01:03:33 +0000327 if( argc<1 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000328 if( zArg==0 ){
danielk197724b03fd2004-05-10 10:34:34 +0000329 sqlite3_set_result_string(context, "NULL", 4);
danielk197751ad0ec2004-05-24 12:39:02 +0000330 }else if( sqlite3IsNumber(zArg, 0, TEXT_Utf8) ){
331 sqlite3_set_result_string(context, zArg, -1);
drh47394702003-08-20 01:03:33 +0000332 }else{
333 int i,j,n;
334 char *z;
danielk197751ad0ec2004-05-24 12:39:02 +0000335 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
drh47394702003-08-20 01:03:33 +0000336 z = sqliteMalloc( i+n+3 );
337 if( z==0 ) return;
338 z[0] = '\'';
danielk197751ad0ec2004-05-24 12:39:02 +0000339 for(i=0, j=1; zArg[i]; i++){
340 z[j++] = zArg[i];
341 if( zArg[i]=='\'' ){
drh47394702003-08-20 01:03:33 +0000342 z[j++] = '\'';
343 }
344 }
345 z[j++] = '\'';
346 z[j] = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000347 sqlite3_set_result_string(context, z, j);
drh47394702003-08-20 01:03:33 +0000348 sqliteFree(z);
349 }
350}
351
drhd24cc422003-03-27 12:51:24 +0000352#ifdef SQLITE_SOUNDEX
353/*
354** Compute the soundex encoding of a word.
355*/
danielk197751ad0ec2004-05-24 12:39:02 +0000356static void soundexFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000357 char zResult[8];
358 const char *zIn;
359 int i, j;
360 static const unsigned char iCode[] = {
361 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
362 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
363 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
365 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
366 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
367 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
368 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
369 };
370 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +0000371 zIn = sqlite3_value_data(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000372 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
373 if( zIn[i] ){
374 zResult[0] = toupper(zIn[i]);
375 for(j=1; j<4 && zIn[i]; i++){
376 int code = iCode[zIn[i]&0x7f];
377 if( code>0 ){
378 zResult[j++] = code + '0';
379 }
380 }
381 while( j<4 ){
382 zResult[j++] = '0';
383 }
384 zResult[j] = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000385 sqlite3_set_result_string(context, zResult, 4);
drhd24cc422003-03-27 12:51:24 +0000386 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000387 sqlite3_set_result_string(context, "?000", 4);
drhd24cc422003-03-27 12:51:24 +0000388 }
389}
390#endif
391
drh193a6b42002-07-07 16:52:46 +0000392#ifdef SQLITE_TEST
393/*
394** This function generates a string of random characters. Used for
395** generating test data.
396*/
danielk197751ad0ec2004-05-24 12:39:02 +0000397static void randStr(sqlite_func *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000398 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000399 "abcdefghijklmnopqrstuvwxyz"
400 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
401 "0123456789"
402 ".-!,:*^+=_|?/<> ";
403 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000404 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000405 if( argc>=1 ){
danielk197751ad0ec2004-05-24 12:39:02 +0000406 iMin = atoi(sqlite3_value_data(argv[0]));
drh193a6b42002-07-07 16:52:46 +0000407 if( iMin<0 ) iMin = 0;
408 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
409 }else{
410 iMin = 1;
411 }
412 if( argc>=2 ){
danielk197751ad0ec2004-05-24 12:39:02 +0000413 iMax = atoi(sqlite3_value_data(argv[1]));
drh193a6b42002-07-07 16:52:46 +0000414 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000415 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000416 }else{
417 iMax = 50;
418 }
419 n = iMin;
420 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000421 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000422 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000423 n += r%(iMax + 1 - iMin);
424 }
drh1dba7272004-01-16 13:58:18 +0000425 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000426 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000427 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000428 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000429 }
430 zBuf[n] = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000431 sqlite3_set_result_string(context, zBuf, n);
drh193a6b42002-07-07 16:52:46 +0000432}
433#endif
434
drh0ac65892002-04-20 14:24:41 +0000435/*
drhd3a149e2002-02-24 17:12:53 +0000436** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000437** sum() or avg() aggregate computation.
438*/
439typedef struct SumCtx SumCtx;
440struct SumCtx {
441 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000442 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000443};
444
445/*
446** Routines used to compute the sum or average.
447*/
danielk19776ddcca52004-05-24 23:48:25 +0000448static void sumStep(sqlite_func *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000449 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000450 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000451 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000452 if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
453 p->sum += sqlite3_value_float(argv[0]);
drh739105c2002-05-29 23:22:23 +0000454 p->cnt++;
455 }
drhdd5baa92002-02-27 19:50:59 +0000456}
457static void sumFinalize(sqlite_func *context){
458 SumCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000459 p = sqlite3_aggregate_context(context, sizeof(*p));
460 sqlite3_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000461}
462static void avgFinalize(sqlite_func *context){
463 SumCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000464 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000465 if( p && p->cnt>0 ){
danielk197724b03fd2004-05-10 10:34:34 +0000466 sqlite3_set_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000467 }
468}
469
470/*
471** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000472** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000473*/
474typedef struct StdDevCtx StdDevCtx;
475struct StdDevCtx {
476 double sum; /* Sum of terms */
477 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000478 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000479};
480
drhef2daf52002-03-04 02:26:15 +0000481#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000482/*
483** Routines used to compute the standard deviation as an aggregate.
484*/
drh1350b032002-02-27 19:00:20 +0000485static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000486 StdDevCtx *p;
487 double x;
drh1350b032002-02-27 19:00:20 +0000488 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000489 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000490 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000491 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000492 p->sum += x;
493 p->sum2 += x*x;
494 p->cnt++;
495 }
drhd3a149e2002-02-24 17:12:53 +0000496}
drh1350b032002-02-27 19:00:20 +0000497static void stdDevFinalize(sqlite_func *context){
danielk197724b03fd2004-05-10 10:34:34 +0000498 double rN = sqlite3_aggregate_count(context);
499 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000500 if( p && p->cnt>1 ){
501 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000502 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000503 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000504 }
drhd3a149e2002-02-24 17:12:53 +0000505}
drhef2daf52002-03-04 02:26:15 +0000506#endif
drhd3a149e2002-02-24 17:12:53 +0000507
drh0bce8352002-02-28 00:41:10 +0000508/*
509** The following structure keeps track of state information for the
510** count() aggregate function.
511*/
512typedef struct CountCtx CountCtx;
513struct CountCtx {
514 int n;
515};
drhdd5baa92002-02-27 19:50:59 +0000516
drh0bce8352002-02-28 00:41:10 +0000517/*
518** Routines to implement the count() aggregate function.
519*/
danielk19776ddcca52004-05-24 23:48:25 +0000520static void countStep(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000521 CountCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000522 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000523 if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000524 p->n++;
525 }
526}
527static void countFinalize(sqlite_func *context){
528 CountCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000529 p = sqlite3_aggregate_context(context, sizeof(*p));
530 sqlite3_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000531}
532
533/*
534** This function tracks state information for the min() and max()
535** aggregate functions.
536*/
537typedef struct MinMaxCtx MinMaxCtx;
538struct MinMaxCtx {
539 char *z; /* The best so far */
540 char zBuf[28]; /* Space that can be used for storage */
541};
542
543/*
544** Routines to implement min() and max() aggregate functions.
545*/
danielk19776ddcca52004-05-24 23:48:25 +0000546static void minmaxStep(sqlite_func *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000547 int max = 0;
548 int cmp = 0;
549 Mem *pArg = (Mem *)argv[0];
550 Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
drh268380c2004-02-25 13:47:31 +0000551
danielk197788208052004-05-25 01:13:20 +0000552 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
553
554 if( pBest->flags ){
555 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
556 cmp = sqlite3MemCompare(pBest, pArg, 0);
557 if( (max && cmp<0) || (!max && cmp>0) ){
558 sqlite3MemCopy(pBest, pArg);
559 }
drh268380c2004-02-25 13:47:31 +0000560 }else{
danielk197788208052004-05-25 01:13:20 +0000561 sqlite3MemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000562 }
563}
564static void minMaxFinalize(sqlite_func *context){
danielk197788208052004-05-25 01:13:20 +0000565 sqlite3_value *pRes;
566 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
567
568 if( pRes->flags ){
569 switch( sqlite3_value_type(pRes) ){
570 case SQLITE3_INTEGER:
571 sqlite3_set_result_int(context, sqlite3_value_int(pRes));
572 break;
573 case SQLITE3_FLOAT:
574 sqlite3_set_result_double(context, sqlite3_value_float(pRes));
575 case SQLITE3_TEXT:
576 case SQLITE3_BLOB:
577 sqlite3_set_result_string(context,
578 sqlite3_value_data(pRes),
579 sqlite3_value_bytes(pRes));
580 break;
581 case SQLITE3_NULL:
582 default:
583 assert(0);
584 }
drh0bce8352002-02-28 00:41:10 +0000585 }
586}
drhdd5baa92002-02-27 19:50:59 +0000587
drhd3a149e2002-02-24 17:12:53 +0000588/*
drha2ed5602002-02-26 23:55:31 +0000589** This function registered all of the above C functions as SQL
590** functions. This should be the only routine in this file with
591** external linkage.
drhdc04c582002-02-24 01:55:15 +0000592*/
danielk19774adee202004-05-08 08:23:19 +0000593void sqlite3RegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000594 static struct {
595 char *zName;
drh268380c2004-02-25 13:47:31 +0000596 signed char nArg;
597 signed char dataType;
598 u8 argType; /* 0: none. 1: db 2: (-1) */
danielk197751ad0ec2004-05-24 12:39:02 +0000599 void (*xFunc)(sqlite_func*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000600 } aFuncs[] = {
drh268380c2004-02-25 13:47:31 +0000601 { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
602 { "min", 0, 0, 0, 0 },
603 { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
604 { "max", 0, 0, 2, 0 },
605 { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
danielk1977a37cdde2004-05-16 11:15:36 +0000606 { "classof", 1, SQLITE_TEXT, 0, typeofFunc }, /* FIX ME: hack */
drh268380c2004-02-25 13:47:31 +0000607 { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
608 { "substr", 3, SQLITE_TEXT, 0, substrFunc },
609 { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
610 { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
611 { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
612 { "upper", 1, SQLITE_TEXT, 0, upperFunc },
613 { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
614 { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
615 { "coalesce", 0, 0, 0, 0 },
616 { "coalesce", 1, 0, 0, 0 },
617 { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
618 { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
619 { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
620 { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
621 { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
danielk197796fc5fe2004-05-13 11:34:16 +0000622 { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc},
drh268380c2004-02-25 13:47:31 +0000623 { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
624 { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
625 { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
626 { "last_statement_change_count",
627 0, SQLITE_NUMERIC, 1, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000628#ifdef SQLITE_SOUNDEX
drh268380c2004-02-25 13:47:31 +0000629 { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000630#endif
drh193a6b42002-07-07 16:52:46 +0000631#ifdef SQLITE_TEST
drh268380c2004-02-25 13:47:31 +0000632 { "randstr", 2, SQLITE_TEXT, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000633#endif
drh0bce8352002-02-28 00:41:10 +0000634 };
635 static struct {
636 char *zName;
drh268380c2004-02-25 13:47:31 +0000637 signed char nArg;
638 signed char dataType;
639 u8 argType;
danielk19776ddcca52004-05-24 23:48:25 +0000640 void (*xStep)(sqlite_func*,int,sqlite3_value**);
drh0bce8352002-02-28 00:41:10 +0000641 void (*xFinalize)(sqlite_func*);
642 } aAggs[] = {
drh268380c2004-02-25 13:47:31 +0000643 { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
644 { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
645 { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
646 { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
647 { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
648 { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000649#if 0
drh268380c2004-02-25 13:47:31 +0000650 { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000651#endif
drh0bce8352002-02-28 00:41:10 +0000652 };
drh268380c2004-02-25 13:47:31 +0000653 static const char *azTypeFuncs[] = { "min", "max", "typeof" };
drh0bce8352002-02-28 00:41:10 +0000654 int i;
655
656 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000657 void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
danielk197724b03fd2004-05-10 10:34:34 +0000658 sqlite3_create_function(db, aFuncs[i].zName,
drh268380c2004-02-25 13:47:31 +0000659 aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
drhc9b84a12002-06-20 11:36:48 +0000660 if( aFuncs[i].xFunc ){
danielk197724b03fd2004-05-10 10:34:34 +0000661 sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
drhc9b84a12002-06-20 11:36:48 +0000662 }
drh0bce8352002-02-28 00:41:10 +0000663 }
664 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000665 void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
danielk197724b03fd2004-05-10 10:34:34 +0000666 sqlite3_create_aggregate(db, aAggs[i].zName,
drh268380c2004-02-25 13:47:31 +0000667 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
danielk197724b03fd2004-05-10 10:34:34 +0000668 sqlite3_function_type(db, aAggs[i].zName, aAggs[i].dataType);
drh0bce8352002-02-28 00:41:10 +0000669 }
danielk197751ad0ec2004-05-24 12:39:02 +0000670
drh268380c2004-02-25 13:47:31 +0000671 for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
672 int n = strlen(azTypeFuncs[i]);
danielk19774adee202004-05-08 08:23:19 +0000673 FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n);
drh268380c2004-02-25 13:47:31 +0000674 while( p ){
675 p->includeTypes = 1;
676 p = p->pNext;
677 }
678 }
danielk19774adee202004-05-08 08:23:19 +0000679 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000680}
danielk19774adee202004-05-08 08:23:19 +0000681
682
683