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