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 | 07d3117 | 2009-02-02 21:57:05 +0000 | [diff] [blame^] | 19 | ** $Id: func.c,v 1.218 2009/02/02 21:57:05 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 | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 23 | #include <assert.h> |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 24 | #include "vdbeInt.h" |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 25 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 26 | /* |
| 27 | ** Return the collating function associated with a function. |
| 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 | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 46 | assert( argc>1 ); |
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 | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 56 | testcase( mask==0 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 57 | iBest = i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 60 | sqlite3_result_value(context, argv[iBest]); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 61 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 62 | |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 63 | /* |
| 64 | ** Return the type of the argument. |
| 65 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 66 | static void typeofFunc( |
| 67 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 68 | int NotUsed, |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 69 | sqlite3_value **argv |
| 70 | ){ |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 71 | const char *z = 0; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 72 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 73 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 74 | case SQLITE_INTEGER: z = "integer"; break; |
| 75 | case SQLITE_TEXT: z = "text"; break; |
| 76 | case SQLITE_FLOAT: z = "real"; break; |
| 77 | case SQLITE_BLOB: z = "blob"; break; |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 78 | default: z = "null"; break; |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 79 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 80 | sqlite3_result_text(context, z, -1, SQLITE_STATIC); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 81 | } |
| 82 | |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 85 | ** Implementation of the length() function |
| 86 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 87 | static void lengthFunc( |
| 88 | sqlite3_context *context, |
| 89 | int argc, |
| 90 | sqlite3_value **argv |
| 91 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 92 | int len; |
| 93 | |
| 94 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 95 | UNUSED_PARAMETER(argc); |
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 | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 105 | if( z==0 ) return; |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 106 | len = 0; |
| 107 | while( *z ){ |
| 108 | len++; |
| 109 | SQLITE_SKIP_UTF8(z); |
| 110 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 111 | sqlite3_result_int(context, len); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | default: { |
| 115 | sqlite3_result_null(context); |
| 116 | break; |
| 117 | } |
| 118 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
| 122 | ** Implementation of the abs() function |
| 123 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 124 | static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 125 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 126 | UNUSED_PARAMETER(argc); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 127 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 128 | case SQLITE_INTEGER: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 129 | i64 iVal = sqlite3_value_int64(argv[0]); |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 130 | if( iVal<0 ){ |
| 131 | if( (iVal<<1)==0 ){ |
| 132 | sqlite3_result_error(context, "integer overflow", -1); |
| 133 | return; |
| 134 | } |
| 135 | iVal = -iVal; |
| 136 | } |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 137 | sqlite3_result_int64(context, iVal); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 140 | case SQLITE_NULL: { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 141 | sqlite3_result_null(context); |
| 142 | break; |
| 143 | } |
| 144 | default: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 145 | double rVal = sqlite3_value_double(argv[0]); |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 146 | if( rVal<0 ) rVal = -rVal; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 147 | sqlite3_result_double(context, rVal); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 154 | ** Implementation of the substr() function. |
| 155 | ** |
| 156 | ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. |
| 157 | ** p1 is 1-indexed. So substr(x,1,1) returns the first character |
| 158 | ** of x. If x is text, then we actually count UTF-8 characters. |
| 159 | ** If x is a blob, then we count bytes. |
| 160 | ** |
| 161 | ** If p1 is negative, then we begin abs(p1) from the end of x[]. |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 162 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 163 | static void substrFunc( |
| 164 | sqlite3_context *context, |
| 165 | int argc, |
| 166 | sqlite3_value **argv |
| 167 | ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 168 | const unsigned char *z; |
| 169 | const unsigned char *z2; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 170 | int len; |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 171 | int p0type; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 172 | i64 p1, p2; |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 173 | int negP2 = 0; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 174 | |
drh | 64f3151 | 2007-10-12 19:11:55 +0000 | [diff] [blame] | 175 | assert( argc==3 || argc==2 ); |
drh | 8198d25 | 2009-02-01 19:42:37 +0000 | [diff] [blame] | 176 | if( sqlite3_value_type(argv[1])==SQLITE_NULL |
| 177 | || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) |
| 178 | ){ |
| 179 | return; |
| 180 | } |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 181 | p0type = sqlite3_value_type(argv[0]); |
| 182 | if( p0type==SQLITE_BLOB ){ |
| 183 | len = sqlite3_value_bytes(argv[0]); |
| 184 | z = sqlite3_value_blob(argv[0]); |
| 185 | if( z==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 186 | assert( len==sqlite3_value_bytes(argv[0]) ); |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 187 | }else{ |
| 188 | z = sqlite3_value_text(argv[0]); |
| 189 | if( z==0 ) return; |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 190 | len = 0; |
| 191 | for(z2=z; *z2; len++){ |
| 192 | SQLITE_SKIP_UTF8(z2); |
| 193 | } |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 194 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 195 | p1 = sqlite3_value_int(argv[1]); |
drh | 64f3151 | 2007-10-12 19:11:55 +0000 | [diff] [blame] | 196 | if( argc==3 ){ |
| 197 | p2 = sqlite3_value_int(argv[2]); |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 198 | if( p2<0 ){ |
| 199 | p2 = -p2; |
| 200 | negP2 = 1; |
| 201 | } |
drh | 64f3151 | 2007-10-12 19:11:55 +0000 | [diff] [blame] | 202 | }else{ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 203 | p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; |
drh | 64f3151 | 2007-10-12 19:11:55 +0000 | [diff] [blame] | 204 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 205 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 206 | p1 += len; |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 207 | if( p1<0 ){ |
| 208 | p2 += p1; |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 209 | if( p2<0 ) p2 = 0; |
drh | 653bc75 | 2002-02-28 03:31:10 +0000 | [diff] [blame] | 210 | p1 = 0; |
| 211 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 212 | }else if( p1>0 ){ |
| 213 | p1--; |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 214 | }else if( p2>0 ){ |
| 215 | p2--; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 216 | } |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 217 | if( negP2 ){ |
| 218 | p1 -= p2; |
drh | 4e79c59 | 2009-02-01 19:23:32 +0000 | [diff] [blame] | 219 | if( p1<0 ){ |
| 220 | p2 += p1; |
| 221 | p1 = 0; |
| 222 | } |
| 223 | } |
drh | 65595cd | 2009-02-02 16:32:55 +0000 | [diff] [blame] | 224 | assert( p1>=0 && p2>=0 ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 225 | if( p1+p2>len ){ |
| 226 | p2 = len-p1; |
| 227 | } |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 228 | if( p0type!=SQLITE_BLOB ){ |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 229 | while( *z && p1 ){ |
| 230 | SQLITE_SKIP_UTF8(z); |
| 231 | p1--; |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 232 | } |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 233 | for(z2=z; *z2 && p2; p2--){ |
| 234 | SQLITE_SKIP_UTF8(z2); |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 235 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 236 | sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT); |
drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame] | 237 | }else{ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 238 | sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 239 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | ** Implementation of the round() function |
| 244 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 245 | static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 246 | int n = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 247 | double r; |
drh | 592ac8c | 2005-08-13 03:07:47 +0000 | [diff] [blame] | 248 | char zBuf[500]; /* larger than the %f representation of the largest double */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 249 | assert( argc==1 || argc==2 ); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 250 | if( argc==2 ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 251 | if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 252 | n = sqlite3_value_int(argv[1]); |
| 253 | if( n>30 ) n = 30; |
| 254 | if( n<0 ) n = 0; |
| 255 | } |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 256 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 257 | r = sqlite3_value_double(argv[0]); |
drh | e866fcb | 2005-07-09 02:38:06 +0000 | [diff] [blame] | 258 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); |
drh | 502b962 | 2006-04-07 13:26:42 +0000 | [diff] [blame] | 259 | sqlite3AtoF(zBuf, &r); |
| 260 | sqlite3_result_double(context, r); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 261 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 262 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 263 | /* |
| 264 | ** Allocate nByte bytes of space using sqlite3_malloc(). If the |
| 265 | ** allocation fails, call sqlite3_result_error_nomem() to notify |
| 266 | ** the database handle that malloc() has failed. |
| 267 | */ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 268 | static void *contextMalloc(sqlite3_context *context, i64 nByte){ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 269 | char *z; |
| 270 | if( nByte>sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 271 | sqlite3_result_error_toobig(context); |
| 272 | z = 0; |
| 273 | }else{ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 274 | z = sqlite3Malloc((int)nByte); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 275 | if( !z && nByte>0 ){ |
| 276 | sqlite3_result_error_nomem(context); |
| 277 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 278 | } |
| 279 | return z; |
| 280 | } |
| 281 | |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 282 | /* |
| 283 | ** Implementation of the upper() and lower() SQL functions. |
| 284 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 285 | static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 286 | char *z1; |
| 287 | const char *z2; |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 288 | int i, n; |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 289 | z2 = (char*)sqlite3_value_text(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 290 | n = sqlite3_value_bytes(argv[0]); |
| 291 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 292 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 293 | if( z2 ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 294 | z1 = contextMalloc(context, ((i64)n)+1); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 295 | if( z1 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 296 | memcpy(z1, z2, n+1); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 297 | for(i=0; z1[i]; i++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 298 | z1[i] = (char)sqlite3Toupper(z1[i]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 299 | } |
| 300 | sqlite3_result_text(context, z1, -1, sqlite3_free); |
| 301 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 304 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 305 | u8 *z1; |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 306 | const char *z2; |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 307 | int i, n; |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 308 | z2 = (char*)sqlite3_value_text(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 309 | n = sqlite3_value_bytes(argv[0]); |
| 310 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 311 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 312 | if( z2 ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 313 | z1 = contextMalloc(context, ((i64)n)+1); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 314 | if( z1 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 315 | memcpy(z1, z2, n+1); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 316 | for(i=0; z1[i]; i++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 317 | z1[i] = sqlite3Tolower(z1[i]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 318 | } |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 319 | sqlite3_result_text(context, (char *)z1, -1, sqlite3_free); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 320 | } |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | /* |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 325 | ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. |
jplyon | b6c9e6e | 2004-01-19 04:53:24 +0000 | [diff] [blame] | 326 | ** All three do the same thing. They return the first non-NULL |
| 327 | ** argument. |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 328 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 329 | static void ifnullFunc( |
| 330 | sqlite3_context *context, |
| 331 | int argc, |
| 332 | sqlite3_value **argv |
| 333 | ){ |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 334 | int i; |
| 335 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 336 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 337 | sqlite3_result_value(context, argv[i]); |
drh | fbc9908 | 2002-02-28 03:14:18 +0000 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | } |
drh | 3212e18 | 2002-02-28 00:46:26 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 344 | ** Implementation of random(). Return a random integer. |
| 345 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 346 | static void randomFunc( |
| 347 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 348 | int NotUsed, |
| 349 | sqlite3_value **NotUsed2 |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 350 | ){ |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 351 | sqlite_int64 r; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 352 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 353 | sqlite3_randomness(sizeof(r), &r); |
drh | 874abbe | 2006-02-23 21:51:12 +0000 | [diff] [blame] | 354 | if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ |
| 355 | /* can always do abs() of the result */ |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 356 | sqlite3_result_int64(context, r); |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 360 | ** Implementation of randomblob(N). Return a random blob |
| 361 | ** that is N bytes long. |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 362 | */ |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 363 | static void randomBlob( |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 364 | sqlite3_context *context, |
| 365 | int argc, |
| 366 | sqlite3_value **argv |
| 367 | ){ |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 368 | int n; |
| 369 | unsigned char *p; |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 370 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 371 | UNUSED_PARAMETER(argc); |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 372 | n = sqlite3_value_int(argv[0]); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 373 | if( n<1 ){ |
| 374 | n = 1; |
| 375 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 376 | p = contextMalloc(context, n); |
drh | 02d8583 | 2007-05-07 19:31:15 +0000 | [diff] [blame] | 377 | if( p ){ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 378 | sqlite3_randomness(n, p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 379 | sqlite3_result_blob(context, (char*)p, n, sqlite3_free); |
drh | 02d8583 | 2007-05-07 19:31:15 +0000 | [diff] [blame] | 380 | } |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 384 | ** Implementation of the last_insert_rowid() SQL function. The return |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 385 | ** value is the same as the sqlite3_last_insert_rowid() API function. |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 386 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 387 | static void last_insert_rowid( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 388 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 389 | int NotUsed, |
| 390 | sqlite3_value **NotUsed2 |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 391 | ){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 392 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 393 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 394 | sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 395 | } |
| 396 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 397 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 398 | ** Implementation of the changes() SQL function. The return value is the |
| 399 | ** same as the sqlite3_changes() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 400 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 401 | static void changes( |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 402 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 403 | int NotUsed, |
| 404 | sqlite3_value **NotUsed2 |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 405 | ){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 406 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 407 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 408 | sqlite3_result_int(context, sqlite3_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 409 | } |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 410 | |
| 411 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 412 | ** Implementation of the total_changes() SQL function. The return value is |
| 413 | ** the same as the sqlite3_total_changes() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 414 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 415 | static void total_changes( |
| 416 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 417 | int NotUsed, |
| 418 | sqlite3_value **NotUsed2 |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 419 | ){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 420 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 421 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 422 | sqlite3_result_int(context, sqlite3_total_changes(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 423 | } |
| 424 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 425 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 426 | ** A structure defining how to do GLOB-style comparisons. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 427 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 428 | struct compareInfo { |
| 429 | u8 matchAll; |
| 430 | u8 matchOne; |
| 431 | u8 matchSet; |
| 432 | u8 noCase; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 433 | }; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 434 | |
drh | b9175ae | 2007-12-07 18:39:04 +0000 | [diff] [blame] | 435 | /* |
| 436 | ** For LIKE and GLOB matching on EBCDIC machines, assume that every |
| 437 | ** character is exactly one byte in size. Also, all characters are |
| 438 | ** able to participate in upper-case-to-lower-case mappings in EBCDIC |
| 439 | ** whereas only characters less than 0x80 do in ASCII. |
| 440 | */ |
| 441 | #if defined(SQLITE_EBCDIC) |
| 442 | # define sqlite3Utf8Read(A,B,C) (*(A++)) |
drh | 6ed4b78 | 2007-12-10 18:07:20 +0000 | [diff] [blame] | 443 | # define GlogUpperToLower(A) A = sqlite3UpperToLower[A] |
drh | b9175ae | 2007-12-07 18:39:04 +0000 | [diff] [blame] | 444 | #else |
drh | 6ed4b78 | 2007-12-10 18:07:20 +0000 | [diff] [blame] | 445 | # define GlogUpperToLower(A) if( A<0x80 ){ A = sqlite3UpperToLower[A]; } |
drh | b9175ae | 2007-12-07 18:39:04 +0000 | [diff] [blame] | 446 | #endif |
| 447 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 448 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 449 | /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 450 | ** case. Thus 'a' LIKE 'A' would be true. */ |
| 451 | static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; |
| 452 | /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator |
| 453 | ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 454 | static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 457 | ** Compare two UTF-8 strings for equality where the first string can |
| 458 | ** potentially be a "glob" expression. Return true (1) if they |
| 459 | ** are the same and false (0) if they are different. |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 460 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 461 | ** Globbing rules: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 462 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 463 | ** '*' Matches any sequence of zero or more characters. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 464 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 465 | ** '?' Matches exactly one character. |
| 466 | ** |
| 467 | ** [...] Matches one character from the enclosed list of |
| 468 | ** characters. |
| 469 | ** |
| 470 | ** [^...] Matches one character not in the enclosed list. |
| 471 | ** |
| 472 | ** With the [...] and [^...] matching, a ']' character can be included |
| 473 | ** in the list by making it the first character after '[' or '^'. A |
| 474 | ** range of characters can be specified using '-'. Example: |
| 475 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 476 | ** it the last character in the list. |
| 477 | ** |
| 478 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 479 | ** |
| 480 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 481 | ** |
| 482 | ** abc[*]xyz Matches "abc*xyz" only |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 483 | */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 484 | static int patternCompare( |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 485 | const u8 *zPattern, /* The glob pattern */ |
| 486 | const u8 *zString, /* The string to compare against the glob */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 487 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 488 | const int esc /* The escape character */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 489 | ){ |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 490 | int c, c2; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 491 | int invert; |
| 492 | int seen; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 493 | u8 matchOne = pInfo->matchOne; |
| 494 | u8 matchAll = pInfo->matchAll; |
| 495 | u8 matchSet = pInfo->matchSet; |
| 496 | u8 noCase = pInfo->noCase; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 497 | int prevEscape = 0; /* True if the previous character was 'escape' */ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 498 | |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 499 | while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 500 | if( !prevEscape && c==matchAll ){ |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 501 | while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll |
| 502 | || c == matchOne ){ |
| 503 | if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){ |
| 504 | return 0; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 505 | } |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 506 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 507 | if( c==0 ){ |
| 508 | return 1; |
| 509 | }else if( c==esc ){ |
| 510 | c = sqlite3Utf8Read(zPattern, 0, &zPattern); |
| 511 | if( c==0 ){ |
| 512 | return 0; |
| 513 | } |
| 514 | }else if( c==matchSet ){ |
| 515 | assert( esc==0 ); /* This is GLOB, not LIKE */ |
| 516 | assert( matchSet<0x80 ); /* '[' is a single-byte character */ |
| 517 | while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 518 | SQLITE_SKIP_UTF8(zString); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 519 | } |
| 520 | return *zString!=0; |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 521 | } |
| 522 | while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){ |
| 523 | if( noCase ){ |
drh | 6ed4b78 | 2007-12-10 18:07:20 +0000 | [diff] [blame] | 524 | GlogUpperToLower(c2); |
| 525 | GlogUpperToLower(c); |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 526 | while( c2 != 0 && c2 != c ){ |
| 527 | c2 = sqlite3Utf8Read(zString, 0, &zString); |
drh | 6ed4b78 | 2007-12-10 18:07:20 +0000 | [diff] [blame] | 528 | GlogUpperToLower(c2); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 529 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 530 | }else{ |
| 531 | while( c2 != 0 && c2 != c ){ |
| 532 | c2 = sqlite3Utf8Read(zString, 0, &zString); |
| 533 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 534 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 535 | if( c2==0 ) return 0; |
| 536 | if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 537 | } |
| 538 | return 0; |
| 539 | }else if( !prevEscape && c==matchOne ){ |
| 540 | if( sqlite3Utf8Read(zString, 0, &zString)==0 ){ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 541 | return 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 542 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 543 | }else if( c==matchSet ){ |
| 544 | int prior_c = 0; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 545 | assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 546 | seen = 0; |
| 547 | invert = 0; |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 548 | c = sqlite3Utf8Read(zString, 0, &zString); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 549 | if( c==0 ) return 0; |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 550 | c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); |
| 551 | if( c2=='^' ){ |
| 552 | invert = 1; |
| 553 | c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); |
| 554 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 555 | if( c2==']' ){ |
| 556 | if( c==']' ) seen = 1; |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 557 | c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 558 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 559 | while( c2 && c2!=']' ){ |
| 560 | if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 561 | c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 562 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 563 | prior_c = 0; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 564 | }else{ |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 565 | if( c==c2 ){ |
| 566 | seen = 1; |
| 567 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 568 | prior_c = c2; |
| 569 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 570 | c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 571 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 572 | if( c2==0 || (seen ^ invert)==0 ){ |
| 573 | return 0; |
| 574 | } |
| 575 | }else if( esc==c && !prevEscape ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 576 | prevEscape = 1; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 577 | }else{ |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 578 | c2 = sqlite3Utf8Read(zString, 0, &zString); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 579 | if( noCase ){ |
drh | 6ed4b78 | 2007-12-10 18:07:20 +0000 | [diff] [blame] | 580 | GlogUpperToLower(c); |
| 581 | GlogUpperToLower(c2); |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 582 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 583 | if( c!=c2 ){ |
| 584 | return 0; |
| 585 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 586 | prevEscape = 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 587 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 588 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 589 | return *zString==0; |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 590 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 591 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 592 | /* |
| 593 | ** Count the number of times that the LIKE operator (or GLOB which is |
| 594 | ** just a variation of LIKE) gets called. This is used for testing |
| 595 | ** only. |
| 596 | */ |
| 597 | #ifdef SQLITE_TEST |
| 598 | int sqlite3_like_count = 0; |
| 599 | #endif |
| 600 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | ** Implementation of the like() SQL function. This function implements |
| 604 | ** the build-in LIKE operator. The first argument to the function is the |
| 605 | ** pattern and the second argument is the string. So, the SQL statements: |
| 606 | ** |
| 607 | ** A LIKE B |
| 608 | ** |
| 609 | ** is implemented as like(B,A). |
| 610 | ** |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 611 | ** This same function (with a different compareInfo structure) computes |
| 612 | ** the GLOB operator. |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 613 | */ |
| 614 | static void likeFunc( |
| 615 | sqlite3_context *context, |
| 616 | int argc, |
| 617 | sqlite3_value **argv |
| 618 | ){ |
drh | beb818d | 2007-05-08 15:34:47 +0000 | [diff] [blame] | 619 | const unsigned char *zA, *zB; |
drh | 85892bd | 2007-05-10 13:23:22 +0000 | [diff] [blame] | 620 | int escape = 0; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 621 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | beb818d | 2007-05-08 15:34:47 +0000 | [diff] [blame] | 622 | |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 623 | zB = sqlite3_value_text(argv[0]); |
| 624 | zA = sqlite3_value_text(argv[1]); |
| 625 | |
drh | beb818d | 2007-05-08 15:34:47 +0000 | [diff] [blame] | 626 | /* Limit the length of the LIKE or GLOB pattern to avoid problems |
| 627 | ** of deep recursion and N*N behavior in patternCompare(). |
| 628 | */ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 629 | if( sqlite3_value_bytes(argv[0]) > |
| 630 | db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ |
drh | beb818d | 2007-05-08 15:34:47 +0000 | [diff] [blame] | 631 | sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); |
| 632 | return; |
| 633 | } |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 634 | assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ |
drh | beb818d | 2007-05-08 15:34:47 +0000 | [diff] [blame] | 635 | |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 636 | if( argc==3 ){ |
| 637 | /* The escape character string must consist of a single UTF-8 character. |
| 638 | ** Otherwise, return an error. |
| 639 | */ |
| 640 | const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 641 | if( zEsc==0 ) return; |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 642 | if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 643 | sqlite3_result_error(context, |
| 644 | "ESCAPE expression must be a single character", -1); |
| 645 | return; |
| 646 | } |
drh | 6615095 | 2007-07-23 19:12:41 +0000 | [diff] [blame] | 647 | escape = sqlite3Utf8Read(zEsc, 0, &zEsc); |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 648 | } |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 649 | if( zA && zB ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 650 | struct compareInfo *pInfo = sqlite3_user_data(context); |
| 651 | #ifdef SQLITE_TEST |
| 652 | sqlite3_like_count++; |
| 653 | #endif |
drh | beb818d | 2007-05-08 15:34:47 +0000 | [diff] [blame] | 654 | |
danielk1977 | b56fe1f | 2007-05-09 08:24:44 +0000 | [diff] [blame] | 655 | sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 656 | } |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /* |
| 660 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 661 | ** argument if the arguments are different. The result is NULL if the |
| 662 | ** arguments are equal to each other. |
| 663 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 664 | static void nullifFunc( |
| 665 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 666 | int NotUsed, |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 667 | sqlite3_value **argv |
| 668 | ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 669 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 670 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 671 | if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 672 | sqlite3_result_value(context, argv[0]); |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 673 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 674 | } |
| 675 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 676 | /* |
| 677 | ** Implementation of the VERSION(*) function. The result is the version |
| 678 | ** of the SQLite library that is running. |
| 679 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 680 | static void versionFunc( |
| 681 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 682 | int NotUsed, |
| 683 | sqlite3_value **NotUsed2 |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 684 | ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 685 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 686 | sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 687 | } |
| 688 | |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 689 | /* Array for converting from half-bytes (nybbles) into ASCII hex |
| 690 | ** digits. */ |
| 691 | static const char hexdigits[] = { |
| 692 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 693 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 694 | }; |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 695 | |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 696 | /* |
| 697 | ** EXPERIMENTAL - This is not an official function. The interface may |
| 698 | ** change. This function may disappear. Do not write code that depends |
| 699 | ** on this function. |
| 700 | ** |
| 701 | ** Implementation of the QUOTE() function. This function takes a single |
| 702 | ** argument. If the argument is numeric, the return value is the same as |
| 703 | ** the argument. If the argument is NULL, the return value is the string |
| 704 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 705 | ** single-quote escapes. |
| 706 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 707 | static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | a0df4cc | 2009-02-02 17:29:59 +0000 | [diff] [blame] | 708 | assert( argc==1 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 709 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 710 | case SQLITE_INTEGER: |
| 711 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 712 | sqlite3_result_value(context, argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 713 | break; |
| 714 | } |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 715 | case SQLITE_BLOB: { |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 716 | char *zText = 0; |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 717 | char const *zBlob = sqlite3_value_blob(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 718 | int nBlob = sqlite3_value_bytes(argv[0]); |
| 719 | assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 720 | zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 721 | if( zText ){ |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 722 | int i; |
| 723 | for(i=0; i<nBlob; i++){ |
| 724 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 725 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 726 | } |
| 727 | zText[(nBlob*2)+2] = '\''; |
| 728 | zText[(nBlob*2)+3] = '\0'; |
| 729 | zText[0] = 'X'; |
| 730 | zText[1] = '\''; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 731 | sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 732 | sqlite3_free(zText); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 733 | } |
| 734 | break; |
| 735 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 736 | case SQLITE_TEXT: { |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 737 | int i,j; |
| 738 | u64 n; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 739 | const unsigned char *zArg = sqlite3_value_text(argv[0]); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 740 | char *z; |
| 741 | |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 742 | if( zArg==0 ) return; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 743 | for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 744 | z = contextMalloc(context, ((i64)i)+((i64)n)+3); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 745 | if( z ){ |
| 746 | z[0] = '\''; |
| 747 | for(i=0, j=1; zArg[i]; i++){ |
| 748 | z[j++] = zArg[i]; |
| 749 | if( zArg[i]=='\'' ){ |
| 750 | z[j++] = '\''; |
| 751 | } |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 752 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 753 | z[j++] = '\''; |
| 754 | z[j] = 0; |
| 755 | sqlite3_result_text(context, z, j, sqlite3_free); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 756 | } |
drh | a0df4cc | 2009-02-02 17:29:59 +0000 | [diff] [blame] | 757 | break; |
| 758 | } |
| 759 | default: { |
| 760 | assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); |
| 761 | sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
| 762 | break; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 763 | } |
drh | 4739470 | 2003-08-20 01:03:33 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 767 | /* |
| 768 | ** The hex() function. Interpret the argument as a blob. Return |
| 769 | ** a hexadecimal rendering as text. |
| 770 | */ |
| 771 | static void hexFunc( |
| 772 | sqlite3_context *context, |
| 773 | int argc, |
| 774 | sqlite3_value **argv |
| 775 | ){ |
| 776 | int i, n; |
| 777 | const unsigned char *pBlob; |
| 778 | char *zHex, *z; |
| 779 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 780 | UNUSED_PARAMETER(argc); |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 781 | pBlob = sqlite3_value_blob(argv[0]); |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 782 | n = sqlite3_value_bytes(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 783 | assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 784 | z = zHex = contextMalloc(context, ((i64)n)*2 + 1); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 785 | if( zHex ){ |
| 786 | for(i=0; i<n; i++, pBlob++){ |
| 787 | unsigned char c = *pBlob; |
| 788 | *(z++) = hexdigits[(c>>4)&0xf]; |
| 789 | *(z++) = hexdigits[c&0xf]; |
| 790 | } |
| 791 | *z = 0; |
| 792 | sqlite3_result_text(context, zHex, n*2, sqlite3_free); |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 793 | } |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 794 | } |
| 795 | |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 796 | /* |
drh | 8cff382 | 2007-05-02 02:08:28 +0000 | [diff] [blame] | 797 | ** The zeroblob(N) function returns a zero-filled blob of size N bytes. |
| 798 | */ |
| 799 | static void zeroblobFunc( |
| 800 | sqlite3_context *context, |
| 801 | int argc, |
| 802 | sqlite3_value **argv |
| 803 | ){ |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 804 | i64 n; |
drh | 8cff382 | 2007-05-02 02:08:28 +0000 | [diff] [blame] | 805 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 806 | UNUSED_PARAMETER(argc); |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 807 | n = sqlite3_value_int64(argv[0]); |
| 808 | if( n>SQLITE_MAX_LENGTH ){ |
| 809 | sqlite3_result_error_toobig(context); |
| 810 | }else{ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 811 | sqlite3_result_zeroblob(context, (int)n); |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 812 | } |
drh | 8cff382 | 2007-05-02 02:08:28 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 816 | ** The replace() function. Three arguments are all strings: call |
| 817 | ** them A, B, and C. The result is also a string which is derived |
| 818 | ** from A by replacing every occurance of B with C. The match |
| 819 | ** must be exact. Collating sequences are not used. |
| 820 | */ |
| 821 | static void replaceFunc( |
| 822 | sqlite3_context *context, |
| 823 | int argc, |
| 824 | sqlite3_value **argv |
| 825 | ){ |
| 826 | const unsigned char *zStr; /* The input string A */ |
| 827 | const unsigned char *zPattern; /* The pattern string B */ |
| 828 | const unsigned char *zRep; /* The replacement string C */ |
| 829 | unsigned char *zOut; /* The output */ |
| 830 | int nStr; /* Size of zStr */ |
| 831 | int nPattern; /* Size of zPattern */ |
| 832 | int nRep; /* Size of zRep */ |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 833 | i64 nOut; /* Maximum size of zOut */ |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 834 | int loopLimit; /* Last zStr[] that might match zPattern[] */ |
| 835 | int i, j; /* Loop counters */ |
| 836 | |
| 837 | assert( argc==3 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 838 | UNUSED_PARAMETER(argc); |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 839 | zStr = sqlite3_value_text(argv[0]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 840 | if( zStr==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 841 | nStr = sqlite3_value_bytes(argv[0]); |
| 842 | assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 843 | zPattern = sqlite3_value_text(argv[1]); |
drh | a605fe8 | 2009-02-01 18:08:40 +0000 | [diff] [blame] | 844 | if( zPattern==0 ){ |
| 845 | assert( sqlite3_value_type(argv[1])==SQLITE_NULL ); |
| 846 | return; |
| 847 | } |
| 848 | if( zPattern[0]==0 ){ |
| 849 | assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); |
| 850 | sqlite3_result_value(context, argv[0]); |
| 851 | return; |
| 852 | } |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 853 | nPattern = sqlite3_value_bytes(argv[1]); |
| 854 | assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 855 | zRep = sqlite3_value_text(argv[2]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 856 | if( zRep==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 857 | nRep = sqlite3_value_bytes(argv[2]); |
| 858 | assert( zRep==sqlite3_value_text(argv[2]) ); |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 859 | nOut = nStr + 1; |
| 860 | assert( nOut<SQLITE_MAX_LENGTH ); |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 861 | zOut = contextMalloc(context, (i64)nOut); |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 862 | if( zOut==0 ){ |
| 863 | return; |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 864 | } |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 865 | loopLimit = nStr - nPattern; |
| 866 | for(i=j=0; i<=loopLimit; i++){ |
| 867 | if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ |
| 868 | zOut[j++] = zStr[i]; |
| 869 | }else{ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 870 | u8 *zOld; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 871 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 872 | nOut += nRep - nPattern; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 873 | if( nOut>=db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | a0206bc | 2007-05-08 15:15:02 +0000 | [diff] [blame] | 874 | sqlite3_result_error_toobig(context); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 875 | sqlite3DbFree(db, zOut); |
danielk1977 | 17374e8 | 2007-05-08 14:39:04 +0000 | [diff] [blame] | 876 | return; |
| 877 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 878 | zOld = zOut; |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 879 | zOut = sqlite3_realloc(zOut, (int)nOut); |
| 880 | if( zOut==0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 881 | sqlite3_result_error_nomem(context); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 882 | sqlite3DbFree(db, zOld); |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 883 | return; |
| 884 | } |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 885 | memcpy(&zOut[j], zRep, nRep); |
| 886 | j += nRep; |
| 887 | i += nPattern-1; |
| 888 | } |
| 889 | } |
drh | 2e6400b | 2007-05-08 15:46:18 +0000 | [diff] [blame] | 890 | assert( j+nStr-i+1==nOut ); |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 891 | memcpy(&zOut[j], &zStr[i], nStr-i); |
| 892 | j += nStr - i; |
| 893 | assert( j<=nOut ); |
| 894 | zOut[j] = 0; |
| 895 | sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); |
| 896 | } |
| 897 | |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 898 | /* |
| 899 | ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. |
| 900 | ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. |
| 901 | */ |
| 902 | static void trimFunc( |
| 903 | sqlite3_context *context, |
| 904 | int argc, |
| 905 | sqlite3_value **argv |
| 906 | ){ |
| 907 | const unsigned char *zIn; /* Input string */ |
| 908 | const unsigned char *zCharSet; /* Set of characters to trim */ |
| 909 | int nIn; /* Number of bytes in input */ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 910 | int flags; /* 1: trimleft 2: trimright 3: trim */ |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 911 | int i; /* Loop counter */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 912 | unsigned char *aLen = 0; /* Length of each character in zCharSet */ |
| 913 | unsigned char **azChar = 0; /* Individual characters in zCharSet */ |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 914 | int nChar; /* Number of characters in zCharSet */ |
| 915 | |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 916 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 917 | return; |
| 918 | } |
| 919 | zIn = sqlite3_value_text(argv[0]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 920 | if( zIn==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 921 | nIn = sqlite3_value_bytes(argv[0]); |
| 922 | assert( zIn==sqlite3_value_text(argv[0]) ); |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 923 | if( argc==1 ){ |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 924 | static const unsigned char lenOne[] = { 1 }; |
danielk1977 | a4de453 | 2008-09-02 15:44:08 +0000 | [diff] [blame] | 925 | static unsigned char * const azOne[] = { (u8*)" " }; |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 926 | nChar = 1; |
drh | 8cff382 | 2007-05-02 02:08:28 +0000 | [diff] [blame] | 927 | aLen = (u8*)lenOne; |
danielk1977 | bc67da4 | 2007-12-11 04:23:19 +0000 | [diff] [blame] | 928 | azChar = (unsigned char **)azOne; |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 929 | zCharSet = 0; |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 930 | }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 931 | return; |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 932 | }else{ |
| 933 | const unsigned char *z; |
| 934 | for(z=zCharSet, nChar=0; *z; nChar++){ |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 935 | SQLITE_SKIP_UTF8(z); |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 936 | } |
| 937 | if( nChar>0 ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 938 | azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 939 | if( azChar==0 ){ |
| 940 | return; |
| 941 | } |
| 942 | aLen = (unsigned char*)&azChar[nChar]; |
| 943 | for(z=zCharSet, nChar=0; *z; nChar++){ |
danielk1977 | bc67da4 | 2007-12-11 04:23:19 +0000 | [diff] [blame] | 944 | azChar[nChar] = (unsigned char *)z; |
drh | 4a91911 | 2007-05-15 11:55:09 +0000 | [diff] [blame] | 945 | SQLITE_SKIP_UTF8(z); |
drh | 3abbd39 | 2008-12-10 23:04:13 +0000 | [diff] [blame] | 946 | aLen[nChar] = (u8)(z - azChar[nChar]); |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 949 | } |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 950 | if( nChar>0 ){ |
shane | 1fc4129 | 2008-07-08 22:28:48 +0000 | [diff] [blame] | 951 | flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 952 | if( flags & 1 ){ |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 953 | while( nIn>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 954 | int len = 0; |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 955 | for(i=0; i<nChar; i++){ |
| 956 | len = aLen[i]; |
| 957 | if( memcmp(zIn, azChar[i], len)==0 ) break; |
| 958 | } |
| 959 | if( i>=nChar ) break; |
| 960 | zIn += len; |
| 961 | nIn -= len; |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | if( flags & 2 ){ |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 965 | while( nIn>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 966 | int len = 0; |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 967 | for(i=0; i<nChar; i++){ |
| 968 | len = aLen[i]; |
| 969 | if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; |
| 970 | } |
| 971 | if( i>=nChar ) break; |
| 972 | nIn -= len; |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
drh | d1e3a61 | 2007-04-27 21:59:52 +0000 | [diff] [blame] | 975 | if( zCharSet ){ |
| 976 | sqlite3_free(azChar); |
| 977 | } |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 978 | } |
| 979 | sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); |
| 980 | } |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 981 | |
danielk1977 | a4de453 | 2008-09-02 15:44:08 +0000 | [diff] [blame] | 982 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 983 | #ifdef SQLITE_SOUNDEX |
| 984 | /* |
| 985 | ** Compute the soundex encoding of a word. |
| 986 | */ |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 987 | static void soundexFunc( |
| 988 | sqlite3_context *context, |
| 989 | int argc, |
| 990 | sqlite3_value **argv |
| 991 | ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 992 | char zResult[8]; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 993 | const u8 *zIn; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 994 | int i, j; |
| 995 | static const unsigned char iCode[] = { |
| 996 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 997 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 998 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 999 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1000 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 1001 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 1002 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 1003 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 1004 | }; |
| 1005 | assert( argc==1 ); |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1006 | zIn = (u8*)sqlite3_value_text(argv[0]); |
drh | bdf67e0 | 2006-08-19 11:34:01 +0000 | [diff] [blame] | 1007 | if( zIn==0 ) zIn = (u8*)""; |
drh | dc86e2b | 2009-01-24 11:30:42 +0000 | [diff] [blame] | 1008 | for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1009 | if( zIn[i] ){ |
drh | bdf67e0 | 2006-08-19 11:34:01 +0000 | [diff] [blame] | 1010 | u8 prevcode = iCode[zIn[i]&0x7f]; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 1011 | zResult[0] = sqlite3Toupper(zIn[i]); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1012 | for(j=1; j<4 && zIn[i]; i++){ |
| 1013 | int code = iCode[zIn[i]&0x7f]; |
| 1014 | if( code>0 ){ |
drh | bdf67e0 | 2006-08-19 11:34:01 +0000 | [diff] [blame] | 1015 | if( code!=prevcode ){ |
| 1016 | prevcode = code; |
| 1017 | zResult[j++] = code + '0'; |
| 1018 | } |
| 1019 | }else{ |
| 1020 | prevcode = 0; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | while( j<4 ){ |
| 1024 | zResult[j++] = '0'; |
| 1025 | } |
| 1026 | zResult[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1027 | sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1028 | }else{ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1029 | sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | #endif |
| 1033 | |
drh | fdb83b2 | 2006-06-17 14:12:47 +0000 | [diff] [blame] | 1034 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 1035 | /* |
| 1036 | ** A function that loads a shared-library extension then returns NULL. |
| 1037 | */ |
| 1038 | static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 1039 | const char *zFile = (const char *)sqlite3_value_text(argv[0]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 1040 | const char *zProc; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 1041 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | fdb83b2 | 2006-06-17 14:12:47 +0000 | [diff] [blame] | 1042 | char *zErrMsg = 0; |
| 1043 | |
| 1044 | if( argc==2 ){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 1045 | zProc = (const char *)sqlite3_value_text(argv[1]); |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 1046 | }else{ |
| 1047 | zProc = 0; |
drh | fdb83b2 | 2006-06-17 14:12:47 +0000 | [diff] [blame] | 1048 | } |
drh | 7a521cf | 2007-04-25 18:23:52 +0000 | [diff] [blame] | 1049 | if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ |
drh | fdb83b2 | 2006-06-17 14:12:47 +0000 | [diff] [blame] | 1050 | sqlite3_result_error(context, zErrMsg, -1); |
| 1051 | sqlite3_free(zErrMsg); |
| 1052 | } |
| 1053 | } |
| 1054 | #endif |
| 1055 | |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 1056 | |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 1057 | /* |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 1058 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1059 | ** sum() or avg() aggregate computation. |
| 1060 | */ |
| 1061 | typedef struct SumCtx SumCtx; |
| 1062 | struct SumCtx { |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1063 | double rSum; /* Floating point sum */ |
| 1064 | i64 iSum; /* Integer sum */ |
| 1065 | i64 cnt; /* Number of elements summed */ |
| 1066 | u8 overflow; /* True if integer overflow seen */ |
| 1067 | u8 approx; /* True if non-integer value was input to the sum */ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | /* |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 1071 | ** Routines used to compute the sum, average, and total. |
| 1072 | ** |
| 1073 | ** The SUM() function follows the (broken) SQL standard which means |
| 1074 | ** that it returns NULL if it sums over no inputs. TOTAL returns |
| 1075 | ** 0.0 in that case. In addition, TOTAL always returns a float where |
| 1076 | ** SUM might return an integer if it never encounters a floating point |
drh | c806d85 | 2006-05-11 13:25:39 +0000 | [diff] [blame] | 1077 | ** value. TOTAL never fails, but SUM might through an exception if |
| 1078 | ** it overflows an integer. |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1079 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1080 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1081 | SumCtx *p; |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 1082 | int type; |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 1083 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 1084 | UNUSED_PARAMETER(argc); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1085 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 1086 | type = sqlite3_value_numeric_type(argv[0]); |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 1087 | if( p && type!=SQLITE_NULL ){ |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 1088 | p->cnt++; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 1089 | if( type==SQLITE_INTEGER ){ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1090 | i64 v = sqlite3_value_int64(argv[0]); |
| 1091 | p->rSum += v; |
| 1092 | if( (p->approx|p->overflow)==0 ){ |
| 1093 | i64 iNewSum = p->iSum + v; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1094 | int s1 = (int)(p->iSum >> (sizeof(i64)*8-1)); |
| 1095 | int s2 = (int)(v >> (sizeof(i64)*8-1)); |
| 1096 | int s3 = (int)(iNewSum >> (sizeof(i64)*8-1)); |
| 1097 | p->overflow = ((s1&s2&~s3) | (~s1&~s2&s3))?1:0; |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1098 | p->iSum = iNewSum; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 1099 | } |
| 1100 | }else{ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1101 | p->rSum += sqlite3_value_double(argv[0]); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 1102 | p->approx = 1; |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 1103 | } |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 1104 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1105 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1106 | static void sumFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1107 | SumCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 1108 | p = sqlite3_aggregate_context(context, 0); |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 1109 | if( p && p->cnt>0 ){ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1110 | if( p->overflow ){ |
| 1111 | sqlite3_result_error(context,"integer overflow",-1); |
| 1112 | }else if( p->approx ){ |
| 1113 | sqlite3_result_double(context, p->rSum); |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 1114 | }else{ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1115 | sqlite3_result_int64(context, p->iSum); |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 1116 | } |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 1117 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1118 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1119 | static void avgFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1120 | SumCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 1121 | p = sqlite3_aggregate_context(context, 0); |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 1122 | if( p && p->cnt>0 ){ |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1123 | sqlite3_result_double(context, p->rSum/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 1126 | static void totalFinalize(sqlite3_context *context){ |
| 1127 | SumCtx *p; |
| 1128 | p = sqlite3_aggregate_context(context, 0); |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 1129 | sqlite3_result_double(context, p ? p->rSum : 0.0); |
drh | a97fdd3 | 2006-01-12 22:17:50 +0000 | [diff] [blame] | 1130 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1131 | |
| 1132 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1133 | ** The following structure keeps track of state information for the |
| 1134 | ** count() aggregate function. |
| 1135 | */ |
| 1136 | typedef struct CountCtx CountCtx; |
| 1137 | struct CountCtx { |
drh | fc6ad39 | 2006-02-09 13:38:19 +0000 | [diff] [blame] | 1138 | i64 n; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1139 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1140 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1141 | /* |
| 1142 | ** Routines to implement the count() aggregate function. |
| 1143 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1144 | static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1145 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1146 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1147 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1148 | p->n++; |
| 1149 | } |
| 1150 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1151 | static void countFinalize(sqlite3_context *context){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1152 | CountCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 1153 | p = sqlite3_aggregate_context(context, 0); |
drh | fc6ad39 | 2006-02-09 13:38:19 +0000 | [diff] [blame] | 1154 | sqlite3_result_int64(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1158 | ** Routines to implement min() and max() aggregate functions. |
| 1159 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 1160 | static void minmaxStep( |
| 1161 | sqlite3_context *context, |
| 1162 | int NotUsed, |
| 1163 | sqlite3_value **argv |
| 1164 | ){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1165 | Mem *pArg = (Mem *)argv[0]; |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 1166 | Mem *pBest; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 1167 | UNUSED_PARAMETER(NotUsed); |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 1168 | |
| 1169 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 1170 | pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 +0000 | [diff] [blame] | 1171 | if( !pBest ) return; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1172 | |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1173 | if( pBest->flags ){ |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 1174 | int max; |
| 1175 | int cmp; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1176 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1177 | /* This step function is used for both the min() and max() aggregates, |
| 1178 | ** the only difference between the two being that the sense of the |
| 1179 | ** comparison is inverted. For the max() aggregate, the |
| 1180 | ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 1181 | ** returns (void *)db, where db is the sqlite3* database pointer. |
| 1182 | ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 1183 | ** aggregate, or 0 for min(). |
| 1184 | */ |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 1185 | max = sqlite3_user_data(context)!=0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1186 | cmp = sqlite3MemCompare(pBest, pArg, pColl); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1187 | if( (max && cmp<0) || (!max && cmp>0) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1188 | sqlite3VdbeMemCopy(pBest, pArg); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1189 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1190 | }else{ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1191 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1192 | } |
| 1193 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1194 | static void minMaxFinalize(sqlite3_context *context){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 +0000 | [diff] [blame] | 1195 | sqlite3_value *pRes; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 1196 | pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); |
| 1197 | if( pRes ){ |
| 1198 | if( pRes->flags ){ |
| 1199 | sqlite3_result_value(context, pRes); |
| 1200 | } |
| 1201 | sqlite3VdbeMemRelease(pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 1204 | |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1205 | /* |
| 1206 | ** group_concat(EXPR, ?SEPARATOR?) |
| 1207 | */ |
| 1208 | static void groupConcatStep( |
| 1209 | sqlite3_context *context, |
| 1210 | int argc, |
| 1211 | sqlite3_value **argv |
| 1212 | ){ |
| 1213 | const char *zVal; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1214 | StrAccum *pAccum; |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1215 | const char *zSep; |
drh | 07d3117 | 2009-02-02 21:57:05 +0000 | [diff] [blame^] | 1216 | int nVal, nSep; |
| 1217 | assert( argc==1 || argc==2 ); |
| 1218 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1219 | pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); |
| 1220 | |
| 1221 | if( pAccum ){ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1222 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1223 | pAccum->useMalloc = 1; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1224 | pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1225 | if( pAccum->nChar ){ |
drh | 07d3117 | 2009-02-02 21:57:05 +0000 | [diff] [blame^] | 1226 | if( argc==2 ){ |
| 1227 | zSep = (char*)sqlite3_value_text(argv[1]); |
| 1228 | nSep = sqlite3_value_bytes(argv[1]); |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1229 | }else{ |
| 1230 | zSep = ","; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1231 | nSep = 1; |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1232 | } |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1233 | sqlite3StrAccumAppend(pAccum, zSep, nSep); |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1234 | } |
drh | 07d3117 | 2009-02-02 21:57:05 +0000 | [diff] [blame^] | 1235 | zVal = (char*)sqlite3_value_text(argv[0]); |
| 1236 | nVal = sqlite3_value_bytes(argv[0]); |
| 1237 | sqlite3StrAccumAppend(pAccum, zVal, nVal); |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | static void groupConcatFinalize(sqlite3_context *context){ |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1241 | StrAccum *pAccum; |
| 1242 | pAccum = sqlite3_aggregate_context(context, 0); |
| 1243 | if( pAccum ){ |
| 1244 | if( pAccum->tooBig ){ |
| 1245 | sqlite3_result_error_toobig(context); |
| 1246 | }else if( pAccum->mallocFailed ){ |
| 1247 | sqlite3_result_error_nomem(context); |
| 1248 | }else{ |
| 1249 | sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, |
| 1250 | sqlite3_free); |
| 1251 | } |
drh | b068969 | 2007-11-01 17:38:30 +0000 | [diff] [blame] | 1252 | } |
| 1253 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 1254 | |
drh | d3a149e | 2002-02-24 17:12:53 +0000 | [diff] [blame] | 1255 | /* |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 1256 | ** This function registered all of the above C functions as SQL |
| 1257 | ** functions. This should be the only routine in this file with |
| 1258 | ** external linkage. |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1259 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1260 | void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 1261 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 1262 | sqlite3AlterFunctions(db); |
| 1263 | #endif |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1264 | if( !db->mallocFailed ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 1265 | int rc = sqlite3_overload_function(db, "MATCH", 2); |
| 1266 | assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); |
| 1267 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1268 | db->mallocFailed = 1; |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 1269 | } |
| 1270 | } |
danielk1977 | fd9e1f3 | 2005-05-22 10:44:34 +0000 | [diff] [blame] | 1271 | #ifdef SQLITE_SSE |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 1272 | (void)sqlite3SseFunctions(db); |
danielk1977 | fd9e1f3 | 2005-05-22 10:44:34 +0000 | [diff] [blame] | 1273 | #endif |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | ** Set the LIKEOPT flag on the 2-argument function with the given name. |
| 1278 | */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1279 | static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1280 | FuncDef *pDef; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1281 | pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName), |
| 1282 | 2, SQLITE_UTF8, 0); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1283 | if( pDef ){ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1284 | pDef->flags = flagVal; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | ** Register the built-in LIKE and GLOB functions. The caseSensitive |
| 1290 | ** parameter determines whether or not the LIKE operator is case |
| 1291 | ** sensitive. GLOB is always case sensitive. |
| 1292 | */ |
| 1293 | void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ |
| 1294 | struct compareInfo *pInfo; |
| 1295 | if( caseSensitive ){ |
| 1296 | pInfo = (struct compareInfo*)&likeInfoAlt; |
| 1297 | }else{ |
| 1298 | pInfo = (struct compareInfo*)&likeInfoNorm; |
| 1299 | } |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1300 | sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); |
| 1301 | sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); |
| 1302 | sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1303 | (struct compareInfo*)&globInfo, likeFunc, 0,0); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1304 | setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); |
| 1305 | setLikeOptFlag(db, "like", |
| 1306 | caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | ** pExpr points to an expression which implements a function. If |
| 1311 | ** it is appropriate to apply the LIKE optimization to that function |
| 1312 | ** then set aWc[0] through aWc[2] to the wildcard characters and |
| 1313 | ** return TRUE. If the function is not a LIKE-style function then |
| 1314 | ** return FALSE. |
| 1315 | */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1316 | int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1317 | FuncDef *pDef; |
danielk1977 | cdbd8ef | 2007-05-12 06:11:12 +0000 | [diff] [blame] | 1318 | if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |
| 1321 | if( pExpr->pList->nExpr!=2 ){ |
| 1322 | return 0; |
| 1323 | } |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1324 | pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1325 | SQLITE_UTF8, 0); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1326 | if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | /* The memcpy() statement assumes that the wildcard characters are |
| 1331 | ** the first three statements in the compareInfo structure. The |
| 1332 | ** asserts() that follow verify that assumption |
| 1333 | */ |
| 1334 | memcpy(aWc, pDef->pUserData, 3); |
| 1335 | assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); |
| 1336 | assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); |
| 1337 | assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1338 | *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1339 | return 1; |
drh | dc04c58 | 2002-02-24 01:55:15 +0000 | [diff] [blame] | 1340 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1341 | |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1342 | /* |
drh | 777c538 | 2008-08-21 20:21:34 +0000 | [diff] [blame] | 1343 | ** All all of the FuncDef structures in the aBuiltinFunc[] array above |
| 1344 | ** to the global function hash table. This occurs at start-time (as |
| 1345 | ** a consequence of calling sqlite3_initialize()). |
| 1346 | ** |
| 1347 | ** After this routine runs |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1348 | */ |
| 1349 | void sqlite3RegisterGlobalFunctions(void){ |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 +0000 | [diff] [blame] | 1350 | /* |
| 1351 | ** The following array holds FuncDef structures for all of the functions |
| 1352 | ** defined in this file. |
| 1353 | ** |
| 1354 | ** The array cannot be constant since changes are made to the |
| 1355 | ** FuncDef.pHash elements at start-time. The elements of this array |
| 1356 | ** are read-only after initialization is complete. |
| 1357 | */ |
| 1358 | static SQLITE_WSD FuncDef aBuiltinFunc[] = { |
| 1359 | FUNCTION(ltrim, 1, 1, 0, trimFunc ), |
| 1360 | FUNCTION(ltrim, 2, 1, 0, trimFunc ), |
| 1361 | FUNCTION(rtrim, 1, 2, 0, trimFunc ), |
| 1362 | FUNCTION(rtrim, 2, 2, 0, trimFunc ), |
| 1363 | FUNCTION(trim, 1, 3, 0, trimFunc ), |
| 1364 | FUNCTION(trim, 2, 3, 0, trimFunc ), |
| 1365 | FUNCTION(min, -1, 0, 1, minmaxFunc ), |
| 1366 | FUNCTION(min, 0, 0, 1, 0 ), |
| 1367 | AGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize ), |
| 1368 | FUNCTION(max, -1, 1, 1, minmaxFunc ), |
| 1369 | FUNCTION(max, 0, 1, 1, 0 ), |
| 1370 | AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ), |
| 1371 | FUNCTION(typeof, 1, 0, 0, typeofFunc ), |
| 1372 | FUNCTION(length, 1, 0, 0, lengthFunc ), |
| 1373 | FUNCTION(substr, 2, 0, 0, substrFunc ), |
| 1374 | FUNCTION(substr, 3, 0, 0, substrFunc ), |
| 1375 | FUNCTION(abs, 1, 0, 0, absFunc ), |
| 1376 | FUNCTION(round, 1, 0, 0, roundFunc ), |
| 1377 | FUNCTION(round, 2, 0, 0, roundFunc ), |
| 1378 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 1379 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 1380 | FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 1381 | FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), |
| 1382 | FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 1383 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 1384 | FUNCTION(ifnull, 2, 0, 1, ifnullFunc ), |
drh | 9373b01 | 2009-02-02 01:50:39 +0000 | [diff] [blame] | 1385 | FUNCTION(random, 0, 0, 0, randomFunc ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 +0000 | [diff] [blame] | 1386 | FUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 1387 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 1388 | FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 1389 | FUNCTION(quote, 1, 0, 0, quoteFunc ), |
| 1390 | FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), |
| 1391 | FUNCTION(changes, 0, 0, 0, changes ), |
| 1392 | FUNCTION(total_changes, 0, 0, 0, total_changes ), |
| 1393 | FUNCTION(replace, 3, 0, 0, replaceFunc ), |
| 1394 | FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), |
| 1395 | #ifdef SQLITE_SOUNDEX |
| 1396 | FUNCTION(soundex, 1, 0, 0, soundexFunc ), |
| 1397 | #endif |
| 1398 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 1399 | FUNCTION(load_extension, 1, 0, 0, loadExt ), |
| 1400 | FUNCTION(load_extension, 2, 0, 0, loadExt ), |
| 1401 | #endif |
| 1402 | AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), |
| 1403 | AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), |
| 1404 | AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), |
| 1405 | AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), |
| 1406 | AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), |
drh | 07d3117 | 2009-02-02 21:57:05 +0000 | [diff] [blame^] | 1407 | AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), |
| 1408 | AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 +0000 | [diff] [blame] | 1409 | |
| 1410 | LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 1411 | #ifdef SQLITE_CASE_SENSITIVE_LIKE |
| 1412 | LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 1413 | LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 1414 | #else |
| 1415 | LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), |
| 1416 | LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), |
| 1417 | #endif |
| 1418 | }; |
| 1419 | |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1420 | int i; |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 +0000 | [diff] [blame] | 1421 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
drh | 106cee5 | 2008-09-03 17:11:16 +0000 | [diff] [blame] | 1422 | FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc); |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 +0000 | [diff] [blame] | 1423 | |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1424 | for(i=0; i<ArraySize(aBuiltinFunc); i++){ |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 +0000 | [diff] [blame] | 1425 | sqlite3FuncDefInsert(pHash, &aFunc[i]); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1426 | } |
drh | 777c538 | 2008-08-21 20:21:34 +0000 | [diff] [blame] | 1427 | sqlite3RegisterDateTimeFunctions(); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1428 | } |