blob: c6ec755ba3305bdf21bb579d42872c6e0de425b0 [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**
danielk1977d641d642004-11-18 15:44:29 +000019** $Id: func.c,v 1.90 2004/11/18 15:44:29 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
danielk1977dc1bdc42004-06-11 10:51:27 +000029static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
30 return context->pColl;
31}
32
drh0bce8352002-02-28 00:41:10 +000033/*
34** Implementation of the non-aggregate min() and max() functions
35*/
drhf9b596e2004-05-26 16:54:42 +000036static void minmaxFunc(
37 sqlite3_context *context,
38 int argc,
39 sqlite3_value **argv
40){
drh0bce8352002-02-28 00:41:10 +000041 int i;
drh268380c2004-02-25 13:47:31 +000042 int mask; /* 0 for min() or 0xffffffff for max() */
drhf9b596e2004-05-26 16:54:42 +000043 int iBest;
danielk1977dc1bdc42004-06-11 10:51:27 +000044 CollSeq *pColl;
drh0bce8352002-02-28 00:41:10 +000045
drh89425d52002-02-28 03:04:48 +000046 if( argc==0 ) return;
drhc44af712004-09-02 15:53:56 +000047 mask = sqlite3_user_data(context)==0 ? 0 : -1;
danielk1977dc1bdc42004-06-11 10:51:27 +000048 pColl = sqlite3GetFuncCollSeq(context);
49 assert( pColl );
danielk1977c572ef72004-05-27 09:28:41 +000050 assert( mask==-1 || mask==0 );
drhf9b596e2004-05-26 16:54:42 +000051 iBest = 0;
drh9c054832004-05-31 18:51:57 +000052 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
drhf9b596e2004-05-26 16:54:42 +000053 for(i=1; i<argc; i++){
drh9c054832004-05-31 18:51:57 +000054 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
danielk1977dc1bdc42004-06-11 10:51:27 +000055 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
drhf9b596e2004-05-26 16:54:42 +000056 iBest = i;
drh0bce8352002-02-28 00:41:10 +000057 }
58 }
drhf4479502004-05-27 03:12:53 +000059 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:10 +000060}
drh0bce8352002-02-28 00:41:10 +000061
drh268380c2004-02-25 13:47:31 +000062/*
63** Return the type of the argument.
64*/
drhf9b596e2004-05-26 16:54:42 +000065static void typeofFunc(
66 sqlite3_context *context,
67 int argc,
68 sqlite3_value **argv
69){
danielk197735bb9d02004-05-24 12:55:54 +000070 const char *z = 0;
danielk197735bb9d02004-05-24 12:55:54 +000071 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000072 case SQLITE_NULL: z = "null"; break;
73 case SQLITE_INTEGER: z = "integer"; break;
74 case SQLITE_TEXT: z = "text"; break;
75 case SQLITE_FLOAT: z = "real"; break;
76 case SQLITE_BLOB: z = "blob"; break;
danielk197735bb9d02004-05-24 12:55:54 +000077 }
danielk1977d8123362004-06-12 09:25:12 +000078 sqlite3_result_text(context, z, -1, SQLITE_STATIC);
drh0bce8352002-02-28 00:41:10 +000079}
80
81/*
82** Implementation of the length() function
83*/
drhf9b596e2004-05-26 16:54:42 +000084static void lengthFunc(
85 sqlite3_context *context,
86 int argc,
87 sqlite3_value **argv
88){
drh0bce8352002-02-28 00:41:10 +000089 int len;
90
91 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000092 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000093 case SQLITE_BLOB:
94 case SQLITE_INTEGER:
95 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +000096 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +000097 break;
98 }
drh9c054832004-05-31 18:51:57 +000099 case SQLITE_TEXT: {
drh4f26d6c2004-05-26 23:25:30 +0000100 const char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000101 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +0000102 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +0000103 break;
104 }
105 default: {
106 sqlite3_result_null(context);
107 break;
108 }
109 }
drh0bce8352002-02-28 00:41:10 +0000110}
111
112/*
113** Implementation of the abs() function
114*/
danielk19770ae8b832004-05-25 12:05:56 +0000115static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000116 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000117 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000118 case SQLITE_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000119 i64 iVal = sqlite3_value_int64(argv[0]);
120 if( iVal<0 ) iVal = iVal * -1;
121 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000122 break;
123 }
drh9c054832004-05-31 18:51:57 +0000124 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000125 sqlite3_result_null(context);
126 break;
127 }
128 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000129 double rVal = sqlite3_value_double(argv[0]);
130 if( rVal<0 ) rVal = rVal * -1.0;
131 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000132 break;
133 }
134 }
drh0bce8352002-02-28 00:41:10 +0000135}
136
137/*
138** Implementation of the substr() function
139*/
drhf9b596e2004-05-26 16:54:42 +0000140static void substrFunc(
141 sqlite3_context *context,
142 int argc,
143 sqlite3_value **argv
144){
drh0bce8352002-02-28 00:41:10 +0000145 const char *z;
drh0bce8352002-02-28 00:41:10 +0000146 const char *z2;
147 int i;
drh0bce8352002-02-28 00:41:10 +0000148 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000149
drh0bce8352002-02-28 00:41:10 +0000150 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000151 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000152 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000153 p1 = sqlite3_value_int(argv[1]);
154 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000155 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000156 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000157 p1 += len;
drh653bc752002-02-28 03:31:10 +0000158 if( p1<0 ){
159 p2 += p1;
160 p1 = 0;
161 }
drh0bce8352002-02-28 00:41:10 +0000162 }else if( p1>0 ){
163 p1--;
164 }
165 if( p1+p2>len ){
166 p2 = len-p1;
167 }
drh77396302004-01-02 13:17:48 +0000168 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000169 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000170 }
drh47c8a672002-02-28 04:00:12 +0000171 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000172 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000173 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000174 }
drh47c8a672002-02-28 04:00:12 +0000175 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000176 if( p2<0 ) p2 = 0;
danielk1977d8123362004-06-12 09:25:12 +0000177 sqlite3_result_text(context, &z[p1], p2, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000178}
179
180/*
181** Implementation of the round() function
182*/
danielk19770ae8b832004-05-25 12:05:56 +0000183static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000184 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000185 double r;
186 char zBuf[100];
187 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000188 if( argc==2 ){
drh9c054832004-05-31 18:51:57 +0000189 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000190 n = sqlite3_value_int(argv[1]);
191 if( n>30 ) n = 30;
192 if( n<0 ) n = 0;
193 }
drh9c054832004-05-31 18:51:57 +0000194 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000195 r = sqlite3_value_double(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000196 sprintf(zBuf,"%.*f",n,r);
danielk1977d8123362004-06-12 09:25:12 +0000197 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000198}
drhdc04c582002-02-24 01:55:15 +0000199
200/*
201** Implementation of the upper() and lower() SQL functions.
202*/
danielk19770ae8b832004-05-25 12:05:56 +0000203static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000204 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000205 int i;
drh9c054832004-05-31 18:51:57 +0000206 if( argc<1 || SQLITE_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++){
drh4c755c02004-08-08 20:22:17 +0000211 z[i] = toupper(z[i]);
drhdc04c582002-02-24 01:55:15 +0000212 }
danielk1977d8123362004-06-12 09:25:12 +0000213 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000214 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000215}
danielk19770ae8b832004-05-25 12:05:56 +0000216static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000217 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000218 int i;
drh9c054832004-05-31 18:51:57 +0000219 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000220 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000221 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000222 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000223 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000224 z[i] = tolower(z[i]);
drhdc04c582002-02-24 01:55:15 +0000225 }
danielk1977d8123362004-06-12 09:25:12 +0000226 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000227 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000228}
229
230/*
drhfbc99082002-02-28 03:14:18 +0000231** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000232** All three do the same thing. They return the first non-NULL
233** argument.
drh3212e182002-02-28 00:46:26 +0000234*/
drhf9b596e2004-05-26 16:54:42 +0000235static void ifnullFunc(
236 sqlite3_context *context,
237 int argc,
238 sqlite3_value **argv
239){
drhfbc99082002-02-28 03:14:18 +0000240 int i;
241 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000242 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000243 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000244 break;
245 }
246 }
drh3212e182002-02-28 00:46:26 +0000247}
248
249/*
drhf9ffac92002-03-02 19:00:31 +0000250** Implementation of random(). Return a random integer.
251*/
drhf9b596e2004-05-26 16:54:42 +0000252static void randomFunc(
253 sqlite3_context *context,
254 int argc,
255 sqlite3_value **argv
256){
drhbbd82df2004-02-11 09:46:30 +0000257 int r;
danielk19774adee202004-05-08 08:23:19 +0000258 sqlite3Randomness(sizeof(r), &r);
drhf4479502004-05-27 03:12:53 +0000259 sqlite3_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000260}
261
262/*
drh6ed41ad2002-04-06 14:10:47 +0000263** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000264** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000265*/
danielk197751ad0ec2004-05-24 12:39:02 +0000266static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000267 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000268 int arg,
269 sqlite3_value **argv
270){
drh9bb575f2004-09-06 17:24:11 +0000271 sqlite3 *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000272 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000273}
274
rdcf146a772004-02-25 22:51:06 +0000275/*
danielk1977b28af712004-06-21 06:50:26 +0000276** Implementation of the changes() SQL function. The return value is the
277** same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000278*/
danielk1977b28af712004-06-21 06:50:26 +0000279static void changes(
drhf9b596e2004-05-26 16:54:42 +0000280 sqlite3_context *context,
281 int arg,
282 sqlite3_value **argv
283){
drh9bb575f2004-09-06 17:24:11 +0000284 sqlite3 *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000285 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000286}
rdcf146a772004-02-25 22:51:06 +0000287
288/*
danielk1977b28af712004-06-21 06:50:26 +0000289** Implementation of the total_changes() SQL function. The return value is
290** the same as the sqlite3_total_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000291*/
danielk1977b28af712004-06-21 06:50:26 +0000292static void total_changes(
293 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000294 int arg,
295 sqlite3_value **argv
296){
drh9bb575f2004-09-06 17:24:11 +0000297 sqlite3 *db = sqlite3_user_data(context);
danielk1977b28af712004-06-21 06:50:26 +0000298 sqlite3_result_int(context, sqlite3_total_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000299}
300
drh6ed41ad2002-04-06 14:10:47 +0000301/*
drh4e5ffc52004-08-31 00:52:37 +0000302** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03 +0000303*/
drh4e5ffc52004-08-31 00:52:37 +0000304struct compareInfo {
305 u8 matchAll;
306 u8 matchOne;
307 u8 matchSet;
308 u8 noCase;
danielk1977d02eb1f2004-06-06 09:44:03 +0000309};
drh4e5ffc52004-08-31 00:52:37 +0000310static const struct compareInfo globInfo = { '*', '?', '[', 0 };
311static const struct compareInfo likeInfo = { '%', '_', 0, 1 };
danielk1977d02eb1f2004-06-06 09:44:03 +0000312
313/*
drh4e5ffc52004-08-31 00:52:37 +0000314** X is a pointer to the first byte of a UTF-8 character. Increment
315** X so that it points to the next character. This only works right
316** if X points to a well-formed UTF-8 string.
danielk1977d02eb1f2004-06-06 09:44:03 +0000317*/
drh4e5ffc52004-08-31 00:52:37 +0000318#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
319#define sqliteCharVal(X) sqlite3ReadUtf8(X)
danielk1977d02eb1f2004-06-06 09:44:03 +0000320
danielk1977d02eb1f2004-06-06 09:44:03 +0000321
322/*
drh4e5ffc52004-08-31 00:52:37 +0000323** Compare two UTF-8 strings for equality where the first string can
324** potentially be a "glob" expression. Return true (1) if they
325** are the same and false (0) if they are different.
drh0ac65892002-04-20 14:24:41 +0000326**
drh4e5ffc52004-08-31 00:52:37 +0000327** Globbing rules:
drh0ac65892002-04-20 14:24:41 +0000328**
drh4e5ffc52004-08-31 00:52:37 +0000329** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03 +0000330**
drh4e5ffc52004-08-31 00:52:37 +0000331** '?' Matches exactly one character.
332**
333** [...] Matches one character from the enclosed list of
334** characters.
335**
336** [^...] Matches one character not in the enclosed list.
337**
338** With the [...] and [^...] matching, a ']' character can be included
339** in the list by making it the first character after '[' or '^'. A
340** range of characters can be specified using '-'. Example:
341** "[a-z]" matches any single lower-case letter. To match a '-', make
342** it the last character in the list.
343**
344** This routine is usually quick, but can be N**2 in the worst case.
345**
346** Hints: to match '*' or '?', put them in "[]". Like this:
347**
348** abc[*]xyz Matches "abc*xyz" only
drh0ac65892002-04-20 14:24:41 +0000349*/
danielk19777c6303c2004-11-17 16:41:29 +0000350static int patternCompare(
drh4e5ffc52004-08-31 00:52:37 +0000351 const u8 *zPattern, /* The glob pattern */
352 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29 +0000353 const struct compareInfo *pInfo, /* Information about how to do the compare */
354 const int esc /* The escape character */
danielk197751ad0ec2004-05-24 12:39:02 +0000355){
danielk1977ad7dd422004-06-06 12:41:49 +0000356 register int c;
drh4e5ffc52004-08-31 00:52:37 +0000357 int invert;
358 int seen;
359 int c2;
360 u8 matchOne = pInfo->matchOne;
361 u8 matchAll = pInfo->matchAll;
362 u8 matchSet = pInfo->matchSet;
363 u8 noCase = pInfo->noCase;
danielk19777c6303c2004-11-17 16:41:29 +0000364 int prevEscape = 0; /* True if the previous character was 'escape' */
danielk1977d02eb1f2004-06-06 09:44:03 +0000365
drh4e5ffc52004-08-31 00:52:37 +0000366 while( (c = *zPattern)!=0 ){
danielk19777c6303c2004-11-17 16:41:29 +0000367 if( !prevEscape && c==matchAll ){
drh4e5ffc52004-08-31 00:52:37 +0000368 while( (c=zPattern[1]) == matchAll || c == matchOne ){
369 if( c==matchOne ){
370 if( *zString==0 ) return 0;
371 sqliteNextChar(zString);
372 }
373 zPattern++;
danielk1977ad7dd422004-06-06 12:41:49 +0000374 }
drh20fc0882004-11-18 13:49:25 +0000375 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
danielk19777c6303c2004-11-17 16:41:29 +0000376 u8 const *zTemp = &zPattern[1];
377 sqliteNextChar(zTemp);
378 c = *zTemp;
379 }
drh4e5ffc52004-08-31 00:52:37 +0000380 if( c==0 ) return 1;
381 if( c==matchSet ){
danielk19777c6303c2004-11-17 16:41:29 +0000382 assert( esc==0 ); /* This is GLOB, not LIKE */
383 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
drh4e5ffc52004-08-31 00:52:37 +0000384 sqliteNextChar(zString);
385 }
386 return *zString!=0;
387 }else{
388 while( (c2 = *zString)!=0 ){
389 if( noCase ){
390 c2 = sqlite3UpperToLower[c2];
391 c = sqlite3UpperToLower[c];
392 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
393 }else{
394 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
395 }
396 if( c2==0 ) return 0;
danielk19777c6303c2004-11-17 16:41:29 +0000397 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
drh4e5ffc52004-08-31 00:52:37 +0000398 sqliteNextChar(zString);
399 }
400 return 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000401 }
danielk19777c6303c2004-11-17 16:41:29 +0000402 }else if( !prevEscape && c==matchOne ){
drh4e5ffc52004-08-31 00:52:37 +0000403 if( *zString==0 ) return 0;
404 sqliteNextChar(zString);
405 zPattern++;
406 }else if( c==matchSet ){
407 int prior_c = 0;
danielk19777c6303c2004-11-17 16:41:29 +0000408 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
drh4e5ffc52004-08-31 00:52:37 +0000409 seen = 0;
410 invert = 0;
411 c = sqliteCharVal(zString);
412 if( c==0 ) return 0;
413 c2 = *++zPattern;
414 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
415 if( c2==']' ){
416 if( c==']' ) seen = 1;
417 c2 = *++zPattern;
418 }
419 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
420 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
421 zPattern++;
422 c2 = sqliteCharVal(zPattern);
423 if( c>=prior_c && c<=c2 ) seen = 1;
424 prior_c = 0;
425 }else if( c==c2 ){
426 seen = 1;
427 prior_c = c2;
428 }else{
429 prior_c = c2;
430 }
431 sqliteNextChar(zPattern);
432 }
433 if( c2==0 || (seen ^ invert)==0 ) return 0;
434 sqliteNextChar(zString);
435 zPattern++;
drh20fc0882004-11-18 13:49:25 +0000436 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
danielk19777c6303c2004-11-17 16:41:29 +0000437 prevEscape = 1;
438 sqliteNextChar(zPattern);
drh4e5ffc52004-08-31 00:52:37 +0000439 }else{
440 if( noCase ){
441 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
442 }else{
443 if( c != *zString ) return 0;
444 }
445 zPattern++;
446 zString++;
danielk19777c6303c2004-11-17 16:41:29 +0000447 prevEscape = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000448 }
danielk197751ad0ec2004-05-24 12:39:02 +0000449 }
drh4e5ffc52004-08-31 00:52:37 +0000450 return *zString==0;
drh0ac65892002-04-20 14:24:41 +0000451}
drh4e5ffc52004-08-31 00:52:37 +0000452
danielk19773f6b0872004-06-17 05:36:44 +0000453
454/*
455** Implementation of the like() SQL function. This function implements
456** the build-in LIKE operator. The first argument to the function is the
457** pattern and the second argument is the string. So, the SQL statements:
458**
459** A LIKE B
460**
461** is implemented as like(B,A).
462**
463** If the pointer retrieved by via a call to sqlite3_user_data() is
464** not NULL, then this function uses UTF-16. Otherwise UTF-8.
465*/
466static void likeFunc(
467 sqlite3_context *context,
468 int argc,
469 sqlite3_value **argv
470){
471 const unsigned char *zA = sqlite3_value_text(argv[0]);
472 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk19777c6303c2004-11-17 16:41:29 +0000473 int escape = 0;
474 if( argc==3 ){
475 /* The escape character string must consist of a single UTF-8 character.
476 ** Otherwise, return an error.
477 */
478 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
479 if( sqlite3utf8CharLen(zEsc, -1)!=1 ){
480 sqlite3_result_error(context,
481 "ESCAPE expression must be a single character", -1);
482 return;
483 }
484 escape = sqlite3ReadUtf8(zEsc);
485 }
danielk19773f6b0872004-06-17 05:36:44 +0000486 if( zA && zB ){
danielk19777c6303c2004-11-17 16:41:29 +0000487 sqlite3_result_int(context, patternCompare(zA, zB, &likeInfo, escape));
danielk19773f6b0872004-06-17 05:36:44 +0000488 }
489}
drh0ac65892002-04-20 14:24:41 +0000490
491/*
492** Implementation of the glob() SQL function. This function implements
493** the build-in GLOB operator. The first argument to the function is the
494** string and the second argument is the pattern. So, the SQL statements:
495**
496** A GLOB B
497**
danielk19777c6303c2004-11-17 16:41:29 +0000498** is implemented as glob(B,A).
drh0ac65892002-04-20 14:24:41 +0000499*/
danielk19770ae8b832004-05-25 12:05:56 +0000500static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
drh4f26d6c2004-05-26 23:25:30 +0000501 const unsigned char *zA = sqlite3_value_text(argv[0]);
502 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000503 if( zA && zB ){
danielk19777c6303c2004-11-17 16:41:29 +0000504 sqlite3_result_int(context, patternCompare(zA, zB, &globInfo, 0));
danielk197751ad0ec2004-05-24 12:39:02 +0000505 }
drh8912d102002-05-26 21:34:58 +0000506}
507
508/*
509** Implementation of the NULLIF(x,y) function. The result is the first
510** argument if the arguments are different. The result is NULL if the
511** arguments are equal to each other.
512*/
drhf9b596e2004-05-26 16:54:42 +0000513static void nullifFunc(
514 sqlite3_context *context,
515 int argc,
516 sqlite3_value **argv
517){
danielk1977dc1bdc42004-06-11 10:51:27 +0000518 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
519 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000520 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000521 }
drh0ac65892002-04-20 14:24:41 +0000522}
523
drh647cb0e2002-11-04 19:32:25 +0000524/*
525** Implementation of the VERSION(*) function. The result is the version
526** of the SQLite library that is running.
527*/
drhf9b596e2004-05-26 16:54:42 +0000528static void versionFunc(
529 sqlite3_context *context,
530 int argc,
531 sqlite3_value **argv
532){
danielk1977d8123362004-06-12 09:25:12 +0000533 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:25 +0000534}
535
danielk19779fd2a9a2004-11-12 13:42:30 +0000536#ifndef SQLITE_OMIT_ALTERTABLE
537/*
538** This function is used by SQL generated to implement the
539** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
540** CREATE INDEX command. The second is a table name. The table name in
541** the CREATE TABLE or CREATE INDEX statement is replaced with the second
542** argument and the result returned. Examples:
543**
544** sqlite_alter_table('CREATE TABLE abc(a, b, c)', 'def')
545** -> 'CREATE TABLE def(a, b, c)'
546**
547** sqlite_alter_table('CREATE INDEX i ON abc(a)', 'def')
548** -> 'CREATE INDEX i ON def(a, b, c)'
549*/
550static void altertableFunc(
551 sqlite3_context *context,
552 int argc,
553 sqlite3_value **argv
554){
555 char const *zSql = sqlite3_value_text(argv[0]);
556 char const *zTableName = sqlite3_value_text(argv[1]);
557
558 char const *zCsr = zSql;
559 char const *zPrev;
560 char *zRet = 0;
561 int tokenType = 0;
562 int len;
563
564 assert( argc==2 );
565 if( zSql ){
566 while( tokenType!=TK_LP ){
567 zPrev = zCsr-len;
568 len = sqlite3GetToken(zCsr, &tokenType);
569 zCsr += len;
570 }
571
572 zRet = sqlite3MPrintf("%.*s%Q(%s", zPrev-zSql, zSql, zTableName, zCsr);
573 sqlite3_result_text(context, zRet, -1, SQLITE_TRANSIENT);
574 sqliteFree(zRet);
575 }
576}
577#endif
578
danielk1977d641d642004-11-18 15:44:29 +0000579#ifndef SQLITE_OMIT_ALTERTABLE
580#ifndef SQLITE_OMIT_TRIGGER
581/* This function is used by SQL generated to implement the ALTER TABLE
582** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
583** statement. The second is a table name. The table name in the CREATE
584** TRIGGER statement is replaced with the second argument and the result
585** returned. This is analagous to altertableFunc() above, except for CREATE
586** TRIGGER, not CREATE INDEX and CREATE TABLE.
587*/
588static void altertriggerFunc(
589 sqlite3_context *context,
590 int argc,
591 sqlite3_value **argv
592){
593 unsigned char const *zSql = sqlite3_value_text(argv[0]);
594 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
595
596 int token;
597 Token tname;
598 int dist = 3;
599 char const *zCsr = zSql;
600 int len = 0;
601 char *zRet;
602
603 /* The principle used to locate the table name in the CREATE TRIGGER
604 ** statement is that the table name is the first token that is immediatedly
605 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
606 ** of TK_WHEN, TK_BEGIN or TK_FOR.
607 */
608 assert( argc==2 );
609 if( zSql ){
610 do {
611 /* Store the token that zCsr points to in tname. */
612 tname.z = zCsr;
613 tname.n = len;
614
615 /* Advance zCsr to the next token. Store that token type in 'token',
616 ** and it's length in 'len' (to be used next iteration of this loop).
617 */
618 do {
619 zCsr += len;
620 len = sqlite3GetToken(zCsr, &token);
621 }while( token==TK_SPACE );
622 assert( len>0 );
623
624 /* Variable 'dist' stores the number of tokens read since the most
625 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
626 ** token is read and 'dist' equals 2, the condition stated above
627 ** to be met.
628 **
629 ** Note that ON cannot be a database, table or column name, so
630 ** there is no need to worry about syntax like
631 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
632 */
633 dist++;
634 if( token==TK_DOT || token==TK_ON ){
635 dist = 0;
636 }
637 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
638
639 /* Variable tname now contains the token that is the old table-name
640 ** in the CREATE TRIGGER statement.
641 */
642 zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
643 zTableName, tname.z+tname.n);
644 sqlite3_result_text(context, zRet, -1, SQLITE_TRANSIENT);
645 sqliteFree(zRet);
646 }
647}
648#endif /* !SQLITE_OMIT_TRIGGER */
649#endif /* !SQLITE_OMIT_ALTERTABLE */
650
drh47394702003-08-20 01:03:33 +0000651/*
652** EXPERIMENTAL - This is not an official function. The interface may
653** change. This function may disappear. Do not write code that depends
654** on this function.
655**
656** Implementation of the QUOTE() function. This function takes a single
657** argument. If the argument is numeric, the return value is the same as
658** the argument. If the argument is NULL, the return value is the string
659** "NULL". Otherwise, the argument is enclosed in single quotes with
660** single-quote escapes.
661*/
danielk19770ae8b832004-05-25 12:05:56 +0000662static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000663 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000664 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000665 case SQLITE_NULL: {
danielk1977d8123362004-06-12 09:25:12 +0000666 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
drhf9b596e2004-05-26 16:54:42 +0000667 break;
drh47394702003-08-20 01:03:33 +0000668 }
drh9c054832004-05-31 18:51:57 +0000669 case SQLITE_INTEGER:
670 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000671 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000672 break;
673 }
danielk19773f41e972004-06-08 00:39:01 +0000674 case SQLITE_BLOB: {
675 static const char hexdigits[] = {
676 '0', '1', '2', '3', '4', '5', '6', '7',
677 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
678 };
679 char *zText = 0;
680 int nBlob = sqlite3_value_bytes(argv[0]);
681 char const *zBlob = sqlite3_value_blob(argv[0]);
682
683 zText = (char *)sqliteMalloc((2*nBlob)+4);
684 if( !zText ){
685 sqlite3_result_error(context, "out of memory", -1);
686 }else{
687 int i;
688 for(i=0; i<nBlob; i++){
689 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
690 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
691 }
692 zText[(nBlob*2)+2] = '\'';
693 zText[(nBlob*2)+3] = '\0';
694 zText[0] = 'X';
695 zText[1] = '\'';
danielk1977d8123362004-06-12 09:25:12 +0000696 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
danielk19773f41e972004-06-08 00:39:01 +0000697 sqliteFree(zText);
698 }
699 break;
700 }
drh9c054832004-05-31 18:51:57 +0000701 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000702 int i,j,n;
drh4f26d6c2004-05-26 23:25:30 +0000703 const char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000704 char *z;
705
706 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
707 z = sqliteMalloc( i+n+3 );
708 if( z==0 ) return;
709 z[0] = '\'';
710 for(i=0, j=1; zArg[i]; i++){
711 z[j++] = zArg[i];
712 if( zArg[i]=='\'' ){
713 z[j++] = '\'';
714 }
715 }
716 z[j++] = '\'';
717 z[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000718 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
drhf9b596e2004-05-26 16:54:42 +0000719 sqliteFree(z);
720 }
drh47394702003-08-20 01:03:33 +0000721 }
722}
723
drhd24cc422003-03-27 12:51:24 +0000724#ifdef SQLITE_SOUNDEX
725/*
726** Compute the soundex encoding of a word.
727*/
danielk19770ae8b832004-05-25 12:05:56 +0000728static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000729 char zResult[8];
drh4c755c02004-08-08 20:22:17 +0000730 const u8 *zIn;
drhd24cc422003-03-27 12:51:24 +0000731 int i, j;
732 static const unsigned char iCode[] = {
733 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
734 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
735 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
736 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
737 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
738 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
739 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
740 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
741 };
742 assert( argc==1 );
drh4c755c02004-08-08 20:22:17 +0000743 zIn = (u8*)sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000744 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
745 if( zIn[i] ){
746 zResult[0] = toupper(zIn[i]);
747 for(j=1; j<4 && zIn[i]; i++){
748 int code = iCode[zIn[i]&0x7f];
749 if( code>0 ){
750 zResult[j++] = code + '0';
751 }
752 }
753 while( j<4 ){
754 zResult[j++] = '0';
755 }
756 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000757 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:24 +0000758 }else{
danielk1977d8123362004-06-12 09:25:12 +0000759 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:24 +0000760 }
761}
762#endif
763
drh193a6b42002-07-07 16:52:46 +0000764#ifdef SQLITE_TEST
765/*
766** This function generates a string of random characters. Used for
767** generating test data.
768*/
danielk19770ae8b832004-05-25 12:05:56 +0000769static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000770 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000771 "abcdefghijklmnopqrstuvwxyz"
772 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
773 "0123456789"
774 ".-!,:*^+=_|?/<> ";
775 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000776 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000777 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000778 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000779 if( iMin<0 ) iMin = 0;
780 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
781 }else{
782 iMin = 1;
783 }
784 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000785 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000786 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000787 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000788 }else{
789 iMax = 50;
790 }
791 n = iMin;
792 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000793 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000794 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000795 n += r%(iMax + 1 - iMin);
796 }
drh1dba7272004-01-16 13:58:18 +0000797 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000798 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000799 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000800 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000801 }
802 zBuf[n] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000803 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT);
804}
drh0e3d7472004-06-19 17:33:07 +0000805#endif /* SQLITE_TEST */
danielk1977d8123362004-06-12 09:25:12 +0000806
drh0e3d7472004-06-19 17:33:07 +0000807#ifdef SQLITE_TEST
danielk1977d8123362004-06-12 09:25:12 +0000808/*
809** The following two SQL functions are used to test returning a text
810** result with a destructor. Function 'test_destructor' takes one argument
811** and returns the same argument interpreted as TEXT. A destructor is
812** passed with the sqlite3_result_text() call.
813**
814** SQL function 'test_destructor_count' returns the number of outstanding
815** allocations made by 'test_destructor';
816**
817** WARNING: Not threadsafe.
818*/
819static int test_destructor_count_var = 0;
820static void destructor(void *p){
821 char *zVal = (char *)p;
822 assert(zVal);
823 zVal--;
824 sqliteFree(zVal);
825 test_destructor_count_var--;
826}
827static void test_destructor(
828 sqlite3_context *pCtx,
829 int nArg,
830 sqlite3_value **argv
831){
832 char *zVal;
danielk1977f4618892004-06-28 13:09:11 +0000833 int len;
drh9bb575f2004-09-06 17:24:11 +0000834 sqlite3 *db = sqlite3_user_data(pCtx);
danielk1977f4618892004-06-28 13:09:11 +0000835
danielk1977d8123362004-06-12 09:25:12 +0000836 test_destructor_count_var++;
837 assert( nArg==1 );
838 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
danielk1977f4618892004-06-28 13:09:11 +0000839 len = sqlite3ValueBytes(argv[0], db->enc);
840 zVal = sqliteMalloc(len+3);
841 zVal[len] = 0;
842 zVal[len-1] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000843 assert( zVal );
844 zVal++;
danielk1977f4618892004-06-28 13:09:11 +0000845 memcpy(zVal, sqlite3ValueText(argv[0], db->enc), len);
846 if( db->enc==SQLITE_UTF8 ){
847 sqlite3_result_text(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000848#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +0000849 }else if( db->enc==SQLITE_UTF16LE ){
850 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
851 }else{
852 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000853#endif /* SQLITE_OMIT_UTF16 */
danielk1977f4618892004-06-28 13:09:11 +0000854 }
danielk1977d8123362004-06-12 09:25:12 +0000855}
856static void test_destructor_count(
857 sqlite3_context *pCtx,
858 int nArg,
859 sqlite3_value **argv
860){
861 sqlite3_result_int(pCtx, test_destructor_count_var);
drh193a6b42002-07-07 16:52:46 +0000862}
drh0e3d7472004-06-19 17:33:07 +0000863#endif /* SQLITE_TEST */
danielk19773f6b0872004-06-17 05:36:44 +0000864
drh0e3d7472004-06-19 17:33:07 +0000865#ifdef SQLITE_TEST
866/*
867** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
868** interface.
869**
870** The test_auxdata() SQL function attempts to register each of its arguments
871** as auxiliary data. If there are no prior registrations of aux data for
872** that argument (meaning the argument is not a constant or this is its first
873** call) then the result for that argument is 0. If there is a prior
874** registration, the result for that argument is 1. The overall result
875** is the individual argument results separated by spaces.
876*/
danielk19773f6b0872004-06-17 05:36:44 +0000877static void free_test_auxdata(void *p) {sqliteFree(p);}
878static void test_auxdata(
879 sqlite3_context *pCtx,
880 int nArg,
881 sqlite3_value **argv
882){
883 int i;
884 char *zRet = sqliteMalloc(nArg*2);
885 if( !zRet ) return;
886 for(i=0; i<nArg; i++){
887 char const *z = sqlite3_value_text(argv[i]);
888 if( z ){
889 char *zAux = sqlite3_get_auxdata(pCtx, i);
890 if( zAux ){
891 zRet[i*2] = '1';
892 if( strcmp(zAux, z) ){
893 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
894 return;
895 }
896 }else{
897 zRet[i*2] = '0';
898 zAux = sqliteStrDup(z);
899 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
900 }
901 zRet[i*2+1] = ' ';
902 }
903 }
904 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
905}
drh0e3d7472004-06-19 17:33:07 +0000906#endif /* SQLITE_TEST */
drh193a6b42002-07-07 16:52:46 +0000907
drh0ac65892002-04-20 14:24:41 +0000908/*
drhd3a149e2002-02-24 17:12:53 +0000909** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000910** sum() or avg() aggregate computation.
911*/
912typedef struct SumCtx SumCtx;
913struct SumCtx {
914 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000915 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000916};
917
918/*
919** Routines used to compute the sum or average.
920*/
danielk19770ae8b832004-05-25 12:05:56 +0000921static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000922 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000923 if( argc<1 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000924 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000925 if( p && SQLITE_NULL!=sqlite3_value_type(argv[0]) ){
drh4f26d6c2004-05-26 23:25:30 +0000926 p->sum += sqlite3_value_double(argv[0]);
drh739105c2002-05-29 23:22:23 +0000927 p->cnt++;
928 }
drhdd5baa92002-02-27 19:50:59 +0000929}
danielk19770ae8b832004-05-25 12:05:56 +0000930static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000931 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000932 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000933 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000934}
danielk19770ae8b832004-05-25 12:05:56 +0000935static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000936 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000937 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000938 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000939 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000940 }
941}
942
943/*
944** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000945** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000946*/
947typedef struct StdDevCtx StdDevCtx;
948struct StdDevCtx {
949 double sum; /* Sum of terms */
950 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000951 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000952};
953
drhef2daf52002-03-04 02:26:15 +0000954#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000955/*
956** Routines used to compute the standard deviation as an aggregate.
957*/
danielk19770ae8b832004-05-25 12:05:56 +0000958static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000959 StdDevCtx *p;
960 double x;
drh1350b032002-02-27 19:00:20 +0000961 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000962 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000963 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000964 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000965 p->sum += x;
966 p->sum2 += x*x;
967 p->cnt++;
968 }
drhd3a149e2002-02-24 17:12:53 +0000969}
danielk19770ae8b832004-05-25 12:05:56 +0000970static void stdDevFinalize(sqlite3_context *context){
danielk197724b03fd2004-05-10 10:34:34 +0000971 double rN = sqlite3_aggregate_count(context);
972 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000973 if( p && p->cnt>1 ){
974 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000975 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000976 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000977 }
drhd3a149e2002-02-24 17:12:53 +0000978}
drhef2daf52002-03-04 02:26:15 +0000979#endif
drhd3a149e2002-02-24 17:12:53 +0000980
drh0bce8352002-02-28 00:41:10 +0000981/*
982** The following structure keeps track of state information for the
983** count() aggregate function.
984*/
985typedef struct CountCtx CountCtx;
986struct CountCtx {
987 int n;
988};
drhdd5baa92002-02-27 19:50:59 +0000989
drh0bce8352002-02-28 00:41:10 +0000990/*
991** Routines to implement the count() aggregate function.
992*/
danielk19770ae8b832004-05-25 12:05:56 +0000993static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000994 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000995 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000996 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000997 p->n++;
998 }
999}
danielk19770ae8b832004-05-25 12:05:56 +00001000static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +00001001 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +00001002 p = sqlite3_aggregate_context(context, sizeof(*p));
drhf4479502004-05-27 03:12:53 +00001003 sqlite3_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +00001004}
1005
1006/*
1007** This function tracks state information for the min() and max()
1008** aggregate functions.
1009*/
1010typedef struct MinMaxCtx MinMaxCtx;
1011struct MinMaxCtx {
1012 char *z; /* The best so far */
1013 char zBuf[28]; /* Space that can be used for storage */
1014};
1015
1016/*
1017** Routines to implement min() and max() aggregate functions.
1018*/
danielk19770ae8b832004-05-25 12:05:56 +00001019static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +00001020 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:32 +00001021 Mem *pBest;
1022
1023 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1024 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:04 +00001025 if( !pBest ) return;
drh268380c2004-02-25 13:47:31 +00001026
danielk197788208052004-05-25 01:13:20 +00001027 if( pBest->flags ){
drh9eb516c2004-07-18 20:52:32 +00001028 int max;
1029 int cmp;
danielk1977dc1bdc42004-06-11 10:51:27 +00001030 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +00001031 /* This step function is used for both the min() and max() aggregates,
1032 ** the only difference between the two being that the sense of the
1033 ** comparison is inverted. For the max() aggregate, the
1034 ** sqlite3_user_data() function returns (void *)-1. For min() it
1035 ** returns (void *)db, where db is the sqlite3* database pointer.
1036 ** Therefore the next statement sets variable 'max' to 1 for the max()
1037 ** aggregate, or 0 for min().
1038 */
danielk197788208052004-05-25 01:13:20 +00001039 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001040 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +00001041 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +00001042 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +00001043 }
drh268380c2004-02-25 13:47:31 +00001044 }else{
danielk19777e18c252004-05-25 11:47:24 +00001045 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +00001046 }
1047}
danielk19770ae8b832004-05-25 12:05:56 +00001048static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +00001049 sqlite3_value *pRes;
drh4f26d6c2004-05-26 23:25:30 +00001050 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +00001051 if( pRes->flags ){
drhf4479502004-05-27 03:12:53 +00001052 sqlite3_result_value(context, pRes);
drh0bce8352002-02-28 00:41:10 +00001053 }
danielk1977b20e56b2004-06-15 13:36:30 +00001054 sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:10 +00001055}
drhdd5baa92002-02-27 19:50:59 +00001056
drh4e5ffc52004-08-31 00:52:37 +00001057
drhd3a149e2002-02-24 17:12:53 +00001058/*
drha2ed5602002-02-26 23:55:31 +00001059** This function registered all of the above C functions as SQL
1060** functions. This should be the only routine in this file with
1061** external linkage.
drhdc04c582002-02-24 01:55:15 +00001062*/
drh9bb575f2004-09-06 17:24:11 +00001063void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
drh57196282004-10-06 15:41:16 +00001064 static const struct {
drh0bce8352002-02-28 00:41:10 +00001065 char *zName;
drh268380c2004-02-25 13:47:31 +00001066 signed char nArg;
danielk1977f4618892004-06-28 13:09:11 +00001067 u8 argType; /* 0: none. 1: db 2: (-1) */
1068 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +00001069 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001070 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +00001071 } aFuncs[] = {
danielk1977f4618892004-06-28 13:09:11 +00001072 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
1073 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
1074 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc },
1075 { "max", 0, 2, SQLITE_UTF8, 1, 0 },
1076 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
1077 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
1078 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
drh6c626082004-11-14 21:56:29 +00001079#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +00001080 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
drh6c626082004-11-14 21:56:29 +00001081#endif
danielk1977f4618892004-06-28 13:09:11 +00001082 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
1083 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
1084 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
1085 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
1086 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
1087 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
1088 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
1089 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
1090 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
1091 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
1092 { "like", 2, 0, SQLITE_UTF8, 0, likeFunc },
danielk19777c6303c2004-11-17 16:41:29 +00001093 { "like", 3, 0, SQLITE_UTF8, 0, likeFunc },
danielk1977f4618892004-06-28 13:09:11 +00001094 { "glob", 2, 0, SQLITE_UTF8, 0, globFunc },
drh94a98362004-09-13 13:13:18 +00001095 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
danielk1977f4618892004-06-28 13:09:11 +00001096 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
1097 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
1098 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid },
1099 { "changes", 0, 1, SQLITE_UTF8, 0, changes },
1100 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes },
danielk19779fd2a9a2004-11-12 13:42:30 +00001101#ifndef SQLITE_OMIT_ALTERTABLE
1102 { "sqlite_alter_table", 2, 0, SQLITE_UTF8, 0, altertableFunc},
danielk1977d641d642004-11-18 15:44:29 +00001103#ifndef SQLITE_OMIT_TRIGGER
1104 { "sqlite_alter_trigger", 2, 0, SQLITE_UTF8, 0, altertriggerFunc},
1105#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00001106#endif
drhd24cc422003-03-27 12:51:24 +00001107#ifdef SQLITE_SOUNDEX
danielk1977f4618892004-06-28 13:09:11 +00001108 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +00001109#endif
drh193a6b42002-07-07 16:52:46 +00001110#ifdef SQLITE_TEST
danielk1977f4618892004-06-28 13:09:11 +00001111 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
1112 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor},
danielk1977d8123362004-06-12 09:25:12 +00001113 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
danielk1977f4618892004-06-28 13:09:11 +00001114 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
drh193a6b42002-07-07 16:52:46 +00001115#endif
drh0bce8352002-02-28 00:41:10 +00001116 };
drh57196282004-10-06 15:41:16 +00001117 static const struct {
drh0bce8352002-02-28 00:41:10 +00001118 char *zName;
drh268380c2004-02-25 13:47:31 +00001119 signed char nArg;
drh268380c2004-02-25 13:47:31 +00001120 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +00001121 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001122 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1123 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +00001124 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +00001125 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1126 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
1127 { "sum", 1, 0, 0, sumStep, sumFinalize },
1128 { "avg", 1, 0, 0, sumStep, avgFinalize },
1129 { "count", 0, 0, 0, countStep, countFinalize },
1130 { "count", 1, 0, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +00001131#if 0
drhf9b596e2004-05-26 16:54:42 +00001132 { "stddev", 1, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +00001133#endif
drh0bce8352002-02-28 00:41:10 +00001134 };
1135 int i;
1136
1137 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001138 void *pArg = 0;
1139 switch( aFuncs[i].argType ){
1140 case 1: pArg = db; break;
1141 case 2: pArg = (void *)(-1); break;
1142 }
danielk1977ad7dd422004-06-06 12:41:49 +00001143 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
danielk1977f9d64d22004-06-19 08:18:07 +00001144 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001145 if( aFuncs[i].needCollSeq ){
1146 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1147 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1148 if( pFunc && aFuncs[i].needCollSeq ){
1149 pFunc->needCollSeq = 1;
1150 }
1151 }
drh0bce8352002-02-28 00:41:10 +00001152 }
1153 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001154 void *pArg = 0;
1155 switch( aAggs[i].argType ){
1156 case 1: pArg = db; break;
1157 case 2: pArg = (void *)(-1); break;
1158 }
danielk1977d8123362004-06-12 09:25:12 +00001159 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001160 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +00001161 if( aAggs[i].needCollSeq ){
1162 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
danielk1977d8123362004-06-12 09:25:12 +00001163 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001164 if( pFunc && aAggs[i].needCollSeq ){
1165 pFunc->needCollSeq = 1;
1166 }
1167 }
drh268380c2004-02-25 13:47:31 +00001168 }
danielk19774adee202004-05-08 08:23:19 +00001169 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +00001170}