blob: cfd377eeb79d9e96f238c88d15d0696224ebd85f [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**
drh26b6d902007-03-17 13:27:54 +000019** $Id: func.c,v 1.137 2007/03/17 13:27:55 drh Exp $
drhdc04c582002-02-24 01:55:15 +000020*/
drhb659e9b2005-01-28 01:29:08 +000021#include "sqliteInt.h"
drhdc04c582002-02-24 01:55:15 +000022#include <ctype.h>
drhb37df7b2005-10-13 02:09:49 +000023/* #include <math.h> */
drhd3a149e2002-02-24 17:12:53 +000024#include <stdlib.h>
drh0bce8352002-02-28 00:41:10 +000025#include <assert.h>
danielk197788208052004-05-25 01:13:20 +000026#include "vdbeInt.h"
drh771d8c32003-08-09 21:32:28 +000027#include "os.h"
drh0bce8352002-02-28 00:41:10 +000028
drh55ef4d92005-08-14 01:20:37 +000029/*
30** Return the collating function associated with a function.
31*/
danielk1977dc1bdc42004-06-11 10:51:27 +000032static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
33 return context->pColl;
34}
35
drh0bce8352002-02-28 00:41:10 +000036/*
37** Implementation of the non-aggregate min() and max() functions
38*/
drhf9b596e2004-05-26 16:54:42 +000039static void minmaxFunc(
40 sqlite3_context *context,
41 int argc,
42 sqlite3_value **argv
43){
drh0bce8352002-02-28 00:41:10 +000044 int i;
drh268380c2004-02-25 13:47:31 +000045 int mask; /* 0 for min() or 0xffffffff for max() */
drhf9b596e2004-05-26 16:54:42 +000046 int iBest;
danielk1977dc1bdc42004-06-11 10:51:27 +000047 CollSeq *pColl;
drh0bce8352002-02-28 00:41:10 +000048
drh89425d52002-02-28 03:04:48 +000049 if( argc==0 ) return;
drhc44af712004-09-02 15:53:56 +000050 mask = sqlite3_user_data(context)==0 ? 0 : -1;
danielk1977dc1bdc42004-06-11 10:51:27 +000051 pColl = sqlite3GetFuncCollSeq(context);
52 assert( pColl );
danielk1977c572ef72004-05-27 09:28:41 +000053 assert( mask==-1 || mask==0 );
drhf9b596e2004-05-26 16:54:42 +000054 iBest = 0;
drh9c054832004-05-31 18:51:57 +000055 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
drhf9b596e2004-05-26 16:54:42 +000056 for(i=1; i<argc; i++){
drh9c054832004-05-31 18:51:57 +000057 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
danielk1977dc1bdc42004-06-11 10:51:27 +000058 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
drhf9b596e2004-05-26 16:54:42 +000059 iBest = i;
drh0bce8352002-02-28 00:41:10 +000060 }
61 }
drhf4479502004-05-27 03:12:53 +000062 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:10 +000063}
drh0bce8352002-02-28 00:41:10 +000064
drh268380c2004-02-25 13:47:31 +000065/*
66** Return the type of the argument.
67*/
drhf9b596e2004-05-26 16:54:42 +000068static void typeofFunc(
69 sqlite3_context *context,
70 int argc,
71 sqlite3_value **argv
72){
danielk197735bb9d02004-05-24 12:55:54 +000073 const char *z = 0;
danielk197735bb9d02004-05-24 12:55:54 +000074 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000075 case SQLITE_NULL: z = "null"; break;
76 case SQLITE_INTEGER: z = "integer"; break;
77 case SQLITE_TEXT: z = "text"; break;
78 case SQLITE_FLOAT: z = "real"; break;
79 case SQLITE_BLOB: z = "blob"; break;
danielk197735bb9d02004-05-24 12:55:54 +000080 }
danielk1977d8123362004-06-12 09:25:12 +000081 sqlite3_result_text(context, z, -1, SQLITE_STATIC);
drh0bce8352002-02-28 00:41:10 +000082}
83
drh5708d2d2005-06-22 10:53:59 +000084
85/*
drh0bce8352002-02-28 00:41:10 +000086** Implementation of the length() function
87*/
drhf9b596e2004-05-26 16:54:42 +000088static void lengthFunc(
89 sqlite3_context *context,
90 int argc,
91 sqlite3_value **argv
92){
drh0bce8352002-02-28 00:41:10 +000093 int len;
94
95 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000096 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000097 case SQLITE_BLOB:
98 case SQLITE_INTEGER:
99 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000100 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +0000101 break;
102 }
drh9c054832004-05-31 18:51:57 +0000103 case SQLITE_TEXT: {
drh2646da72005-12-09 20:02:05 +0000104 const unsigned char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000105 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +0000106 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +0000107 break;
108 }
109 default: {
110 sqlite3_result_null(context);
111 break;
112 }
113 }
drh0bce8352002-02-28 00:41:10 +0000114}
115
116/*
117** Implementation of the abs() function
118*/
danielk19770ae8b832004-05-25 12:05:56 +0000119static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000120 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000121 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000122 case SQLITE_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000123 i64 iVal = sqlite3_value_int64(argv[0]);
drh52fc8492006-02-23 21:43:55 +0000124 if( iVal<0 ){
125 if( (iVal<<1)==0 ){
126 sqlite3_result_error(context, "integer overflow", -1);
127 return;
128 }
129 iVal = -iVal;
130 }
danielk1977f93bbbe2004-05-27 10:30:52 +0000131 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000132 break;
133 }
drh9c054832004-05-31 18:51:57 +0000134 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000135 sqlite3_result_null(context);
136 break;
137 }
138 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000139 double rVal = sqlite3_value_double(argv[0]);
drh52fc8492006-02-23 21:43:55 +0000140 if( rVal<0 ) rVal = -rVal;
danielk1977f93bbbe2004-05-27 10:30:52 +0000141 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000142 break;
143 }
144 }
drh0bce8352002-02-28 00:41:10 +0000145}
146
147/*
148** Implementation of the substr() function
149*/
drhf9b596e2004-05-26 16:54:42 +0000150static void substrFunc(
151 sqlite3_context *context,
152 int argc,
153 sqlite3_value **argv
154){
drh2646da72005-12-09 20:02:05 +0000155 const unsigned char *z;
156 const unsigned char *z2;
drh0bce8352002-02-28 00:41:10 +0000157 int i;
drh0bce8352002-02-28 00:41:10 +0000158 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000159
drh0bce8352002-02-28 00:41:10 +0000160 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000161 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000162 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000163 p1 = sqlite3_value_int(argv[1]);
164 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000165 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000166 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000167 p1 += len;
drh653bc752002-02-28 03:31:10 +0000168 if( p1<0 ){
169 p2 += p1;
170 p1 = 0;
171 }
drh0bce8352002-02-28 00:41:10 +0000172 }else if( p1>0 ){
173 p1--;
174 }
175 if( p1+p2>len ){
176 p2 = len-p1;
177 }
drh77396302004-01-02 13:17:48 +0000178 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000179 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000180 }
drh47c8a672002-02-28 04:00:12 +0000181 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000182 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000183 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000184 }
drh47c8a672002-02-28 04:00:12 +0000185 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000186 if( p2<0 ) p2 = 0;
drh2646da72005-12-09 20:02:05 +0000187 sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10 +0000188}
189
190/*
191** Implementation of the round() function
192*/
danielk19770ae8b832004-05-25 12:05:56 +0000193static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000194 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000195 double r;
drh592ac8c2005-08-13 03:07:47 +0000196 char zBuf[500]; /* larger than the %f representation of the largest double */
drh0bce8352002-02-28 00:41:10 +0000197 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000198 if( argc==2 ){
drh9c054832004-05-31 18:51:57 +0000199 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000200 n = sqlite3_value_int(argv[1]);
201 if( n>30 ) n = 30;
202 if( n<0 ) n = 0;
203 }
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/*
drh137c7282007-01-29 17:58:28 +0000276** Implementation of randomblob(N). Return a random blob
277** that is N bytes long.
drh63cf66f2007-01-29 15:50:05 +0000278*/
drh137c7282007-01-29 17:58:28 +0000279static void randomBlob(
drh63cf66f2007-01-29 15:50:05 +0000280 sqlite3_context *context,
281 int argc,
282 sqlite3_value **argv
283){
drh137c7282007-01-29 17:58:28 +0000284 int n;
285 unsigned char *p;
drh63cf66f2007-01-29 15:50:05 +0000286 assert( argc==1 );
287 n = sqlite3_value_int(argv[0]);
drh137c7282007-01-29 17:58:28 +0000288 if( n<1 ) n = 1;
289 p = sqlite3_malloc(n);
290 sqlite3Randomness(n, p);
291 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
drh63cf66f2007-01-29 15:50:05 +0000292}
293
294/*
drh6ed41ad2002-04-06 14:10:47 +0000295** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000296** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000297*/
danielk197751ad0ec2004-05-24 12:39:02 +0000298static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000299 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000300 int arg,
301 sqlite3_value **argv
302){
drh9bb575f2004-09-06 17:24:11 +0000303 sqlite3 *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000304 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000305}
306
rdcf146a772004-02-25 22:51:06 +0000307/*
danielk1977b28af712004-06-21 06:50:26 +0000308** Implementation of the changes() SQL function. The return value is the
309** same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000310*/
danielk1977b28af712004-06-21 06:50:26 +0000311static void changes(
drhf9b596e2004-05-26 16:54:42 +0000312 sqlite3_context *context,
313 int arg,
314 sqlite3_value **argv
315){
drh9bb575f2004-09-06 17:24:11 +0000316 sqlite3 *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000317 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000318}
rdcf146a772004-02-25 22:51:06 +0000319
320/*
danielk1977b28af712004-06-21 06:50:26 +0000321** Implementation of the total_changes() SQL function. The return value is
322** the same as the sqlite3_total_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000323*/
danielk1977b28af712004-06-21 06:50:26 +0000324static void total_changes(
325 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000326 int arg,
327 sqlite3_value **argv
328){
drh9bb575f2004-09-06 17:24:11 +0000329 sqlite3 *db = sqlite3_user_data(context);
danielk1977b28af712004-06-21 06:50:26 +0000330 sqlite3_result_int(context, sqlite3_total_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000331}
332
drh6ed41ad2002-04-06 14:10:47 +0000333/*
drh4e5ffc52004-08-31 00:52:37 +0000334** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03 +0000335*/
drh4e5ffc52004-08-31 00:52:37 +0000336struct compareInfo {
337 u8 matchAll;
338 u8 matchOne;
339 u8 matchSet;
340 u8 noCase;
danielk1977d02eb1f2004-06-06 09:44:03 +0000341};
drh55ef4d92005-08-14 01:20:37 +0000342
drh4e5ffc52004-08-31 00:52:37 +0000343static const struct compareInfo globInfo = { '*', '?', '[', 0 };
drh55ef4d92005-08-14 01:20:37 +0000344/* The correct SQL-92 behavior is for the LIKE operator to ignore
345** case. Thus 'a' LIKE 'A' would be true. */
346static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
347/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
348** is case sensitive causing 'a' LIKE 'A' to be false */
349static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
danielk1977d02eb1f2004-06-06 09:44:03 +0000350
351/*
drh4e5ffc52004-08-31 00:52:37 +0000352** X is a pointer to the first byte of a UTF-8 character. Increment
353** X so that it points to the next character. This only works right
354** if X points to a well-formed UTF-8 string.
danielk1977d02eb1f2004-06-06 09:44:03 +0000355*/
drh4e5ffc52004-08-31 00:52:37 +0000356#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
357#define sqliteCharVal(X) sqlite3ReadUtf8(X)
danielk1977d02eb1f2004-06-06 09:44:03 +0000358
danielk1977d02eb1f2004-06-06 09:44:03 +0000359
360/*
drh4e5ffc52004-08-31 00:52:37 +0000361** Compare two UTF-8 strings for equality where the first string can
362** potentially be a "glob" expression. Return true (1) if they
363** are the same and false (0) if they are different.
drh0ac65892002-04-20 14:24:41 +0000364**
drh4e5ffc52004-08-31 00:52:37 +0000365** Globbing rules:
drh0ac65892002-04-20 14:24:41 +0000366**
drh4e5ffc52004-08-31 00:52:37 +0000367** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03 +0000368**
drh4e5ffc52004-08-31 00:52:37 +0000369** '?' Matches exactly one character.
370**
371** [...] Matches one character from the enclosed list of
372** characters.
373**
374** [^...] Matches one character not in the enclosed list.
375**
376** With the [...] and [^...] matching, a ']' character can be included
377** in the list by making it the first character after '[' or '^'. A
378** range of characters can be specified using '-'. Example:
379** "[a-z]" matches any single lower-case letter. To match a '-', make
380** it the last character in the list.
381**
382** This routine is usually quick, but can be N**2 in the worst case.
383**
384** Hints: to match '*' or '?', put them in "[]". Like this:
385**
386** abc[*]xyz Matches "abc*xyz" only
drh0ac65892002-04-20 14:24:41 +0000387*/
danielk19777c6303c2004-11-17 16:41:29 +0000388static int patternCompare(
drh4e5ffc52004-08-31 00:52:37 +0000389 const u8 *zPattern, /* The glob pattern */
390 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29 +0000391 const struct compareInfo *pInfo, /* Information about how to do the compare */
392 const int esc /* The escape character */
danielk197751ad0ec2004-05-24 12:39:02 +0000393){
danielk1977ad7dd422004-06-06 12:41:49 +0000394 register int c;
drh4e5ffc52004-08-31 00:52:37 +0000395 int invert;
396 int seen;
397 int c2;
398 u8 matchOne = pInfo->matchOne;
399 u8 matchAll = pInfo->matchAll;
400 u8 matchSet = pInfo->matchSet;
401 u8 noCase = pInfo->noCase;
danielk19777c6303c2004-11-17 16:41:29 +0000402 int prevEscape = 0; /* True if the previous character was 'escape' */
danielk1977d02eb1f2004-06-06 09:44:03 +0000403
drh4e5ffc52004-08-31 00:52:37 +0000404 while( (c = *zPattern)!=0 ){
danielk19777c6303c2004-11-17 16:41:29 +0000405 if( !prevEscape && c==matchAll ){
drh4e5ffc52004-08-31 00:52:37 +0000406 while( (c=zPattern[1]) == matchAll || c == matchOne ){
407 if( c==matchOne ){
408 if( *zString==0 ) return 0;
409 sqliteNextChar(zString);
410 }
411 zPattern++;
danielk1977ad7dd422004-06-06 12:41:49 +0000412 }
drh20fc0882004-11-18 13:49:25 +0000413 if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){
danielk19777c6303c2004-11-17 16:41:29 +0000414 u8 const *zTemp = &zPattern[1];
415 sqliteNextChar(zTemp);
416 c = *zTemp;
417 }
drh4e5ffc52004-08-31 00:52:37 +0000418 if( c==0 ) return 1;
419 if( c==matchSet ){
danielk19777c6303c2004-11-17 16:41:29 +0000420 assert( esc==0 ); /* This is GLOB, not LIKE */
421 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){
drh4e5ffc52004-08-31 00:52:37 +0000422 sqliteNextChar(zString);
423 }
424 return *zString!=0;
425 }else{
426 while( (c2 = *zString)!=0 ){
427 if( noCase ){
428 c2 = sqlite3UpperToLower[c2];
429 c = sqlite3UpperToLower[c];
430 while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }
431 }else{
432 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
433 }
434 if( c2==0 ) return 0;
danielk19777c6303c2004-11-17 16:41:29 +0000435 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;
drh4e5ffc52004-08-31 00:52:37 +0000436 sqliteNextChar(zString);
437 }
438 return 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000439 }
danielk19777c6303c2004-11-17 16:41:29 +0000440 }else if( !prevEscape && c==matchOne ){
drh4e5ffc52004-08-31 00:52:37 +0000441 if( *zString==0 ) return 0;
442 sqliteNextChar(zString);
443 zPattern++;
444 }else if( c==matchSet ){
445 int prior_c = 0;
danielk19777c6303c2004-11-17 16:41:29 +0000446 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
drh4e5ffc52004-08-31 00:52:37 +0000447 seen = 0;
448 invert = 0;
449 c = sqliteCharVal(zString);
450 if( c==0 ) return 0;
451 c2 = *++zPattern;
452 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
453 if( c2==']' ){
454 if( c==']' ) seen = 1;
455 c2 = *++zPattern;
456 }
457 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
458 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
459 zPattern++;
460 c2 = sqliteCharVal(zPattern);
461 if( c>=prior_c && c<=c2 ) seen = 1;
462 prior_c = 0;
463 }else if( c==c2 ){
464 seen = 1;
465 prior_c = c2;
466 }else{
467 prior_c = c2;
468 }
469 sqliteNextChar(zPattern);
470 }
471 if( c2==0 || (seen ^ invert)==0 ) return 0;
472 sqliteNextChar(zString);
473 zPattern++;
drh20fc0882004-11-18 13:49:25 +0000474 }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){
danielk19777c6303c2004-11-17 16:41:29 +0000475 prevEscape = 1;
476 sqliteNextChar(zPattern);
drh4e5ffc52004-08-31 00:52:37 +0000477 }else{
478 if( noCase ){
479 if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;
480 }else{
481 if( c != *zString ) return 0;
482 }
483 zPattern++;
484 zString++;
danielk19777c6303c2004-11-17 16:41:29 +0000485 prevEscape = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +0000486 }
danielk197751ad0ec2004-05-24 12:39:02 +0000487 }
drh4e5ffc52004-08-31 00:52:37 +0000488 return *zString==0;
drh0ac65892002-04-20 14:24:41 +0000489}
drh4e5ffc52004-08-31 00:52:37 +0000490
drh55ef4d92005-08-14 01:20:37 +0000491/*
492** Count the number of times that the LIKE operator (or GLOB which is
493** just a variation of LIKE) gets called. This is used for testing
494** only.
495*/
496#ifdef SQLITE_TEST
497int sqlite3_like_count = 0;
498#endif
499
danielk19773f6b0872004-06-17 05:36:44 +0000500
501/*
502** Implementation of the like() SQL function. This function implements
503** the build-in LIKE operator. The first argument to the function is the
504** pattern and the second argument is the string. So, the SQL statements:
505**
506** A LIKE B
507**
508** is implemented as like(B,A).
509**
drh55ef4d92005-08-14 01:20:37 +0000510** This same function (with a different compareInfo structure) computes
511** the GLOB operator.
danielk19773f6b0872004-06-17 05:36:44 +0000512*/
513static void likeFunc(
514 sqlite3_context *context,
515 int argc,
516 sqlite3_value **argv
517){
518 const unsigned char *zA = sqlite3_value_text(argv[0]);
519 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk19777c6303c2004-11-17 16:41:29 +0000520 int escape = 0;
521 if( argc==3 ){
522 /* The escape character string must consist of a single UTF-8 character.
523 ** Otherwise, return an error.
524 */
525 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
drh2646da72005-12-09 20:02:05 +0000526 if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){
danielk19777c6303c2004-11-17 16:41:29 +0000527 sqlite3_result_error(context,
528 "ESCAPE expression must be a single character", -1);
529 return;
530 }
531 escape = sqlite3ReadUtf8(zEsc);
532 }
danielk19773f6b0872004-06-17 05:36:44 +0000533 if( zA && zB ){
drh55ef4d92005-08-14 01:20:37 +0000534 struct compareInfo *pInfo = sqlite3_user_data(context);
535#ifdef SQLITE_TEST
536 sqlite3_like_count++;
537#endif
538 sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape));
danielk197751ad0ec2004-05-24 12:39:02 +0000539 }
drh8912d102002-05-26 21:34:58 +0000540}
541
542/*
543** Implementation of the NULLIF(x,y) function. The result is the first
544** argument if the arguments are different. The result is NULL if the
545** arguments are equal to each other.
546*/
drhf9b596e2004-05-26 16:54:42 +0000547static void nullifFunc(
548 sqlite3_context *context,
549 int argc,
550 sqlite3_value **argv
551){
danielk1977dc1bdc42004-06-11 10:51:27 +0000552 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
553 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000554 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000555 }
drh0ac65892002-04-20 14:24:41 +0000556}
557
drh647cb0e2002-11-04 19:32:25 +0000558/*
559** Implementation of the VERSION(*) function. The result is the version
560** of the SQLite library that is running.
561*/
drhf9b596e2004-05-26 16:54:42 +0000562static void versionFunc(
563 sqlite3_context *context,
564 int argc,
565 sqlite3_value **argv
566){
danielk1977d8123362004-06-12 09:25:12 +0000567 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:25 +0000568}
569
drh137c7282007-01-29 17:58:28 +0000570/* Array for converting from half-bytes (nybbles) into ASCII hex
571** digits. */
572static const char hexdigits[] = {
573 '0', '1', '2', '3', '4', '5', '6', '7',
574 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
575};
danielk1977d641d642004-11-18 15:44:29 +0000576
drh47394702003-08-20 01:03:33 +0000577/*
578** EXPERIMENTAL - This is not an official function. The interface may
579** change. This function may disappear. Do not write code that depends
580** on this function.
581**
582** Implementation of the QUOTE() function. This function takes a single
583** argument. If the argument is numeric, the return value is the same as
584** the argument. If the argument is NULL, the return value is the string
585** "NULL". Otherwise, the argument is enclosed in single quotes with
586** single-quote escapes.
587*/
danielk19770ae8b832004-05-25 12:05:56 +0000588static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000589 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000590 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000591 case SQLITE_NULL: {
danielk1977d8123362004-06-12 09:25:12 +0000592 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
drhf9b596e2004-05-26 16:54:42 +0000593 break;
drh47394702003-08-20 01:03:33 +0000594 }
drh9c054832004-05-31 18:51:57 +0000595 case SQLITE_INTEGER:
596 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000597 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000598 break;
599 }
danielk19773f41e972004-06-08 00:39:01 +0000600 case SQLITE_BLOB: {
danielk19773f41e972004-06-08 00:39:01 +0000601 char *zText = 0;
602 int nBlob = sqlite3_value_bytes(argv[0]);
603 char const *zBlob = sqlite3_value_blob(argv[0]);
604
605 zText = (char *)sqliteMalloc((2*nBlob)+4);
606 if( !zText ){
607 sqlite3_result_error(context, "out of memory", -1);
608 }else{
609 int i;
610 for(i=0; i<nBlob; i++){
611 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
612 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
613 }
614 zText[(nBlob*2)+2] = '\'';
615 zText[(nBlob*2)+3] = '\0';
616 zText[0] = 'X';
617 zText[1] = '\'';
danielk1977d8123362004-06-12 09:25:12 +0000618 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
danielk19773f41e972004-06-08 00:39:01 +0000619 sqliteFree(zText);
620 }
621 break;
622 }
drh9c054832004-05-31 18:51:57 +0000623 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000624 int i,j,n;
drh2646da72005-12-09 20:02:05 +0000625 const unsigned char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000626 char *z;
627
628 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
629 z = sqliteMalloc( i+n+3 );
630 if( z==0 ) return;
631 z[0] = '\'';
632 for(i=0, j=1; zArg[i]; i++){
633 z[j++] = zArg[i];
634 if( zArg[i]=='\'' ){
635 z[j++] = '\'';
636 }
637 }
638 z[j++] = '\'';
639 z[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000640 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
drhf9b596e2004-05-26 16:54:42 +0000641 sqliteFree(z);
642 }
drh47394702003-08-20 01:03:33 +0000643 }
644}
645
drh137c7282007-01-29 17:58:28 +0000646/*
647** The hex() function. Interpret the argument as a blob. Return
648** a hexadecimal rendering as text.
649*/
650static void hexFunc(
651 sqlite3_context *context,
652 int argc,
653 sqlite3_value **argv
654){
655 int i, n;
656 const unsigned char *pBlob;
657 char *zHex, *z;
658 assert( argc==1 );
659 pBlob = sqlite3_value_blob(argv[0]);
660 n = sqlite3_value_bytes(argv[0]);
661 z = zHex = sqlite3_malloc(n*2 + 1);
662 if( zHex==0 ) return;
663 for(i=0; i<n; i++, pBlob++){
664 unsigned char c = *pBlob;
665 *(z++) = hexdigits[(c>>4)&0xf];
666 *(z++) = hexdigits[c&0xf];
667 }
668 *z = 0;
669 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
670}
671
drh26b6d902007-03-17 13:27:54 +0000672/*
673** The replace() function. Three arguments are all strings: call
674** them A, B, and C. The result is also a string which is derived
675** from A by replacing every occurance of B with C. The match
676** must be exact. Collating sequences are not used.
677*/
678static void replaceFunc(
679 sqlite3_context *context,
680 int argc,
681 sqlite3_value **argv
682){
683 const unsigned char *zStr; /* The input string A */
684 const unsigned char *zPattern; /* The pattern string B */
685 const unsigned char *zRep; /* The replacement string C */
686 unsigned char *zOut; /* The output */
687 int nStr; /* Size of zStr */
688 int nPattern; /* Size of zPattern */
689 int nRep; /* Size of zRep */
690 int nOut; /* Maximum size of zOut */
691 int loopLimit; /* Last zStr[] that might match zPattern[] */
692 int i, j; /* Loop counters */
693
694 assert( argc==3 );
695 if( sqlite3_value_type(argv[0])==SQLITE_NULL ||
696 sqlite3_value_type(argv[1])==SQLITE_NULL ||
697 sqlite3_value_type(argv[2])==SQLITE_NULL ){
698 return;
699 }
700 nStr = sqlite3_value_bytes(argv[0]);
701 zStr = sqlite3_value_text(argv[0]);
702 nPattern = sqlite3_value_bytes(argv[1]);
703 zPattern = sqlite3_value_text(argv[1]);
704 nRep = sqlite3_value_bytes(argv[2]);
705 zRep = sqlite3_value_text(argv[2]);
706 if( nPattern>=nRep ){
707 nOut = nStr;
708 }else{
709 nOut = (nStr/nPattern + 1)*nRep;
710 }
711 zOut = sqlite3_malloc(nOut+1);
712 if( zOut==0 ) return;
713 loopLimit = nStr - nPattern;
714 for(i=j=0; i<=loopLimit; i++){
715 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
716 zOut[j++] = zStr[i];
717 }else{
718 memcpy(&zOut[j], zRep, nRep);
719 j += nRep;
720 i += nPattern-1;
721 }
722 }
723 memcpy(&zOut[j], &zStr[i], nStr-i);
724 j += nStr - i;
725 assert( j<=nOut );
726 zOut[j] = 0;
727 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
728}
729
730
drhd24cc422003-03-27 12:51:24 +0000731#ifdef SQLITE_SOUNDEX
732/*
733** Compute the soundex encoding of a word.
734*/
drh137c7282007-01-29 17:58:28 +0000735static void soundexFunc(
736 sqlite3_context *context,
737 int argc,
738 sqlite3_value **argv
739){
drhd24cc422003-03-27 12:51:24 +0000740 char zResult[8];
drh4c755c02004-08-08 20:22:17 +0000741 const u8 *zIn;
drhd24cc422003-03-27 12:51:24 +0000742 int i, j;
743 static const unsigned char iCode[] = {
744 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
746 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
747 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
748 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
749 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
750 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
751 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
752 };
753 assert( argc==1 );
drh4c755c02004-08-08 20:22:17 +0000754 zIn = (u8*)sqlite3_value_text(argv[0]);
drhbdf67e02006-08-19 11:34:01 +0000755 if( zIn==0 ) zIn = (u8*)"";
drhd24cc422003-03-27 12:51:24 +0000756 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
757 if( zIn[i] ){
drhbdf67e02006-08-19 11:34:01 +0000758 u8 prevcode = iCode[zIn[i]&0x7f];
drhd24cc422003-03-27 12:51:24 +0000759 zResult[0] = toupper(zIn[i]);
760 for(j=1; j<4 && zIn[i]; i++){
761 int code = iCode[zIn[i]&0x7f];
762 if( code>0 ){
drhbdf67e02006-08-19 11:34:01 +0000763 if( code!=prevcode ){
764 prevcode = code;
765 zResult[j++] = code + '0';
766 }
767 }else{
768 prevcode = 0;
drhd24cc422003-03-27 12:51:24 +0000769 }
770 }
771 while( j<4 ){
772 zResult[j++] = '0';
773 }
774 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000775 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:24 +0000776 }else{
danielk1977d8123362004-06-12 09:25:12 +0000777 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:24 +0000778 }
779}
780#endif
781
drhfdb83b22006-06-17 14:12:47 +0000782#ifndef SQLITE_OMIT_LOAD_EXTENSION
783/*
784** A function that loads a shared-library extension then returns NULL.
785*/
786static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197765fd59f2006-06-24 11:51:33 +0000787 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
drhfdb83b22006-06-17 14:12:47 +0000788 const char *zProc = 0;
789 sqlite3 *db = sqlite3_user_data(context);
790 char *zErrMsg = 0;
791
792 if( argc==2 ){
danielk197765fd59f2006-06-24 11:51:33 +0000793 zProc = (const char *)sqlite3_value_text(argv[1]);
drhfdb83b22006-06-17 14:12:47 +0000794 }
795 if( sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
796 sqlite3_result_error(context, zErrMsg, -1);
797 sqlite3_free(zErrMsg);
798 }
799}
800#endif
801
drh193a6b42002-07-07 16:52:46 +0000802#ifdef SQLITE_TEST
803/*
804** This function generates a string of random characters. Used for
805** generating test data.
806*/
danielk19770ae8b832004-05-25 12:05:56 +0000807static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000808 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000809 "abcdefghijklmnopqrstuvwxyz"
810 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
811 "0123456789"
812 ".-!,:*^+=_|?/<> ";
813 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000814 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000815 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000816 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000817 if( iMin<0 ) iMin = 0;
818 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
819 }else{
820 iMin = 1;
821 }
822 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000823 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000824 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000825 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000826 }else{
827 iMax = 50;
828 }
829 n = iMin;
830 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000831 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000832 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000833 n += r%(iMax + 1 - iMin);
834 }
drh1dba7272004-01-16 13:58:18 +0000835 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000836 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000837 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000838 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000839 }
840 zBuf[n] = 0;
drh2646da72005-12-09 20:02:05 +0000841 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
danielk1977d8123362004-06-12 09:25:12 +0000842}
drh0e3d7472004-06-19 17:33:07 +0000843#endif /* SQLITE_TEST */
danielk1977d8123362004-06-12 09:25:12 +0000844
drh0e3d7472004-06-19 17:33:07 +0000845#ifdef SQLITE_TEST
danielk1977d8123362004-06-12 09:25:12 +0000846/*
847** The following two SQL functions are used to test returning a text
848** result with a destructor. Function 'test_destructor' takes one argument
849** and returns the same argument interpreted as TEXT. A destructor is
850** passed with the sqlite3_result_text() call.
851**
852** SQL function 'test_destructor_count' returns the number of outstanding
853** allocations made by 'test_destructor';
854**
855** WARNING: Not threadsafe.
856*/
857static int test_destructor_count_var = 0;
858static void destructor(void *p){
859 char *zVal = (char *)p;
860 assert(zVal);
861 zVal--;
862 sqliteFree(zVal);
863 test_destructor_count_var--;
864}
865static void test_destructor(
866 sqlite3_context *pCtx,
867 int nArg,
868 sqlite3_value **argv
869){
870 char *zVal;
danielk1977f4618892004-06-28 13:09:11 +0000871 int len;
drh9bb575f2004-09-06 17:24:11 +0000872 sqlite3 *db = sqlite3_user_data(pCtx);
danielk1977f4618892004-06-28 13:09:11 +0000873
danielk1977d8123362004-06-12 09:25:12 +0000874 test_destructor_count_var++;
875 assert( nArg==1 );
876 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
danielk197714db2662006-01-09 16:12:04 +0000877 len = sqlite3ValueBytes(argv[0], ENC(db));
danielk1977f4618892004-06-28 13:09:11 +0000878 zVal = sqliteMalloc(len+3);
879 zVal[len] = 0;
880 zVal[len-1] = 0;
danielk1977d8123362004-06-12 09:25:12 +0000881 assert( zVal );
882 zVal++;
danielk197714db2662006-01-09 16:12:04 +0000883 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
884 if( ENC(db)==SQLITE_UTF8 ){
danielk1977f4618892004-06-28 13:09:11 +0000885 sqlite3_result_text(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000886#ifndef SQLITE_OMIT_UTF16
danielk197714db2662006-01-09 16:12:04 +0000887 }else if( ENC(db)==SQLITE_UTF16LE ){
danielk1977f4618892004-06-28 13:09:11 +0000888 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
889 }else{
890 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
drh6c626082004-11-14 21:56:29 +0000891#endif /* SQLITE_OMIT_UTF16 */
danielk1977f4618892004-06-28 13:09:11 +0000892 }
danielk1977d8123362004-06-12 09:25:12 +0000893}
894static void test_destructor_count(
895 sqlite3_context *pCtx,
896 int nArg,
897 sqlite3_value **argv
898){
899 sqlite3_result_int(pCtx, test_destructor_count_var);
drh193a6b42002-07-07 16:52:46 +0000900}
drh0e3d7472004-06-19 17:33:07 +0000901#endif /* SQLITE_TEST */
danielk19773f6b0872004-06-17 05:36:44 +0000902
drh0e3d7472004-06-19 17:33:07 +0000903#ifdef SQLITE_TEST
904/*
905** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
906** interface.
907**
908** The test_auxdata() SQL function attempts to register each of its arguments
909** as auxiliary data. If there are no prior registrations of aux data for
910** that argument (meaning the argument is not a constant or this is its first
911** call) then the result for that argument is 0. If there is a prior
912** registration, the result for that argument is 1. The overall result
913** is the individual argument results separated by spaces.
914*/
danielk19773f6b0872004-06-17 05:36:44 +0000915static void free_test_auxdata(void *p) {sqliteFree(p);}
916static void test_auxdata(
917 sqlite3_context *pCtx,
918 int nArg,
919 sqlite3_value **argv
920){
921 int i;
922 char *zRet = sqliteMalloc(nArg*2);
923 if( !zRet ) return;
924 for(i=0; i<nArg; i++){
drh2646da72005-12-09 20:02:05 +0000925 char const *z = (char*)sqlite3_value_text(argv[i]);
danielk19773f6b0872004-06-17 05:36:44 +0000926 if( z ){
927 char *zAux = sqlite3_get_auxdata(pCtx, i);
928 if( zAux ){
929 zRet[i*2] = '1';
930 if( strcmp(zAux, z) ){
931 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
932 return;
933 }
934 }else{
935 zRet[i*2] = '0';
936 zAux = sqliteStrDup(z);
937 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
938 }
939 zRet[i*2+1] = ' ';
940 }
941 }
942 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
943}
drh0e3d7472004-06-19 17:33:07 +0000944#endif /* SQLITE_TEST */
drh193a6b42002-07-07 16:52:46 +0000945
danielk197701427a62005-01-11 13:02:33 +0000946#ifdef SQLITE_TEST
947/*
948** A function to test error reporting from user functions. This function
949** returns a copy of it's first argument as an error.
950*/
951static void test_error(
952 sqlite3_context *pCtx,
953 int nArg,
954 sqlite3_value **argv
955){
drh2646da72005-12-09 20:02:05 +0000956 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
danielk197701427a62005-01-11 13:02:33 +0000957}
958#endif /* SQLITE_TEST */
959
drh0ac65892002-04-20 14:24:41 +0000960/*
drhd3a149e2002-02-24 17:12:53 +0000961** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000962** sum() or avg() aggregate computation.
963*/
964typedef struct SumCtx SumCtx;
965struct SumCtx {
drh8c08e862006-02-11 17:34:00 +0000966 double rSum; /* Floating point sum */
967 i64 iSum; /* Integer sum */
968 i64 cnt; /* Number of elements summed */
969 u8 overflow; /* True if integer overflow seen */
970 u8 approx; /* True if non-integer value was input to the sum */
drhdd5baa92002-02-27 19:50:59 +0000971};
972
973/*
drha97fdd32006-01-12 22:17:50 +0000974** Routines used to compute the sum, average, and total.
975**
976** The SUM() function follows the (broken) SQL standard which means
977** that it returns NULL if it sums over no inputs. TOTAL returns
978** 0.0 in that case. In addition, TOTAL always returns a float where
979** SUM might return an integer if it never encounters a floating point
drhc806d852006-05-11 13:25:39 +0000980** value. TOTAL never fails, but SUM might through an exception if
981** it overflows an integer.
drhdd5baa92002-02-27 19:50:59 +0000982*/
danielk19770ae8b832004-05-25 12:05:56 +0000983static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000984 SumCtx *p;
drh3d1d95e2005-09-08 10:37:01 +0000985 int type;
drh3f219f42005-09-08 19:45:57 +0000986 assert( argc==1 );
drh4f26d6c2004-05-26 23:25:30 +0000987 p = sqlite3_aggregate_context(context, sizeof(*p));
drh29d72102006-02-09 22:13:41 +0000988 type = sqlite3_value_numeric_type(argv[0]);
drh3d1d95e2005-09-08 10:37:01 +0000989 if( p && type!=SQLITE_NULL ){
drh739105c2002-05-29 23:22:23 +0000990 p->cnt++;
drh29d72102006-02-09 22:13:41 +0000991 if( type==SQLITE_INTEGER ){
drh8c08e862006-02-11 17:34:00 +0000992 i64 v = sqlite3_value_int64(argv[0]);
993 p->rSum += v;
994 if( (p->approx|p->overflow)==0 ){
995 i64 iNewSum = p->iSum + v;
996 int s1 = p->iSum >> (sizeof(i64)*8-1);
997 int s2 = v >> (sizeof(i64)*8-1);
998 int s3 = iNewSum >> (sizeof(i64)*8-1);
999 p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
1000 p->iSum = iNewSum;
drh29d72102006-02-09 22:13:41 +00001001 }
1002 }else{
drh8c08e862006-02-11 17:34:00 +00001003 p->rSum += sqlite3_value_double(argv[0]);
drh29d72102006-02-09 22:13:41 +00001004 p->approx = 1;
drh3f219f42005-09-08 19:45:57 +00001005 }
drh739105c2002-05-29 23:22:23 +00001006 }
drhdd5baa92002-02-27 19:50:59 +00001007}
danielk19770ae8b832004-05-25 12:05:56 +00001008static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +00001009 SumCtx *p;
drhabfcea22005-09-06 20:36:48 +00001010 p = sqlite3_aggregate_context(context, 0);
drhc2bd9132005-09-08 20:37:43 +00001011 if( p && p->cnt>0 ){
drh8c08e862006-02-11 17:34:00 +00001012 if( p->overflow ){
1013 sqlite3_result_error(context,"integer overflow",-1);
1014 }else if( p->approx ){
1015 sqlite3_result_double(context, p->rSum);
drhc2bd9132005-09-08 20:37:43 +00001016 }else{
drh8c08e862006-02-11 17:34:00 +00001017 sqlite3_result_int64(context, p->iSum);
drhc2bd9132005-09-08 20:37:43 +00001018 }
drh3d1d95e2005-09-08 10:37:01 +00001019 }
drhdd5baa92002-02-27 19:50:59 +00001020}
danielk19770ae8b832004-05-25 12:05:56 +00001021static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +00001022 SumCtx *p;
drhabfcea22005-09-06 20:36:48 +00001023 p = sqlite3_aggregate_context(context, 0);
drh739105c2002-05-29 23:22:23 +00001024 if( p && p->cnt>0 ){
drh8c08e862006-02-11 17:34:00 +00001025 sqlite3_result_double(context, p->rSum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +00001026 }
1027}
drha97fdd32006-01-12 22:17:50 +00001028static void totalFinalize(sqlite3_context *context){
1029 SumCtx *p;
1030 p = sqlite3_aggregate_context(context, 0);
drh8c08e862006-02-11 17:34:00 +00001031 sqlite3_result_double(context, p ? p->rSum : 0.0);
drha97fdd32006-01-12 22:17:50 +00001032}
drhdd5baa92002-02-27 19:50:59 +00001033
1034/*
drh0bce8352002-02-28 00:41:10 +00001035** The following structure keeps track of state information for the
1036** count() aggregate function.
1037*/
1038typedef struct CountCtx CountCtx;
1039struct CountCtx {
drhfc6ad392006-02-09 13:38:19 +00001040 i64 n;
drh0bce8352002-02-28 00:41:10 +00001041};
drhdd5baa92002-02-27 19:50:59 +00001042
drh0bce8352002-02-28 00:41:10 +00001043/*
1044** Routines to implement the count() aggregate function.
1045*/
danielk19770ae8b832004-05-25 12:05:56 +00001046static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +00001047 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +00001048 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +00001049 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +00001050 p->n++;
1051 }
1052}
danielk19770ae8b832004-05-25 12:05:56 +00001053static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +00001054 CountCtx *p;
drhabfcea22005-09-06 20:36:48 +00001055 p = sqlite3_aggregate_context(context, 0);
drhfc6ad392006-02-09 13:38:19 +00001056 sqlite3_result_int64(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +00001057}
1058
1059/*
drh0bce8352002-02-28 00:41:10 +00001060** Routines to implement min() and max() aggregate functions.
1061*/
danielk19770ae8b832004-05-25 12:05:56 +00001062static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +00001063 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:32 +00001064 Mem *pBest;
1065
1066 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1067 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:04 +00001068 if( !pBest ) return;
drh268380c2004-02-25 13:47:31 +00001069
danielk197788208052004-05-25 01:13:20 +00001070 if( pBest->flags ){
drh9eb516c2004-07-18 20:52:32 +00001071 int max;
1072 int cmp;
danielk1977dc1bdc42004-06-11 10:51:27 +00001073 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +00001074 /* This step function is used for both the min() and max() aggregates,
1075 ** the only difference between the two being that the sense of the
1076 ** comparison is inverted. For the max() aggregate, the
1077 ** sqlite3_user_data() function returns (void *)-1. For min() it
1078 ** returns (void *)db, where db is the sqlite3* database pointer.
1079 ** Therefore the next statement sets variable 'max' to 1 for the max()
1080 ** aggregate, or 0 for min().
1081 */
danielk197788208052004-05-25 01:13:20 +00001082 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001083 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +00001084 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +00001085 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +00001086 }
drh268380c2004-02-25 13:47:31 +00001087 }else{
danielk19777e18c252004-05-25 11:47:24 +00001088 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +00001089 }
1090}
danielk19770ae8b832004-05-25 12:05:56 +00001091static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +00001092 sqlite3_value *pRes;
drhabfcea22005-09-06 20:36:48 +00001093 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
1094 if( pRes ){
1095 if( pRes->flags ){
1096 sqlite3_result_value(context, pRes);
1097 }
1098 sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:10 +00001099 }
1100}
drhdd5baa92002-02-27 19:50:59 +00001101
drh4e5ffc52004-08-31 00:52:37 +00001102
drhd3a149e2002-02-24 17:12:53 +00001103/*
drha2ed5602002-02-26 23:55:31 +00001104** This function registered all of the above C functions as SQL
1105** functions. This should be the only routine in this file with
1106** external linkage.
drhdc04c582002-02-24 01:55:15 +00001107*/
drh9bb575f2004-09-06 17:24:11 +00001108void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
drh57196282004-10-06 15:41:16 +00001109 static const struct {
drh0bce8352002-02-28 00:41:10 +00001110 char *zName;
drh268380c2004-02-25 13:47:31 +00001111 signed char nArg;
danielk1977f4618892004-06-28 13:09:11 +00001112 u8 argType; /* 0: none. 1: db 2: (-1) */
1113 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +00001114 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001115 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +00001116 } aFuncs[] = {
danielk1977f4618892004-06-28 13:09:11 +00001117 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
1118 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
1119 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc },
1120 { "max", 0, 2, SQLITE_UTF8, 1, 0 },
1121 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
1122 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
1123 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
drh6c626082004-11-14 21:56:29 +00001124#ifndef SQLITE_OMIT_UTF16
danielk1977f4618892004-06-28 13:09:11 +00001125 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr },
drh6c626082004-11-14 21:56:29 +00001126#endif
danielk1977f4618892004-06-28 13:09:11 +00001127 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
1128 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
1129 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
1130 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
1131 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
1132 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
1133 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
1134 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
drh137c7282007-01-29 17:58:28 +00001135 { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc },
danielk1977f4618892004-06-28 13:09:11 +00001136 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
1137 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
drh137c7282007-01-29 17:58:28 +00001138 { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob },
drh94a98362004-09-13 13:13:18 +00001139 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
danielk1977f4618892004-06-28 13:09:11 +00001140 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
1141 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
1142 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid },
drh26b6d902007-03-17 13:27:54 +00001143 { "changes", 0, 1, SQLITE_UTF8, 0, changes },
1144 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes },
1145 { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc },
drhd24cc422003-03-27 12:51:24 +00001146#ifdef SQLITE_SOUNDEX
danielk1977f4618892004-06-28 13:09:11 +00001147 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +00001148#endif
drhfdb83b22006-06-17 14:12:47 +00001149#ifndef SQLITE_OMIT_LOAD_EXTENSION
1150 { "load_extension", 1, 1, SQLITE_UTF8, 0, loadExt },
1151 { "load_extension", 2, 1, SQLITE_UTF8, 0, loadExt },
1152#endif
drh193a6b42002-07-07 16:52:46 +00001153#ifdef SQLITE_TEST
danielk1977f4618892004-06-28 13:09:11 +00001154 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
1155 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor},
danielk1977d8123362004-06-12 09:25:12 +00001156 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
danielk1977f4618892004-06-28 13:09:11 +00001157 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
danielk197701427a62005-01-11 13:02:33 +00001158 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
drh193a6b42002-07-07 16:52:46 +00001159#endif
drh0bce8352002-02-28 00:41:10 +00001160 };
drh57196282004-10-06 15:41:16 +00001161 static const struct {
drh0bce8352002-02-28 00:41:10 +00001162 char *zName;
drh268380c2004-02-25 13:47:31 +00001163 signed char nArg;
drh268380c2004-02-25 13:47:31 +00001164 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +00001165 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +00001166 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1167 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +00001168 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +00001169 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1170 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
1171 { "sum", 1, 0, 0, sumStep, sumFinalize },
drha97fdd32006-01-12 22:17:50 +00001172 { "total", 1, 0, 0, sumStep, totalFinalize },
danielk1977dc1bdc42004-06-11 10:51:27 +00001173 { "avg", 1, 0, 0, sumStep, avgFinalize },
1174 { "count", 0, 0, 0, countStep, countFinalize },
1175 { "count", 1, 0, 0, countStep, countFinalize },
drh0bce8352002-02-28 00:41:10 +00001176 };
1177 int i;
1178
1179 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001180 void *pArg = 0;
1181 switch( aFuncs[i].argType ){
1182 case 1: pArg = db; break;
1183 case 2: pArg = (void *)(-1); break;
1184 }
danielk1977771151b2006-01-17 13:21:40 +00001185 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
danielk1977f9d64d22004-06-19 08:18:07 +00001186 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001187 if( aFuncs[i].needCollSeq ){
1188 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1189 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1190 if( pFunc && aFuncs[i].needCollSeq ){
1191 pFunc->needCollSeq = 1;
1192 }
1193 }
drh0bce8352002-02-28 00:41:10 +00001194 }
drh1f01ec12005-02-15 21:36:18 +00001195#ifndef SQLITE_OMIT_ALTERTABLE
1196 sqlite3AlterFunctions(db);
1197#endif
drh198bf392006-01-06 21:52:49 +00001198#ifndef SQLITE_OMIT_PARSER
danielk1977f744bb52005-12-06 17:19:11 +00001199 sqlite3AttachFunctions(db);
drh198bf392006-01-06 21:52:49 +00001200#endif
drh0bce8352002-02-28 00:41:10 +00001201 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00001202 void *pArg = 0;
1203 switch( aAggs[i].argType ){
1204 case 1: pArg = db; break;
1205 case 2: pArg = (void *)(-1); break;
1206 }
danielk1977771151b2006-01-17 13:21:40 +00001207 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001208 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +00001209 if( aAggs[i].needCollSeq ){
1210 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
danielk1977d8123362004-06-12 09:25:12 +00001211 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00001212 if( pFunc && aAggs[i].needCollSeq ){
1213 pFunc->needCollSeq = 1;
1214 }
1215 }
drh268380c2004-02-25 13:47:31 +00001216 }
danielk19774adee202004-05-08 08:23:19 +00001217 sqlite3RegisterDateTimeFunctions(db);
drhb7481e72006-09-16 21:45:14 +00001218 sqlite3_overload_function(db, "MATCH", 2);
danielk1977fd9e1f32005-05-22 10:44:34 +00001219#ifdef SQLITE_SSE
drh37527852006-03-16 16:19:56 +00001220 (void)sqlite3SseFunctions(db);
danielk1977fd9e1f32005-05-22 10:44:34 +00001221#endif
drh55ef4d92005-08-14 01:20:37 +00001222#ifdef SQLITE_CASE_SENSITIVE_LIKE
1223 sqlite3RegisterLikeFunctions(db, 1);
1224#else
1225 sqlite3RegisterLikeFunctions(db, 0);
1226#endif
1227}
1228
1229/*
1230** Set the LIKEOPT flag on the 2-argument function with the given name.
1231*/
drhd64fe2f2005-08-28 17:00:23 +00001232static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
drh55ef4d92005-08-14 01:20:37 +00001233 FuncDef *pDef;
1234 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
1235 if( pDef ){
drhd64fe2f2005-08-28 17:00:23 +00001236 pDef->flags = flagVal;
drh55ef4d92005-08-14 01:20:37 +00001237 }
1238}
1239
1240/*
1241** Register the built-in LIKE and GLOB functions. The caseSensitive
1242** parameter determines whether or not the LIKE operator is case
1243** sensitive. GLOB is always case sensitive.
1244*/
1245void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
1246 struct compareInfo *pInfo;
1247 if( caseSensitive ){
1248 pInfo = (struct compareInfo*)&likeInfoAlt;
1249 }else{
1250 pInfo = (struct compareInfo*)&likeInfoNorm;
1251 }
danielk1977771151b2006-01-17 13:21:40 +00001252 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1253 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1254 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
drh55ef4d92005-08-14 01:20:37 +00001255 (struct compareInfo*)&globInfo, likeFunc, 0,0);
drhd64fe2f2005-08-28 17:00:23 +00001256 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1257 setLikeOptFlag(db, "like",
1258 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
drh55ef4d92005-08-14 01:20:37 +00001259}
1260
1261/*
1262** pExpr points to an expression which implements a function. If
1263** it is appropriate to apply the LIKE optimization to that function
1264** then set aWc[0] through aWc[2] to the wildcard characters and
1265** return TRUE. If the function is not a LIKE-style function then
1266** return FALSE.
1267*/
drhd64fe2f2005-08-28 17:00:23 +00001268int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
drh55ef4d92005-08-14 01:20:37 +00001269 FuncDef *pDef;
1270 if( pExpr->op!=TK_FUNCTION ){
1271 return 0;
1272 }
1273 if( pExpr->pList->nExpr!=2 ){
1274 return 0;
1275 }
drh2646da72005-12-09 20:02:05 +00001276 pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
drh55ef4d92005-08-14 01:20:37 +00001277 SQLITE_UTF8, 0);
drhd64fe2f2005-08-28 17:00:23 +00001278 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
drh55ef4d92005-08-14 01:20:37 +00001279 return 0;
1280 }
1281
1282 /* The memcpy() statement assumes that the wildcard characters are
1283 ** the first three statements in the compareInfo structure. The
1284 ** asserts() that follow verify that assumption
1285 */
1286 memcpy(aWc, pDef->pUserData, 3);
1287 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
1288 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
1289 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
drhd64fe2f2005-08-28 17:00:23 +00001290 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
drh55ef4d92005-08-14 01:20:37 +00001291 return 1;
drhdc04c582002-02-24 01:55:15 +00001292}