blob: 205742a3e87e01f5aa1f53e46823eadca16e4aa8 [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**
drhe866fcb2005-07-09 02:38:06 +000019** $Id: func.c,v 1.102 2005/07/09 02:38:06 drh Exp $
drhdc04c582002-02-24 01:55:15 +000020*/
drhb659e9b2005-01-28 01:29:08 +000021#include "sqliteInt.h"
drhdc04c582002-02-24 01:55:15 +000022#include <ctype.h>
drhd3a149e2002-02-24 17:12:53 +000023#include <math.h>
24#include <stdlib.h>
drh0bce8352002-02-28 00:41:10 +000025#include <assert.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
drh5708d2d2005-06-22 10:53:59 +000081
82/*
drh0bce8352002-02-28 00:41:10 +000083** Implementation of the length() function
84*/
drhf9b596e2004-05-26 16:54:42 +000085static void lengthFunc(
86 sqlite3_context *context,
87 int argc,
88 sqlite3_value **argv
89){
drh0bce8352002-02-28 00:41:10 +000090 int len;
91
92 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000093 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000094 case SQLITE_BLOB:
95 case SQLITE_INTEGER:
96 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +000097 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +000098 break;
99 }
drh9c054832004-05-31 18:51:57 +0000100 case SQLITE_TEXT: {
drh4f26d6c2004-05-26 23:25:30 +0000101 const char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000102 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +0000103 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +0000104 break;
105 }
106 default: {
107 sqlite3_result_null(context);
108 break;
109 }
110 }
drh0bce8352002-02-28 00:41:10 +0000111}
112
113/*
114** Implementation of the abs() function
115*/
danielk19770ae8b832004-05-25 12:05:56 +0000116static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000117 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000118 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000119 case SQLITE_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000120 i64 iVal = sqlite3_value_int64(argv[0]);
121 if( iVal<0 ) iVal = iVal * -1;
122 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000123 break;
124 }
drh9c054832004-05-31 18:51:57 +0000125 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000126 sqlite3_result_null(context);
127 break;
128 }
129 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000130 double rVal = sqlite3_value_double(argv[0]);
131 if( rVal<0 ) rVal = rVal * -1.0;
132 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000133 break;
134 }
135 }
drh0bce8352002-02-28 00:41:10 +0000136}
137
138/*
139** Implementation of the substr() function
140*/
drhf9b596e2004-05-26 16:54:42 +0000141static void substrFunc(
142 sqlite3_context *context,
143 int argc,
144 sqlite3_value **argv
145){
drh0bce8352002-02-28 00:41:10 +0000146 const char *z;
drh0bce8352002-02-28 00:41:10 +0000147 const char *z2;
148 int i;
drh0bce8352002-02-28 00:41:10 +0000149 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000150
drh0bce8352002-02-28 00:41:10 +0000151 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000152 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000153 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000154 p1 = sqlite3_value_int(argv[1]);
155 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000156 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000157 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000158 p1 += len;
drh653bc752002-02-28 03:31:10 +0000159 if( p1<0 ){
160 p2 += p1;
161 p1 = 0;
162 }
drh0bce8352002-02-28 00:41:10 +0000163 }else if( p1>0 ){
164 p1--;
165 }
166 if( p1+p2>len ){
167 p2 = len-p1;
168 }
drh77396302004-01-02 13:17:48 +0000169 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000170 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000171 }
drh47c8a672002-02-28 04:00:12 +0000172 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000173 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000174 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000175 }
drh47c8a672002-02-28 04:00:12 +0000176 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000177 if( p2<0 ) p2 = 0;
danielk1977d8123362004-06-12 09:25:12 +0000178 sqlite3_result_text(context, &z[p1], p2, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000179}
180
181/*
182** Implementation of the round() function
183*/
danielk19770ae8b832004-05-25 12:05:56 +0000184static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000185 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000186 double r;
187 char zBuf[100];
188 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000189 if( argc==2 ){
drh9c054832004-05-31 18:51:57 +0000190 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000191 n = sqlite3_value_int(argv[1]);
192 if( n>30 ) n = 30;
193 if( n<0 ) n = 0;
194 }
drh9c054832004-05-31 18:51:57 +0000195 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000196 r = sqlite3_value_double(argv[0]);
drhe866fcb2005-07-09 02:38:06 +0000197 sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
danielk1977d8123362004-06-12 09:25:12 +0000198 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000199}
drhdc04c582002-02-24 01:55:15 +0000200
201/*
202** Implementation of the upper() and lower() SQL functions.
203*/
danielk19770ae8b832004-05-25 12:05:56 +0000204static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000205 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000206 int i;
drh9c054832004-05-31 18:51:57 +0000207 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000208 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000209 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000210 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000211 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000212 z[i] = toupper(z[i]);
drhdc04c582002-02-24 01:55:15 +0000213 }
danielk1977d8123362004-06-12 09:25:12 +0000214 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000215 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000216}
danielk19770ae8b832004-05-25 12:05:56 +0000217static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000218 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000219 int i;
drh9c054832004-05-31 18:51:57 +0000220 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000221 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000222 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000223 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000224 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000225 z[i] = tolower(z[i]);
drhdc04c582002-02-24 01:55:15 +0000226 }
danielk1977d8123362004-06-12 09:25:12 +0000227 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000228 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000229}
230
231/*
drhfbc99082002-02-28 03:14:18 +0000232** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000233** All three do the same thing. They return the first non-NULL
234** argument.
drh3212e182002-02-28 00:46:26 +0000235*/
drhf9b596e2004-05-26 16:54:42 +0000236static void ifnullFunc(
237 sqlite3_context *context,
238 int argc,
239 sqlite3_value **argv
240){
drhfbc99082002-02-28 03:14:18 +0000241 int i;
242 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000243 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000244 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000245 break;
246 }
247 }
drh3212e182002-02-28 00:46:26 +0000248}
249
250/*
drhf9ffac92002-03-02 19:00:31 +0000251** Implementation of random(). Return a random integer.
252*/
drhf9b596e2004-05-26 16:54:42 +0000253static void randomFunc(
254 sqlite3_context *context,
255 int argc,
256 sqlite3_value **argv
257){
drhbbd82df2004-02-11 09:46:30 +0000258 int r;
danielk19774adee202004-05-08 08:23:19 +0000259 sqlite3Randomness(sizeof(r), &r);
drhf4479502004-05-27 03:12:53 +0000260 sqlite3_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000261}
262
263/*
drh6ed41ad2002-04-06 14:10:47 +0000264** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000265** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000266*/
danielk197751ad0ec2004-05-24 12:39:02 +0000267static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000268 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000269 int arg,
270 sqlite3_value **argv
271){
drh9bb575f2004-09-06 17:24:11 +0000272 sqlite3 *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000273 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000274}
275
rdcf146a772004-02-25 22:51:06 +0000276/*
danielk1977b28af712004-06-21 06:50:26 +0000277** Implementation of the changes() SQL function. The return value is the
278** same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000279*/
danielk1977b28af712004-06-21 06:50:26 +0000280static void changes(
drhf9b596e2004-05-26 16:54:42 +0000281 sqlite3_context *context,
282 int arg,
283 sqlite3_value **argv
284){
drh9bb575f2004-09-06 17:24:11 +0000285 sqlite3 *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000286 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000287}
rdcf146a772004-02-25 22:51:06 +0000288
289/*
danielk1977b28af712004-06-21 06:50:26 +0000290** Implementation of the total_changes() SQL function. The return value is
291** the same as the sqlite3_total_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000292*/
danielk1977b28af712004-06-21 06:50:26 +0000293static void total_changes(
294 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000295 int arg,
296 sqlite3_value **argv
297){
drh9bb575f2004-09-06 17:24:11 +0000298 sqlite3 *db = sqlite3_user_data(context);
danielk1977b28af712004-06-21 06:50:26 +0000299 sqlite3_result_int(context, sqlite3_total_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000300}
301
drh6ed41ad2002-04-06 14:10:47 +0000302/*
drh4e5ffc52004-08-31 00:52:37 +0000303** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03 +0000304*/
drh4e5ffc52004-08-31 00:52:37 +0000305struct compareInfo {
306 u8 matchAll;
307 u8 matchOne;
308 u8 matchSet;
309 u8 noCase;
danielk1977d02eb1f2004-06-06 09:44:03 +0000310};
drh4e5ffc52004-08-31 00:52:37 +0000311static const struct compareInfo globInfo = { '*', '?', '[', 0 };
drh70031fa2005-07-08 13:53:21 +0000312#ifndef SQLITE_CASE_SENSITIVE_LIKE
313 /* The correct SQL-92 behavior is for the LIKE operator to ignore
314 ** case. Thus 'a' LIKE 'A' would be true. */
315 static const struct compareInfo likeInfo = { '%', '_', 0, 1 };
316#else
317 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
318 ** is case sensitive causing 'a' LIKE 'A' to be false */
319 static const struct compareInfo likeInfo = { '%', '_', 0, 0 };
320#endif
danielk1977d02eb1f2004-06-06 09:44:03 +0000321
322/*
drh4e5ffc52004-08-31 00:52:37 +0000323** X is a pointer to the first byte of a UTF-8 character. Increment
324** X so that it points to the next character. This only works right
325** if X points to a well-formed UTF-8 string.
danielk1977d02eb1f2004-06-06 09:44:03 +0000326*/
drh4e5ffc52004-08-31 00:52:37 +0000327#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
328#define sqliteCharVal(X) sqlite3ReadUtf8(X)
danielk1977d02eb1f2004-06-06 09:44:03 +0000329
danielk1977d02eb1f2004-06-06 09:44:03 +0000330
331/*
drh4e5ffc52004-08-31 00:52:37 +0000332** Compare two UTF-8 strings for equality where the first string can
333** potentially be a "glob" expression. Return true (1) if they
334** are the same and false (0) if they are different.
drh0ac65892002-04-20 14:24:41 +0000335**
drh4e5ffc52004-08-31 00:52:37 +0000336** Globbing rules:
drh0ac65892002-04-20 14:24:41 +0000337**
drh4e5ffc52004-08-31 00:52:37 +0000338** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03 +0000339**
drh4e5ffc52004-08-31 00:52:37 +0000340** '?' Matches exactly one character.
341**
342** [...] Matches one character from the enclosed list of
343** characters.
344**
345** [^...] Matches one character not in the enclosed list.
346**
347** With the [...] and [^...] matching, a ']' character can be included
348** in the list by making it the first character after '[' or '^'. A
349** range of characters can be specified using '-'. Example:
350** "[a-z]" matches any single lower-case letter. To match a '-', make
351** it the last character in the list.
352**
353** This routine is usually quick, but can be N**2 in the worst case.
354**
355** Hints: to match '*' or '?', put them in "[]". Like this:
356**
357** abc[*]xyz Matches "abc*xyz" only
drh0ac65892002-04-20 14:24:41 +0000358*/
danielk19777c6303c2004-11-17 16:41:29 +0000359static int patternCompare(
drh4e5ffc52004-08-31 00:52:37 +0000360 const u8 *zPattern, /* The glob pattern */
361 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29 +0000362 const struct compareInfo *pInfo, /* Information about how to do the compare */
363 const int esc /* The escape character */
danielk197751ad0ec2004-05-24 12:39:02 +0000364){
danielk1977ad7dd422004-06-06 12:41:49 +0000365 register int c;
drh4e5ffc52004-08-31 00:52:37 +0000366 int invert;
367 int seen;
368 int c2;
369 u8 matchOne = pInfo->matchOne;
370 u8 matchAll = pInfo->matchAll;
371 u8 matchSet = pInfo->matchSet;
372 u8 noCase = pInfo->noCase;
danielk19777c6303c2004-11-17 16:41:29 +0000373 int prevEscape = 0; /* True if the previous character was 'escape' */
danielk1977d02eb1f2004-06-06 09:44:03 +0000374
drh4e5ffc52004-08-31 00:52:37 +0000375 while( (c = *zPattern)!=0 ){
danielk19777c6303c2004-11-17 16:41:29 +0000376 if( !prevEscape && c==matchAll ){
drh4e5ffc52004-08-31 00:52:37 +0000377 while( (c=zPattern[1]) == matchAll || c == matchOne ){
378 if( c==matchOne ){
379 if( *zString==0 ) return 0;
380 sqliteNextChar(zString);
381 }
382 zPattern++;
danielk1977ad7dd422004-06-06 12:41:49 +0000383 }
drh20fc0882004-11-18 13:49:25 +0000384 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
danielk19777c6303c2004-11-17 16:41:29 +0000385 u8 const *zTemp = &zPattern[1];
386 sqliteNextChar(zTemp);
387 c = *zTemp;
388 }
drh4e5ffc52004-08-31 00:52:37 +0000389 if( c==0 ) return 1;
390 if( c==matchSet ){
danielk19777c6303c2004-11-17 16:41:29 +0000391 assert( esc==0 ); /* This is GLOB, not LIKE */
392 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
drh4e5ffc52004-08-31 00:52:37 +0000393 sqliteNextChar(zString);
394 }
395 return *zString!=0;
396 }else{
397 while( (c2 = *zString)!=0 ){
398 if( noCase ){
399 c2 = sqlite3UpperToLower[c2];
400 c = sqlite3UpperToLower[c];
401 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
402 }else{
403 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
404 }
405 if( c2==0 ) return 0;
danielk19777c6303c2004-11-17 16:41:29 +0000406 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
drh4e5ffc52004-08-31 00:52:37 +0000407 sqliteNextChar(zString);
408 }
409 return 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000410 }
danielk19777c6303c2004-11-17 16:41:29 +0000411 }else if( !prevEscape && c==matchOne ){
drh4e5ffc52004-08-31 00:52:37 +0000412 if( *zString==0 ) return 0;
413 sqliteNextChar(zString);
414 zPattern++;
415 }else if( c==matchSet ){
416 int prior_c = 0;
danielk19777c6303c2004-11-17 16:41:29 +0000417 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
drh4e5ffc52004-08-31 00:52:37 +0000418 seen = 0;
419 invert = 0;
420 c = sqliteCharVal(zString);
421 if( c==0 ) return 0;
422 c2 = *++zPattern;
423 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
424 if( c2==']' ){
425 if( c==']' ) seen = 1;
426 c2 = *++zPattern;
427 }
428 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
429 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
430 zPattern++;
431 c2 = sqliteCharVal(zPattern);
432 if( c>=prior_c && c<=c2 ) seen = 1;
433 prior_c = 0;
434 }else if( c==c2 ){
435 seen = 1;
436 prior_c = c2;
437 }else{
438 prior_c = c2;
439 }
440 sqliteNextChar(zPattern);
441 }
442 if( c2==0 || (seen ^ invert)==0 ) return 0;
443 sqliteNextChar(zString);
444 zPattern++;
drh20fc0882004-11-18 13:49:25 +0000445 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
danielk19777c6303c2004-11-17 16:41:29 +0000446 prevEscape = 1;
447 sqliteNextChar(zPattern);
drh4e5ffc52004-08-31 00:52:37 +0000448 }else{
449 if( noCase ){
450 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
451 }else{
452 if( c != *zString ) return 0;
453 }
454 zPattern++;
455 zString++;
danielk19777c6303c2004-11-17 16:41:29 +0000456 prevEscape = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000457 }
danielk197751ad0ec2004-05-24 12:39:02 +0000458 }
drh4e5ffc52004-08-31 00:52:37 +0000459 return *zString==0;
drh0ac65892002-04-20 14:24:41 +0000460}
drh4e5ffc52004-08-31 00:52:37 +0000461
danielk19773f6b0872004-06-17 05:36:44 +0000462
463/*
464** Implementation of the like() SQL function. This function implements
465** the build-in LIKE operator. The first argument to the function is the
466** pattern and the second argument is the string. So, the SQL statements:
467**
468** A LIKE B
469**
470** is implemented as like(B,A).
471**
472** If the pointer retrieved by via a call to sqlite3_user_data() is
473** not NULL, then this function uses UTF-16. Otherwise UTF-8.
474*/
475static void likeFunc(
476 sqlite3_context *context,
477 int argc,
478 sqlite3_value **argv
479){
480 const unsigned char *zA = sqlite3_value_text(argv[0]);
481 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk19777c6303c2004-11-17 16:41:29 +0000482 int escape = 0;
483 if( argc==3 ){
484 /* The escape character string must consist of a single UTF-8 character.
485 ** Otherwise, return an error.
486 */
487 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
488 if( sqlite3utf8CharLen(zEsc, -1)!=1 ){
489 sqlite3_result_error(context,
490 "ESCAPE expression must be a single character", -1);
491 return;
492 }
493 escape = sqlite3ReadUtf8(zEsc);
494 }
danielk19773f6b0872004-06-17 05:36:44 +0000495 if( zA && zB ){
danielk19777c6303c2004-11-17 16:41:29 +0000496 sqlite3_result_int(context, patternCompare(zA, zB, &likeInfo, escape));
danielk19773f6b0872004-06-17 05:36:44 +0000497 }
498}
drh0ac65892002-04-20 14:24:41 +0000499
500/*
501** Implementation of the glob() SQL function. This function implements
502** the build-in GLOB operator. The first argument to the function is the
503** string and the second argument is the pattern. So, the SQL statements:
504**
505** A GLOB B
506**
danielk19777c6303c2004-11-17 16:41:29 +0000507** is implemented as glob(B,A).
drh0ac65892002-04-20 14:24:41 +0000508*/
danielk19770ae8b832004-05-25 12:05:56 +0000509static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
drh4f26d6c2004-05-26 23:25:30 +0000510 const unsigned char *zA = sqlite3_value_text(argv[0]);
511 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000512 if( zA && zB ){
danielk19777c6303c2004-11-17 16:41:29 +0000513 sqlite3_result_int(context, patternCompare(zA, zB, &globInfo, 0));
danielk197751ad0ec2004-05-24 12:39:02 +0000514 }
drh8912d102002-05-26 21:34:58 +0000515}
516
517/*
518** Implementation of the NULLIF(x,y) function. The result is the first
519** argument if the arguments are different. The result is NULL if the
520** arguments are equal to each other.
521*/
drhf9b596e2004-05-26 16:54:42 +0000522static void nullifFunc(
523 sqlite3_context *context,
524 int argc,
525 sqlite3_value **argv
526){
danielk1977dc1bdc42004-06-11 10:51:27 +0000527 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
528 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000529 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000530 }
drh0ac65892002-04-20 14:24:41 +0000531}
532
drh647cb0e2002-11-04 19:32:25 +0000533/*
534** Implementation of the VERSION(*) function. The result is the version
535** of the SQLite library that is running.
536*/
drhf9b596e2004-05-26 16:54:42 +0000537static void versionFunc(
538 sqlite3_context *context,
539 int argc,
540 sqlite3_value **argv
541){
danielk1977d8123362004-06-12 09:25:12 +0000542 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:25 +0000543}
544
danielk1977d641d642004-11-18 15:44:29 +0000545
drh47394702003-08-20 01:03:33 +0000546/*
547** EXPERIMENTAL - This is not an official function. The interface may
548** change. This function may disappear. Do not write code that depends
549** on this function.
550**
551** Implementation of the QUOTE() function. This function takes a single
552** argument. If the argument is numeric, the return value is the same as
553** the argument. If the argument is NULL, the return value is the string
554** "NULL". Otherwise, the argument is enclosed in single quotes with
555** single-quote escapes.
556*/
danielk19770ae8b832004-05-25 12:05:56 +0000557static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000558 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000559 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000560 case SQLITE_NULL: {
danielk1977d8123362004-06-12 09:25:12 +0000561 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
drhf9b596e2004-05-26 16:54:42 +0000562 break;
drh47394702003-08-20 01:03:33 +0000563 }
drh9c054832004-05-31 18:51:57 +0000564 case SQLITE_INTEGER:
565 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000566 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000567 break;
568 }
danielk19773f41e972004-06-08 00:39:01 +0000569 case SQLITE_BLOB: {
570 static const char hexdigits[] = {
571 '0', '1', '2', '3', '4', '5', '6', '7',
572 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
573 };
574 char *zText = 0;
575 int nBlob = sqlite3_value_bytes(argv[0]);
576 char const *zBlob = sqlite3_value_blob(argv[0]);
577
578 zText = (char *)sqliteMalloc((2*nBlob)+4);
579 if( !zText ){
580 sqlite3_result_error(context, "out of memory", -1);
581 }else{
582 int i;
583 for(i=0; i<nBlob; i++){
584 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
585 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
586 }
587 zText[(nBlob*2)+2] = '\'';
588 zText[(nBlob*2)+3] = '\0';
589 zText[0] = 'X';
590 zText[1] = '\'';
danielk1977d8123362004-06-12 09:25:12 +0000591 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
danielk19773f41e972004-06-08 00:39:01 +0000592 sqliteFree(zText);
593 }
594 break;
595 }
drh9c054832004-05-31 18:51:57 +0000596 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000597 int i,j,n;
drh4f26d6c2004-05-26 23:25:30 +0000598 const char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000599 char *z;
600
601 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
602 z = sqliteMalloc( i+n+3 );
603 if( z==0 ) return;
604 z[0] = '\'';
605 for(i=0, j=1; zArg[i]; i++){
606 z[j++] = zArg[i];
607 if( zArg[i]=='\'' ){
608 z[j++] = '\'';
609 }
610 }
611 z[j++] = '\'';
612 z[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000613 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
drhf9b596e2004-05-26 16:54:42 +0000614 sqliteFree(z);
615 }
drh47394702003-08-20 01:03:33 +0000616 }
617}
618
drhd24cc422003-03-27 12:51:24 +0000619#ifdef SQLITE_SOUNDEX
620/*
621** Compute the soundex encoding of a word.
622*/
danielk19770ae8b832004-05-25 12:05:56 +0000623static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000624 char zResult[8];
drh4c755c02004-08-08 20:22:17 +0000625 const u8 *zIn;
drhd24cc422003-03-27 12:51:24 +0000626 int i, j;
627 static const unsigned char iCode[] = {
628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
629 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
630 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
631 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
632 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
633 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
634 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
635 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
636 };
637 assert( argc==1 );
drh4c755c02004-08-08 20:22:17 +0000638 zIn = (u8*)sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000639 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
640 if( zIn[i] ){
641 zResult[0] = toupper(zIn[i]);
642 for(j=1; j<4 && zIn[i]; i++){
643 int code = iCode[zIn[i]&0x7f];
644 if( code>0 ){
645 zResult[j++] = code + '0';
646 }
647 }
648 while( j<4 ){
649 zResult[j++] = '0';
650 }
651 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000652 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:24 +0000653 }else{
danielk1977d8123362004-06-12 09:25:12 +0000654 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:24 +0000655 }
656}
657#endif
658
drh193a6b42002-07-07 16:52:46 +0000659#ifdef SQLITE_TEST
660/*
661** This function generates a string of random characters. Used for
662** generating test data.
663*/
danielk19770ae8b832004-05-25 12:05:56 +0000664static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000665 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000666 "abcdefghijklmnopqrstuvwxyz"
667 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
668 "0123456789"
669 ".-!,:*^+=_|?/<> ";
670 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000671 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000672 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000673 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000674 if( iMin<0 ) iMin = 0;
675 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
676 }else{
677 iMin = 1;
678 }
679 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000680 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000681 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000682 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000683 }else{
684 iMax = 50;
685 }
686 n = iMin;
687 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000688 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000689 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000690 n += r%(iMax + 1 - iMin);
691 }
drh1dba7272004-01-16 13:58:18 +0000692 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000693 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000694 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000695 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000696 }
697 zBuf[n] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000698 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT);
699}
drh0e3d7472004-06-19 17:33:07 +0000700#endif /* SQLITE_TEST */
danielk1977d8123362004-06-12 09:25:12 +0000701
drh0e3d7472004-06-19 17:33:07 +0000702#ifdef SQLITE_TEST
danielk1977d8123362004-06-12 09:25:12 +0000703/*
704** The following two SQL functions are used to test returning a text
705** result with a destructor. Function 'test_destructor' takes one argument
706** and returns the same argument interpreted as TEXT. A destructor is
707** passed with the sqlite3_result_text() call.
708**
709** SQL function 'test_destructor_count' returns the number of outstanding
710** allocations made by 'test_destructor';
711**
712** WARNING: Not threadsafe.
713*/
714static int test_destructor_count_var = 0;
715static void destructor(void *p){
716 char *zVal = (char *)p;
717 assert(zVal);
718 zVal--;
719 sqliteFree(zVal);
720 test_destructor_count_var--;
721}
722static void test_destructor(
723 sqlite3_context *pCtx,
724 int nArg,
725 sqlite3_value **argv
726){
727 char *zVal;
danielk1977f4618892004-06-28 13:09:11 +0000728 int len;
drh9bb575f2004-09-06 17:24:11 +0000729 sqlite3 *db = sqlite3_user_data(pCtx);
danielk1977f4618892004-06-28 13:09:11 +0000730
danielk1977d8123362004-06-12 09:25:12 +0000731 test_destructor_count_var++;
732 assert( nArg==1 );
733 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
danielk1977f4618892004-06-28 13:09:11 +0000734 len = sqlite3ValueBytes(argv[0], db->enc);
735 zVal = sqliteMalloc(len+3);
736 zVal[len] = 0;
737 zVal[len-1] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000738 assert( zVal );
739 zVal++;
danielk1977f4618892004-06-28 13:09:11 +0000740 memcpy(zVal, sqlite3ValueText(argv[0], db->enc), len);
741 if( db->enc==SQLITE_UTF8 ){
742 sqlite3_result_text(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000743#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +0000744 }else if( db->enc==SQLITE_UTF16LE ){
745 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
746 }else{
747 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000748#endif /* SQLITE_OMIT_UTF16 */
danielk1977f4618892004-06-28 13:09:11 +0000749 }
danielk1977d8123362004-06-12 09:25:12 +0000750}
751static void test_destructor_count(
752 sqlite3_context *pCtx,
753 int nArg,
754 sqlite3_value **argv
755){
756 sqlite3_result_int(pCtx, test_destructor_count_var);
drh193a6b42002-07-07 16:52:46 +0000757}
drh0e3d7472004-06-19 17:33:07 +0000758#endif /* SQLITE_TEST */
danielk19773f6b0872004-06-17 05:36:44 +0000759
drh0e3d7472004-06-19 17:33:07 +0000760#ifdef SQLITE_TEST
761/*
762** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
763** interface.
764**
765** The test_auxdata() SQL function attempts to register each of its arguments
766** as auxiliary data. If there are no prior registrations of aux data for
767** that argument (meaning the argument is not a constant or this is its first
768** call) then the result for that argument is 0. If there is a prior
769** registration, the result for that argument is 1. The overall result
770** is the individual argument results separated by spaces.
771*/
danielk19773f6b0872004-06-17 05:36:44 +0000772static void free_test_auxdata(void *p) {sqliteFree(p);}
773static void test_auxdata(
774 sqlite3_context *pCtx,
775 int nArg,
776 sqlite3_value **argv
777){
778 int i;
779 char *zRet = sqliteMalloc(nArg*2);
780 if( !zRet ) return;
781 for(i=0; i<nArg; i++){
782 char const *z = sqlite3_value_text(argv[i]);
783 if( z ){
784 char *zAux = sqlite3_get_auxdata(pCtx, i);
785 if( zAux ){
786 zRet[i*2] = '1';
787 if( strcmp(zAux, z) ){
788 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
789 return;
790 }
791 }else{
792 zRet[i*2] = '0';
793 zAux = sqliteStrDup(z);
794 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
795 }
796 zRet[i*2+1] = ' ';
797 }
798 }
799 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
800}
drh0e3d7472004-06-19 17:33:07 +0000801#endif /* SQLITE_TEST */
drh193a6b42002-07-07 16:52:46 +0000802
danielk197701427a62005-01-11 13:02:33 +0000803#ifdef SQLITE_TEST
804/*
805** A function to test error reporting from user functions. This function
806** returns a copy of it's first argument as an error.
807*/
808static void test_error(
809 sqlite3_context *pCtx,
810 int nArg,
811 sqlite3_value **argv
812){
danielk197724c8ab82005-02-09 01:40:23 +0000813 sqlite3_result_error(pCtx, sqlite3_value_text(argv[0]), 0);
danielk197701427a62005-01-11 13:02:33 +0000814}
815#endif /* SQLITE_TEST */
816
drh0ac65892002-04-20 14:24:41 +0000817/*
drhd3a149e2002-02-24 17:12:53 +0000818** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000819** sum() or avg() aggregate computation.
820*/
821typedef struct SumCtx SumCtx;
822struct SumCtx {
823 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000824 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000825};
826
827/*
828** Routines used to compute the sum or average.
829*/
danielk19770ae8b832004-05-25 12:05:56 +0000830static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000831 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000832 if( argc<1 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000833 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000834 if( p && SQLITE_NULL!=sqlite3_value_type(argv[0]) ){
drh4f26d6c2004-05-26 23:25:30 +0000835 p->sum += sqlite3_value_double(argv[0]);
drh739105c2002-05-29 23:22:23 +0000836 p->cnt++;
837 }
drhdd5baa92002-02-27 19:50:59 +0000838}
danielk19770ae8b832004-05-25 12:05:56 +0000839static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000840 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000841 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000842 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000843}
danielk19770ae8b832004-05-25 12:05:56 +0000844static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000845 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000846 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000847 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000848 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000849 }
850}
851
852/*
853** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000854** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000855*/
856typedef struct StdDevCtx StdDevCtx;
857struct StdDevCtx {
858 double sum; /* Sum of terms */
859 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000860 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000861};
862
drh0bce8352002-02-28 00:41:10 +0000863/*
864** The following structure keeps track of state information for the
865** count() aggregate function.
866*/
867typedef struct CountCtx CountCtx;
868struct CountCtx {
869 int n;
870};
drhdd5baa92002-02-27 19:50:59 +0000871
drh0bce8352002-02-28 00:41:10 +0000872/*
873** Routines to implement the count() aggregate function.
874*/
danielk19770ae8b832004-05-25 12:05:56 +0000875static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000876 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000877 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000878 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000879 p->n++;
880 }
881}
danielk19770ae8b832004-05-25 12:05:56 +0000882static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000883 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000884 p = sqlite3_aggregate_context(context, sizeof(*p));
drhf4479502004-05-27 03:12:53 +0000885 sqlite3_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000886}
887
888/*
889** This function tracks state information for the min() and max()
890** aggregate functions.
891*/
892typedef struct MinMaxCtx MinMaxCtx;
893struct MinMaxCtx {
894 char *z; /* The best so far */
895 char zBuf[28]; /* Space that can be used for storage */
896};
897
898/*
899** Routines to implement min() and max() aggregate functions.
900*/
danielk19770ae8b832004-05-25 12:05:56 +0000901static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000902 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:32 +0000903 Mem *pBest;
904
905 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
906 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:04 +0000907 if( !pBest ) return;
drh268380c2004-02-25 13:47:31 +0000908
danielk197788208052004-05-25 01:13:20 +0000909 if( pBest->flags ){
drh9eb516c2004-07-18 20:52:32 +0000910 int max;
911 int cmp;
danielk1977dc1bdc42004-06-11 10:51:27 +0000912 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +0000913 /* This step function is used for both the min() and max() aggregates,
914 ** the only difference between the two being that the sense of the
915 ** comparison is inverted. For the max() aggregate, the
916 ** sqlite3_user_data() function returns (void *)-1. For min() it
917 ** returns (void *)db, where db is the sqlite3* database pointer.
918 ** Therefore the next statement sets variable 'max' to 1 for the max()
919 ** aggregate, or 0 for min().
920 */
danielk197788208052004-05-25 01:13:20 +0000921 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +0000922 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +0000923 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000924 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000925 }
drh268380c2004-02-25 13:47:31 +0000926 }else{
danielk19777e18c252004-05-25 11:47:24 +0000927 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000928 }
929}
danielk19770ae8b832004-05-25 12:05:56 +0000930static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000931 sqlite3_value *pRes;
drh4f26d6c2004-05-26 23:25:30 +0000932 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +0000933 if( pRes->flags ){
drhf4479502004-05-27 03:12:53 +0000934 sqlite3_result_value(context, pRes);
drh0bce8352002-02-28 00:41:10 +0000935 }
danielk1977b20e56b2004-06-15 13:36:30 +0000936 sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:10 +0000937}
drhdd5baa92002-02-27 19:50:59 +0000938
drh4e5ffc52004-08-31 00:52:37 +0000939
drhd3a149e2002-02-24 17:12:53 +0000940/*
drha2ed5602002-02-26 23:55:31 +0000941** This function registered all of the above C functions as SQL
942** functions. This should be the only routine in this file with
943** external linkage.
drhdc04c582002-02-24 01:55:15 +0000944*/
drh9bb575f2004-09-06 17:24:11 +0000945void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
drh57196282004-10-06 15:41:16 +0000946 static const struct {
drh0bce8352002-02-28 00:41:10 +0000947 char *zName;
drh268380c2004-02-25 13:47:31 +0000948 signed char nArg;
danielk1977f4618892004-06-28 13:09:11 +0000949 u8 argType; /* 0: none. 1: db 2: (-1) */
950 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +0000951 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000952 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000953 } aFuncs[] = {
danielk1977f4618892004-06-28 13:09:11 +0000954 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
955 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
956 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc },
957 { "max", 0, 2, SQLITE_UTF8, 1, 0 },
958 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
959 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
960 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
drh6c626082004-11-14 21:56:29 +0000961#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +0000962 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
drh6c626082004-11-14 21:56:29 +0000963#endif
danielk1977f4618892004-06-28 13:09:11 +0000964 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
965 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
966 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
967 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
968 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
969 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
970 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
971 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
972 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
973 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
974 { "like", 2, 0, SQLITE_UTF8, 0, likeFunc },
danielk19777c6303c2004-11-17 16:41:29 +0000975 { "like", 3, 0, SQLITE_UTF8, 0, likeFunc },
danielk1977f4618892004-06-28 13:09:11 +0000976 { "glob", 2, 0, SQLITE_UTF8, 0, globFunc },
drh94a98362004-09-13 13:13:18 +0000977 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
danielk1977f4618892004-06-28 13:09:11 +0000978 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
979 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
980 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid },
981 { "changes", 0, 1, SQLITE_UTF8, 0, changes },
982 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes },
drhd24cc422003-03-27 12:51:24 +0000983#ifdef SQLITE_SOUNDEX
danielk1977f4618892004-06-28 13:09:11 +0000984 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000985#endif
drh193a6b42002-07-07 16:52:46 +0000986#ifdef SQLITE_TEST
danielk1977f4618892004-06-28 13:09:11 +0000987 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
988 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor},
danielk1977d8123362004-06-12 09:25:12 +0000989 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
danielk1977f4618892004-06-28 13:09:11 +0000990 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
danielk197701427a62005-01-11 13:02:33 +0000991 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
drh193a6b42002-07-07 16:52:46 +0000992#endif
drh0bce8352002-02-28 00:41:10 +0000993 };
drh57196282004-10-06 15:41:16 +0000994 static const struct {
drh0bce8352002-02-28 00:41:10 +0000995 char *zName;
drh268380c2004-02-25 13:47:31 +0000996 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000997 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +0000998 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000999 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1000 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +00001001 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +00001002 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1003 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
1004 { "sum", 1, 0, 0, sumStep, sumFinalize },
1005 { "avg", 1, 0, 0, sumStep, avgFinalize },
1006 { "count", 0, 0, 0, countStep, countFinalize },
1007 { "count", 1, 0, 0, countStep, countFinalize },
drh0bce8352002-02-28 00:41:10 +00001008 };
1009 int i;
1010
1011 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001012 void *pArg = 0;
1013 switch( aFuncs[i].argType ){
1014 case 1: pArg = db; break;
1015 case 2: pArg = (void *)(-1); break;
1016 }
danielk1977ad7dd422004-06-06 12:41:49 +00001017 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
danielk1977f9d64d22004-06-19 08:18:07 +00001018 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001019 if( aFuncs[i].needCollSeq ){
1020 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1021 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1022 if( pFunc && aFuncs[i].needCollSeq ){
1023 pFunc->needCollSeq = 1;
1024 }
1025 }
drh0bce8352002-02-28 00:41:10 +00001026 }
drh1f01ec12005-02-15 21:36:18 +00001027#ifndef SQLITE_OMIT_ALTERTABLE
1028 sqlite3AlterFunctions(db);
1029#endif
drh0bce8352002-02-28 00:41:10 +00001030 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001031 void *pArg = 0;
1032 switch( aAggs[i].argType ){
1033 case 1: pArg = db; break;
1034 case 2: pArg = (void *)(-1); break;
1035 }
danielk1977d8123362004-06-12 09:25:12 +00001036 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001037 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +00001038 if( aAggs[i].needCollSeq ){
1039 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
danielk1977d8123362004-06-12 09:25:12 +00001040 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001041 if( pFunc && aAggs[i].needCollSeq ){
1042 pFunc->needCollSeq = 1;
1043 }
1044 }
drh268380c2004-02-25 13:47:31 +00001045 }
danielk19774adee202004-05-08 08:23:19 +00001046 sqlite3RegisterDateTimeFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001047#ifdef SQLITE_SSE
1048 {
danielk1977fd9e1f32005-05-22 10:44:34 +00001049 sqlite3SseFunctions(db);
1050 }
1051#endif
drhdc04c582002-02-24 01:55:15 +00001052}