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