drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1 | /* |
| 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 | ** |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.60 2004/05/27 09:28:42 danielk1977 Exp $ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 20 | */ |
| 21 | #include <ctype.h> |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 22 | #include <math.h> |
| 23 | #include <stdlib.h> |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include "sqliteInt.h" |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 26 | #include "vdbeInt.h" |
drh | 771d8c3 | 2003-08-09 21:32:28 +0000 | [diff] [blame] | 27 | #include "os.h" |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | ** Implementation of the non-aggregate min() and max() functions |
| 31 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 32 | static void minmaxFunc( |
| 33 | sqlite3_context *context, |
| 34 | int argc, |
| 35 | sqlite3_value **argv |
| 36 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 37 | int i; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 38 | int mask; /* 0 for min() or 0xffffffff for max() */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 39 | int iBest; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 40 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 41 | if( argc==0 ) return; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 42 | mask = (int)sqlite3_user_data(context); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 43 | assert( mask==-1 || mask==0 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 44 | iBest = 0; |
| 45 | for(i=1; i<argc; i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 46 | if( (sqlite3MemCompare(argv[iBest], argv[i], 0)^mask)>=0 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 47 | iBest = i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 50 | sqlite3_result_value(context, argv[iBest]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 51 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 52 | |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 53 | /* |
| 54 | ** Return the type of the argument. |
| 55 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 56 | static void typeofFunc( |
| 57 | sqlite3_context *context, |
| 58 | int argc, |
| 59 | sqlite3_value **argv |
| 60 | ){ |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 61 | const char *z = 0; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 62 | switch( sqlite3_value_type(argv[0]) ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 63 | case SQLITE3_NULL: z = "null"; break; |
| 64 | case SQLITE3_INTEGER: z = "integer"; break; |
| 65 | case SQLITE3_TEXT: z = "text"; break; |
| 66 | case SQLITE3_FLOAT: z = "real"; break; |
| 67 | case SQLITE3_BLOB: z = "blob"; break; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 68 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 69 | sqlite3_result_text(context, z, -1, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | ** Implementation of the length() function |
| 74 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 75 | static void lengthFunc( |
| 76 | sqlite3_context *context, |
| 77 | int argc, |
| 78 | sqlite3_value **argv |
| 79 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 80 | int len; |
| 81 | |
| 82 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 83 | switch( sqlite3_value_type(argv[0]) ){ |
| 84 | case SQLITE3_BLOB: |
| 85 | case SQLITE3_INTEGER: |
| 86 | case SQLITE3_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 87 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | case SQLITE3_TEXT: { |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 91 | const char *z = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 92 | for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 93 | sqlite3_result_int(context, len); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | default: { |
| 97 | sqlite3_result_null(context); |
| 98 | break; |
| 99 | } |
| 100 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | ** Implementation of the abs() function |
| 105 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 106 | static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 107 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 108 | switch( sqlite3_value_type(argv[0]) ){ |
| 109 | case SQLITE3_INTEGER: { |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 110 | sqlite3_result_int64(context, -sqlite3_value_int64(argv[0])); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | case SQLITE3_NULL: { |
| 114 | sqlite3_result_null(context); |
| 115 | break; |
| 116 | } |
| 117 | default: { |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 118 | sqlite3_result_double(context, -sqlite3_value_double(argv[0])); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | ** Implementation of the substr() function |
| 126 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 127 | static void substrFunc( |
| 128 | sqlite3_context *context, |
| 129 | int argc, |
| 130 | sqlite3_value **argv |
| 131 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 132 | const char *z; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 133 | const char *z2; |
| 134 | int i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 135 | int p1, p2, len; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 136 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 137 | assert( argc==3 ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 138 | z = sqlite3_value_text(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 139 | if( z==0 ) return; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 140 | p1 = sqlite3_value_int(argv[1]); |
| 141 | p2 = sqlite3_value_int(argv[2]); |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 142 | for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 143 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 144 | p1 += len; |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 145 | if( p1<0 ){ |
| 146 | p2 += p1; |
| 147 | p1 = 0; |
| 148 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 149 | }else if( p1>0 ){ |
| 150 | p1--; |
| 151 | } |
| 152 | if( p1+p2>len ){ |
| 153 | p2 = len-p1; |
| 154 | } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 155 | for(i=0; i<p1 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 156 | if( (z[i]&0xc0)==0x80 ) p1++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 157 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 158 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 159 | for(; i<p1+p2 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 160 | if( (z[i]&0xc0)==0x80 ) p2++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 161 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 162 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 163 | if( p2<0 ) p2 = 0; |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 164 | sqlite3_result_text(context, &z[p1], p2, 1); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | ** Implementation of the round() function |
| 169 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 170 | static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 171 | int n = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 172 | double r; |
| 173 | char zBuf[100]; |
| 174 | assert( argc==1 || argc==2 ); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 175 | if( argc==2 ){ |
| 176 | if( SQLITE3_NULL==sqlite3_value_type(argv[1]) ) return; |
| 177 | n = sqlite3_value_int(argv[1]); |
| 178 | if( n>30 ) n = 30; |
| 179 | if( n<0 ) n = 0; |
| 180 | } |
| 181 | if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 182 | r = sqlite3_value_double(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 183 | sprintf(zBuf,"%.*f",n,r); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 184 | sqlite3_result_text(context, zBuf, -1, 1); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 185 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | ** Implementation of the upper() and lower() SQL functions. |
| 189 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 190 | static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 191 | char *z; |
| 192 | int i; |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 193 | if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 194 | z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 195 | if( z==0 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 196 | strcpy(z, sqlite3_value_text(argv[0])); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 197 | for(i=0; z[i]; i++){ |
| 198 | if( islower(z[i]) ) z[i] = toupper(z[i]); |
| 199 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 200 | sqlite3_result_text(context, z, -1, 1); |
| 201 | sqliteFree(z); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 202 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 203 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 204 | char *z; |
| 205 | int i; |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 206 | if( argc<1 || SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 207 | z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 208 | if( z==0 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 209 | strcpy(z, sqlite3_value_text(argv[0])); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 210 | for(i=0; z[i]; i++){ |
| 211 | if( isupper(z[i]) ) z[i] = tolower(z[i]); |
| 212 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 213 | sqlite3_result_text(context, z, -1, 1); |
| 214 | sqliteFree(z); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 218 | ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. |
jplyon | b6c9e6e | 2004-01-19 04:53:24 +0000 | [diff] [blame] | 219 | ** All three do the same thing. They return the first non-NULL |
| 220 | ** argument. |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 221 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 222 | static void ifnullFunc( |
| 223 | sqlite3_context *context, |
| 224 | int argc, |
| 225 | sqlite3_value **argv |
| 226 | ){ |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 227 | int i; |
| 228 | for(i=0; i<argc; i++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 229 | if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 230 | sqlite3_result_value(context, argv[i]); |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | } |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 237 | ** Implementation of random(). Return a random integer. |
| 238 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 239 | static void randomFunc( |
| 240 | sqlite3_context *context, |
| 241 | int argc, |
| 242 | sqlite3_value **argv |
| 243 | ){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 244 | int r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 245 | sqlite3Randomness(sizeof(r), &r); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 246 | sqlite3_result_int(context, r); |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 250 | ** Implementation of the last_insert_rowid() SQL function. The return |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 251 | ** value is the same as the sqlite3_last_insert_rowid() API function. |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 252 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 253 | static void last_insert_rowid( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 254 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 255 | int arg, |
| 256 | sqlite3_value **argv |
| 257 | ){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 258 | sqlite *db = sqlite3_user_data(context); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 259 | sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 260 | } |
| 261 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 262 | /* |
| 263 | ** Implementation of the change_count() SQL function. The return |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 264 | ** value is the same as the sqlite3_changes() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 265 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 266 | static void change_count( |
| 267 | sqlite3_context *context, |
| 268 | int arg, |
| 269 | sqlite3_value **argv |
| 270 | ){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 271 | sqlite *db = sqlite3_user_data(context); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 272 | sqlite3_result_int(context, sqlite3_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 273 | } |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | ** Implementation of the last_statement_change_count() SQL function. The |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 277 | ** return value is the same as the sqlite3_last_statement_changes() API |
| 278 | ** function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 279 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 280 | static void last_statement_change_count( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 281 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 282 | int arg, |
| 283 | sqlite3_value **argv |
| 284 | ){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 285 | sqlite *db = sqlite3_user_data(context); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 286 | sqlite3_result_int(context, sqlite3_last_statement_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 287 | } |
| 288 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 289 | /* |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 290 | ** Implementation of the like() SQL function. This function implements |
| 291 | ** the build-in LIKE operator. The first argument to the function is the |
| 292 | ** string and the second argument is the pattern. So, the SQL statements: |
| 293 | ** |
| 294 | ** A LIKE B |
| 295 | ** |
| 296 | ** is implemented as like(A,B). |
| 297 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 298 | static void likeFunc( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 299 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 300 | int argc, |
| 301 | sqlite3_value **argv |
| 302 | ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 303 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 304 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 305 | if( zA && zB ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 306 | sqlite3_result_int(context, sqlite3LikeCompare(zA, zB)); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 307 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* |
| 311 | ** Implementation of the glob() SQL function. This function implements |
| 312 | ** the build-in GLOB operator. The first argument to the function is the |
| 313 | ** string and the second argument is the pattern. So, the SQL statements: |
| 314 | ** |
| 315 | ** A GLOB B |
| 316 | ** |
| 317 | ** is implemented as glob(A,B). |
| 318 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 319 | static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 320 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 321 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 322 | if( zA && zB ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 323 | sqlite3_result_int(context, sqlite3GlobCompare(zA, zB)); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 324 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 329 | ** argument if the arguments are different. The result is NULL if the |
| 330 | ** arguments are equal to each other. |
| 331 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 332 | static void nullifFunc( |
| 333 | sqlite3_context *context, |
| 334 | int argc, |
| 335 | sqlite3_value **argv |
| 336 | ){ |
| 337 | if( sqlite3MemCompare(argv[0], argv[1], 0)!=0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 338 | sqlite3_result_value(context, argv[0]); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 339 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 340 | } |
| 341 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 342 | /* |
| 343 | ** Implementation of the VERSION(*) function. The result is the version |
| 344 | ** of the SQLite library that is running. |
| 345 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 346 | static void versionFunc( |
| 347 | sqlite3_context *context, |
| 348 | int argc, |
| 349 | sqlite3_value **argv |
| 350 | ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 351 | sqlite3_result_text(context, sqlite3_version, -1, 0); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 352 | } |
| 353 | |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 354 | /* |
| 355 | ** EXPERIMENTAL - This is not an official function. The interface may |
| 356 | ** change. This function may disappear. Do not write code that depends |
| 357 | ** on this function. |
| 358 | ** |
| 359 | ** Implementation of the QUOTE() function. This function takes a single |
| 360 | ** argument. If the argument is numeric, the return value is the same as |
| 361 | ** the argument. If the argument is NULL, the return value is the string |
| 362 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 363 | ** single-quote escapes. |
| 364 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 365 | static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 366 | if( argc<1 ) return; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 367 | switch( sqlite3_value_type(argv[0]) ){ |
| 368 | case SQLITE3_NULL: { |
| 369 | sqlite3_result_text(context, "NULL", 4, 0); |
| 370 | break; |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 371 | } |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 372 | case SQLITE3_INTEGER: |
| 373 | case SQLITE3_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 374 | sqlite3_result_value(context, argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | case SQLITE3_BLOB: /*** FIX ME. Use a BLOB encoding ***/ |
| 378 | case SQLITE3_TEXT: { |
| 379 | int i,j,n; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 380 | const char *zArg = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 381 | char *z; |
| 382 | |
| 383 | for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } |
| 384 | z = sqliteMalloc( i+n+3 ); |
| 385 | if( z==0 ) return; |
| 386 | z[0] = '\''; |
| 387 | for(i=0, j=1; zArg[i]; i++){ |
| 388 | z[j++] = zArg[i]; |
| 389 | if( zArg[i]=='\'' ){ |
| 390 | z[j++] = '\''; |
| 391 | } |
| 392 | } |
| 393 | z[j++] = '\''; |
| 394 | z[j] = 0; |
| 395 | sqlite3_result_text(context, z, j, 1); |
| 396 | sqliteFree(z); |
| 397 | } |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 401 | #ifdef SQLITE_SOUNDEX |
| 402 | /* |
| 403 | ** Compute the soundex encoding of a word. |
| 404 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 405 | static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 406 | char zResult[8]; |
| 407 | const char *zIn; |
| 408 | int i, j; |
| 409 | static const unsigned char iCode[] = { |
| 410 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 411 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 412 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 413 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 414 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 415 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 416 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 417 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 418 | }; |
| 419 | assert( argc==1 ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 420 | zIn = sqlite3_value_text(argv[0]); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 421 | for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} |
| 422 | if( zIn[i] ){ |
| 423 | zResult[0] = toupper(zIn[i]); |
| 424 | for(j=1; j<4 && zIn[i]; i++){ |
| 425 | int code = iCode[zIn[i]&0x7f]; |
| 426 | if( code>0 ){ |
| 427 | zResult[j++] = code + '0'; |
| 428 | } |
| 429 | } |
| 430 | while( j<4 ){ |
| 431 | zResult[j++] = '0'; |
| 432 | } |
| 433 | zResult[j] = 0; |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 434 | sqlite3_result_text(context, zResult, 4, 1); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 435 | }else{ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 436 | sqlite3_result_text(context, "?000", 4, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | #endif |
| 440 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 441 | #ifdef SQLITE_TEST |
| 442 | /* |
| 443 | ** This function generates a string of random characters. Used for |
| 444 | ** generating test data. |
| 445 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 446 | static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 447 | static const unsigned char zSrc[] = |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 448 | "abcdefghijklmnopqrstuvwxyz" |
| 449 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 450 | "0123456789" |
| 451 | ".-!,:*^+=_|?/<> "; |
| 452 | int iMin, iMax, n, r, i; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 453 | unsigned char zBuf[1000]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 454 | if( argc>=1 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 455 | iMin = sqlite3_value_int(argv[0]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 456 | if( iMin<0 ) iMin = 0; |
| 457 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 458 | }else{ |
| 459 | iMin = 1; |
| 460 | } |
| 461 | if( argc>=2 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 462 | iMax = sqlite3_value_int(argv[1]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 463 | if( iMax<iMin ) iMax = iMin; |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 464 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 465 | }else{ |
| 466 | iMax = 50; |
| 467 | } |
| 468 | n = iMin; |
| 469 | if( iMax>iMin ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 470 | sqlite3Randomness(sizeof(r), &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 471 | r &= 0x7fffffff; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 472 | n += r%(iMax + 1 - iMin); |
| 473 | } |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 474 | assert( n<sizeof(zBuf) ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 475 | sqlite3Randomness(n, zBuf); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 476 | for(i=0; i<n; i++){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 477 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 478 | } |
| 479 | zBuf[n] = 0; |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 480 | sqlite3_result_text(context, zBuf, n, 1); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 481 | } |
| 482 | #endif |
| 483 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 484 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 485 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 486 | ** sum() or avg() aggregate computation. |
| 487 | */ |
| 488 | typedef struct SumCtx SumCtx; |
| 489 | struct SumCtx { |
| 490 | double sum; /* Sum of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 491 | int cnt; /* Number of elements summed */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | /* |
| 495 | ** Routines used to compute the sum or average. |
| 496 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 497 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 498 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 499 | if( argc<1 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 500 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 501 | if( p && SQLITE3_NULL!=sqlite3_value_type(argv[0]) ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 502 | p->sum += sqlite3_value_double(argv[0]); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 503 | p->cnt++; |
| 504 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 505 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 506 | static void sumFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 507 | SumCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 508 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 509 | sqlite3_result_double(context, p ? p->sum : 0.0); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 510 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 511 | static void avgFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 512 | SumCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 513 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 514 | if( p && p->cnt>0 ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 515 | sqlite3_result_double(context, p->sum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | ** An instance of the following structure holds the context of a |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 521 | ** variance or standard deviation computation. |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 522 | */ |
| 523 | typedef struct StdDevCtx StdDevCtx; |
| 524 | struct StdDevCtx { |
| 525 | double sum; /* Sum of terms */ |
| 526 | double sum2; /* Sum of the squares of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 527 | int cnt; /* Number of terms counted */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 528 | }; |
| 529 | |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 530 | #if 0 /* Omit because math library is required */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 531 | /* |
| 532 | ** Routines used to compute the standard deviation as an aggregate. |
| 533 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 534 | static void stdDevStep(sqlite3_context *context, int argc, const char **argv){ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 535 | StdDevCtx *p; |
| 536 | double x; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 537 | if( argc<1 ) return; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 538 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 539 | if( p && argv[0] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 540 | x = sqlite3AtoF(argv[0], 0); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 541 | p->sum += x; |
| 542 | p->sum2 += x*x; |
| 543 | p->cnt++; |
| 544 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 545 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 546 | static void stdDevFinalize(sqlite3_context *context){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 547 | double rN = sqlite3_aggregate_count(context); |
| 548 | StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 549 | if( p && p->cnt>1 ){ |
| 550 | double rCnt = cnt; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 551 | sqlite3_set_result_double(context, |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 552 | sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0))); |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 553 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 554 | } |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 555 | #endif |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 556 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 557 | /* |
| 558 | ** The following structure keeps track of state information for the |
| 559 | ** count() aggregate function. |
| 560 | */ |
| 561 | typedef struct CountCtx CountCtx; |
| 562 | struct CountCtx { |
| 563 | int n; |
| 564 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 565 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 566 | /* |
| 567 | ** Routines to implement the count() aggregate function. |
| 568 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 569 | static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 570 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 571 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 572 | if( (argc==0 || SQLITE3_NULL!=sqlite3_value_type(argv[0])) && p ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 573 | p->n++; |
| 574 | } |
| 575 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 576 | static void countFinalize(sqlite3_context *context){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 577 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 578 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 579 | sqlite3_result_int(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
| 583 | ** This function tracks state information for the min() and max() |
| 584 | ** aggregate functions. |
| 585 | */ |
| 586 | typedef struct MinMaxCtx MinMaxCtx; |
| 587 | struct MinMaxCtx { |
| 588 | char *z; /* The best so far */ |
| 589 | char zBuf[28]; /* Space that can be used for storage */ |
| 590 | }; |
| 591 | |
| 592 | /* |
| 593 | ** Routines to implement min() and max() aggregate functions. |
| 594 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 595 | static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 596 | int max = 0; |
| 597 | int cmp = 0; |
| 598 | Mem *pArg = (Mem *)argv[0]; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 599 | Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 600 | |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 601 | if( SQLITE3_NULL==sqlite3_value_type(argv[0]) ) return; |
| 602 | |
| 603 | if( pBest->flags ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 604 | /* This step function is used for both the min() and max() aggregates, |
| 605 | ** the only difference between the two being that the sense of the |
| 606 | ** comparison is inverted. For the max() aggregate, the |
| 607 | ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 608 | ** returns (void *)db, where db is the sqlite3* database pointer. |
| 609 | ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 610 | ** aggregate, or 0 for min(). |
| 611 | */ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 612 | max = ((sqlite3_user_data(context)==(void *)-1)?1:0); |
| 613 | cmp = sqlite3MemCompare(pBest, pArg, 0); |
| 614 | if( (max && cmp<0) || (!max && cmp>0) ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 615 | sqlite3VdbeMemCopy(pBest, pArg); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 616 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 617 | }else{ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 618 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 621 | static void minMaxFinalize(sqlite3_context *context){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 622 | sqlite3_value *pRes; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 623 | pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 624 | if( pRes->flags ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 625 | sqlite3_result_value(context, pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 628 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 629 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 630 | ** This function registered all of the above C functions as SQL |
| 631 | ** functions. This should be the only routine in this file with |
| 632 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 633 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 634 | void sqlite3RegisterBuiltinFunctions(sqlite *db){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 635 | static struct { |
| 636 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 637 | signed char nArg; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 638 | u8 argType; /* 0: none. 1: db 2: (-1) */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 639 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 640 | } aFuncs[] = { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 641 | { "min", -1, 0, minmaxFunc }, |
| 642 | { "min", 0, 0, 0 }, |
| 643 | { "max", -1, 2, minmaxFunc }, |
| 644 | { "max", 0, 2, 0 }, |
| 645 | { "typeof", 1, 0, typeofFunc }, |
| 646 | { "classof", 1, 0, typeofFunc }, /* FIX ME: hack */ |
| 647 | { "length", 1, 0, lengthFunc }, |
| 648 | { "substr", 3, 0, substrFunc }, |
| 649 | { "abs", 1, 0, absFunc }, |
| 650 | { "round", 1, 0, roundFunc }, |
| 651 | { "round", 2, 0, roundFunc }, |
| 652 | { "upper", 1, 0, upperFunc }, |
| 653 | { "lower", 1, 0, lowerFunc }, |
| 654 | { "coalesce", -1, 0, ifnullFunc }, |
| 655 | { "coalesce", 0, 0, 0 }, |
| 656 | { "coalesce", 1, 0, 0 }, |
| 657 | { "ifnull", 2, 0, ifnullFunc }, |
| 658 | { "random", -1, 0, randomFunc }, |
| 659 | { "like", 2, 0, likeFunc }, |
| 660 | { "glob", 2, 0, globFunc }, |
| 661 | { "nullif", 2, 0, nullifFunc }, |
| 662 | { "sqlite_version", 0, 0, versionFunc}, |
| 663 | { "quote", 1, 0, quoteFunc }, |
| 664 | { "last_insert_rowid", 0, 1, last_insert_rowid }, |
| 665 | { "change_count", 0, 1, change_count }, |
| 666 | { "last_statement_change_count", 0, 1, last_statement_change_count }, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 667 | #ifdef SQLITE_SOUNDEX |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 668 | { "soundex", 1, 0, soundexFunc}, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 669 | #endif |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 670 | #ifdef SQLITE_TEST |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 671 | { "randstr", 2, 0, randStr }, |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 672 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 673 | }; |
| 674 | static struct { |
| 675 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 676 | signed char nArg; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 677 | u8 argType; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 678 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); |
| 679 | void (*xFinalize)(sqlite3_context*); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 680 | } aAggs[] = { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 681 | { "min", 1, 0, minmaxStep, minMaxFinalize }, |
| 682 | { "max", 1, 2, minmaxStep, minMaxFinalize }, |
| 683 | { "sum", 1, 0, sumStep, sumFinalize }, |
| 684 | { "avg", 1, 0, sumStep, avgFinalize }, |
| 685 | { "count", 0, 0, countStep, countFinalize }, |
| 686 | { "count", 1, 0, countStep, countFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 687 | #if 0 |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 688 | { "stddev", 1, 0, stdDevStep, stdDevFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 689 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 690 | }; |
| 691 | int i; |
| 692 | |
| 693 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 694 | void *pArg = 0; |
| 695 | switch( aFuncs[i].argType ){ |
| 696 | case 1: pArg = db; break; |
| 697 | case 2: pArg = (void *)(-1); break; |
| 698 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 699 | sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0, |
| 700 | pArg, aFuncs[i].xFunc, 0, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 701 | } |
| 702 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 703 | void *pArg = 0; |
| 704 | switch( aAggs[i].argType ){ |
| 705 | case 1: pArg = db; break; |
| 706 | case 2: pArg = (void *)(-1); break; |
| 707 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 708 | sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg, |
| 709 | 0, aAggs[i].xStep, aAggs[i].xFinalize); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 710 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 711 | sqlite3RegisterDateTimeFunctions(db); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 712 | } |