blob: 5e63c3aa02abefbad7b2848ac9fa1095585e6697 [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**
danielk19770ae8b832004-05-25 12:05:56 +000019** $Id: func.c,v 1.55 2004/05/25 12:05:57 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*/
danielk19770ae8b832004-05-25 12:05:56 +000032static void minmaxFunc(sqlite3_context *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 }
danielk19777e18c252004-05-25 11:47:24 +000056 sqlite3_result_text(context, zBest, -1, 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*/
danielk19770ae8b832004-05-25 12:05:56 +000062static void typeofFunc(sqlite3_context *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 }
danielk19777e18c252004-05-25 11:47:24 +000072 sqlite3_result_text(context, z, -1, 0);
drh0bce8352002-02-28 00:41:10 +000073}
74
75/*
76** Implementation of the length() function
77*/
danielk19770ae8b832004-05-25 12:05:56 +000078static void lengthFunc(sqlite3_context *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
danielk19777e18c252004-05-25 11:47:24 +000090 sqlite3_result_int32(context, len);
drh0bce8352002-02-28 00:41:10 +000091}
92
93/*
94** Implementation of the abs() function
95*/
danielk19770ae8b832004-05-25 12:05:56 +000096static void absFunc(sqlite3_context *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++;
danielk19777e18c252004-05-25 11:47:24 +0000102 sqlite3_result_text(context, z, -1, 1);
drh0bce8352002-02-28 00:41:10 +0000103}
104
105/*
106** Implementation of the substr() function
107*/
danielk19770ae8b832004-05-25 12:05:56 +0000108static void substrFunc(sqlite3_context *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;
danielk19777e18c252004-05-25 11:47:24 +0000148 sqlite3_result_text(context, &z[p1], p2, 1);
drh0bce8352002-02-28 00:41:10 +0000149}
150
151/*
152** Implementation of the round() function
153*/
danielk19770ae8b832004-05-25 12:05:56 +0000154static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000155 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);
danielk19777e18c252004-05-25 11:47:24 +0000168 sqlite3_result_text(context, zBuf, -1, 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*/
danielk19770ae8b832004-05-25 12:05:56 +0000174static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000175 char *z;
176 int i;
danielk19777e18c252004-05-25 11:47:24 +0000177 if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
178 z = sqliteMalloc(sqlite3_value_bytes(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000179 if( z==0 ) return;
danielk19777e18c252004-05-25 11:47:24 +0000180 strcpy(z, sqlite3_value_data(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000181 for(i=0; z[i]; i++){
182 if( islower(z[i]) ) z[i] = toupper(z[i]);
183 }
danielk19777e18c252004-05-25 11:47:24 +0000184 sqlite3_result_text(context, z, -1, 1);
185 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000186}
danielk19770ae8b832004-05-25 12:05:56 +0000187static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000188 char *z;
189 int i;
danielk19777e18c252004-05-25 11:47:24 +0000190 if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
191 z = sqliteMalloc(sqlite3_value_bytes(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000192 if( z==0 ) return;
danielk19777e18c252004-05-25 11:47:24 +0000193 strcpy(z, sqlite3_value_data(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000194 for(i=0; z[i]; i++){
195 if( isupper(z[i]) ) z[i] = tolower(z[i]);
196 }
danielk19777e18c252004-05-25 11:47:24 +0000197 sqlite3_result_text(context, z, -1, 1);
198 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000199}
200
201/*
drhfbc99082002-02-28 03:14:18 +0000202** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000203** All three do the same thing. They return the first non-NULL
204** argument.
drh3212e182002-02-28 00:46:26 +0000205*/
danielk19770ae8b832004-05-25 12:05:56 +0000206static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhfbc99082002-02-28 03:14:18 +0000207 int i;
208 for(i=0; i<argc; i++){
danielk197751ad0ec2004-05-24 12:39:02 +0000209 if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
danielk19777e18c252004-05-25 11:47:24 +0000210 sqlite3_result_text(context, sqlite3_value_data(argv[i]), -1, 1);
drhfbc99082002-02-28 03:14:18 +0000211 break;
212 }
213 }
drh3212e182002-02-28 00:46:26 +0000214}
215
216/*
drhf9ffac92002-03-02 19:00:31 +0000217** Implementation of random(). Return a random integer.
218*/
danielk19770ae8b832004-05-25 12:05:56 +0000219static void randomFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000220 int r;
danielk19774adee202004-05-08 08:23:19 +0000221 sqlite3Randomness(sizeof(r), &r);
danielk19777e18c252004-05-25 11:47:24 +0000222 sqlite3_result_int32(context, r);
drhf9ffac92002-03-02 19:00:31 +0000223}
224
225/*
drh6ed41ad2002-04-06 14:10:47 +0000226** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000227** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000228*/
danielk197751ad0ec2004-05-24 12:39:02 +0000229static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000230 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000231 int arg,
232 sqlite3_value **argv
233){
danielk197724b03fd2004-05-10 10:34:34 +0000234 sqlite *db = sqlite3_user_data(context);
danielk19777e18c252004-05-25 11:47:24 +0000235 sqlite3_result_int32(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000236}
237
rdcf146a772004-02-25 22:51:06 +0000238/*
239** Implementation of the change_count() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000240** value is the same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000241*/
danielk19770ae8b832004-05-25 12:05:56 +0000242static void change_count(sqlite3_context *context, int arg, sqlite3_value **argv){
danielk197724b03fd2004-05-10 10:34:34 +0000243 sqlite *db = sqlite3_user_data(context);
danielk19777e18c252004-05-25 11:47:24 +0000244 sqlite3_result_int32(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000245}
rdcf146a772004-02-25 22:51:06 +0000246
247/*
248** Implementation of the last_statement_change_count() SQL function. The
danielk197751ad0ec2004-05-24 12:39:02 +0000249** return value is the same as the sqlite3_last_statement_changes() API
250** function.
rdcf146a772004-02-25 22:51:06 +0000251*/
danielk197751ad0ec2004-05-24 12:39:02 +0000252static void last_statement_change_count(
danielk19770ae8b832004-05-25 12:05:56 +0000253 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000254 int arg,
255 sqlite3_value **argv
256){
danielk197724b03fd2004-05-10 10:34:34 +0000257 sqlite *db = sqlite3_user_data(context);
danielk19777e18c252004-05-25 11:47:24 +0000258 sqlite3_result_int32(context, sqlite3_last_statement_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000259}
260
drh6ed41ad2002-04-06 14:10:47 +0000261/*
drh0ac65892002-04-20 14:24:41 +0000262** Implementation of the like() SQL function. This function implements
263** the build-in LIKE operator. The first argument to the function is the
264** string and the second argument is the pattern. So, the SQL statements:
265**
266** A LIKE B
267**
268** is implemented as like(A,B).
269*/
danielk197751ad0ec2004-05-24 12:39:02 +0000270static void likeFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000271 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000272 int argc,
273 sqlite3_value **argv
274){
275 const unsigned char *zA = sqlite3_value_data(argv[0]);
276 const unsigned char *zB = sqlite3_value_data(argv[1]);
277 if( zA && zB ){
danielk19777e18c252004-05-25 11:47:24 +0000278 sqlite3_result_int32(context, sqlite3LikeCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000279 }
drh0ac65892002-04-20 14:24:41 +0000280}
281
282/*
283** Implementation of the glob() SQL function. This function implements
284** the build-in GLOB operator. The first argument to the function is the
285** string and the second argument is the pattern. So, the SQL statements:
286**
287** A GLOB B
288**
289** is implemented as glob(A,B).
290*/
danielk19770ae8b832004-05-25 12:05:56 +0000291static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000292 const unsigned char *zA = sqlite3_value_data(argv[0]);
293 const unsigned char *zB = sqlite3_value_data(argv[1]);
294 if( zA && zB ){
danielk19777e18c252004-05-25 11:47:24 +0000295 sqlite3_result_int32(context, sqlite3GlobCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000296 }
drh8912d102002-05-26 21:34:58 +0000297}
298
299/*
300** Implementation of the NULLIF(x,y) function. The result is the first
301** argument if the arguments are different. The result is NULL if the
302** arguments are equal to each other.
303*/
danielk19770ae8b832004-05-25 12:05:56 +0000304static void nullifFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000305 const unsigned char *zX = sqlite3_value_data(argv[0]);
306 const unsigned char *zY = sqlite3_value_data(argv[1]);
307 if( zX!=0 && sqlite3Compare(zX, zY)!=0 ){
danielk19777e18c252004-05-25 11:47:24 +0000308 sqlite3_result_text(context, zX, -1, 1);
drh8912d102002-05-26 21:34:58 +0000309 }
drh0ac65892002-04-20 14:24:41 +0000310}
311
drh647cb0e2002-11-04 19:32:25 +0000312/*
313** Implementation of the VERSION(*) function. The result is the version
314** of the SQLite library that is running.
315*/
danielk19770ae8b832004-05-25 12:05:56 +0000316static void versionFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19777e18c252004-05-25 11:47:24 +0000317 sqlite3_result_text(context, sqlite3_version, -1, 0);
drh647cb0e2002-11-04 19:32:25 +0000318}
319
drh47394702003-08-20 01:03:33 +0000320/*
321** EXPERIMENTAL - This is not an official function. The interface may
322** change. This function may disappear. Do not write code that depends
323** on this function.
324**
325** Implementation of the QUOTE() function. This function takes a single
326** argument. If the argument is numeric, the return value is the same as
327** the argument. If the argument is NULL, the return value is the string
328** "NULL". Otherwise, the argument is enclosed in single quotes with
329** single-quote escapes.
330*/
danielk19770ae8b832004-05-25 12:05:56 +0000331static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000332 const char *zArg = sqlite3_value_data(argv[0]);
drh47394702003-08-20 01:03:33 +0000333 if( argc<1 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000334 if( zArg==0 ){
danielk19777e18c252004-05-25 11:47:24 +0000335 sqlite3_result_text(context, "NULL", 4, 0);
danielk197751ad0ec2004-05-24 12:39:02 +0000336 }else if( sqlite3IsNumber(zArg, 0, TEXT_Utf8) ){
danielk19777e18c252004-05-25 11:47:24 +0000337 sqlite3_result_text(context, zArg, -1, 1);
drh47394702003-08-20 01:03:33 +0000338 }else{
339 int i,j,n;
340 char *z;
danielk197751ad0ec2004-05-24 12:39:02 +0000341 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
drh47394702003-08-20 01:03:33 +0000342 z = sqliteMalloc( i+n+3 );
343 if( z==0 ) return;
344 z[0] = '\'';
danielk197751ad0ec2004-05-24 12:39:02 +0000345 for(i=0, j=1; zArg[i]; i++){
346 z[j++] = zArg[i];
347 if( zArg[i]=='\'' ){
drh47394702003-08-20 01:03:33 +0000348 z[j++] = '\'';
349 }
350 }
351 z[j++] = '\'';
352 z[j] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000353 sqlite3_result_text(context, z, j, 1);
drh47394702003-08-20 01:03:33 +0000354 sqliteFree(z);
355 }
356}
357
drhd24cc422003-03-27 12:51:24 +0000358#ifdef SQLITE_SOUNDEX
359/*
360** Compute the soundex encoding of a word.
361*/
danielk19770ae8b832004-05-25 12:05:56 +0000362static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000363 char zResult[8];
364 const char *zIn;
365 int i, j;
366 static const unsigned char iCode[] = {
367 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
368 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
369 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
371 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
372 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
373 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
374 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
375 };
376 assert( argc==1 );
danielk197751ad0ec2004-05-24 12:39:02 +0000377 zIn = sqlite3_value_data(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000378 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
379 if( zIn[i] ){
380 zResult[0] = toupper(zIn[i]);
381 for(j=1; j<4 && zIn[i]; i++){
382 int code = iCode[zIn[i]&0x7f];
383 if( code>0 ){
384 zResult[j++] = code + '0';
385 }
386 }
387 while( j<4 ){
388 zResult[j++] = '0';
389 }
390 zResult[j] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000391 sqlite3_result_text(context, zResult, 4, 1);
drhd24cc422003-03-27 12:51:24 +0000392 }else{
danielk19777e18c252004-05-25 11:47:24 +0000393 sqlite3_result_text(context, "?000", 4, 0);
drhd24cc422003-03-27 12:51:24 +0000394 }
395}
396#endif
397
drh193a6b42002-07-07 16:52:46 +0000398#ifdef SQLITE_TEST
399/*
400** This function generates a string of random characters. Used for
401** generating test data.
402*/
danielk19770ae8b832004-05-25 12:05:56 +0000403static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000404 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000405 "abcdefghijklmnopqrstuvwxyz"
406 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
407 "0123456789"
408 ".-!,:*^+=_|?/<> ";
409 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000410 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000411 if( argc>=1 ){
danielk197751ad0ec2004-05-24 12:39:02 +0000412 iMin = atoi(sqlite3_value_data(argv[0]));
drh193a6b42002-07-07 16:52:46 +0000413 if( iMin<0 ) iMin = 0;
414 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
415 }else{
416 iMin = 1;
417 }
418 if( argc>=2 ){
danielk197751ad0ec2004-05-24 12:39:02 +0000419 iMax = atoi(sqlite3_value_data(argv[1]));
drh193a6b42002-07-07 16:52:46 +0000420 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000421 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000422 }else{
423 iMax = 50;
424 }
425 n = iMin;
426 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000427 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000428 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000429 n += r%(iMax + 1 - iMin);
430 }
drh1dba7272004-01-16 13:58:18 +0000431 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000432 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000433 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000434 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000435 }
436 zBuf[n] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000437 sqlite3_result_text(context, zBuf, n, 1);
drh193a6b42002-07-07 16:52:46 +0000438}
439#endif
440
drh0ac65892002-04-20 14:24:41 +0000441/*
drhd3a149e2002-02-24 17:12:53 +0000442** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000443** sum() or avg() aggregate computation.
444*/
445typedef struct SumCtx SumCtx;
446struct SumCtx {
447 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000448 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000449};
450
451/*
452** Routines used to compute the sum or average.
453*/
danielk19770ae8b832004-05-25 12:05:56 +0000454static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000455 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000456 if( argc<1 ) return;
danielk19770ae8b832004-05-25 12:05:56 +0000457 p = sqlite3_get_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000458 if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
459 p->sum += sqlite3_value_float(argv[0]);
drh739105c2002-05-29 23:22:23 +0000460 p->cnt++;
461 }
drhdd5baa92002-02-27 19:50:59 +0000462}
danielk19770ae8b832004-05-25 12:05:56 +0000463static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000464 SumCtx *p;
danielk19770ae8b832004-05-25 12:05:56 +0000465 p = sqlite3_get_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000466 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000467}
danielk19770ae8b832004-05-25 12:05:56 +0000468static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000469 SumCtx *p;
danielk19770ae8b832004-05-25 12:05:56 +0000470 p = sqlite3_get_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000471 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000472 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000473 }
474}
475
476/*
477** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000478** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000479*/
480typedef struct StdDevCtx StdDevCtx;
481struct StdDevCtx {
482 double sum; /* Sum of terms */
483 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000484 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000485};
486
drhef2daf52002-03-04 02:26:15 +0000487#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000488/*
489** Routines used to compute the standard deviation as an aggregate.
490*/
danielk19770ae8b832004-05-25 12:05:56 +0000491static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000492 StdDevCtx *p;
493 double x;
drh1350b032002-02-27 19:00:20 +0000494 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000495 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000496 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000497 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000498 p->sum += x;
499 p->sum2 += x*x;
500 p->cnt++;
501 }
drhd3a149e2002-02-24 17:12:53 +0000502}
danielk19770ae8b832004-05-25 12:05:56 +0000503static void stdDevFinalize(sqlite3_context *context){
danielk197724b03fd2004-05-10 10:34:34 +0000504 double rN = sqlite3_aggregate_count(context);
505 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000506 if( p && p->cnt>1 ){
507 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000508 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000509 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000510 }
drhd3a149e2002-02-24 17:12:53 +0000511}
drhef2daf52002-03-04 02:26:15 +0000512#endif
drhd3a149e2002-02-24 17:12:53 +0000513
drh0bce8352002-02-28 00:41:10 +0000514/*
515** The following structure keeps track of state information for the
516** count() aggregate function.
517*/
518typedef struct CountCtx CountCtx;
519struct CountCtx {
520 int n;
521};
drhdd5baa92002-02-27 19:50:59 +0000522
drh0bce8352002-02-28 00:41:10 +0000523/*
524** Routines to implement the count() aggregate function.
525*/
danielk19770ae8b832004-05-25 12:05:56 +0000526static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000527 CountCtx *p;
danielk19770ae8b832004-05-25 12:05:56 +0000528 p = sqlite3_get_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000529 if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000530 p->n++;
531 }
532}
danielk19770ae8b832004-05-25 12:05:56 +0000533static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000534 CountCtx *p;
danielk19770ae8b832004-05-25 12:05:56 +0000535 p = sqlite3_get_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000536 sqlite3_result_int32(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000537}
538
539/*
540** This function tracks state information for the min() and max()
541** aggregate functions.
542*/
543typedef struct MinMaxCtx MinMaxCtx;
544struct MinMaxCtx {
545 char *z; /* The best so far */
546 char zBuf[28]; /* Space that can be used for storage */
547};
548
549/*
550** Routines to implement min() and max() aggregate functions.
551*/
danielk19770ae8b832004-05-25 12:05:56 +0000552static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000553 int max = 0;
554 int cmp = 0;
555 Mem *pArg = (Mem *)argv[0];
danielk19770ae8b832004-05-25 12:05:56 +0000556 Mem *pBest = (Mem *)sqlite3_get_context(context, sizeof(*pBest));
drh268380c2004-02-25 13:47:31 +0000557
danielk197788208052004-05-25 01:13:20 +0000558 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
559
560 if( pBest->flags ){
danielk19777e18c252004-05-25 11:47:24 +0000561 /* This step function is used for both the min() and max() aggregates,
562 ** the only difference between the two being that the sense of the
563 ** comparison is inverted. For the max() aggregate, the
564 ** sqlite3_user_data() function returns (void *)-1. For min() it
565 ** returns (void *)db, where db is the sqlite3* database pointer.
566 ** Therefore the next statement sets variable 'max' to 1 for the max()
567 ** aggregate, or 0 for min().
568 */
danielk197788208052004-05-25 01:13:20 +0000569 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
570 cmp = sqlite3MemCompare(pBest, pArg, 0);
571 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000572 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000573 }
drh268380c2004-02-25 13:47:31 +0000574 }else{
danielk19777e18c252004-05-25 11:47:24 +0000575 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000576 }
577}
danielk19770ae8b832004-05-25 12:05:56 +0000578static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000579 sqlite3_value *pRes;
danielk19770ae8b832004-05-25 12:05:56 +0000580 pRes = (sqlite3_value *)sqlite3_get_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +0000581
582 if( pRes->flags ){
583 switch( sqlite3_value_type(pRes) ){
584 case SQLITE3_INTEGER:
danielk19777e18c252004-05-25 11:47:24 +0000585 sqlite3_result_int32(context, sqlite3_value_int(pRes));
danielk197788208052004-05-25 01:13:20 +0000586 break;
587 case SQLITE3_FLOAT:
danielk19777e18c252004-05-25 11:47:24 +0000588 sqlite3_result_double(context, sqlite3_value_float(pRes));
danielk197788208052004-05-25 01:13:20 +0000589 case SQLITE3_TEXT:
590 case SQLITE3_BLOB:
danielk19777e18c252004-05-25 11:47:24 +0000591 sqlite3_result_text(context,
592 sqlite3_value_data(pRes), sqlite3_value_bytes(pRes), 1);
danielk197788208052004-05-25 01:13:20 +0000593 break;
594 case SQLITE3_NULL:
595 default:
596 assert(0);
597 }
drh0bce8352002-02-28 00:41:10 +0000598 }
599}
drhdd5baa92002-02-27 19:50:59 +0000600
drhd3a149e2002-02-24 17:12:53 +0000601/*
drha2ed5602002-02-26 23:55:31 +0000602** This function registered all of the above C functions as SQL
603** functions. This should be the only routine in this file with
604** external linkage.
drhdc04c582002-02-24 01:55:15 +0000605*/
danielk19774adee202004-05-08 08:23:19 +0000606void sqlite3RegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000607 static struct {
608 char *zName;
drh268380c2004-02-25 13:47:31 +0000609 signed char nArg;
610 signed char dataType;
611 u8 argType; /* 0: none. 1: db 2: (-1) */
danielk19770ae8b832004-05-25 12:05:56 +0000612 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000613 } aFuncs[] = {
drh268380c2004-02-25 13:47:31 +0000614 { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
615 { "min", 0, 0, 0, 0 },
616 { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
617 { "max", 0, 0, 2, 0 },
618 { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
danielk1977a37cdde2004-05-16 11:15:36 +0000619 { "classof", 1, SQLITE_TEXT, 0, typeofFunc }, /* FIX ME: hack */
drh268380c2004-02-25 13:47:31 +0000620 { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
621 { "substr", 3, SQLITE_TEXT, 0, substrFunc },
622 { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
623 { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
624 { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
625 { "upper", 1, SQLITE_TEXT, 0, upperFunc },
626 { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
627 { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
628 { "coalesce", 0, 0, 0, 0 },
629 { "coalesce", 1, 0, 0, 0 },
630 { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
631 { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
632 { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
633 { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
634 { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
danielk197796fc5fe2004-05-13 11:34:16 +0000635 { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc},
drh268380c2004-02-25 13:47:31 +0000636 { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
637 { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
638 { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
639 { "last_statement_change_count",
640 0, SQLITE_NUMERIC, 1, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000641#ifdef SQLITE_SOUNDEX
drh268380c2004-02-25 13:47:31 +0000642 { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000643#endif
drh193a6b42002-07-07 16:52:46 +0000644#ifdef SQLITE_TEST
drh268380c2004-02-25 13:47:31 +0000645 { "randstr", 2, SQLITE_TEXT, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000646#endif
drh0bce8352002-02-28 00:41:10 +0000647 };
648 static struct {
649 char *zName;
drh268380c2004-02-25 13:47:31 +0000650 signed char nArg;
651 signed char dataType;
652 u8 argType;
danielk19770ae8b832004-05-25 12:05:56 +0000653 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
654 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +0000655 } aAggs[] = {
drh268380c2004-02-25 13:47:31 +0000656 { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
657 { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
658 { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
659 { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
660 { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
661 { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000662#if 0
drh268380c2004-02-25 13:47:31 +0000663 { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000664#endif
drh0bce8352002-02-28 00:41:10 +0000665 };
drh268380c2004-02-25 13:47:31 +0000666 static const char *azTypeFuncs[] = { "min", "max", "typeof" };
drh0bce8352002-02-28 00:41:10 +0000667 int i;
668
669 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000670 void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
danielk197724b03fd2004-05-10 10:34:34 +0000671 sqlite3_create_function(db, aFuncs[i].zName,
drh268380c2004-02-25 13:47:31 +0000672 aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
drhc9b84a12002-06-20 11:36:48 +0000673 if( aFuncs[i].xFunc ){
danielk197724b03fd2004-05-10 10:34:34 +0000674 sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
drhc9b84a12002-06-20 11:36:48 +0000675 }
drh0bce8352002-02-28 00:41:10 +0000676 }
677 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
drh268380c2004-02-25 13:47:31 +0000678 void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
danielk197724b03fd2004-05-10 10:34:34 +0000679 sqlite3_create_aggregate(db, aAggs[i].zName,
drh268380c2004-02-25 13:47:31 +0000680 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
danielk197724b03fd2004-05-10 10:34:34 +0000681 sqlite3_function_type(db, aAggs[i].zName, aAggs[i].dataType);
drh0bce8352002-02-28 00:41:10 +0000682 }
danielk197751ad0ec2004-05-24 12:39:02 +0000683
drh268380c2004-02-25 13:47:31 +0000684 for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
685 int n = strlen(azTypeFuncs[i]);
danielk19774adee202004-05-08 08:23:19 +0000686 FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n);
drh268380c2004-02-25 13:47:31 +0000687 while( p ){
688 p->includeTypes = 1;
689 p = p->pNext;
690 }
691 }
danielk19774adee202004-05-08 08:23:19 +0000692 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000693}
danielk19774adee202004-05-08 08:23:19 +0000694
695
696