blob: 4f697713fe8b02fa6de091730b76a8a9033999dc [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**
danielk197765fd59f2006-06-24 11:51:33 +000019** $Id: func.c,v 1.132 2006/06/24 11:51:33 danielk1977 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 }
drhd589a922006-03-02 03:02:48 +0000204 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) 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);
drh502b9622006-04-07 13:26:42 +0000207 sqlite3AtoF(zBuf, &r);
208 sqlite3_result_double(context, r);
drh0bce8352002-02-28 00:41:10 +0000209}
drhdc04c582002-02-24 01:55:15 +0000210
211/*
212** Implementation of the upper() and lower() SQL functions.
213*/
danielk19770ae8b832004-05-25 12:05:56 +0000214static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000215 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000216 int i;
drh9c054832004-05-31 18:51:57 +0000217 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000218 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000219 if( z==0 ) return;
drh2646da72005-12-09 20:02:05 +0000220 strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000221 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000222 z[i] = toupper(z[i]);
drhdc04c582002-02-24 01:55:15 +0000223 }
drh2646da72005-12-09 20:02:05 +0000224 sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000225 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000226}
danielk19770ae8b832004-05-25 12:05:56 +0000227static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000228 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000229 int i;
drh9c054832004-05-31 18:51:57 +0000230 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000231 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000232 if( z==0 ) return;
drh2646da72005-12-09 20:02:05 +0000233 strcpy((char*)z, (char*)sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000234 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000235 z[i] = tolower(z[i]);
drhdc04c582002-02-24 01:55:15 +0000236 }
drh2646da72005-12-09 20:02:05 +0000237 sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000238 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000239}
240
241/*
drhfbc99082002-02-28 03:14:18 +0000242** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000243** All three do the same thing. They return the first non-NULL
244** argument.
drh3212e182002-02-28 00:46:26 +0000245*/
drhf9b596e2004-05-26 16:54:42 +0000246static void ifnullFunc(
247 sqlite3_context *context,
248 int argc,
249 sqlite3_value **argv
250){
drhfbc99082002-02-28 03:14:18 +0000251 int i;
252 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000253 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000254 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000255 break;
256 }
257 }
drh3212e182002-02-28 00:46:26 +0000258}
259
260/*
drhf9ffac92002-03-02 19:00:31 +0000261** Implementation of random(). Return a random integer.
262*/
drhf9b596e2004-05-26 16:54:42 +0000263static void randomFunc(
264 sqlite3_context *context,
265 int argc,
266 sqlite3_value **argv
267){
drh52fc8492006-02-23 21:43:55 +0000268 sqlite_int64 r;
danielk19774adee202004-05-08 08:23:19 +0000269 sqlite3Randomness(sizeof(r), &r);
drh874abbe2006-02-23 21:51:12 +0000270 if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */
271 /* can always do abs() of the result */
drh52fc8492006-02-23 21:43:55 +0000272 sqlite3_result_int64(context, r);
drhf9ffac92002-03-02 19:00:31 +0000273}
274
275/*
drh6ed41ad2002-04-06 14:10:47 +0000276** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000277** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000278*/
danielk197751ad0ec2004-05-24 12:39:02 +0000279static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000280 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000281 int arg,
282 sqlite3_value **argv
283){
drh9bb575f2004-09-06 17:24:11 +0000284 sqlite3 *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000285 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000286}
287
rdcf146a772004-02-25 22:51:06 +0000288/*
danielk1977b28af712004-06-21 06:50:26 +0000289** Implementation of the changes() SQL function. The return value is the
290** same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000291*/
danielk1977b28af712004-06-21 06:50:26 +0000292static void changes(
drhf9b596e2004-05-26 16:54:42 +0000293 sqlite3_context *context,
294 int arg,
295 sqlite3_value **argv
296){
drh9bb575f2004-09-06 17:24:11 +0000297 sqlite3 *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000298 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000299}
rdcf146a772004-02-25 22:51:06 +0000300
301/*
danielk1977b28af712004-06-21 06:50:26 +0000302** Implementation of the total_changes() SQL function. The return value is
303** the same as the sqlite3_total_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000304*/
danielk1977b28af712004-06-21 06:50:26 +0000305static void total_changes(
306 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000307 int arg,
308 sqlite3_value **argv
309){
drh9bb575f2004-09-06 17:24:11 +0000310 sqlite3 *db = sqlite3_user_data(context);
danielk1977b28af712004-06-21 06:50:26 +0000311 sqlite3_result_int(context, sqlite3_total_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000312}
313
drh6ed41ad2002-04-06 14:10:47 +0000314/*
drh4e5ffc52004-08-31 00:52:37 +0000315** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03 +0000316*/
drh4e5ffc52004-08-31 00:52:37 +0000317struct compareInfo {
318 u8 matchAll;
319 u8 matchOne;
320 u8 matchSet;
321 u8 noCase;
danielk1977d02eb1f2004-06-06 09:44:03 +0000322};
drh55ef4d92005-08-14 01:20:37 +0000323
drh4e5ffc52004-08-31 00:52:37 +0000324static const struct compareInfo globInfo = { '*', '?', '[', 0 };
drh55ef4d92005-08-14 01:20:37 +0000325/* The correct SQL-92 behavior is for the LIKE operator to ignore
326** case. Thus 'a' LIKE 'A' would be true. */
327static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
328/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
329** is case sensitive causing 'a' LIKE 'A' to be false */
330static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
danielk1977d02eb1f2004-06-06 09:44:03 +0000331
332/*
drh4e5ffc52004-08-31 00:52:37 +0000333** X is a pointer to the first byte of a UTF-8 character. Increment
334** X so that it points to the next character. This only works right
335** if X points to a well-formed UTF-8 string.
danielk1977d02eb1f2004-06-06 09:44:03 +0000336*/
drh4e5ffc52004-08-31 00:52:37 +0000337#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
338#define sqliteCharVal(X) sqlite3ReadUtf8(X)
danielk1977d02eb1f2004-06-06 09:44:03 +0000339
danielk1977d02eb1f2004-06-06 09:44:03 +0000340
341/*
drh4e5ffc52004-08-31 00:52:37 +0000342** Compare two UTF-8 strings for equality where the first string can
343** potentially be a "glob" expression. Return true (1) if they
344** are the same and false (0) if they are different.
drh0ac65892002-04-20 14:24:41 +0000345**
drh4e5ffc52004-08-31 00:52:37 +0000346** Globbing rules:
drh0ac65892002-04-20 14:24:41 +0000347**
drh4e5ffc52004-08-31 00:52:37 +0000348** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03 +0000349**
drh4e5ffc52004-08-31 00:52:37 +0000350** '?' Matches exactly one character.
351**
352** [...] Matches one character from the enclosed list of
353** characters.
354**
355** [^...] Matches one character not in the enclosed list.
356**
357** With the [...] and [^...] matching, a ']' character can be included
358** in the list by making it the first character after '[' or '^'. A
359** range of characters can be specified using '-'. Example:
360** "[a-z]" matches any single lower-case letter. To match a '-', make
361** it the last character in the list.
362**
363** This routine is usually quick, but can be N**2 in the worst case.
364**
365** Hints: to match '*' or '?', put them in "[]". Like this:
366**
367** abc[*]xyz Matches "abc*xyz" only
drh0ac65892002-04-20 14:24:41 +0000368*/
danielk19777c6303c2004-11-17 16:41:29 +0000369static int patternCompare(
drh4e5ffc52004-08-31 00:52:37 +0000370 const u8 *zPattern, /* The glob pattern */
371 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29 +0000372 const struct compareInfo *pInfo, /* Information about how to do the compare */
373 const int esc /* The escape character */
danielk197751ad0ec2004-05-24 12:39:02 +0000374){
danielk1977ad7dd422004-06-06 12:41:49 +0000375 register int c;
drh4e5ffc52004-08-31 00:52:37 +0000376 int invert;
377 int seen;
378 int c2;
379 u8 matchOne = pInfo->matchOne;
380 u8 matchAll = pInfo->matchAll;
381 u8 matchSet = pInfo->matchSet;
382 u8 noCase = pInfo->noCase;
danielk19777c6303c2004-11-17 16:41:29 +0000383 int prevEscape = 0; /* True if the previous character was 'escape' */
danielk1977d02eb1f2004-06-06 09:44:03 +0000384
drh4e5ffc52004-08-31 00:52:37 +0000385 while( (c = *zPattern)!=0 ){
danielk19777c6303c2004-11-17 16:41:29 +0000386 if( !prevEscape && c==matchAll ){
drh4e5ffc52004-08-31 00:52:37 +0000387 while( (c=zPattern[1]) == matchAll || c == matchOne ){
388 if( c==matchOne ){
389 if( *zString==0 ) return 0;
390 sqliteNextChar(zString);
391 }
392 zPattern++;
danielk1977ad7dd422004-06-06 12:41:49 +0000393 }
drh20fc0882004-11-18 13:49:25 +0000394 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
danielk19777c6303c2004-11-17 16:41:29 +0000395 u8 const *zTemp = &zPattern[1];
396 sqliteNextChar(zTemp);
397 c = *zTemp;
398 }
drh4e5ffc52004-08-31 00:52:37 +0000399 if( c==0 ) return 1;
400 if( c==matchSet ){
danielk19777c6303c2004-11-17 16:41:29 +0000401 assert( esc==0 ); /* This is GLOB, not LIKE */
402 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
drh4e5ffc52004-08-31 00:52:37 +0000403 sqliteNextChar(zString);
404 }
405 return *zString!=0;
406 }else{
407 while( (c2 = *zString)!=0 ){
408 if( noCase ){
409 c2 = sqlite3UpperToLower[c2];
410 c = sqlite3UpperToLower[c];
411 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
412 }else{
413 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
414 }
415 if( c2==0 ) return 0;
danielk19777c6303c2004-11-17 16:41:29 +0000416 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
drh4e5ffc52004-08-31 00:52:37 +0000417 sqliteNextChar(zString);
418 }
419 return 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000420 }
danielk19777c6303c2004-11-17 16:41:29 +0000421 }else if( !prevEscape && c==matchOne ){
drh4e5ffc52004-08-31 00:52:37 +0000422 if( *zString==0 ) return 0;
423 sqliteNextChar(zString);
424 zPattern++;
425 }else if( c==matchSet ){
426 int prior_c = 0;
danielk19777c6303c2004-11-17 16:41:29 +0000427 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
drh4e5ffc52004-08-31 00:52:37 +0000428 seen = 0;
429 invert = 0;
430 c = sqliteCharVal(zString);
431 if( c==0 ) return 0;
432 c2 = *++zPattern;
433 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
434 if( c2==']' ){
435 if( c==']' ) seen = 1;
436 c2 = *++zPattern;
437 }
438 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
439 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
440 zPattern++;
441 c2 = sqliteCharVal(zPattern);
442 if( c>=prior_c && c<=c2 ) seen = 1;
443 prior_c = 0;
444 }else if( c==c2 ){
445 seen = 1;
446 prior_c = c2;
447 }else{
448 prior_c = c2;
449 }
450 sqliteNextChar(zPattern);
451 }
452 if( c2==0 || (seen ^ invert)==0 ) return 0;
453 sqliteNextChar(zString);
454 zPattern++;
drh20fc0882004-11-18 13:49:25 +0000455 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
danielk19777c6303c2004-11-17 16:41:29 +0000456 prevEscape = 1;
457 sqliteNextChar(zPattern);
drh4e5ffc52004-08-31 00:52:37 +0000458 }else{
459 if( noCase ){
460 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
461 }else{
462 if( c != *zString ) return 0;
463 }
464 zPattern++;
465 zString++;
danielk19777c6303c2004-11-17 16:41:29 +0000466 prevEscape = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000467 }
danielk197751ad0ec2004-05-24 12:39:02 +0000468 }
drh4e5ffc52004-08-31 00:52:37 +0000469 return *zString==0;
drh0ac65892002-04-20 14:24:41 +0000470}
drh4e5ffc52004-08-31 00:52:37 +0000471
drh55ef4d92005-08-14 01:20:37 +0000472/*
473** Count the number of times that the LIKE operator (or GLOB which is
474** just a variation of LIKE) gets called. This is used for testing
475** only.
476*/
477#ifdef SQLITE_TEST
478int sqlite3_like_count = 0;
479#endif
480
danielk19773f6b0872004-06-17 05:36:44 +0000481
482/*
483** Implementation of the like() SQL function. This function implements
484** the build-in LIKE operator. The first argument to the function is the
485** pattern and the second argument is the string. So, the SQL statements:
486**
487** A LIKE B
488**
489** is implemented as like(B,A).
490**
drh55ef4d92005-08-14 01:20:37 +0000491** This same function (with a different compareInfo structure) computes
492** the GLOB operator.
danielk19773f6b0872004-06-17 05:36:44 +0000493*/
494static void likeFunc(
495 sqlite3_context *context,
496 int argc,
497 sqlite3_value **argv
498){
499 const unsigned char *zA = sqlite3_value_text(argv[0]);
500 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk19777c6303c2004-11-17 16:41:29 +0000501 int escape = 0;
502 if( argc==3 ){
503 /* The escape character string must consist of a single UTF-8 character.
504 ** Otherwise, return an error.
505 */
506 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
drh2646da72005-12-09 20:02:05 +0000507 if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){
danielk19777c6303c2004-11-17 16:41:29 +0000508 sqlite3_result_error(context,
509 "ESCAPE expression must be a single character", -1);
510 return;
511 }
512 escape = sqlite3ReadUtf8(zEsc);
513 }
danielk19773f6b0872004-06-17 05:36:44 +0000514 if( zA && zB ){
drh55ef4d92005-08-14 01:20:37 +0000515 struct compareInfo *pInfo = sqlite3_user_data(context);
516#ifdef SQLITE_TEST
517 sqlite3_like_count++;
518#endif
519 sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape));
danielk197751ad0ec2004-05-24 12:39:02 +0000520 }
drh8912d102002-05-26 21:34:58 +0000521}
522
523/*
524** Implementation of the NULLIF(x,y) function. The result is the first
525** argument if the arguments are different. The result is NULL if the
526** arguments are equal to each other.
527*/
drhf9b596e2004-05-26 16:54:42 +0000528static void nullifFunc(
529 sqlite3_context *context,
530 int argc,
531 sqlite3_value **argv
532){
danielk1977dc1bdc42004-06-11 10:51:27 +0000533 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
534 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000535 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000536 }
drh0ac65892002-04-20 14:24:41 +0000537}
538
drh647cb0e2002-11-04 19:32:25 +0000539/*
540** Implementation of the VERSION(*) function. The result is the version
541** of the SQLite library that is running.
542*/
drhf9b596e2004-05-26 16:54:42 +0000543static void versionFunc(
544 sqlite3_context *context,
545 int argc,
546 sqlite3_value **argv
547){
danielk1977d8123362004-06-12 09:25:12 +0000548 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:25 +0000549}
550
drh7f375902006-06-13 17:38:59 +0000551/*
552** The MATCH() function is unimplemented. If anybody tries to use it,
553** return an error.
554*/
555static void matchStub(
556 sqlite3_context *context,
557 int argc,
558 sqlite3_value **argv
559){
560 static const char zErr[] = "MATCH is not implemented";
561 sqlite3_result_error(context, zErr, sizeof(zErr)-1);
562}
563
danielk1977d641d642004-11-18 15:44:29 +0000564
drh47394702003-08-20 01:03:33 +0000565/*
566** EXPERIMENTAL - This is not an official function. The interface may
567** change. This function may disappear. Do not write code that depends
568** on this function.
569**
570** Implementation of the QUOTE() function. This function takes a single
571** argument. If the argument is numeric, the return value is the same as
572** the argument. If the argument is NULL, the return value is the string
573** "NULL". Otherwise, the argument is enclosed in single quotes with
574** single-quote escapes.
575*/
danielk19770ae8b832004-05-25 12:05:56 +0000576static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000577 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000578 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000579 case SQLITE_NULL: {
danielk1977d8123362004-06-12 09:25:12 +0000580 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
drhf9b596e2004-05-26 16:54:42 +0000581 break;
drh47394702003-08-20 01:03:33 +0000582 }
drh9c054832004-05-31 18:51:57 +0000583 case SQLITE_INTEGER:
584 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000585 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000586 break;
587 }
danielk19773f41e972004-06-08 00:39:01 +0000588 case SQLITE_BLOB: {
589 static const char hexdigits[] = {
590 '0', '1', '2', '3', '4', '5', '6', '7',
591 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
592 };
593 char *zText = 0;
594 int nBlob = sqlite3_value_bytes(argv[0]);
595 char const *zBlob = sqlite3_value_blob(argv[0]);
596
597 zText = (char *)sqliteMalloc((2*nBlob)+4);
598 if( !zText ){
599 sqlite3_result_error(context, "out of memory", -1);
600 }else{
601 int i;
602 for(i=0; i<nBlob; i++){
603 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
604 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
605 }
606 zText[(nBlob*2)+2] = '\'';
607 zText[(nBlob*2)+3] = '\0';
608 zText[0] = 'X';
609 zText[1] = '\'';
danielk1977d8123362004-06-12 09:25:12 +0000610 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
danielk19773f41e972004-06-08 00:39:01 +0000611 sqliteFree(zText);
612 }
613 break;
614 }
drh9c054832004-05-31 18:51:57 +0000615 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000616 int i,j,n;
drh2646da72005-12-09 20:02:05 +0000617 const unsigned char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000618 char *z;
619
620 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
621 z = sqliteMalloc( i+n+3 );
622 if( z==0 ) return;
623 z[0] = '\'';
624 for(i=0, j=1; zArg[i]; i++){
625 z[j++] = zArg[i];
626 if( zArg[i]=='\'' ){
627 z[j++] = '\'';
628 }
629 }
630 z[j++] = '\'';
631 z[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000632 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
drhf9b596e2004-05-26 16:54:42 +0000633 sqliteFree(z);
634 }
drh47394702003-08-20 01:03:33 +0000635 }
636}
637
drhd24cc422003-03-27 12:51:24 +0000638#ifdef SQLITE_SOUNDEX
639/*
640** Compute the soundex encoding of a word.
641*/
danielk19770ae8b832004-05-25 12:05:56 +0000642static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000643 char zResult[8];
drh4c755c02004-08-08 20:22:17 +0000644 const u8 *zIn;
drhd24cc422003-03-27 12:51:24 +0000645 int i, j;
646 static const unsigned char iCode[] = {
647 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
648 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
649 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
650 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
651 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
652 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
653 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
654 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
655 };
656 assert( argc==1 );
drh4c755c02004-08-08 20:22:17 +0000657 zIn = (u8*)sqlite3_value_text(argv[0]);
drh88897a72006-06-13 19:26:10 +0000658 if( zIn==0 ) zIn = "";
drhd24cc422003-03-27 12:51:24 +0000659 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
660 if( zIn[i] ){
661 zResult[0] = toupper(zIn[i]);
662 for(j=1; j<4 && zIn[i]; i++){
663 int code = iCode[zIn[i]&0x7f];
664 if( code>0 ){
665 zResult[j++] = code + '0';
666 }
667 }
668 while( j<4 ){
669 zResult[j++] = '0';
670 }
671 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000672 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:24 +0000673 }else{
danielk1977d8123362004-06-12 09:25:12 +0000674 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:24 +0000675 }
676}
677#endif
678
drhfdb83b22006-06-17 14:12:47 +0000679#ifndef SQLITE_OMIT_LOAD_EXTENSION
680/*
681** A function that loads a shared-library extension then returns NULL.
682*/
683static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197765fd59f2006-06-24 11:51:33 +0000684 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
drhfdb83b22006-06-17 14:12:47 +0000685 const char *zProc = 0;
686 sqlite3 *db = sqlite3_user_data(context);
687 char *zErrMsg = 0;
688
689 if( argc==2 ){
danielk197765fd59f2006-06-24 11:51:33 +0000690 zProc = (const char *)sqlite3_value_text(argv[1]);
drhfdb83b22006-06-17 14:12:47 +0000691 }
692 if( sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
693 sqlite3_result_error(context, zErrMsg, -1);
694 sqlite3_free(zErrMsg);
695 }
696}
697#endif
698
drh193a6b42002-07-07 16:52:46 +0000699#ifdef SQLITE_TEST
700/*
701** This function generates a string of random characters. Used for
702** generating test data.
703*/
danielk19770ae8b832004-05-25 12:05:56 +0000704static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000705 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000706 "abcdefghijklmnopqrstuvwxyz"
707 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
708 "0123456789"
709 ".-!,:*^+=_|?/<> ";
710 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000711 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000712 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000713 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000714 if( iMin<0 ) iMin = 0;
715 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
716 }else{
717 iMin = 1;
718 }
719 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000720 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000721 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000722 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000723 }else{
724 iMax = 50;
725 }
726 n = iMin;
727 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000728 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000729 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000730 n += r%(iMax + 1 - iMin);
731 }
drh1dba7272004-01-16 13:58:18 +0000732 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000734 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000735 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000736 }
737 zBuf[n] = 0;
drh2646da72005-12-09 20:02:05 +0000738 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
danielk1977d8123362004-06-12 09:25:12 +0000739}
drh0e3d7472004-06-19 17:33:07 +0000740#endif /* SQLITE_TEST */
danielk1977d8123362004-06-12 09:25:12 +0000741
drh0e3d7472004-06-19 17:33:07 +0000742#ifdef SQLITE_TEST
danielk1977d8123362004-06-12 09:25:12 +0000743/*
744** The following two SQL functions are used to test returning a text
745** result with a destructor. Function 'test_destructor' takes one argument
746** and returns the same argument interpreted as TEXT. A destructor is
747** passed with the sqlite3_result_text() call.
748**
749** SQL function 'test_destructor_count' returns the number of outstanding
750** allocations made by 'test_destructor';
751**
752** WARNING: Not threadsafe.
753*/
754static int test_destructor_count_var = 0;
755static void destructor(void *p){
756 char *zVal = (char *)p;
757 assert(zVal);
758 zVal--;
759 sqliteFree(zVal);
760 test_destructor_count_var--;
761}
762static void test_destructor(
763 sqlite3_context *pCtx,
764 int nArg,
765 sqlite3_value **argv
766){
767 char *zVal;
danielk1977f4618892004-06-28 13:09:11 +0000768 int len;
drh9bb575f2004-09-06 17:24:11 +0000769 sqlite3 *db = sqlite3_user_data(pCtx);
danielk1977f4618892004-06-28 13:09:11 +0000770
danielk1977d8123362004-06-12 09:25:12 +0000771 test_destructor_count_var++;
772 assert( nArg==1 );
773 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
danielk197714db2662006-01-09 16:12:04 +0000774 len = sqlite3ValueBytes(argv[0], ENC(db));
danielk1977f4618892004-06-28 13:09:11 +0000775 zVal = sqliteMalloc(len+3);
776 zVal[len] = 0;
777 zVal[len-1] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000778 assert( zVal );
779 zVal++;
danielk197714db2662006-01-09 16:12:04 +0000780 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
781 if( ENC(db)==SQLITE_UTF8 ){
danielk1977f4618892004-06-28 13:09:11 +0000782 sqlite3_result_text(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000783#ifndef SQLITE_OMIT_UTF16
danielk197714db2662006-01-09 16:12:04 +0000784 }else if( ENC(db)==SQLITE_UTF16LE ){
danielk1977f4618892004-06-28 13:09:11 +0000785 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
786 }else{
787 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000788#endif /* SQLITE_OMIT_UTF16 */
danielk1977f4618892004-06-28 13:09:11 +0000789 }
danielk1977d8123362004-06-12 09:25:12 +0000790}
791static void test_destructor_count(
792 sqlite3_context *pCtx,
793 int nArg,
794 sqlite3_value **argv
795){
796 sqlite3_result_int(pCtx, test_destructor_count_var);
drh193a6b42002-07-07 16:52:46 +0000797}
drh0e3d7472004-06-19 17:33:07 +0000798#endif /* SQLITE_TEST */
danielk19773f6b0872004-06-17 05:36:44 +0000799
drh0e3d7472004-06-19 17:33:07 +0000800#ifdef SQLITE_TEST
801/*
802** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
803** interface.
804**
805** The test_auxdata() SQL function attempts to register each of its arguments
806** as auxiliary data. If there are no prior registrations of aux data for
807** that argument (meaning the argument is not a constant or this is its first
808** call) then the result for that argument is 0. If there is a prior
809** registration, the result for that argument is 1. The overall result
810** is the individual argument results separated by spaces.
811*/
danielk19773f6b0872004-06-17 05:36:44 +0000812static void free_test_auxdata(void *p) {sqliteFree(p);}
813static void test_auxdata(
814 sqlite3_context *pCtx,
815 int nArg,
816 sqlite3_value **argv
817){
818 int i;
819 char *zRet = sqliteMalloc(nArg*2);
820 if( !zRet ) return;
821 for(i=0; i<nArg; i++){
drh2646da72005-12-09 20:02:05 +0000822 char const *z = (char*)sqlite3_value_text(argv[i]);
danielk19773f6b0872004-06-17 05:36:44 +0000823 if( z ){
824 char *zAux = sqlite3_get_auxdata(pCtx, i);
825 if( zAux ){
826 zRet[i*2] = '1';
827 if( strcmp(zAux, z) ){
828 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
829 return;
830 }
831 }else{
832 zRet[i*2] = '0';
833 zAux = sqliteStrDup(z);
834 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
835 }
836 zRet[i*2+1] = ' ';
837 }
838 }
839 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
840}
drh0e3d7472004-06-19 17:33:07 +0000841#endif /* SQLITE_TEST */
drh193a6b42002-07-07 16:52:46 +0000842
danielk197701427a62005-01-11 13:02:33 +0000843#ifdef SQLITE_TEST
844/*
845** A function to test error reporting from user functions. This function
846** returns a copy of it's first argument as an error.
847*/
848static void test_error(
849 sqlite3_context *pCtx,
850 int nArg,
851 sqlite3_value **argv
852){
drh2646da72005-12-09 20:02:05 +0000853 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
danielk197701427a62005-01-11 13:02:33 +0000854}
855#endif /* SQLITE_TEST */
856
drh0ac65892002-04-20 14:24:41 +0000857/*
drhd3a149e2002-02-24 17:12:53 +0000858** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000859** sum() or avg() aggregate computation.
860*/
861typedef struct SumCtx SumCtx;
862struct SumCtx {
drh8c08e862006-02-11 17:34:00 +0000863 double rSum; /* Floating point sum */
864 i64 iSum; /* Integer sum */
865 i64 cnt; /* Number of elements summed */
866 u8 overflow; /* True if integer overflow seen */
867 u8 approx; /* True if non-integer value was input to the sum */
drhdd5baa92002-02-27 19:50:59 +0000868};
869
870/*
drha97fdd32006-01-12 22:17:50 +0000871** Routines used to compute the sum, average, and total.
872**
873** The SUM() function follows the (broken) SQL standard which means
874** that it returns NULL if it sums over no inputs. TOTAL returns
875** 0.0 in that case. In addition, TOTAL always returns a float where
876** SUM might return an integer if it never encounters a floating point
drhc806d852006-05-11 13:25:39 +0000877** value. TOTAL never fails, but SUM might through an exception if
878** it overflows an integer.
drhdd5baa92002-02-27 19:50:59 +0000879*/
danielk19770ae8b832004-05-25 12:05:56 +0000880static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000881 SumCtx *p;
drh3d1d95e2005-09-08 10:37:01 +0000882 int type;
drh3f219f42005-09-08 19:45:57 +0000883 assert( argc==1 );
drh4f26d6c2004-05-26 23:25:30 +0000884 p = sqlite3_aggregate_context(context, sizeof(*p));
drh29d72102006-02-09 22:13:41 +0000885 type = sqlite3_value_numeric_type(argv[0]);
drh3d1d95e2005-09-08 10:37:01 +0000886 if( p && type!=SQLITE_NULL ){
drh739105c2002-05-29 23:22:23 +0000887 p->cnt++;
drh29d72102006-02-09 22:13:41 +0000888 if( type==SQLITE_INTEGER ){
drh8c08e862006-02-11 17:34:00 +0000889 i64 v = sqlite3_value_int64(argv[0]);
890 p->rSum += v;
891 if( (p->approx|p->overflow)==0 ){
892 i64 iNewSum = p->iSum + v;
893 int s1 = p->iSum >> (sizeof(i64)*8-1);
894 int s2 = v >> (sizeof(i64)*8-1);
895 int s3 = iNewSum >> (sizeof(i64)*8-1);
896 p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
897 p->iSum = iNewSum;
drh29d72102006-02-09 22:13:41 +0000898 }
899 }else{
drh8c08e862006-02-11 17:34:00 +0000900 p->rSum += sqlite3_value_double(argv[0]);
drh29d72102006-02-09 22:13:41 +0000901 p->approx = 1;
drh3f219f42005-09-08 19:45:57 +0000902 }
drh739105c2002-05-29 23:22:23 +0000903 }
drhdd5baa92002-02-27 19:50:59 +0000904}
danielk19770ae8b832004-05-25 12:05:56 +0000905static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000906 SumCtx *p;
drhabfcea22005-09-06 20:36:48 +0000907 p = sqlite3_aggregate_context(context, 0);
drhc2bd9132005-09-08 20:37:43 +0000908 if( p && p->cnt>0 ){
drh8c08e862006-02-11 17:34:00 +0000909 if( p->overflow ){
910 sqlite3_result_error(context,"integer overflow",-1);
911 }else if( p->approx ){
912 sqlite3_result_double(context, p->rSum);
drhc2bd9132005-09-08 20:37:43 +0000913 }else{
drh8c08e862006-02-11 17:34:00 +0000914 sqlite3_result_int64(context, p->iSum);
drhc2bd9132005-09-08 20:37:43 +0000915 }
drh3d1d95e2005-09-08 10:37:01 +0000916 }
drhdd5baa92002-02-27 19:50:59 +0000917}
danielk19770ae8b832004-05-25 12:05:56 +0000918static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000919 SumCtx *p;
drhabfcea22005-09-06 20:36:48 +0000920 p = sqlite3_aggregate_context(context, 0);
drh739105c2002-05-29 23:22:23 +0000921 if( p && p->cnt>0 ){
drh8c08e862006-02-11 17:34:00 +0000922 sqlite3_result_double(context, p->rSum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000923 }
924}
drha97fdd32006-01-12 22:17:50 +0000925static void totalFinalize(sqlite3_context *context){
926 SumCtx *p;
927 p = sqlite3_aggregate_context(context, 0);
drh8c08e862006-02-11 17:34:00 +0000928 sqlite3_result_double(context, p ? p->rSum : 0.0);
drha97fdd32006-01-12 22:17:50 +0000929}
drhdd5baa92002-02-27 19:50:59 +0000930
931/*
drh0bce8352002-02-28 00:41:10 +0000932** The following structure keeps track of state information for the
933** count() aggregate function.
934*/
935typedef struct CountCtx CountCtx;
936struct CountCtx {
drhfc6ad392006-02-09 13:38:19 +0000937 i64 n;
drh0bce8352002-02-28 00:41:10 +0000938};
drhdd5baa92002-02-27 19:50:59 +0000939
drh0bce8352002-02-28 00:41:10 +0000940/*
941** Routines to implement the count() aggregate function.
942*/
danielk19770ae8b832004-05-25 12:05:56 +0000943static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000944 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000945 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000946 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000947 p->n++;
948 }
949}
danielk19770ae8b832004-05-25 12:05:56 +0000950static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000951 CountCtx *p;
drhabfcea22005-09-06 20:36:48 +0000952 p = sqlite3_aggregate_context(context, 0);
drhfc6ad392006-02-09 13:38:19 +0000953 sqlite3_result_int64(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000954}
955
956/*
drh0bce8352002-02-28 00:41:10 +0000957** Routines to implement min() and max() aggregate functions.
958*/
danielk19770ae8b832004-05-25 12:05:56 +0000959static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000960 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:32 +0000961 Mem *pBest;
962
963 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
964 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:04 +0000965 if( !pBest ) return;
drh268380c2004-02-25 13:47:31 +0000966
danielk197788208052004-05-25 01:13:20 +0000967 if( pBest->flags ){
drh9eb516c2004-07-18 20:52:32 +0000968 int max;
969 int cmp;
danielk1977dc1bdc42004-06-11 10:51:27 +0000970 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +0000971 /* This step function is used for both the min() and max() aggregates,
972 ** the only difference between the two being that the sense of the
973 ** comparison is inverted. For the max() aggregate, the
974 ** sqlite3_user_data() function returns (void *)-1. For min() it
975 ** returns (void *)db, where db is the sqlite3* database pointer.
976 ** Therefore the next statement sets variable 'max' to 1 for the max()
977 ** aggregate, or 0 for min().
978 */
danielk197788208052004-05-25 01:13:20 +0000979 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +0000980 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +0000981 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000982 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000983 }
drh268380c2004-02-25 13:47:31 +0000984 }else{
danielk19777e18c252004-05-25 11:47:24 +0000985 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000986 }
987}
danielk19770ae8b832004-05-25 12:05:56 +0000988static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000989 sqlite3_value *pRes;
drhabfcea22005-09-06 20:36:48 +0000990 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
991 if( pRes ){
992 if( pRes->flags ){
993 sqlite3_result_value(context, pRes);
994 }
995 sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:10 +0000996 }
997}
drhdd5baa92002-02-27 19:50:59 +0000998
drh4e5ffc52004-08-31 00:52:37 +0000999
drhd3a149e2002-02-24 17:12:53 +00001000/*
drha2ed5602002-02-26 23:55:31 +00001001** This function registered all of the above C functions as SQL
1002** functions. This should be the only routine in this file with
1003** external linkage.
drhdc04c582002-02-24 01:55:15 +00001004*/
drh9bb575f2004-09-06 17:24:11 +00001005void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
drh57196282004-10-06 15:41:16 +00001006 static const struct {
drh0bce8352002-02-28 00:41:10 +00001007 char *zName;
drh268380c2004-02-25 13:47:31 +00001008 signed char nArg;
danielk1977f4618892004-06-28 13:09:11 +00001009 u8 argType; /* 0: none. 1: db 2: (-1) */
1010 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +00001011 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001012 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +00001013 } aFuncs[] = {
danielk1977f4618892004-06-28 13:09:11 +00001014 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
1015 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
1016 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc },
1017 { "max", 0, 2, SQLITE_UTF8, 1, 0 },
1018 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
1019 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
1020 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
drh6c626082004-11-14 21:56:29 +00001021#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +00001022 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
drh6c626082004-11-14 21:56:29 +00001023#endif
danielk1977f4618892004-06-28 13:09:11 +00001024 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
1025 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
1026 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
1027 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
1028 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
1029 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
1030 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
1031 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
1032 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
1033 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
drh94a98362004-09-13 13:13:18 +00001034 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
danielk1977f4618892004-06-28 13:09:11 +00001035 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
1036 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
1037 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid },
1038 { "changes", 0, 1, SQLITE_UTF8, 0, changes },
1039 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes },
drh7f375902006-06-13 17:38:59 +00001040 { "match", 2, 0, SQLITE_UTF8, 0, matchStub },
drhd24cc422003-03-27 12:51:24 +00001041#ifdef SQLITE_SOUNDEX
danielk1977f4618892004-06-28 13:09:11 +00001042 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +00001043#endif
drhfdb83b22006-06-17 14:12:47 +00001044#ifndef SQLITE_OMIT_LOAD_EXTENSION
1045 { "load_extension", 1, 1, SQLITE_UTF8, 0, loadExt },
1046 { "load_extension", 2, 1, SQLITE_UTF8, 0, loadExt },
1047#endif
drh193a6b42002-07-07 16:52:46 +00001048#ifdef SQLITE_TEST
danielk1977f4618892004-06-28 13:09:11 +00001049 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
1050 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor},
danielk1977d8123362004-06-12 09:25:12 +00001051 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
danielk1977f4618892004-06-28 13:09:11 +00001052 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
danielk197701427a62005-01-11 13:02:33 +00001053 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
drh193a6b42002-07-07 16:52:46 +00001054#endif
drh0bce8352002-02-28 00:41:10 +00001055 };
drh57196282004-10-06 15:41:16 +00001056 static const struct {
drh0bce8352002-02-28 00:41:10 +00001057 char *zName;
drh268380c2004-02-25 13:47:31 +00001058 signed char nArg;
drh268380c2004-02-25 13:47:31 +00001059 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +00001060 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001061 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1062 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +00001063 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +00001064 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1065 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
1066 { "sum", 1, 0, 0, sumStep, sumFinalize },
drha97fdd32006-01-12 22:17:50 +00001067 { "total", 1, 0, 0, sumStep, totalFinalize },
danielk1977dc1bdc42004-06-11 10:51:27 +00001068 { "avg", 1, 0, 0, sumStep, avgFinalize },
1069 { "count", 0, 0, 0, countStep, countFinalize },
1070 { "count", 1, 0, 0, countStep, countFinalize },
drh0bce8352002-02-28 00:41:10 +00001071 };
1072 int i;
1073
1074 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001075 void *pArg = 0;
1076 switch( aFuncs[i].argType ){
1077 case 1: pArg = db; break;
1078 case 2: pArg = (void *)(-1); break;
1079 }
danielk1977771151b2006-01-17 13:21:40 +00001080 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
danielk1977f9d64d22004-06-19 08:18:07 +00001081 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001082 if( aFuncs[i].needCollSeq ){
1083 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1084 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1085 if( pFunc && aFuncs[i].needCollSeq ){
1086 pFunc->needCollSeq = 1;
1087 }
1088 }
drh0bce8352002-02-28 00:41:10 +00001089 }
drh1f01ec12005-02-15 21:36:18 +00001090#ifndef SQLITE_OMIT_ALTERTABLE
1091 sqlite3AlterFunctions(db);
1092#endif
drh198bf392006-01-06 21:52:49 +00001093#ifndef SQLITE_OMIT_PARSER
danielk1977f744bb52005-12-06 17:19:11 +00001094 sqlite3AttachFunctions(db);
drh198bf392006-01-06 21:52:49 +00001095#endif
drh0bce8352002-02-28 00:41:10 +00001096 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001097 void *pArg = 0;
1098 switch( aAggs[i].argType ){
1099 case 1: pArg = db; break;
1100 case 2: pArg = (void *)(-1); break;
1101 }
danielk1977771151b2006-01-17 13:21:40 +00001102 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001103 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +00001104 if( aAggs[i].needCollSeq ){
1105 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
danielk1977d8123362004-06-12 09:25:12 +00001106 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001107 if( pFunc && aAggs[i].needCollSeq ){
1108 pFunc->needCollSeq = 1;
1109 }
1110 }
drh268380c2004-02-25 13:47:31 +00001111 }
danielk19774adee202004-05-08 08:23:19 +00001112 sqlite3RegisterDateTimeFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001113#ifdef SQLITE_SSE
drh37527852006-03-16 16:19:56 +00001114 (void)sqlite3SseFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001115#endif
drh55ef4d92005-08-14 01:20:37 +00001116#ifdef SQLITE_CASE_SENSITIVE_LIKE
1117 sqlite3RegisterLikeFunctions(db, 1);
1118#else
1119 sqlite3RegisterLikeFunctions(db, 0);
1120#endif
1121}
1122
1123/*
1124** Set the LIKEOPT flag on the 2-argument function with the given name.
1125*/
drhd64fe2f2005-08-28 17:00:23 +00001126static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
drh55ef4d92005-08-14 01:20:37 +00001127 FuncDef *pDef;
1128 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
1129 if( pDef ){
drhd64fe2f2005-08-28 17:00:23 +00001130 pDef->flags = flagVal;
drh55ef4d92005-08-14 01:20:37 +00001131 }
1132}
1133
1134/*
1135** Register the built-in LIKE and GLOB functions. The caseSensitive
1136** parameter determines whether or not the LIKE operator is case
1137** sensitive. GLOB is always case sensitive.
1138*/
1139void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
1140 struct compareInfo *pInfo;
1141 if( caseSensitive ){
1142 pInfo = (struct compareInfo*)&likeInfoAlt;
1143 }else{
1144 pInfo = (struct compareInfo*)&likeInfoNorm;
1145 }
danielk1977771151b2006-01-17 13:21:40 +00001146 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1147 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1148 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
drh55ef4d92005-08-14 01:20:37 +00001149 (struct compareInfo*)&globInfo, likeFunc, 0,0);
drhd64fe2f2005-08-28 17:00:23 +00001150 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1151 setLikeOptFlag(db, "like",
1152 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
drh55ef4d92005-08-14 01:20:37 +00001153}
1154
1155/*
1156** pExpr points to an expression which implements a function. If
1157** it is appropriate to apply the LIKE optimization to that function
1158** then set aWc[0] through aWc[2] to the wildcard characters and
1159** return TRUE. If the function is not a LIKE-style function then
1160** return FALSE.
1161*/
drhd64fe2f2005-08-28 17:00:23 +00001162int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
drh55ef4d92005-08-14 01:20:37 +00001163 FuncDef *pDef;
1164 if( pExpr->op!=TK_FUNCTION ){
1165 return 0;
1166 }
1167 if( pExpr->pList->nExpr!=2 ){
1168 return 0;
1169 }
drh2646da72005-12-09 20:02:05 +00001170 pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
drh55ef4d92005-08-14 01:20:37 +00001171 SQLITE_UTF8, 0);
drhd64fe2f2005-08-28 17:00:23 +00001172 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
drh55ef4d92005-08-14 01:20:37 +00001173 return 0;
1174 }
1175
1176 /* The memcpy() statement assumes that the wildcard characters are
1177 ** the first three statements in the compareInfo structure. The
1178 ** asserts() that follow verify that assumption
1179 */
1180 memcpy(aWc, pDef->pUserData, 3);
1181 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
1182 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
1183 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
drhd64fe2f2005-08-28 17:00:23 +00001184 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
drh55ef4d92005-08-14 01:20:37 +00001185 return 1;
drhdc04c582002-02-24 01:55:15 +00001186}