blob: be3fc943e175574376dedb8650661f197ce6fb9e [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**
drh52fc8492006-02-23 21:43:55 +000019** $Id: func.c,v 1.123 2006/02/23 21:43:56 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>
drhb37df7b2005-10-13 02:09:49 +000023/* #include <math.h> */
drhd3a149e2002-02-24 17:12:53 +000024#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
drh55ef4d92005-08-14 01:20:37 +000029/*
30** Return the collating function associated with a function.
31*/
danielk1977dc1bdc42004-06-11 10:51:27 +000032static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
33 return context->pColl;
34}
35
drh0bce8352002-02-28 00:41:10 +000036/*
37** Implementation of the non-aggregate min() and max() functions
38*/
drhf9b596e2004-05-26 16:54:42 +000039static void minmaxFunc(
40 sqlite3_context *context,
41 int argc,
42 sqlite3_value **argv
43){
drh0bce8352002-02-28 00:41:10 +000044 int i;
drh268380c2004-02-25 13:47:31 +000045 int mask; /* 0 for min() or 0xffffffff for max() */
drhf9b596e2004-05-26 16:54:42 +000046 int iBest;
danielk1977dc1bdc42004-06-11 10:51:27 +000047 CollSeq *pColl;
drh0bce8352002-02-28 00:41:10 +000048
drh89425d52002-02-28 03:04:48 +000049 if( argc==0 ) return;
drhc44af712004-09-02 15:53:56 +000050 mask = sqlite3_user_data(context)==0 ? 0 : -1;
danielk1977dc1bdc42004-06-11 10:51:27 +000051 pColl = sqlite3GetFuncCollSeq(context);
52 assert( pColl );
danielk1977c572ef72004-05-27 09:28:41 +000053 assert( mask==-1 || mask==0 );
drhf9b596e2004-05-26 16:54:42 +000054 iBest = 0;
drh9c054832004-05-31 18:51:57 +000055 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
drhf9b596e2004-05-26 16:54:42 +000056 for(i=1; i<argc; i++){
drh9c054832004-05-31 18:51:57 +000057 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
danielk1977dc1bdc42004-06-11 10:51:27 +000058 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
drhf9b596e2004-05-26 16:54:42 +000059 iBest = i;
drh0bce8352002-02-28 00:41:10 +000060 }
61 }
drhf4479502004-05-27 03:12:53 +000062 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:10 +000063}
drh0bce8352002-02-28 00:41:10 +000064
drh268380c2004-02-25 13:47:31 +000065/*
66** Return the type of the argument.
67*/
drhf9b596e2004-05-26 16:54:42 +000068static void typeofFunc(
69 sqlite3_context *context,
70 int argc,
71 sqlite3_value **argv
72){
danielk197735bb9d02004-05-24 12:55:54 +000073 const char *z = 0;
danielk197735bb9d02004-05-24 12:55:54 +000074 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000075 case SQLITE_NULL: z = "null"; break;
76 case SQLITE_INTEGER: z = "integer"; break;
77 case SQLITE_TEXT: z = "text"; break;
78 case SQLITE_FLOAT: z = "real"; break;
79 case SQLITE_BLOB: z = "blob"; break;
danielk197735bb9d02004-05-24 12:55:54 +000080 }
danielk1977d8123362004-06-12 09:25:12 +000081 sqlite3_result_text(context, z, -1, SQLITE_STATIC);
drh0bce8352002-02-28 00:41:10 +000082}
83
drh5708d2d2005-06-22 10:53:59 +000084
85/*
drh0bce8352002-02-28 00:41:10 +000086** Implementation of the length() function
87*/
drhf9b596e2004-05-26 16:54:42 +000088static void lengthFunc(
89 sqlite3_context *context,
90 int argc,
91 sqlite3_value **argv
92){
drh0bce8352002-02-28 00:41:10 +000093 int len;
94
95 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000096 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000097 case SQLITE_BLOB:
98 case SQLITE_INTEGER:
99 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000100 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +0000101 break;
102 }
drh9c054832004-05-31 18:51:57 +0000103 case SQLITE_TEXT: {
drh2646da72005-12-09 20:02:05 +0000104 const unsigned char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000105 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +0000106 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +0000107 break;
108 }
109 default: {
110 sqlite3_result_null(context);
111 break;
112 }
113 }
drh0bce8352002-02-28 00:41:10 +0000114}
115
116/*
117** Implementation of the abs() function
118*/
danielk19770ae8b832004-05-25 12:05:56 +0000119static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000120 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000121 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000122 case SQLITE_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000123 i64 iVal = sqlite3_value_int64(argv[0]);
drh52fc8492006-02-23 21:43:55 +0000124 if( iVal<0 ){
125 if( (iVal<<1)==0 ){
126 sqlite3_result_error(context, "integer overflow", -1);
127 return;
128 }
129 iVal = -iVal;
130 }
danielk1977f93bbbe2004-05-27 10:30:52 +0000131 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000132 break;
133 }
drh9c054832004-05-31 18:51:57 +0000134 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000135 sqlite3_result_null(context);
136 break;
137 }
138 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000139 double rVal = sqlite3_value_double(argv[0]);
drh52fc8492006-02-23 21:43:55 +0000140 if( rVal<0 ) rVal = -rVal;
danielk1977f93bbbe2004-05-27 10:30:52 +0000141 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000142 break;
143 }
144 }
drh0bce8352002-02-28 00:41:10 +0000145}
146
147/*
148** Implementation of the substr() function
149*/
drhf9b596e2004-05-26 16:54:42 +0000150static void substrFunc(
151 sqlite3_context *context,
152 int argc,
153 sqlite3_value **argv
154){
drh2646da72005-12-09 20:02:05 +0000155 const unsigned char *z;
156 const unsigned char *z2;
drh0bce8352002-02-28 00:41:10 +0000157 int i;
drh0bce8352002-02-28 00:41:10 +0000158 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000159
drh0bce8352002-02-28 00:41:10 +0000160 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000161 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000162 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000163 p1 = sqlite3_value_int(argv[1]);
164 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000165 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000166 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000167 p1 += len;
drh653bc752002-02-28 03:31:10 +0000168 if( p1<0 ){
169 p2 += p1;
170 p1 = 0;
171 }
drh0bce8352002-02-28 00:41:10 +0000172 }else if( p1>0 ){
173 p1--;
174 }
175 if( p1+p2>len ){
176 p2 = len-p1;
177 }
drh77396302004-01-02 13:17:48 +0000178 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000179 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000180 }
drh47c8a672002-02-28 04:00:12 +0000181 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000182 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000183 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000184 }
drh47c8a672002-02-28 04:00:12 +0000185 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000186 if( p2<0 ) p2 = 0;
drh2646da72005-12-09 20:02:05 +0000187 sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000188}
189
190/*
191** Implementation of the round() function
192*/
danielk19770ae8b832004-05-25 12:05:56 +0000193static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000194 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000195 double r;
drh592ac8c2005-08-13 03:07:47 +0000196 char zBuf[500]; /* larger than the %f representation of the largest double */
drh0bce8352002-02-28 00:41:10 +0000197 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000198 if( argc==2 ){
drh9c054832004-05-31 18:51:57 +0000199 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000200 n = sqlite3_value_int(argv[1]);
201 if( n>30 ) n = 30;
202 if( n<0 ) n = 0;
203 }
drh9c054832004-05-31 18:51:57 +0000204 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000205 r = sqlite3_value_double(argv[0]);
drhe866fcb2005-07-09 02:38:06 +0000206 sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
danielk1977d8123362004-06-12 09:25:12 +0000207 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000208}
drhdc04c582002-02-24 01:55:15 +0000209
210/*
211** Implementation of the upper() and lower() SQL functions.
212*/
danielk19770ae8b832004-05-25 12:05:56 +0000213static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000214 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000215 int i;
drh9c054832004-05-31 18:51:57 +0000216 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000217 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000218 if( z==0 ) return;
drh2646da72005-12-09 20:02:05 +0000219 strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000220 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000221 z[i] = toupper(z[i]);
drhdc04c582002-02-24 01:55:15 +0000222 }
drh2646da72005-12-09 20:02:05 +0000223 sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000224 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000225}
danielk19770ae8b832004-05-25 12:05:56 +0000226static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000227 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000228 int i;
drh9c054832004-05-31 18:51:57 +0000229 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000230 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000231 if( z==0 ) return;
drh2646da72005-12-09 20:02:05 +0000232 strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000233 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000234 z[i] = tolower(z[i]);
drhdc04c582002-02-24 01:55:15 +0000235 }
drh2646da72005-12-09 20:02:05 +0000236 sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000237 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000238}
239
240/*
drhfbc99082002-02-28 03:14:18 +0000241** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000242** All three do the same thing. They return the first non-NULL
243** argument.
drh3212e182002-02-28 00:46:26 +0000244*/
drhf9b596e2004-05-26 16:54:42 +0000245static void ifnullFunc(
246 sqlite3_context *context,
247 int argc,
248 sqlite3_value **argv
249){
drhfbc99082002-02-28 03:14:18 +0000250 int i;
251 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000252 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000253 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000254 break;
255 }
256 }
drh3212e182002-02-28 00:46:26 +0000257}
258
259/*
drhf9ffac92002-03-02 19:00:31 +0000260** Implementation of random(). Return a random integer.
261*/
drhf9b596e2004-05-26 16:54:42 +0000262static void randomFunc(
263 sqlite3_context *context,
264 int argc,
265 sqlite3_value **argv
266){
drh52fc8492006-02-23 21:43:55 +0000267 sqlite_int64 r;
danielk19774adee202004-05-08 08:23:19 +0000268 sqlite3Randomness(sizeof(r), &r);
drh52fc8492006-02-23 21:43:55 +0000269 sqlite3_result_int64(context, r);
drhf9ffac92002-03-02 19:00:31 +0000270}
271
272/*
drh6ed41ad2002-04-06 14:10:47 +0000273** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000274** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000275*/
danielk197751ad0ec2004-05-24 12:39:02 +0000276static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000277 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000278 int arg,
279 sqlite3_value **argv
280){
drh9bb575f2004-09-06 17:24:11 +0000281 sqlite3 *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000282 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000283}
284
rdcf146a772004-02-25 22:51:06 +0000285/*
danielk1977b28af712004-06-21 06:50:26 +0000286** Implementation of the changes() SQL function. The return value is the
287** same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000288*/
danielk1977b28af712004-06-21 06:50:26 +0000289static void changes(
drhf9b596e2004-05-26 16:54:42 +0000290 sqlite3_context *context,
291 int arg,
292 sqlite3_value **argv
293){
drh9bb575f2004-09-06 17:24:11 +0000294 sqlite3 *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000295 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000296}
rdcf146a772004-02-25 22:51:06 +0000297
298/*
danielk1977b28af712004-06-21 06:50:26 +0000299** Implementation of the total_changes() SQL function. The return value is
300** the same as the sqlite3_total_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000301*/
danielk1977b28af712004-06-21 06:50:26 +0000302static void total_changes(
303 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000304 int arg,
305 sqlite3_value **argv
306){
drh9bb575f2004-09-06 17:24:11 +0000307 sqlite3 *db = sqlite3_user_data(context);
danielk1977b28af712004-06-21 06:50:26 +0000308 sqlite3_result_int(context, sqlite3_total_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000309}
310
drh6ed41ad2002-04-06 14:10:47 +0000311/*
drh4e5ffc52004-08-31 00:52:37 +0000312** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03 +0000313*/
drh4e5ffc52004-08-31 00:52:37 +0000314struct compareInfo {
315 u8 matchAll;
316 u8 matchOne;
317 u8 matchSet;
318 u8 noCase;
danielk1977d02eb1f2004-06-06 09:44:03 +0000319};
drh55ef4d92005-08-14 01:20:37 +0000320
drh4e5ffc52004-08-31 00:52:37 +0000321static const struct compareInfo globInfo = { '*', '?', '[', 0 };
drh55ef4d92005-08-14 01:20:37 +0000322/* The correct SQL-92 behavior is for the LIKE operator to ignore
323** case. Thus 'a' LIKE 'A' would be true. */
324static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
325/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
326** is case sensitive causing 'a' LIKE 'A' to be false */
327static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
danielk1977d02eb1f2004-06-06 09:44:03 +0000328
329/*
drh4e5ffc52004-08-31 00:52:37 +0000330** X is a pointer to the first byte of a UTF-8 character. Increment
331** X so that it points to the next character. This only works right
332** if X points to a well-formed UTF-8 string.
danielk1977d02eb1f2004-06-06 09:44:03 +0000333*/
drh4e5ffc52004-08-31 00:52:37 +0000334#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
335#define sqliteCharVal(X) sqlite3ReadUtf8(X)
danielk1977d02eb1f2004-06-06 09:44:03 +0000336
danielk1977d02eb1f2004-06-06 09:44:03 +0000337
338/*
drh4e5ffc52004-08-31 00:52:37 +0000339** Compare two UTF-8 strings for equality where the first string can
340** potentially be a "glob" expression. Return true (1) if they
341** are the same and false (0) if they are different.
drh0ac65892002-04-20 14:24:41 +0000342**
drh4e5ffc52004-08-31 00:52:37 +0000343** Globbing rules:
drh0ac65892002-04-20 14:24:41 +0000344**
drh4e5ffc52004-08-31 00:52:37 +0000345** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03 +0000346**
drh4e5ffc52004-08-31 00:52:37 +0000347** '?' Matches exactly one character.
348**
349** [...] Matches one character from the enclosed list of
350** characters.
351**
352** [^...] Matches one character not in the enclosed list.
353**
354** With the [...] and [^...] matching, a ']' character can be included
355** in the list by making it the first character after '[' or '^'. A
356** range of characters can be specified using '-'. Example:
357** "[a-z]" matches any single lower-case letter. To match a '-', make
358** it the last character in the list.
359**
360** This routine is usually quick, but can be N**2 in the worst case.
361**
362** Hints: to match '*' or '?', put them in "[]". Like this:
363**
364** abc[*]xyz Matches "abc*xyz" only
drh0ac65892002-04-20 14:24:41 +0000365*/
danielk19777c6303c2004-11-17 16:41:29 +0000366static int patternCompare(
drh4e5ffc52004-08-31 00:52:37 +0000367 const u8 *zPattern, /* The glob pattern */
368 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29 +0000369 const struct compareInfo *pInfo, /* Information about how to do the compare */
370 const int esc /* The escape character */
danielk197751ad0ec2004-05-24 12:39:02 +0000371){
danielk1977ad7dd422004-06-06 12:41:49 +0000372 register int c;
drh4e5ffc52004-08-31 00:52:37 +0000373 int invert;
374 int seen;
375 int c2;
376 u8 matchOne = pInfo->matchOne;
377 u8 matchAll = pInfo->matchAll;
378 u8 matchSet = pInfo->matchSet;
379 u8 noCase = pInfo->noCase;
danielk19777c6303c2004-11-17 16:41:29 +0000380 int prevEscape = 0; /* True if the previous character was 'escape' */
danielk1977d02eb1f2004-06-06 09:44:03 +0000381
drh4e5ffc52004-08-31 00:52:37 +0000382 while( (c = *zPattern)!=0 ){
danielk19777c6303c2004-11-17 16:41:29 +0000383 if( !prevEscape && c==matchAll ){
drh4e5ffc52004-08-31 00:52:37 +0000384 while( (c=zPattern[1]) == matchAll || c == matchOne ){
385 if( c==matchOne ){
386 if( *zString==0 ) return 0;
387 sqliteNextChar(zString);
388 }
389 zPattern++;
danielk1977ad7dd422004-06-06 12:41:49 +0000390 }
drh20fc0882004-11-18 13:49:25 +0000391 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
danielk19777c6303c2004-11-17 16:41:29 +0000392 u8 const *zTemp = &zPattern[1];
393 sqliteNextChar(zTemp);
394 c = *zTemp;
395 }
drh4e5ffc52004-08-31 00:52:37 +0000396 if( c==0 ) return 1;
397 if( c==matchSet ){
danielk19777c6303c2004-11-17 16:41:29 +0000398 assert( esc==0 ); /* This is GLOB, not LIKE */
399 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
drh4e5ffc52004-08-31 00:52:37 +0000400 sqliteNextChar(zString);
401 }
402 return *zString!=0;
403 }else{
404 while( (c2 = *zString)!=0 ){
405 if( noCase ){
406 c2 = sqlite3UpperToLower[c2];
407 c = sqlite3UpperToLower[c];
408 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
409 }else{
410 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
411 }
412 if( c2==0 ) return 0;
danielk19777c6303c2004-11-17 16:41:29 +0000413 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
drh4e5ffc52004-08-31 00:52:37 +0000414 sqliteNextChar(zString);
415 }
416 return 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000417 }
danielk19777c6303c2004-11-17 16:41:29 +0000418 }else if( !prevEscape && c==matchOne ){
drh4e5ffc52004-08-31 00:52:37 +0000419 if( *zString==0 ) return 0;
420 sqliteNextChar(zString);
421 zPattern++;
422 }else if( c==matchSet ){
423 int prior_c = 0;
danielk19777c6303c2004-11-17 16:41:29 +0000424 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
drh4e5ffc52004-08-31 00:52:37 +0000425 seen = 0;
426 invert = 0;
427 c = sqliteCharVal(zString);
428 if( c==0 ) return 0;
429 c2 = *++zPattern;
430 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
431 if( c2==']' ){
432 if( c==']' ) seen = 1;
433 c2 = *++zPattern;
434 }
435 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
436 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
437 zPattern++;
438 c2 = sqliteCharVal(zPattern);
439 if( c>=prior_c && c<=c2 ) seen = 1;
440 prior_c = 0;
441 }else if( c==c2 ){
442 seen = 1;
443 prior_c = c2;
444 }else{
445 prior_c = c2;
446 }
447 sqliteNextChar(zPattern);
448 }
449 if( c2==0 || (seen ^ invert)==0 ) return 0;
450 sqliteNextChar(zString);
451 zPattern++;
drh20fc0882004-11-18 13:49:25 +0000452 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
danielk19777c6303c2004-11-17 16:41:29 +0000453 prevEscape = 1;
454 sqliteNextChar(zPattern);
drh4e5ffc52004-08-31 00:52:37 +0000455 }else{
456 if( noCase ){
457 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
458 }else{
459 if( c != *zString ) return 0;
460 }
461 zPattern++;
462 zString++;
danielk19777c6303c2004-11-17 16:41:29 +0000463 prevEscape = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000464 }
danielk197751ad0ec2004-05-24 12:39:02 +0000465 }
drh4e5ffc52004-08-31 00:52:37 +0000466 return *zString==0;
drh0ac65892002-04-20 14:24:41 +0000467}
drh4e5ffc52004-08-31 00:52:37 +0000468
drh55ef4d92005-08-14 01:20:37 +0000469/*
470** Count the number of times that the LIKE operator (or GLOB which is
471** just a variation of LIKE) gets called. This is used for testing
472** only.
473*/
474#ifdef SQLITE_TEST
475int sqlite3_like_count = 0;
476#endif
477
danielk19773f6b0872004-06-17 05:36:44 +0000478
479/*
480** Implementation of the like() SQL function. This function implements
481** the build-in LIKE operator. The first argument to the function is the
482** pattern and the second argument is the string. So, the SQL statements:
483**
484** A LIKE B
485**
486** is implemented as like(B,A).
487**
drh55ef4d92005-08-14 01:20:37 +0000488** This same function (with a different compareInfo structure) computes
489** the GLOB operator.
danielk19773f6b0872004-06-17 05:36:44 +0000490*/
491static void likeFunc(
492 sqlite3_context *context,
493 int argc,
494 sqlite3_value **argv
495){
496 const unsigned char *zA = sqlite3_value_text(argv[0]);
497 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk19777c6303c2004-11-17 16:41:29 +0000498 int escape = 0;
499 if( argc==3 ){
500 /* The escape character string must consist of a single UTF-8 character.
501 ** Otherwise, return an error.
502 */
503 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
drh2646da72005-12-09 20:02:05 +0000504 if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){
danielk19777c6303c2004-11-17 16:41:29 +0000505 sqlite3_result_error(context,
506 "ESCAPE expression must be a single character", -1);
507 return;
508 }
509 escape = sqlite3ReadUtf8(zEsc);
510 }
danielk19773f6b0872004-06-17 05:36:44 +0000511 if( zA && zB ){
drh55ef4d92005-08-14 01:20:37 +0000512 struct compareInfo *pInfo = sqlite3_user_data(context);
513#ifdef SQLITE_TEST
514 sqlite3_like_count++;
515#endif
516 sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape));
danielk197751ad0ec2004-05-24 12:39:02 +0000517 }
drh8912d102002-05-26 21:34:58 +0000518}
519
520/*
521** Implementation of the NULLIF(x,y) function. The result is the first
522** argument if the arguments are different. The result is NULL if the
523** arguments are equal to each other.
524*/
drhf9b596e2004-05-26 16:54:42 +0000525static void nullifFunc(
526 sqlite3_context *context,
527 int argc,
528 sqlite3_value **argv
529){
danielk1977dc1bdc42004-06-11 10:51:27 +0000530 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
531 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000532 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000533 }
drh0ac65892002-04-20 14:24:41 +0000534}
535
drh647cb0e2002-11-04 19:32:25 +0000536/*
537** Implementation of the VERSION(*) function. The result is the version
538** of the SQLite library that is running.
539*/
drhf9b596e2004-05-26 16:54:42 +0000540static void versionFunc(
541 sqlite3_context *context,
542 int argc,
543 sqlite3_value **argv
544){
danielk1977d8123362004-06-12 09:25:12 +0000545 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:25 +0000546}
547
danielk1977d641d642004-11-18 15:44:29 +0000548
drh47394702003-08-20 01:03:33 +0000549/*
550** EXPERIMENTAL - This is not an official function. The interface may
551** change. This function may disappear. Do not write code that depends
552** on this function.
553**
554** Implementation of the QUOTE() function. This function takes a single
555** argument. If the argument is numeric, the return value is the same as
556** the argument. If the argument is NULL, the return value is the string
557** "NULL". Otherwise, the argument is enclosed in single quotes with
558** single-quote escapes.
559*/
danielk19770ae8b832004-05-25 12:05:56 +0000560static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000561 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000562 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000563 case SQLITE_NULL: {
danielk1977d8123362004-06-12 09:25:12 +0000564 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
drhf9b596e2004-05-26 16:54:42 +0000565 break;
drh47394702003-08-20 01:03:33 +0000566 }
drh9c054832004-05-31 18:51:57 +0000567 case SQLITE_INTEGER:
568 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000569 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000570 break;
571 }
danielk19773f41e972004-06-08 00:39:01 +0000572 case SQLITE_BLOB: {
573 static const char hexdigits[] = {
574 '0', '1', '2', '3', '4', '5', '6', '7',
575 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
576 };
577 char *zText = 0;
578 int nBlob = sqlite3_value_bytes(argv[0]);
579 char const *zBlob = sqlite3_value_blob(argv[0]);
580
581 zText = (char *)sqliteMalloc((2*nBlob)+4);
582 if( !zText ){
583 sqlite3_result_error(context, "out of memory", -1);
584 }else{
585 int i;
586 for(i=0; i<nBlob; i++){
587 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
588 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
589 }
590 zText[(nBlob*2)+2] = '\'';
591 zText[(nBlob*2)+3] = '\0';
592 zText[0] = 'X';
593 zText[1] = '\'';
danielk1977d8123362004-06-12 09:25:12 +0000594 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
danielk19773f41e972004-06-08 00:39:01 +0000595 sqliteFree(zText);
596 }
597 break;
598 }
drh9c054832004-05-31 18:51:57 +0000599 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000600 int i,j,n;
drh2646da72005-12-09 20:02:05 +0000601 const unsigned char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000602 char *z;
603
604 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
605 z = sqliteMalloc( i+n+3 );
606 if( z==0 ) return;
607 z[0] = '\'';
608 for(i=0, j=1; zArg[i]; i++){
609 z[j++] = zArg[i];
610 if( zArg[i]=='\'' ){
611 z[j++] = '\'';
612 }
613 }
614 z[j++] = '\'';
615 z[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000616 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
drhf9b596e2004-05-26 16:54:42 +0000617 sqliteFree(z);
618 }
drh47394702003-08-20 01:03:33 +0000619 }
620}
621
drhd24cc422003-03-27 12:51:24 +0000622#ifdef SQLITE_SOUNDEX
623/*
624** Compute the soundex encoding of a word.
625*/
danielk19770ae8b832004-05-25 12:05:56 +0000626static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000627 char zResult[8];
drh4c755c02004-08-08 20:22:17 +0000628 const u8 *zIn;
drhd24cc422003-03-27 12:51:24 +0000629 int i, j;
630 static const unsigned char iCode[] = {
631 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
634 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
635 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
636 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
637 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
638 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
639 };
640 assert( argc==1 );
drh4c755c02004-08-08 20:22:17 +0000641 zIn = (u8*)sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000642 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
643 if( zIn[i] ){
644 zResult[0] = toupper(zIn[i]);
645 for(j=1; j<4 && zIn[i]; i++){
646 int code = iCode[zIn[i]&0x7f];
647 if( code>0 ){
648 zResult[j++] = code + '0';
649 }
650 }
651 while( j<4 ){
652 zResult[j++] = '0';
653 }
654 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000655 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:24 +0000656 }else{
danielk1977d8123362004-06-12 09:25:12 +0000657 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:24 +0000658 }
659}
660#endif
661
drh193a6b42002-07-07 16:52:46 +0000662#ifdef SQLITE_TEST
663/*
664** This function generates a string of random characters. Used for
665** generating test data.
666*/
danielk19770ae8b832004-05-25 12:05:56 +0000667static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000668 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000669 "abcdefghijklmnopqrstuvwxyz"
670 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
671 "0123456789"
672 ".-!,:*^+=_|?/<> ";
673 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000674 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000675 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000676 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000677 if( iMin<0 ) iMin = 0;
678 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
679 }else{
680 iMin = 1;
681 }
682 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000683 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000684 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000685 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000686 }else{
687 iMax = 50;
688 }
689 n = iMin;
690 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000692 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000693 n += r%(iMax + 1 - iMin);
694 }
drh1dba7272004-01-16 13:58:18 +0000695 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000696 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000697 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000698 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000699 }
700 zBuf[n] = 0;
drh2646da72005-12-09 20:02:05 +0000701 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
danielk1977d8123362004-06-12 09:25:12 +0000702}
drh0e3d7472004-06-19 17:33:07 +0000703#endif /* SQLITE_TEST */
danielk1977d8123362004-06-12 09:25:12 +0000704
drh0e3d7472004-06-19 17:33:07 +0000705#ifdef SQLITE_TEST
danielk1977d8123362004-06-12 09:25:12 +0000706/*
707** The following two SQL functions are used to test returning a text
708** result with a destructor. Function 'test_destructor' takes one argument
709** and returns the same argument interpreted as TEXT. A destructor is
710** passed with the sqlite3_result_text() call.
711**
712** SQL function 'test_destructor_count' returns the number of outstanding
713** allocations made by 'test_destructor';
714**
715** WARNING: Not threadsafe.
716*/
717static int test_destructor_count_var = 0;
718static void destructor(void *p){
719 char *zVal = (char *)p;
720 assert(zVal);
721 zVal--;
722 sqliteFree(zVal);
723 test_destructor_count_var--;
724}
725static void test_destructor(
726 sqlite3_context *pCtx,
727 int nArg,
728 sqlite3_value **argv
729){
730 char *zVal;
danielk1977f4618892004-06-28 13:09:11 +0000731 int len;
drh9bb575f2004-09-06 17:24:11 +0000732 sqlite3 *db = sqlite3_user_data(pCtx);
danielk1977f4618892004-06-28 13:09:11 +0000733
danielk1977d8123362004-06-12 09:25:12 +0000734 test_destructor_count_var++;
735 assert( nArg==1 );
736 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
danielk197714db2662006-01-09 16:12:04 +0000737 len = sqlite3ValueBytes(argv[0], ENC(db));
danielk1977f4618892004-06-28 13:09:11 +0000738 zVal = sqliteMalloc(len+3);
739 zVal[len] = 0;
740 zVal[len-1] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000741 assert( zVal );
742 zVal++;
danielk197714db2662006-01-09 16:12:04 +0000743 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
744 if( ENC(db)==SQLITE_UTF8 ){
danielk1977f4618892004-06-28 13:09:11 +0000745 sqlite3_result_text(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000746#ifndef SQLITE_OMIT_UTF16
danielk197714db2662006-01-09 16:12:04 +0000747 }else if( ENC(db)==SQLITE_UTF16LE ){
danielk1977f4618892004-06-28 13:09:11 +0000748 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
749 }else{
750 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000751#endif /* SQLITE_OMIT_UTF16 */
danielk1977f4618892004-06-28 13:09:11 +0000752 }
danielk1977d8123362004-06-12 09:25:12 +0000753}
754static void test_destructor_count(
755 sqlite3_context *pCtx,
756 int nArg,
757 sqlite3_value **argv
758){
759 sqlite3_result_int(pCtx, test_destructor_count_var);
drh193a6b42002-07-07 16:52:46 +0000760}
drh0e3d7472004-06-19 17:33:07 +0000761#endif /* SQLITE_TEST */
danielk19773f6b0872004-06-17 05:36:44 +0000762
drh0e3d7472004-06-19 17:33:07 +0000763#ifdef SQLITE_TEST
764/*
765** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
766** interface.
767**
768** The test_auxdata() SQL function attempts to register each of its arguments
769** as auxiliary data. If there are no prior registrations of aux data for
770** that argument (meaning the argument is not a constant or this is its first
771** call) then the result for that argument is 0. If there is a prior
772** registration, the result for that argument is 1. The overall result
773** is the individual argument results separated by spaces.
774*/
danielk19773f6b0872004-06-17 05:36:44 +0000775static void free_test_auxdata(void *p) {sqliteFree(p);}
776static void test_auxdata(
777 sqlite3_context *pCtx,
778 int nArg,
779 sqlite3_value **argv
780){
781 int i;
782 char *zRet = sqliteMalloc(nArg*2);
783 if( !zRet ) return;
784 for(i=0; i<nArg; i++){
drh2646da72005-12-09 20:02:05 +0000785 char const *z = (char*)sqlite3_value_text(argv[i]);
danielk19773f6b0872004-06-17 05:36:44 +0000786 if( z ){
787 char *zAux = sqlite3_get_auxdata(pCtx, i);
788 if( zAux ){
789 zRet[i*2] = '1';
790 if( strcmp(zAux, z) ){
791 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
792 return;
793 }
794 }else{
795 zRet[i*2] = '0';
796 zAux = sqliteStrDup(z);
797 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
798 }
799 zRet[i*2+1] = ' ';
800 }
801 }
802 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
803}
drh0e3d7472004-06-19 17:33:07 +0000804#endif /* SQLITE_TEST */
drh193a6b42002-07-07 16:52:46 +0000805
danielk197701427a62005-01-11 13:02:33 +0000806#ifdef SQLITE_TEST
807/*
808** A function to test error reporting from user functions. This function
809** returns a copy of it's first argument as an error.
810*/
811static void test_error(
812 sqlite3_context *pCtx,
813 int nArg,
814 sqlite3_value **argv
815){
drh2646da72005-12-09 20:02:05 +0000816 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
danielk197701427a62005-01-11 13:02:33 +0000817}
818#endif /* SQLITE_TEST */
819
drh0ac65892002-04-20 14:24:41 +0000820/*
drhd3a149e2002-02-24 17:12:53 +0000821** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000822** sum() or avg() aggregate computation.
823*/
824typedef struct SumCtx SumCtx;
825struct SumCtx {
drh8c08e862006-02-11 17:34:00 +0000826 double rSum; /* Floating point sum */
827 i64 iSum; /* Integer sum */
828 i64 cnt; /* Number of elements summed */
829 u8 overflow; /* True if integer overflow seen */
830 u8 approx; /* True if non-integer value was input to the sum */
drhdd5baa92002-02-27 19:50:59 +0000831};
832
833/*
drha97fdd32006-01-12 22:17:50 +0000834** Routines used to compute the sum, average, and total.
835**
836** The SUM() function follows the (broken) SQL standard which means
837** that it returns NULL if it sums over no inputs. TOTAL returns
838** 0.0 in that case. In addition, TOTAL always returns a float where
839** SUM might return an integer if it never encounters a floating point
840** value.
drh29d72102006-02-09 22:13:41 +0000841**
842** I am told that SUM() should raise an exception if it encounters
843** a integer overflow. But after pondering this, I decided that
844** behavior leads to brittle programs. So instead, I have coded
845** SUM() to revert to using floating point if it encounters an
846** integer overflow. The answer may not be exact, but it will be
847** close. If the SUM() function returns an integer, the value is
848** exact. If SUM() returns a floating point value, it means the
849** value might be approximated.
drhdd5baa92002-02-27 19:50:59 +0000850*/
danielk19770ae8b832004-05-25 12:05:56 +0000851static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000852 SumCtx *p;
drh3d1d95e2005-09-08 10:37:01 +0000853 int type;
drh3f219f42005-09-08 19:45:57 +0000854 assert( argc==1 );
drh4f26d6c2004-05-26 23:25:30 +0000855 p = sqlite3_aggregate_context(context, sizeof(*p));
drh29d72102006-02-09 22:13:41 +0000856 type = sqlite3_value_numeric_type(argv[0]);
drh3d1d95e2005-09-08 10:37:01 +0000857 if( p && type!=SQLITE_NULL ){
drh739105c2002-05-29 23:22:23 +0000858 p->cnt++;
drh29d72102006-02-09 22:13:41 +0000859 if( type==SQLITE_INTEGER ){
drh8c08e862006-02-11 17:34:00 +0000860 i64 v = sqlite3_value_int64(argv[0]);
861 p->rSum += v;
862 if( (p->approx|p->overflow)==0 ){
863 i64 iNewSum = p->iSum + v;
864 int s1 = p->iSum >> (sizeof(i64)*8-1);
865 int s2 = v >> (sizeof(i64)*8-1);
866 int s3 = iNewSum >> (sizeof(i64)*8-1);
867 p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
868 p->iSum = iNewSum;
drh29d72102006-02-09 22:13:41 +0000869 }
870 }else{
drh8c08e862006-02-11 17:34:00 +0000871 p->rSum += sqlite3_value_double(argv[0]);
drh29d72102006-02-09 22:13:41 +0000872 p->approx = 1;
drh3f219f42005-09-08 19:45:57 +0000873 }
drh739105c2002-05-29 23:22:23 +0000874 }
drhdd5baa92002-02-27 19:50:59 +0000875}
danielk19770ae8b832004-05-25 12:05:56 +0000876static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000877 SumCtx *p;
drhabfcea22005-09-06 20:36:48 +0000878 p = sqlite3_aggregate_context(context, 0);
drhc2bd9132005-09-08 20:37:43 +0000879 if( p && p->cnt>0 ){
drh8c08e862006-02-11 17:34:00 +0000880 if( p->overflow ){
881 sqlite3_result_error(context,"integer overflow",-1);
882 }else if( p->approx ){
883 sqlite3_result_double(context, p->rSum);
drhc2bd9132005-09-08 20:37:43 +0000884 }else{
drh8c08e862006-02-11 17:34:00 +0000885 sqlite3_result_int64(context, p->iSum);
drhc2bd9132005-09-08 20:37:43 +0000886 }
drh3d1d95e2005-09-08 10:37:01 +0000887 }
drhdd5baa92002-02-27 19:50:59 +0000888}
danielk19770ae8b832004-05-25 12:05:56 +0000889static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000890 SumCtx *p;
drhabfcea22005-09-06 20:36:48 +0000891 p = sqlite3_aggregate_context(context, 0);
drh739105c2002-05-29 23:22:23 +0000892 if( p && p->cnt>0 ){
drh8c08e862006-02-11 17:34:00 +0000893 sqlite3_result_double(context, p->rSum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000894 }
895}
drha97fdd32006-01-12 22:17:50 +0000896static void totalFinalize(sqlite3_context *context){
897 SumCtx *p;
898 p = sqlite3_aggregate_context(context, 0);
drh8c08e862006-02-11 17:34:00 +0000899 sqlite3_result_double(context, p ? p->rSum : 0.0);
drha97fdd32006-01-12 22:17:50 +0000900}
drhdd5baa92002-02-27 19:50:59 +0000901
902/*
drh0bce8352002-02-28 00:41:10 +0000903** The following structure keeps track of state information for the
904** count() aggregate function.
905*/
906typedef struct CountCtx CountCtx;
907struct CountCtx {
drhfc6ad392006-02-09 13:38:19 +0000908 i64 n;
drh0bce8352002-02-28 00:41:10 +0000909};
drhdd5baa92002-02-27 19:50:59 +0000910
drh0bce8352002-02-28 00:41:10 +0000911/*
912** Routines to implement the count() aggregate function.
913*/
danielk19770ae8b832004-05-25 12:05:56 +0000914static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000915 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000916 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000917 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000918 p->n++;
919 }
920}
danielk19770ae8b832004-05-25 12:05:56 +0000921static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000922 CountCtx *p;
drhabfcea22005-09-06 20:36:48 +0000923 p = sqlite3_aggregate_context(context, 0);
drhfc6ad392006-02-09 13:38:19 +0000924 sqlite3_result_int64(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000925}
926
927/*
drh0bce8352002-02-28 00:41:10 +0000928** Routines to implement min() and max() aggregate functions.
929*/
danielk19770ae8b832004-05-25 12:05:56 +0000930static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000931 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:32 +0000932 Mem *pBest;
933
934 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
935 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:04 +0000936 if( !pBest ) return;
drh268380c2004-02-25 13:47:31 +0000937
danielk197788208052004-05-25 01:13:20 +0000938 if( pBest->flags ){
drh9eb516c2004-07-18 20:52:32 +0000939 int max;
940 int cmp;
danielk1977dc1bdc42004-06-11 10:51:27 +0000941 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +0000942 /* This step function is used for both the min() and max() aggregates,
943 ** the only difference between the two being that the sense of the
944 ** comparison is inverted. For the max() aggregate, the
945 ** sqlite3_user_data() function returns (void *)-1. For min() it
946 ** returns (void *)db, where db is the sqlite3* database pointer.
947 ** Therefore the next statement sets variable 'max' to 1 for the max()
948 ** aggregate, or 0 for min().
949 */
danielk197788208052004-05-25 01:13:20 +0000950 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +0000951 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +0000952 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000953 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000954 }
drh268380c2004-02-25 13:47:31 +0000955 }else{
danielk19777e18c252004-05-25 11:47:24 +0000956 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000957 }
958}
danielk19770ae8b832004-05-25 12:05:56 +0000959static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000960 sqlite3_value *pRes;
drhabfcea22005-09-06 20:36:48 +0000961 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
962 if( pRes ){
963 if( pRes->flags ){
964 sqlite3_result_value(context, pRes);
965 }
966 sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:10 +0000967 }
968}
drhdd5baa92002-02-27 19:50:59 +0000969
drh4e5ffc52004-08-31 00:52:37 +0000970
drhd3a149e2002-02-24 17:12:53 +0000971/*
drha2ed5602002-02-26 23:55:31 +0000972** This function registered all of the above C functions as SQL
973** functions. This should be the only routine in this file with
974** external linkage.
drhdc04c582002-02-24 01:55:15 +0000975*/
drh9bb575f2004-09-06 17:24:11 +0000976void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
drh57196282004-10-06 15:41:16 +0000977 static const struct {
drh0bce8352002-02-28 00:41:10 +0000978 char *zName;
drh268380c2004-02-25 13:47:31 +0000979 signed char nArg;
danielk1977f4618892004-06-28 13:09:11 +0000980 u8 argType; /* 0: none. 1: db 2: (-1) */
981 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +0000982 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000983 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000984 } aFuncs[] = {
danielk1977f4618892004-06-28 13:09:11 +0000985 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
986 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
987 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc },
988 { "max", 0, 2, SQLITE_UTF8, 1, 0 },
989 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
990 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
991 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
drh6c626082004-11-14 21:56:29 +0000992#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +0000993 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
drh6c626082004-11-14 21:56:29 +0000994#endif
danielk1977f4618892004-06-28 13:09:11 +0000995 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
996 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
997 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
998 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
999 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
1000 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
1001 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
1002 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
1003 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
1004 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
drh94a98362004-09-13 13:13:18 +00001005 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
danielk1977f4618892004-06-28 13:09:11 +00001006 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
1007 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
1008 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid },
1009 { "changes", 0, 1, SQLITE_UTF8, 0, changes },
1010 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes },
drhd24cc422003-03-27 12:51:24 +00001011#ifdef SQLITE_SOUNDEX
danielk1977f4618892004-06-28 13:09:11 +00001012 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +00001013#endif
drh193a6b42002-07-07 16:52:46 +00001014#ifdef SQLITE_TEST
danielk1977f4618892004-06-28 13:09:11 +00001015 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
1016 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor},
danielk1977d8123362004-06-12 09:25:12 +00001017 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
danielk1977f4618892004-06-28 13:09:11 +00001018 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
danielk197701427a62005-01-11 13:02:33 +00001019 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
drh193a6b42002-07-07 16:52:46 +00001020#endif
drh0bce8352002-02-28 00:41:10 +00001021 };
drh57196282004-10-06 15:41:16 +00001022 static const struct {
drh0bce8352002-02-28 00:41:10 +00001023 char *zName;
drh268380c2004-02-25 13:47:31 +00001024 signed char nArg;
drh268380c2004-02-25 13:47:31 +00001025 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +00001026 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001027 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1028 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +00001029 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +00001030 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1031 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
1032 { "sum", 1, 0, 0, sumStep, sumFinalize },
drha97fdd32006-01-12 22:17:50 +00001033 { "total", 1, 0, 0, sumStep, totalFinalize },
danielk1977dc1bdc42004-06-11 10:51:27 +00001034 { "avg", 1, 0, 0, sumStep, avgFinalize },
1035 { "count", 0, 0, 0, countStep, countFinalize },
1036 { "count", 1, 0, 0, countStep, countFinalize },
drh0bce8352002-02-28 00:41:10 +00001037 };
1038 int i;
1039
1040 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001041 void *pArg = 0;
1042 switch( aFuncs[i].argType ){
1043 case 1: pArg = db; break;
1044 case 2: pArg = (void *)(-1); break;
1045 }
danielk1977771151b2006-01-17 13:21:40 +00001046 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
danielk1977f9d64d22004-06-19 08:18:07 +00001047 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001048 if( aFuncs[i].needCollSeq ){
1049 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1050 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1051 if( pFunc && aFuncs[i].needCollSeq ){
1052 pFunc->needCollSeq = 1;
1053 }
1054 }
drh0bce8352002-02-28 00:41:10 +00001055 }
drh1f01ec12005-02-15 21:36:18 +00001056#ifndef SQLITE_OMIT_ALTERTABLE
1057 sqlite3AlterFunctions(db);
1058#endif
drh198bf392006-01-06 21:52:49 +00001059#ifndef SQLITE_OMIT_PARSER
danielk1977f744bb52005-12-06 17:19:11 +00001060 sqlite3AttachFunctions(db);
drh198bf392006-01-06 21:52:49 +00001061#endif
drh0bce8352002-02-28 00:41:10 +00001062 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001063 void *pArg = 0;
1064 switch( aAggs[i].argType ){
1065 case 1: pArg = db; break;
1066 case 2: pArg = (void *)(-1); break;
1067 }
danielk1977771151b2006-01-17 13:21:40 +00001068 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001069 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +00001070 if( aAggs[i].needCollSeq ){
1071 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
danielk1977d8123362004-06-12 09:25:12 +00001072 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001073 if( pFunc && aAggs[i].needCollSeq ){
1074 pFunc->needCollSeq = 1;
1075 }
1076 }
drh268380c2004-02-25 13:47:31 +00001077 }
danielk19774adee202004-05-08 08:23:19 +00001078 sqlite3RegisterDateTimeFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001079#ifdef SQLITE_SSE
drh55ef4d92005-08-14 01:20:37 +00001080 sqlite3SseFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001081#endif
drh55ef4d92005-08-14 01:20:37 +00001082#ifdef SQLITE_CASE_SENSITIVE_LIKE
1083 sqlite3RegisterLikeFunctions(db, 1);
1084#else
1085 sqlite3RegisterLikeFunctions(db, 0);
1086#endif
1087}
1088
1089/*
1090** Set the LIKEOPT flag on the 2-argument function with the given name.
1091*/
drhd64fe2f2005-08-28 17:00:23 +00001092static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
drh55ef4d92005-08-14 01:20:37 +00001093 FuncDef *pDef;
1094 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
1095 if( pDef ){
drhd64fe2f2005-08-28 17:00:23 +00001096 pDef->flags = flagVal;
drh55ef4d92005-08-14 01:20:37 +00001097 }
1098}
1099
1100/*
1101** Register the built-in LIKE and GLOB functions. The caseSensitive
1102** parameter determines whether or not the LIKE operator is case
1103** sensitive. GLOB is always case sensitive.
1104*/
1105void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
1106 struct compareInfo *pInfo;
1107 if( caseSensitive ){
1108 pInfo = (struct compareInfo*)&likeInfoAlt;
1109 }else{
1110 pInfo = (struct compareInfo*)&likeInfoNorm;
1111 }
danielk1977771151b2006-01-17 13:21:40 +00001112 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1113 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1114 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
drh55ef4d92005-08-14 01:20:37 +00001115 (struct compareInfo*)&globInfo, likeFunc, 0,0);
drhd64fe2f2005-08-28 17:00:23 +00001116 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1117 setLikeOptFlag(db, "like",
1118 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
drh55ef4d92005-08-14 01:20:37 +00001119}
1120
1121/*
1122** pExpr points to an expression which implements a function. If
1123** it is appropriate to apply the LIKE optimization to that function
1124** then set aWc[0] through aWc[2] to the wildcard characters and
1125** return TRUE. If the function is not a LIKE-style function then
1126** return FALSE.
1127*/
drhd64fe2f2005-08-28 17:00:23 +00001128int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
drh55ef4d92005-08-14 01:20:37 +00001129 FuncDef *pDef;
1130 if( pExpr->op!=TK_FUNCTION ){
1131 return 0;
1132 }
1133 if( pExpr->pList->nExpr!=2 ){
1134 return 0;
1135 }
drh2646da72005-12-09 20:02:05 +00001136 pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
drh55ef4d92005-08-14 01:20:37 +00001137 SQLITE_UTF8, 0);
drhd64fe2f2005-08-28 17:00:23 +00001138 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
drh55ef4d92005-08-14 01:20:37 +00001139 return 0;
1140 }
1141
1142 /* The memcpy() statement assumes that the wildcard characters are
1143 ** the first three statements in the compareInfo structure. The
1144 ** asserts() that follow verify that assumption
1145 */
1146 memcpy(aWc, pDef->pUserData, 3);
1147 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
1148 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
1149 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
drhd64fe2f2005-08-28 17:00:23 +00001150 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
drh55ef4d92005-08-14 01:20:37 +00001151 return 1;
drhdc04c582002-02-24 01:55:15 +00001152}