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