blob: 919b3a83a41f64aac4dfe046b4fa96bc8b061122 [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**
drhd64fe2f2005-08-28 17:00:23 +000019** $Id: func.c,v 1.106 2005/08/28 17:00:23 drh Exp $
drhdc04c582002-02-24 01:55:15 +000020*/
drhb659e9b2005-01-28 01:29:08 +000021#include "sqliteInt.h"
drhdc04c582002-02-24 01:55:15 +000022#include <ctype.h>
drhd3a149e2002-02-24 17:12:53 +000023#include <math.h>
24#include <stdlib.h>
drh0bce8352002-02-28 00:41:10 +000025#include <assert.h>
danielk197788208052004-05-25 01:13:20 +000026#include "vdbeInt.h"
drh771d8c32003-08-09 21:32:28 +000027#include "os.h"
drh0bce8352002-02-28 00:41:10 +000028
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: {
drh4f26d6c2004-05-26 23:25:30 +0000104 const 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]);
124 if( iVal<0 ) iVal = iVal * -1;
125 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000126 break;
127 }
drh9c054832004-05-31 18:51:57 +0000128 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000129 sqlite3_result_null(context);
130 break;
131 }
132 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000133 double rVal = sqlite3_value_double(argv[0]);
134 if( rVal<0 ) rVal = rVal * -1.0;
135 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000136 break;
137 }
138 }
drh0bce8352002-02-28 00:41:10 +0000139}
140
141/*
142** Implementation of the substr() function
143*/
drhf9b596e2004-05-26 16:54:42 +0000144static void substrFunc(
145 sqlite3_context *context,
146 int argc,
147 sqlite3_value **argv
148){
drh0bce8352002-02-28 00:41:10 +0000149 const char *z;
drh0bce8352002-02-28 00:41:10 +0000150 const char *z2;
151 int i;
drh0bce8352002-02-28 00:41:10 +0000152 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000153
drh0bce8352002-02-28 00:41:10 +0000154 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000155 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000156 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000157 p1 = sqlite3_value_int(argv[1]);
158 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000159 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000160 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000161 p1 += len;
drh653bc752002-02-28 03:31:10 +0000162 if( p1<0 ){
163 p2 += p1;
164 p1 = 0;
165 }
drh0bce8352002-02-28 00:41:10 +0000166 }else if( p1>0 ){
167 p1--;
168 }
169 if( p1+p2>len ){
170 p2 = len-p1;
171 }
drh77396302004-01-02 13:17:48 +0000172 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000173 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000174 }
drh47c8a672002-02-28 04:00:12 +0000175 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000176 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000177 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000178 }
drh47c8a672002-02-28 04:00:12 +0000179 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000180 if( p2<0 ) p2 = 0;
danielk1977d8123362004-06-12 09:25:12 +0000181 sqlite3_result_text(context, &z[p1], p2, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000182}
183
184/*
185** Implementation of the round() function
186*/
danielk19770ae8b832004-05-25 12:05:56 +0000187static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000188 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000189 double r;
drh592ac8c2005-08-13 03:07:47 +0000190 char zBuf[500]; /* larger than the %f representation of the largest double */
drh0bce8352002-02-28 00:41:10 +0000191 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000192 if( argc==2 ){
drh9c054832004-05-31 18:51:57 +0000193 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000194 n = sqlite3_value_int(argv[1]);
195 if( n>30 ) n = 30;
196 if( n<0 ) n = 0;
197 }
drh9c054832004-05-31 18:51:57 +0000198 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000199 r = sqlite3_value_double(argv[0]);
drhe866fcb2005-07-09 02:38:06 +0000200 sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
danielk1977d8123362004-06-12 09:25:12 +0000201 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000202}
drhdc04c582002-02-24 01:55:15 +0000203
204/*
205** Implementation of the upper() and lower() SQL functions.
206*/
danielk19770ae8b832004-05-25 12:05:56 +0000207static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000208 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000209 int i;
drh9c054832004-05-31 18:51:57 +0000210 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000211 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000212 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000213 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000214 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000215 z[i] = toupper(z[i]);
drhdc04c582002-02-24 01:55:15 +0000216 }
danielk1977d8123362004-06-12 09:25:12 +0000217 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000218 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000219}
danielk19770ae8b832004-05-25 12:05:56 +0000220static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh8cd9db02004-07-18 23:06:53 +0000221 unsigned char *z;
drhdc04c582002-02-24 01:55:15 +0000222 int i;
drh9c054832004-05-31 18:51:57 +0000223 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000224 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000225 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000226 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000227 for(i=0; z[i]; i++){
drh4c755c02004-08-08 20:22:17 +0000228 z[i] = tolower(z[i]);
drhdc04c582002-02-24 01:55:15 +0000229 }
danielk1977d8123362004-06-12 09:25:12 +0000230 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
danielk19777e18c252004-05-25 11:47:24 +0000231 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000232}
233
234/*
drhfbc99082002-02-28 03:14:18 +0000235** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000236** All three do the same thing. They return the first non-NULL
237** argument.
drh3212e182002-02-28 00:46:26 +0000238*/
drhf9b596e2004-05-26 16:54:42 +0000239static void ifnullFunc(
240 sqlite3_context *context,
241 int argc,
242 sqlite3_value **argv
243){
drhfbc99082002-02-28 03:14:18 +0000244 int i;
245 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000246 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000247 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000248 break;
249 }
250 }
drh3212e182002-02-28 00:46:26 +0000251}
252
253/*
drhf9ffac92002-03-02 19:00:31 +0000254** Implementation of random(). Return a random integer.
255*/
drhf9b596e2004-05-26 16:54:42 +0000256static void randomFunc(
257 sqlite3_context *context,
258 int argc,
259 sqlite3_value **argv
260){
drhbbd82df2004-02-11 09:46:30 +0000261 int r;
danielk19774adee202004-05-08 08:23:19 +0000262 sqlite3Randomness(sizeof(r), &r);
drhf4479502004-05-27 03:12:53 +0000263 sqlite3_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000264}
265
266/*
drh6ed41ad2002-04-06 14:10:47 +0000267** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000268** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000269*/
danielk197751ad0ec2004-05-24 12:39:02 +0000270static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000271 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000272 int arg,
273 sqlite3_value **argv
274){
drh9bb575f2004-09-06 17:24:11 +0000275 sqlite3 *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000276 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000277}
278
rdcf146a772004-02-25 22:51:06 +0000279/*
danielk1977b28af712004-06-21 06:50:26 +0000280** Implementation of the changes() SQL function. The return value is the
281** same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000282*/
danielk1977b28af712004-06-21 06:50:26 +0000283static void changes(
drhf9b596e2004-05-26 16:54:42 +0000284 sqlite3_context *context,
285 int arg,
286 sqlite3_value **argv
287){
drh9bb575f2004-09-06 17:24:11 +0000288 sqlite3 *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000289 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000290}
rdcf146a772004-02-25 22:51:06 +0000291
292/*
danielk1977b28af712004-06-21 06:50:26 +0000293** Implementation of the total_changes() SQL function. The return value is
294** the same as the sqlite3_total_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000295*/
danielk1977b28af712004-06-21 06:50:26 +0000296static void total_changes(
297 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000298 int arg,
299 sqlite3_value **argv
300){
drh9bb575f2004-09-06 17:24:11 +0000301 sqlite3 *db = sqlite3_user_data(context);
danielk1977b28af712004-06-21 06:50:26 +0000302 sqlite3_result_int(context, sqlite3_total_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000303}
304
drh6ed41ad2002-04-06 14:10:47 +0000305/*
drh4e5ffc52004-08-31 00:52:37 +0000306** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03 +0000307*/
drh4e5ffc52004-08-31 00:52:37 +0000308struct compareInfo {
309 u8 matchAll;
310 u8 matchOne;
311 u8 matchSet;
312 u8 noCase;
danielk1977d02eb1f2004-06-06 09:44:03 +0000313};
drh55ef4d92005-08-14 01:20:37 +0000314
drh4e5ffc52004-08-31 00:52:37 +0000315static const struct compareInfo globInfo = { '*', '?', '[', 0 };
drh55ef4d92005-08-14 01:20:37 +0000316/* The correct SQL-92 behavior is for the LIKE operator to ignore
317** case. Thus 'a' LIKE 'A' would be true. */
318static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
319/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
320** is case sensitive causing 'a' LIKE 'A' to be false */
321static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
danielk1977d02eb1f2004-06-06 09:44:03 +0000322
323/*
drh4e5ffc52004-08-31 00:52:37 +0000324** X is a pointer to the first byte of a UTF-8 character. Increment
325** X so that it points to the next character. This only works right
326** if X points to a well-formed UTF-8 string.
danielk1977d02eb1f2004-06-06 09:44:03 +0000327*/
drh4e5ffc52004-08-31 00:52:37 +0000328#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
329#define sqliteCharVal(X) sqlite3ReadUtf8(X)
danielk1977d02eb1f2004-06-06 09:44:03 +0000330
danielk1977d02eb1f2004-06-06 09:44:03 +0000331
332/*
drh4e5ffc52004-08-31 00:52:37 +0000333** Compare two UTF-8 strings for equality where the first string can
334** potentially be a "glob" expression. Return true (1) if they
335** are the same and false (0) if they are different.
drh0ac65892002-04-20 14:24:41 +0000336**
drh4e5ffc52004-08-31 00:52:37 +0000337** Globbing rules:
drh0ac65892002-04-20 14:24:41 +0000338**
drh4e5ffc52004-08-31 00:52:37 +0000339** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03 +0000340**
drh4e5ffc52004-08-31 00:52:37 +0000341** '?' Matches exactly one character.
342**
343** [...] Matches one character from the enclosed list of
344** characters.
345**
346** [^...] Matches one character not in the enclosed list.
347**
348** With the [...] and [^...] matching, a ']' character can be included
349** in the list by making it the first character after '[' or '^'. A
350** range of characters can be specified using '-'. Example:
351** "[a-z]" matches any single lower-case letter. To match a '-', make
352** it the last character in the list.
353**
354** This routine is usually quick, but can be N**2 in the worst case.
355**
356** Hints: to match '*' or '?', put them in "[]". Like this:
357**
358** abc[*]xyz Matches "abc*xyz" only
drh0ac65892002-04-20 14:24:41 +0000359*/
danielk19777c6303c2004-11-17 16:41:29 +0000360static int patternCompare(
drh4e5ffc52004-08-31 00:52:37 +0000361 const u8 *zPattern, /* The glob pattern */
362 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29 +0000363 const struct compareInfo *pInfo, /* Information about how to do the compare */
364 const int esc /* The escape character */
danielk197751ad0ec2004-05-24 12:39:02 +0000365){
danielk1977ad7dd422004-06-06 12:41:49 +0000366 register int c;
drh4e5ffc52004-08-31 00:52:37 +0000367 int invert;
368 int seen;
369 int c2;
370 u8 matchOne = pInfo->matchOne;
371 u8 matchAll = pInfo->matchAll;
372 u8 matchSet = pInfo->matchSet;
373 u8 noCase = pInfo->noCase;
danielk19777c6303c2004-11-17 16:41:29 +0000374 int prevEscape = 0; /* True if the previous character was 'escape' */
danielk1977d02eb1f2004-06-06 09:44:03 +0000375
drh4e5ffc52004-08-31 00:52:37 +0000376 while( (c = *zPattern)!=0 ){
danielk19777c6303c2004-11-17 16:41:29 +0000377 if( !prevEscape && c==matchAll ){
drh4e5ffc52004-08-31 00:52:37 +0000378 while( (c=zPattern[1]) == matchAll || c == matchOne ){
379 if( c==matchOne ){
380 if( *zString==0 ) return 0;
381 sqliteNextChar(zString);
382 }
383 zPattern++;
danielk1977ad7dd422004-06-06 12:41:49 +0000384 }
drh20fc0882004-11-18 13:49:25 +0000385 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
danielk19777c6303c2004-11-17 16:41:29 +0000386 u8 const *zTemp = &zPattern[1];
387 sqliteNextChar(zTemp);
388 c = *zTemp;
389 }
drh4e5ffc52004-08-31 00:52:37 +0000390 if( c==0 ) return 1;
391 if( c==matchSet ){
danielk19777c6303c2004-11-17 16:41:29 +0000392 assert( esc==0 ); /* This is GLOB, not LIKE */
393 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
drh4e5ffc52004-08-31 00:52:37 +0000394 sqliteNextChar(zString);
395 }
396 return *zString!=0;
397 }else{
398 while( (c2 = *zString)!=0 ){
399 if( noCase ){
400 c2 = sqlite3UpperToLower[c2];
401 c = sqlite3UpperToLower[c];
402 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
403 }else{
404 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
405 }
406 if( c2==0 ) return 0;
danielk19777c6303c2004-11-17 16:41:29 +0000407 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
drh4e5ffc52004-08-31 00:52:37 +0000408 sqliteNextChar(zString);
409 }
410 return 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000411 }
danielk19777c6303c2004-11-17 16:41:29 +0000412 }else if( !prevEscape && c==matchOne ){
drh4e5ffc52004-08-31 00:52:37 +0000413 if( *zString==0 ) return 0;
414 sqliteNextChar(zString);
415 zPattern++;
416 }else if( c==matchSet ){
417 int prior_c = 0;
danielk19777c6303c2004-11-17 16:41:29 +0000418 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
drh4e5ffc52004-08-31 00:52:37 +0000419 seen = 0;
420 invert = 0;
421 c = sqliteCharVal(zString);
422 if( c==0 ) return 0;
423 c2 = *++zPattern;
424 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
425 if( c2==']' ){
426 if( c==']' ) seen = 1;
427 c2 = *++zPattern;
428 }
429 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
430 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
431 zPattern++;
432 c2 = sqliteCharVal(zPattern);
433 if( c>=prior_c && c<=c2 ) seen = 1;
434 prior_c = 0;
435 }else if( c==c2 ){
436 seen = 1;
437 prior_c = c2;
438 }else{
439 prior_c = c2;
440 }
441 sqliteNextChar(zPattern);
442 }
443 if( c2==0 || (seen ^ invert)==0 ) return 0;
444 sqliteNextChar(zString);
445 zPattern++;
drh20fc0882004-11-18 13:49:25 +0000446 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
danielk19777c6303c2004-11-17 16:41:29 +0000447 prevEscape = 1;
448 sqliteNextChar(zPattern);
drh4e5ffc52004-08-31 00:52:37 +0000449 }else{
450 if( noCase ){
451 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
452 }else{
453 if( c != *zString ) return 0;
454 }
455 zPattern++;
456 zString++;
danielk19777c6303c2004-11-17 16:41:29 +0000457 prevEscape = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000458 }
danielk197751ad0ec2004-05-24 12:39:02 +0000459 }
drh4e5ffc52004-08-31 00:52:37 +0000460 return *zString==0;
drh0ac65892002-04-20 14:24:41 +0000461}
drh4e5ffc52004-08-31 00:52:37 +0000462
drh55ef4d92005-08-14 01:20:37 +0000463/*
464** Count the number of times that the LIKE operator (or GLOB which is
465** just a variation of LIKE) gets called. This is used for testing
466** only.
467*/
468#ifdef SQLITE_TEST
469int sqlite3_like_count = 0;
470#endif
471
danielk19773f6b0872004-06-17 05:36:44 +0000472
473/*
474** Implementation of the like() SQL function. This function implements
475** the build-in LIKE operator. The first argument to the function is the
476** pattern and the second argument is the string. So, the SQL statements:
477**
478** A LIKE B
479**
480** is implemented as like(B,A).
481**
drh55ef4d92005-08-14 01:20:37 +0000482** This same function (with a different compareInfo structure) computes
483** the GLOB operator.
danielk19773f6b0872004-06-17 05:36:44 +0000484*/
485static void likeFunc(
486 sqlite3_context *context,
487 int argc,
488 sqlite3_value **argv
489){
490 const unsigned char *zA = sqlite3_value_text(argv[0]);
491 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk19777c6303c2004-11-17 16:41:29 +0000492 int escape = 0;
493 if( argc==3 ){
494 /* The escape character string must consist of a single UTF-8 character.
495 ** Otherwise, return an error.
496 */
497 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
498 if( sqlite3utf8CharLen(zEsc, -1)!=1 ){
499 sqlite3_result_error(context,
500 "ESCAPE expression must be a single character", -1);
501 return;
502 }
503 escape = sqlite3ReadUtf8(zEsc);
504 }
danielk19773f6b0872004-06-17 05:36:44 +0000505 if( zA && zB ){
drh55ef4d92005-08-14 01:20:37 +0000506 struct compareInfo *pInfo = sqlite3_user_data(context);
507#ifdef SQLITE_TEST
508 sqlite3_like_count++;
509#endif
510 sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape));
danielk197751ad0ec2004-05-24 12:39:02 +0000511 }
drh8912d102002-05-26 21:34:58 +0000512}
513
514/*
515** Implementation of the NULLIF(x,y) function. The result is the first
516** argument if the arguments are different. The result is NULL if the
517** arguments are equal to each other.
518*/
drhf9b596e2004-05-26 16:54:42 +0000519static void nullifFunc(
520 sqlite3_context *context,
521 int argc,
522 sqlite3_value **argv
523){
danielk1977dc1bdc42004-06-11 10:51:27 +0000524 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
525 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000526 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000527 }
drh0ac65892002-04-20 14:24:41 +0000528}
529
drh647cb0e2002-11-04 19:32:25 +0000530/*
531** Implementation of the VERSION(*) function. The result is the version
532** of the SQLite library that is running.
533*/
drhf9b596e2004-05-26 16:54:42 +0000534static void versionFunc(
535 sqlite3_context *context,
536 int argc,
537 sqlite3_value **argv
538){
danielk1977d8123362004-06-12 09:25:12 +0000539 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:25 +0000540}
541
danielk1977d641d642004-11-18 15:44:29 +0000542
drh47394702003-08-20 01:03:33 +0000543/*
544** EXPERIMENTAL - This is not an official function. The interface may
545** change. This function may disappear. Do not write code that depends
546** on this function.
547**
548** Implementation of the QUOTE() function. This function takes a single
549** argument. If the argument is numeric, the return value is the same as
550** the argument. If the argument is NULL, the return value is the string
551** "NULL". Otherwise, the argument is enclosed in single quotes with
552** single-quote escapes.
553*/
danielk19770ae8b832004-05-25 12:05:56 +0000554static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000555 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000556 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000557 case SQLITE_NULL: {
danielk1977d8123362004-06-12 09:25:12 +0000558 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
drhf9b596e2004-05-26 16:54:42 +0000559 break;
drh47394702003-08-20 01:03:33 +0000560 }
drh9c054832004-05-31 18:51:57 +0000561 case SQLITE_INTEGER:
562 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000563 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000564 break;
565 }
danielk19773f41e972004-06-08 00:39:01 +0000566 case SQLITE_BLOB: {
567 static const char hexdigits[] = {
568 '0', '1', '2', '3', '4', '5', '6', '7',
569 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
570 };
571 char *zText = 0;
572 int nBlob = sqlite3_value_bytes(argv[0]);
573 char const *zBlob = sqlite3_value_blob(argv[0]);
574
575 zText = (char *)sqliteMalloc((2*nBlob)+4);
576 if( !zText ){
577 sqlite3_result_error(context, "out of memory", -1);
578 }else{
579 int i;
580 for(i=0; i<nBlob; i++){
581 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
582 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
583 }
584 zText[(nBlob*2)+2] = '\'';
585 zText[(nBlob*2)+3] = '\0';
586 zText[0] = 'X';
587 zText[1] = '\'';
danielk1977d8123362004-06-12 09:25:12 +0000588 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
danielk19773f41e972004-06-08 00:39:01 +0000589 sqliteFree(zText);
590 }
591 break;
592 }
drh9c054832004-05-31 18:51:57 +0000593 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000594 int i,j,n;
drh4f26d6c2004-05-26 23:25:30 +0000595 const char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000596 char *z;
597
598 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
599 z = sqliteMalloc( i+n+3 );
600 if( z==0 ) return;
601 z[0] = '\'';
602 for(i=0, j=1; zArg[i]; i++){
603 z[j++] = zArg[i];
604 if( zArg[i]=='\'' ){
605 z[j++] = '\'';
606 }
607 }
608 z[j++] = '\'';
609 z[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000610 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
drhf9b596e2004-05-26 16:54:42 +0000611 sqliteFree(z);
612 }
drh47394702003-08-20 01:03:33 +0000613 }
614}
615
drhd24cc422003-03-27 12:51:24 +0000616#ifdef SQLITE_SOUNDEX
617/*
618** Compute the soundex encoding of a word.
619*/
danielk19770ae8b832004-05-25 12:05:56 +0000620static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000621 char zResult[8];
drh4c755c02004-08-08 20:22:17 +0000622 const u8 *zIn;
drhd24cc422003-03-27 12:51:24 +0000623 int i, j;
624 static const unsigned char iCode[] = {
625 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
626 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
627 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
629 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
630 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
631 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
632 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
633 };
634 assert( argc==1 );
drh4c755c02004-08-08 20:22:17 +0000635 zIn = (u8*)sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000636 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
637 if( zIn[i] ){
638 zResult[0] = toupper(zIn[i]);
639 for(j=1; j<4 && zIn[i]; i++){
640 int code = iCode[zIn[i]&0x7f];
641 if( code>0 ){
642 zResult[j++] = code + '0';
643 }
644 }
645 while( j<4 ){
646 zResult[j++] = '0';
647 }
648 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000649 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:24 +0000650 }else{
danielk1977d8123362004-06-12 09:25:12 +0000651 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:24 +0000652 }
653}
654#endif
655
drh193a6b42002-07-07 16:52:46 +0000656#ifdef SQLITE_TEST
657/*
658** This function generates a string of random characters. Used for
659** generating test data.
660*/
danielk19770ae8b832004-05-25 12:05:56 +0000661static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000662 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000663 "abcdefghijklmnopqrstuvwxyz"
664 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
665 "0123456789"
666 ".-!,:*^+=_|?/<> ";
667 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000668 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000669 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000670 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000671 if( iMin<0 ) iMin = 0;
672 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
673 }else{
674 iMin = 1;
675 }
676 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000677 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000678 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000679 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000680 }else{
681 iMax = 50;
682 }
683 n = iMin;
684 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000685 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000686 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000687 n += r%(iMax + 1 - iMin);
688 }
drh1dba7272004-01-16 13:58:18 +0000689 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000690 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000691 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000692 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000693 }
694 zBuf[n] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000695 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT);
696}
drh0e3d7472004-06-19 17:33:07 +0000697#endif /* SQLITE_TEST */
danielk1977d8123362004-06-12 09:25:12 +0000698
drh0e3d7472004-06-19 17:33:07 +0000699#ifdef SQLITE_TEST
danielk1977d8123362004-06-12 09:25:12 +0000700/*
701** The following two SQL functions are used to test returning a text
702** result with a destructor. Function 'test_destructor' takes one argument
703** and returns the same argument interpreted as TEXT. A destructor is
704** passed with the sqlite3_result_text() call.
705**
706** SQL function 'test_destructor_count' returns the number of outstanding
707** allocations made by 'test_destructor';
708**
709** WARNING: Not threadsafe.
710*/
711static int test_destructor_count_var = 0;
712static void destructor(void *p){
713 char *zVal = (char *)p;
714 assert(zVal);
715 zVal--;
716 sqliteFree(zVal);
717 test_destructor_count_var--;
718}
719static void test_destructor(
720 sqlite3_context *pCtx,
721 int nArg,
722 sqlite3_value **argv
723){
724 char *zVal;
danielk1977f4618892004-06-28 13:09:11 +0000725 int len;
drh9bb575f2004-09-06 17:24:11 +0000726 sqlite3 *db = sqlite3_user_data(pCtx);
danielk1977f4618892004-06-28 13:09:11 +0000727
danielk1977d8123362004-06-12 09:25:12 +0000728 test_destructor_count_var++;
729 assert( nArg==1 );
730 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
danielk1977f4618892004-06-28 13:09:11 +0000731 len = sqlite3ValueBytes(argv[0], db->enc);
732 zVal = sqliteMalloc(len+3);
733 zVal[len] = 0;
734 zVal[len-1] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000735 assert( zVal );
736 zVal++;
danielk1977f4618892004-06-28 13:09:11 +0000737 memcpy(zVal, sqlite3ValueText(argv[0], db->enc), len);
738 if( db->enc==SQLITE_UTF8 ){
739 sqlite3_result_text(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000740#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +0000741 }else if( db->enc==SQLITE_UTF16LE ){
742 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
743 }else{
744 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000745#endif /* SQLITE_OMIT_UTF16 */
danielk1977f4618892004-06-28 13:09:11 +0000746 }
danielk1977d8123362004-06-12 09:25:12 +0000747}
748static void test_destructor_count(
749 sqlite3_context *pCtx,
750 int nArg,
751 sqlite3_value **argv
752){
753 sqlite3_result_int(pCtx, test_destructor_count_var);
drh193a6b42002-07-07 16:52:46 +0000754}
drh0e3d7472004-06-19 17:33:07 +0000755#endif /* SQLITE_TEST */
danielk19773f6b0872004-06-17 05:36:44 +0000756
drh0e3d7472004-06-19 17:33:07 +0000757#ifdef SQLITE_TEST
758/*
759** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
760** interface.
761**
762** The test_auxdata() SQL function attempts to register each of its arguments
763** as auxiliary data. If there are no prior registrations of aux data for
764** that argument (meaning the argument is not a constant or this is its first
765** call) then the result for that argument is 0. If there is a prior
766** registration, the result for that argument is 1. The overall result
767** is the individual argument results separated by spaces.
768*/
danielk19773f6b0872004-06-17 05:36:44 +0000769static void free_test_auxdata(void *p) {sqliteFree(p);}
770static void test_auxdata(
771 sqlite3_context *pCtx,
772 int nArg,
773 sqlite3_value **argv
774){
775 int i;
776 char *zRet = sqliteMalloc(nArg*2);
777 if( !zRet ) return;
778 for(i=0; i<nArg; i++){
779 char const *z = sqlite3_value_text(argv[i]);
780 if( z ){
781 char *zAux = sqlite3_get_auxdata(pCtx, i);
782 if( zAux ){
783 zRet[i*2] = '1';
784 if( strcmp(zAux, z) ){
785 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
786 return;
787 }
788 }else{
789 zRet[i*2] = '0';
790 zAux = sqliteStrDup(z);
791 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
792 }
793 zRet[i*2+1] = ' ';
794 }
795 }
796 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
797}
drh0e3d7472004-06-19 17:33:07 +0000798#endif /* SQLITE_TEST */
drh193a6b42002-07-07 16:52:46 +0000799
danielk197701427a62005-01-11 13:02:33 +0000800#ifdef SQLITE_TEST
801/*
802** A function to test error reporting from user functions. This function
803** returns a copy of it's first argument as an error.
804*/
805static void test_error(
806 sqlite3_context *pCtx,
807 int nArg,
808 sqlite3_value **argv
809){
danielk197724c8ab82005-02-09 01:40:23 +0000810 sqlite3_result_error(pCtx, sqlite3_value_text(argv[0]), 0);
danielk197701427a62005-01-11 13:02:33 +0000811}
812#endif /* SQLITE_TEST */
813
drh0ac65892002-04-20 14:24:41 +0000814/*
drhd3a149e2002-02-24 17:12:53 +0000815** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000816** sum() or avg() aggregate computation.
817*/
818typedef struct SumCtx SumCtx;
819struct SumCtx {
820 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000821 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000822};
823
824/*
825** Routines used to compute the sum or average.
826*/
danielk19770ae8b832004-05-25 12:05:56 +0000827static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000828 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000829 if( argc<1 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000830 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000831 if( p && SQLITE_NULL!=sqlite3_value_type(argv[0]) ){
drh4f26d6c2004-05-26 23:25:30 +0000832 p->sum += sqlite3_value_double(argv[0]);
drh739105c2002-05-29 23:22:23 +0000833 p->cnt++;
834 }
drhdd5baa92002-02-27 19:50:59 +0000835}
danielk19770ae8b832004-05-25 12:05:56 +0000836static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000837 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000838 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000839 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000840}
danielk19770ae8b832004-05-25 12:05:56 +0000841static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000842 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000843 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000844 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000845 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000846 }
847}
848
849/*
850** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000851** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000852*/
853typedef struct StdDevCtx StdDevCtx;
854struct StdDevCtx {
855 double sum; /* Sum of terms */
856 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000857 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000858};
859
drh0bce8352002-02-28 00:41:10 +0000860/*
861** The following structure keeps track of state information for the
862** count() aggregate function.
863*/
864typedef struct CountCtx CountCtx;
865struct CountCtx {
866 int n;
867};
drhdd5baa92002-02-27 19:50:59 +0000868
drh0bce8352002-02-28 00:41:10 +0000869/*
870** Routines to implement the count() aggregate function.
871*/
danielk19770ae8b832004-05-25 12:05:56 +0000872static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000873 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000874 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000875 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000876 p->n++;
877 }
878}
danielk19770ae8b832004-05-25 12:05:56 +0000879static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000880 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000881 p = sqlite3_aggregate_context(context, sizeof(*p));
drhf4479502004-05-27 03:12:53 +0000882 sqlite3_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000883}
884
885/*
drh0bce8352002-02-28 00:41:10 +0000886** Routines to implement min() and max() aggregate functions.
887*/
danielk19770ae8b832004-05-25 12:05:56 +0000888static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000889 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:32 +0000890 Mem *pBest;
891
892 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
893 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:04 +0000894 if( !pBest ) return;
drh268380c2004-02-25 13:47:31 +0000895
danielk197788208052004-05-25 01:13:20 +0000896 if( pBest->flags ){
drh9eb516c2004-07-18 20:52:32 +0000897 int max;
898 int cmp;
danielk1977dc1bdc42004-06-11 10:51:27 +0000899 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +0000900 /* This step function is used for both the min() and max() aggregates,
901 ** the only difference between the two being that the sense of the
902 ** comparison is inverted. For the max() aggregate, the
903 ** sqlite3_user_data() function returns (void *)-1. For min() it
904 ** returns (void *)db, where db is the sqlite3* database pointer.
905 ** Therefore the next statement sets variable 'max' to 1 for the max()
906 ** aggregate, or 0 for min().
907 */
danielk197788208052004-05-25 01:13:20 +0000908 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +0000909 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +0000910 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000911 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000912 }
drh268380c2004-02-25 13:47:31 +0000913 }else{
danielk19777e18c252004-05-25 11:47:24 +0000914 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000915 }
916}
danielk19770ae8b832004-05-25 12:05:56 +0000917static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000918 sqlite3_value *pRes;
drh4f26d6c2004-05-26 23:25:30 +0000919 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +0000920 if( pRes->flags ){
drhf4479502004-05-27 03:12:53 +0000921 sqlite3_result_value(context, pRes);
drh0bce8352002-02-28 00:41:10 +0000922 }
danielk1977b20e56b2004-06-15 13:36:30 +0000923 sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:10 +0000924}
drhdd5baa92002-02-27 19:50:59 +0000925
drh4e5ffc52004-08-31 00:52:37 +0000926
drhd3a149e2002-02-24 17:12:53 +0000927/*
drha2ed5602002-02-26 23:55:31 +0000928** This function registered all of the above C functions as SQL
929** functions. This should be the only routine in this file with
930** external linkage.
drhdc04c582002-02-24 01:55:15 +0000931*/
drh9bb575f2004-09-06 17:24:11 +0000932void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
drh57196282004-10-06 15:41:16 +0000933 static const struct {
drh0bce8352002-02-28 00:41:10 +0000934 char *zName;
drh268380c2004-02-25 13:47:31 +0000935 signed char nArg;
danielk1977f4618892004-06-28 13:09:11 +0000936 u8 argType; /* 0: none. 1: db 2: (-1) */
937 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +0000938 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000939 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000940 } aFuncs[] = {
danielk1977f4618892004-06-28 13:09:11 +0000941 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
942 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
943 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc },
944 { "max", 0, 2, SQLITE_UTF8, 1, 0 },
945 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
946 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
947 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
drh6c626082004-11-14 21:56:29 +0000948#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +0000949 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
drh6c626082004-11-14 21:56:29 +0000950#endif
danielk1977f4618892004-06-28 13:09:11 +0000951 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
952 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
953 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
954 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
955 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
956 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
957 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
958 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
959 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
960 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
drh94a98362004-09-13 13:13:18 +0000961 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
danielk1977f4618892004-06-28 13:09:11 +0000962 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
963 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
964 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid },
965 { "changes", 0, 1, SQLITE_UTF8, 0, changes },
966 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes },
drhd24cc422003-03-27 12:51:24 +0000967#ifdef SQLITE_SOUNDEX
danielk1977f4618892004-06-28 13:09:11 +0000968 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000969#endif
drh193a6b42002-07-07 16:52:46 +0000970#ifdef SQLITE_TEST
danielk1977f4618892004-06-28 13:09:11 +0000971 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
972 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor},
danielk1977d8123362004-06-12 09:25:12 +0000973 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
danielk1977f4618892004-06-28 13:09:11 +0000974 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
danielk197701427a62005-01-11 13:02:33 +0000975 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
drh193a6b42002-07-07 16:52:46 +0000976#endif
drh0bce8352002-02-28 00:41:10 +0000977 };
drh57196282004-10-06 15:41:16 +0000978 static const struct {
drh0bce8352002-02-28 00:41:10 +0000979 char *zName;
drh268380c2004-02-25 13:47:31 +0000980 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000981 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +0000982 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000983 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
984 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +0000985 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +0000986 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
987 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
988 { "sum", 1, 0, 0, sumStep, sumFinalize },
989 { "avg", 1, 0, 0, sumStep, avgFinalize },
990 { "count", 0, 0, 0, countStep, countFinalize },
991 { "count", 1, 0, 0, countStep, countFinalize },
drh0bce8352002-02-28 00:41:10 +0000992 };
993 int i;
994
995 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000996 void *pArg = 0;
997 switch( aFuncs[i].argType ){
998 case 1: pArg = db; break;
999 case 2: pArg = (void *)(-1); break;
1000 }
danielk1977ad7dd422004-06-06 12:41:49 +00001001 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
danielk1977f9d64d22004-06-19 08:18:07 +00001002 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001003 if( aFuncs[i].needCollSeq ){
1004 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1005 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1006 if( pFunc && aFuncs[i].needCollSeq ){
1007 pFunc->needCollSeq = 1;
1008 }
1009 }
drh0bce8352002-02-28 00:41:10 +00001010 }
drh1f01ec12005-02-15 21:36:18 +00001011#ifndef SQLITE_OMIT_ALTERTABLE
1012 sqlite3AlterFunctions(db);
1013#endif
drh0bce8352002-02-28 00:41:10 +00001014 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001015 void *pArg = 0;
1016 switch( aAggs[i].argType ){
1017 case 1: pArg = db; break;
1018 case 2: pArg = (void *)(-1); break;
1019 }
danielk1977d8123362004-06-12 09:25:12 +00001020 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001021 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +00001022 if( aAggs[i].needCollSeq ){
1023 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
danielk1977d8123362004-06-12 09:25:12 +00001024 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001025 if( pFunc && aAggs[i].needCollSeq ){
1026 pFunc->needCollSeq = 1;
1027 }
1028 }
drh268380c2004-02-25 13:47:31 +00001029 }
danielk19774adee202004-05-08 08:23:19 +00001030 sqlite3RegisterDateTimeFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001031#ifdef SQLITE_SSE
drh55ef4d92005-08-14 01:20:37 +00001032 sqlite3SseFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001033#endif
drh55ef4d92005-08-14 01:20:37 +00001034#ifdef SQLITE_CASE_SENSITIVE_LIKE
1035 sqlite3RegisterLikeFunctions(db, 1);
1036#else
1037 sqlite3RegisterLikeFunctions(db, 0);
1038#endif
1039}
1040
1041/*
1042** Set the LIKEOPT flag on the 2-argument function with the given name.
1043*/
drhd64fe2f2005-08-28 17:00:23 +00001044static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
drh55ef4d92005-08-14 01:20:37 +00001045 FuncDef *pDef;
1046 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
1047 if( pDef ){
drhd64fe2f2005-08-28 17:00:23 +00001048 pDef->flags = flagVal;
drh55ef4d92005-08-14 01:20:37 +00001049 }
1050}
1051
1052/*
1053** Register the built-in LIKE and GLOB functions. The caseSensitive
1054** parameter determines whether or not the LIKE operator is case
1055** sensitive. GLOB is always case sensitive.
1056*/
1057void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
1058 struct compareInfo *pInfo;
1059 if( caseSensitive ){
1060 pInfo = (struct compareInfo*)&likeInfoAlt;
1061 }else{
1062 pInfo = (struct compareInfo*)&likeInfoNorm;
1063 }
1064 sqlite3_create_function(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1065 sqlite3_create_function(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1066 sqlite3_create_function(db, "glob", 2, SQLITE_UTF8,
1067 (struct compareInfo*)&globInfo, likeFunc, 0,0);
drhd64fe2f2005-08-28 17:00:23 +00001068 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1069 setLikeOptFlag(db, "like",
1070 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
drh55ef4d92005-08-14 01:20:37 +00001071}
1072
1073/*
1074** pExpr points to an expression which implements a function. If
1075** it is appropriate to apply the LIKE optimization to that function
1076** then set aWc[0] through aWc[2] to the wildcard characters and
1077** return TRUE. If the function is not a LIKE-style function then
1078** return FALSE.
1079*/
drhd64fe2f2005-08-28 17:00:23 +00001080int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
drh55ef4d92005-08-14 01:20:37 +00001081 FuncDef *pDef;
1082 if( pExpr->op!=TK_FUNCTION ){
1083 return 0;
1084 }
1085 if( pExpr->pList->nExpr!=2 ){
1086 return 0;
1087 }
1088 pDef = sqlite3FindFunction(db, pExpr->token.z, pExpr->token.n, 2,
1089 SQLITE_UTF8, 0);
drhd64fe2f2005-08-28 17:00:23 +00001090 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
drh55ef4d92005-08-14 01:20:37 +00001091 return 0;
1092 }
1093
1094 /* The memcpy() statement assumes that the wildcard characters are
1095 ** the first three statements in the compareInfo structure. The
1096 ** asserts() that follow verify that assumption
1097 */
1098 memcpy(aWc, pDef->pUserData, 3);
1099 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
1100 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
1101 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
drhd64fe2f2005-08-28 17:00:23 +00001102 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
drh55ef4d92005-08-14 01:20:37 +00001103 return 1;
drhdc04c582002-02-24 01:55:15 +00001104}