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 | 88897a7 | 2006-06-13 19:26:10 +0000 | [diff] [blame] | 19 | ** $Id: func.c,v 1.130 2006/06/13 19:26:11 drh Exp $ |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 20 | */ |
drh | b659e9b | 2005-01-28 01:29:08 +0000 | [diff] [blame] | 21 | #include "sqliteInt.h" |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 23 | /* #include <math.h> */ |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 25 | #include <assert.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 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 29 | /* |
| 30 | ** Return the collating function associated with a function. |
| 31 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 32 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 33 | return context->pColl; |
| 34 | } |
| 35 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 36 | /* |
| 37 | ** Implementation of the non-aggregate min() and max() functions |
| 38 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 39 | static void minmaxFunc( |
| 40 | sqlite3_context *context, |
| 41 | int argc, |
| 42 | sqlite3_value **argv |
| 43 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 44 | int i; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 45 | int mask; /* 0 for min() or 0xffffffff for max() */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 46 | int iBest; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 47 | CollSeq *pColl; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 48 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 49 | if( argc==0 ) return; |
drh | c44af71 | 2004-09-02 15:53:56 +0000 | [diff] [blame] | 50 | mask = sqlite3_user_data(context)==0 ? 0 : -1; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 51 | pColl = sqlite3GetFuncCollSeq(context); |
| 52 | assert( pColl ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 53 | assert( mask==-1 || mask==0 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 54 | iBest = 0; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 55 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 56 | for(i=1; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 57 | if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 58 | if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 59 | iBest = i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 62 | sqlite3_result_value(context, argv[iBest]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 63 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 64 | |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 65 | /* |
| 66 | ** Return the type of the argument. |
| 67 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 68 | static void typeofFunc( |
| 69 | sqlite3_context *context, |
| 70 | int argc, |
| 71 | sqlite3_value **argv |
| 72 | ){ |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 73 | const char *z = 0; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 74 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 75 | case SQLITE_NULL: z = "null"; break; |
| 76 | case SQLITE_INTEGER: z = "integer"; break; |
| 77 | case SQLITE_TEXT: z = "text"; break; |
| 78 | case SQLITE_FLOAT: z = "real"; break; |
| 79 | case SQLITE_BLOB: z = "blob"; break; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 80 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 81 | sqlite3_result_text(context, z, -1, SQLITE_STATIC); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 82 | } |
| 83 | |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 86 | ** Implementation of the length() function |
| 87 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 88 | static void lengthFunc( |
| 89 | sqlite3_context *context, |
| 90 | int argc, |
| 91 | sqlite3_value **argv |
| 92 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 93 | int len; |
| 94 | |
| 95 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 96 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 97 | case SQLITE_BLOB: |
| 98 | case SQLITE_INTEGER: |
| 99 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 100 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 101 | break; |
| 102 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 103 | case SQLITE_TEXT: { |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 104 | const unsigned char *z = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 105 | for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 106 | sqlite3_result_int(context, len); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | default: { |
| 110 | sqlite3_result_null(context); |
| 111 | break; |
| 112 | } |
| 113 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | ** Implementation of the abs() function |
| 118 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 119 | static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 120 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 121 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 122 | case SQLITE_INTEGER: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 123 | i64 iVal = sqlite3_value_int64(argv[0]); |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 124 | if( iVal<0 ){ |
| 125 | if( (iVal<<1)==0 ){ |
| 126 | sqlite3_result_error(context, "integer overflow", -1); |
| 127 | return; |
| 128 | } |
| 129 | iVal = -iVal; |
| 130 | } |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 131 | sqlite3_result_int64(context, iVal); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 134 | case SQLITE_NULL: { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 135 | sqlite3_result_null(context); |
| 136 | break; |
| 137 | } |
| 138 | default: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 139 | double rVal = sqlite3_value_double(argv[0]); |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 140 | if( rVal<0 ) rVal = -rVal; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 141 | sqlite3_result_double(context, rVal); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | ** Implementation of the substr() function |
| 149 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 150 | static void substrFunc( |
| 151 | sqlite3_context *context, |
| 152 | int argc, |
| 153 | sqlite3_value **argv |
| 154 | ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 155 | const unsigned char *z; |
| 156 | const unsigned char *z2; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 157 | int i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 158 | int p1, p2, len; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 159 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 160 | assert( argc==3 ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 161 | z = sqlite3_value_text(argv[0]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 162 | if( z==0 ) return; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 163 | p1 = sqlite3_value_int(argv[1]); |
| 164 | p2 = sqlite3_value_int(argv[2]); |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 165 | for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 166 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 167 | p1 += len; |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 168 | if( p1<0 ){ |
| 169 | p2 += p1; |
| 170 | p1 = 0; |
| 171 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 172 | }else if( p1>0 ){ |
| 173 | p1--; |
| 174 | } |
| 175 | if( p1+p2>len ){ |
| 176 | p2 = len-p1; |
| 177 | } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 178 | for(i=0; i<p1 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 179 | if( (z[i]&0xc0)==0x80 ) p1++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 180 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 181 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } |
drh | 7739630 | 2004-01-02 13:17:48 +0000 | [diff] [blame] | 182 | for(; i<p1+p2 && z[i]; i++){ |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 183 | if( (z[i]&0xc0)==0x80 ) p2++; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 184 | } |
drh | 47c8a67 | 2002-02-28 04:00:12 +0000 | [diff] [blame] | 185 | while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 186 | if( p2<0 ) p2 = 0; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 187 | sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
| 191 | ** Implementation of the round() function |
| 192 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 193 | static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 194 | int n = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 195 | double r; |
drh | 592ac8c | 2005-08-13 03:07:47 +0000 | [diff] [blame] | 196 | char zBuf[500]; /* larger than the %f representation of the largest double */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 197 | assert( argc==1 || argc==2 ); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 198 | if( argc==2 ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 199 | if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 200 | n = sqlite3_value_int(argv[1]); |
| 201 | if( n>30 ) n = 30; |
| 202 | if( n<0 ) n = 0; |
| 203 | } |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 204 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 205 | r = sqlite3_value_double(argv[0]); |
drh | e866fcb | 2005-07-09 02:38:06 +0000 | [diff] [blame] | 206 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); |
drh | 502b962 | 2006-04-07 13:26:42 +0000 | [diff] [blame] | 207 | sqlite3AtoF(zBuf, &r); |
| 208 | sqlite3_result_double(context, r); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 209 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | ** Implementation of the upper() and lower() SQL functions. |
| 213 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 214 | static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 8cd9db0 | 2004-07-18 23:06:53 +0000 | [diff] [blame] | 215 | unsigned char *z; |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 216 | int i; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 217 | if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 218 | z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 219 | if( z==0 ) return; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 220 | strcpy((char*)z, (char*)sqlite3_value_text(argv[0])); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 221 | for(i=0; z[i]; i++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 222 | z[i] = toupper(z[i]); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 223 | } |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 224 | sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 225 | sqliteFree(z); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 226 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 227 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 8cd9db0 | 2004-07-18 23:06:53 +0000 | [diff] [blame] | 228 | unsigned char *z; |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 229 | int i; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 230 | if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 231 | z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 232 | if( z==0 ) return; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 233 | strcpy((char*)z, (char*)sqlite3_value_text(argv[0])); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 234 | for(i=0; z[i]; i++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 235 | z[i] = tolower(z[i]); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 236 | } |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 237 | sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 238 | sqliteFree(z); |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 242 | ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. |
jplyon | b6c9e6e | 2004-01-19 04:53:24 +0000 | [diff] [blame] | 243 | ** All three do the same thing. They return the first non-NULL |
| 244 | ** argument. |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 245 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 246 | static void ifnullFunc( |
| 247 | sqlite3_context *context, |
| 248 | int argc, |
| 249 | sqlite3_value **argv |
| 250 | ){ |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 251 | int i; |
| 252 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 253 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 254 | sqlite3_result_value(context, argv[i]); |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | } |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 261 | ** Implementation of random(). Return a random integer. |
| 262 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 263 | static void randomFunc( |
| 264 | sqlite3_context *context, |
| 265 | int argc, |
| 266 | sqlite3_value **argv |
| 267 | ){ |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 268 | sqlite_int64 r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 269 | sqlite3Randomness(sizeof(r), &r); |
drh | 874abbe | 2006-02-23 21:51:12 +0000 | [diff] [blame] | 270 | if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ |
| 271 | /* can always do abs() of the result */ |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 272 | sqlite3_result_int64(context, r); |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 276 | ** Implementation of the last_insert_rowid() SQL function. The return |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 277 | ** value is the same as the sqlite3_last_insert_rowid() API function. |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 278 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 279 | static void last_insert_rowid( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 280 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 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 | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 285 | sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 286 | } |
| 287 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 288 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 289 | ** Implementation of the changes() SQL function. The return value is the |
| 290 | ** same as the sqlite3_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 changes( |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 293 | sqlite3_context *context, |
| 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); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 298 | sqlite3_result_int(context, sqlite3_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 299 | } |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 300 | |
| 301 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 302 | ** Implementation of the total_changes() SQL function. The return value is |
| 303 | ** the same as the sqlite3_total_changes() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 304 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 305 | static void total_changes( |
| 306 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 307 | int arg, |
| 308 | sqlite3_value **argv |
| 309 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 310 | sqlite3 *db = sqlite3_user_data(context); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 311 | sqlite3_result_int(context, sqlite3_total_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 312 | } |
| 313 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 314 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 315 | ** A structure defining how to do GLOB-style comparisons. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 316 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 317 | struct compareInfo { |
| 318 | u8 matchAll; |
| 319 | u8 matchOne; |
| 320 | u8 matchSet; |
| 321 | u8 noCase; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 322 | }; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 323 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 324 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 325 | /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 326 | ** case. Thus 'a' LIKE 'A' would be true. */ |
| 327 | static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; |
| 328 | /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator |
| 329 | ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 330 | static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 331 | |
| 332 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 333 | ** X is a pointer to the first byte of a UTF-8 character. Increment |
| 334 | ** X so that it points to the next character. This only works right |
| 335 | ** if X points to a well-formed UTF-8 string. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 336 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 337 | #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} |
| 338 | #define sqliteCharVal(X) sqlite3ReadUtf8(X) |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 339 | |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 342 | ** Compare two UTF-8 strings for equality where the first string can |
| 343 | ** potentially be a "glob" expression. Return true (1) if they |
| 344 | ** are the same and false (0) if they are different. |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 345 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 346 | ** Globbing rules: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 347 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 348 | ** '*' Matches any sequence of zero or more characters. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 349 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 350 | ** '?' Matches exactly one character. |
| 351 | ** |
| 352 | ** [...] Matches one character from the enclosed list of |
| 353 | ** characters. |
| 354 | ** |
| 355 | ** [^...] Matches one character not in the enclosed list. |
| 356 | ** |
| 357 | ** With the [...] and [^...] matching, a ']' character can be included |
| 358 | ** in the list by making it the first character after '[' or '^'. A |
| 359 | ** range of characters can be specified using '-'. Example: |
| 360 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 361 | ** it the last character in the list. |
| 362 | ** |
| 363 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 364 | ** |
| 365 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 366 | ** |
| 367 | ** abc[*]xyz Matches "abc*xyz" only |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 368 | */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 369 | static int patternCompare( |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 370 | const u8 *zPattern, /* The glob pattern */ |
| 371 | const u8 *zString, /* The string to compare against the glob */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 372 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 373 | const int esc /* The escape character */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 374 | ){ |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 375 | register int c; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 376 | int invert; |
| 377 | int seen; |
| 378 | int c2; |
| 379 | u8 matchOne = pInfo->matchOne; |
| 380 | u8 matchAll = pInfo->matchAll; |
| 381 | u8 matchSet = pInfo->matchSet; |
| 382 | u8 noCase = pInfo->noCase; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 383 | int prevEscape = 0; /* True if the previous character was 'escape' */ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 384 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 385 | while( (c = *zPattern)!=0 ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 386 | if( !prevEscape && c==matchAll ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 387 | while( (c=zPattern[1]) == matchAll || c == matchOne ){ |
| 388 | if( c==matchOne ){ |
| 389 | if( *zString==0 ) return 0; |
| 390 | sqliteNextChar(zString); |
| 391 | } |
| 392 | zPattern++; |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 393 | } |
drh | 20fc088 | 2004-11-18 13:49:25 +0000 | [diff] [blame] | 394 | if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 395 | u8 const *zTemp = &zPattern[1]; |
| 396 | sqliteNextChar(zTemp); |
| 397 | c = *zTemp; |
| 398 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 399 | if( c==0 ) return 1; |
| 400 | if( c==matchSet ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 401 | assert( esc==0 ); /* This is GLOB, not LIKE */ |
| 402 | while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 403 | sqliteNextChar(zString); |
| 404 | } |
| 405 | return *zString!=0; |
| 406 | }else{ |
| 407 | while( (c2 = *zString)!=0 ){ |
| 408 | if( noCase ){ |
| 409 | c2 = sqlite3UpperToLower[c2]; |
| 410 | c = sqlite3UpperToLower[c]; |
| 411 | while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } |
| 412 | }else{ |
| 413 | while( c2 != 0 && c2 != c ){ c2 = *++zString; } |
| 414 | } |
| 415 | if( c2==0 ) return 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 416 | if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 417 | sqliteNextChar(zString); |
| 418 | } |
| 419 | return 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 420 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 421 | }else if( !prevEscape && c==matchOne ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 422 | if( *zString==0 ) return 0; |
| 423 | sqliteNextChar(zString); |
| 424 | zPattern++; |
| 425 | }else if( c==matchSet ){ |
| 426 | int prior_c = 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 427 | assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 428 | seen = 0; |
| 429 | invert = 0; |
| 430 | c = sqliteCharVal(zString); |
| 431 | if( c==0 ) return 0; |
| 432 | c2 = *++zPattern; |
| 433 | if( c2=='^' ){ invert = 1; c2 = *++zPattern; } |
| 434 | if( c2==']' ){ |
| 435 | if( c==']' ) seen = 1; |
| 436 | c2 = *++zPattern; |
| 437 | } |
| 438 | while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ |
| 439 | if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ |
| 440 | zPattern++; |
| 441 | c2 = sqliteCharVal(zPattern); |
| 442 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 443 | prior_c = 0; |
| 444 | }else if( c==c2 ){ |
| 445 | seen = 1; |
| 446 | prior_c = c2; |
| 447 | }else{ |
| 448 | prior_c = c2; |
| 449 | } |
| 450 | sqliteNextChar(zPattern); |
| 451 | } |
| 452 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 453 | sqliteNextChar(zString); |
| 454 | zPattern++; |
drh | 20fc088 | 2004-11-18 13:49:25 +0000 | [diff] [blame] | 455 | }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 456 | prevEscape = 1; |
| 457 | sqliteNextChar(zPattern); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 458 | }else{ |
| 459 | if( noCase ){ |
| 460 | if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; |
| 461 | }else{ |
| 462 | if( c != *zString ) return 0; |
| 463 | } |
| 464 | zPattern++; |
| 465 | zString++; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 466 | prevEscape = 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 467 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 468 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 469 | return *zString==0; |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 470 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 471 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 472 | /* |
| 473 | ** Count the number of times that the LIKE operator (or GLOB which is |
| 474 | ** just a variation of LIKE) gets called. This is used for testing |
| 475 | ** only. |
| 476 | */ |
| 477 | #ifdef SQLITE_TEST |
| 478 | int sqlite3_like_count = 0; |
| 479 | #endif |
| 480 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | ** Implementation of the like() SQL function. This function implements |
| 484 | ** the build-in LIKE operator. The first argument to the function is the |
| 485 | ** pattern and the second argument is the string. So, the SQL statements: |
| 486 | ** |
| 487 | ** A LIKE B |
| 488 | ** |
| 489 | ** is implemented as like(B,A). |
| 490 | ** |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 491 | ** This same function (with a different compareInfo structure) computes |
| 492 | ** the GLOB operator. |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 493 | */ |
| 494 | static void likeFunc( |
| 495 | sqlite3_context *context, |
| 496 | int argc, |
| 497 | sqlite3_value **argv |
| 498 | ){ |
| 499 | const unsigned char *zA = sqlite3_value_text(argv[0]); |
| 500 | const unsigned char *zB = sqlite3_value_text(argv[1]); |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 501 | int escape = 0; |
| 502 | if( argc==3 ){ |
| 503 | /* The escape character string must consist of a single UTF-8 character. |
| 504 | ** Otherwise, return an error. |
| 505 | */ |
| 506 | const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 507 | if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 508 | sqlite3_result_error(context, |
| 509 | "ESCAPE expression must be a single character", -1); |
| 510 | return; |
| 511 | } |
| 512 | escape = sqlite3ReadUtf8(zEsc); |
| 513 | } |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 514 | if( zA && zB ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 515 | struct compareInfo *pInfo = sqlite3_user_data(context); |
| 516 | #ifdef SQLITE_TEST |
| 517 | sqlite3_like_count++; |
| 518 | #endif |
| 519 | sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 520 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
| 524 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 525 | ** argument if the arguments are different. The result is NULL if the |
| 526 | ** arguments are equal to each other. |
| 527 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 528 | static void nullifFunc( |
| 529 | sqlite3_context *context, |
| 530 | int argc, |
| 531 | sqlite3_value **argv |
| 532 | ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 533 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
| 534 | if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 535 | sqlite3_result_value(context, argv[0]); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 536 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 537 | } |
| 538 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 539 | /* |
| 540 | ** Implementation of the VERSION(*) function. The result is the version |
| 541 | ** of the SQLite library that is running. |
| 542 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 543 | static void versionFunc( |
| 544 | sqlite3_context *context, |
| 545 | int argc, |
| 546 | sqlite3_value **argv |
| 547 | ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 548 | sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 549 | } |
| 550 | |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 551 | /* |
| 552 | ** The MATCH() function is unimplemented. If anybody tries to use it, |
| 553 | ** return an error. |
| 554 | */ |
| 555 | static void matchStub( |
| 556 | sqlite3_context *context, |
| 557 | int argc, |
| 558 | sqlite3_value **argv |
| 559 | ){ |
| 560 | static const char zErr[] = "MATCH is not implemented"; |
| 561 | sqlite3_result_error(context, zErr, sizeof(zErr)-1); |
| 562 | } |
| 563 | |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 564 | |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 565 | /* |
| 566 | ** EXPERIMENTAL - This is not an official function. The interface may |
| 567 | ** change. This function may disappear. Do not write code that depends |
| 568 | ** on this function. |
| 569 | ** |
| 570 | ** Implementation of the QUOTE() function. This function takes a single |
| 571 | ** argument. If the argument is numeric, the return value is the same as |
| 572 | ** the argument. If the argument is NULL, the return value is the string |
| 573 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 574 | ** single-quote escapes. |
| 575 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 576 | static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 577 | if( argc<1 ) return; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 578 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 579 | case SQLITE_NULL: { |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 580 | sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 581 | break; |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 582 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 583 | case SQLITE_INTEGER: |
| 584 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 585 | sqlite3_result_value(context, argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 586 | break; |
| 587 | } |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 588 | case SQLITE_BLOB: { |
| 589 | static const char hexdigits[] = { |
| 590 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 591 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 592 | }; |
| 593 | char *zText = 0; |
| 594 | int nBlob = sqlite3_value_bytes(argv[0]); |
| 595 | char const *zBlob = sqlite3_value_blob(argv[0]); |
| 596 | |
| 597 | zText = (char *)sqliteMalloc((2*nBlob)+4); |
| 598 | if( !zText ){ |
| 599 | sqlite3_result_error(context, "out of memory", -1); |
| 600 | }else{ |
| 601 | int i; |
| 602 | for(i=0; i<nBlob; i++){ |
| 603 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 604 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 605 | } |
| 606 | zText[(nBlob*2)+2] = '\''; |
| 607 | zText[(nBlob*2)+3] = '\0'; |
| 608 | zText[0] = 'X'; |
| 609 | zText[1] = '\''; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 610 | sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 611 | sqliteFree(zText); |
| 612 | } |
| 613 | break; |
| 614 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 615 | case SQLITE_TEXT: { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 616 | int i,j,n; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 617 | const unsigned char *zArg = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 618 | char *z; |
| 619 | |
| 620 | for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } |
| 621 | z = sqliteMalloc( i+n+3 ); |
| 622 | if( z==0 ) return; |
| 623 | z[0] = '\''; |
| 624 | for(i=0, j=1; zArg[i]; i++){ |
| 625 | z[j++] = zArg[i]; |
| 626 | if( zArg[i]=='\'' ){ |
| 627 | z[j++] = '\''; |
| 628 | } |
| 629 | } |
| 630 | z[j++] = '\''; |
| 631 | z[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 632 | sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 633 | sqliteFree(z); |
| 634 | } |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 638 | #ifdef SQLITE_SOUNDEX |
| 639 | /* |
| 640 | ** Compute the soundex encoding of a word. |
| 641 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 642 | static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 643 | char zResult[8]; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 644 | const u8 *zIn; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 645 | int i, j; |
| 646 | static const unsigned char iCode[] = { |
| 647 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 648 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 649 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 650 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 651 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 652 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 653 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 654 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 655 | }; |
| 656 | assert( argc==1 ); |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 657 | zIn = (u8*)sqlite3_value_text(argv[0]); |
drh | 88897a7 | 2006-06-13 19:26:10 +0000 | [diff] [blame] | 658 | if( zIn==0 ) zIn = ""; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 659 | for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} |
| 660 | if( zIn[i] ){ |
| 661 | zResult[0] = toupper(zIn[i]); |
| 662 | for(j=1; j<4 && zIn[i]; i++){ |
| 663 | int code = iCode[zIn[i]&0x7f]; |
| 664 | if( code>0 ){ |
| 665 | zResult[j++] = code + '0'; |
| 666 | } |
| 667 | } |
| 668 | while( j<4 ){ |
| 669 | zResult[j++] = '0'; |
| 670 | } |
| 671 | zResult[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 672 | sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 673 | }else{ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 674 | sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | #endif |
| 678 | |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 679 | #ifdef SQLITE_TEST |
| 680 | /* |
| 681 | ** This function generates a string of random characters. Used for |
| 682 | ** generating test data. |
| 683 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 684 | static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 685 | static const unsigned char zSrc[] = |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 686 | "abcdefghijklmnopqrstuvwxyz" |
| 687 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 688 | "0123456789" |
| 689 | ".-!,:*^+=_|?/<> "; |
| 690 | int iMin, iMax, n, r, i; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 691 | unsigned char zBuf[1000]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 692 | if( argc>=1 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 693 | iMin = sqlite3_value_int(argv[0]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 694 | if( iMin<0 ) iMin = 0; |
| 695 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 696 | }else{ |
| 697 | iMin = 1; |
| 698 | } |
| 699 | if( argc>=2 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 700 | iMax = sqlite3_value_int(argv[1]); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 701 | if( iMax<iMin ) iMax = iMin; |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 702 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 703 | }else{ |
| 704 | iMax = 50; |
| 705 | } |
| 706 | n = iMin; |
| 707 | if( iMax>iMin ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 708 | sqlite3Randomness(sizeof(r), &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 709 | r &= 0x7fffffff; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 710 | n += r%(iMax + 1 - iMin); |
| 711 | } |
drh | 1dba727 | 2004-01-16 13:58:18 +0000 | [diff] [blame] | 712 | assert( n<sizeof(zBuf) ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 713 | sqlite3Randomness(n, zBuf); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 714 | for(i=0; i<n; i++){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 715 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 716 | } |
| 717 | zBuf[n] = 0; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 718 | sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 719 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 720 | #endif /* SQLITE_TEST */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 721 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 722 | #ifdef SQLITE_TEST |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 723 | /* |
| 724 | ** The following two SQL functions are used to test returning a text |
| 725 | ** result with a destructor. Function 'test_destructor' takes one argument |
| 726 | ** and returns the same argument interpreted as TEXT. A destructor is |
| 727 | ** passed with the sqlite3_result_text() call. |
| 728 | ** |
| 729 | ** SQL function 'test_destructor_count' returns the number of outstanding |
| 730 | ** allocations made by 'test_destructor'; |
| 731 | ** |
| 732 | ** WARNING: Not threadsafe. |
| 733 | */ |
| 734 | static int test_destructor_count_var = 0; |
| 735 | static void destructor(void *p){ |
| 736 | char *zVal = (char *)p; |
| 737 | assert(zVal); |
| 738 | zVal--; |
| 739 | sqliteFree(zVal); |
| 740 | test_destructor_count_var--; |
| 741 | } |
| 742 | static void test_destructor( |
| 743 | sqlite3_context *pCtx, |
| 744 | int nArg, |
| 745 | sqlite3_value **argv |
| 746 | ){ |
| 747 | char *zVal; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 748 | int len; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 749 | sqlite3 *db = sqlite3_user_data(pCtx); |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 750 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 751 | test_destructor_count_var++; |
| 752 | assert( nArg==1 ); |
| 753 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 754 | len = sqlite3ValueBytes(argv[0], ENC(db)); |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 755 | zVal = sqliteMalloc(len+3); |
| 756 | zVal[len] = 0; |
| 757 | zVal[len-1] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 758 | assert( zVal ); |
| 759 | zVal++; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 760 | memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len); |
| 761 | if( ENC(db)==SQLITE_UTF8 ){ |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 762 | sqlite3_result_text(pCtx, zVal, -1, destructor); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 763 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 764 | }else if( ENC(db)==SQLITE_UTF16LE ){ |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 765 | sqlite3_result_text16le(pCtx, zVal, -1, destructor); |
| 766 | }else{ |
| 767 | sqlite3_result_text16be(pCtx, zVal, -1, destructor); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 768 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 769 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 770 | } |
| 771 | static void test_destructor_count( |
| 772 | sqlite3_context *pCtx, |
| 773 | int nArg, |
| 774 | sqlite3_value **argv |
| 775 | ){ |
| 776 | sqlite3_result_int(pCtx, test_destructor_count_var); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 777 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 778 | #endif /* SQLITE_TEST */ |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 779 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 780 | #ifdef SQLITE_TEST |
| 781 | /* |
| 782 | ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() |
| 783 | ** interface. |
| 784 | ** |
| 785 | ** The test_auxdata() SQL function attempts to register each of its arguments |
| 786 | ** as auxiliary data. If there are no prior registrations of aux data for |
| 787 | ** that argument (meaning the argument is not a constant or this is its first |
| 788 | ** call) then the result for that argument is 0. If there is a prior |
| 789 | ** registration, the result for that argument is 1. The overall result |
| 790 | ** is the individual argument results separated by spaces. |
| 791 | */ |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 792 | static void free_test_auxdata(void *p) {sqliteFree(p);} |
| 793 | static void test_auxdata( |
| 794 | sqlite3_context *pCtx, |
| 795 | int nArg, |
| 796 | sqlite3_value **argv |
| 797 | ){ |
| 798 | int i; |
| 799 | char *zRet = sqliteMalloc(nArg*2); |
| 800 | if( !zRet ) return; |
| 801 | for(i=0; i<nArg; i++){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 802 | char const *z = (char*)sqlite3_value_text(argv[i]); |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 803 | if( z ){ |
| 804 | char *zAux = sqlite3_get_auxdata(pCtx, i); |
| 805 | if( zAux ){ |
| 806 | zRet[i*2] = '1'; |
| 807 | if( strcmp(zAux, z) ){ |
| 808 | sqlite3_result_error(pCtx, "Auxilary data corruption", -1); |
| 809 | return; |
| 810 | } |
| 811 | }else{ |
| 812 | zRet[i*2] = '0'; |
| 813 | zAux = sqliteStrDup(z); |
| 814 | sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); |
| 815 | } |
| 816 | zRet[i*2+1] = ' '; |
| 817 | } |
| 818 | } |
| 819 | sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); |
| 820 | } |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 821 | #endif /* SQLITE_TEST */ |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 822 | |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 823 | #ifdef SQLITE_TEST |
| 824 | /* |
| 825 | ** A function to test error reporting from user functions. This function |
| 826 | ** returns a copy of it's first argument as an error. |
| 827 | */ |
| 828 | static void test_error( |
| 829 | sqlite3_context *pCtx, |
| 830 | int nArg, |
| 831 | sqlite3_value **argv |
| 832 | ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 833 | sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 834 | } |
| 835 | #endif /* SQLITE_TEST */ |
| 836 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 837 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 838 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 839 | ** sum() or avg() aggregate computation. |
| 840 | */ |
| 841 | typedef struct SumCtx SumCtx; |
| 842 | struct SumCtx { |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 843 | double rSum; /* Floating point sum */ |
| 844 | i64 iSum; /* Integer sum */ |
| 845 | i64 cnt; /* Number of elements summed */ |
| 846 | u8 overflow; /* True if integer overflow seen */ |
| 847 | u8 approx; /* True if non-integer value was input to the sum */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 848 | }; |
| 849 | |
| 850 | /* |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 851 | ** Routines used to compute the sum, average, and total. |
| 852 | ** |
| 853 | ** The SUM() function follows the (broken) SQL standard which means |
| 854 | ** that it returns NULL if it sums over no inputs. TOTAL returns |
| 855 | ** 0.0 in that case. In addition, TOTAL always returns a float where |
| 856 | ** SUM might return an integer if it never encounters a floating point |
drh | c806d85 | 2006-05-11 13:25:39 +0000 | [diff] [blame] | 857 | ** value. TOTAL never fails, but SUM might through an exception if |
| 858 | ** it overflows an integer. |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 859 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 860 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 861 | SumCtx *p; |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 862 | int type; |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 863 | assert( argc==1 ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 864 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 865 | type = sqlite3_value_numeric_type(argv[0]); |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 866 | if( p && type!=SQLITE_NULL ){ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 867 | p->cnt++; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 868 | if( type==SQLITE_INTEGER ){ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 869 | i64 v = sqlite3_value_int64(argv[0]); |
| 870 | p->rSum += v; |
| 871 | if( (p->approx|p->overflow)==0 ){ |
| 872 | i64 iNewSum = p->iSum + v; |
| 873 | int s1 = p->iSum >> (sizeof(i64)*8-1); |
| 874 | int s2 = v >> (sizeof(i64)*8-1); |
| 875 | int s3 = iNewSum >> (sizeof(i64)*8-1); |
| 876 | p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); |
| 877 | p->iSum = iNewSum; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 878 | } |
| 879 | }else{ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 880 | p->rSum += sqlite3_value_double(argv[0]); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 881 | p->approx = 1; |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 882 | } |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 883 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 884 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 885 | static void sumFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 886 | SumCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 887 | p = sqlite3_aggregate_context(context, 0); |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 888 | if( p && p->cnt>0 ){ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 889 | if( p->overflow ){ |
| 890 | sqlite3_result_error(context,"integer overflow",-1); |
| 891 | }else if( p->approx ){ |
| 892 | sqlite3_result_double(context, p->rSum); |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 893 | }else{ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 894 | sqlite3_result_int64(context, p->iSum); |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 895 | } |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 896 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 897 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 898 | static void avgFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 899 | SumCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 900 | p = sqlite3_aggregate_context(context, 0); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 901 | if( p && p->cnt>0 ){ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 902 | sqlite3_result_double(context, p->rSum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 905 | static void totalFinalize(sqlite3_context *context){ |
| 906 | SumCtx *p; |
| 907 | p = sqlite3_aggregate_context(context, 0); |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 908 | sqlite3_result_double(context, p ? p->rSum : 0.0); |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 909 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 910 | |
| 911 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 912 | ** The following structure keeps track of state information for the |
| 913 | ** count() aggregate function. |
| 914 | */ |
| 915 | typedef struct CountCtx CountCtx; |
| 916 | struct CountCtx { |
drh | fc6ad39 | 2006-02-09 13:38:19 +0000 | [diff] [blame] | 917 | i64 n; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 918 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 919 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 920 | /* |
| 921 | ** Routines to implement the count() aggregate function. |
| 922 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 923 | static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 924 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 925 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 926 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 927 | p->n++; |
| 928 | } |
| 929 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 930 | static void countFinalize(sqlite3_context *context){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 931 | CountCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 932 | p = sqlite3_aggregate_context(context, 0); |
drh | fc6ad39 | 2006-02-09 13:38:19 +0000 | [diff] [blame] | 933 | sqlite3_result_int64(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 937 | ** Routines to implement min() and max() aggregate functions. |
| 938 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 939 | static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 940 | Mem *pArg = (Mem *)argv[0]; |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 941 | Mem *pBest; |
| 942 | |
| 943 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 944 | pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 +0000 | [diff] [blame] | 945 | if( !pBest ) return; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 946 | |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 947 | if( pBest->flags ){ |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 948 | int max; |
| 949 | int cmp; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 950 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 951 | /* This step function is used for both the min() and max() aggregates, |
| 952 | ** the only difference between the two being that the sense of the |
| 953 | ** comparison is inverted. For the max() aggregate, the |
| 954 | ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 955 | ** returns (void *)db, where db is the sqlite3* database pointer. |
| 956 | ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 957 | ** aggregate, or 0 for min(). |
| 958 | */ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 959 | max = ((sqlite3_user_data(context)==(void *)-1)?1:0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 960 | cmp = sqlite3MemCompare(pBest, pArg, pColl); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 961 | if( (max && cmp<0) || (!max && cmp>0) ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 962 | sqlite3VdbeMemCopy(pBest, pArg); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 963 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 964 | }else{ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 965 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 968 | static void minMaxFinalize(sqlite3_context *context){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 969 | sqlite3_value *pRes; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 970 | pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); |
| 971 | if( pRes ){ |
| 972 | if( pRes->flags ){ |
| 973 | sqlite3_result_value(context, pRes); |
| 974 | } |
| 975 | sqlite3VdbeMemRelease(pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 978 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 979 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 980 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 981 | ** This function registered all of the above C functions as SQL |
| 982 | ** functions. This should be the only routine in this file with |
| 983 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 984 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 985 | void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 986 | static const struct { |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 987 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 988 | signed char nArg; |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 989 | u8 argType; /* 0: none. 1: db 2: (-1) */ |
| 990 | u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 991 | u8 needCollSeq; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 992 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 993 | } aFuncs[] = { |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 994 | { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, |
| 995 | { "min", 0, 0, SQLITE_UTF8, 1, 0 }, |
| 996 | { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc }, |
| 997 | { "max", 0, 2, SQLITE_UTF8, 1, 0 }, |
| 998 | { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, |
| 999 | { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, |
| 1000 | { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1001 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1002 | { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1003 | #endif |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1004 | { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, |
| 1005 | { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, |
| 1006 | { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, |
| 1007 | { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, |
| 1008 | { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, |
| 1009 | { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, |
| 1010 | { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, |
| 1011 | { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, |
| 1012 | { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, |
| 1013 | { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, |
drh | 94a9836 | 2004-09-13 13:13:18 +0000 | [diff] [blame] | 1014 | { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1015 | { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, |
| 1016 | { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, |
| 1017 | { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid }, |
| 1018 | { "changes", 0, 1, SQLITE_UTF8, 0, changes }, |
| 1019 | { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes }, |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1020 | { "match", 2, 0, SQLITE_UTF8, 0, matchStub }, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1021 | #ifdef SQLITE_SOUNDEX |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1022 | { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1023 | #endif |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1024 | #ifdef SQLITE_TEST |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1025 | { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, |
| 1026 | { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor}, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1027 | { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1028 | { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 1029 | { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1030 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1031 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1032 | static const struct { |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1033 | char *zName; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1034 | signed char nArg; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1035 | u8 argType; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1036 | u8 needCollSeq; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1037 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); |
| 1038 | void (*xFinalize)(sqlite3_context*); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1039 | } aAggs[] = { |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1040 | { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, |
| 1041 | { "max", 1, 2, 1, minmaxStep, minMaxFinalize }, |
| 1042 | { "sum", 1, 0, 0, sumStep, sumFinalize }, |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 1043 | { "total", 1, 0, 0, sumStep, totalFinalize }, |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1044 | { "avg", 1, 0, 0, sumStep, avgFinalize }, |
| 1045 | { "count", 0, 0, 0, countStep, countFinalize }, |
| 1046 | { "count", 1, 0, 0, countStep, countFinalize }, |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1047 | }; |
| 1048 | int i; |
| 1049 | |
| 1050 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1051 | void *pArg = 0; |
| 1052 | switch( aFuncs[i].argType ){ |
| 1053 | case 1: pArg = db; break; |
| 1054 | case 2: pArg = (void *)(-1); break; |
| 1055 | } |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1056 | sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1057 | aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1058 | if( aFuncs[i].needCollSeq ){ |
| 1059 | FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, |
| 1060 | strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); |
| 1061 | if( pFunc && aFuncs[i].needCollSeq ){ |
| 1062 | pFunc->needCollSeq = 1; |
| 1063 | } |
| 1064 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1065 | } |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 1066 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 1067 | sqlite3AlterFunctions(db); |
| 1068 | #endif |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1069 | #ifndef SQLITE_OMIT_PARSER |
danielk1977 | f744bb5 | 2005-12-06 17:19:11 +0000 | [diff] [blame] | 1070 | sqlite3AttachFunctions(db); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1071 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1072 | for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1073 | void *pArg = 0; |
| 1074 | switch( aAggs[i].argType ){ |
| 1075 | case 1: pArg = db; break; |
| 1076 | case 2: pArg = (void *)(-1); break; |
| 1077 | } |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1078 | sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1079 | pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1080 | if( aAggs[i].needCollSeq ){ |
| 1081 | FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1082 | strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1083 | if( pFunc && aAggs[i].needCollSeq ){ |
| 1084 | pFunc->needCollSeq = 1; |
| 1085 | } |
| 1086 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1087 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1088 | sqlite3RegisterDateTimeFunctions(db); |
danielk1977 | fd9e1f3 | 2005-05-22 10:44:34 +0000 | [diff] [blame] | 1089 | #ifdef SQLITE_SSE |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 1090 | (void)sqlite3SseFunctions(db); |
danielk1977 | fd9e1f3 | 2005-05-22 10:44:34 +0000 | [diff] [blame] | 1091 | #endif |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1092 | #ifdef SQLITE_CASE_SENSITIVE_LIKE |
| 1093 | sqlite3RegisterLikeFunctions(db, 1); |
| 1094 | #else |
| 1095 | sqlite3RegisterLikeFunctions(db, 0); |
| 1096 | #endif |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | ** Set the LIKEOPT flag on the 2-argument function with the given name. |
| 1101 | */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1102 | static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1103 | FuncDef *pDef; |
| 1104 | pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); |
| 1105 | if( pDef ){ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1106 | pDef->flags = flagVal; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | ** Register the built-in LIKE and GLOB functions. The caseSensitive |
| 1112 | ** parameter determines whether or not the LIKE operator is case |
| 1113 | ** sensitive. GLOB is always case sensitive. |
| 1114 | */ |
| 1115 | void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ |
| 1116 | struct compareInfo *pInfo; |
| 1117 | if( caseSensitive ){ |
| 1118 | pInfo = (struct compareInfo*)&likeInfoAlt; |
| 1119 | }else{ |
| 1120 | pInfo = (struct compareInfo*)&likeInfoNorm; |
| 1121 | } |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1122 | sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); |
| 1123 | sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); |
| 1124 | sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1125 | (struct compareInfo*)&globInfo, likeFunc, 0,0); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1126 | setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); |
| 1127 | setLikeOptFlag(db, "like", |
| 1128 | caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | ** pExpr points to an expression which implements a function. If |
| 1133 | ** it is appropriate to apply the LIKE optimization to that function |
| 1134 | ** then set aWc[0] through aWc[2] to the wildcard characters and |
| 1135 | ** return TRUE. If the function is not a LIKE-style function then |
| 1136 | ** return FALSE. |
| 1137 | */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1138 | int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1139 | FuncDef *pDef; |
| 1140 | if( pExpr->op!=TK_FUNCTION ){ |
| 1141 | return 0; |
| 1142 | } |
| 1143 | if( pExpr->pList->nExpr!=2 ){ |
| 1144 | return 0; |
| 1145 | } |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1146 | pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1147 | SQLITE_UTF8, 0); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1148 | if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | /* The memcpy() statement assumes that the wildcard characters are |
| 1153 | ** the first three statements in the compareInfo structure. The |
| 1154 | ** asserts() that follow verify that assumption |
| 1155 | */ |
| 1156 | memcpy(aWc, pDef->pUserData, 3); |
| 1157 | assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); |
| 1158 | assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); |
| 1159 | assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1160 | *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1161 | return 1; |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1162 | } |