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 | ** |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.99 2005/06/22 10:53:59 drh Exp $ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 20 | */ |
drh | b659e9b | 2005-01-28 01:29:08 +0000 | [diff] [blame] | 21 | #include "sqliteInt.h" |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 23 | #include <math.h> |
| 24 | #include <stdlib.h> |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 25 | #include <assert.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 | |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 29 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 30 | return context->pColl; |
| 31 | } |
| 32 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 33 | /* |
| 34 | ** Implementation of the non-aggregate min() and max() functions |
| 35 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 36 | static void minmaxFunc( |
| 37 | sqlite3_context *context, |
| 38 | int argc, |
| 39 | sqlite3_value **argv |
| 40 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 41 | int i; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 42 | int mask; /* 0 for min() or 0xffffffff for max() */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 43 | int iBest; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 44 | CollSeq *pColl; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 45 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 46 | if( argc==0 ) return; |
drh | c44af71 | 2004-09-02 15:53:56 +0000 | [diff] [blame] | 47 | mask = sqlite3_user_data(context)==0 ? 0 : -1; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 48 | pColl = sqlite3GetFuncCollSeq(context); |
| 49 | assert( pColl ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 50 | assert( mask==-1 || mask==0 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 51 | iBest = 0; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 52 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 53 | for(i=1; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 54 | if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 55 | if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 56 | iBest = i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 59 | sqlite3_result_value(context, argv[iBest]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 60 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 61 | |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 62 | /* |
| 63 | ** Return the type of the argument. |
| 64 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 65 | static void typeofFunc( |
| 66 | sqlite3_context *context, |
| 67 | int argc, |
| 68 | sqlite3_value **argv |
| 69 | ){ |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 70 | const char *z = 0; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 71 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 72 | 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; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 77 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 78 | sqlite3_result_text(context, z, -1, SQLITE_STATIC); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame^] | 82 | ** Convert the argument to a numeric type. |
| 83 | */ |
| 84 | static void numericFunc( |
| 85 | sqlite3_context *context, |
| 86 | int argc, |
| 87 | sqlite3_value **argv |
| 88 | ){ |
| 89 | const char *z = 0; |
| 90 | switch( sqlite3_value_type(argv[0]) ){ |
| 91 | case SQLITE_NULL: { |
| 92 | sqlite3_result_int(context, 0); |
| 93 | break; |
| 94 | } |
| 95 | case SQLITE_INTEGER: |
| 96 | case SQLITE_FLOAT: { |
| 97 | sqlite3_result_value(context, argv[0]); |
| 98 | break; |
| 99 | } |
| 100 | case SQLITE_TEXT: |
| 101 | case SQLITE_BLOB: { |
| 102 | z = sqlite3_value_text(argv[0]); |
| 103 | while( *z && *z!='.' ){ z++; } |
| 104 | if( *z ){ |
| 105 | sqlite3_result_double(context, sqlite3_value_double(argv[0])); |
| 106 | }else{ |
| 107 | sqlite3_result_int64(context, sqlite3_value_int64(argv[0])); |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | ** Convert the argument to TEXT |
| 116 | */ |
| 117 | static void textFunc( |
| 118 | sqlite3_context *context, |
| 119 | int argc, |
| 120 | sqlite3_value **argv |
| 121 | ){ |
| 122 | switch( sqlite3_value_type(argv[0]) ){ |
| 123 | case SQLITE_NULL: { |
| 124 | sqlite3_result_text(context, "", 0, SQLITE_STATIC); |
| 125 | break; |
| 126 | } |
| 127 | case SQLITE_BLOB: |
| 128 | case SQLITE_INTEGER: |
| 129 | case SQLITE_FLOAT: { |
| 130 | sqlite3_result_text(context, sqlite3_value_text(argv[0]), |
| 131 | sqlite3_value_bytes(argv[0]), SQLITE_TRANSIENT); |
| 132 | break; |
| 133 | } |
| 134 | case SQLITE_TEXT: { |
| 135 | sqlite3_result_value(context, argv[0]); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | ** Convert the argument to TEXT |
| 143 | */ |
| 144 | static void blobFunc( |
| 145 | sqlite3_context *context, |
| 146 | int argc, |
| 147 | sqlite3_value **argv |
| 148 | ){ |
| 149 | switch( sqlite3_value_type(argv[0]) ){ |
| 150 | case SQLITE_NULL: { |
| 151 | sqlite3_result_blob(context, "", 0, SQLITE_STATIC); |
| 152 | break; |
| 153 | } |
| 154 | case SQLITE_TEXT: |
| 155 | case SQLITE_INTEGER: |
| 156 | case SQLITE_FLOAT: { |
| 157 | sqlite3_result_blob(context, sqlite3_value_text(argv[0]), |
| 158 | sqlite3_value_bytes(argv[0]), SQLITE_TRANSIENT); |
| 159 | break; |
| 160 | } |
| 161 | case SQLITE_BLOB: { |
| 162 | sqlite3_result_value(context, argv[0]); |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 169 | ** Implementation of the length() function |
| 170 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 171 | static void lengthFunc( |
| 172 | sqlite3_context *context, |
| 173 | int argc, |
| 174 | sqlite3_value **argv |
| 175 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 176 | int len; |
| 177 | |
| 178 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 179 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 180 | case SQLITE_BLOB: |
| 181 | case SQLITE_INTEGER: |
| 182 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 183 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 186 | case SQLITE_TEXT: { |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 187 | const char *z = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 188 | for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 189 | sqlite3_result_int(context, len); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | default: { |
| 193 | sqlite3_result_null(context); |
| 194 | break; |
| 195 | } |
| 196 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | ** Implementation of the abs() function |
| 201 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 202 | static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 203 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 204 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 205 | case SQLITE_INTEGER: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 206 | i64 iVal = sqlite3_value_int64(argv[0]); |
| 207 | if( iVal<0 ) iVal = iVal * -1; |
| 208 | sqlite3_result_int64(context, iVal); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 211 | case SQLITE_NULL: { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 212 | sqlite3_result_null(context); |
| 213 | break; |
| 214 | } |
| 215 | default: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 216 | double rVal = sqlite3_value_double(argv[0]); |
| 217 | if( rVal<0 ) rVal = rVal * -1.0; |
| 218 | sqlite3_result_double(context, rVal); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
| 225 | ** Implementation of the substr() function |
| 226 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 227 | static void substrFunc( |
| 228 | sqlite3_context *context, |
| 229 | int argc, |
| 230 | sqlite3_value **argv |
| 231 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 232 | const char *z; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 233 | const char *z2; |
| 234 | int i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 235 | int p1, p2, len; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 236 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 237 | assert( argc==3 ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 238 | z = sqlite3_value_text(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 239 | if( z==0 ) return; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 240 | p1 = sqlite3_value_int(argv[1]); |
| 241 | p2 = sqlite3_value_int(argv[2]); |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 242 | for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 243 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 244 | p1 += len; |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 245 | if( p1<0 ){ |
| 246 | p2 += p1; |
| 247 | p1 = 0; |
| 248 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 249 | }else if( p1>0 ){ |
| 250 | p1--; |
| 251 | } |
| 252 | if( p1+p2>len ){ |
| 253 | p2 = len-p1; |
| 254 | } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 255 | for(i=0; i<p1 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 256 | if( (z[i]&0xc0)==0x80 ) p1++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 257 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 258 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 259 | for(; i<p1+p2 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 260 | if( (z[i]&0xc0)==0x80 ) p2++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 261 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 262 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 263 | if( p2<0 ) p2 = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 264 | sqlite3_result_text(context, &z[p1], p2, SQLITE_TRANSIENT); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | ** Implementation of the round() function |
| 269 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 270 | static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 271 | int n = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 272 | double r; |
| 273 | char zBuf[100]; |
| 274 | assert( argc==1 || argc==2 ); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 275 | if( argc==2 ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 276 | if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 277 | n = sqlite3_value_int(argv[1]); |
| 278 | if( n>30 ) n = 30; |
| 279 | if( n<0 ) n = 0; |
| 280 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 281 | if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 282 | r = sqlite3_value_double(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 283 | sprintf(zBuf,"%.*f",n,r); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 284 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 285 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | ** Implementation of the upper() and lower() SQL functions. |
| 289 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 290 | static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 8cd9db0 | 2004-07-18 23:06:53 +0000 | [diff] [blame] | 291 | unsigned char *z; |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 292 | int i; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 293 | if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 294 | z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 295 | if( z==0 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 296 | strcpy(z, sqlite3_value_text(argv[0])); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 297 | for(i=0; z[i]; i++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 298 | z[i] = toupper(z[i]); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 299 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 300 | sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 301 | sqliteFree(z); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 302 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 303 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 8cd9db0 | 2004-07-18 23:06:53 +0000 | [diff] [blame] | 304 | unsigned char *z; |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 305 | int i; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 306 | if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 307 | z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 308 | if( z==0 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 309 | strcpy(z, sqlite3_value_text(argv[0])); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 310 | for(i=0; z[i]; i++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 311 | z[i] = tolower(z[i]); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 312 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 313 | sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 314 | sqliteFree(z); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 318 | ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. |
jplyon | b6c9e6e | 2004-01-19 04:53:24 +0000 | [diff] [blame] | 319 | ** All three do the same thing. They return the first non-NULL |
| 320 | ** argument. |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 321 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 322 | static void ifnullFunc( |
| 323 | sqlite3_context *context, |
| 324 | int argc, |
| 325 | sqlite3_value **argv |
| 326 | ){ |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 327 | int i; |
| 328 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 329 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 330 | sqlite3_result_value(context, argv[i]); |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | } |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 337 | ** Implementation of random(). Return a random integer. |
| 338 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 339 | static void randomFunc( |
| 340 | sqlite3_context *context, |
| 341 | int argc, |
| 342 | sqlite3_value **argv |
| 343 | ){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 344 | int r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 345 | sqlite3Randomness(sizeof(r), &r); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 346 | sqlite3_result_int(context, r); |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 350 | ** Implementation of the last_insert_rowid() SQL function. The return |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 351 | ** value is the same as the sqlite3_last_insert_rowid() API function. |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 352 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 353 | static void last_insert_rowid( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 354 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 355 | int arg, |
| 356 | sqlite3_value **argv |
| 357 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 358 | sqlite3 *db = sqlite3_user_data(context); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 359 | sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 360 | } |
| 361 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 362 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 363 | ** Implementation of the changes() SQL function. The return value is the |
| 364 | ** same as the sqlite3_changes() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 365 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 366 | static void changes( |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 367 | sqlite3_context *context, |
| 368 | int arg, |
| 369 | sqlite3_value **argv |
| 370 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 371 | sqlite3 *db = sqlite3_user_data(context); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 372 | sqlite3_result_int(context, sqlite3_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 373 | } |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 374 | |
| 375 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 376 | ** Implementation of the total_changes() SQL function. The return value is |
| 377 | ** the same as the sqlite3_total_changes() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 378 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 379 | static void total_changes( |
| 380 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 381 | int arg, |
| 382 | sqlite3_value **argv |
| 383 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 384 | sqlite3 *db = sqlite3_user_data(context); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 385 | sqlite3_result_int(context, sqlite3_total_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 386 | } |
| 387 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 388 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 389 | ** A structure defining how to do GLOB-style comparisons. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 390 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 391 | struct compareInfo { |
| 392 | u8 matchAll; |
| 393 | u8 matchOne; |
| 394 | u8 matchSet; |
| 395 | u8 noCase; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 396 | }; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 397 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 398 | static const struct compareInfo likeInfo = { '%', '_', 0, 1 }; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 399 | |
| 400 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 401 | ** X is a pointer to the first byte of a UTF-8 character. Increment |
| 402 | ** X so that it points to the next character. This only works right |
| 403 | ** if X points to a well-formed UTF-8 string. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 404 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 405 | #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} |
| 406 | #define sqliteCharVal(X) sqlite3ReadUtf8(X) |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 407 | |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 408 | |
| 409 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 410 | ** Compare two UTF-8 strings for equality where the first string can |
| 411 | ** potentially be a "glob" expression. Return true (1) if they |
| 412 | ** are the same and false (0) if they are different. |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 413 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 414 | ** Globbing rules: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 415 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 416 | ** '*' Matches any sequence of zero or more characters. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 417 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 418 | ** '?' Matches exactly one character. |
| 419 | ** |
| 420 | ** [...] Matches one character from the enclosed list of |
| 421 | ** characters. |
| 422 | ** |
| 423 | ** [^...] Matches one character not in the enclosed list. |
| 424 | ** |
| 425 | ** With the [...] and [^...] matching, a ']' character can be included |
| 426 | ** in the list by making it the first character after '[' or '^'. A |
| 427 | ** range of characters can be specified using '-'. Example: |
| 428 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 429 | ** it the last character in the list. |
| 430 | ** |
| 431 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 432 | ** |
| 433 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 434 | ** |
| 435 | ** abc[*]xyz Matches "abc*xyz" only |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 436 | */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 437 | static int patternCompare( |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 438 | const u8 *zPattern, /* The glob pattern */ |
| 439 | const u8 *zString, /* The string to compare against the glob */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 440 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 441 | const int esc /* The escape character */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 442 | ){ |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 443 | register int c; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 444 | int invert; |
| 445 | int seen; |
| 446 | int c2; |
| 447 | u8 matchOne = pInfo->matchOne; |
| 448 | u8 matchAll = pInfo->matchAll; |
| 449 | u8 matchSet = pInfo->matchSet; |
| 450 | u8 noCase = pInfo->noCase; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 451 | int prevEscape = 0; /* True if the previous character was 'escape' */ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 452 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 453 | while( (c = *zPattern)!=0 ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 454 | if( !prevEscape && c==matchAll ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 455 | while( (c=zPattern[1]) == matchAll || c == matchOne ){ |
| 456 | if( c==matchOne ){ |
| 457 | if( *zString==0 ) return 0; |
| 458 | sqliteNextChar(zString); |
| 459 | } |
| 460 | zPattern++; |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 461 | } |
drh | 20fc088 | 2004-11-18 13:49:25 +0000 | [diff] [blame] | 462 | if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 463 | u8 const *zTemp = &zPattern[1]; |
| 464 | sqliteNextChar(zTemp); |
| 465 | c = *zTemp; |
| 466 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 467 | if( c==0 ) return 1; |
| 468 | if( c==matchSet ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 469 | assert( esc==0 ); /* This is GLOB, not LIKE */ |
| 470 | while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 471 | sqliteNextChar(zString); |
| 472 | } |
| 473 | return *zString!=0; |
| 474 | }else{ |
| 475 | while( (c2 = *zString)!=0 ){ |
| 476 | if( noCase ){ |
| 477 | c2 = sqlite3UpperToLower[c2]; |
| 478 | c = sqlite3UpperToLower[c]; |
| 479 | while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } |
| 480 | }else{ |
| 481 | while( c2 != 0 && c2 != c ){ c2 = *++zString; } |
| 482 | } |
| 483 | if( c2==0 ) return 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 484 | if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 485 | sqliteNextChar(zString); |
| 486 | } |
| 487 | return 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 488 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 489 | }else if( !prevEscape && c==matchOne ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 490 | if( *zString==0 ) return 0; |
| 491 | sqliteNextChar(zString); |
| 492 | zPattern++; |
| 493 | }else if( c==matchSet ){ |
| 494 | int prior_c = 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 495 | assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 496 | seen = 0; |
| 497 | invert = 0; |
| 498 | c = sqliteCharVal(zString); |
| 499 | if( c==0 ) return 0; |
| 500 | c2 = *++zPattern; |
| 501 | if( c2=='^' ){ invert = 1; c2 = *++zPattern; } |
| 502 | if( c2==']' ){ |
| 503 | if( c==']' ) seen = 1; |
| 504 | c2 = *++zPattern; |
| 505 | } |
| 506 | while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ |
| 507 | if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ |
| 508 | zPattern++; |
| 509 | c2 = sqliteCharVal(zPattern); |
| 510 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 511 | prior_c = 0; |
| 512 | }else if( c==c2 ){ |
| 513 | seen = 1; |
| 514 | prior_c = c2; |
| 515 | }else{ |
| 516 | prior_c = c2; |
| 517 | } |
| 518 | sqliteNextChar(zPattern); |
| 519 | } |
| 520 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 521 | sqliteNextChar(zString); |
| 522 | zPattern++; |
drh | 20fc088 | 2004-11-18 13:49:25 +0000 | [diff] [blame] | 523 | }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 524 | prevEscape = 1; |
| 525 | sqliteNextChar(zPattern); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 526 | }else{ |
| 527 | if( noCase ){ |
| 528 | if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; |
| 529 | }else{ |
| 530 | if( c != *zString ) return 0; |
| 531 | } |
| 532 | zPattern++; |
| 533 | zString++; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 534 | prevEscape = 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 535 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 536 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 537 | return *zString==0; |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 538 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 539 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 540 | |
| 541 | /* |
| 542 | ** Implementation of the like() SQL function. This function implements |
| 543 | ** the build-in LIKE operator. The first argument to the function is the |
| 544 | ** pattern and the second argument is the string. So, the SQL statements: |
| 545 | ** |
| 546 | ** A LIKE B |
| 547 | ** |
| 548 | ** is implemented as like(B,A). |
| 549 | ** |
| 550 | ** If the pointer retrieved by via a call to sqlite3_user_data() is |
| 551 | ** not NULL, then this function uses UTF-16. Otherwise UTF-8. |
| 552 | */ |
| 553 | static void likeFunc( |
| 554 | sqlite3_context *context, |
| 555 | int argc, |
| 556 | sqlite3_value **argv |
| 557 | ){ |
| 558 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 559 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 560 | int escape = 0; |
| 561 | if( argc==3 ){ |
| 562 | /* The escape character string must consist of a single UTF-8 character. |
| 563 | ** Otherwise, return an error. |
| 564 | */ |
| 565 | const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
| 566 | if( sqlite3utf8CharLen(zEsc, -1)!=1 ){ |
| 567 | sqlite3_result_error(context, |
| 568 | "ESCAPE expression must be a single character", -1); |
| 569 | return; |
| 570 | } |
| 571 | escape = sqlite3ReadUtf8(zEsc); |
| 572 | } |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 573 | if( zA && zB ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 574 | sqlite3_result_int(context, patternCompare(zA, zB, &likeInfo, escape)); |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | ** Implementation of the glob() SQL function. This function implements |
| 580 | ** the build-in GLOB operator. The first argument to the function is the |
| 581 | ** string and the second argument is the pattern. So, the SQL statements: |
| 582 | ** |
| 583 | ** A GLOB B |
| 584 | ** |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 585 | ** is implemented as glob(B,A). |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 586 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 587 | static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 588 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 589 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 590 | if( zA && zB ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 591 | sqlite3_result_int(context, patternCompare(zA, zB, &globInfo, 0)); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 592 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* |
| 596 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 597 | ** argument if the arguments are different. The result is NULL if the |
| 598 | ** arguments are equal to each other. |
| 599 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 600 | static void nullifFunc( |
| 601 | sqlite3_context *context, |
| 602 | int argc, |
| 603 | sqlite3_value **argv |
| 604 | ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 605 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
| 606 | if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 607 | sqlite3_result_value(context, argv[0]); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 608 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 609 | } |
| 610 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 611 | /* |
| 612 | ** Implementation of the VERSION(*) function. The result is the version |
| 613 | ** of the SQLite library that is running. |
| 614 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 615 | static void versionFunc( |
| 616 | sqlite3_context *context, |
| 617 | int argc, |
| 618 | sqlite3_value **argv |
| 619 | ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 620 | sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 621 | } |
| 622 | |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 623 | |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 624 | /* |
| 625 | ** EXPERIMENTAL - This is not an official function. The interface may |
| 626 | ** change. This function may disappear. Do not write code that depends |
| 627 | ** on this function. |
| 628 | ** |
| 629 | ** Implementation of the QUOTE() function. This function takes a single |
| 630 | ** argument. If the argument is numeric, the return value is the same as |
| 631 | ** the argument. If the argument is NULL, the return value is the string |
| 632 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 633 | ** single-quote escapes. |
| 634 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 635 | static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 636 | if( argc<1 ) return; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 637 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 638 | case SQLITE_NULL: { |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 639 | sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 640 | break; |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 641 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 642 | case SQLITE_INTEGER: |
| 643 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 644 | sqlite3_result_value(context, argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 645 | break; |
| 646 | } |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 647 | case SQLITE_BLOB: { |
| 648 | static const char hexdigits[] = { |
| 649 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 650 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 651 | }; |
| 652 | char *zText = 0; |
| 653 | int nBlob = sqlite3_value_bytes(argv[0]); |
| 654 | char const *zBlob = sqlite3_value_blob(argv[0]); |
| 655 | |
| 656 | zText = (char *)sqliteMalloc((2*nBlob)+4); |
| 657 | if( !zText ){ |
| 658 | sqlite3_result_error(context, "out of memory", -1); |
| 659 | }else{ |
| 660 | int i; |
| 661 | for(i=0; i<nBlob; i++){ |
| 662 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 663 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 664 | } |
| 665 | zText[(nBlob*2)+2] = '\''; |
| 666 | zText[(nBlob*2)+3] = '\0'; |
| 667 | zText[0] = 'X'; |
| 668 | zText[1] = '\''; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 669 | sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 670 | sqliteFree(zText); |
| 671 | } |
| 672 | break; |
| 673 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 674 | case SQLITE_TEXT: { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 675 | int i,j,n; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 676 | const char *zArg = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 677 | char *z; |
| 678 | |
| 679 | for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } |
| 680 | z = sqliteMalloc( i+n+3 ); |
| 681 | if( z==0 ) return; |
| 682 | z[0] = '\''; |
| 683 | for(i=0, j=1; zArg[i]; i++){ |
| 684 | z[j++] = zArg[i]; |
| 685 | if( zArg[i]=='\'' ){ |
| 686 | z[j++] = '\''; |
| 687 | } |
| 688 | } |
| 689 | z[j++] = '\''; |
| 690 | z[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 691 | sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 692 | sqliteFree(z); |
| 693 | } |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 697 | #ifdef SQLITE_SOUNDEX |
| 698 | /* |
| 699 | ** Compute the soundex encoding of a word. |
| 700 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 701 | static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 702 | char zResult[8]; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 703 | const u8 *zIn; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 704 | int i, j; |
| 705 | static const unsigned char iCode[] = { |
| 706 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 707 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 708 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 709 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 710 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 711 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 712 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 713 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 714 | }; |
| 715 | assert( argc==1 ); |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 716 | zIn = (u8*)sqlite3_value_text(argv[0]); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 717 | for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} |
| 718 | if( zIn[i] ){ |
| 719 | zResult[0] = toupper(zIn[i]); |
| 720 | for(j=1; j<4 && zIn[i]; i++){ |
| 721 | int code = iCode[zIn[i]&0x7f]; |
| 722 | if( code>0 ){ |
| 723 | zResult[j++] = code + '0'; |
| 724 | } |
| 725 | } |
| 726 | while( j<4 ){ |
| 727 | zResult[j++] = '0'; |
| 728 | } |
| 729 | zResult[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 730 | sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 731 | }else{ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 732 | sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | #endif |
| 736 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 737 | #ifdef SQLITE_TEST |
| 738 | /* |
| 739 | ** This function generates a string of random characters. Used for |
| 740 | ** generating test data. |
| 741 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 742 | static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 743 | static const unsigned char zSrc[] = |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 744 | "abcdefghijklmnopqrstuvwxyz" |
| 745 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 746 | "0123456789" |
| 747 | ".-!,:*^+=_|?/<> "; |
| 748 | int iMin, iMax, n, r, i; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 749 | unsigned char zBuf[1000]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 750 | if( argc>=1 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 751 | iMin = sqlite3_value_int(argv[0]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 752 | if( iMin<0 ) iMin = 0; |
| 753 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 754 | }else{ |
| 755 | iMin = 1; |
| 756 | } |
| 757 | if( argc>=2 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 758 | iMax = sqlite3_value_int(argv[1]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 759 | if( iMax<iMin ) iMax = iMin; |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 760 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 761 | }else{ |
| 762 | iMax = 50; |
| 763 | } |
| 764 | n = iMin; |
| 765 | if( iMax>iMin ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 766 | sqlite3Randomness(sizeof(r), &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 767 | r &= 0x7fffffff; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 768 | n += r%(iMax + 1 - iMin); |
| 769 | } |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 770 | assert( n<sizeof(zBuf) ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 771 | sqlite3Randomness(n, zBuf); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 772 | for(i=0; i<n; i++){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 773 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 774 | } |
| 775 | zBuf[n] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 776 | sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT); |
| 777 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 778 | #endif /* SQLITE_TEST */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 779 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 780 | #ifdef SQLITE_TEST |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 781 | /* |
| 782 | ** The following two SQL functions are used to test returning a text |
| 783 | ** result with a destructor. Function 'test_destructor' takes one argument |
| 784 | ** and returns the same argument interpreted as TEXT. A destructor is |
| 785 | ** passed with the sqlite3_result_text() call. |
| 786 | ** |
| 787 | ** SQL function 'test_destructor_count' returns the number of outstanding |
| 788 | ** allocations made by 'test_destructor'; |
| 789 | ** |
| 790 | ** WARNING: Not threadsafe. |
| 791 | */ |
| 792 | static int test_destructor_count_var = 0; |
| 793 | static void destructor(void *p){ |
| 794 | char *zVal = (char *)p; |
| 795 | assert(zVal); |
| 796 | zVal--; |
| 797 | sqliteFree(zVal); |
| 798 | test_destructor_count_var--; |
| 799 | } |
| 800 | static void test_destructor( |
| 801 | sqlite3_context *pCtx, |
| 802 | int nArg, |
| 803 | sqlite3_value **argv |
| 804 | ){ |
| 805 | char *zVal; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 806 | int len; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 807 | sqlite3 *db = sqlite3_user_data(pCtx); |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 808 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 809 | test_destructor_count_var++; |
| 810 | assert( nArg==1 ); |
| 811 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 812 | len = sqlite3ValueBytes(argv[0], db->enc); |
| 813 | zVal = sqliteMalloc(len+3); |
| 814 | zVal[len] = 0; |
| 815 | zVal[len-1] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 816 | assert( zVal ); |
| 817 | zVal++; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 818 | memcpy(zVal, sqlite3ValueText(argv[0], db->enc), len); |
| 819 | if( db->enc==SQLITE_UTF8 ){ |
| 820 | sqlite3_result_text(pCtx, zVal, -1, destructor); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 821 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 822 | }else if( db->enc==SQLITE_UTF16LE ){ |
| 823 | sqlite3_result_text16le(pCtx, zVal, -1, destructor); |
| 824 | }else{ |
| 825 | sqlite3_result_text16be(pCtx, zVal, -1, destructor); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 826 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 827 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 828 | } |
| 829 | static void test_destructor_count( |
| 830 | sqlite3_context *pCtx, |
| 831 | int nArg, |
| 832 | sqlite3_value **argv |
| 833 | ){ |
| 834 | sqlite3_result_int(pCtx, test_destructor_count_var); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 835 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 836 | #endif /* SQLITE_TEST */ |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 837 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 838 | #ifdef SQLITE_TEST |
| 839 | /* |
| 840 | ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() |
| 841 | ** interface. |
| 842 | ** |
| 843 | ** The test_auxdata() SQL function attempts to register each of its arguments |
| 844 | ** as auxiliary data. If there are no prior registrations of aux data for |
| 845 | ** that argument (meaning the argument is not a constant or this is its first |
| 846 | ** call) then the result for that argument is 0. If there is a prior |
| 847 | ** registration, the result for that argument is 1. The overall result |
| 848 | ** is the individual argument results separated by spaces. |
| 849 | */ |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 850 | static void free_test_auxdata(void *p) {sqliteFree(p);} |
| 851 | static void test_auxdata( |
| 852 | sqlite3_context *pCtx, |
| 853 | int nArg, |
| 854 | sqlite3_value **argv |
| 855 | ){ |
| 856 | int i; |
| 857 | char *zRet = sqliteMalloc(nArg*2); |
| 858 | if( !zRet ) return; |
| 859 | for(i=0; i<nArg; i++){ |
| 860 | char const *z = sqlite3_value_text(argv[i]); |
| 861 | if( z ){ |
| 862 | char *zAux = sqlite3_get_auxdata(pCtx, i); |
| 863 | if( zAux ){ |
| 864 | zRet[i*2] = '1'; |
| 865 | if( strcmp(zAux, z) ){ |
| 866 | sqlite3_result_error(pCtx, "Auxilary data corruption", -1); |
| 867 | return; |
| 868 | } |
| 869 | }else{ |
| 870 | zRet[i*2] = '0'; |
| 871 | zAux = sqliteStrDup(z); |
| 872 | sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); |
| 873 | } |
| 874 | zRet[i*2+1] = ' '; |
| 875 | } |
| 876 | } |
| 877 | sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); |
| 878 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 879 | #endif /* SQLITE_TEST */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 880 | |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 881 | #ifdef SQLITE_TEST |
| 882 | /* |
| 883 | ** A function to test error reporting from user functions. This function |
| 884 | ** returns a copy of it's first argument as an error. |
| 885 | */ |
| 886 | static void test_error( |
| 887 | sqlite3_context *pCtx, |
| 888 | int nArg, |
| 889 | sqlite3_value **argv |
| 890 | ){ |
danielk1977 | 24c8ab8 | 2005-02-09 01:40:23 +0000 | [diff] [blame] | 891 | sqlite3_result_error(pCtx, sqlite3_value_text(argv[0]), 0); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 892 | } |
| 893 | #endif /* SQLITE_TEST */ |
| 894 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 895 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 896 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 897 | ** sum() or avg() aggregate computation. |
| 898 | */ |
| 899 | typedef struct SumCtx SumCtx; |
| 900 | struct SumCtx { |
| 901 | double sum; /* Sum of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 902 | int cnt; /* Number of elements summed */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | /* |
| 906 | ** Routines used to compute the sum or average. |
| 907 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 908 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 909 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 910 | if( argc<1 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 911 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 912 | if( p && SQLITE_NULL!=sqlite3_value_type(argv[0]) ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 913 | p->sum += sqlite3_value_double(argv[0]); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 914 | p->cnt++; |
| 915 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 916 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 917 | static void sumFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 918 | SumCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 919 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 920 | sqlite3_result_double(context, p ? p->sum : 0.0); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 921 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 922 | static void avgFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 923 | SumCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 924 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 925 | if( p && p->cnt>0 ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 926 | sqlite3_result_double(context, p->sum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | ** An instance of the following structure holds the context of a |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 932 | ** variance or standard deviation computation. |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 933 | */ |
| 934 | typedef struct StdDevCtx StdDevCtx; |
| 935 | struct StdDevCtx { |
| 936 | double sum; /* Sum of terms */ |
| 937 | double sum2; /* Sum of the squares of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 938 | int cnt; /* Number of terms counted */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 939 | }; |
| 940 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 941 | /* |
| 942 | ** The following structure keeps track of state information for the |
| 943 | ** count() aggregate function. |
| 944 | */ |
| 945 | typedef struct CountCtx CountCtx; |
| 946 | struct CountCtx { |
| 947 | int n; |
| 948 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 949 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 950 | /* |
| 951 | ** Routines to implement the count() aggregate function. |
| 952 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 953 | static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 954 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 955 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 956 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 957 | p->n++; |
| 958 | } |
| 959 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 960 | static void countFinalize(sqlite3_context *context){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 961 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 962 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 963 | sqlite3_result_int(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /* |
| 967 | ** This function tracks state information for the min() and max() |
| 968 | ** aggregate functions. |
| 969 | */ |
| 970 | typedef struct MinMaxCtx MinMaxCtx; |
| 971 | struct MinMaxCtx { |
| 972 | char *z; /* The best so far */ |
| 973 | char zBuf[28]; /* Space that can be used for storage */ |
| 974 | }; |
| 975 | |
| 976 | /* |
| 977 | ** Routines to implement min() and max() aggregate functions. |
| 978 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 979 | static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 980 | Mem *pArg = (Mem *)argv[0]; |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 981 | Mem *pBest; |
| 982 | |
| 983 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 984 | pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 +0000 | [diff] [blame] | 985 | if( !pBest ) return; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 986 | |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 987 | if( pBest->flags ){ |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 988 | int max; |
| 989 | int cmp; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 990 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 991 | /* This step function is used for both the min() and max() aggregates, |
| 992 | ** the only difference between the two being that the sense of the |
| 993 | ** comparison is inverted. For the max() aggregate, the |
| 994 | ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 995 | ** returns (void *)db, where db is the sqlite3* database pointer. |
| 996 | ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 997 | ** aggregate, or 0 for min(). |
| 998 | */ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 999 | max = ((sqlite3_user_data(context)==(void *)-1)?1:0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1000 | cmp = sqlite3MemCompare(pBest, pArg, pColl); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1001 | if( (max && cmp<0) || (!max && cmp>0) ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1002 | sqlite3VdbeMemCopy(pBest, pArg); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1003 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1004 | }else{ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1005 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1008 | static void minMaxFinalize(sqlite3_context *context){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1009 | sqlite3_value *pRes; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1010 | pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1011 | if( pRes->flags ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1012 | sqlite3_result_value(context, pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1013 | } |
danielk1977 | b20e56b | 2004-06-15 13:36:30 +0000 | [diff] [blame] | 1014 | sqlite3VdbeMemRelease(pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1015 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1016 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 1017 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 1018 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 1019 | ** This function registered all of the above C functions as SQL |
| 1020 | ** functions. This should be the only routine in this file with |
| 1021 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1022 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1023 | void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1024 | static const struct { |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1025 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1026 | signed char nArg; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1027 | u8 argType; /* 0: none. 1: db 2: (-1) */ |
| 1028 | u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1029 | u8 needCollSeq; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1030 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1031 | } aFuncs[] = { |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1032 | { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, |
| 1033 | { "min", 0, 0, SQLITE_UTF8, 1, 0 }, |
| 1034 | { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc }, |
| 1035 | { "max", 0, 2, SQLITE_UTF8, 1, 0 }, |
| 1036 | { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, |
| 1037 | { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, |
| 1038 | { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1039 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1040 | { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1041 | #endif |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1042 | { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, |
| 1043 | { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, |
| 1044 | { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, |
| 1045 | { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, |
| 1046 | { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, |
| 1047 | { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, |
| 1048 | { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, |
| 1049 | { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, |
| 1050 | { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, |
| 1051 | { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, |
| 1052 | { "like", 2, 0, SQLITE_UTF8, 0, likeFunc }, |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 1053 | { "like", 3, 0, SQLITE_UTF8, 0, likeFunc }, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1054 | { "glob", 2, 0, SQLITE_UTF8, 0, globFunc }, |
drh | 94a9836 | 2004-09-13 13:13:18 +0000 | [diff] [blame] | 1055 | { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1056 | { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, |
| 1057 | { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, |
| 1058 | { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid }, |
| 1059 | { "changes", 0, 1, SQLITE_UTF8, 0, changes }, |
| 1060 | { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes }, |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame^] | 1061 | { "text", 1, 0, SQLITE_UTF8, 0, textFunc }, |
| 1062 | { "numeric", 1, 0, SQLITE_UTF8, 0, numericFunc }, |
| 1063 | { "blob", 1, 0, SQLITE_UTF8, 0, blobFunc }, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1064 | #ifdef SQLITE_SOUNDEX |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1065 | { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1066 | #endif |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1067 | #ifdef SQLITE_TEST |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1068 | { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, |
| 1069 | { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor}, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1070 | { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1071 | { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 1072 | { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1073 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1074 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1075 | static const struct { |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1076 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1077 | signed char nArg; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1078 | u8 argType; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1079 | u8 needCollSeq; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1080 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); |
| 1081 | void (*xFinalize)(sqlite3_context*); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1082 | } aAggs[] = { |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1083 | { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, |
| 1084 | { "max", 1, 2, 1, minmaxStep, minMaxFinalize }, |
| 1085 | { "sum", 1, 0, 0, sumStep, sumFinalize }, |
| 1086 | { "avg", 1, 0, 0, sumStep, avgFinalize }, |
| 1087 | { "count", 0, 0, 0, countStep, countFinalize }, |
| 1088 | { "count", 1, 0, 0, countStep, countFinalize }, |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1089 | }; |
| 1090 | int i; |
| 1091 | |
| 1092 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1093 | void *pArg = 0; |
| 1094 | switch( aFuncs[i].argType ){ |
| 1095 | case 1: pArg = db; break; |
| 1096 | case 2: pArg = (void *)(-1); break; |
| 1097 | } |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 1098 | sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1099 | aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1100 | if( aFuncs[i].needCollSeq ){ |
| 1101 | FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, |
| 1102 | strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); |
| 1103 | if( pFunc && aFuncs[i].needCollSeq ){ |
| 1104 | pFunc->needCollSeq = 1; |
| 1105 | } |
| 1106 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1107 | } |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 1108 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 1109 | sqlite3AlterFunctions(db); |
| 1110 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1111 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1112 | void *pArg = 0; |
| 1113 | switch( aAggs[i].argType ){ |
| 1114 | case 1: pArg = db; break; |
| 1115 | case 2: pArg = (void *)(-1); break; |
| 1116 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1117 | sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1118 | pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1119 | if( aAggs[i].needCollSeq ){ |
| 1120 | FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1121 | strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1122 | if( pFunc && aAggs[i].needCollSeq ){ |
| 1123 | pFunc->needCollSeq = 1; |
| 1124 | } |
| 1125 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1126 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1127 | sqlite3RegisterDateTimeFunctions(db); |
danielk1977 | fd9e1f3 | 2005-05-22 10:44:34 +0000 | [diff] [blame] | 1128 | #ifdef SQLITE_SSE |
| 1129 | { |
danielk1977 | fd9e1f3 | 2005-05-22 10:44:34 +0000 | [diff] [blame] | 1130 | sqlite3SseFunctions(db); |
| 1131 | } |
| 1132 | #endif |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1133 | } |