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 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.91 2004/11/19 05:14:55 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 | |
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; |
drh | c44af71 | 2004-09-02 15:53:56 +0000 | [diff] [blame] | 47 | mask = sqlite3_user_data(context)==0 ? 0 : -1; |
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 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 271 | sqlite3 *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 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 284 | sqlite3 *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 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 297 | sqlite3 *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 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 301 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 302 | ** A structure defining how to do GLOB-style comparisons. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 303 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 304 | struct compareInfo { |
| 305 | u8 matchAll; |
| 306 | u8 matchOne; |
| 307 | u8 matchSet; |
| 308 | u8 noCase; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 309 | }; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 310 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 311 | static const struct compareInfo likeInfo = { '%', '_', 0, 1 }; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 312 | |
| 313 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 314 | ** X is a pointer to the first byte of a UTF-8 character. Increment |
| 315 | ** X so that it points to the next character. This only works right |
| 316 | ** if X points to a well-formed UTF-8 string. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 317 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 318 | #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} |
| 319 | #define sqliteCharVal(X) sqlite3ReadUtf8(X) |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 320 | |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 321 | |
| 322 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 323 | ** Compare two UTF-8 strings for equality where the first string can |
| 324 | ** potentially be a "glob" expression. Return true (1) if they |
| 325 | ** are the same and false (0) if they are different. |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 326 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 327 | ** Globbing rules: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 328 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 329 | ** '*' Matches any sequence of zero or more characters. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 330 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 331 | ** '?' Matches exactly one character. |
| 332 | ** |
| 333 | ** [...] Matches one character from the enclosed list of |
| 334 | ** characters. |
| 335 | ** |
| 336 | ** [^...] Matches one character not in the enclosed list. |
| 337 | ** |
| 338 | ** With the [...] and [^...] matching, a ']' character can be included |
| 339 | ** in the list by making it the first character after '[' or '^'. A |
| 340 | ** range of characters can be specified using '-'. Example: |
| 341 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 342 | ** it the last character in the list. |
| 343 | ** |
| 344 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 345 | ** |
| 346 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 347 | ** |
| 348 | ** abc[*]xyz Matches "abc*xyz" only |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 349 | */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 350 | static int patternCompare( |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 351 | const u8 *zPattern, /* The glob pattern */ |
| 352 | const u8 *zString, /* The string to compare against the glob */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 353 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 354 | const int esc /* The escape character */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 355 | ){ |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 356 | register int c; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 357 | int invert; |
| 358 | int seen; |
| 359 | int c2; |
| 360 | u8 matchOne = pInfo->matchOne; |
| 361 | u8 matchAll = pInfo->matchAll; |
| 362 | u8 matchSet = pInfo->matchSet; |
| 363 | u8 noCase = pInfo->noCase; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 364 | int prevEscape = 0; /* True if the previous character was 'escape' */ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 365 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 366 | while( (c = *zPattern)!=0 ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 367 | if( !prevEscape && c==matchAll ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 368 | while( (c=zPattern[1]) == matchAll || c == matchOne ){ |
| 369 | if( c==matchOne ){ |
| 370 | if( *zString==0 ) return 0; |
| 371 | sqliteNextChar(zString); |
| 372 | } |
| 373 | zPattern++; |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 374 | } |
drh | 20fc088 | 2004-11-18 13:49:25 +0000 | [diff] [blame] | 375 | if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 376 | u8 const *zTemp = &zPattern[1]; |
| 377 | sqliteNextChar(zTemp); |
| 378 | c = *zTemp; |
| 379 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 380 | if( c==0 ) return 1; |
| 381 | if( c==matchSet ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 382 | assert( esc==0 ); /* This is GLOB, not LIKE */ |
| 383 | while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 384 | sqliteNextChar(zString); |
| 385 | } |
| 386 | return *zString!=0; |
| 387 | }else{ |
| 388 | while( (c2 = *zString)!=0 ){ |
| 389 | if( noCase ){ |
| 390 | c2 = sqlite3UpperToLower[c2]; |
| 391 | c = sqlite3UpperToLower[c]; |
| 392 | while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } |
| 393 | }else{ |
| 394 | while( c2 != 0 && c2 != c ){ c2 = *++zString; } |
| 395 | } |
| 396 | if( c2==0 ) return 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 397 | if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 398 | sqliteNextChar(zString); |
| 399 | } |
| 400 | return 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 401 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 402 | }else if( !prevEscape && c==matchOne ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 403 | if( *zString==0 ) return 0; |
| 404 | sqliteNextChar(zString); |
| 405 | zPattern++; |
| 406 | }else if( c==matchSet ){ |
| 407 | int prior_c = 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 408 | assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 409 | seen = 0; |
| 410 | invert = 0; |
| 411 | c = sqliteCharVal(zString); |
| 412 | if( c==0 ) return 0; |
| 413 | c2 = *++zPattern; |
| 414 | if( c2=='^' ){ invert = 1; c2 = *++zPattern; } |
| 415 | if( c2==']' ){ |
| 416 | if( c==']' ) seen = 1; |
| 417 | c2 = *++zPattern; |
| 418 | } |
| 419 | while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ |
| 420 | if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ |
| 421 | zPattern++; |
| 422 | c2 = sqliteCharVal(zPattern); |
| 423 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 424 | prior_c = 0; |
| 425 | }else if( c==c2 ){ |
| 426 | seen = 1; |
| 427 | prior_c = c2; |
| 428 | }else{ |
| 429 | prior_c = c2; |
| 430 | } |
| 431 | sqliteNextChar(zPattern); |
| 432 | } |
| 433 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 434 | sqliteNextChar(zString); |
| 435 | zPattern++; |
drh | 20fc088 | 2004-11-18 13:49:25 +0000 | [diff] [blame] | 436 | }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 437 | prevEscape = 1; |
| 438 | sqliteNextChar(zPattern); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 439 | }else{ |
| 440 | if( noCase ){ |
| 441 | if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; |
| 442 | }else{ |
| 443 | if( c != *zString ) return 0; |
| 444 | } |
| 445 | zPattern++; |
| 446 | zString++; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 447 | prevEscape = 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 448 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 449 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 450 | return *zString==0; |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 451 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 452 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | ** Implementation of the like() SQL function. This function implements |
| 456 | ** the build-in LIKE operator. The first argument to the function is the |
| 457 | ** pattern and the second argument is the string. So, the SQL statements: |
| 458 | ** |
| 459 | ** A LIKE B |
| 460 | ** |
| 461 | ** is implemented as like(B,A). |
| 462 | ** |
| 463 | ** If the pointer retrieved by via a call to sqlite3_user_data() is |
| 464 | ** not NULL, then this function uses UTF-16. Otherwise UTF-8. |
| 465 | */ |
| 466 | static void likeFunc( |
| 467 | sqlite3_context *context, |
| 468 | int argc, |
| 469 | sqlite3_value **argv |
| 470 | ){ |
| 471 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 472 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 473 | int escape = 0; |
| 474 | if( argc==3 ){ |
| 475 | /* The escape character string must consist of a single UTF-8 character. |
| 476 | ** Otherwise, return an error. |
| 477 | */ |
| 478 | const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
| 479 | if( sqlite3utf8CharLen(zEsc, -1)!=1 ){ |
| 480 | sqlite3_result_error(context, |
| 481 | "ESCAPE expression must be a single character", -1); |
| 482 | return; |
| 483 | } |
| 484 | escape = sqlite3ReadUtf8(zEsc); |
| 485 | } |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 486 | if( zA && zB ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 487 | sqlite3_result_int(context, patternCompare(zA, zB, &likeInfo, escape)); |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | ** Implementation of the glob() SQL function. This function implements |
| 493 | ** the build-in GLOB operator. The first argument to the function is the |
| 494 | ** string and the second argument is the pattern. So, the SQL statements: |
| 495 | ** |
| 496 | ** A GLOB B |
| 497 | ** |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 498 | ** is implemented as glob(B,A). |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 499 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 500 | static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 501 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 502 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 503 | if( zA && zB ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 504 | sqlite3_result_int(context, patternCompare(zA, zB, &globInfo, 0)); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 505 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 510 | ** argument if the arguments are different. The result is NULL if the |
| 511 | ** arguments are equal to each other. |
| 512 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 513 | static void nullifFunc( |
| 514 | sqlite3_context *context, |
| 515 | int argc, |
| 516 | sqlite3_value **argv |
| 517 | ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 518 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
| 519 | if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 520 | sqlite3_result_value(context, argv[0]); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 521 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 522 | } |
| 523 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 524 | /* |
| 525 | ** Implementation of the VERSION(*) function. The result is the version |
| 526 | ** of the SQLite library that is running. |
| 527 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 528 | static void versionFunc( |
| 529 | sqlite3_context *context, |
| 530 | int argc, |
| 531 | sqlite3_value **argv |
| 532 | ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 533 | sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 534 | } |
| 535 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 536 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 537 | /* |
| 538 | ** This function is used by SQL generated to implement the |
| 539 | ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or |
| 540 | ** CREATE INDEX command. The second is a table name. The table name in |
| 541 | ** the CREATE TABLE or CREATE INDEX statement is replaced with the second |
| 542 | ** argument and the result returned. Examples: |
| 543 | ** |
| 544 | ** sqlite_alter_table('CREATE TABLE abc(a, b, c)', 'def') |
| 545 | ** -> 'CREATE TABLE def(a, b, c)' |
| 546 | ** |
| 547 | ** sqlite_alter_table('CREATE INDEX i ON abc(a)', 'def') |
| 548 | ** -> 'CREATE INDEX i ON def(a, b, c)' |
| 549 | */ |
| 550 | static void altertableFunc( |
| 551 | sqlite3_context *context, |
| 552 | int argc, |
| 553 | sqlite3_value **argv |
| 554 | ){ |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 555 | unsigned char const *zSql = sqlite3_value_text(argv[0]); |
| 556 | unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 557 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 558 | int token; |
| 559 | Token tname; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 560 | char const *zCsr = zSql; |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 561 | int len = 0; |
| 562 | char *zRet; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 563 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 564 | /* The principle used to locate the table name in the CREATE TABLE |
| 565 | ** statement is that the table name is the first token that is immediatedly |
| 566 | ** followed by a left parenthesis - TK_LP. |
| 567 | */ |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 568 | if( zSql ){ |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 569 | do { |
| 570 | /* Store the token that zCsr points to in tname. */ |
| 571 | tname.z = zCsr; |
| 572 | tname.n = len; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 573 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 574 | /* Advance zCsr to the next token. Store that token type in 'token', |
| 575 | ** and it's length in 'len' (to be used next iteration of this loop). |
| 576 | */ |
| 577 | do { |
| 578 | zCsr += len; |
| 579 | len = sqlite3GetToken(zCsr, &token); |
| 580 | } while( token==TK_SPACE ); |
| 581 | assert( len>0 ); |
| 582 | } while( token!=TK_LP ); |
| 583 | |
| 584 | zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql, |
| 585 | zTableName, tname.z+tname.n); |
| 586 | sqlite3_result_text(context, zRet, -1, sqlite3FreeX); |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | #endif |
| 590 | |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 591 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 592 | #ifndef SQLITE_OMIT_TRIGGER |
| 593 | /* This function is used by SQL generated to implement the ALTER TABLE |
| 594 | ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER |
| 595 | ** statement. The second is a table name. The table name in the CREATE |
| 596 | ** TRIGGER statement is replaced with the second argument and the result |
| 597 | ** returned. This is analagous to altertableFunc() above, except for CREATE |
| 598 | ** TRIGGER, not CREATE INDEX and CREATE TABLE. |
| 599 | */ |
| 600 | static void altertriggerFunc( |
| 601 | sqlite3_context *context, |
| 602 | int argc, |
| 603 | sqlite3_value **argv |
| 604 | ){ |
| 605 | unsigned char const *zSql = sqlite3_value_text(argv[0]); |
| 606 | unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
| 607 | |
| 608 | int token; |
| 609 | Token tname; |
| 610 | int dist = 3; |
| 611 | char const *zCsr = zSql; |
| 612 | int len = 0; |
| 613 | char *zRet; |
| 614 | |
| 615 | /* The principle used to locate the table name in the CREATE TRIGGER |
| 616 | ** statement is that the table name is the first token that is immediatedly |
| 617 | ** preceded by either TK_ON or TK_DOT and immediatedly followed by one |
| 618 | ** of TK_WHEN, TK_BEGIN or TK_FOR. |
| 619 | */ |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 620 | if( zSql ){ |
| 621 | do { |
| 622 | /* Store the token that zCsr points to in tname. */ |
| 623 | tname.z = zCsr; |
| 624 | tname.n = len; |
| 625 | |
| 626 | /* Advance zCsr to the next token. Store that token type in 'token', |
| 627 | ** and it's length in 'len' (to be used next iteration of this loop). |
| 628 | */ |
| 629 | do { |
| 630 | zCsr += len; |
| 631 | len = sqlite3GetToken(zCsr, &token); |
| 632 | }while( token==TK_SPACE ); |
| 633 | assert( len>0 ); |
| 634 | |
| 635 | /* Variable 'dist' stores the number of tokens read since the most |
| 636 | ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN |
| 637 | ** token is read and 'dist' equals 2, the condition stated above |
| 638 | ** to be met. |
| 639 | ** |
| 640 | ** Note that ON cannot be a database, table or column name, so |
| 641 | ** there is no need to worry about syntax like |
| 642 | ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. |
| 643 | */ |
| 644 | dist++; |
| 645 | if( token==TK_DOT || token==TK_ON ){ |
| 646 | dist = 0; |
| 647 | } |
| 648 | } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); |
| 649 | |
| 650 | /* Variable tname now contains the token that is the old table-name |
| 651 | ** in the CREATE TRIGGER statement. |
| 652 | */ |
| 653 | zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql, |
| 654 | zTableName, tname.z+tname.n); |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame^] | 655 | sqlite3_result_text(context, zRet, -1, sqlite3FreeX); |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | #endif /* !SQLITE_OMIT_TRIGGER */ |
| 659 | #endif /* !SQLITE_OMIT_ALTERTABLE */ |
| 660 | |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 661 | /* |
| 662 | ** EXPERIMENTAL - This is not an official function. The interface may |
| 663 | ** change. This function may disappear. Do not write code that depends |
| 664 | ** on this function. |
| 665 | ** |
| 666 | ** Implementation of the QUOTE() function. This function takes a single |
| 667 | ** argument. If the argument is numeric, the return value is the same as |
| 668 | ** the argument. If the argument is NULL, the return value is the string |
| 669 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 670 | ** single-quote escapes. |
| 671 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 672 | static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 673 | if( argc<1 ) return; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 674 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 675 | case SQLITE_NULL: { |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 676 | sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 677 | break; |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 678 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 679 | case SQLITE_INTEGER: |
| 680 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 681 | sqlite3_result_value(context, argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 682 | break; |
| 683 | } |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 684 | case SQLITE_BLOB: { |
| 685 | static const char hexdigits[] = { |
| 686 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 687 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 688 | }; |
| 689 | char *zText = 0; |
| 690 | int nBlob = sqlite3_value_bytes(argv[0]); |
| 691 | char const *zBlob = sqlite3_value_blob(argv[0]); |
| 692 | |
| 693 | zText = (char *)sqliteMalloc((2*nBlob)+4); |
| 694 | if( !zText ){ |
| 695 | sqlite3_result_error(context, "out of memory", -1); |
| 696 | }else{ |
| 697 | int i; |
| 698 | for(i=0; i<nBlob; i++){ |
| 699 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 700 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 701 | } |
| 702 | zText[(nBlob*2)+2] = '\''; |
| 703 | zText[(nBlob*2)+3] = '\0'; |
| 704 | zText[0] = 'X'; |
| 705 | zText[1] = '\''; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 706 | sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 707 | sqliteFree(zText); |
| 708 | } |
| 709 | break; |
| 710 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 711 | case SQLITE_TEXT: { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 712 | int i,j,n; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 713 | const char *zArg = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 714 | char *z; |
| 715 | |
| 716 | for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } |
| 717 | z = sqliteMalloc( i+n+3 ); |
| 718 | if( z==0 ) return; |
| 719 | z[0] = '\''; |
| 720 | for(i=0, j=1; zArg[i]; i++){ |
| 721 | z[j++] = zArg[i]; |
| 722 | if( zArg[i]=='\'' ){ |
| 723 | z[j++] = '\''; |
| 724 | } |
| 725 | } |
| 726 | z[j++] = '\''; |
| 727 | z[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 728 | sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 729 | sqliteFree(z); |
| 730 | } |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 734 | #ifdef SQLITE_SOUNDEX |
| 735 | /* |
| 736 | ** Compute the soundex encoding of a word. |
| 737 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 738 | static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 739 | char zResult[8]; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 740 | const u8 *zIn; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 741 | int i, j; |
| 742 | static const unsigned char iCode[] = { |
| 743 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 744 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 745 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 746 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 747 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 748 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 749 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 750 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 751 | }; |
| 752 | assert( argc==1 ); |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 753 | zIn = (u8*)sqlite3_value_text(argv[0]); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 754 | for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} |
| 755 | if( zIn[i] ){ |
| 756 | zResult[0] = toupper(zIn[i]); |
| 757 | for(j=1; j<4 && zIn[i]; i++){ |
| 758 | int code = iCode[zIn[i]&0x7f]; |
| 759 | if( code>0 ){ |
| 760 | zResult[j++] = code + '0'; |
| 761 | } |
| 762 | } |
| 763 | while( j<4 ){ |
| 764 | zResult[j++] = '0'; |
| 765 | } |
| 766 | zResult[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 767 | sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 768 | }else{ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 769 | sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | #endif |
| 773 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 774 | #ifdef SQLITE_TEST |
| 775 | /* |
| 776 | ** This function generates a string of random characters. Used for |
| 777 | ** generating test data. |
| 778 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 779 | static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 780 | static const unsigned char zSrc[] = |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 781 | "abcdefghijklmnopqrstuvwxyz" |
| 782 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 783 | "0123456789" |
| 784 | ".-!,:*^+=_|?/<> "; |
| 785 | int iMin, iMax, n, r, i; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 786 | unsigned char zBuf[1000]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 787 | if( argc>=1 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 788 | iMin = sqlite3_value_int(argv[0]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 789 | if( iMin<0 ) iMin = 0; |
| 790 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 791 | }else{ |
| 792 | iMin = 1; |
| 793 | } |
| 794 | if( argc>=2 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 795 | iMax = sqlite3_value_int(argv[1]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 796 | if( iMax<iMin ) iMax = iMin; |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 797 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 798 | }else{ |
| 799 | iMax = 50; |
| 800 | } |
| 801 | n = iMin; |
| 802 | if( iMax>iMin ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 803 | sqlite3Randomness(sizeof(r), &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 804 | r &= 0x7fffffff; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 805 | n += r%(iMax + 1 - iMin); |
| 806 | } |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 807 | assert( n<sizeof(zBuf) ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 808 | sqlite3Randomness(n, zBuf); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 809 | for(i=0; i<n; i++){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 810 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 811 | } |
| 812 | zBuf[n] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 813 | sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT); |
| 814 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 815 | #endif /* SQLITE_TEST */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 816 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 817 | #ifdef SQLITE_TEST |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 818 | /* |
| 819 | ** The following two SQL functions are used to test returning a text |
| 820 | ** result with a destructor. Function 'test_destructor' takes one argument |
| 821 | ** and returns the same argument interpreted as TEXT. A destructor is |
| 822 | ** passed with the sqlite3_result_text() call. |
| 823 | ** |
| 824 | ** SQL function 'test_destructor_count' returns the number of outstanding |
| 825 | ** allocations made by 'test_destructor'; |
| 826 | ** |
| 827 | ** WARNING: Not threadsafe. |
| 828 | */ |
| 829 | static int test_destructor_count_var = 0; |
| 830 | static void destructor(void *p){ |
| 831 | char *zVal = (char *)p; |
| 832 | assert(zVal); |
| 833 | zVal--; |
| 834 | sqliteFree(zVal); |
| 835 | test_destructor_count_var--; |
| 836 | } |
| 837 | static void test_destructor( |
| 838 | sqlite3_context *pCtx, |
| 839 | int nArg, |
| 840 | sqlite3_value **argv |
| 841 | ){ |
| 842 | char *zVal; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 843 | int len; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 844 | sqlite3 *db = sqlite3_user_data(pCtx); |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 845 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 846 | test_destructor_count_var++; |
| 847 | assert( nArg==1 ); |
| 848 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 849 | len = sqlite3ValueBytes(argv[0], db->enc); |
| 850 | zVal = sqliteMalloc(len+3); |
| 851 | zVal[len] = 0; |
| 852 | zVal[len-1] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 853 | assert( zVal ); |
| 854 | zVal++; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 855 | memcpy(zVal, sqlite3ValueText(argv[0], db->enc), len); |
| 856 | if( db->enc==SQLITE_UTF8 ){ |
| 857 | sqlite3_result_text(pCtx, zVal, -1, destructor); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 858 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 859 | }else if( db->enc==SQLITE_UTF16LE ){ |
| 860 | sqlite3_result_text16le(pCtx, zVal, -1, destructor); |
| 861 | }else{ |
| 862 | sqlite3_result_text16be(pCtx, zVal, -1, destructor); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 863 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 864 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 865 | } |
| 866 | static void test_destructor_count( |
| 867 | sqlite3_context *pCtx, |
| 868 | int nArg, |
| 869 | sqlite3_value **argv |
| 870 | ){ |
| 871 | sqlite3_result_int(pCtx, test_destructor_count_var); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 872 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 873 | #endif /* SQLITE_TEST */ |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 874 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 875 | #ifdef SQLITE_TEST |
| 876 | /* |
| 877 | ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() |
| 878 | ** interface. |
| 879 | ** |
| 880 | ** The test_auxdata() SQL function attempts to register each of its arguments |
| 881 | ** as auxiliary data. If there are no prior registrations of aux data for |
| 882 | ** that argument (meaning the argument is not a constant or this is its first |
| 883 | ** call) then the result for that argument is 0. If there is a prior |
| 884 | ** registration, the result for that argument is 1. The overall result |
| 885 | ** is the individual argument results separated by spaces. |
| 886 | */ |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 887 | static void free_test_auxdata(void *p) {sqliteFree(p);} |
| 888 | static void test_auxdata( |
| 889 | sqlite3_context *pCtx, |
| 890 | int nArg, |
| 891 | sqlite3_value **argv |
| 892 | ){ |
| 893 | int i; |
| 894 | char *zRet = sqliteMalloc(nArg*2); |
| 895 | if( !zRet ) return; |
| 896 | for(i=0; i<nArg; i++){ |
| 897 | char const *z = sqlite3_value_text(argv[i]); |
| 898 | if( z ){ |
| 899 | char *zAux = sqlite3_get_auxdata(pCtx, i); |
| 900 | if( zAux ){ |
| 901 | zRet[i*2] = '1'; |
| 902 | if( strcmp(zAux, z) ){ |
| 903 | sqlite3_result_error(pCtx, "Auxilary data corruption", -1); |
| 904 | return; |
| 905 | } |
| 906 | }else{ |
| 907 | zRet[i*2] = '0'; |
| 908 | zAux = sqliteStrDup(z); |
| 909 | sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); |
| 910 | } |
| 911 | zRet[i*2+1] = ' '; |
| 912 | } |
| 913 | } |
| 914 | sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); |
| 915 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 916 | #endif /* SQLITE_TEST */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 917 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 918 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 919 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 920 | ** sum() or avg() aggregate computation. |
| 921 | */ |
| 922 | typedef struct SumCtx SumCtx; |
| 923 | struct SumCtx { |
| 924 | double sum; /* Sum of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 925 | int cnt; /* Number of elements summed */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 926 | }; |
| 927 | |
| 928 | /* |
| 929 | ** Routines used to compute the sum or average. |
| 930 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 931 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 932 | SumCtx *p; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 933 | if( argc<1 ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 934 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 935 | if( p && SQLITE_NULL!=sqlite3_value_type(argv[0]) ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 936 | p->sum += sqlite3_value_double(argv[0]); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 937 | p->cnt++; |
| 938 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 939 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 940 | static void sumFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 941 | SumCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 942 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 943 | sqlite3_result_double(context, p ? p->sum : 0.0); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 944 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 945 | static void avgFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 946 | SumCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 947 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 948 | if( p && p->cnt>0 ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 949 | sqlite3_result_double(context, p->sum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | ** An instance of the following structure holds the context of a |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 955 | ** variance or standard deviation computation. |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 956 | */ |
| 957 | typedef struct StdDevCtx StdDevCtx; |
| 958 | struct StdDevCtx { |
| 959 | double sum; /* Sum of terms */ |
| 960 | double sum2; /* Sum of the squares of terms */ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 961 | int cnt; /* Number of terms counted */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 962 | }; |
| 963 | |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 964 | #if 0 /* Omit because math library is required */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 965 | /* |
| 966 | ** Routines used to compute the standard deviation as an aggregate. |
| 967 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 968 | static void stdDevStep(sqlite3_context *context, int argc, const char **argv){ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 969 | StdDevCtx *p; |
| 970 | double x; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 971 | if( argc<1 ) return; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 972 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 973 | if( p && argv[0] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 974 | x = sqlite3AtoF(argv[0], 0); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 975 | p->sum += x; |
| 976 | p->sum2 += x*x; |
| 977 | p->cnt++; |
| 978 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 979 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 980 | static void stdDevFinalize(sqlite3_context *context){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 981 | double rN = sqlite3_aggregate_count(context); |
| 982 | StdDevCtx *p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 983 | if( p && p->cnt>1 ){ |
| 984 | double rCnt = cnt; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 985 | sqlite3_set_result_double(context, |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 986 | sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0))); |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 987 | } |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 988 | } |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 989 | #endif |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 990 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 991 | /* |
| 992 | ** The following structure keeps track of state information for the |
| 993 | ** count() aggregate function. |
| 994 | */ |
| 995 | typedef struct CountCtx CountCtx; |
| 996 | struct CountCtx { |
| 997 | int n; |
| 998 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 999 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1000 | /* |
| 1001 | ** Routines to implement the count() aggregate function. |
| 1002 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1003 | static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1004 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1005 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1006 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1007 | p->n++; |
| 1008 | } |
| 1009 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1010 | static void countFinalize(sqlite3_context *context){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1011 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1012 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1013 | sqlite3_result_int(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | ** This function tracks state information for the min() and max() |
| 1018 | ** aggregate functions. |
| 1019 | */ |
| 1020 | typedef struct MinMaxCtx MinMaxCtx; |
| 1021 | struct MinMaxCtx { |
| 1022 | char *z; /* The best so far */ |
| 1023 | char zBuf[28]; /* Space that can be used for storage */ |
| 1024 | }; |
| 1025 | |
| 1026 | /* |
| 1027 | ** Routines to implement min() and max() aggregate functions. |
| 1028 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1029 | static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1030 | Mem *pArg = (Mem *)argv[0]; |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 1031 | Mem *pBest; |
| 1032 | |
| 1033 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 1034 | pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 +0000 | [diff] [blame] | 1035 | if( !pBest ) return; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1036 | |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1037 | if( pBest->flags ){ |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 1038 | int max; |
| 1039 | int cmp; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1040 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1041 | /* This step function is used for both the min() and max() aggregates, |
| 1042 | ** the only difference between the two being that the sense of the |
| 1043 | ** comparison is inverted. For the max() aggregate, the |
| 1044 | ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 1045 | ** returns (void *)db, where db is the sqlite3* database pointer. |
| 1046 | ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 1047 | ** aggregate, or 0 for min(). |
| 1048 | */ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1049 | max = ((sqlite3_user_data(context)==(void *)-1)?1:0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1050 | cmp = sqlite3MemCompare(pBest, pArg, pColl); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1051 | if( (max && cmp<0) || (!max && cmp>0) ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1052 | sqlite3VdbeMemCopy(pBest, pArg); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1053 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1054 | }else{ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1055 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1058 | static void minMaxFinalize(sqlite3_context *context){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1059 | sqlite3_value *pRes; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1060 | pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1061 | if( pRes->flags ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1062 | sqlite3_result_value(context, pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1063 | } |
danielk1977 | b20e56b | 2004-06-15 13:36:30 +0000 | [diff] [blame] | 1064 | sqlite3VdbeMemRelease(pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1065 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1066 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 1067 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 1068 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 1069 | ** This function registered all of the above C functions as SQL |
| 1070 | ** functions. This should be the only routine in this file with |
| 1071 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1072 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1073 | void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1074 | static const struct { |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1075 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1076 | signed char nArg; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1077 | u8 argType; /* 0: none. 1: db 2: (-1) */ |
| 1078 | u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1079 | u8 needCollSeq; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1080 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1081 | } aFuncs[] = { |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1082 | { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, |
| 1083 | { "min", 0, 0, SQLITE_UTF8, 1, 0 }, |
| 1084 | { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc }, |
| 1085 | { "max", 0, 2, SQLITE_UTF8, 1, 0 }, |
| 1086 | { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, |
| 1087 | { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, |
| 1088 | { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1089 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1090 | { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1091 | #endif |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1092 | { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, |
| 1093 | { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, |
| 1094 | { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, |
| 1095 | { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, |
| 1096 | { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, |
| 1097 | { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, |
| 1098 | { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, |
| 1099 | { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, |
| 1100 | { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, |
| 1101 | { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, |
| 1102 | { "like", 2, 0, SQLITE_UTF8, 0, likeFunc }, |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 1103 | { "like", 3, 0, SQLITE_UTF8, 0, likeFunc }, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1104 | { "glob", 2, 0, SQLITE_UTF8, 0, globFunc }, |
drh | 94a9836 | 2004-09-13 13:13:18 +0000 | [diff] [blame] | 1105 | { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1106 | { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, |
| 1107 | { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, |
| 1108 | { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid }, |
| 1109 | { "changes", 0, 1, SQLITE_UTF8, 0, changes }, |
| 1110 | { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes }, |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1111 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 1112 | { "sqlite_alter_table", 2, 0, SQLITE_UTF8, 0, altertableFunc}, |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 1113 | #ifndef SQLITE_OMIT_TRIGGER |
| 1114 | { "sqlite_alter_trigger", 2, 0, SQLITE_UTF8, 0, altertriggerFunc}, |
| 1115 | #endif |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1116 | #endif |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1117 | #ifdef SQLITE_SOUNDEX |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1118 | { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1119 | #endif |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1120 | #ifdef SQLITE_TEST |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1121 | { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, |
| 1122 | { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor}, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1123 | { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1124 | { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1125 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1126 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1127 | static const struct { |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1128 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1129 | signed char nArg; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1130 | u8 argType; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1131 | u8 needCollSeq; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1132 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); |
| 1133 | void (*xFinalize)(sqlite3_context*); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1134 | } aAggs[] = { |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1135 | { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, |
| 1136 | { "max", 1, 2, 1, minmaxStep, minMaxFinalize }, |
| 1137 | { "sum", 1, 0, 0, sumStep, sumFinalize }, |
| 1138 | { "avg", 1, 0, 0, sumStep, avgFinalize }, |
| 1139 | { "count", 0, 0, 0, countStep, countFinalize }, |
| 1140 | { "count", 1, 0, 0, countStep, countFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 1141 | #if 0 |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1142 | { "stddev", 1, 0, stdDevStep, stdDevFinalize }, |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 1143 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1144 | }; |
| 1145 | int i; |
| 1146 | |
| 1147 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1148 | void *pArg = 0; |
| 1149 | switch( aFuncs[i].argType ){ |
| 1150 | case 1: pArg = db; break; |
| 1151 | case 2: pArg = (void *)(-1); break; |
| 1152 | } |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 1153 | sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1154 | aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1155 | if( aFuncs[i].needCollSeq ){ |
| 1156 | FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, |
| 1157 | strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); |
| 1158 | if( pFunc && aFuncs[i].needCollSeq ){ |
| 1159 | pFunc->needCollSeq = 1; |
| 1160 | } |
| 1161 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1162 | } |
| 1163 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1164 | void *pArg = 0; |
| 1165 | switch( aAggs[i].argType ){ |
| 1166 | case 1: pArg = db; break; |
| 1167 | case 2: pArg = (void *)(-1); break; |
| 1168 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1169 | sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1170 | pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1171 | if( aAggs[i].needCollSeq ){ |
| 1172 | FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1173 | strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1174 | if( pFunc && aAggs[i].needCollSeq ){ |
| 1175 | pFunc->needCollSeq = 1; |
| 1176 | } |
| 1177 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1178 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1179 | sqlite3RegisterDateTimeFunctions(db); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1180 | } |