blob: eec3604297254a0329ed172693ab73d8e6b4e5af [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**
danielk19776ddcca52004-05-24 23:48:25 +000019** $Id: func.c,v 1.52 2004/05/24 23:48:26 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"
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*/
danielk197751ad0ec2004-05-24 12:39:02 +000031static void minmaxFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +000032 const char *zBest;
33 int i;
drh268380c2004-02-25 13:47:31 +000034 int (*xCompare)(const char*, const char*);
35 int mask; /* 0 for min() or 0xffffffff for max() */
danielk197751ad0ec2004-05-24 12:39:02 +000036 const char *zArg;
drh0bce8352002-02-28 00:41:10 +000037
drh89425d52002-02-28 03:04:48 +000038 if( argc==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +000039 mask = (int)sqlite3_user_data(context);
danielk197751ad0ec2004-05-24 12:39:02 +000040 zBest = sqlite3_value_data(argv[0]);
drh8912d102002-05-26 21:34:58 +000041 if( zBest==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +000042 zArg = sqlite3_value_data(argv[1]);
43 if( zArg[0]=='n' ){
danielk19774adee202004-05-08 08:23:19 +000044 xCompare = sqlite3Compare;
drh268380c2004-02-25 13:47:31 +000045 }else{
46 xCompare = strcmp;
47 }
48 for(i=2; i<argc; i+=2){
danielk197751ad0ec2004-05-24 12:39:02 +000049 zArg = sqlite3_value_data(argv[i]);
50 if( zArg==0 ) return;
51 if( (xCompare(zArg, zBest)^mask)<0 ){
52 zBest = zArg;
drh0bce8352002-02-28 00:41:10 +000053 }
54 }
danielk197724b03fd2004-05-10 10:34:34 +000055 sqlite3_set_result_string(context, zBest, -1);
drh0bce8352002-02-28 00:41:10 +000056}
drh0bce8352002-02-28 00:41:10 +000057
drh268380c2004-02-25 13:47:31 +000058/*
59** Return the type of the argument.
60*/
danielk197751ad0ec2004-05-24 12:39:02 +000061static void typeofFunc(sqlite_func *context, int argc, sqlite3_value **argv){
danielk197735bb9d02004-05-24 12:55:54 +000062 const char *z = 0;
drh268380c2004-02-25 13:47:31 +000063 assert( argc==2 );
danielk197735bb9d02004-05-24 12:55:54 +000064 switch( sqlite3_value_type(argv[0]) ){
65 case SQLITE3_NULL: z = "null" ; break;
66 case SQLITE3_INTEGER: z = "integer" ; break;
67 case SQLITE3_TEXT: z = "text" ; break;
68 case SQLITE3_FLOAT: z = "real" ; break;
69 case SQLITE3_BLOB: z = "blob" ; break;
70 }
71 sqlite3_set_result_string(context, z, -1);
drh0bce8352002-02-28 00:41:10 +000072}
73
74/*
75** Implementation of the length() function
76*/
danielk197751ad0ec2004-05-24 12:39:02 +000077static void lengthFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +000078 const char *z;
79 int len;
80
81 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +000082 z = sqlite3_value_data(argv[0]);
drh8912d102002-05-26 21:34:58 +000083 if( z==0 ) return;
drh0bce8352002-02-28 00:41:10 +000084#ifdef SQLITE_UTF8
drh8912d102002-05-26 21:34:58 +000085 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +000086#else
drh8912d102002-05-26 21:34:58 +000087 len = strlen(z);
drh0bce8352002-02-28 00:41:10 +000088#endif
danielk197724b03fd2004-05-10 10:34:34 +000089 sqlite3_set_result_int(context, len);
drh0bce8352002-02-28 00:41:10 +000090}
91
92/*
93** Implementation of the abs() function
94*/
danielk197751ad0ec2004-05-24 12:39:02 +000095static void absFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +000096 const char *z;
97 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +000098 z = sqlite3_value_data(argv[0]);
drh8912d102002-05-26 21:34:58 +000099 if( z==0 ) return;
100 if( z[0]=='-' && isdigit(z[1]) ) z++;
danielk197724b03fd2004-05-10 10:34:34 +0000101 sqlite3_set_result_string(context, z, -1);
drh0bce8352002-02-28 00:41:10 +0000102}
103
104/*
105** Implementation of the substr() function
106*/
danielk197751ad0ec2004-05-24 12:39:02 +0000107static void substrFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000108 const char *z;
109#ifdef SQLITE_UTF8
110 const char *z2;
111 int i;
112#endif
113 int p1, p2, len;
114 assert( argc==3 );
danielk197751ad0ec2004-05-24 12:39:02 +0000115 z = sqlite3_value_data(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000116 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000117 p1 = sqlite3_value_int(argv[1]);
118 p2 = sqlite3_value_int(argv[2]);
drh0bce8352002-02-28 00:41:10 +0000119#ifdef SQLITE_UTF8
drh47c8a672002-02-28 04:00:12 +0000120 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000121#else
122 len = strlen(z);
123#endif
124 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000125 p1 += len;
drh653bc752002-02-28 03:31:10 +0000126 if( p1<0 ){
127 p2 += p1;
128 p1 = 0;
129 }
drh0bce8352002-02-28 00:41:10 +0000130 }else if( p1>0 ){
131 p1--;
132 }
133 if( p1+p2>len ){
134 p2 = len-p1;
135 }
136#ifdef SQLITE_UTF8
drh77396302004-01-02 13:17:48 +0000137 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000138 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000139 }
drh47c8a672002-02-28 04:00:12 +0000140 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000141 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000142 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000143 }
drh47c8a672002-02-28 04:00:12 +0000144 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh0bce8352002-02-28 00:41:10 +0000145#endif
drh653bc752002-02-28 03:31:10 +0000146 if( p2<0 ) p2 = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000147 sqlite3_set_result_string(context, &z[p1], p2);
drh0bce8352002-02-28 00:41:10 +0000148}
149
150/*
151** Implementation of the round() function
152*/
danielk197751ad0ec2004-05-24 12:39:02 +0000153static void roundFunc(sqlite_func *context, int argc, sqlite3_value **argv){
154 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000155 double r;
156 char zBuf[100];
157 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000158 if( argc==2 ){
159 if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
160 n = sqlite3_value_int(argv[1]);
161 if( n>30 ) n = 30;
162 if( n<0 ) n = 0;
163 }
164 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
165 r = sqlite3_value_float(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000166 sprintf(zBuf,"%.*f",n,r);
danielk197724b03fd2004-05-10 10:34:34 +0000167 sqlite3_set_result_string(context, zBuf, -1);
drh0bce8352002-02-28 00:41:10 +0000168}
drhdc04c582002-02-24 01:55:15 +0000169
170/*
171** Implementation of the upper() and lower() SQL functions.
172*/
danielk197751ad0ec2004-05-24 12:39:02 +0000173static void upperFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000174 char *z;
175 int i;
danielk197751ad0ec2004-05-24 12:39:02 +0000176 if( argc<1 ) return;
177 z = sqlite3_set_result_string(context, sqlite3_value_data(argv[0]), -1);
drhdc04c582002-02-24 01:55:15 +0000178 if( z==0 ) return;
179 for(i=0; z[i]; i++){
180 if( islower(z[i]) ) z[i] = toupper(z[i]);
181 }
182}
danielk197751ad0ec2004-05-24 12:39:02 +0000183static void lowerFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000184 char *z;
185 int i;
danielk197751ad0ec2004-05-24 12:39:02 +0000186 if( argc<1 ) return;
187 z = sqlite3_set_result_string(context, sqlite3_value_data(argv[0]), -1);
drhdc04c582002-02-24 01:55:15 +0000188 if( z==0 ) return;
189 for(i=0; z[i]; i++){
190 if( isupper(z[i]) ) z[i] = tolower(z[i]);
191 }
192}
193
194/*
drhfbc99082002-02-28 03:14:18 +0000195** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000196** All three do the same thing. They return the first non-NULL
197** argument.
drh3212e182002-02-28 00:46:26 +0000198*/
danielk197751ad0ec2004-05-24 12:39:02 +0000199static void ifnullFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhfbc99082002-02-28 03:14:18 +0000200 int i;
201 for(i=0; i<argc; i++){
danielk197751ad0ec2004-05-24 12:39:02 +0000202 if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
203 sqlite3_set_result_string(context, sqlite3_value_data(argv[i]), -1);
drhfbc99082002-02-28 03:14:18 +0000204 break;
205 }
206 }
drh3212e182002-02-28 00:46:26 +0000207}
208
209/*
drhf9ffac92002-03-02 19:00:31 +0000210** Implementation of random(). Return a random integer.
211*/
danielk197751ad0ec2004-05-24 12:39:02 +0000212static void randomFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000213 int r;
danielk19774adee202004-05-08 08:23:19 +0000214 sqlite3Randomness(sizeof(r), &r);
danielk197724b03fd2004-05-10 10:34:34 +0000215 sqlite3_set_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000216}
217
218/*
drh6ed41ad2002-04-06 14:10:47 +0000219** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000220** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000221*/
danielk197751ad0ec2004-05-24 12:39:02 +0000222static void last_insert_rowid(
223 sqlite_func *context,
224 int arg,
225 sqlite3_value **argv
226){
danielk197724b03fd2004-05-10 10:34:34 +0000227 sqlite *db = sqlite3_user_data(context);
228 sqlite3_set_result_int(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000229}
230
rdcf146a772004-02-25 22:51:06 +0000231/*
232** Implementation of the change_count() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000233** value is the same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000234*/
danielk197751ad0ec2004-05-24 12:39:02 +0000235static void change_count(sqlite_func *context, int arg, sqlite3_value **argv){
danielk197724b03fd2004-05-10 10:34:34 +0000236 sqlite *db = sqlite3_user_data(context);
237 sqlite3_set_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000238}
rdcf146a772004-02-25 22:51:06 +0000239
240/*
241** Implementation of the last_statement_change_count() SQL function. The
danielk197751ad0ec2004-05-24 12:39:02 +0000242** return value is the same as the sqlite3_last_statement_changes() API
243** function.
rdcf146a772004-02-25 22:51:06 +0000244*/
danielk197751ad0ec2004-05-24 12:39:02 +0000245static void last_statement_change_count(
246 sqlite_func *context,
247 int arg,
248 sqlite3_value **argv
249){
danielk197724b03fd2004-05-10 10:34:34 +0000250 sqlite *db = sqlite3_user_data(context);
251 sqlite3_set_result_int(context, sqlite3_last_statement_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000252}
253
drh6ed41ad2002-04-06 14:10:47 +0000254/*
drh0ac65892002-04-20 14:24:41 +0000255** Implementation of the like() SQL function. This function implements
256** the build-in LIKE operator. The first argument to the function is the
257** string and the second argument is the pattern. So, the SQL statements:
258**
259** A LIKE B
260**
261** is implemented as like(A,B).
262*/
danielk197751ad0ec2004-05-24 12:39:02 +0000263static void likeFunc(
264 sqlite_func *context,
265 int argc,
266 sqlite3_value **argv
267){
268 const unsigned char *zA = sqlite3_value_data(argv[0]);
269 const unsigned char *zB = sqlite3_value_data(argv[1]);
270 if( zA && zB ){
271 sqlite3_set_result_int(context, sqlite3LikeCompare(zA, zB));
272 }
drh0ac65892002-04-20 14:24:41 +0000273}
274
275/*
276** Implementation of the glob() SQL function. This function implements
277** the build-in GLOB operator. The first argument to the function is the
278** string and the second argument is the pattern. So, the SQL statements:
279**
280** A GLOB B
281**
282** is implemented as glob(A,B).
283*/
danielk197751ad0ec2004-05-24 12:39:02 +0000284static void globFunc(sqlite_func *context, int arg, sqlite3_value **argv){
285 const unsigned char *zA = sqlite3_value_data(argv[0]);
286 const unsigned char *zB = sqlite3_value_data(argv[1]);
287 if( zA && zB ){
288 sqlite3_set_result_int(context, sqlite3GlobCompare(zA, zB));
289 }
drh8912d102002-05-26 21:34:58 +0000290}
291
292/*
293** Implementation of the NULLIF(x,y) function. The result is the first
294** argument if the arguments are different. The result is NULL if the
295** arguments are equal to each other.
296*/
danielk197751ad0ec2004-05-24 12:39:02 +0000297static void nullifFunc(sqlite_func *context, int argc, sqlite3_value **argv){
298 const unsigned char *zX = sqlite3_value_data(argv[0]);
299 const unsigned char *zY = sqlite3_value_data(argv[1]);
300 if( zX!=0 && sqlite3Compare(zX, zY)!=0 ){
301 sqlite3_set_result_string(context, zX, -1);
drh8912d102002-05-26 21:34:58 +0000302 }
drh0ac65892002-04-20 14:24:41 +0000303}
304
drh647cb0e2002-11-04 19:32:25 +0000305/*
306** Implementation of the VERSION(*) function. The result is the version
307** of the SQLite library that is running.
308*/
danielk197751ad0ec2004-05-24 12:39:02 +0000309static void versionFunc(sqlite_func *context, int argc, sqlite3_value **argv){
danielk197724b03fd2004-05-10 10:34:34 +0000310 sqlite3_set_result_string(context, sqlite3_version, -1);
drh647cb0e2002-11-04 19:32:25 +0000311}
312
drh47394702003-08-20 01:03:33 +0000313/*
314** EXPERIMENTAL - This is not an official function. The interface may
315** change. This function may disappear. Do not write code that depends
316** on this function.
317**
318** Implementation of the QUOTE() function. This function takes a single
319** argument. If the argument is numeric, the return value is the same as
320** the argument. If the argument is NULL, the return value is the string
321** "NULL". Otherwise, the argument is enclosed in single quotes with
322** single-quote escapes.
323*/
danielk197751ad0ec2004-05-24 12:39:02 +0000324static void quoteFunc(sqlite_func *context, int argc, sqlite3_value **argv){
325 const char *zArg = sqlite3_value_data(argv[0]);
drh47394702003-08-20 01:03:33 +0000326 if( argc<1 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000327 if( zArg==0 ){
danielk197724b03fd2004-05-10 10:34:34 +0000328 sqlite3_set_result_string(context, "NULL", 4);
danielk197751ad0ec2004-05-24 12:39:02 +0000329 }else if( sqlite3IsNumber(zArg, 0, TEXT_Utf8) ){
330 sqlite3_set_result_string(context, zArg, -1);
drh47394702003-08-20 01:03:33 +0000331 }else{
332 int i,j,n;
333 char *z;
danielk197751ad0ec2004-05-24 12:39:02 +0000334 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
drh47394702003-08-20 01:03:33 +0000335 z = sqliteMalloc( i+n+3 );
336 if( z==0 ) return;
337 z[0] = '\'';
danielk197751ad0ec2004-05-24 12:39:02 +0000338 for(i=0, j=1; zArg[i]; i++){
339 z[j++] = zArg[i];
340 if( zArg[i]=='\'' ){
drh47394702003-08-20 01:03:33 +0000341 z[j++] = '\'';
342 }
343 }
344 z[j++] = '\'';
345 z[j] = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000346 sqlite3_set_result_string(context, z, j);
drh47394702003-08-20 01:03:33 +0000347 sqliteFree(z);
348 }
349}
350
drhd24cc422003-03-27 12:51:24 +0000351#ifdef SQLITE_SOUNDEX
352/*
353** Compute the soundex encoding of a word.
354*/
danielk197751ad0ec2004-05-24 12:39:02 +0000355static void soundexFunc(sqlite_func *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000356 char zResult[8];
357 const char *zIn;
358 int i, j;
359 static const unsigned char iCode[] = {
360 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
365 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
366 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
367 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
368 };
369 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +0000370 zIn = sqlite3_value_data(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000371 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
372 if( zIn[i] ){
373 zResult[0] = toupper(zIn[i]);
374 for(j=1; j<4 && zIn[i]; i++){
375 int code = iCode[zIn[i]&0x7f];
376 if( code>0 ){
377 zResult[j++] = code + '0';
378 }
379 }
380 while( j<4 ){
381 zResult[j++] = '0';
382 }
383 zResult[j] = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000384 sqlite3_set_result_string(context, zResult, 4);
drhd24cc422003-03-27 12:51:24 +0000385 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000386 sqlite3_set_result_string(context, "?000", 4);
drhd24cc422003-03-27 12:51:24 +0000387 }
388}
389#endif
390
drh193a6b42002-07-07 16:52:46 +0000391#ifdef SQLITE_TEST
392/*
393** This function generates a string of random characters. Used for
394** generating test data.
395*/
danielk197751ad0ec2004-05-24 12:39:02 +0000396static void randStr(sqlite_func *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000397 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000398 "abcdefghijklmnopqrstuvwxyz"
399 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
400 "0123456789"
401 ".-!,:*^+=_|?/<> ";
402 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000403 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000404 if( argc>=1 ){
danielk197751ad0ec2004-05-24 12:39:02 +0000405 iMin = atoi(sqlite3_value_data(argv[0]));
drh193a6b42002-07-07 16:52:46 +0000406 if( iMin<0 ) iMin = 0;
407 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
408 }else{
409 iMin = 1;
410 }
411 if( argc>=2 ){
danielk197751ad0ec2004-05-24 12:39:02 +0000412 iMax = atoi(sqlite3_value_data(argv[1]));
drh193a6b42002-07-07 16:52:46 +0000413 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000414 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000415 }else{
416 iMax = 50;
417 }
418 n = iMin;
419 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000420 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000421 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000422 n += r%(iMax + 1 - iMin);
423 }
drh1dba7272004-01-16 13:58:18 +0000424 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000426 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000427 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000428 }
429 zBuf[n] = 0;
danielk197724b03fd2004-05-10 10:34:34 +0000430 sqlite3_set_result_string(context, zBuf, n);
drh193a6b42002-07-07 16:52:46 +0000431}
432#endif
433
drh0ac65892002-04-20 14:24:41 +0000434/*
drhd3a149e2002-02-24 17:12:53 +0000435** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000436** sum() or avg() aggregate computation.
437*/
438typedef struct SumCtx SumCtx;
439struct SumCtx {
440 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000441 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000442};
443
444/*
445** Routines used to compute the sum or average.
446*/
danielk19776ddcca52004-05-24 23:48:25 +0000447static void sumStep(sqlite_func *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000448 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000449 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000450 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000451 if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
452 p->sum += sqlite3_value_float(argv[0]);
drh739105c2002-05-29 23:22:23 +0000453 p->cnt++;
454 }
drhdd5baa92002-02-27 19:50:59 +0000455}
456static void sumFinalize(sqlite_func *context){
457 SumCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000458 p = sqlite3_aggregate_context(context, sizeof(*p));
459 sqlite3_set_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000460}
461static void avgFinalize(sqlite_func *context){
462 SumCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000463 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000464 if( p && p->cnt>0 ){
danielk197724b03fd2004-05-10 10:34:34 +0000465 sqlite3_set_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000466 }
467}
468
469/*
470** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000471** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000472*/
473typedef struct StdDevCtx StdDevCtx;
474struct StdDevCtx {
475 double sum; /* Sum of terms */
476 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000477 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000478};
479
drhef2daf52002-03-04 02:26:15 +0000480#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000481/*
482** Routines used to compute the standard deviation as an aggregate.
483*/
drh1350b032002-02-27 19:00:20 +0000484static void stdDevStep(sqlite_func *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000485 StdDevCtx *p;
486 double x;
drh1350b032002-02-27 19:00:20 +0000487 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000488 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000489 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000490 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000491 p->sum += x;
492 p->sum2 += x*x;
493 p->cnt++;
494 }
drhd3a149e2002-02-24 17:12:53 +0000495}
drh1350b032002-02-27 19:00:20 +0000496static void stdDevFinalize(sqlite_func *context){
danielk197724b03fd2004-05-10 10:34:34 +0000497 double rN = sqlite3_aggregate_count(context);
498 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000499 if( p && p->cnt>1 ){
500 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000501 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000502 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000503 }
drhd3a149e2002-02-24 17:12:53 +0000504}
drhef2daf52002-03-04 02:26:15 +0000505#endif
drhd3a149e2002-02-24 17:12:53 +0000506
drh0bce8352002-02-28 00:41:10 +0000507/*
508** The following structure keeps track of state information for the
509** count() aggregate function.
510*/
511typedef struct CountCtx CountCtx;
512struct CountCtx {
513 int n;
514};
drhdd5baa92002-02-27 19:50:59 +0000515
drh0bce8352002-02-28 00:41:10 +0000516/*
517** Routines to implement the count() aggregate function.
518*/
danielk19776ddcca52004-05-24 23:48:25 +0000519static void countStep(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000520 CountCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000521 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000522 if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000523 p->n++;
524 }
525}
526static void countFinalize(sqlite_func *context){
527 CountCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000528 p = sqlite3_aggregate_context(context, sizeof(*p));
529 sqlite3_set_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000530}
531
532/*
533** This function tracks state information for the min() and max()
534** aggregate functions.
535*/
536typedef struct MinMaxCtx MinMaxCtx;
537struct MinMaxCtx {
538 char *z; /* The best so far */
539 char zBuf[28]; /* Space that can be used for storage */
540};
541
542/*
543** Routines to implement min() and max() aggregate functions.
544*/
danielk19776ddcca52004-05-24 23:48:25 +0000545static void minmaxStep(sqlite_func *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000546 MinMaxCtx *p;
drh268380c2004-02-25 13:47:31 +0000547 int (*xCompare)(const char*, const char*);
548 int mask; /* 0 for min() or 0xffffffff for max() */
danielk19776ddcca52004-05-24 23:48:25 +0000549 const char *zArg0 = sqlite3_value_data(argv[0]);
550 const char *zArg1 = sqlite3_value_data(argv[1]);
drh268380c2004-02-25 13:47:31 +0000551
552 assert( argc==2 );
danielk19776ddcca52004-05-24 23:48:25 +0000553 if( zArg1[0]=='n' ){
danielk19774adee202004-05-08 08:23:19 +0000554 xCompare = sqlite3Compare;
drh268380c2004-02-25 13:47:31 +0000555 }else{
556 xCompare = strcmp;
drh0bce8352002-02-28 00:41:10 +0000557 }
danielk197724b03fd2004-05-10 10:34:34 +0000558 mask = (int)sqlite3_user_data(context);
559 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000560 if( p==0 || argc<1 || zArg0==0 ) return;
561 if( p->z==0 || (xCompare(zArg0,p->z)^mask)<0 ){
drh8912d102002-05-26 21:34:58 +0000562 int len;
drh00706be2004-01-30 14:49:16 +0000563 if( !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000564 sqliteFree(p->z);
565 }
danielk19776ddcca52004-05-24 23:48:25 +0000566 len = strlen(zArg0);
drh00706be2004-01-30 14:49:16 +0000567 if( len < sizeof(p->zBuf)-1 ){
568 p->z = &p->zBuf[1];
569 p->zBuf[0] = 1;
drh0bce8352002-02-28 00:41:10 +0000570 }else{
drh8912d102002-05-26 21:34:58 +0000571 p->z = sqliteMalloc( len+1 );
drh00706be2004-01-30 14:49:16 +0000572 p->zBuf[0] = 0;
drh8912d102002-05-26 21:34:58 +0000573 if( p->z==0 ) return;
drh0bce8352002-02-28 00:41:10 +0000574 }
danielk19776ddcca52004-05-24 23:48:25 +0000575 strcpy(p->z, zArg0);
drh0bce8352002-02-28 00:41:10 +0000576 }
577}
578static void minMaxFinalize(sqlite_func *context){
579 MinMaxCtx *p;
danielk197724b03fd2004-05-10 10:34:34 +0000580 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000581 if( p && p->z ){
danielk197724b03fd2004-05-10 10:34:34 +0000582 sqlite3_set_result_string(context, p->z, strlen(p->z));
drh0bce8352002-02-28 00:41:10 +0000583 }
drh00706be2004-01-30 14:49:16 +0000584 if( p && !p->zBuf[0] ){
drh0bce8352002-02-28 00:41:10 +0000585 sqliteFree(p->z);
586 }
587}
drhdd5baa92002-02-27 19:50:59 +0000588
drhd3a149e2002-02-24 17:12:53 +0000589/*
drha2ed5602002-02-26 23:55:31 +0000590** This function registered all of the above C functions as SQL
591** functions. This should be the only routine in this file with
592** external linkage.
drhdc04c582002-02-24 01:55:15 +0000593*/
danielk19774adee202004-05-08 08:23:19 +0000594void sqlite3RegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000595 static struct {
596 char *zName;
drh268380c2004-02-25 13:47:31 +0000597 signed char nArg;
598 signed char dataType;
599 u8 argType; /* 0: none. 1: db 2: (-1) */
danielk197751ad0ec2004-05-24 12:39:02 +0000600 void (*xFunc)(sqlite_func*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000601 } aFuncs[] = {
drh268380c2004-02-25 13:47:31 +0000602 { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
603 { "min", 0, 0, 0, 0 },
604 { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
605 { "max", 0, 0, 2, 0 },
606 { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
danielk1977a37cdde2004-05-16 11:15:36 +0000607 { "classof", 1, SQLITE_TEXT, 0, typeofFunc }, /* FIX ME: hack */
drh268380c2004-02-25 13:47:31 +0000608 { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
609 { "substr", 3, SQLITE_TEXT, 0, substrFunc },
610 { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
611 { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
612 { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
613 { "upper", 1, SQLITE_TEXT, 0, upperFunc },
614 { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
615 { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
616 { "coalesce", 0, 0, 0, 0 },
617 { "coalesce", 1, 0, 0, 0 },
618 { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
619 { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
620 { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
621 { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
622 { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
danielk197796fc5fe2004-05-13 11:34:16 +0000623 { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc},
drh268380c2004-02-25 13:47:31 +0000624 { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
625 { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
626 { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
627 { "last_statement_change_count",
628 0, SQLITE_NUMERIC, 1, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000629#ifdef SQLITE_SOUNDEX
drh268380c2004-02-25 13:47:31 +0000630 { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000631#endif
drh193a6b42002-07-07 16:52:46 +0000632#ifdef SQLITE_TEST
drh268380c2004-02-25 13:47:31 +0000633 { "randstr", 2, SQLITE_TEXT, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000634#endif
drh0bce8352002-02-28 00:41:10 +0000635 };
636 static struct {
637 char *zName;
drh268380c2004-02-25 13:47:31 +0000638 signed char nArg;
639 signed char dataType;
640 u8 argType;
danielk19776ddcca52004-05-24 23:48:25 +0000641 void (*xStep)(sqlite_func*,int,sqlite3_value**);
drh0bce8352002-02-28 00:41:10 +0000642 void (*xFinalize)(sqlite_func*);
643 } aAggs[] = {
drh268380c2004-02-25 13:47:31 +0000644 { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
645 { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
646 { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
647 { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
648 { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
649 { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000650#if 0
drh268380c2004-02-25 13:47:31 +0000651 { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000652#endif
drh0bce8352002-02-28 00:41:10 +0000653 };
drh268380c2004-02-25 13:47:31 +0000654 static const char *azTypeFuncs[] = { "min", "max", "typeof" };
drh0bce8352002-02-28 00:41:10 +0000655 int i;
656
657 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000658 void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
danielk197724b03fd2004-05-10 10:34:34 +0000659 sqlite3_create_function(db, aFuncs[i].zName,
drh268380c2004-02-25 13:47:31 +0000660 aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
drhc9b84a12002-06-20 11:36:48 +0000661 if( aFuncs[i].xFunc ){
danielk197724b03fd2004-05-10 10:34:34 +0000662 sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
drhc9b84a12002-06-20 11:36:48 +0000663 }
drh0bce8352002-02-28 00:41:10 +0000664 }
665 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000666 void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
danielk197724b03fd2004-05-10 10:34:34 +0000667 sqlite3_create_aggregate(db, aAggs[i].zName,
drh268380c2004-02-25 13:47:31 +0000668 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
danielk197724b03fd2004-05-10 10:34:34 +0000669 sqlite3_function_type(db, aAggs[i].zName, aAggs[i].dataType);
drh0bce8352002-02-28 00:41:10 +0000670 }
danielk197751ad0ec2004-05-24 12:39:02 +0000671
drh268380c2004-02-25 13:47:31 +0000672 for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
673 int n = strlen(azTypeFuncs[i]);
danielk19774adee202004-05-08 08:23:19 +0000674 FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n);
drh268380c2004-02-25 13:47:31 +0000675 while( p ){
676 p->includeTypes = 1;
677 p = p->pNext;
678 }
679 }
danielk19774adee202004-05-08 08:23:19 +0000680 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000681}
danielk19774adee202004-05-08 08:23:19 +0000682
683
684