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