blob: 31dd1f2fef3bc208941abbc8e380943cc38a0d09 [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**
danielk1977c572ef72004-05-27 09:28:41 +000019** $Id: func.c,v 1.60 2004/05/27 09:28:42 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*/
drhf9b596e2004-05-26 16:54:42 +000032static void minmaxFunc(
33 sqlite3_context *context,
34 int argc,
35 sqlite3_value **argv
36){
drh0bce8352002-02-28 00:41:10 +000037 int i;
drh268380c2004-02-25 13:47:31 +000038 int mask; /* 0 for min() or 0xffffffff for max() */
drhf9b596e2004-05-26 16:54:42 +000039 int iBest;
drh0bce8352002-02-28 00:41:10 +000040
drh89425d52002-02-28 03:04:48 +000041 if( argc==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +000042 mask = (int)sqlite3_user_data(context);
danielk1977c572ef72004-05-27 09:28:41 +000043 assert( mask==-1 || mask==0 );
drhf9b596e2004-05-26 16:54:42 +000044 iBest = 0;
45 for(i=1; i<argc; i++){
danielk1977c572ef72004-05-27 09:28:41 +000046 if( (sqlite3MemCompare(argv[iBest], argv[i], 0)^mask)>=0 ){
drhf9b596e2004-05-26 16:54:42 +000047 iBest = i;
drh0bce8352002-02-28 00:41:10 +000048 }
49 }
drhf4479502004-05-27 03:12:53 +000050 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:10 +000051}
drh0bce8352002-02-28 00:41:10 +000052
drh268380c2004-02-25 13:47:31 +000053/*
54** Return the type of the argument.
55*/
drhf9b596e2004-05-26 16:54:42 +000056static void typeofFunc(
57 sqlite3_context *context,
58 int argc,
59 sqlite3_value **argv
60){
danielk197735bb9d02004-05-24 12:55:54 +000061 const char *z = 0;
danielk197735bb9d02004-05-24 12:55:54 +000062 switch( sqlite3_value_type(argv[0]) ){
drhf9b596e2004-05-26 16:54:42 +000063 case SQLITE3_NULL: z = "null"; break;
64 case SQLITE3_INTEGER: z = "integer"; break;
65 case SQLITE3_TEXT: z = "text"; break;
66 case SQLITE3_FLOAT: z = "real"; break;
67 case SQLITE3_BLOB: z = "blob"; break;
danielk197735bb9d02004-05-24 12:55:54 +000068 }
danielk19777e18c252004-05-25 11:47:24 +000069 sqlite3_result_text(context, z, -1, 0);
drh0bce8352002-02-28 00:41:10 +000070}
71
72/*
73** Implementation of the length() function
74*/
drhf9b596e2004-05-26 16:54:42 +000075static void lengthFunc(
76 sqlite3_context *context,
77 int argc,
78 sqlite3_value **argv
79){
drh0bce8352002-02-28 00:41:10 +000080 int len;
81
82 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000083 switch( sqlite3_value_type(argv[0]) ){
84 case SQLITE3_BLOB:
85 case SQLITE3_INTEGER:
86 case SQLITE3_FLOAT: {
drhf4479502004-05-27 03:12:53 +000087 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +000088 break;
89 }
90 case SQLITE3_TEXT: {
drh4f26d6c2004-05-26 23:25:30 +000091 const char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +000092 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +000093 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +000094 break;
95 }
96 default: {
97 sqlite3_result_null(context);
98 break;
99 }
100 }
drh0bce8352002-02-28 00:41:10 +0000101}
102
103/*
104** Implementation of the abs() function
105*/
danielk19770ae8b832004-05-25 12:05:56 +0000106static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000107 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000108 switch( sqlite3_value_type(argv[0]) ){
109 case SQLITE3_INTEGER: {
drh4f26d6c2004-05-26 23:25:30 +0000110 sqlite3_result_int64(context, -sqlite3_value_int64(argv[0]));
drhf9b596e2004-05-26 16:54:42 +0000111 break;
112 }
113 case SQLITE3_NULL: {
114 sqlite3_result_null(context);
115 break;
116 }
117 default: {
drh4f26d6c2004-05-26 23:25:30 +0000118 sqlite3_result_double(context, -sqlite3_value_double(argv[0]));
drhf9b596e2004-05-26 16:54:42 +0000119 break;
120 }
121 }
drh0bce8352002-02-28 00:41:10 +0000122}
123
124/*
125** Implementation of the substr() function
126*/
drhf9b596e2004-05-26 16:54:42 +0000127static void substrFunc(
128 sqlite3_context *context,
129 int argc,
130 sqlite3_value **argv
131){
drh0bce8352002-02-28 00:41:10 +0000132 const char *z;
drh0bce8352002-02-28 00:41:10 +0000133 const char *z2;
134 int i;
drh0bce8352002-02-28 00:41:10 +0000135 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000136
drh0bce8352002-02-28 00:41:10 +0000137 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000138 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000139 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000140 p1 = sqlite3_value_int(argv[1]);
141 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000142 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000143 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000144 p1 += len;
drh653bc752002-02-28 03:31:10 +0000145 if( p1<0 ){
146 p2 += p1;
147 p1 = 0;
148 }
drh0bce8352002-02-28 00:41:10 +0000149 }else if( p1>0 ){
150 p1--;
151 }
152 if( p1+p2>len ){
153 p2 = len-p1;
154 }
drh77396302004-01-02 13:17:48 +0000155 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000156 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000157 }
drh47c8a672002-02-28 04:00:12 +0000158 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000159 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000160 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000161 }
drh47c8a672002-02-28 04:00:12 +0000162 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000163 if( p2<0 ) p2 = 0;
danielk19777e18c252004-05-25 11:47:24 +0000164 sqlite3_result_text(context, &z[p1], p2, 1);
drh0bce8352002-02-28 00:41:10 +0000165}
166
167/*
168** Implementation of the round() function
169*/
danielk19770ae8b832004-05-25 12:05:56 +0000170static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000171 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000172 double r;
173 char zBuf[100];
174 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000175 if( argc==2 ){
176 if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
177 n = sqlite3_value_int(argv[1]);
178 if( n>30 ) n = 30;
179 if( n<0 ) n = 0;
180 }
181 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000182 r = sqlite3_value_double(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000183 sprintf(zBuf,"%.*f",n,r);
danielk19777e18c252004-05-25 11:47:24 +0000184 sqlite3_result_text(context, zBuf, -1, 1);
drh0bce8352002-02-28 00:41:10 +0000185}
drhdc04c582002-02-24 01:55:15 +0000186
187/*
188** Implementation of the upper() and lower() SQL functions.
189*/
danielk19770ae8b832004-05-25 12:05:56 +0000190static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000191 char *z;
192 int i;
danielk19777e18c252004-05-25 11:47:24 +0000193 if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000194 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000195 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000196 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000197 for(i=0; z[i]; i++){
198 if( islower(z[i]) ) z[i] = toupper(z[i]);
199 }
danielk19777e18c252004-05-25 11:47:24 +0000200 sqlite3_result_text(context, z, -1, 1);
201 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000202}
danielk19770ae8b832004-05-25 12:05:56 +0000203static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000204 char *z;
205 int i;
danielk19777e18c252004-05-25 11:47:24 +0000206 if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000207 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000208 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000209 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000210 for(i=0; z[i]; i++){
211 if( isupper(z[i]) ) z[i] = tolower(z[i]);
212 }
danielk19777e18c252004-05-25 11:47:24 +0000213 sqlite3_result_text(context, z, -1, 1);
214 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000215}
216
217/*
drhfbc99082002-02-28 03:14:18 +0000218** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000219** All three do the same thing. They return the first non-NULL
220** argument.
drh3212e182002-02-28 00:46:26 +0000221*/
drhf9b596e2004-05-26 16:54:42 +0000222static void ifnullFunc(
223 sqlite3_context *context,
224 int argc,
225 sqlite3_value **argv
226){
drhfbc99082002-02-28 03:14:18 +0000227 int i;
228 for(i=0; i<argc; i++){
danielk197751ad0ec2004-05-24 12:39:02 +0000229 if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000230 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000231 break;
232 }
233 }
drh3212e182002-02-28 00:46:26 +0000234}
235
236/*
drhf9ffac92002-03-02 19:00:31 +0000237** Implementation of random(). Return a random integer.
238*/
drhf9b596e2004-05-26 16:54:42 +0000239static void randomFunc(
240 sqlite3_context *context,
241 int argc,
242 sqlite3_value **argv
243){
drhbbd82df2004-02-11 09:46:30 +0000244 int r;
danielk19774adee202004-05-08 08:23:19 +0000245 sqlite3Randomness(sizeof(r), &r);
drhf4479502004-05-27 03:12:53 +0000246 sqlite3_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000247}
248
249/*
drh6ed41ad2002-04-06 14:10:47 +0000250** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000251** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000252*/
danielk197751ad0ec2004-05-24 12:39:02 +0000253static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000254 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000255 int arg,
256 sqlite3_value **argv
257){
danielk197724b03fd2004-05-10 10:34:34 +0000258 sqlite *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000259 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000260}
261
rdcf146a772004-02-25 22:51:06 +0000262/*
263** Implementation of the change_count() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000264** value is the same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000265*/
drhf9b596e2004-05-26 16:54:42 +0000266static void change_count(
267 sqlite3_context *context,
268 int arg,
269 sqlite3_value **argv
270){
danielk197724b03fd2004-05-10 10:34:34 +0000271 sqlite *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000272 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000273}
rdcf146a772004-02-25 22:51:06 +0000274
275/*
276** Implementation of the last_statement_change_count() SQL function. The
danielk197751ad0ec2004-05-24 12:39:02 +0000277** return value is the same as the sqlite3_last_statement_changes() API
278** function.
rdcf146a772004-02-25 22:51:06 +0000279*/
danielk197751ad0ec2004-05-24 12:39:02 +0000280static void last_statement_change_count(
danielk19770ae8b832004-05-25 12:05:56 +0000281 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000282 int arg,
283 sqlite3_value **argv
284){
danielk197724b03fd2004-05-10 10:34:34 +0000285 sqlite *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000286 sqlite3_result_int(context, sqlite3_last_statement_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000287}
288
drh6ed41ad2002-04-06 14:10:47 +0000289/*
drh0ac65892002-04-20 14:24:41 +0000290** Implementation of the like() SQL function. This function implements
291** the build-in LIKE operator. The first argument to the function is the
292** string and the second argument is the pattern. So, the SQL statements:
293**
294** A LIKE B
295**
296** is implemented as like(A,B).
297*/
danielk197751ad0ec2004-05-24 12:39:02 +0000298static void likeFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000299 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000300 int argc,
301 sqlite3_value **argv
302){
drh4f26d6c2004-05-26 23:25:30 +0000303 const unsigned char *zA = sqlite3_value_text(argv[0]);
304 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000305 if( zA && zB ){
drhf4479502004-05-27 03:12:53 +0000306 sqlite3_result_int(context, sqlite3LikeCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000307 }
drh0ac65892002-04-20 14:24:41 +0000308}
309
310/*
311** Implementation of the glob() SQL function. This function implements
312** the build-in GLOB operator. The first argument to the function is the
313** string and the second argument is the pattern. So, the SQL statements:
314**
315** A GLOB B
316**
317** is implemented as glob(A,B).
318*/
danielk19770ae8b832004-05-25 12:05:56 +0000319static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
drh4f26d6c2004-05-26 23:25:30 +0000320 const unsigned char *zA = sqlite3_value_text(argv[0]);
321 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000322 if( zA && zB ){
drhf4479502004-05-27 03:12:53 +0000323 sqlite3_result_int(context, sqlite3GlobCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000324 }
drh8912d102002-05-26 21:34:58 +0000325}
326
327/*
328** Implementation of the NULLIF(x,y) function. The result is the first
329** argument if the arguments are different. The result is NULL if the
330** arguments are equal to each other.
331*/
drhf9b596e2004-05-26 16:54:42 +0000332static void nullifFunc(
333 sqlite3_context *context,
334 int argc,
335 sqlite3_value **argv
336){
337 if( sqlite3MemCompare(argv[0], argv[1], 0)!=0 ){
drhf4479502004-05-27 03:12:53 +0000338 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000339 }
drh0ac65892002-04-20 14:24:41 +0000340}
341
drh647cb0e2002-11-04 19:32:25 +0000342/*
343** Implementation of the VERSION(*) function. The result is the version
344** of the SQLite library that is running.
345*/
drhf9b596e2004-05-26 16:54:42 +0000346static void versionFunc(
347 sqlite3_context *context,
348 int argc,
349 sqlite3_value **argv
350){
danielk19777e18c252004-05-25 11:47:24 +0000351 sqlite3_result_text(context, sqlite3_version, -1, 0);
drh647cb0e2002-11-04 19:32:25 +0000352}
353
drh47394702003-08-20 01:03:33 +0000354/*
355** EXPERIMENTAL - This is not an official function. The interface may
356** change. This function may disappear. Do not write code that depends
357** on this function.
358**
359** Implementation of the QUOTE() function. This function takes a single
360** argument. If the argument is numeric, the return value is the same as
361** the argument. If the argument is NULL, the return value is the string
362** "NULL". Otherwise, the argument is enclosed in single quotes with
363** single-quote escapes.
364*/
danielk19770ae8b832004-05-25 12:05:56 +0000365static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000366 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000367 switch( sqlite3_value_type(argv[0]) ){
368 case SQLITE3_NULL: {
369 sqlite3_result_text(context, "NULL", 4, 0);
370 break;
drh47394702003-08-20 01:03:33 +0000371 }
drhf9b596e2004-05-26 16:54:42 +0000372 case SQLITE3_INTEGER:
373 case SQLITE3_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000374 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000375 break;
376 }
377 case SQLITE3_BLOB: /*** FIX ME. Use a BLOB encoding ***/
378 case SQLITE3_TEXT: {
379 int i,j,n;
drh4f26d6c2004-05-26 23:25:30 +0000380 const char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000381 char *z;
382
383 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
384 z = sqliteMalloc( i+n+3 );
385 if( z==0 ) return;
386 z[0] = '\'';
387 for(i=0, j=1; zArg[i]; i++){
388 z[j++] = zArg[i];
389 if( zArg[i]=='\'' ){
390 z[j++] = '\'';
391 }
392 }
393 z[j++] = '\'';
394 z[j] = 0;
395 sqlite3_result_text(context, z, j, 1);
396 sqliteFree(z);
397 }
drh47394702003-08-20 01:03:33 +0000398 }
399}
400
drhd24cc422003-03-27 12:51:24 +0000401#ifdef SQLITE_SOUNDEX
402/*
403** Compute the soundex encoding of a word.
404*/
danielk19770ae8b832004-05-25 12:05:56 +0000405static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000406 char zResult[8];
407 const char *zIn;
408 int i, j;
409 static const unsigned char iCode[] = {
410 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
411 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
412 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
413 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
414 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
415 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
416 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
417 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
418 };
419 assert( argc==1 );
drh4f26d6c2004-05-26 23:25:30 +0000420 zIn = sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000421 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
422 if( zIn[i] ){
423 zResult[0] = toupper(zIn[i]);
424 for(j=1; j<4 && zIn[i]; i++){
425 int code = iCode[zIn[i]&0x7f];
426 if( code>0 ){
427 zResult[j++] = code + '0';
428 }
429 }
430 while( j<4 ){
431 zResult[j++] = '0';
432 }
433 zResult[j] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000434 sqlite3_result_text(context, zResult, 4, 1);
drhd24cc422003-03-27 12:51:24 +0000435 }else{
danielk19777e18c252004-05-25 11:47:24 +0000436 sqlite3_result_text(context, "?000", 4, 0);
drhd24cc422003-03-27 12:51:24 +0000437 }
438}
439#endif
440
drh193a6b42002-07-07 16:52:46 +0000441#ifdef SQLITE_TEST
442/*
443** This function generates a string of random characters. Used for
444** generating test data.
445*/
danielk19770ae8b832004-05-25 12:05:56 +0000446static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000447 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000448 "abcdefghijklmnopqrstuvwxyz"
449 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
450 "0123456789"
451 ".-!,:*^+=_|?/<> ";
452 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000453 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000454 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000455 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000456 if( iMin<0 ) iMin = 0;
457 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
458 }else{
459 iMin = 1;
460 }
461 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000462 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000463 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000464 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000465 }else{
466 iMax = 50;
467 }
468 n = iMin;
469 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000470 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000471 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000472 n += r%(iMax + 1 - iMin);
473 }
drh1dba7272004-01-16 13:58:18 +0000474 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000475 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000476 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000477 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000478 }
479 zBuf[n] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000480 sqlite3_result_text(context, zBuf, n, 1);
drh193a6b42002-07-07 16:52:46 +0000481}
482#endif
483
drh0ac65892002-04-20 14:24:41 +0000484/*
drhd3a149e2002-02-24 17:12:53 +0000485** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000486** sum() or avg() aggregate computation.
487*/
488typedef struct SumCtx SumCtx;
489struct SumCtx {
490 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000491 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000492};
493
494/*
495** Routines used to compute the sum or average.
496*/
danielk19770ae8b832004-05-25 12:05:56 +0000497static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000498 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000499 if( argc<1 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000500 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000501 if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
drh4f26d6c2004-05-26 23:25:30 +0000502 p->sum += sqlite3_value_double(argv[0]);
drh739105c2002-05-29 23:22:23 +0000503 p->cnt++;
504 }
drhdd5baa92002-02-27 19:50:59 +0000505}
danielk19770ae8b832004-05-25 12:05:56 +0000506static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000507 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000508 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000509 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000510}
danielk19770ae8b832004-05-25 12:05:56 +0000511static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000512 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000513 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000514 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000515 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000516 }
517}
518
519/*
520** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000521** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000522*/
523typedef struct StdDevCtx StdDevCtx;
524struct StdDevCtx {
525 double sum; /* Sum of terms */
526 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000527 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000528};
529
drhef2daf52002-03-04 02:26:15 +0000530#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000531/*
532** Routines used to compute the standard deviation as an aggregate.
533*/
danielk19770ae8b832004-05-25 12:05:56 +0000534static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000535 StdDevCtx *p;
536 double x;
drh1350b032002-02-27 19:00:20 +0000537 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000538 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000539 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000540 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000541 p->sum += x;
542 p->sum2 += x*x;
543 p->cnt++;
544 }
drhd3a149e2002-02-24 17:12:53 +0000545}
danielk19770ae8b832004-05-25 12:05:56 +0000546static void stdDevFinalize(sqlite3_context *context){
danielk197724b03fd2004-05-10 10:34:34 +0000547 double rN = sqlite3_aggregate_count(context);
548 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000549 if( p && p->cnt>1 ){
550 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000551 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000552 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000553 }
drhd3a149e2002-02-24 17:12:53 +0000554}
drhef2daf52002-03-04 02:26:15 +0000555#endif
drhd3a149e2002-02-24 17:12:53 +0000556
drh0bce8352002-02-28 00:41:10 +0000557/*
558** The following structure keeps track of state information for the
559** count() aggregate function.
560*/
561typedef struct CountCtx CountCtx;
562struct CountCtx {
563 int n;
564};
drhdd5baa92002-02-27 19:50:59 +0000565
drh0bce8352002-02-28 00:41:10 +0000566/*
567** Routines to implement the count() aggregate function.
568*/
danielk19770ae8b832004-05-25 12:05:56 +0000569static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000570 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000571 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000572 if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000573 p->n++;
574 }
575}
danielk19770ae8b832004-05-25 12:05:56 +0000576static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000577 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000578 p = sqlite3_aggregate_context(context, sizeof(*p));
drhf4479502004-05-27 03:12:53 +0000579 sqlite3_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000580}
581
582/*
583** This function tracks state information for the min() and max()
584** aggregate functions.
585*/
586typedef struct MinMaxCtx MinMaxCtx;
587struct MinMaxCtx {
588 char *z; /* The best so far */
589 char zBuf[28]; /* Space that can be used for storage */
590};
591
592/*
593** Routines to implement min() and max() aggregate functions.
594*/
danielk19770ae8b832004-05-25 12:05:56 +0000595static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000596 int max = 0;
597 int cmp = 0;
598 Mem *pArg = (Mem *)argv[0];
drh4f26d6c2004-05-26 23:25:30 +0000599 Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
drh268380c2004-02-25 13:47:31 +0000600
danielk197788208052004-05-25 01:13:20 +0000601 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
602
603 if( pBest->flags ){
danielk19777e18c252004-05-25 11:47:24 +0000604 /* This step function is used for both the min() and max() aggregates,
605 ** the only difference between the two being that the sense of the
606 ** comparison is inverted. For the max() aggregate, the
607 ** sqlite3_user_data() function returns (void *)-1. For min() it
608 ** returns (void *)db, where db is the sqlite3* database pointer.
609 ** Therefore the next statement sets variable 'max' to 1 for the max()
610 ** aggregate, or 0 for min().
611 */
danielk197788208052004-05-25 01:13:20 +0000612 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
613 cmp = sqlite3MemCompare(pBest, pArg, 0);
614 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000615 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000616 }
drh268380c2004-02-25 13:47:31 +0000617 }else{
danielk19777e18c252004-05-25 11:47:24 +0000618 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000619 }
620}
danielk19770ae8b832004-05-25 12:05:56 +0000621static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000622 sqlite3_value *pRes;
drh4f26d6c2004-05-26 23:25:30 +0000623 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +0000624 if( pRes->flags ){
drhf4479502004-05-27 03:12:53 +0000625 sqlite3_result_value(context, pRes);
drh0bce8352002-02-28 00:41:10 +0000626 }
627}
drhdd5baa92002-02-27 19:50:59 +0000628
drhd3a149e2002-02-24 17:12:53 +0000629/*
drha2ed5602002-02-26 23:55:31 +0000630** This function registered all of the above C functions as SQL
631** functions. This should be the only routine in this file with
632** external linkage.
drhdc04c582002-02-24 01:55:15 +0000633*/
danielk19774adee202004-05-08 08:23:19 +0000634void sqlite3RegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000635 static struct {
636 char *zName;
drh268380c2004-02-25 13:47:31 +0000637 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000638 u8 argType; /* 0: none. 1: db 2: (-1) */
danielk19770ae8b832004-05-25 12:05:56 +0000639 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000640 } aFuncs[] = {
drhf9b596e2004-05-26 16:54:42 +0000641 { "min", -1, 0, minmaxFunc },
642 { "min", 0, 0, 0 },
643 { "max", -1, 2, minmaxFunc },
644 { "max", 0, 2, 0 },
645 { "typeof", 1, 0, typeofFunc },
646 { "classof", 1, 0, typeofFunc }, /* FIX ME: hack */
647 { "length", 1, 0, lengthFunc },
648 { "substr", 3, 0, substrFunc },
649 { "abs", 1, 0, absFunc },
650 { "round", 1, 0, roundFunc },
651 { "round", 2, 0, roundFunc },
652 { "upper", 1, 0, upperFunc },
653 { "lower", 1, 0, lowerFunc },
654 { "coalesce", -1, 0, ifnullFunc },
655 { "coalesce", 0, 0, 0 },
656 { "coalesce", 1, 0, 0 },
657 { "ifnull", 2, 0, ifnullFunc },
658 { "random", -1, 0, randomFunc },
659 { "like", 2, 0, likeFunc },
660 { "glob", 2, 0, globFunc },
661 { "nullif", 2, 0, nullifFunc },
662 { "sqlite_version", 0, 0, versionFunc},
663 { "quote", 1, 0, quoteFunc },
664 { "last_insert_rowid", 0, 1, last_insert_rowid },
665 { "change_count", 0, 1, change_count },
666 { "last_statement_change_count", 0, 1, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000667#ifdef SQLITE_SOUNDEX
drhf9b596e2004-05-26 16:54:42 +0000668 { "soundex", 1, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000669#endif
drh193a6b42002-07-07 16:52:46 +0000670#ifdef SQLITE_TEST
drhf9b596e2004-05-26 16:54:42 +0000671 { "randstr", 2, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000672#endif
drh0bce8352002-02-28 00:41:10 +0000673 };
674 static struct {
675 char *zName;
drh268380c2004-02-25 13:47:31 +0000676 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000677 u8 argType;
danielk19770ae8b832004-05-25 12:05:56 +0000678 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
679 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +0000680 } aAggs[] = {
drhf9b596e2004-05-26 16:54:42 +0000681 { "min", 1, 0, minmaxStep, minMaxFinalize },
682 { "max", 1, 2, minmaxStep, minMaxFinalize },
683 { "sum", 1, 0, sumStep, sumFinalize },
684 { "avg", 1, 0, sumStep, avgFinalize },
685 { "count", 0, 0, countStep, countFinalize },
686 { "count", 1, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000687#if 0
drhf9b596e2004-05-26 16:54:42 +0000688 { "stddev", 1, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000689#endif
drh0bce8352002-02-28 00:41:10 +0000690 };
691 int i;
692
693 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000694 void *pArg = 0;
695 switch( aFuncs[i].argType ){
696 case 1: pArg = db; break;
697 case 2: pArg = (void *)(-1); break;
698 }
danielk197765904932004-05-26 06:18:37 +0000699 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0,
700 pArg, aFuncs[i].xFunc, 0, 0);
drh0bce8352002-02-28 00:41:10 +0000701 }
702 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000703 void *pArg = 0;
704 switch( aAggs[i].argType ){
705 case 1: pArg = db; break;
706 case 2: pArg = (void *)(-1); break;
707 }
danielk197765904932004-05-26 06:18:37 +0000708 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg,
709 0, aAggs[i].xStep, aAggs[i].xFinalize);
drh268380c2004-02-25 13:47:31 +0000710 }
danielk19774adee202004-05-08 08:23:19 +0000711 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000712}