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