blob: 0563351c235141d3d97293c4c6af758b46686243 [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**
danielk1977dc1bdc42004-06-11 10:51:27 +000019** $Id: func.c,v 1.66 2004/06/11 10:51:32 danielk1977 Exp $
drhdc04c582002-02-24 01:55:15 +000020*/
21#include <ctype.h>
drhd3a149e2002-02-24 17:12:53 +000022#include <math.h>
23#include <stdlib.h>
drh0bce8352002-02-28 00:41:10 +000024#include <assert.h>
25#include "sqliteInt.h"
danielk197788208052004-05-25 01:13:20 +000026#include "vdbeInt.h"
drh771d8c32003-08-09 21:32:28 +000027#include "os.h"
drh0bce8352002-02-28 00:41:10 +000028
danielk1977dc1bdc42004-06-11 10:51:27 +000029static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
30 return context->pColl;
31}
32
drh0bce8352002-02-28 00:41:10 +000033/*
34** Implementation of the non-aggregate min() and max() functions
35*/
drhf9b596e2004-05-26 16:54:42 +000036static void minmaxFunc(
37 sqlite3_context *context,
38 int argc,
39 sqlite3_value **argv
40){
drh0bce8352002-02-28 00:41:10 +000041 int i;
drh268380c2004-02-25 13:47:31 +000042 int mask; /* 0 for min() or 0xffffffff for max() */
drhf9b596e2004-05-26 16:54:42 +000043 int iBest;
danielk1977dc1bdc42004-06-11 10:51:27 +000044 CollSeq *pColl;
drh0bce8352002-02-28 00:41:10 +000045
drh89425d52002-02-28 03:04:48 +000046 if( argc==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +000047 mask = (int)sqlite3_user_data(context);
danielk1977dc1bdc42004-06-11 10:51:27 +000048 pColl = sqlite3GetFuncCollSeq(context);
49 assert( pColl );
danielk1977c572ef72004-05-27 09:28:41 +000050 assert( mask==-1 || mask==0 );
drhf9b596e2004-05-26 16:54:42 +000051 iBest = 0;
drh9c054832004-05-31 18:51:57 +000052 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
drhf9b596e2004-05-26 16:54:42 +000053 for(i=1; i<argc; i++){
drh9c054832004-05-31 18:51:57 +000054 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
danielk1977dc1bdc42004-06-11 10:51:27 +000055 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
drhf9b596e2004-05-26 16:54:42 +000056 iBest = i;
drh0bce8352002-02-28 00:41:10 +000057 }
58 }
drhf4479502004-05-27 03:12:53 +000059 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:10 +000060}
drh0bce8352002-02-28 00:41:10 +000061
drh268380c2004-02-25 13:47:31 +000062/*
63** Return the type of the argument.
64*/
drhf9b596e2004-05-26 16:54:42 +000065static void typeofFunc(
66 sqlite3_context *context,
67 int argc,
68 sqlite3_value **argv
69){
danielk197735bb9d02004-05-24 12:55:54 +000070 const char *z = 0;
danielk197735bb9d02004-05-24 12:55:54 +000071 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000072 case SQLITE_NULL: z = "null"; break;
73 case SQLITE_INTEGER: z = "integer"; break;
74 case SQLITE_TEXT: z = "text"; break;
75 case SQLITE_FLOAT: z = "real"; break;
76 case SQLITE_BLOB: z = "blob"; break;
danielk197735bb9d02004-05-24 12:55:54 +000077 }
danielk19777e18c252004-05-25 11:47:24 +000078 sqlite3_result_text(context, z, -1, 0);
drh0bce8352002-02-28 00:41:10 +000079}
80
81/*
82** Implementation of the length() function
83*/
drhf9b596e2004-05-26 16:54:42 +000084static void lengthFunc(
85 sqlite3_context *context,
86 int argc,
87 sqlite3_value **argv
88){
drh0bce8352002-02-28 00:41:10 +000089 int len;
90
91 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +000092 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +000093 case SQLITE_BLOB:
94 case SQLITE_INTEGER:
95 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +000096 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42 +000097 break;
98 }
drh9c054832004-05-31 18:51:57 +000099 case SQLITE_TEXT: {
drh4f26d6c2004-05-26 23:25:30 +0000100 const char *z = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000101 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
drhf4479502004-05-27 03:12:53 +0000102 sqlite3_result_int(context, len);
drhf9b596e2004-05-26 16:54:42 +0000103 break;
104 }
105 default: {
106 sqlite3_result_null(context);
107 break;
108 }
109 }
drh0bce8352002-02-28 00:41:10 +0000110}
111
112/*
113** Implementation of the abs() function
114*/
danielk19770ae8b832004-05-25 12:05:56 +0000115static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000116 assert( argc==1 );
drhf9b596e2004-05-26 16:54:42 +0000117 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000118 case SQLITE_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000119 i64 iVal = sqlite3_value_int64(argv[0]);
120 if( iVal<0 ) iVal = iVal * -1;
121 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42 +0000122 break;
123 }
drh9c054832004-05-31 18:51:57 +0000124 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000125 sqlite3_result_null(context);
126 break;
127 }
128 default: {
danielk1977f93bbbe2004-05-27 10:30:52 +0000129 double rVal = sqlite3_value_double(argv[0]);
130 if( rVal<0 ) rVal = rVal * -1.0;
131 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42 +0000132 break;
133 }
134 }
drh0bce8352002-02-28 00:41:10 +0000135}
136
137/*
138** Implementation of the substr() function
139*/
drhf9b596e2004-05-26 16:54:42 +0000140static void substrFunc(
141 sqlite3_context *context,
142 int argc,
143 sqlite3_value **argv
144){
drh0bce8352002-02-28 00:41:10 +0000145 const char *z;
drh0bce8352002-02-28 00:41:10 +0000146 const char *z2;
147 int i;
drh0bce8352002-02-28 00:41:10 +0000148 int p1, p2, len;
drhf9b596e2004-05-26 16:54:42 +0000149
drh0bce8352002-02-28 00:41:10 +0000150 assert( argc==3 );
drh4f26d6c2004-05-26 23:25:30 +0000151 z = sqlite3_value_text(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000152 if( z==0 ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000153 p1 = sqlite3_value_int(argv[1]);
154 p2 = sqlite3_value_int(argv[2]);
drh47c8a672002-02-28 04:00:12 +0000155 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
drh0bce8352002-02-28 00:41:10 +0000156 if( p1<0 ){
drh89425d52002-02-28 03:04:48 +0000157 p1 += len;
drh653bc752002-02-28 03:31:10 +0000158 if( p1<0 ){
159 p2 += p1;
160 p1 = 0;
161 }
drh0bce8352002-02-28 00:41:10 +0000162 }else if( p1>0 ){
163 p1--;
164 }
165 if( p1+p2>len ){
166 p2 = len-p1;
167 }
drh77396302004-01-02 13:17:48 +0000168 for(i=0; i<p1 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000169 if( (z[i]&0xc0)==0x80 ) p1++;
drh0bce8352002-02-28 00:41:10 +0000170 }
drh47c8a672002-02-28 04:00:12 +0000171 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
drh77396302004-01-02 13:17:48 +0000172 for(; i<p1+p2 && z[i]; i++){
drh47c8a672002-02-28 04:00:12 +0000173 if( (z[i]&0xc0)==0x80 ) p2++;
drh0bce8352002-02-28 00:41:10 +0000174 }
drh47c8a672002-02-28 04:00:12 +0000175 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
drh653bc752002-02-28 03:31:10 +0000176 if( p2<0 ) p2 = 0;
danielk19777e18c252004-05-25 11:47:24 +0000177 sqlite3_result_text(context, &z[p1], p2, 1);
drh0bce8352002-02-28 00:41:10 +0000178}
179
180/*
181** Implementation of the round() function
182*/
danielk19770ae8b832004-05-25 12:05:56 +0000183static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197751ad0ec2004-05-24 12:39:02 +0000184 int n = 0;
drh0bce8352002-02-28 00:41:10 +0000185 double r;
186 char zBuf[100];
187 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02 +0000188 if( argc==2 ){
drh9c054832004-05-31 18:51:57 +0000189 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
danielk197751ad0ec2004-05-24 12:39:02 +0000190 n = sqlite3_value_int(argv[1]);
191 if( n>30 ) n = 30;
192 if( n<0 ) n = 0;
193 }
drh9c054832004-05-31 18:51:57 +0000194 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
drh4f26d6c2004-05-26 23:25:30 +0000195 r = sqlite3_value_double(argv[0]);
drh0bce8352002-02-28 00:41:10 +0000196 sprintf(zBuf,"%.*f",n,r);
danielk19777e18c252004-05-25 11:47:24 +0000197 sqlite3_result_text(context, zBuf, -1, 1);
drh0bce8352002-02-28 00:41:10 +0000198}
drhdc04c582002-02-24 01:55:15 +0000199
200/*
201** Implementation of the upper() and lower() SQL functions.
202*/
danielk19770ae8b832004-05-25 12:05:56 +0000203static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000204 char *z;
205 int i;
drh9c054832004-05-31 18:51:57 +0000206 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000207 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000208 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000209 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000210 for(i=0; z[i]; i++){
211 if( islower(z[i]) ) z[i] = toupper(z[i]);
212 }
danielk19777e18c252004-05-25 11:47:24 +0000213 sqlite3_result_text(context, z, -1, 1);
214 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000215}
danielk19770ae8b832004-05-25 12:05:56 +0000216static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdc04c582002-02-24 01:55:15 +0000217 char *z;
218 int i;
drh9c054832004-05-31 18:51:57 +0000219 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk1977c572ef72004-05-27 09:28:41 +0000220 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);
drhdc04c582002-02-24 01:55:15 +0000221 if( z==0 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000222 strcpy(z, sqlite3_value_text(argv[0]));
drhdc04c582002-02-24 01:55:15 +0000223 for(i=0; z[i]; i++){
224 if( isupper(z[i]) ) z[i] = tolower(z[i]);
225 }
danielk19777e18c252004-05-25 11:47:24 +0000226 sqlite3_result_text(context, z, -1, 1);
227 sqliteFree(z);
drhdc04c582002-02-24 01:55:15 +0000228}
229
230/*
drhfbc99082002-02-28 03:14:18 +0000231** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
jplyonb6c9e6e2004-01-19 04:53:24 +0000232** All three do the same thing. They return the first non-NULL
233** argument.
drh3212e182002-02-28 00:46:26 +0000234*/
drhf9b596e2004-05-26 16:54:42 +0000235static void ifnullFunc(
236 sqlite3_context *context,
237 int argc,
238 sqlite3_value **argv
239){
drhfbc99082002-02-28 03:14:18 +0000240 int i;
241 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000242 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drhf4479502004-05-27 03:12:53 +0000243 sqlite3_result_value(context, argv[i]);
drhfbc99082002-02-28 03:14:18 +0000244 break;
245 }
246 }
drh3212e182002-02-28 00:46:26 +0000247}
248
249/*
drhf9ffac92002-03-02 19:00:31 +0000250** Implementation of random(). Return a random integer.
251*/
drhf9b596e2004-05-26 16:54:42 +0000252static void randomFunc(
253 sqlite3_context *context,
254 int argc,
255 sqlite3_value **argv
256){
drhbbd82df2004-02-11 09:46:30 +0000257 int r;
danielk19774adee202004-05-08 08:23:19 +0000258 sqlite3Randomness(sizeof(r), &r);
drhf4479502004-05-27 03:12:53 +0000259 sqlite3_result_int(context, r);
drhf9ffac92002-03-02 19:00:31 +0000260}
261
262/*
drh6ed41ad2002-04-06 14:10:47 +0000263** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000264** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47 +0000265*/
danielk197751ad0ec2004-05-24 12:39:02 +0000266static void last_insert_rowid(
danielk19770ae8b832004-05-25 12:05:56 +0000267 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000268 int arg,
269 sqlite3_value **argv
270){
danielk197724b03fd2004-05-10 10:34:34 +0000271 sqlite *db = sqlite3_user_data(context);
drhf9b596e2004-05-26 16:54:42 +0000272 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47 +0000273}
274
rdcf146a772004-02-25 22:51:06 +0000275/*
276** Implementation of the change_count() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34 +0000277** value is the same as the sqlite3_changes() API function.
rdcf146a772004-02-25 22:51:06 +0000278*/
drhf9b596e2004-05-26 16:54:42 +0000279static void change_count(
280 sqlite3_context *context,
281 int arg,
282 sqlite3_value **argv
283){
danielk197724b03fd2004-05-10 10:34:34 +0000284 sqlite *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000285 sqlite3_result_int(context, sqlite3_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000286}
rdcf146a772004-02-25 22:51:06 +0000287
288/*
289** Implementation of the last_statement_change_count() SQL function. The
danielk197751ad0ec2004-05-24 12:39:02 +0000290** return value is the same as the sqlite3_last_statement_changes() API
291** function.
rdcf146a772004-02-25 22:51:06 +0000292*/
danielk197751ad0ec2004-05-24 12:39:02 +0000293static void last_statement_change_count(
danielk19770ae8b832004-05-25 12:05:56 +0000294 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000295 int arg,
296 sqlite3_value **argv
297){
danielk197724b03fd2004-05-10 10:34:34 +0000298 sqlite *db = sqlite3_user_data(context);
drhf4479502004-05-27 03:12:53 +0000299 sqlite3_result_int(context, sqlite3_last_statement_changes(db));
rdcb0c374f2004-02-20 22:53:38 +0000300}
301
drh6ed41ad2002-04-06 14:10:47 +0000302/*
danielk1977d02eb1f2004-06-06 09:44:03 +0000303** A LIKE pattern compiles to an instance of the following structure. Refer
304** to the comment for compileLike() function for details.
305*/
306struct LikePattern {
307 int nState;
308 struct LikeState {
309 int val; /* Unicode codepoint or -1 for any char i.e. '_' */
310 int failstate; /* State to jump to if next char is not val */
311 } aState[0];
312};
313typedef struct LikePattern LikePattern;
314
315void deleteLike(void *pLike){
316 sqliteFree(pLike);
317}
318
319
320/* #define TRACE_LIKE */
321
322#if defined(TRACE_LIKE) && !defined(NDEBUG)
323char *dumpLike(LikePattern *pLike){
324 int i;
325 int k = 0;
326 char *zBuf = (char *)sqliteMalloc(pLike->nState*40);
327
328 k += sprintf(&zBuf[k], "%d states - ", pLike->nState);
329 for(i=0; i<pLike->nState; i++){
330 k += sprintf(&zBuf[k], " %d:(%d, %d)", i, pLike->aState[i].val,
331 pLike->aState[i].failstate);
332 }
333 return zBuf;
334}
335#endif
336
337/*
338** This function compiles an SQL 'LIKE' pattern into a state machine,
339** represented by a LikePattern structure.
340**
341** Each state of the state-machine has two attributes, 'val' and
342** 'failstate'. The val attribute is either the value of a unicode
343** codepoint, or -1, indicating a '_' wildcard (match any single
344** character). The failstate is either the number of another state
345** or -1, indicating jump to 'no match'.
346**
347** To see if a string matches a pattern the pattern is
348** compiled to a state machine that is executed according to the algorithm
349** below. The string is assumed to be terminated by a 'NUL' character
350** (unicode codepoint 0).
351**
352** 1 S = 0
353** 2 DO
354** 3 C = <Next character from input string>
355** 4 IF( C matches <State S val> )
356** 5 S = S+1
357** 6 ELSE IF( S != <State S failstate> )
358** 7 S = <State S failstate>
359** 8 <Rewind Input string 1 character>
360** 9 WHILE( (C != NUL) AND (S != FAILED) )
361** 10
362** 11 IF( S == <number of states> )
363** 12 RETURN MATCH
364** 13 ELSE
365** 14 RETURN NO-MATCH
366**
367** In practice there is a small optimization to avoid the <Rewind>
368** operation in line 8 of the description above.
369**
370** For example, the following pattern, 'X%ABabc%_Y' is compiled to
371** the state machine below.
372**
373** State Val FailState
374** -------------------------------
375** 0 120 (x) -1 (NO MATCH)
376** 1 97 (a) 1
377** 2 98 (b) 1
378** 3 97 (a) 1
379** 4 98 (b) 2
380** 5 99 (c) 3
381** 6 -1 (_) 6
382** 7 121 (y) 7
383** 8 0 (NUL) 7
384**
385** The algorithms implemented to compile and execute the state machine were
386** first presented in "Fast pattern matching in strings", Knuth, Morris and
387** Pratt, 1977.
388**
389*/
390LikePattern *compileLike(sqlite3_value *pPattern, u8 enc){
391 LikePattern *pLike;
392 struct LikeState *aState;
393 int pc_state = -1; /* State number of previous '%' wild card */
394 int n = 0;
395 int c;
396
397 int offset = 0;
398 const char *zLike;
399
400 if( enc==TEXT_Utf8 ){
401 zLike = sqlite3_value_text(pPattern);
402 n = sqlite3_value_bytes(pPattern) + 1;
403 }else{
404 zLike = sqlite3_value_text16(pPattern);
405 n = sqlite3_value_bytes16(pPattern)/2 + 1;
406 }
407
408 pLike = (LikePattern *)
409 sqliteMalloc(sizeof(LikePattern)+n*sizeof(struct LikeState));
410 aState = pLike->aState;
411
412 n = 0;
413 do {
414 c = sqlite3ReadUniChar(zLike, &offset, &enc, 1);
415 if( c==95 ){ /* A '_' wildcard */
416 aState[n].val = -1;
417 n++;
418 }else if( c==37 ){ /* A '%' wildcard */
419 aState[n].failstate = n;
420 pc_state = n;
421 }else{ /* A regular character */
422 aState[n].val = c;
423
424 assert( pc_state<=n );
425 if( pc_state<0 ){
426 aState[n].failstate = -1;
427 }else if( pc_state==n ){
danielk1977ad7dd422004-06-06 12:41:49 +0000428 if( c ){
429 aState[n].failstate = pc_state;
430 }else{
431 aState[n].failstate = -2;
432 }
danielk1977d02eb1f2004-06-06 09:44:03 +0000433 }else{
434 int k = pLike->aState[n-1].failstate;
435 while( k>pc_state && aState[k+1].val!=-1 && aState[k+1].val!=c ){
436 k = aState[k].failstate;
437 }
438 if( k!=pc_state && aState[k+1].val==c ){
439 assert( k==pc_state );
440 k++;
441 }
442 aState[n].failstate = k;
443 }
444 n++;
445 }
446 }while( c );
447 pLike->nState = n;
448#if defined(TRACE_LIKE) && !defined(NDEBUG)
449 {
450 char *zCompiled = dumpLike(pLike);
451 printf("Pattern=\"%s\" Compiled=\"%s\"\n", zPattern, zCompiled);
452 sqliteFree(zCompiled);
453 }
454#endif
455 return pLike;
456}
457
458/*
drh0ac65892002-04-20 14:24:41 +0000459** Implementation of the like() SQL function. This function implements
460** the build-in LIKE operator. The first argument to the function is the
danielk1977d02eb1f2004-06-06 09:44:03 +0000461** pattern and the second argument is the string. So, the SQL statements:
drh0ac65892002-04-20 14:24:41 +0000462**
463** A LIKE B
464**
danielk1977d02eb1f2004-06-06 09:44:03 +0000465** is implemented as like(B,A).
466**
467** If the pointer retrieved by via a call to sqlite3_user_data() is
468** not NULL, then this function uses UTF-16. Otherwise UTF-8.
drh0ac65892002-04-20 14:24:41 +0000469*/
danielk197751ad0ec2004-05-24 12:39:02 +0000470static void likeFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000471 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000472 int argc,
473 sqlite3_value **argv
474){
danielk1977ad7dd422004-06-06 12:41:49 +0000475 register int c;
danielk1977d02eb1f2004-06-06 09:44:03 +0000476 u8 enc;
477 int offset = 0;
478 const unsigned char *zString;
479 LikePattern *pLike = sqlite3_get_auxdata(context, 0);
danielk1977ad7dd422004-06-06 12:41:49 +0000480 struct LikeState *aState;
481 register struct LikeState *pState;
danielk1977d02eb1f2004-06-06 09:44:03 +0000482
483 /* If either argument is NULL, the result is NULL */
484 if( sqlite3_value_type(argv[1])==SQLITE_NULL ||
485 sqlite3_value_type(argv[0])==SQLITE_NULL ){
486 return;
487 }
488
489 /* If the user-data pointer is NULL, use UTF-8. Otherwise UTF-16. */
490 if( sqlite3_user_data(context) ){
491 enc = TEXT_Utf16;
492 zString = (const unsigned char *)sqlite3_value_text16(argv[1]);
danielk1977ad7dd422004-06-06 12:41:49 +0000493 assert(0);
danielk1977d02eb1f2004-06-06 09:44:03 +0000494 }else{
495 enc = TEXT_Utf8;
496 zString = sqlite3_value_text(argv[1]);
497 }
498
499 /* If the LIKE pattern has not been compiled, compile it now. */
500 if( !pLike ){
501 pLike = compileLike(argv[0], enc);
502 if( !pLike ){
503 sqlite3_result_error(context, "out of memory", -1);
504 return;
505 }
506 sqlite3_set_auxdata(context, 0, pLike, deleteLike);
507 }
danielk1977ad7dd422004-06-06 12:41:49 +0000508 aState = pLike->aState;
509 pState = aState;
danielk1977d02eb1f2004-06-06 09:44:03 +0000510
danielk1977d02eb1f2004-06-06 09:44:03 +0000511 do {
danielk1977ad7dd422004-06-06 12:41:49 +0000512 if( enc==TEXT_Utf8 ){
513 c = zString[offset++];
514 if( c&0x80 ){
515 offset--;
516 c = sqlite3ReadUniChar(zString, &offset, &enc, 1);
517 }
518 }else{
519 c = sqlite3ReadUniChar(zString, &offset, &enc, 1);
520 }
521
522skip_read:
danielk1977d02eb1f2004-06-06 09:44:03 +0000523
524#if defined(TRACE_LIKE) && !defined(NDEBUG)
525 printf("State=%d:(%d, %d) Input=%d\n",
danielk1977ad7dd422004-06-06 12:41:49 +0000526 (aState - pState), pState->val, pState->failstate, c);
danielk1977d02eb1f2004-06-06 09:44:03 +0000527#endif
528
danielk1977ad7dd422004-06-06 12:41:49 +0000529 if( pState->val==-1 || pState->val==c ){
530 pState++;
danielk1977d02eb1f2004-06-06 09:44:03 +0000531 }else{
danielk1977ad7dd422004-06-06 12:41:49 +0000532 struct LikeState *pFailState = &aState[pState->failstate];
533 if( pState!=pFailState ){
534 pState = pFailState;
535 if( c && pState>=aState ) goto skip_read;
danielk1977d02eb1f2004-06-06 09:44:03 +0000536 }
537 }
danielk1977ad7dd422004-06-06 12:41:49 +0000538 }while( c && pState>=aState );
danielk1977d02eb1f2004-06-06 09:44:03 +0000539
danielk1977ad7dd422004-06-06 12:41:49 +0000540 if( (pState-aState)==pLike->nState || (pState-aState)<-1 ){
danielk1977d02eb1f2004-06-06 09:44:03 +0000541 sqlite3_result_int(context, 1);
542 }else{
543 sqlite3_result_int(context, 0);
danielk197751ad0ec2004-05-24 12:39:02 +0000544 }
drh0ac65892002-04-20 14:24:41 +0000545}
546
547/*
548** Implementation of the glob() SQL function. This function implements
549** the build-in GLOB operator. The first argument to the function is the
550** string and the second argument is the pattern. So, the SQL statements:
551**
552** A GLOB B
553**
554** is implemented as glob(A,B).
555*/
danielk19770ae8b832004-05-25 12:05:56 +0000556static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){
drh4f26d6c2004-05-26 23:25:30 +0000557 const unsigned char *zA = sqlite3_value_text(argv[0]);
558 const unsigned char *zB = sqlite3_value_text(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02 +0000559 if( zA && zB ){
drhf4479502004-05-27 03:12:53 +0000560 sqlite3_result_int(context, sqlite3GlobCompare(zA, zB));
danielk197751ad0ec2004-05-24 12:39:02 +0000561 }
drh8912d102002-05-26 21:34:58 +0000562}
563
564/*
565** Implementation of the NULLIF(x,y) function. The result is the first
566** argument if the arguments are different. The result is NULL if the
567** arguments are equal to each other.
568*/
drhf9b596e2004-05-26 16:54:42 +0000569static void nullifFunc(
570 sqlite3_context *context,
571 int argc,
572 sqlite3_value **argv
573){
danielk1977dc1bdc42004-06-11 10:51:27 +0000574 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
575 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53 +0000576 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58 +0000577 }
drh0ac65892002-04-20 14:24:41 +0000578}
579
drh647cb0e2002-11-04 19:32:25 +0000580/*
581** Implementation of the VERSION(*) function. The result is the version
582** of the SQLite library that is running.
583*/
drhf9b596e2004-05-26 16:54:42 +0000584static void versionFunc(
585 sqlite3_context *context,
586 int argc,
587 sqlite3_value **argv
588){
danielk19777e18c252004-05-25 11:47:24 +0000589 sqlite3_result_text(context, sqlite3_version, -1, 0);
drh647cb0e2002-11-04 19:32:25 +0000590}
591
drh47394702003-08-20 01:03:33 +0000592/*
593** EXPERIMENTAL - This is not an official function. The interface may
594** change. This function may disappear. Do not write code that depends
595** on this function.
596**
597** Implementation of the QUOTE() function. This function takes a single
598** argument. If the argument is numeric, the return value is the same as
599** the argument. If the argument is NULL, the return value is the string
600** "NULL". Otherwise, the argument is enclosed in single quotes with
601** single-quote escapes.
602*/
danielk19770ae8b832004-05-25 12:05:56 +0000603static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47394702003-08-20 01:03:33 +0000604 if( argc<1 ) return;
drhf9b596e2004-05-26 16:54:42 +0000605 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57 +0000606 case SQLITE_NULL: {
drhf9b596e2004-05-26 16:54:42 +0000607 sqlite3_result_text(context, "NULL", 4, 0);
608 break;
drh47394702003-08-20 01:03:33 +0000609 }
drh9c054832004-05-31 18:51:57 +0000610 case SQLITE_INTEGER:
611 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53 +0000612 sqlite3_result_value(context, argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000613 break;
614 }
danielk19773f41e972004-06-08 00:39:01 +0000615 case SQLITE_BLOB: {
616 static const char hexdigits[] = {
617 '0', '1', '2', '3', '4', '5', '6', '7',
618 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
619 };
620 char *zText = 0;
621 int nBlob = sqlite3_value_bytes(argv[0]);
622 char const *zBlob = sqlite3_value_blob(argv[0]);
623
624 zText = (char *)sqliteMalloc((2*nBlob)+4);
625 if( !zText ){
626 sqlite3_result_error(context, "out of memory", -1);
627 }else{
628 int i;
629 for(i=0; i<nBlob; i++){
630 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
631 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
632 }
633 zText[(nBlob*2)+2] = '\'';
634 zText[(nBlob*2)+3] = '\0';
635 zText[0] = 'X';
636 zText[1] = '\'';
637 sqlite3_result_text(context, zText, -1, 1);
638 sqliteFree(zText);
639 }
640 break;
641 }
drh9c054832004-05-31 18:51:57 +0000642 case SQLITE_TEXT: {
drhf9b596e2004-05-26 16:54:42 +0000643 int i,j,n;
drh4f26d6c2004-05-26 23:25:30 +0000644 const char *zArg = sqlite3_value_text(argv[0]);
drhf9b596e2004-05-26 16:54:42 +0000645 char *z;
646
647 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
648 z = sqliteMalloc( i+n+3 );
649 if( z==0 ) return;
650 z[0] = '\'';
651 for(i=0, j=1; zArg[i]; i++){
652 z[j++] = zArg[i];
653 if( zArg[i]=='\'' ){
654 z[j++] = '\'';
655 }
656 }
657 z[j++] = '\'';
658 z[j] = 0;
659 sqlite3_result_text(context, z, j, 1);
660 sqliteFree(z);
661 }
drh47394702003-08-20 01:03:33 +0000662 }
663}
664
drhd24cc422003-03-27 12:51:24 +0000665#ifdef SQLITE_SOUNDEX
666/*
667** Compute the soundex encoding of a word.
668*/
danielk19770ae8b832004-05-25 12:05:56 +0000669static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhd24cc422003-03-27 12:51:24 +0000670 char zResult[8];
671 const char *zIn;
672 int i, j;
673 static const unsigned char iCode[] = {
674 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
675 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
676 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
677 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
678 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
679 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
680 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
681 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
682 };
683 assert( argc==1 );
drh4f26d6c2004-05-26 23:25:30 +0000684 zIn = sqlite3_value_text(argv[0]);
drhd24cc422003-03-27 12:51:24 +0000685 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
686 if( zIn[i] ){
687 zResult[0] = toupper(zIn[i]);
688 for(j=1; j<4 && zIn[i]; i++){
689 int code = iCode[zIn[i]&0x7f];
690 if( code>0 ){
691 zResult[j++] = code + '0';
692 }
693 }
694 while( j<4 ){
695 zResult[j++] = '0';
696 }
697 zResult[j] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000698 sqlite3_result_text(context, zResult, 4, 1);
drhd24cc422003-03-27 12:51:24 +0000699 }else{
danielk19777e18c252004-05-25 11:47:24 +0000700 sqlite3_result_text(context, "?000", 4, 0);
drhd24cc422003-03-27 12:51:24 +0000701 }
702}
703#endif
704
drh193a6b42002-07-07 16:52:46 +0000705#ifdef SQLITE_TEST
706/*
707** This function generates a string of random characters. Used for
708** generating test data.
709*/
danielk19770ae8b832004-05-25 12:05:56 +0000710static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
drhbbd82df2004-02-11 09:46:30 +0000711 static const unsigned char zSrc[] =
drh193a6b42002-07-07 16:52:46 +0000712 "abcdefghijklmnopqrstuvwxyz"
713 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
714 "0123456789"
715 ".-!,:*^+=_|?/<> ";
716 int iMin, iMax, n, r, i;
drhbbd82df2004-02-11 09:46:30 +0000717 unsigned char zBuf[1000];
drh193a6b42002-07-07 16:52:46 +0000718 if( argc>=1 ){
drhf9b596e2004-05-26 16:54:42 +0000719 iMin = sqlite3_value_int(argv[0]);
drh193a6b42002-07-07 16:52:46 +0000720 if( iMin<0 ) iMin = 0;
721 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
722 }else{
723 iMin = 1;
724 }
725 if( argc>=2 ){
drhf9b596e2004-05-26 16:54:42 +0000726 iMax = sqlite3_value_int(argv[1]);
drh193a6b42002-07-07 16:52:46 +0000727 if( iMax<iMin ) iMax = iMin;
drh1dba7272004-01-16 13:58:18 +0000728 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
drh193a6b42002-07-07 16:52:46 +0000729 }else{
730 iMax = 50;
731 }
732 n = iMin;
733 if( iMax>iMin ){
danielk19774adee202004-05-08 08:23:19 +0000734 sqlite3Randomness(sizeof(r), &r);
drhbbd82df2004-02-11 09:46:30 +0000735 r &= 0x7fffffff;
drh193a6b42002-07-07 16:52:46 +0000736 n += r%(iMax + 1 - iMin);
737 }
drh1dba7272004-01-16 13:58:18 +0000738 assert( n<sizeof(zBuf) );
danielk19774adee202004-05-08 08:23:19 +0000739 sqlite3Randomness(n, zBuf);
drh193a6b42002-07-07 16:52:46 +0000740 for(i=0; i<n; i++){
drhbbd82df2004-02-11 09:46:30 +0000741 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
drh193a6b42002-07-07 16:52:46 +0000742 }
743 zBuf[n] = 0;
danielk19777e18c252004-05-25 11:47:24 +0000744 sqlite3_result_text(context, zBuf, n, 1);
drh193a6b42002-07-07 16:52:46 +0000745}
746#endif
747
drh0ac65892002-04-20 14:24:41 +0000748/*
drhd3a149e2002-02-24 17:12:53 +0000749** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:59 +0000750** sum() or avg() aggregate computation.
751*/
752typedef struct SumCtx SumCtx;
753struct SumCtx {
754 double sum; /* Sum of terms */
drh739105c2002-05-29 23:22:23 +0000755 int cnt; /* Number of elements summed */
drhdd5baa92002-02-27 19:50:59 +0000756};
757
758/*
759** Routines used to compute the sum or average.
760*/
danielk19770ae8b832004-05-25 12:05:56 +0000761static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:59 +0000762 SumCtx *p;
drhdd5baa92002-02-27 19:50:59 +0000763 if( argc<1 ) return;
drh4f26d6c2004-05-26 23:25:30 +0000764 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000765 if( p && SQLITE_NULL!=sqlite3_value_type(argv[0]) ){
drh4f26d6c2004-05-26 23:25:30 +0000766 p->sum += sqlite3_value_double(argv[0]);
drh739105c2002-05-29 23:22:23 +0000767 p->cnt++;
768 }
drhdd5baa92002-02-27 19:50:59 +0000769}
danielk19770ae8b832004-05-25 12:05:56 +0000770static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000771 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000772 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk19777e18c252004-05-25 11:47:24 +0000773 sqlite3_result_double(context, p ? p->sum : 0.0);
drhdd5baa92002-02-27 19:50:59 +0000774}
danielk19770ae8b832004-05-25 12:05:56 +0000775static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:59 +0000776 SumCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000777 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000778 if( p && p->cnt>0 ){
danielk19777e18c252004-05-25 11:47:24 +0000779 sqlite3_result_double(context, p->sum/(double)p->cnt);
drhdd5baa92002-02-27 19:50:59 +0000780 }
781}
782
783/*
784** An instance of the following structure holds the context of a
drha2ed5602002-02-26 23:55:31 +0000785** variance or standard deviation computation.
drhd3a149e2002-02-24 17:12:53 +0000786*/
787typedef struct StdDevCtx StdDevCtx;
788struct StdDevCtx {
789 double sum; /* Sum of terms */
790 double sum2; /* Sum of the squares of terms */
drh739105c2002-05-29 23:22:23 +0000791 int cnt; /* Number of terms counted */
drhd3a149e2002-02-24 17:12:53 +0000792};
793
drhef2daf52002-03-04 02:26:15 +0000794#if 0 /* Omit because math library is required */
drhd3a149e2002-02-24 17:12:53 +0000795/*
796** Routines used to compute the standard deviation as an aggregate.
797*/
danielk19770ae8b832004-05-25 12:05:56 +0000798static void stdDevStep(sqlite3_context *context, int argc, const char **argv){
drhd3a149e2002-02-24 17:12:53 +0000799 StdDevCtx *p;
800 double x;
drh1350b032002-02-27 19:00:20 +0000801 if( argc<1 ) return;
danielk197724b03fd2004-05-10 10:34:34 +0000802 p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000803 if( p && argv[0] ){
danielk19774adee202004-05-08 08:23:19 +0000804 x = sqlite3AtoF(argv[0], 0);
drh739105c2002-05-29 23:22:23 +0000805 p->sum += x;
806 p->sum2 += x*x;
807 p->cnt++;
808 }
drhd3a149e2002-02-24 17:12:53 +0000809}
danielk19770ae8b832004-05-25 12:05:56 +0000810static void stdDevFinalize(sqlite3_context *context){
danielk197724b03fd2004-05-10 10:34:34 +0000811 double rN = sqlite3_aggregate_count(context);
812 StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p));
drh739105c2002-05-29 23:22:23 +0000813 if( p && p->cnt>1 ){
814 double rCnt = cnt;
danielk197724b03fd2004-05-10 10:34:34 +0000815 sqlite3_set_result_double(context,
drh739105c2002-05-29 23:22:23 +0000816 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
drhd3a149e2002-02-24 17:12:53 +0000817 }
drhd3a149e2002-02-24 17:12:53 +0000818}
drhef2daf52002-03-04 02:26:15 +0000819#endif
drhd3a149e2002-02-24 17:12:53 +0000820
drh0bce8352002-02-28 00:41:10 +0000821/*
822** The following structure keeps track of state information for the
823** count() aggregate function.
824*/
825typedef struct CountCtx CountCtx;
826struct CountCtx {
827 int n;
828};
drhdd5baa92002-02-27 19:50:59 +0000829
drh0bce8352002-02-28 00:41:10 +0000830/*
831** Routines to implement the count() aggregate function.
832*/
danielk19770ae8b832004-05-25 12:05:56 +0000833static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10 +0000834 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000835 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000836 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:10 +0000837 p->n++;
838 }
839}
danielk19770ae8b832004-05-25 12:05:56 +0000840static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:10 +0000841 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000842 p = sqlite3_aggregate_context(context, sizeof(*p));
drhf4479502004-05-27 03:12:53 +0000843 sqlite3_result_int(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:10 +0000844}
845
846/*
847** This function tracks state information for the min() and max()
848** aggregate functions.
849*/
850typedef struct MinMaxCtx MinMaxCtx;
851struct MinMaxCtx {
852 char *z; /* The best so far */
853 char zBuf[28]; /* Space that can be used for storage */
854};
855
856/*
857** Routines to implement min() and max() aggregate functions.
858*/
danielk19770ae8b832004-05-25 12:05:56 +0000859static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197788208052004-05-25 01:13:20 +0000860 int max = 0;
861 int cmp = 0;
862 Mem *pArg = (Mem *)argv[0];
drh4f26d6c2004-05-26 23:25:30 +0000863 Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
drh268380c2004-02-25 13:47:31 +0000864
drh9c054832004-05-31 18:51:57 +0000865 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
danielk197788208052004-05-25 01:13:20 +0000866
867 if( pBest->flags ){
danielk1977dc1bdc42004-06-11 10:51:27 +0000868 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:24 +0000869 /* This step function is used for both the min() and max() aggregates,
870 ** the only difference between the two being that the sense of the
871 ** comparison is inverted. For the max() aggregate, the
872 ** sqlite3_user_data() function returns (void *)-1. For min() it
873 ** returns (void *)db, where db is the sqlite3* database pointer.
874 ** Therefore the next statement sets variable 'max' to 1 for the max()
875 ** aggregate, or 0 for min().
876 */
danielk197788208052004-05-25 01:13:20 +0000877 max = ((sqlite3_user_data(context)==(void *)-1)?1:0);
danielk1977dc1bdc42004-06-11 10:51:27 +0000878 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:20 +0000879 if( (max && cmp<0) || (!max && cmp>0) ){
danielk19777e18c252004-05-25 11:47:24 +0000880 sqlite3VdbeMemCopy(pBest, pArg);
danielk197788208052004-05-25 01:13:20 +0000881 }
drh268380c2004-02-25 13:47:31 +0000882 }else{
danielk19777e18c252004-05-25 11:47:24 +0000883 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:10 +0000884 }
885}
danielk19770ae8b832004-05-25 12:05:56 +0000886static void minMaxFinalize(sqlite3_context *context){
danielk197788208052004-05-25 01:13:20 +0000887 sqlite3_value *pRes;
drh4f26d6c2004-05-26 23:25:30 +0000888 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem));
danielk197788208052004-05-25 01:13:20 +0000889 if( pRes->flags ){
drhf4479502004-05-27 03:12:53 +0000890 sqlite3_result_value(context, pRes);
drh0bce8352002-02-28 00:41:10 +0000891 }
892}
drhdd5baa92002-02-27 19:50:59 +0000893
drhd3a149e2002-02-24 17:12:53 +0000894/*
drha2ed5602002-02-26 23:55:31 +0000895** This function registered all of the above C functions as SQL
896** functions. This should be the only routine in this file with
897** external linkage.
drhdc04c582002-02-24 01:55:15 +0000898*/
danielk19774adee202004-05-08 08:23:19 +0000899void sqlite3RegisterBuiltinFunctions(sqlite *db){
drh0bce8352002-02-28 00:41:10 +0000900 static struct {
901 char *zName;
drh268380c2004-02-25 13:47:31 +0000902 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000903 u8 argType; /* 0: none. 1: db 2: (-1) */
danielk1977d02eb1f2004-06-06 09:44:03 +0000904 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
danielk1977dc1bdc42004-06-11 10:51:27 +0000905 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000906 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
drh0bce8352002-02-28 00:41:10 +0000907 } aFuncs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +0000908 { "min", -1, 0, 0, 1, minmaxFunc },
909 { "min", 0, 0, 0, 1, 0 },
910 { "max", -1, 2, 0, 1, minmaxFunc },
911 { "max", 0, 2, 0, 1, 0 },
912 { "typeof", 1, 0, 0, 0, typeofFunc },
913 { "length", 1, 0, 0, 0, lengthFunc },
914 { "substr", 3, 0, 0, 0, substrFunc },
915 { "abs", 1, 0, 0, 0, absFunc },
916 { "round", 1, 0, 0, 0, roundFunc },
917 { "round", 2, 0, 0, 0, roundFunc },
918 { "upper", 1, 0, 0, 0, upperFunc },
919 { "lower", 1, 0, 0, 0, lowerFunc },
920 { "coalesce", -1, 0, 0, 0, ifnullFunc },
921 { "coalesce", 0, 0, 0, 0, 0 },
922 { "coalesce", 1, 0, 0, 0, 0 },
923 { "ifnull", 2, 0, 0, 1, ifnullFunc },
924 { "random", -1, 0, 0, 0, randomFunc },
925 { "like", 2, 0, 0, 0, likeFunc }, /* UTF-8 */
926 { "like", 2, 2, 1, 0, likeFunc }, /* UTF-16 */
927 { "glob", 2, 0, 0, 0, globFunc },
928 { "nullif", 2, 0, 0, 0, nullifFunc },
929 { "sqlite_version", 0, 0, 0, 0, versionFunc},
930 { "quote", 1, 0, 0, 0, quoteFunc },
931 { "last_insert_rowid", 0, 1, 0, 0, last_insert_rowid },
932 { "change_count", 0, 1, 0, 0, change_count },
933 { "last_statement_change_count", 0, 1, 0, 0, last_statement_change_count },
drhd24cc422003-03-27 12:51:24 +0000934#ifdef SQLITE_SOUNDEX
danielk1977dc1bdc42004-06-11 10:51:27 +0000935 { "soundex", 1, 0, 0, 0, soundexFunc},
drhd24cc422003-03-27 12:51:24 +0000936#endif
drh193a6b42002-07-07 16:52:46 +0000937#ifdef SQLITE_TEST
danielk1977dc1bdc42004-06-11 10:51:27 +0000938 { "randstr", 2, 0, 0, 0, randStr },
drh193a6b42002-07-07 16:52:46 +0000939#endif
drh0bce8352002-02-28 00:41:10 +0000940 };
941 static struct {
942 char *zName;
drh268380c2004-02-25 13:47:31 +0000943 signed char nArg;
drh268380c2004-02-25 13:47:31 +0000944 u8 argType;
danielk1977dc1bdc42004-06-11 10:51:27 +0000945 u8 needCollSeq;
danielk19770ae8b832004-05-25 12:05:56 +0000946 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
947 void (*xFinalize)(sqlite3_context*);
drh0bce8352002-02-28 00:41:10 +0000948 } aAggs[] = {
danielk1977dc1bdc42004-06-11 10:51:27 +0000949 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
950 { "max", 1, 2, 1, minmaxStep, minMaxFinalize },
951 { "sum", 1, 0, 0, sumStep, sumFinalize },
952 { "avg", 1, 0, 0, sumStep, avgFinalize },
953 { "count", 0, 0, 0, countStep, countFinalize },
954 { "count", 1, 0, 0, countStep, countFinalize },
drhef2daf52002-03-04 02:26:15 +0000955#if 0
drhf9b596e2004-05-26 16:54:42 +0000956 { "stddev", 1, 0, stdDevStep, stdDevFinalize },
drhef2daf52002-03-04 02:26:15 +0000957#endif
drh0bce8352002-02-28 00:41:10 +0000958 };
959 int i;
960
961 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000962 void *pArg = 0;
963 switch( aFuncs[i].argType ){
964 case 1: pArg = db; break;
965 case 2: pArg = (void *)(-1); break;
966 }
danielk1977ad7dd422004-06-06 12:41:49 +0000967 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
968 aFuncs[i].eTextRep, 0, pArg, aFuncs[i].xFunc, 0, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +0000969 if( aFuncs[i].needCollSeq ){
970 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
971 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
972 if( pFunc && aFuncs[i].needCollSeq ){
973 pFunc->needCollSeq = 1;
974 }
975 }
drh0bce8352002-02-28 00:41:10 +0000976 }
977 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +0000978 void *pArg = 0;
979 switch( aAggs[i].argType ){
980 case 1: pArg = db; break;
981 case 2: pArg = (void *)(-1); break;
982 }
danielk197765904932004-05-26 06:18:37 +0000983 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg,
984 0, aAggs[i].xStep, aAggs[i].xFinalize);
danielk1977dc1bdc42004-06-11 10:51:27 +0000985 if( aAggs[i].needCollSeq ){
986 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
987 strlen(aAggs[i].zName), aAggs[i].nArg, 0, 0);
988 if( pFunc && aAggs[i].needCollSeq ){
989 pFunc->needCollSeq = 1;
990 }
991 }
drh268380c2004-02-25 13:47:31 +0000992 }
danielk19774adee202004-05-08 08:23:19 +0000993 sqlite3RegisterDateTimeFunctions(db);
drhdc04c582002-02-24 01:55:15 +0000994}
danielk1977dc1bdc42004-06-11 10:51:27 +0000995