blob: c1b9863823eb47a16ca49a65119e338fde612138 [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**
danielk1977f93bbbe2004-05-27 10:30:52 +000019** $Id: func.c,v 1.61 2004/05/27 10:30:53 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;
danielk1977f93bbbe2004-05-27 10:30:52 +000045 if( sqlite3_value_type(argv[0])==SQLITE3_NULL ) return;
drhf9b596e2004-05-26 16:54:42 +000046 for(i=1; i<argc; i++){
danielk1977f93bbbe2004-05-27 10:30:52 +000047 if( sqlite3_value_type(argv[i])==SQLITE3_NULL ) return;
danielk1977c572ef72004-05-27 09:28:41 +000048 if( (sqlite3MemCompare(argv[iBest], argv[i], 0)^mask)>=0 ){
drhf9b596e2004-05-26 16:54:42 +000049 iBest = i;
drh0bce8352002-02-28 00:41:10 +000050 }
51 }
drhf4479502004-05-27 03:12:53 +000052 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:10 +000053}
drh0bce8352002-02-28 00:41:10 +000054
drh268380c2004-02-25 13:47:31 +000055/*
56** Return the type of the argument.
57*/
drhf9b596e2004-05-26 16:54:42 +000058static void typeofFunc(
59 sqlite3_context *context,
60 int argc,
61 sqlite3_value **argv
62){
danielk197735bb9d02004-05-24 12:55:54 +000063 const char *z = 0;
danielk197735bb9d02004-05-24 12:55:54 +000064 switch( sqlite3_value_type(argv[0]) ){
drhf9b596e2004-05-26 16:54:42 +000065 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;
danielk197735bb9d02004-05-24 12:55:54 +000070 }
danielk19777e18c252004-05-25 11:47:24 +000071 sqlite3_result_text(context, z, -1, 0);
drh0bce8352002-02-28 00:41:10 +000072}
73
74/*
75** Implementation of the length() function
76*/
drhf9b596e2004-05-26 16:54:42 +000077static void lengthFunc(
78 sqlite3_context *context,
79 int argc,
80 sqlite3_value **argv
81){
drh0bce8352002-02-28 00:41:10 +000082 int len;
83
84 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000085 switch( sqlite3_value_type(argv[0]) ){
86 case SQLITE3_BLOB:
87 case SQLITE3_INTEGER:
88 case SQLITE3_FLOAT: {
drhf4479502004-05-27 03:12:53 +000089 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +000090 break;
91 }
92 case SQLITE3_TEXT: {
drh4f26d6c2004-05-26 23:25:30 +000093 const char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +000094 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +000095 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +000096 break;
97 }
98 default: {
99 sqlite3_result_null(context);
100 break;
101 }
102 }
drh0bce8352002-02-28 00:41:10 +0000103}
104
105/*
106** Implementation of the abs() function
107*/
danielk19770ae8b832004-05-25 12:05:56 +0000108static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000109 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000110 switch( sqlite3_value_type(argv[0]) ){
111 case SQLITE3_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000112 i64 iVal = sqlite3_value_int64(argv[0]);
113 if( iVal<0 ) iVal = iVal * -1;
114 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000115 break;
116 }
117 case SQLITE3_NULL: {
118 sqlite3_result_null(context);
119 break;
120 }
121 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000122 double rVal = sqlite3_value_double(argv[0]);
123 if( rVal<0 ) rVal = rVal * -1.0;
124 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000125 break;
126 }
127 }
drh0bce8352002-02-28 00:41:10 +0000128}
129
130/*
131** Implementation of the substr() function
132*/
drhf9b596e2004-05-26 16:54:42 +0000133static void substrFunc(
134 sqlite3_context *context,
135 int argc,
136 sqlite3_value **argv
137){
drh0bce8352002-02-28 00:41:10 +0000138 const char *z;
drh0bce8352002-02-28 00:41:10 +0000139 const char *z2;
140 int i;
drh0bce8352002-02-28 00:41:10 +0000141 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000142
drh0bce8352002-02-28 00:41:10 +0000143 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000144 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000145 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000146 p1 = sqlite3_value_int(argv[1]);
147 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000148 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000149 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000150 p1 += len;
drh653bc752002-02-28 03:31:10 +0000151 if( p1<0 ){
152 p2 += p1;
153 p1 = 0;
154 }
drh0bce8352002-02-28 00:41:10 +0000155 }else if( p1>0 ){
156 p1--;
157 }
158 if( p1+p2>len ){
159 p2 = len-p1;
160 }
drh77396302004-01-02 13:17:48 +0000161 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000162 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000163 }
drh47c8a672002-02-28 04:00:12 +0000164 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000165 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000166 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000167 }
drh47c8a672002-02-28 04:00:12 +0000168 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000169 if( p2<0 ) p2 = 0;
danielk19777e18c252004-05-25 11:47:24 +0000170 sqlite3_result_text(context, &z[p1], p2, 1);
drh0bce8352002-02-28 00:41:10 +0000171}
172
173/*
174** Implementation of the round() function
175*/
danielk19770ae8b832004-05-25 12:05:56 +0000176static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000177 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000178 double r;
179 char zBuf[100];
180 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000181 if( argc==2 ){
182 if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return;
183 n = sqlite3_value_int(argv[1]);
184 if( n>30 ) n = 30;
185 if( n<0 ) n = 0;
186 }
187 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000188 r = sqlite3_value_double(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000189 sprintf(zBuf,"%.*f",n,r);
danielk19777e18c252004-05-25 11:47:24 +0000190 sqlite3_result_text(context, zBuf, -1, 1);
drh0bce8352002-02-28 00:41:10 +0000191}
drhdc04c582002-02-24 01:55:15 +0000192
193/*
194** Implementation of the upper() and lower() SQL functions.
195*/
danielk19770ae8b832004-05-25 12:05:56 +0000196static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000197 char *z;
198 int i;
danielk19777e18c252004-05-25 11:47:24 +0000199 if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000200 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000201 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000202 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000203 for(i=0; z[i]; i++){
204 if( islower(z[i]) ) z[i] = toupper(z[i]);
205 }
danielk19777e18c252004-05-25 11:47:24 +0000206 sqlite3_result_text(context, z, -1, 1);
207 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000208}
danielk19770ae8b832004-05-25 12:05:56 +0000209static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000210 char *z;
211 int i;
danielk19777e18c252004-05-25 11:47:24 +0000212 if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000213 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000214 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000215 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000216 for(i=0; z[i]; i++){
217 if( isupper(z[i]) ) z[i] = tolower(z[i]);
218 }
danielk19777e18c252004-05-25 11:47:24 +0000219 sqlite3_result_text(context, z, -1, 1);
220 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000221}
222
223/*
drhfbc99082002-02-28 03:14:18 +0000224** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000225** All three do the same thing. They return the first non-NULL
226** argument.
drh3212e182002-02-28 00:46:26 +0000227*/
drhf9b596e2004-05-26 16:54:42 +0000228static void ifnullFunc(
229 sqlite3_context *context,
230 int argc,
231 sqlite3_value **argv
232){
drhfbc99082002-02-28 03:14:18 +0000233 int i;
234 for(i=0; i<argc; i++){
danielk197751ad0ec2004-05-24 12:39:02 +0000235 if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000236 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000237 break;
238 }
239 }
drh3212e182002-02-28 00:46:26 +0000240}
241
242/*
drhf9ffac92002-03-02 19:00:31 +0000243** Implementation of random(). Return a random integer.
244*/
drhf9b596e2004-05-26 16:54:42 +0000245static void randomFunc(
246 sqlite3_context *context,
247 int argc,
248 sqlite3_value **argv
249){
drhbbd82df2004-02-11 09:46:30 +0000250 int r;
danielk19774adee202004-05-08 08:23:19 +0000251 sqlite3Randomness(sizeof(r), &r);
drhf4479502004-05-27 03:12:53 +0000252 sqlite3_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000253}
254
255/*
drh6ed41ad2002-04-06 14:10:47 +0000256** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000257** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000258*/
danielk197751ad0ec2004-05-24 12:39:02 +0000259static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000260 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000261 int arg,
262 sqlite3_value **argv
263){
danielk197724b03fd2004-05-10 10:34:34 +0000264 sqlite *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000265 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000266}
267
rdcf146a772004-02-25 22:51:06 +0000268/*
269** Implementation of the change_count() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000270** value is the same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000271*/
drhf9b596e2004-05-26 16:54:42 +0000272static void change_count(
273 sqlite3_context *context,
274 int arg,
275 sqlite3_value **argv
276){
danielk197724b03fd2004-05-10 10:34:34 +0000277 sqlite *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000278 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000279}
rdcf146a772004-02-25 22:51:06 +0000280
281/*
282** Implementation of the last_statement_change_count() SQL function. The
danielk197751ad0ec2004-05-24 12:39:02 +0000283** return value is the same as the sqlite3_last_statement_changes() API
284** function.
rdcf146a772004-02-25 22:51:06 +0000285*/
danielk197751ad0ec2004-05-24 12:39:02 +0000286static void last_statement_change_count(
danielk19770ae8b832004-05-25 12:05:56 +0000287 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000288 int arg,
289 sqlite3_value **argv
290){
danielk197724b03fd2004-05-10 10:34:34 +0000291 sqlite *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000292 sqlite3_result_int(context, sqlite3_last_statement_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000293}
294
drh6ed41ad2002-04-06 14:10:47 +0000295/*
drh0ac65892002-04-20 14:24:41 +0000296** Implementation of the like() SQL function. This function implements
297** the build-in LIKE operator. The first argument to the function is the
298** string and the second argument is the pattern. So, the SQL statements:
299**
300** A LIKE B
301**
302** is implemented as like(A,B).
303*/
danielk197751ad0ec2004-05-24 12:39:02 +0000304static void likeFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000305 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000306 int argc,
307 sqlite3_value **argv
308){
drh4f26d6c2004-05-26 23:25:30 +0000309 const unsigned char *zA = sqlite3_value_text(argv[0]);
310 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000311 if( zA && zB ){
drhf4479502004-05-27 03:12:53 +0000312 sqlite3_result_int(context, sqlite3LikeCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000313 }
drh0ac65892002-04-20 14:24:41 +0000314}
315
316/*
317** Implementation of the glob() SQL function. This function implements
318** the build-in GLOB operator. The first argument to the function is the
319** string and the second argument is the pattern. So, the SQL statements:
320**
321** A GLOB B
322**
323** is implemented as glob(A,B).
324*/
danielk19770ae8b832004-05-25 12:05:56 +0000325static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
drh4f26d6c2004-05-26 23:25:30 +0000326 const unsigned char *zA = sqlite3_value_text(argv[0]);
327 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000328 if( zA && zB ){
drhf4479502004-05-27 03:12:53 +0000329 sqlite3_result_int(context, sqlite3GlobCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000330 }
drh8912d102002-05-26 21:34:58 +0000331}
332
333/*
334** Implementation of the NULLIF(x,y) function. The result is the first
335** argument if the arguments are different. The result is NULL if the
336** arguments are equal to each other.
337*/
drhf9b596e2004-05-26 16:54:42 +0000338static void nullifFunc(
339 sqlite3_context *context,
340 int argc,
341 sqlite3_value **argv
342){
343 if( sqlite3MemCompare(argv[0], argv[1], 0)!=0 ){
drhf4479502004-05-27 03:12:53 +0000344 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000345 }
drh0ac65892002-04-20 14:24:41 +0000346}
347
drh647cb0e2002-11-04 19:32:25 +0000348/*
349** Implementation of the VERSION(*) function. The result is the version
350** of the SQLite library that is running.
351*/
drhf9b596e2004-05-26 16:54:42 +0000352static void versionFunc(
353 sqlite3_context *context,
354 int argc,
355 sqlite3_value **argv
356){
danielk19777e18c252004-05-25 11:47:24 +0000357 sqlite3_result_text(context, sqlite3_version, -1, 0);
drh647cb0e2002-11-04 19:32:25 +0000358}
359
drh47394702003-08-20 01:03:33 +0000360/*
361** EXPERIMENTAL - This is not an official function. The interface may
362** change. This function may disappear. Do not write code that depends
363** on this function.
364**
365** Implementation of the QUOTE() function. This function takes a single
366** argument. If the argument is numeric, the return value is the same as
367** the argument. If the argument is NULL, the return value is the string
368** "NULL". Otherwise, the argument is enclosed in single quotes with
369** single-quote escapes.
370*/
danielk19770ae8b832004-05-25 12:05:56 +0000371static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000372 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000373 switch( sqlite3_value_type(argv[0]) ){
374 case SQLITE3_NULL: {
375 sqlite3_result_text(context, "NULL", 4, 0);
376 break;
drh47394702003-08-20 01:03:33 +0000377 }
drhf9b596e2004-05-26 16:54:42 +0000378 case SQLITE3_INTEGER:
379 case SQLITE3_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000380 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000381 break;
382 }
383 case SQLITE3_BLOB: /*** FIX ME. Use a BLOB encoding ***/
384 case SQLITE3_TEXT: {
385 int i,j,n;
drh4f26d6c2004-05-26 23:25:30 +0000386 const char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000387 char *z;
388
389 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
390 z = sqliteMalloc( i+n+3 );
391 if( z==0 ) return;
392 z[0] = '\'';
393 for(i=0, j=1; zArg[i]; i++){
394 z[j++] = zArg[i];
395 if( zArg[i]=='\'' ){
396 z[j++] = '\'';
397 }
398 }
399 z[j++] = '\'';
400 z[j] = 0;
401 sqlite3_result_text(context, z, j, 1);
402 sqliteFree(z);
403 }
drh47394702003-08-20 01:03:33 +0000404 }
405}
406
drhd24cc422003-03-27 12:51:24 +0000407#ifdef SQLITE_SOUNDEX
408/*
409** Compute the soundex encoding of a word.
410*/
danielk19770ae8b832004-05-25 12:05:56 +0000411static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000412 char zResult[8];
413 const char *zIn;
414 int i, j;
415 static const unsigned char iCode[] = {
416 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
417 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
418 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
419 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
420 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
421 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
422 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
423 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
424 };
425 assert( argc==1 );
drh4f26d6c2004-05-26 23:25:30 +0000426 zIn = sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000427 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
428 if( zIn[i] ){
429 zResult[0] = toupper(zIn[i]);
430 for(j=1; j<4 && zIn[i]; i++){
431 int code = iCode[zIn[i]&0x7f];
432 if( code>0 ){
433 zResult[j++] = code + '0';
434 }
435 }
436 while( j<4 ){
437 zResult[j++] = '0';
438 }
439 zResult[j] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000440 sqlite3_result_text(context, zResult, 4, 1);
drhd24cc422003-03-27 12:51:24 +0000441 }else{
danielk19777e18c252004-05-25 11:47:24 +0000442 sqlite3_result_text(context, "?000", 4, 0);
drhd24cc422003-03-27 12:51:24 +0000443 }
444}
445#endif
446
drh193a6b42002-07-07 16:52:46 +0000447#ifdef SQLITE_TEST
448/*
449** This function generates a string of random characters. Used for
450** generating test data.
451*/
danielk19770ae8b832004-05-25 12:05:56 +0000452static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000453 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000454 "abcdefghijklmnopqrstuvwxyz"
455 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
456 "0123456789"
457 ".-!,:*^+=_|?/<> ";
458 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000459 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000460 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000461 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000462 if( iMin<0 ) iMin = 0;
463 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
464 }else{
465 iMin = 1;
466 }
467 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000468 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000469 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000470 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000471 }else{
472 iMax = 50;
473 }
474 n = iMin;
475 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000476 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000477 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000478 n += r%(iMax + 1 - iMin);
479 }
drh1dba7272004-01-16 13:58:18 +0000480 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000481 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000482 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000483 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000484 }
485 zBuf[n] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000486 sqlite3_result_text(context, zBuf, n, 1);
drh193a6b42002-07-07 16:52:46 +0000487}
488#endif
489
drh0ac65892002-04-20 14:24:41 +0000490/*
drhd3a149e2002-02-24 17:12:53 +0000491** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000492** sum() or avg() aggregate computation.
493*/
494typedef struct SumCtx SumCtx;
495struct SumCtx {
496 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000497 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000498};
499
500/*
501** Routines used to compute the sum or average.
502*/
danielk19770ae8b832004-05-25 12:05:56 +0000503static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000504 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000505 if( argc<1 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000506 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000507 if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){
drh4f26d6c2004-05-26 23:25:30 +0000508 p->sum += sqlite3_value_double(argv[0]);
drh739105c2002-05-29 23:22:23 +0000509 p->cnt++;
510 }
drhdd5baa92002-02-27 19:50:59 +0000511}
danielk19770ae8b832004-05-25 12:05:56 +0000512static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000513 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000514 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000515 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000516}
danielk19770ae8b832004-05-25 12:05:56 +0000517static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000518 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000519 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000520 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000521 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000522 }
523}
524
525/*
526** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000527** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000528*/
529typedef struct StdDevCtx StdDevCtx;
530struct StdDevCtx {
531 double sum; /* Sum of terms */
532 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000533 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000534};
535
drhef2daf52002-03-04 02:26:15 +0000536#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000537/*
538** Routines used to compute the standard deviation as an aggregate.
539*/
danielk19770ae8b832004-05-25 12:05:56 +0000540static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000541 StdDevCtx *p;
542 double x;
drh1350b032002-02-27 19:00:20 +0000543 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000544 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000545 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000546 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000547 p->sum += x;
548 p->sum2 += x*x;
549 p->cnt++;
550 }
drhd3a149e2002-02-24 17:12:53 +0000551}
danielk19770ae8b832004-05-25 12:05:56 +0000552static void stdDevFinalize(sqlite3_context *context){
danielk197724b03fd2004-05-10 10:34:34 +0000553 double rN = sqlite3_aggregate_count(context);
554 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000555 if( p && p->cnt>1 ){
556 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000557 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000558 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000559 }
drhd3a149e2002-02-24 17:12:53 +0000560}
drhef2daf52002-03-04 02:26:15 +0000561#endif
drhd3a149e2002-02-24 17:12:53 +0000562
drh0bce8352002-02-28 00:41:10 +0000563/*
564** The following structure keeps track of state information for the
565** count() aggregate function.
566*/
567typedef struct CountCtx CountCtx;
568struct CountCtx {
569 int n;
570};
drhdd5baa92002-02-27 19:50:59 +0000571
drh0bce8352002-02-28 00:41:10 +0000572/*
573** Routines to implement the count() aggregate function.
574*/
danielk19770ae8b832004-05-25 12:05:56 +0000575static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000576 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000577 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19776ddcca52004-05-24 23:48:25 +0000578 if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000579 p->n++;
580 }
581}
danielk19770ae8b832004-05-25 12:05:56 +0000582static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000583 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000584 p = sqlite3_aggregate_context(context, sizeof(*p));
drhf4479502004-05-27 03:12:53 +0000585 sqlite3_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000586}
587
588/*
589** This function tracks state information for the min() and max()
590** aggregate functions.
591*/
592typedef struct MinMaxCtx MinMaxCtx;
593struct MinMaxCtx {
594 char *z; /* The best so far */
595 char zBuf[28]; /* Space that can be used for storage */
596};
597
598/*
599** Routines to implement min() and max() aggregate functions.
600*/
danielk19770ae8b832004-05-25 12:05:56 +0000601static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000602 int max = 0;
603 int cmp = 0;
604 Mem *pArg = (Mem *)argv[0];
drh4f26d6c2004-05-26 23:25:30 +0000605 Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
drh268380c2004-02-25 13:47:31 +0000606
danielk197788208052004-05-25 01:13:20 +0000607 if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return;
608
609 if( pBest->flags ){
danielk19777e18c252004-05-25 11:47:24 +0000610 /* This step function is used for both the min() and max() aggregates,
611 ** the only difference between the two being that the sense of the
612 ** comparison is inverted. For the max() aggregate, the
613 ** sqlite3_user_data() function returns (void *)-1. For min() it
614 ** returns (void *)db, where db is the sqlite3* database pointer.
615 ** Therefore the next statement sets variable 'max' to 1 for the max()
616 ** aggregate, or 0 for min().
617 */
danielk197788208052004-05-25 01:13:20 +0000618 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
619 cmp = sqlite3MemCompare(pBest, pArg, 0);
620 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000621 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000622 }
drh268380c2004-02-25 13:47:31 +0000623 }else{
danielk19777e18c252004-05-25 11:47:24 +0000624 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000625 }
626}
danielk19770ae8b832004-05-25 12:05:56 +0000627static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000628 sqlite3_value *pRes;
drh4f26d6c2004-05-26 23:25:30 +0000629 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +0000630 if( pRes->flags ){
drhf4479502004-05-27 03:12:53 +0000631 sqlite3_result_value(context, pRes);
drh0bce8352002-02-28 00:41:10 +0000632 }
633}
drhdd5baa92002-02-27 19:50:59 +0000634
drhd3a149e2002-02-24 17:12:53 +0000635/*
drha2ed5602002-02-26 23:55:31 +0000636** This function registered all of the above C functions as SQL
637** functions. This should be the only routine in this file with
638** external linkage.
drhdc04c582002-02-24 01:55:15 +0000639*/
danielk19774adee202004-05-08 08:23:19 +0000640void sqlite3RegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000641 static struct {
642 char *zName;
drh268380c2004-02-25 13:47:31 +0000643 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000644 u8 argType; /* 0: none. 1: db 2: (-1) */
danielk19770ae8b832004-05-25 12:05:56 +0000645 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000646 } aFuncs[] = {
drhf9b596e2004-05-26 16:54:42 +0000647 { "min", -1, 0, minmaxFunc },
648 { "min", 0, 0, 0 },
649 { "max", -1, 2, minmaxFunc },
650 { "max", 0, 2, 0 },
651 { "typeof", 1, 0, typeofFunc },
652 { "classof", 1, 0, typeofFunc }, /* FIX ME: hack */
653 { "length", 1, 0, lengthFunc },
654 { "substr", 3, 0, substrFunc },
655 { "abs", 1, 0, absFunc },
656 { "round", 1, 0, roundFunc },
657 { "round", 2, 0, roundFunc },
658 { "upper", 1, 0, upperFunc },
659 { "lower", 1, 0, lowerFunc },
660 { "coalesce", -1, 0, ifnullFunc },
661 { "coalesce", 0, 0, 0 },
662 { "coalesce", 1, 0, 0 },
663 { "ifnull", 2, 0, ifnullFunc },
664 { "random", -1, 0, randomFunc },
665 { "like", 2, 0, likeFunc },
666 { "glob", 2, 0, globFunc },
667 { "nullif", 2, 0, nullifFunc },
668 { "sqlite_version", 0, 0, versionFunc},
669 { "quote", 1, 0, quoteFunc },
670 { "last_insert_rowid", 0, 1, last_insert_rowid },
671 { "change_count", 0, 1, change_count },
672 { "last_statement_change_count", 0, 1, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000673#ifdef SQLITE_SOUNDEX
drhf9b596e2004-05-26 16:54:42 +0000674 { "soundex", 1, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000675#endif
drh193a6b42002-07-07 16:52:46 +0000676#ifdef SQLITE_TEST
drhf9b596e2004-05-26 16:54:42 +0000677 { "randstr", 2, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000678#endif
drh0bce8352002-02-28 00:41:10 +0000679 };
680 static struct {
681 char *zName;
drh268380c2004-02-25 13:47:31 +0000682 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000683 u8 argType;
danielk19770ae8b832004-05-25 12:05:56 +0000684 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
685 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +0000686 } aAggs[] = {
drhf9b596e2004-05-26 16:54:42 +0000687 { "min", 1, 0, minmaxStep, minMaxFinalize },
688 { "max", 1, 2, minmaxStep, minMaxFinalize },
689 { "sum", 1, 0, sumStep, sumFinalize },
690 { "avg", 1, 0, sumStep, avgFinalize },
691 { "count", 0, 0, countStep, countFinalize },
692 { "count", 1, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000693#if 0
drhf9b596e2004-05-26 16:54:42 +0000694 { "stddev", 1, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000695#endif
drh0bce8352002-02-28 00:41:10 +0000696 };
697 int i;
698
699 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000700 void *pArg = 0;
701 switch( aFuncs[i].argType ){
702 case 1: pArg = db; break;
703 case 2: pArg = (void *)(-1); break;
704 }
danielk197765904932004-05-26 06:18:37 +0000705 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0,
706 pArg, aFuncs[i].xFunc, 0, 0);
drh0bce8352002-02-28 00:41:10 +0000707 }
708 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000709 void *pArg = 0;
710 switch( aAggs[i].argType ){
711 case 1: pArg = db; break;
712 case 2: pArg = (void *)(-1); break;
713 }
danielk197765904932004-05-26 06:18:37 +0000714 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg,
715 0, aAggs[i].xStep, aAggs[i].xFinalize);
drh268380c2004-02-25 13:47:31 +0000716 }
danielk19774adee202004-05-08 08:23:19 +0000717 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000718}