drh | dc04c58 | 2002-02-24 01:55:15 | [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 | ************************************************************************* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 12 | ** This file contains the C-language implementations for many of the SQL |
drh | ede7ae3 | 2014-08-06 11:58:40 | [diff] [blame] | 13 | ** functions of SQLite. (Some function, and in particular the date and |
| 14 | ** time functions, are implemented separately.) |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 15 | */ |
drh | b659e9b | 2005-01-28 01:29:08 | [diff] [blame] | 16 | #include "sqliteInt.h" |
drh | d3a149e | 2002-02-24 17:12:53 | [diff] [blame] | 17 | #include <stdlib.h> |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 18 | #include <assert.h> |
drh | ef9f719 | 2020-01-17 19:14:08 | [diff] [blame] | 19 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 05d7bfd | 2019-05-10 12:06:47 | [diff] [blame] | 20 | #include <math.h> |
drh | ef9f719 | 2020-01-17 19:14:08 | [diff] [blame] | 21 | #endif |
danielk1977 | 8820805 | 2004-05-25 01:13:20 | [diff] [blame] | 22 | #include "vdbeInt.h" |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 23 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 24 | /* |
| 25 | ** Return the collating function associated with a function. |
| 26 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 27 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
drh | a9e03b1 | 2015-03-12 06:46:52 | [diff] [blame] | 28 | VdbeOp *pOp; |
| 29 | assert( context->pVdbe!=0 ); |
| 30 | pOp = &context->pVdbe->aOp[context->iOp-1]; |
drh | a15cc47 | 2014-09-25 13:17:30 | [diff] [blame] | 31 | assert( pOp->opcode==OP_CollSeq ); |
| 32 | assert( pOp->p4type==P4_COLLSEQ ); |
| 33 | return pOp->p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 34 | } |
| 35 | |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 36 | /* |
drh | 7a95789 | 2012-02-02 17:35:43 | [diff] [blame] | 37 | ** Indicate that the accumulator load should be skipped on this |
| 38 | ** iteration of the aggregate loop. |
| 39 | */ |
| 40 | static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ |
drh | 21d5978 | 2018-01-23 19:24:54 | [diff] [blame] | 41 | assert( context->isError<=0 ); |
| 42 | context->isError = -1; |
drh | 7a95789 | 2012-02-02 17:35:43 | [diff] [blame] | 43 | context->skipFlag = 1; |
| 44 | } |
| 45 | |
| 46 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 47 | ** Implementation of the non-aggregate min() and max() functions |
| 48 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 49 | static void minmaxFunc( |
| 50 | sqlite3_context *context, |
| 51 | int argc, |
| 52 | sqlite3_value **argv |
| 53 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 54 | int i; |
drh | 268380c | 2004-02-25 13:47:31 | [diff] [blame] | 55 | int mask; /* 0 for min() or 0xffffffff for max() */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 56 | int iBest; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 57 | CollSeq *pColl; |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 58 | |
drh | 65595cd | 2009-02-02 16:32:55 | [diff] [blame] | 59 | assert( argc>1 ); |
drh | c44af71 | 2004-09-02 15:53:56 | [diff] [blame] | 60 | mask = sqlite3_user_data(context)==0 ? 0 : -1; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 61 | pColl = sqlite3GetFuncCollSeq(context); |
| 62 | assert( pColl ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 | [diff] [blame] | 63 | assert( mask==-1 || mask==0 ); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 64 | iBest = 0; |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 65 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 66 | for(i=1; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 67 | if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 68 | if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ |
drh | 65595cd | 2009-02-02 16:32:55 | [diff] [blame] | 69 | testcase( mask==0 ); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 70 | iBest = i; |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 71 | } |
| 72 | } |
drh | f447950 | 2004-05-27 03:12:53 | [diff] [blame] | 73 | sqlite3_result_value(context, argv[iBest]); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 74 | } |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 75 | |
drh | 268380c | 2004-02-25 13:47:31 | [diff] [blame] | 76 | /* |
| 77 | ** Return the type of the argument. |
| 78 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 79 | static void typeofFunc( |
| 80 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 81 | int NotUsed, |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 82 | sqlite3_value **argv |
| 83 | ){ |
drh | 9d8e401 | 2017-07-06 13:51:50 | [diff] [blame] | 84 | static const char *azType[] = { "integer", "real", "text", "blob", "null" }; |
| 85 | int i = sqlite3_value_type(argv[0]) - 1; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 86 | UNUSED_PARAMETER(NotUsed); |
drh | 9d8e401 | 2017-07-06 13:51:50 | [diff] [blame] | 87 | assert( i>=0 && i<ArraySize(azType) ); |
| 88 | assert( SQLITE_INTEGER==1 ); |
| 89 | assert( SQLITE_FLOAT==2 ); |
| 90 | assert( SQLITE_TEXT==3 ); |
| 91 | assert( SQLITE_BLOB==4 ); |
| 92 | assert( SQLITE_NULL==5 ); |
drh | 3cef364 | 2017-07-14 19:22:08 | [diff] [blame] | 93 | /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns |
| 94 | ** the datatype code for the initial datatype of the sqlite3_value object |
| 95 | ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, |
| 96 | ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */ |
drh | 9d8e401 | 2017-07-06 13:51:50 | [diff] [blame] | 97 | sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 98 | } |
| 99 | |
drh | 9d44f18 | 2022-01-09 16:54:02 | [diff] [blame] | 100 | /* subtype(X) |
| 101 | ** |
| 102 | ** Return the subtype of X |
| 103 | */ |
| 104 | static void subtypeFunc( |
| 105 | sqlite3_context *context, |
| 106 | int argc, |
| 107 | sqlite3_value **argv |
| 108 | ){ |
drh | 69b0ce3 | 2022-02-04 13:15:01 | [diff] [blame] | 109 | UNUSED_PARAMETER(argc); |
drh | 9d44f18 | 2022-01-09 16:54:02 | [diff] [blame] | 110 | sqlite3_result_int(context, sqlite3_value_subtype(argv[0])); |
| 111 | } |
drh | 5708d2d | 2005-06-22 10:53:59 | [diff] [blame] | 112 | |
| 113 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 114 | ** Implementation of the length() function |
| 115 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 116 | static void lengthFunc( |
| 117 | sqlite3_context *context, |
| 118 | int argc, |
| 119 | sqlite3_value **argv |
| 120 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 121 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 122 | UNUSED_PARAMETER(argc); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 123 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 124 | case SQLITE_BLOB: |
| 125 | case SQLITE_INTEGER: |
| 126 | case SQLITE_FLOAT: { |
drh | f447950 | 2004-05-27 03:12:53 | [diff] [blame] | 127 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 128 | break; |
| 129 | } |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 130 | case SQLITE_TEXT: { |
drh | 2646da7 | 2005-12-09 20:02:05 | [diff] [blame] | 131 | const unsigned char *z = sqlite3_value_text(argv[0]); |
drh | 7ea3469 | 2018-01-23 04:22:33 | [diff] [blame] | 132 | const unsigned char *z0; |
| 133 | unsigned char c; |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 134 | if( z==0 ) return; |
drh | 7ea3469 | 2018-01-23 04:22:33 | [diff] [blame] | 135 | z0 = z; |
| 136 | while( (c = *z)!=0 ){ |
| 137 | z++; |
| 138 | if( c>=0xc0 ){ |
| 139 | while( (*z & 0xc0)==0x80 ){ z++; z0++; } |
| 140 | } |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 141 | } |
drh | 7ea3469 | 2018-01-23 04:22:33 | [diff] [blame] | 142 | sqlite3_result_int(context, (int)(z-z0)); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | default: { |
| 146 | sqlite3_result_null(context); |
| 147 | break; |
| 148 | } |
| 149 | } |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
drh | b3d7f1c | 2023-06-03 11:22:30 | [diff] [blame] | 153 | ** Implementation of the octet_length() function |
| 154 | */ |
| 155 | static void bytelengthFunc( |
| 156 | sqlite3_context *context, |
| 157 | int argc, |
| 158 | sqlite3_value **argv |
| 159 | ){ |
| 160 | assert( argc==1 ); |
| 161 | UNUSED_PARAMETER(argc); |
| 162 | switch( sqlite3_value_type(argv[0]) ){ |
| 163 | case SQLITE_BLOB: { |
| 164 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
| 165 | break; |
| 166 | } |
| 167 | case SQLITE_INTEGER: |
| 168 | case SQLITE_FLOAT: { |
| 169 | i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2; |
| 170 | sqlite3_result_int64(context, sqlite3_value_bytes(argv[0])*m); |
| 171 | break; |
| 172 | } |
| 173 | case SQLITE_TEXT: { |
| 174 | if( sqlite3_value_encoding(argv[0])<=SQLITE_UTF8 ){ |
| 175 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
| 176 | }else{ |
| 177 | sqlite3_result_int(context, sqlite3_value_bytes16(argv[0])); |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | default: { |
| 182 | sqlite3_result_null(context); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 189 | ** Implementation of the abs() function. |
| 190 | ** |
| 191 | ** IMP: R-23979-26855 The abs(X) function returns the absolute value of |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 192 | ** the numeric argument X. |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 193 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 194 | static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 195 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 196 | UNUSED_PARAMETER(argc); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 197 | switch( sqlite3_value_type(argv[0]) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 198 | case SQLITE_INTEGER: { |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 | [diff] [blame] | 199 | i64 iVal = sqlite3_value_int64(argv[0]); |
drh | 52fc849 | 2006-02-23 21:43:55 | [diff] [blame] | 200 | if( iVal<0 ){ |
drh | 693e671 | 2014-01-24 22:58:00 | [diff] [blame] | 201 | if( iVal==SMALLEST_INT64 ){ |
drh | eb091cd | 2013-11-09 19:47:15 | [diff] [blame] | 202 | /* IMP: R-31676-45509 If X is the integer -9223372036854775808 |
| 203 | ** then abs(X) throws an integer overflow error since there is no |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 204 | ** equivalent positive 64-bit two complement value. */ |
drh | 52fc849 | 2006-02-23 21:43:55 | [diff] [blame] | 205 | sqlite3_result_error(context, "integer overflow", -1); |
| 206 | return; |
| 207 | } |
| 208 | iVal = -iVal; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 209 | } |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 | [diff] [blame] | 210 | sqlite3_result_int64(context, iVal); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 211 | break; |
| 212 | } |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 213 | case SQLITE_NULL: { |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 214 | /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 215 | sqlite3_result_null(context); |
| 216 | break; |
| 217 | } |
| 218 | default: { |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 219 | /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 220 | ** something that can be converted into a number, we have: |
drh | 643091f | 2014-11-20 23:21:23 | [diff] [blame] | 221 | ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob |
| 222 | ** that cannot be converted to a numeric value. |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 223 | */ |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 | [diff] [blame] | 224 | double rVal = sqlite3_value_double(argv[0]); |
drh | 52fc849 | 2006-02-23 21:43:55 | [diff] [blame] | 225 | if( rVal<0 ) rVal = -rVal; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 | [diff] [blame] | 226 | sqlite3_result_double(context, rVal); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 233 | ** Implementation of the instr() function. |
| 234 | ** |
| 235 | ** instr(haystack,needle) finds the first occurrence of needle |
| 236 | ** in haystack and returns the number of previous characters plus 1, |
| 237 | ** or 0 if needle does not occur within haystack. |
| 238 | ** |
| 239 | ** If both haystack and needle are BLOBs, then the result is one more than |
| 240 | ** the number of bytes in haystack prior to the first occurrence of needle, |
| 241 | ** or 0 if needle never occurs in haystack. |
| 242 | */ |
| 243 | static void instrFunc( |
| 244 | sqlite3_context *context, |
| 245 | int argc, |
| 246 | sqlite3_value **argv |
| 247 | ){ |
| 248 | const unsigned char *zHaystack; |
| 249 | const unsigned char *zNeedle; |
| 250 | int nHaystack; |
| 251 | int nNeedle; |
| 252 | int typeHaystack, typeNeedle; |
| 253 | int N = 1; |
| 254 | int isText; |
drh | c930b40 | 2019-01-08 15:18:24 | [diff] [blame] | 255 | unsigned char firstChar; |
drh | 97b0250 | 2019-09-17 03:16:29 | [diff] [blame] | 256 | sqlite3_value *pC1 = 0; |
| 257 | sqlite3_value *pC2 = 0; |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 258 | |
drh | 68c804b | 2012-12-04 11:03:11 | [diff] [blame] | 259 | UNUSED_PARAMETER(argc); |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 260 | typeHaystack = sqlite3_value_type(argv[0]); |
| 261 | typeNeedle = sqlite3_value_type(argv[1]); |
| 262 | if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; |
| 263 | nHaystack = sqlite3_value_bytes(argv[0]); |
| 264 | nNeedle = sqlite3_value_bytes(argv[1]); |
dan | 895decf | 2016-12-30 14:15:56 | [diff] [blame] | 265 | if( nNeedle>0 ){ |
| 266 | if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ |
| 267 | zHaystack = sqlite3_value_blob(argv[0]); |
| 268 | zNeedle = sqlite3_value_blob(argv[1]); |
| 269 | isText = 0; |
drh | 97b0250 | 2019-09-17 03:16:29 | [diff] [blame] | 270 | }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){ |
dan | 895decf | 2016-12-30 14:15:56 | [diff] [blame] | 271 | zHaystack = sqlite3_value_text(argv[0]); |
| 272 | zNeedle = sqlite3_value_text(argv[1]); |
| 273 | isText = 1; |
drh | 97b0250 | 2019-09-17 03:16:29 | [diff] [blame] | 274 | }else{ |
| 275 | pC1 = sqlite3_value_dup(argv[0]); |
| 276 | zHaystack = sqlite3_value_text(pC1); |
drh | 9d70284 | 2019-09-18 11:16:46 | [diff] [blame] | 277 | if( zHaystack==0 ) goto endInstrOOM; |
| 278 | nHaystack = sqlite3_value_bytes(pC1); |
drh | 97b0250 | 2019-09-17 03:16:29 | [diff] [blame] | 279 | pC2 = sqlite3_value_dup(argv[1]); |
| 280 | zNeedle = sqlite3_value_text(pC2); |
drh | 9d70284 | 2019-09-18 11:16:46 | [diff] [blame] | 281 | if( zNeedle==0 ) goto endInstrOOM; |
| 282 | nNeedle = sqlite3_value_bytes(pC2); |
drh | 97b0250 | 2019-09-17 03:16:29 | [diff] [blame] | 283 | isText = 1; |
dan | 895decf | 2016-12-30 14:15:56 | [diff] [blame] | 284 | } |
drh | 9d70284 | 2019-09-18 11:16:46 | [diff] [blame] | 285 | if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM; |
drh | c930b40 | 2019-01-08 15:18:24 | [diff] [blame] | 286 | firstChar = zNeedle[0]; |
| 287 | while( nNeedle<=nHaystack |
| 288 | && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0) |
| 289 | ){ |
dan | 895decf | 2016-12-30 14:15:56 | [diff] [blame] | 290 | N++; |
| 291 | do{ |
| 292 | nHaystack--; |
| 293 | zHaystack++; |
| 294 | }while( isText && (zHaystack[0]&0xc0)==0x80 ); |
| 295 | } |
| 296 | if( nNeedle>nHaystack ) N = 0; |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 297 | } |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 298 | sqlite3_result_int(context, N); |
drh | 97b0250 | 2019-09-17 03:16:29 | [diff] [blame] | 299 | endInstr: |
| 300 | sqlite3_value_free(pC1); |
| 301 | sqlite3_value_free(pC2); |
drh | 9d70284 | 2019-09-18 11:16:46 | [diff] [blame] | 302 | return; |
| 303 | endInstrOOM: |
| 304 | sqlite3_result_error_nomem(context); |
| 305 | goto endInstr; |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
drh | 6bcd585 | 2022-01-08 21:00:38 | [diff] [blame] | 309 | ** Implementation of the printf() (a.k.a. format()) SQL function. |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 310 | */ |
| 311 | static void printfFunc( |
| 312 | sqlite3_context *context, |
| 313 | int argc, |
| 314 | sqlite3_value **argv |
| 315 | ){ |
| 316 | PrintfArguments x; |
| 317 | StrAccum str; |
| 318 | const char *zFormat; |
| 319 | int n; |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 320 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 321 | |
| 322 | if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ |
| 323 | x.nArg = argc-1; |
| 324 | x.nUsed = 0; |
| 325 | x.apArg = argv+1; |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 326 | sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 327 | str.printfFlags = SQLITE_PRINTF_SQLFUNC; |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 328 | sqlite3_str_appendf(&str, zFormat, &x); |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 329 | n = str.nChar; |
| 330 | sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, |
| 331 | SQLITE_DYNAMIC); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 336 | ** Implementation of the substr() function. |
| 337 | ** |
| 338 | ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. |
| 339 | ** p1 is 1-indexed. So substr(x,1,1) returns the first character |
| 340 | ** of x. If x is text, then we actually count UTF-8 characters. |
| 341 | ** If x is a blob, then we count bytes. |
| 342 | ** |
| 343 | ** If p1 is negative, then we begin abs(p1) from the end of x[]. |
shaneh | 779b8f1 | 2009-11-12 05:04:50 | [diff] [blame] | 344 | ** |
drh | f7b5496 | 2013-05-28 12:11:54 | [diff] [blame] | 345 | ** If p2 is negative, return the p2 characters preceding p1. |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 346 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 347 | static void substrFunc( |
| 348 | sqlite3_context *context, |
| 349 | int argc, |
| 350 | sqlite3_value **argv |
| 351 | ){ |
drh | 2646da7 | 2005-12-09 20:02:05 | [diff] [blame] | 352 | const unsigned char *z; |
| 353 | const unsigned char *z2; |
drh | 023ae03 | 2007-05-08 12:12:16 | [diff] [blame] | 354 | int len; |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 355 | int p0type; |
drh | 023ae03 | 2007-05-08 12:12:16 | [diff] [blame] | 356 | i64 p1, p2; |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 357 | |
drh | 64f3151 | 2007-10-12 19:11:55 | [diff] [blame] | 358 | assert( argc==3 || argc==2 ); |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 359 | p0type = sqlite3_value_type(argv[0]); |
drh | b097ef2 | 2024-12-18 20:29:29 | [diff] [blame] | 360 | p1 = sqlite3_value_int64(argv[1]); |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 361 | if( p0type==SQLITE_BLOB ){ |
| 362 | len = sqlite3_value_bytes(argv[0]); |
| 363 | z = sqlite3_value_blob(argv[0]); |
| 364 | if( z==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 365 | assert( len==sqlite3_value_bytes(argv[0]) ); |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 366 | }else{ |
| 367 | z = sqlite3_value_text(argv[0]); |
| 368 | if( z==0 ) return; |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 369 | len = 0; |
drh | 4adc4cb | 2009-11-11 20:53:31 | [diff] [blame] | 370 | if( p1<0 ){ |
| 371 | for(z2=z; *z2; len++){ |
| 372 | SQLITE_SKIP_UTF8(z2); |
| 373 | } |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 374 | } |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 375 | } |
drh | 64f3151 | 2007-10-12 19:11:55 | [diff] [blame] | 376 | if( argc==3 ){ |
drh | b097ef2 | 2024-12-18 20:29:29 | [diff] [blame] | 377 | p2 = sqlite3_value_int64(argv[2]); |
drh | 3efac4a | 2025-02-09 20:23:29 | [diff] [blame] | 378 | if( p2==0 && sqlite3_value_type(argv[2])==SQLITE_NULL ) return; |
drh | 64f3151 | 2007-10-12 19:11:55 | [diff] [blame] | 379 | }else{ |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 380 | p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; |
drh | 64f3151 | 2007-10-12 19:11:55 | [diff] [blame] | 381 | } |
drh | 3efac4a | 2025-02-09 20:23:29 | [diff] [blame] | 382 | if( p1==0 ){ |
| 383 | #ifdef SQLITE_SUBSTR_COMPATIBILITY |
| 384 | /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as |
| 385 | ** as substr(X,1,N) - it returns the first N characters of X. This |
| 386 | ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] |
| 387 | ** from 2009-02-02 for compatibility of applications that exploited the |
| 388 | ** old buggy behavior. */ |
| 389 | p1 = 1; /* <rdar://problem/6778339> */ |
| 390 | #endif |
| 391 | if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return; |
| 392 | } |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 393 | if( p1<0 ){ |
drh | 89425d5 | 2002-02-28 03:04:48 | [diff] [blame] | 394 | p1 += len; |
drh | 653bc75 | 2002-02-28 03:31:10 | [diff] [blame] | 395 | if( p1<0 ){ |
drh | e0190a6 | 2024-12-28 12:32:01 | [diff] [blame] | 396 | if( p2<0 ){ |
| 397 | p2 = 0; |
| 398 | }else{ |
| 399 | p2 += p1; |
| 400 | } |
drh | 653bc75 | 2002-02-28 03:31:10 | [diff] [blame] | 401 | p1 = 0; |
| 402 | } |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 403 | }else if( p1>0 ){ |
| 404 | p1--; |
drh | 65595cd | 2009-02-02 16:32:55 | [diff] [blame] | 405 | }else if( p2>0 ){ |
| 406 | p2--; |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 407 | } |
drh | e0190a6 | 2024-12-28 12:32:01 | [diff] [blame] | 408 | if( p2<0 ){ |
| 409 | if( p2<-p1 ){ |
| 410 | p2 = p1; |
| 411 | }else{ |
| 412 | p2 = -p2; |
drh | 4e79c59 | 2009-02-01 19:23:32 | [diff] [blame] | 413 | } |
drh | e0190a6 | 2024-12-28 12:32:01 | [diff] [blame] | 414 | p1 -= p2; |
drh | 4e79c59 | 2009-02-01 19:23:32 | [diff] [blame] | 415 | } |
drh | 65595cd | 2009-02-02 16:32:55 | [diff] [blame] | 416 | assert( p1>=0 && p2>=0 ); |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 417 | if( p0type!=SQLITE_BLOB ){ |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 418 | while( *z && p1 ){ |
| 419 | SQLITE_SKIP_UTF8(z); |
| 420 | p1--; |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 421 | } |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 422 | for(z2=z; *z2 && p2; p2--){ |
| 423 | SQLITE_SKIP_UTF8(z2); |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 424 | } |
drh | bbf483f | 2014-09-09 20:30:24 | [diff] [blame] | 425 | sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, |
| 426 | SQLITE_UTF8); |
drh | f764e6f | 2007-05-15 01:13:47 | [diff] [blame] | 427 | }else{ |
drh | 2dcd4fa | 2024-12-19 12:08:39 | [diff] [blame] | 428 | if( p1>=len ){ |
| 429 | p1 = p2 = 0; |
| 430 | }else if( p2>len-p1 ){ |
drh | 4adc4cb | 2009-11-11 20:53:31 | [diff] [blame] | 431 | p2 = len-p1; |
drh | 2dcd4fa | 2024-12-19 12:08:39 | [diff] [blame] | 432 | assert( p2>0 ); |
drh | 4adc4cb | 2009-11-11 20:53:31 | [diff] [blame] | 433 | } |
drh | bbf483f | 2014-09-09 20:30:24 | [diff] [blame] | 434 | sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 435 | } |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* |
| 439 | ** Implementation of the round() function |
| 440 | */ |
shane | fbd60f8 | 2009-02-04 03:59:25 | [diff] [blame] | 441 | #ifndef SQLITE_OMIT_FLOATING_POINT |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 442 | static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 47bc07d | 2024-12-18 18:29:19 | [diff] [blame] | 443 | i64 n = 0; |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 444 | double r; |
drh | 50d654d | 2009-06-03 01:24:54 | [diff] [blame] | 445 | char *zBuf; |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 446 | assert( argc==1 || argc==2 ); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 447 | if( argc==2 ){ |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 448 | if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; |
drh | 47bc07d | 2024-12-18 18:29:19 | [diff] [blame] | 449 | n = sqlite3_value_int64(argv[1]); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 450 | if( n>30 ) n = 30; |
| 451 | if( n<0 ) n = 0; |
| 452 | } |
drh | d589a92 | 2006-03-02 03:02:48 | [diff] [blame] | 453 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 454 | r = sqlite3_value_double(argv[0]); |
shaneh | 147e176 | 2010-02-17 04:19:27 | [diff] [blame] | 455 | /* If Y==0 and X will fit in a 64-bit int, |
| 456 | ** handle the rounding directly, |
| 457 | ** otherwise use printf. |
| 458 | */ |
drh | 84422db | 2019-05-30 13:47:10 | [diff] [blame] | 459 | if( r<-4503599627370496.0 || r>+4503599627370496.0 ){ |
| 460 | /* The value has no fractional part so there is nothing to round */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 461 | }else if( n==0 ){ |
drh | 84422db | 2019-05-30 13:47:10 | [diff] [blame] | 462 | r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); |
drh | 50d654d | 2009-06-03 01:24:54 | [diff] [blame] | 463 | }else{ |
drh | ccfb50d | 2024-12-19 19:52:13 | [diff] [blame] | 464 | zBuf = sqlite3_mprintf("%!.*f",(int)n,r); |
shaneh | 147e176 | 2010-02-17 04:19:27 | [diff] [blame] | 465 | if( zBuf==0 ){ |
| 466 | sqlite3_result_error_nomem(context); |
| 467 | return; |
| 468 | } |
drh | 55700bc | 2019-06-07 22:51:13 | [diff] [blame] | 469 | sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); |
drh | 50d654d | 2009-06-03 01:24:54 | [diff] [blame] | 470 | sqlite3_free(zBuf); |
drh | 50d654d | 2009-06-03 01:24:54 | [diff] [blame] | 471 | } |
shaneh | 147e176 | 2010-02-17 04:19:27 | [diff] [blame] | 472 | sqlite3_result_double(context, r); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 473 | } |
shane | fbd60f8 | 2009-02-04 03:59:25 | [diff] [blame] | 474 | #endif |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 475 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 | [diff] [blame] | 476 | /* |
drh | f3cdcdc | 2015-04-29 16:50:28 | [diff] [blame] | 477 | ** Allocate nByte bytes of space using sqlite3Malloc(). If the |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 | [diff] [blame] | 478 | ** allocation fails, call sqlite3_result_error_nomem() to notify |
drh | 27e62db | 2009-04-02 10:16:17 | [diff] [blame] | 479 | ** the database handle that malloc() has failed and return NULL. |
| 480 | ** If nByte is larger than the maximum string or blob length, then |
| 481 | ** raise an SQLITE_TOOBIG exception and return NULL. |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 | [diff] [blame] | 482 | */ |
drh | b1a6c3c | 2008-03-20 16:30:17 | [diff] [blame] | 483 | static void *contextMalloc(sqlite3_context *context, i64 nByte){ |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 484 | char *z; |
drh | 27e62db | 2009-04-02 10:16:17 | [diff] [blame] | 485 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | ef31c6a | 2009-04-02 09:07:12 | [diff] [blame] | 486 | assert( nByte>0 ); |
drh | 27e62db | 2009-04-02 10:16:17 | [diff] [blame] | 487 | testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 488 | testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); |
| 489 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 490 | sqlite3_result_error_toobig(context); |
| 491 | z = 0; |
| 492 | }else{ |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 493 | z = sqlite3Malloc(nByte); |
drh | ef31c6a | 2009-04-02 09:07:12 | [diff] [blame] | 494 | if( !z ){ |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 495 | sqlite3_result_error_nomem(context); |
| 496 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 497 | } |
| 498 | return z; |
| 499 | } |
| 500 | |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 501 | /* |
| 502 | ** Implementation of the upper() and lower() SQL functions. |
| 503 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 504 | static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 505 | char *z1; |
| 506 | const char *z2; |
drh | 9310ef2 | 2007-04-27 17:16:20 | [diff] [blame] | 507 | int i, n; |
drh | 1d34fde | 2009-02-03 15:50:33 | [diff] [blame] | 508 | UNUSED_PARAMETER(argc); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 509 | z2 = (char*)sqlite3_value_text(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 510 | n = sqlite3_value_bytes(argv[0]); |
| 511 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 512 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 513 | if( z2 ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 | [diff] [blame] | 514 | z1 = contextMalloc(context, ((i64)n)+1); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 515 | if( z1 ){ |
drh | df901d3 | 2011-10-13 18:00:11 | [diff] [blame] | 516 | for(i=0; i<n; i++){ |
| 517 | z1[i] = (char)sqlite3Toupper(z2[i]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 518 | } |
drh | df901d3 | 2011-10-13 18:00:11 | [diff] [blame] | 519 | sqlite3_result_text(context, z1, n, sqlite3_free); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 520 | } |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 521 | } |
| 522 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 523 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | df901d3 | 2011-10-13 18:00:11 | [diff] [blame] | 524 | char *z1; |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 525 | const char *z2; |
drh | 9310ef2 | 2007-04-27 17:16:20 | [diff] [blame] | 526 | int i, n; |
drh | 1d34fde | 2009-02-03 15:50:33 | [diff] [blame] | 527 | UNUSED_PARAMETER(argc); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 528 | z2 = (char*)sqlite3_value_text(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 529 | n = sqlite3_value_bytes(argv[0]); |
| 530 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 531 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 532 | if( z2 ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 | [diff] [blame] | 533 | z1 = contextMalloc(context, ((i64)n)+1); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 534 | if( z1 ){ |
drh | df901d3 | 2011-10-13 18:00:11 | [diff] [blame] | 535 | for(i=0; i<n; i++){ |
| 536 | z1[i] = sqlite3Tolower(z2[i]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 537 | } |
drh | df901d3 | 2011-10-13 18:00:11 | [diff] [blame] | 538 | sqlite3_result_text(context, z1, n, sqlite3_free); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 539 | } |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
drh | ae6bb95 | 2009-11-11 00:24:31 | [diff] [blame] | 543 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 544 | ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented |
| 545 | ** as VDBE code so that unused argument values do not have to be computed. |
| 546 | ** However, we still need some kind of function implementation for this |
| 547 | ** routines in the function table. The noopFunc macro provides this. |
| 548 | ** noopFunc will never be called so it doesn't matter what the implementation |
| 549 | ** is. We might as well use the "version()" function as a substitute. |
drh | ae6bb95 | 2009-11-11 00:24:31 | [diff] [blame] | 550 | */ |
drh | cca9f3d | 2013-09-06 15:23:29 | [diff] [blame] | 551 | #define noopFunc versionFunc /* Substitute function - never called */ |
drh | 3212e18 | 2002-02-28 00:46:26 | [diff] [blame] | 552 | |
| 553 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 554 | ** Implementation of random(). Return a random integer. |
drh | f9ffac9 | 2002-03-02 19:00:31 | [diff] [blame] | 555 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 556 | static void randomFunc( |
| 557 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 558 | int NotUsed, |
| 559 | sqlite3_value **NotUsed2 |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 560 | ){ |
drh | 52fc849 | 2006-02-23 21:43:55 | [diff] [blame] | 561 | sqlite_int64 r; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 562 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | 2fa1868 | 2008-03-19 14:15:34 | [diff] [blame] | 563 | sqlite3_randomness(sizeof(r), &r); |
drh | 3034e3d | 2009-04-02 14:05:21 | [diff] [blame] | 564 | if( r<0 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 565 | /* We need to prevent a random number of 0x8000000000000000 |
drh | 3034e3d | 2009-04-02 14:05:21 | [diff] [blame] | 566 | ** (or -9223372036854775808) since when you do abs() of that |
| 567 | ** number of you get the same value back again. To do this |
| 568 | ** in a way that is testable, mask the sign bit off of negative |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 569 | ** values, resulting in a positive value. Then take the |
drh | 3034e3d | 2009-04-02 14:05:21 | [diff] [blame] | 570 | ** 2s complement of that positive value. The end result can |
| 571 | ** therefore be no less than -9223372036854775807. |
| 572 | */ |
drh | af8001b | 2012-02-11 19:53:24 | [diff] [blame] | 573 | r = -(r & LARGEST_INT64); |
drh | 3034e3d | 2009-04-02 14:05:21 | [diff] [blame] | 574 | } |
drh | 52fc849 | 2006-02-23 21:43:55 | [diff] [blame] | 575 | sqlite3_result_int64(context, r); |
drh | f9ffac9 | 2002-03-02 19:00:31 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 579 | ** Implementation of randomblob(N). Return a random blob |
| 580 | ** that is N bytes long. |
drh | 63cf66f | 2007-01-29 15:50:05 | [diff] [blame] | 581 | */ |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 582 | static void randomBlob( |
drh | 63cf66f | 2007-01-29 15:50:05 | [diff] [blame] | 583 | sqlite3_context *context, |
| 584 | int argc, |
| 585 | sqlite3_value **argv |
| 586 | ){ |
drh | 3cb7920 | 2019-01-18 14:53:15 | [diff] [blame] | 587 | sqlite3_int64 n; |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 588 | unsigned char *p; |
drh | 63cf66f | 2007-01-29 15:50:05 | [diff] [blame] | 589 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 590 | UNUSED_PARAMETER(argc); |
drh | 3cb7920 | 2019-01-18 14:53:15 | [diff] [blame] | 591 | n = sqlite3_value_int64(argv[0]); |
drh | 023ae03 | 2007-05-08 12:12:16 | [diff] [blame] | 592 | if( n<1 ){ |
| 593 | n = 1; |
| 594 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 595 | p = contextMalloc(context, n); |
drh | 02d8583 | 2007-05-07 19:31:15 | [diff] [blame] | 596 | if( p ){ |
drh | 2fa1868 | 2008-03-19 14:15:34 | [diff] [blame] | 597 | sqlite3_randomness(n, p); |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 598 | sqlite3_result_blob(context, (char*)p, n, sqlite3_free); |
drh | 02d8583 | 2007-05-07 19:31:15 | [diff] [blame] | 599 | } |
drh | 63cf66f | 2007-01-29 15:50:05 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
drh | 6ed41ad | 2002-04-06 14:10:47 | [diff] [blame] | 603 | ** Implementation of the last_insert_rowid() SQL function. The return |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 | [diff] [blame] | 604 | ** value is the same as the sqlite3_last_insert_rowid() API function. |
drh | 6ed41ad | 2002-04-06 14:10:47 | [diff] [blame] | 605 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 606 | static void last_insert_rowid( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 607 | sqlite3_context *context, |
| 608 | int NotUsed, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 609 | sqlite3_value **NotUsed2 |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 610 | ){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 611 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 612 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | ab2f1f9 | 2010-01-11 18:26:42 | [diff] [blame] | 613 | /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a |
| 614 | ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface |
| 615 | ** function. */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 616 | sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); |
drh | 6ed41ad | 2002-04-06 14:10:47 | [diff] [blame] | 617 | } |
| 618 | |
rdc | f146a77 | 2004-02-25 22:51:06 | [diff] [blame] | 619 | /* |
drh | ab2f1f9 | 2010-01-11 18:26:42 | [diff] [blame] | 620 | ** Implementation of the changes() SQL function. |
| 621 | ** |
drh | c0bd26a | 2021-09-14 18:57:30 | [diff] [blame] | 622 | ** IMP: R-32760-32347 The changes() SQL function is a wrapper |
| 623 | ** around the sqlite3_changes64() C/C++ function and hence follows the |
| 624 | ** same rules for counting changes. |
rdc | f146a77 | 2004-02-25 22:51:06 | [diff] [blame] | 625 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 | [diff] [blame] | 626 | static void changes( |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 627 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 628 | int NotUsed, |
| 629 | sqlite3_value **NotUsed2 |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 630 | ){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 631 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 632 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
dan | 2c71887 | 2021-06-22 18:32:05 | [diff] [blame] | 633 | sqlite3_result_int64(context, sqlite3_changes64(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 | [diff] [blame] | 634 | } |
rdc | f146a77 | 2004-02-25 22:51:06 | [diff] [blame] | 635 | |
| 636 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 | [diff] [blame] | 637 | ** Implementation of the total_changes() SQL function. The return value is |
larrybr | 10496f7 | 2021-06-23 16:07:20 | [diff] [blame] | 638 | ** the same as the sqlite3_total_changes64() API function. |
rdc | f146a77 | 2004-02-25 22:51:06 | [diff] [blame] | 639 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 | [diff] [blame] | 640 | static void total_changes( |
| 641 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 642 | int NotUsed, |
| 643 | sqlite3_value **NotUsed2 |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 644 | ){ |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 645 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 646 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | c0bd26a | 2021-09-14 18:57:30 | [diff] [blame] | 647 | /* IMP: R-11217-42568 This function is a wrapper around the |
| 648 | ** sqlite3_total_changes64() C/C++ interface. */ |
larrybr | 10496f7 | 2021-06-23 16:07:20 | [diff] [blame] | 649 | sqlite3_result_int64(context, sqlite3_total_changes64(db)); |
rdc | b0c374f | 2004-02-20 22:53:38 | [diff] [blame] | 650 | } |
| 651 | |
drh | 6ed41ad | 2002-04-06 14:10:47 | [diff] [blame] | 652 | /* |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 653 | ** A structure defining how to do GLOB-style comparisons. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 | [diff] [blame] | 654 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 655 | struct compareInfo { |
drh | 07e8347 | 2016-01-11 03:48:18 | [diff] [blame] | 656 | u8 matchAll; /* "*" or "%" */ |
| 657 | u8 matchOne; /* "?" or "_" */ |
| 658 | u8 matchSet; /* "[" or 0 */ |
| 659 | u8 noCase; /* true to ignore case differences */ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 | [diff] [blame] | 660 | }; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 661 | |
drh | b9175ae | 2007-12-07 18:39:04 | [diff] [blame] | 662 | /* |
| 663 | ** For LIKE and GLOB matching on EBCDIC machines, assume that every |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 664 | ** character is exactly one byte in size. Also, provide the Utf8Read() |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 665 | ** macro for fast reading of the next character in the common case where |
| 666 | ** the next character is ASCII. |
drh | b9175ae | 2007-12-07 18:39:04 | [diff] [blame] | 667 | */ |
| 668 | #if defined(SQLITE_EBCDIC) |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 669 | # define sqlite3Utf8Read(A) (*((*A)++)) |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 670 | # define Utf8Read(A) (*(A++)) |
drh | b9175ae | 2007-12-07 18:39:04 | [diff] [blame] | 671 | #else |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 672 | # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) |
drh | b9175ae | 2007-12-07 18:39:04 | [diff] [blame] | 673 | #endif |
| 674 | |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 675 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 676 | /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 677 | ** case. Thus 'a' LIKE 'A' would be true. */ |
| 678 | static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; |
| 679 | /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator |
| 680 | ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 681 | static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 | [diff] [blame] | 682 | |
| 683 | /* |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 684 | ** Possible error returns from patternMatch() |
| 685 | */ |
| 686 | #define SQLITE_MATCH 0 |
| 687 | #define SQLITE_NOMATCH 1 |
| 688 | #define SQLITE_NOWILDCARDMATCH 2 |
| 689 | |
| 690 | /* |
| 691 | ** Compare two UTF-8 strings for equality where the first string is |
| 692 | ** a GLOB or LIKE expression. Return values: |
| 693 | ** |
| 694 | ** SQLITE_MATCH: Match |
| 695 | ** SQLITE_NOMATCH: No match |
| 696 | ** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards. |
drh | 0ac6589 | 2002-04-20 14:24:41 | [diff] [blame] | 697 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 698 | ** Globbing rules: |
drh | 0ac6589 | 2002-04-20 14:24:41 | [diff] [blame] | 699 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 700 | ** '*' Matches any sequence of zero or more characters. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 | [diff] [blame] | 701 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 702 | ** '?' Matches exactly one character. |
| 703 | ** |
| 704 | ** [...] Matches one character from the enclosed list of |
| 705 | ** characters. |
| 706 | ** |
| 707 | ** [^...] Matches one character not in the enclosed list. |
| 708 | ** |
| 709 | ** With the [...] and [^...] matching, a ']' character can be included |
| 710 | ** in the list by making it the first character after '[' or '^'. A |
| 711 | ** range of characters can be specified using '-'. Example: |
| 712 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 713 | ** it the last character in the list. |
| 714 | ** |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 715 | ** Like matching rules: |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 716 | ** |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 717 | ** '%' Matches any sequence of zero or more characters |
| 718 | ** |
| 719 | *** '_' Matches any one character |
| 720 | ** |
| 721 | ** Ec Where E is the "esc" character and c is any other |
| 722 | ** character, including '%', '_', and esc, match exactly c. |
| 723 | ** |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 724 | ** The comments within this routine usually assume glob matching. |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 725 | ** |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 726 | ** This routine is usually quick, but can be N**2 in the worst case. |
drh | 0ac6589 | 2002-04-20 14:24:41 | [diff] [blame] | 727 | */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 | [diff] [blame] | 728 | static int patternCompare( |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 729 | const u8 *zPattern, /* The glob pattern */ |
| 730 | const u8 *zString, /* The string to compare against the glob */ |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 | [diff] [blame] | 731 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 732 | u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 733 | ){ |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 734 | u32 c, c2; /* Next pattern and input string chars */ |
| 735 | u32 matchOne = pInfo->matchOne; /* "?" or "_" */ |
| 736 | u32 matchAll = pInfo->matchAll; /* "*" or "%" */ |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 737 | u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ |
| 738 | const u8 *zEscaped = 0; /* One past the last escaped input char */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 739 | |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 740 | while( (c = Utf8Read(zPattern))!=0 ){ |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 741 | if( c==matchAll ){ /* Match "*" */ |
| 742 | /* Skip over multiple "*" characters in the pattern. If there |
| 743 | ** are also "?" characters, skip those as well, but consume a |
| 744 | ** single character of the input string for each "?" skipped */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 745 | while( (c=Utf8Read(zPattern)) == matchAll |
drh | 3394169 | 2021-02-15 17:02:01 | [diff] [blame] | 746 | || (c == matchOne && matchOne!=0) ){ |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 747 | if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 748 | return SQLITE_NOWILDCARDMATCH; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 749 | } |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 | [diff] [blame] | 750 | } |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 751 | if( c==0 ){ |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 752 | return SQLITE_MATCH; /* "*" at the end of the pattern matches */ |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 753 | }else if( c==matchOther ){ |
drh | 07e8347 | 2016-01-11 03:48:18 | [diff] [blame] | 754 | if( pInfo->matchSet==0 ){ |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 755 | c = sqlite3Utf8Read(&zPattern); |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 756 | if( c==0 ) return SQLITE_NOWILDCARDMATCH; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 757 | }else{ |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 758 | /* "[...]" immediately follows the "*". We have to do a slow |
| 759 | ** recursive search in this case, but it is an unusual case. */ |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 760 | assert( matchOther<0x80 ); /* '[' is a single-byte character */ |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 761 | while( *zString ){ |
| 762 | int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther); |
| 763 | if( bMatch!=SQLITE_NOMATCH ) return bMatch; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 764 | SQLITE_SKIP_UTF8(zString); |
| 765 | } |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 766 | return SQLITE_NOWILDCARDMATCH; |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 767 | } |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 768 | } |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 769 | |
| 770 | /* At this point variable c contains the first character of the |
| 771 | ** pattern string past the "*". Search in the input string for the |
dan | 1a4a737 | 2016-12-01 17:34:59 | [diff] [blame] | 772 | ** first matching character and recursively continue the match from |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 773 | ** that point. |
| 774 | ** |
| 775 | ** For a case-insensitive search, set variable cx to be the same as |
| 776 | ** c but in the other case and search the input string for either |
| 777 | ** c or cx. |
| 778 | */ |
dan | 2bc4a6c | 2022-10-14 15:10:36 | [diff] [blame] | 779 | if( c<0x80 ){ |
drh | eba21f9 | 2017-10-30 18:49:11 | [diff] [blame] | 780 | char zStop[3]; |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 781 | int bMatch; |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 782 | if( noCase ){ |
drh | eba21f9 | 2017-10-30 18:49:11 | [diff] [blame] | 783 | zStop[0] = sqlite3Toupper(c); |
| 784 | zStop[1] = sqlite3Tolower(c); |
| 785 | zStop[2] = 0; |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 786 | }else{ |
drh | eba21f9 | 2017-10-30 18:49:11 | [diff] [blame] | 787 | zStop[0] = c; |
| 788 | zStop[1] = 0; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 789 | } |
drh | eba21f9 | 2017-10-30 18:49:11 | [diff] [blame] | 790 | while(1){ |
| 791 | zString += strcspn((const char*)zString, zStop); |
| 792 | if( zString[0]==0 ) break; |
| 793 | zString++; |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 794 | bMatch = patternCompare(zPattern,zString,pInfo,matchOther); |
| 795 | if( bMatch!=SQLITE_NOMATCH ) return bMatch; |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 796 | } |
| 797 | }else{ |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 798 | int bMatch; |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 799 | while( (c2 = Utf8Read(zString))!=0 ){ |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 800 | if( c2!=c ) continue; |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 801 | bMatch = patternCompare(zPattern,zString,pInfo,matchOther); |
| 802 | if( bMatch!=SQLITE_NOMATCH ) return bMatch; |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 803 | } |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 804 | } |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 805 | return SQLITE_NOWILDCARDMATCH; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 806 | } |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 807 | if( c==matchOther ){ |
drh | 07e8347 | 2016-01-11 03:48:18 | [diff] [blame] | 808 | if( pInfo->matchSet==0 ){ |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 809 | c = sqlite3Utf8Read(&zPattern); |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 810 | if( c==0 ) return SQLITE_NOMATCH; |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 811 | zEscaped = zPattern; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 812 | }else{ |
| 813 | u32 prior_c = 0; |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 814 | int seen = 0; |
| 815 | int invert = 0; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 816 | c = sqlite3Utf8Read(&zString); |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 817 | if( c==0 ) return SQLITE_NOMATCH; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 818 | c2 = sqlite3Utf8Read(&zPattern); |
| 819 | if( c2=='^' ){ |
| 820 | invert = 1; |
| 821 | c2 = sqlite3Utf8Read(&zPattern); |
| 822 | } |
| 823 | if( c2==']' ){ |
| 824 | if( c==']' ) seen = 1; |
| 825 | c2 = sqlite3Utf8Read(&zPattern); |
| 826 | } |
| 827 | while( c2 && c2!=']' ){ |
| 828 | if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 829 | c2 = sqlite3Utf8Read(&zPattern); |
| 830 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 831 | prior_c = 0; |
| 832 | }else{ |
| 833 | if( c==c2 ){ |
| 834 | seen = 1; |
| 835 | } |
| 836 | prior_c = c2; |
| 837 | } |
| 838 | c2 = sqlite3Utf8Read(&zPattern); |
| 839 | } |
| 840 | if( c2==0 || (seen ^ invert)==0 ){ |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 841 | return SQLITE_NOMATCH; |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 842 | } |
| 843 | continue; |
| 844 | } |
| 845 | } |
drh | b087048 | 2015-06-17 13:20:54 | [diff] [blame] | 846 | c2 = Utf8Read(zString); |
drh | 88b3322 | 2014-09-25 03:51:37 | [diff] [blame] | 847 | if( c==c2 ) continue; |
drh | c80937a | 2016-06-06 01:48:14 | [diff] [blame] | 848 | if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){ |
drh | 9fdfdc8 | 2014-09-25 11:08:57 | [diff] [blame] | 849 | continue; |
| 850 | } |
| 851 | if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 852 | return SQLITE_NOMATCH; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 853 | } |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 854 | return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH; |
drh | 0ac6589 | 2002-04-20 14:24:41 | [diff] [blame] | 855 | } |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 856 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 857 | /* |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 858 | ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and |
| 859 | ** non-zero if there is no match. |
drh | 56282a5 | 2013-04-10 16:13:38 | [diff] [blame] | 860 | */ |
| 861 | int sqlite3_strglob(const char *zGlobPattern, const char *zString){ |
drh | 42dddb9 | 2022-10-25 13:44:18 | [diff] [blame] | 862 | if( zString==0 ){ |
| 863 | return zGlobPattern!=0; |
| 864 | }else if( zGlobPattern==0 ){ |
| 865 | return 1; |
| 866 | }else { |
| 867 | return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); |
| 868 | } |
drh | 56282a5 | 2013-04-10 16:13:38 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
drh | 698a01c | 2016-12-01 18:49:40 | [diff] [blame] | 872 | ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for |
| 873 | ** a miss - like strcmp(). |
drh | 8b4a94a | 2015-11-24 21:23:59 | [diff] [blame] | 874 | */ |
| 875 | int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ |
drh | 42dddb9 | 2022-10-25 13:44:18 | [diff] [blame] | 876 | if( zStr==0 ){ |
| 877 | return zPattern!=0; |
| 878 | }else if( zPattern==0 ){ |
| 879 | return 1; |
| 880 | }else{ |
| 881 | return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); |
| 882 | } |
drh | 8b4a94a | 2015-11-24 21:23:59 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 886 | ** Count the number of times that the LIKE operator (or GLOB which is |
| 887 | ** just a variation of LIKE) gets called. This is used for testing |
| 888 | ** only. |
| 889 | */ |
| 890 | #ifdef SQLITE_TEST |
| 891 | int sqlite3_like_count = 0; |
| 892 | #endif |
| 893 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 | [diff] [blame] | 894 | |
| 895 | /* |
| 896 | ** Implementation of the like() SQL function. This function implements |
larrybr | 55be216 | 2023-06-07 17:03:22 | [diff] [blame] | 897 | ** the built-in LIKE operator. The first argument to the function is the |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 | [diff] [blame] | 898 | ** pattern and the second argument is the string. So, the SQL statements: |
| 899 | ** |
| 900 | ** A LIKE B |
| 901 | ** |
| 902 | ** is implemented as like(B,A). |
| 903 | ** |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 904 | ** This same function (with a different compareInfo structure) computes |
| 905 | ** the GLOB operator. |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 | [diff] [blame] | 906 | */ |
| 907 | static void likeFunc( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 908 | sqlite3_context *context, |
| 909 | int argc, |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 | [diff] [blame] | 910 | sqlite3_value **argv |
| 911 | ){ |
drh | beb818d | 2007-05-08 15:34:47 | [diff] [blame] | 912 | const unsigned char *zA, *zB; |
drh | 07e8347 | 2016-01-11 03:48:18 | [diff] [blame] | 913 | u32 escape; |
drh | 27e62db | 2009-04-02 10:16:17 | [diff] [blame] | 914 | int nPat; |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 915 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | 07e8347 | 2016-01-11 03:48:18 | [diff] [blame] | 916 | struct compareInfo *pInfo = sqlite3_user_data(context); |
drh | 589c787 | 2020-03-19 18:13:28 | [diff] [blame] | 917 | struct compareInfo backupInfo; |
drh | beb818d | 2007-05-08 15:34:47 | [diff] [blame] | 918 | |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 919 | #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 920 | if( sqlite3_value_type(argv[0])==SQLITE_BLOB |
| 921 | || sqlite3_value_type(argv[1])==SQLITE_BLOB |
| 922 | ){ |
| 923 | #ifdef SQLITE_TEST |
| 924 | sqlite3_like_count++; |
| 925 | #endif |
| 926 | sqlite3_result_int(context, 0); |
| 927 | return; |
| 928 | } |
| 929 | #endif |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 930 | |
drh | beb818d | 2007-05-08 15:34:47 | [diff] [blame] | 931 | /* Limit the length of the LIKE or GLOB pattern to avoid problems |
| 932 | ** of deep recursion and N*N behavior in patternCompare(). |
| 933 | */ |
drh | 27e62db | 2009-04-02 10:16:17 | [diff] [blame] | 934 | nPat = sqlite3_value_bytes(argv[0]); |
| 935 | testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); |
| 936 | testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); |
| 937 | if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ |
drh | beb818d | 2007-05-08 15:34:47 | [diff] [blame] | 938 | sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); |
| 939 | return; |
| 940 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 | [diff] [blame] | 941 | if( argc==3 ){ |
| 942 | /* The escape character string must consist of a single UTF-8 character. |
| 943 | ** Otherwise, return an error. |
| 944 | */ |
| 945 | const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 946 | if( zEsc==0 ) return; |
drh | ee85813 | 2007-05-08 20:37:38 | [diff] [blame] | 947 | if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 948 | sqlite3_result_error(context, |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 | [diff] [blame] | 949 | "ESCAPE expression must be a single character", -1); |
| 950 | return; |
| 951 | } |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 952 | escape = sqlite3Utf8Read(&zEsc); |
drh | 589c787 | 2020-03-19 18:13:28 | [diff] [blame] | 953 | if( escape==pInfo->matchAll || escape==pInfo->matchOne ){ |
| 954 | memcpy(&backupInfo, pInfo, sizeof(backupInfo)); |
| 955 | pInfo = &backupInfo; |
| 956 | if( escape==pInfo->matchAll ) pInfo->matchAll = 0; |
| 957 | if( escape==pInfo->matchOne ) pInfo->matchOne = 0; |
| 958 | } |
drh | 07e8347 | 2016-01-11 03:48:18 | [diff] [blame] | 959 | }else{ |
| 960 | escape = pInfo->matchSet; |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 | [diff] [blame] | 961 | } |
drh | cf83323 | 2019-04-30 11:54:36 | [diff] [blame] | 962 | zB = sqlite3_value_text(argv[0]); |
| 963 | zA = sqlite3_value_text(argv[1]); |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 | [diff] [blame] | 964 | if( zA && zB ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 965 | #ifdef SQLITE_TEST |
| 966 | sqlite3_like_count++; |
| 967 | #endif |
drh | f49759b | 2017-08-25 19:51:51 | [diff] [blame] | 968 | sqlite3_result_int(context, |
| 969 | patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 | [diff] [blame] | 970 | } |
drh | 8912d10 | 2002-05-26 21:34:58 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* |
| 974 | ** Implementation of the NULLIF(x,y) function. The result is the first |
| 975 | ** argument if the arguments are different. The result is NULL if the |
| 976 | ** arguments are equal to each other. |
| 977 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 978 | static void nullifFunc( |
| 979 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 980 | int NotUsed, |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 981 | sqlite3_value **argv |
| 982 | ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 983 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 984 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 985 | if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ |
drh | f447950 | 2004-05-27 03:12:53 | [diff] [blame] | 986 | sqlite3_result_value(context, argv[0]); |
drh | 8912d10 | 2002-05-26 21:34:58 | [diff] [blame] | 987 | } |
drh | 0ac6589 | 2002-04-20 14:24:41 | [diff] [blame] | 988 | } |
| 989 | |
drh | 647cb0e | 2002-11-04 19:32:25 | [diff] [blame] | 990 | /* |
drh | 47baebc | 2009-08-14 16:01:24 | [diff] [blame] | 991 | ** Implementation of the sqlite_version() function. The result is the version |
drh | 647cb0e | 2002-11-04 19:32:25 | [diff] [blame] | 992 | ** of the SQLite library that is running. |
| 993 | */ |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 994 | static void versionFunc( |
| 995 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 996 | int NotUsed, |
| 997 | sqlite3_value **NotUsed2 |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 998 | ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 999 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | ab2f1f9 | 2010-01-11 18:26:42 | [diff] [blame] | 1000 | /* IMP: R-48699-48617 This function is an SQL wrapper around the |
| 1001 | ** sqlite3_libversion() C-interface. */ |
| 1002 | sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); |
drh | 647cb0e | 2002-11-04 19:32:25 | [diff] [blame] | 1003 | } |
| 1004 | |
drh | 47baebc | 2009-08-14 16:01:24 | [diff] [blame] | 1005 | /* |
| 1006 | ** Implementation of the sqlite_source_id() function. The result is a string |
| 1007 | ** that identifies the particular version of the source code used to build |
| 1008 | ** SQLite. |
| 1009 | */ |
| 1010 | static void sourceidFunc( |
| 1011 | sqlite3_context *context, |
| 1012 | int NotUsed, |
| 1013 | sqlite3_value **NotUsed2 |
| 1014 | ){ |
| 1015 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | ab2f1f9 | 2010-01-11 18:26:42 | [diff] [blame] | 1016 | /* IMP: R-24470-31136 This function is an SQL wrapper around the |
| 1017 | ** sqlite3_sourceid() C interface. */ |
| 1018 | sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); |
drh | 47baebc | 2009-08-14 16:01:24 | [diff] [blame] | 1019 | } |
| 1020 | |
shaneh | bdea6d1 | 2010-02-23 04:19:54 | [diff] [blame] | 1021 | /* |
drh | 3ca84ef | 2011-04-25 18:03:10 | [diff] [blame] | 1022 | ** Implementation of the sqlite_log() function. This is a wrapper around |
| 1023 | ** sqlite3_log(). The return value is NULL. The function exists purely for |
| 1024 | ** its side-effects. |
| 1025 | */ |
drh | 840561f | 2011-04-27 18:08:42 | [diff] [blame] | 1026 | static void errlogFunc( |
drh | 3ca84ef | 2011-04-25 18:03:10 | [diff] [blame] | 1027 | sqlite3_context *context, |
| 1028 | int argc, |
| 1029 | sqlite3_value **argv |
| 1030 | ){ |
| 1031 | UNUSED_PARAMETER(argc); |
| 1032 | UNUSED_PARAMETER(context); |
| 1033 | sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1])); |
| 1034 | } |
| 1035 | |
| 1036 | /* |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1037 | ** Implementation of the sqlite_compileoption_used() function. |
| 1038 | ** The result is an integer that identifies if the compiler option |
| 1039 | ** was used to build SQLite. |
shaneh | bdea6d1 | 2010-02-23 04:19:54 | [diff] [blame] | 1040 | */ |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1041 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 1042 | static void compileoptionusedFunc( |
shaneh | bdea6d1 | 2010-02-23 04:19:54 | [diff] [blame] | 1043 | sqlite3_context *context, |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1044 | int argc, |
| 1045 | sqlite3_value **argv |
shaneh | bdea6d1 | 2010-02-23 04:19:54 | [diff] [blame] | 1046 | ){ |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1047 | const char *zOptName; |
| 1048 | assert( argc==1 ); |
| 1049 | UNUSED_PARAMETER(argc); |
drh | a3e414c | 2010-08-03 18:06:25 | [diff] [blame] | 1050 | /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL |
| 1051 | ** function is a wrapper around the sqlite3_compileoption_used() C/C++ |
| 1052 | ** function. |
| 1053 | */ |
drh | 264a2d4 | 2010-02-25 15:28:41 | [diff] [blame] | 1054 | if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1055 | sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1056 | } |
shaneh | bdea6d1 | 2010-02-23 04:19:54 | [diff] [blame] | 1057 | } |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1058 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 1059 | |
| 1060 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1061 | ** Implementation of the sqlite_compileoption_get() function. |
| 1062 | ** The result is a string that identifies the compiler options |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1063 | ** used to build SQLite. |
| 1064 | */ |
| 1065 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 1066 | static void compileoptiongetFunc( |
| 1067 | sqlite3_context *context, |
| 1068 | int argc, |
| 1069 | sqlite3_value **argv |
| 1070 | ){ |
| 1071 | int n; |
| 1072 | assert( argc==1 ); |
| 1073 | UNUSED_PARAMETER(argc); |
drh | a3e414c | 2010-08-03 18:06:25 | [diff] [blame] | 1074 | /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function |
| 1075 | ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. |
| 1076 | */ |
shaneh | dc97a8c | 2010-02-23 20:08:35 | [diff] [blame] | 1077 | n = sqlite3_value_int(argv[0]); |
| 1078 | sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); |
| 1079 | } |
| 1080 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
shaneh | bdea6d1 | 2010-02-23 04:19:54 | [diff] [blame] | 1081 | |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1082 | /* Array for converting from half-bytes (nybbles) into ASCII hex |
| 1083 | ** digits. */ |
| 1084 | static const char hexdigits[] = { |
| 1085 | '0', '1', '2', '3', '4', '5', '6', '7', |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1086 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1087 | }; |
danielk1977 | d641d64 | 2004-11-18 15:44:29 | [diff] [blame] | 1088 | |
drh | 4739470 | 2003-08-20 01:03:33 | [diff] [blame] | 1089 | /* |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1090 | ** Append to pStr text that is the SQL literal representation of the |
| 1091 | ** value contained in pValue. |
drh | 4739470 | 2003-08-20 01:03:33 | [diff] [blame] | 1092 | */ |
drh | b6205d4 | 2025-02-24 13:51:24 | [diff] [blame] | 1093 | void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue, int bEscape){ |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1094 | /* As currently implemented, the string must be initially empty. |
| 1095 | ** we might relax this requirement in the future, but that will |
| 1096 | ** require enhancements to the implementation. */ |
| 1097 | assert( pStr!=0 && pStr->nChar==0 ); |
| 1098 | |
| 1099 | switch( sqlite3_value_type(pValue) ){ |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 1100 | case SQLITE_FLOAT: { |
drh | 72b3fbc | 2012-06-19 03:11:25 | [diff] [blame] | 1101 | double r1, r2; |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1102 | const char *zVal; |
| 1103 | r1 = sqlite3_value_double(pValue); |
drh | f79b0bd | 2024-02-26 22:28:21 | [diff] [blame] | 1104 | sqlite3_str_appendf(pStr, "%!0.15g", r1); |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1105 | zVal = sqlite3_str_value(pStr); |
| 1106 | if( zVal ){ |
| 1107 | sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8); |
| 1108 | if( r1!=r2 ){ |
| 1109 | sqlite3_str_reset(pStr); |
drh | f79b0bd | 2024-02-26 22:28:21 | [diff] [blame] | 1110 | sqlite3_str_appendf(pStr, "%!0.20e", r1); |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1111 | } |
drh | 72b3fbc | 2012-06-19 03:11:25 | [diff] [blame] | 1112 | } |
drh | 72b3fbc | 2012-06-19 03:11:25 | [diff] [blame] | 1113 | break; |
| 1114 | } |
| 1115 | case SQLITE_INTEGER: { |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1116 | sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue)); |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 1117 | break; |
| 1118 | } |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 | [diff] [blame] | 1119 | case SQLITE_BLOB: { |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1120 | char const *zBlob = sqlite3_value_blob(pValue); |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 1121 | i64 nBlob = sqlite3_value_bytes(pValue); |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1122 | assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */ |
| 1123 | sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4); |
| 1124 | if( pStr->accError==0 ){ |
| 1125 | char *zText = pStr->zText; |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 | [diff] [blame] | 1126 | int i; |
| 1127 | for(i=0; i<nBlob; i++){ |
| 1128 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 1129 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 1130 | } |
| 1131 | zText[(nBlob*2)+2] = '\''; |
| 1132 | zText[(nBlob*2)+3] = '\0'; |
| 1133 | zText[0] = 'X'; |
| 1134 | zText[1] = '\''; |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1135 | pStr->nChar = nBlob*2 + 3; |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 | [diff] [blame] | 1136 | } |
| 1137 | break; |
| 1138 | } |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 1139 | case SQLITE_TEXT: { |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1140 | const unsigned char *zArg = sqlite3_value_text(pValue); |
drh | b6205d4 | 2025-02-24 13:51:24 | [diff] [blame] | 1141 | sqlite3_str_appendf(pStr, bEscape ? "%#Q" : "%Q", zArg); |
drh | a0df4cc | 2009-02-02 17:29:59 | [diff] [blame] | 1142 | break; |
| 1143 | } |
| 1144 | default: { |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1145 | assert( sqlite3_value_type(pValue)==SQLITE_NULL ); |
| 1146 | sqlite3_str_append(pStr, "NULL", 4); |
drh | a0df4cc | 2009-02-02 17:29:59 | [diff] [blame] | 1147 | break; |
drh | f9b596e | 2004-05-26 16:54:42 | [diff] [blame] | 1148 | } |
drh | 4739470 | 2003-08-20 01:03:33 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1152 | /* |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 1153 | ** Return true if z[] begins with N hexadecimal digits, and write |
| 1154 | ** a decoding of those digits into *pVal. Or return false if any |
| 1155 | ** one of the first N characters in z[] is not a hexadecimal digit. |
| 1156 | */ |
| 1157 | static int isNHex(const char *z, int N, u32 *pVal){ |
| 1158 | int i; |
| 1159 | int v = 0; |
| 1160 | for(i=0; i<N; i++){ |
| 1161 | if( !sqlite3Isxdigit(z[i]) ) return 0; |
| 1162 | v = (v<<4) + sqlite3HexToInt(z[i]); |
| 1163 | } |
| 1164 | *pVal = v; |
| 1165 | return 1; |
| 1166 | } |
| 1167 | |
| 1168 | /* |
| 1169 | ** Implementation of the UNISTR() function. |
| 1170 | ** |
| 1171 | ** This is intended to be a work-alike of the UNISTR() function in |
| 1172 | ** PostgreSQL. Quoting from the PG documentation (PostgreSQL 17 - |
| 1173 | ** scraped on 2025-02-22): |
| 1174 | ** |
| 1175 | ** Evaluate escaped Unicode characters in the argument. Unicode |
| 1176 | ** characters can be specified as \XXXX (4 hexadecimal digits), |
| 1177 | ** \+XXXXXX (6 hexadecimal digits), \uXXXX (4 hexadecimal digits), |
| 1178 | ** or \UXXXXXXXX (8 hexadecimal digits). To specify a backslash, |
| 1179 | ** write two backslashes. All other characters are taken literally. |
| 1180 | */ |
| 1181 | static void unistrFunc( |
| 1182 | sqlite3_context *context, |
| 1183 | int argc, |
| 1184 | sqlite3_value **argv |
| 1185 | ){ |
| 1186 | char *zOut; |
| 1187 | const char *zIn; |
| 1188 | int nIn; |
| 1189 | int i, j, n; |
| 1190 | u32 v; |
| 1191 | |
| 1192 | assert( argc==1 ); |
drh | b6205d4 | 2025-02-24 13:51:24 | [diff] [blame] | 1193 | UNUSED_PARAMETER( argc ); |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 1194 | zIn = (const char*)sqlite3_value_text(argv[0]); |
| 1195 | if( zIn==0 ) return; |
| 1196 | nIn = sqlite3_value_bytes(argv[0]); |
| 1197 | zOut = sqlite3_malloc64(nIn+1); |
| 1198 | if( zOut==0 ){ |
| 1199 | sqlite3_result_error_nomem(context); |
| 1200 | return; |
| 1201 | } |
| 1202 | i = j = 0; |
| 1203 | while( i<nIn ){ |
| 1204 | char *z = strchr(&zIn[i],'\\'); |
| 1205 | if( z==0 ){ |
| 1206 | n = nIn - i; |
| 1207 | memmove(&zOut[j], &zIn[i], n); |
| 1208 | j += n; |
| 1209 | break; |
| 1210 | } |
| 1211 | n = z - &zIn[i]; |
| 1212 | if( n>0 ){ |
| 1213 | memmove(&zOut[j], &zIn[i], n); |
| 1214 | j += n; |
| 1215 | i += n; |
| 1216 | } |
| 1217 | if( zIn[i+1]=='\\' ){ |
| 1218 | i += 2; |
| 1219 | zOut[j++] = '\\'; |
| 1220 | }else if( sqlite3Isxdigit(zIn[i+1]) ){ |
| 1221 | if( !isNHex(&zIn[i+1], 4, &v) ) goto unistr_error; |
| 1222 | i += 5; |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 1223 | j += sqlite3AppendOneUtf8Character(&zOut[j], v); |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 1224 | }else if( zIn[i+1]=='+' ){ |
| 1225 | if( !isNHex(&zIn[i+2], 6, &v) ) goto unistr_error; |
| 1226 | i += 8; |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 1227 | j += sqlite3AppendOneUtf8Character(&zOut[j], v); |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 1228 | }else if( zIn[i+1]=='u' ){ |
| 1229 | if( !isNHex(&zIn[i+2], 4, &v) ) goto unistr_error; |
| 1230 | i += 6; |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 1231 | j += sqlite3AppendOneUtf8Character(&zOut[j], v); |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 1232 | }else if( zIn[i+1]=='U' ){ |
| 1233 | if( !isNHex(&zIn[i+2], 8, &v) ) goto unistr_error; |
| 1234 | i += 10; |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 1235 | j += sqlite3AppendOneUtf8Character(&zOut[j], v); |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 1236 | }else{ |
| 1237 | goto unistr_error; |
| 1238 | } |
| 1239 | } |
| 1240 | zOut[j] = 0; |
| 1241 | sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8); |
| 1242 | return; |
| 1243 | |
| 1244 | unistr_error: |
| 1245 | sqlite3_free(zOut); |
| 1246 | sqlite3_result_error(context, "invalid Unicode escape", -1); |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1252 | ** Implementation of the QUOTE() function. |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1253 | ** |
| 1254 | ** The quote(X) function returns the text of an SQL literal which is the |
| 1255 | ** value of its argument suitable for inclusion into an SQL statement. |
| 1256 | ** Strings are surrounded by single-quotes with escapes on interior quotes |
| 1257 | ** as needed. BLOBs are encoded as hexadecimal literals. Strings with |
| 1258 | ** embedded NUL characters cannot be represented as string literals in SQL |
| 1259 | ** and hence the returned string literal is truncated prior to the first NUL. |
drh | b6205d4 | 2025-02-24 13:51:24 | [diff] [blame] | 1260 | ** |
| 1261 | ** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is |
| 1262 | ** implemented instead. The difference is that UNISTR_QUOTE() uses the |
| 1263 | ** UNISTR() function to escape control characters. |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1264 | */ |
| 1265 | static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 1266 | sqlite3_str str; |
| 1267 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1268 | assert( argc==1 ); |
| 1269 | UNUSED_PARAMETER(argc); |
| 1270 | sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | b6205d4 | 2025-02-24 13:51:24 | [diff] [blame] | 1271 | sqlite3QuoteValue(&str,argv[0],SQLITE_PTR_TO_INT(sqlite3_user_data(context))); |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1272 | sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar, |
| 1273 | SQLITE_DYNAMIC); |
dan | af7b8dc | 2022-02-12 13:37:27 | [diff] [blame] | 1274 | if( str.accError!=SQLITE_OK ){ |
| 1275 | sqlite3_result_null(context); |
| 1276 | sqlite3_result_error_code(context, str.accError); |
drh | ef95d55 | 2021-12-10 21:01:24 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | /* |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1281 | ** The unicode() function. Return the integer unicode code-point value |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1282 | ** for the first character of the input string. |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1283 | */ |
| 1284 | static void unicodeFunc( |
| 1285 | sqlite3_context *context, |
| 1286 | int argc, |
| 1287 | sqlite3_value **argv |
| 1288 | ){ |
| 1289 | const unsigned char *z = sqlite3_value_text(argv[0]); |
drh | 1d59d03 | 2013-03-01 23:40:26 | [diff] [blame] | 1290 | (void)argc; |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1291 | if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | ** The char() function takes zero or more arguments, each of which is |
| 1296 | ** an integer. It constructs a string where each character of the string |
| 1297 | ** is the unicode character for the corresponding integer argument. |
| 1298 | */ |
| 1299 | static void charFunc( |
| 1300 | sqlite3_context *context, |
| 1301 | int argc, |
| 1302 | sqlite3_value **argv |
| 1303 | ){ |
| 1304 | unsigned char *z, *zOut; |
| 1305 | int i; |
drh | f3cdcdc | 2015-04-29 16:50:28 | [diff] [blame] | 1306 | zOut = z = sqlite3_malloc64( argc*4+1 ); |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1307 | if( z==0 ){ |
| 1308 | sqlite3_result_error_nomem(context); |
| 1309 | return; |
| 1310 | } |
| 1311 | for(i=0; i<argc; i++){ |
mistachkin | c954544 | 2013-02-26 05:42:30 | [diff] [blame] | 1312 | sqlite3_int64 x; |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1313 | unsigned c; |
| 1314 | x = sqlite3_value_int64(argv[i]); |
| 1315 | if( x<0 || x>0x10ffff ) x = 0xfffd; |
| 1316 | c = (unsigned)(x & 0x1fffff); |
drh | fe7a5d1 | 2013-03-07 14:00:04 | [diff] [blame] | 1317 | if( c<0x00080 ){ |
| 1318 | *zOut++ = (u8)(c&0xFF); |
| 1319 | }else if( c<0x00800 ){ |
| 1320 | *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); |
| 1321 | *zOut++ = 0x80 + (u8)(c & 0x3F); |
| 1322 | }else if( c<0x10000 ){ |
| 1323 | *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); |
| 1324 | *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); |
| 1325 | *zOut++ = 0x80 + (u8)(c & 0x3F); |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1326 | }else{ |
drh | fe7a5d1 | 2013-03-07 14:00:04 | [diff] [blame] | 1327 | *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); |
| 1328 | *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); |
| 1329 | *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); |
| 1330 | *zOut++ = 0x80 + (u8)(c & 0x3F); |
| 1331 | } \ |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1332 | } |
drh | 61a5b6e | 2023-07-22 15:21:41 | [diff] [blame] | 1333 | *zOut = 0; |
drh | bbf483f | 2014-09-09 20:30:24 | [diff] [blame] | 1334 | sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1338 | ** The hex() function. Interpret the argument as a blob. Return |
| 1339 | ** a hexadecimal rendering as text. |
| 1340 | */ |
| 1341 | static void hexFunc( |
| 1342 | sqlite3_context *context, |
| 1343 | int argc, |
| 1344 | sqlite3_value **argv |
| 1345 | ){ |
| 1346 | int i, n; |
| 1347 | const unsigned char *pBlob; |
| 1348 | char *zHex, *z; |
| 1349 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 1350 | UNUSED_PARAMETER(argc); |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 1351 | pBlob = sqlite3_value_blob(argv[0]); |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1352 | n = sqlite3_value_bytes(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 1353 | assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ |
drh | b1a6c3c | 2008-03-20 16:30:17 | [diff] [blame] | 1354 | z = zHex = contextMalloc(context, ((i64)n)*2 + 1); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 1355 | if( zHex ){ |
| 1356 | for(i=0; i<n; i++, pBlob++){ |
| 1357 | unsigned char c = *pBlob; |
| 1358 | *(z++) = hexdigits[(c>>4)&0xf]; |
| 1359 | *(z++) = hexdigits[c&0xf]; |
| 1360 | } |
| 1361 | *z = 0; |
larrybr | 99d4397 | 2023-10-13 01:39:47 | [diff] [blame] | 1362 | sqlite3_result_text64(context, zHex, (u64)(z-zHex), |
| 1363 | sqlite3_free, SQLITE_UTF8); |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1364 | } |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1365 | } |
| 1366 | |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1367 | /* |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1368 | ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr |
| 1369 | ** contains character ch, or 0 if it does not. |
| 1370 | */ |
| 1371 | static int strContainsChar(const u8 *zStr, int nStr, u32 ch){ |
| 1372 | const u8 *zEnd = &zStr[nStr]; |
| 1373 | const u8 *z = zStr; |
| 1374 | while( z<zEnd ){ |
| 1375 | u32 tst = Utf8Read(z); |
| 1376 | if( tst==ch ) return 1; |
| 1377 | } |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | ** The unhex() function. This function may be invoked with either one or |
| 1383 | ** two arguments. In both cases the first argument is interpreted as text |
| 1384 | ** a text value containing a set of pairs of hexadecimal digits which are |
| 1385 | ** decoded and returned as a blob. |
| 1386 | ** |
| 1387 | ** If there is only a single argument, then it must consist only of an |
larrybr | 55be216 | 2023-06-07 17:03:22 | [diff] [blame] | 1388 | ** even number of hexadecimal digits. Otherwise, return NULL. |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1389 | ** |
| 1390 | ** Or, if there is a second argument, then any character that appears in |
| 1391 | ** the second argument is also allowed to appear between pairs of hexadecimal |
| 1392 | ** digits in the first argument. If any other character appears in the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1393 | ** first argument, or if one of the allowed characters appears between |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1394 | ** two hexadecimal digits that make up a single byte, NULL is returned. |
| 1395 | ** |
| 1396 | ** The following expressions are all true: |
| 1397 | ** |
| 1398 | ** unhex('ABCD') IS x'ABCD' |
| 1399 | ** unhex('AB CD') IS NULL |
| 1400 | ** unhex('AB CD', ' ') IS x'ABCD' |
| 1401 | ** unhex('A BCD', ' ') IS NULL |
dan | e3c11d5 | 2023-01-23 14:11:34 | [diff] [blame] | 1402 | */ |
| 1403 | static void unhexFunc( |
| 1404 | sqlite3_context *pCtx, |
| 1405 | int argc, |
| 1406 | sqlite3_value **argv |
| 1407 | ){ |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1408 | const u8 *zPass = (const u8*)""; |
| 1409 | int nPass = 0; |
dan | e3c11d5 | 2023-01-23 14:11:34 | [diff] [blame] | 1410 | const u8 *zHex = sqlite3_value_text(argv[0]); |
| 1411 | int nHex = sqlite3_value_bytes(argv[0]); |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1412 | #ifdef SQLITE_DEBUG |
drh | 3c7e90b | 2023-02-18 15:50:23 | [diff] [blame] | 1413 | const u8 *zEnd = zHex ? &zHex[nHex] : 0; |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1414 | #endif |
| 1415 | u8 *pBlob = 0; |
| 1416 | u8 *p = 0; |
| 1417 | |
| 1418 | assert( argc==1 || argc==2 ); |
| 1419 | if( argc==2 ){ |
| 1420 | zPass = sqlite3_value_text(argv[1]); |
| 1421 | nPass = sqlite3_value_bytes(argv[1]); |
| 1422 | } |
| 1423 | if( !zHex || !zPass ) return; |
| 1424 | |
| 1425 | p = pBlob = contextMalloc(pCtx, (nHex/2)+1); |
| 1426 | if( pBlob ){ |
| 1427 | u8 c; /* Most significant digit of next byte */ |
| 1428 | u8 d; /* Least significant digit of next byte */ |
| 1429 | |
| 1430 | while( (c = *zHex)!=0x00 ){ |
| 1431 | while( !sqlite3Isxdigit(c) ){ |
| 1432 | u32 ch = Utf8Read(zHex); |
| 1433 | assert( zHex<=zEnd ); |
| 1434 | if( !strContainsChar(zPass, nPass, ch) ) goto unhex_null; |
| 1435 | c = *zHex; |
| 1436 | if( c==0x00 ) goto unhex_done; |
dan | e3c11d5 | 2023-01-23 14:11:34 | [diff] [blame] | 1437 | } |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1438 | zHex++; |
| 1439 | assert( *zEnd==0x00 ); |
| 1440 | assert( zHex<=zEnd ); |
| 1441 | d = *(zHex++); |
| 1442 | if( !sqlite3Isxdigit(d) ) goto unhex_null; |
| 1443 | *(p++) = (sqlite3HexToInt(c)<<4) | sqlite3HexToInt(d); |
dan | e3c11d5 | 2023-01-23 14:11:34 | [diff] [blame] | 1444 | } |
| 1445 | } |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 1446 | |
| 1447 | unhex_done: |
| 1448 | sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free); |
| 1449 | return; |
| 1450 | |
| 1451 | unhex_null: |
| 1452 | sqlite3_free(pBlob); |
| 1453 | return; |
dan | e3c11d5 | 2023-01-23 14:11:34 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | /* |
drh | 8cff382 | 2007-05-02 02:08:28 | [diff] [blame] | 1458 | ** The zeroblob(N) function returns a zero-filled blob of size N bytes. |
| 1459 | */ |
| 1460 | static void zeroblobFunc( |
| 1461 | sqlite3_context *context, |
| 1462 | int argc, |
| 1463 | sqlite3_value **argv |
| 1464 | ){ |
drh | 98640a3 | 2007-06-07 19:08:32 | [diff] [blame] | 1465 | i64 n; |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 1466 | int rc; |
drh | 8cff382 | 2007-05-02 02:08:28 | [diff] [blame] | 1467 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 1468 | UNUSED_PARAMETER(argc); |
drh | 98640a3 | 2007-06-07 19:08:32 | [diff] [blame] | 1469 | n = sqlite3_value_int64(argv[0]); |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 1470 | if( n<0 ) n = 0; |
| 1471 | rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */ |
| 1472 | if( rc ){ |
| 1473 | sqlite3_result_error_code(context, rc); |
drh | 98640a3 | 2007-06-07 19:08:32 | [diff] [blame] | 1474 | } |
drh | 8cff382 | 2007-05-02 02:08:28 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | /* |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1478 | ** The replace() function. Three arguments are all strings: call |
| 1479 | ** them A, B, and C. The result is also a string which is derived |
drh | f7b5496 | 2013-05-28 12:11:54 | [diff] [blame] | 1480 | ** from A by replacing every occurrence of B with C. The match |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1481 | ** must be exact. Collating sequences are not used. |
| 1482 | */ |
| 1483 | static void replaceFunc( |
| 1484 | sqlite3_context *context, |
| 1485 | int argc, |
| 1486 | sqlite3_value **argv |
| 1487 | ){ |
| 1488 | const unsigned char *zStr; /* The input string A */ |
| 1489 | const unsigned char *zPattern; /* The pattern string B */ |
| 1490 | const unsigned char *zRep; /* The replacement string C */ |
| 1491 | unsigned char *zOut; /* The output */ |
| 1492 | int nStr; /* Size of zStr */ |
| 1493 | int nPattern; /* Size of zPattern */ |
| 1494 | int nRep; /* Size of zRep */ |
drh | 2e6400b | 2007-05-08 15:46:18 | [diff] [blame] | 1495 | i64 nOut; /* Maximum size of zOut */ |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1496 | int loopLimit; /* Last zStr[] that might match zPattern[] */ |
| 1497 | int i, j; /* Loop counters */ |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1498 | unsigned cntExpand; /* Number zOut expansions */ |
| 1499 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1500 | |
| 1501 | assert( argc==3 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 1502 | UNUSED_PARAMETER(argc); |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1503 | zStr = sqlite3_value_text(argv[0]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1504 | if( zStr==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 1505 | nStr = sqlite3_value_bytes(argv[0]); |
| 1506 | assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1507 | zPattern = sqlite3_value_text(argv[1]); |
drh | a605fe8 | 2009-02-01 18:08:40 | [diff] [blame] | 1508 | if( zPattern==0 ){ |
drh | 2333606 | 2009-02-03 13:19:12 | [diff] [blame] | 1509 | assert( sqlite3_value_type(argv[1])==SQLITE_NULL |
| 1510 | || sqlite3_context_db_handle(context)->mallocFailed ); |
drh | a605fe8 | 2009-02-01 18:08:40 | [diff] [blame] | 1511 | return; |
| 1512 | } |
| 1513 | if( zPattern[0]==0 ){ |
| 1514 | assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); |
drh | 8dca190 | 2024-01-20 13:18:22 | [diff] [blame] | 1515 | sqlite3_result_text(context, (const char*)zStr, nStr, SQLITE_TRANSIENT); |
drh | a605fe8 | 2009-02-01 18:08:40 | [diff] [blame] | 1516 | return; |
| 1517 | } |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 1518 | nPattern = sqlite3_value_bytes(argv[1]); |
| 1519 | assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1520 | zRep = sqlite3_value_text(argv[2]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1521 | if( zRep==0 ) return; |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 1522 | nRep = sqlite3_value_bytes(argv[2]); |
| 1523 | assert( zRep==sqlite3_value_text(argv[2]) ); |
drh | 2e6400b | 2007-05-08 15:46:18 | [diff] [blame] | 1524 | nOut = nStr + 1; |
| 1525 | assert( nOut<SQLITE_MAX_LENGTH ); |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 1526 | zOut = contextMalloc(context, nOut); |
drh | 2e6400b | 2007-05-08 15:46:18 | [diff] [blame] | 1527 | if( zOut==0 ){ |
| 1528 | return; |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1529 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1530 | loopLimit = nStr - nPattern; |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1531 | cntExpand = 0; |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1532 | for(i=j=0; i<=loopLimit; i++){ |
| 1533 | if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ |
| 1534 | zOut[j++] = zStr[i]; |
| 1535 | }else{ |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1536 | if( nRep>nPattern ){ |
| 1537 | nOut += nRep - nPattern; |
drh | c86d82f | 2018-02-10 02:31:30 | [diff] [blame] | 1538 | testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 1539 | testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1540 | if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 1541 | sqlite3_result_error_toobig(context); |
| 1542 | sqlite3_free(zOut); |
| 1543 | return; |
| 1544 | } |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1545 | cntExpand++; |
| 1546 | if( (cntExpand&(cntExpand-1))==0 ){ |
| 1547 | /* Grow the size of the output buffer only on substitutions |
| 1548 | ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ |
| 1549 | u8 *zOld; |
| 1550 | zOld = zOut; |
drh | d924e7b | 2020-05-17 00:26:44 | [diff] [blame] | 1551 | zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1)); |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1552 | if( zOut==0 ){ |
| 1553 | sqlite3_result_error_nomem(context); |
| 1554 | sqlite3_free(zOld); |
| 1555 | return; |
| 1556 | } |
| 1557 | } |
drh | 2e6400b | 2007-05-08 15:46:18 | [diff] [blame] | 1558 | } |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1559 | memcpy(&zOut[j], zRep, nRep); |
| 1560 | j += nRep; |
| 1561 | i += nPattern-1; |
| 1562 | } |
| 1563 | } |
drh | f313952 | 2018-02-09 23:25:14 | [diff] [blame] | 1564 | assert( j+nStr-i+1<=nOut ); |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1565 | memcpy(&zOut[j], &zStr[i], nStr-i); |
| 1566 | j += nStr - i; |
| 1567 | assert( j<=nOut ); |
| 1568 | zOut[j] = 0; |
| 1569 | sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); |
| 1570 | } |
| 1571 | |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1572 | /* |
| 1573 | ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. |
| 1574 | ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. |
| 1575 | */ |
| 1576 | static void trimFunc( |
| 1577 | sqlite3_context *context, |
| 1578 | int argc, |
| 1579 | sqlite3_value **argv |
| 1580 | ){ |
| 1581 | const unsigned char *zIn; /* Input string */ |
| 1582 | const unsigned char *zCharSet; /* Set of characters to trim */ |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1583 | unsigned int nIn; /* Number of bytes in input */ |
drh | 7209c69 | 2008-04-27 18:40:11 | [diff] [blame] | 1584 | int flags; /* 1: trimleft 2: trimright 3: trim */ |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1585 | int i; /* Loop counter */ |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1586 | unsigned int *aLen = 0; /* Length of each character in zCharSet */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 1587 | unsigned char **azChar = 0; /* Individual characters in zCharSet */ |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1588 | int nChar; /* Number of characters in zCharSet */ |
| 1589 | |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1590 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
| 1591 | return; |
| 1592 | } |
| 1593 | zIn = sqlite3_value_text(argv[0]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1594 | if( zIn==0 ) return; |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1595 | nIn = (unsigned)sqlite3_value_bytes(argv[0]); |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 1596 | assert( zIn==sqlite3_value_text(argv[0]) ); |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1597 | if( argc==1 ){ |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1598 | static const unsigned lenOne[] = { 1 }; |
danielk1977 | a4de453 | 2008-09-02 15:44:08 | [diff] [blame] | 1599 | static unsigned char * const azOne[] = { (u8*)" " }; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1600 | nChar = 1; |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1601 | aLen = (unsigned*)lenOne; |
danielk1977 | bc67da4 | 2007-12-11 04:23:19 | [diff] [blame] | 1602 | azChar = (unsigned char **)azOne; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1603 | zCharSet = 0; |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1604 | }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1605 | return; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1606 | }else{ |
| 1607 | const unsigned char *z; |
| 1608 | for(z=zCharSet, nChar=0; *z; nChar++){ |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 1609 | SQLITE_SKIP_UTF8(z); |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1610 | } |
| 1611 | if( nChar>0 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1612 | azChar = contextMalloc(context, |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1613 | ((i64)nChar)*(sizeof(char*)+sizeof(unsigned))); |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1614 | if( azChar==0 ){ |
| 1615 | return; |
| 1616 | } |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1617 | aLen = (unsigned*)&azChar[nChar]; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1618 | for(z=zCharSet, nChar=0; *z; nChar++){ |
danielk1977 | bc67da4 | 2007-12-11 04:23:19 | [diff] [blame] | 1619 | azChar[nChar] = (unsigned char *)z; |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 1620 | SQLITE_SKIP_UTF8(z); |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1621 | aLen[nChar] = (unsigned)(z - azChar[nChar]); |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1622 | } |
| 1623 | } |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1624 | } |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1625 | if( nChar>0 ){ |
shane | 1fc4129 | 2008-07-08 22:28:48 | [diff] [blame] | 1626 | flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1627 | if( flags & 1 ){ |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1628 | while( nIn>0 ){ |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1629 | unsigned int len = 0; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1630 | for(i=0; i<nChar; i++){ |
| 1631 | len = aLen[i]; |
drh | 27e62db | 2009-04-02 10:16:17 | [diff] [blame] | 1632 | if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1633 | } |
| 1634 | if( i>=nChar ) break; |
| 1635 | zIn += len; |
| 1636 | nIn -= len; |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | if( flags & 2 ){ |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1640 | while( nIn>0 ){ |
drh | 972da42 | 2021-06-15 14:34:21 | [diff] [blame] | 1641 | unsigned int len = 0; |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1642 | for(i=0; i<nChar; i++){ |
| 1643 | len = aLen[i]; |
| 1644 | if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; |
| 1645 | } |
| 1646 | if( i>=nChar ) break; |
| 1647 | nIn -= len; |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1648 | } |
| 1649 | } |
drh | d1e3a61 | 2007-04-27 21:59:52 | [diff] [blame] | 1650 | if( zCharSet ){ |
| 1651 | sqlite3_free(azChar); |
| 1652 | } |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 1653 | } |
| 1654 | sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); |
| 1655 | } |
drh | 26b6d90 | 2007-03-17 13:27:54 | [diff] [blame] | 1656 | |
drh | e1e67ab | 2023-08-29 15:24:41 | [diff] [blame] | 1657 | /* The core implementation of the CONCAT(...) and CONCAT_WS(SEP,...) |
| 1658 | ** functions. |
| 1659 | ** |
| 1660 | ** Return a string value that is the concatenation of all non-null |
| 1661 | ** entries in argv[]. Use zSep as the separator. |
| 1662 | */ |
| 1663 | static void concatFuncCore( |
| 1664 | sqlite3_context *context, |
| 1665 | int argc, |
| 1666 | sqlite3_value **argv, |
| 1667 | int nSep, |
| 1668 | const char *zSep |
| 1669 | ){ |
drh | 23e59b3 | 2025-06-11 00:01:42 | [diff] [blame] | 1670 | i64 j, n = 0; |
drh | e1e67ab | 2023-08-29 15:24:41 | [diff] [blame] | 1671 | int i; |
| 1672 | char *z; |
| 1673 | for(i=0; i<argc; i++){ |
| 1674 | n += sqlite3_value_bytes(argv[i]); |
| 1675 | } |
drh | f4fc2ee | 2025-02-16 10:57:25 | [diff] [blame] | 1676 | n += (argc-1)*(i64)nSep; |
drh | e1e67ab | 2023-08-29 15:24:41 | [diff] [blame] | 1677 | z = sqlite3_malloc64(n+1); |
| 1678 | if( z==0 ){ |
| 1679 | sqlite3_result_error_nomem(context); |
| 1680 | return; |
| 1681 | } |
| 1682 | j = 0; |
| 1683 | for(i=0; i<argc; i++){ |
drh | 23e59b3 | 2025-06-11 00:01:42 | [diff] [blame] | 1684 | if( sqlite3_value_type(argv[i])!=SQLITE_NULL ){ |
| 1685 | int k = sqlite3_value_bytes(argv[i]); |
drh | e1e67ab | 2023-08-29 15:24:41 | [diff] [blame] | 1686 | const char *v = (const char*)sqlite3_value_text(argv[i]); |
drh | 43d71eb | 2023-10-02 15:56:37 | [diff] [blame] | 1687 | if( v!=0 ){ |
drh | e1e67ab | 2023-08-29 15:24:41 | [diff] [blame] | 1688 | if( j>0 && nSep>0 ){ |
| 1689 | memcpy(&z[j], zSep, nSep); |
| 1690 | j += nSep; |
| 1691 | } |
| 1692 | memcpy(&z[j], v, k); |
| 1693 | j += k; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | z[j] = 0; |
| 1698 | assert( j<=n ); |
drh | 51e3f7a | 2023-10-02 17:06:28 | [diff] [blame] | 1699 | sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8); |
drh | e1e67ab | 2023-08-29 15:24:41 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | ** The CONCAT(...) function. Generate a string result that is the |
| 1704 | ** concatentation of all non-null arguments. |
| 1705 | */ |
| 1706 | static void concatFunc( |
| 1707 | sqlite3_context *context, |
| 1708 | int argc, |
| 1709 | sqlite3_value **argv |
| 1710 | ){ |
| 1711 | concatFuncCore(context, argc, argv, 0, ""); |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | ** The CONCAT_WS(separator, ...) function. |
| 1716 | ** |
| 1717 | ** Generate a string that is the concatenation of 2nd through the Nth |
| 1718 | ** argument. Use the first argument (which must be non-NULL) as the |
| 1719 | ** separator. |
| 1720 | */ |
| 1721 | static void concatwsFunc( |
| 1722 | sqlite3_context *context, |
| 1723 | int argc, |
| 1724 | sqlite3_value **argv |
| 1725 | ){ |
| 1726 | int nSep = sqlite3_value_bytes(argv[0]); |
| 1727 | const char *zSep = (const char*)sqlite3_value_text(argv[0]); |
| 1728 | if( zSep==0 ) return; |
| 1729 | concatFuncCore(context, argc-1, argv+1, nSep, zSep); |
| 1730 | } |
| 1731 | |
danielk1977 | a4de453 | 2008-09-02 15:44:08 | [diff] [blame] | 1732 | |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 1733 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 1734 | /* |
| 1735 | ** The "unknown" function is automatically substituted in place of |
| 1736 | ** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN |
drh | e030619 | 2023-05-05 14:16:31 | [diff] [blame] | 1737 | ** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used. |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 1738 | ** When the "sqlite3" command-line shell is built using this functionality, |
| 1739 | ** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries |
| 1740 | ** involving application-defined functions to be examined in a generic |
| 1741 | ** sqlite3 shell. |
| 1742 | */ |
| 1743 | static void unknownFunc( |
| 1744 | sqlite3_context *context, |
| 1745 | int argc, |
| 1746 | sqlite3_value **argv |
| 1747 | ){ |
| 1748 | /* no-op */ |
drh | 3547e49 | 2022-12-23 14:49:24 | [diff] [blame] | 1749 | (void)context; |
| 1750 | (void)argc; |
| 1751 | (void)argv; |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 1752 | } |
| 1753 | #endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/ |
| 1754 | |
| 1755 | |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 1756 | /* IMP: R-25361-16150 This function is omitted from SQLite by default. It |
| 1757 | ** is only available if the SQLITE_SOUNDEX compile-time option is used |
| 1758 | ** when SQLite is built. |
| 1759 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1760 | #ifdef SQLITE_SOUNDEX |
| 1761 | /* |
| 1762 | ** Compute the soundex encoding of a word. |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 1763 | ** |
| 1764 | ** IMP: R-59782-00072 The soundex(X) function returns a string that is the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1765 | ** soundex encoding of the string X. |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1766 | */ |
drh | 137c728 | 2007-01-29 17:58:28 | [diff] [blame] | 1767 | static void soundexFunc( |
| 1768 | sqlite3_context *context, |
| 1769 | int argc, |
| 1770 | sqlite3_value **argv |
| 1771 | ){ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1772 | char zResult[8]; |
drh | 4c755c0 | 2004-08-08 20:22:17 | [diff] [blame] | 1773 | const u8 *zIn; |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1774 | int i, j; |
| 1775 | static const unsigned char iCode[] = { |
| 1776 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1777 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1778 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1779 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1780 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 1781 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 1782 | 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, |
| 1783 | 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, |
| 1784 | }; |
| 1785 | assert( argc==1 ); |
drh | 4c755c0 | 2004-08-08 20:22:17 | [diff] [blame] | 1786 | zIn = (u8*)sqlite3_value_text(argv[0]); |
drh | bdf67e0 | 2006-08-19 11:34:01 | [diff] [blame] | 1787 | if( zIn==0 ) zIn = (u8*)""; |
drh | dc86e2b | 2009-01-24 11:30:42 | [diff] [blame] | 1788 | for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1789 | if( zIn[i] ){ |
drh | bdf67e0 | 2006-08-19 11:34:01 | [diff] [blame] | 1790 | u8 prevcode = iCode[zIn[i]&0x7f]; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 | [diff] [blame] | 1791 | zResult[0] = sqlite3Toupper(zIn[i]); |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1792 | for(j=1; j<4 && zIn[i]; i++){ |
| 1793 | int code = iCode[zIn[i]&0x7f]; |
| 1794 | if( code>0 ){ |
drh | bdf67e0 | 2006-08-19 11:34:01 | [diff] [blame] | 1795 | if( code!=prevcode ){ |
| 1796 | prevcode = code; |
| 1797 | zResult[j++] = code + '0'; |
| 1798 | } |
| 1799 | }else{ |
| 1800 | prevcode = 0; |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | while( j<4 ){ |
| 1804 | zResult[j++] = '0'; |
| 1805 | } |
| 1806 | zResult[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1807 | sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1808 | }else{ |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 1809 | /* IMP: R-64894-50321 The string "?000" is returned if the argument |
| 1810 | ** is NULL or contains no ASCII alphabetic characters. */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1811 | sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1812 | } |
| 1813 | } |
drh | 2ba3ccc | 2009-12-08 02:06:08 | [diff] [blame] | 1814 | #endif /* SQLITE_SOUNDEX */ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 1815 | |
drh | fdb83b2 | 2006-06-17 14:12:47 | [diff] [blame] | 1816 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 1817 | /* |
| 1818 | ** A function that loads a shared-library extension then returns NULL. |
| 1819 | */ |
| 1820 | static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 | [diff] [blame] | 1821 | const char *zFile = (const char *)sqlite3_value_text(argv[0]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1822 | const char *zProc; |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 1823 | sqlite3 *db = sqlite3_context_db_handle(context); |
drh | fdb83b2 | 2006-06-17 14:12:47 | [diff] [blame] | 1824 | char *zErrMsg = 0; |
| 1825 | |
drh | 191dd06 | 2016-04-21 01:30:09 | [diff] [blame] | 1826 | /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc |
drh | 1a55ded | 2016-04-20 00:30:05 | [diff] [blame] | 1827 | ** flag is set. See the sqlite3_enable_load_extension() API. |
| 1828 | */ |
drh | f602a16 | 2016-04-21 01:58:21 | [diff] [blame] | 1829 | if( (db->flags & SQLITE_LoadExtFunc)==0 ){ |
| 1830 | sqlite3_result_error(context, "not authorized", -1); |
| 1831 | return; |
| 1832 | } |
drh | 1a55ded | 2016-04-20 00:30:05 | [diff] [blame] | 1833 | |
drh | fdb83b2 | 2006-06-17 14:12:47 | [diff] [blame] | 1834 | if( argc==2 ){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 | [diff] [blame] | 1835 | zProc = (const char *)sqlite3_value_text(argv[1]); |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1836 | }else{ |
| 1837 | zProc = 0; |
drh | fdb83b2 | 2006-06-17 14:12:47 | [diff] [blame] | 1838 | } |
drh | 7a521cf | 2007-04-25 18:23:52 | [diff] [blame] | 1839 | if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ |
drh | fdb83b2 | 2006-06-17 14:12:47 | [diff] [blame] | 1840 | sqlite3_result_error(context, zErrMsg, -1); |
| 1841 | sqlite3_free(zErrMsg); |
| 1842 | } |
| 1843 | } |
| 1844 | #endif |
| 1845 | |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 | [diff] [blame] | 1846 | |
drh | 0ac6589 | 2002-04-20 14:24:41 | [diff] [blame] | 1847 | /* |
drh | d3a149e | 2002-02-24 17:12:53 | [diff] [blame] | 1848 | ** An instance of the following structure holds the context of a |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 1849 | ** sum() or avg() aggregate computation. |
| 1850 | */ |
| 1851 | typedef struct SumCtx SumCtx; |
| 1852 | struct SumCtx { |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1853 | double rSum; /* Running sum as as a double */ |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1854 | double rErr; /* Error term for Kahan-Babushka-Neumaier summation */ |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1855 | i64 iSum; /* Running sum as a signed integer */ |
drh | 8c08e86 | 2006-02-11 17:34:00 | [diff] [blame] | 1856 | i64 cnt; /* Number of elements summed */ |
drh | 12b198f | 2023-06-26 19:35:20 | [diff] [blame] | 1857 | u8 approx; /* True if any non-integer value was input to the sum */ |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1858 | u8 ovrfl; /* Integer overflow seen */ |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 1859 | }; |
| 1860 | |
| 1861 | /* |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1862 | ** Do one step of the Kahan-Babushka-Neumaier summation. |
| 1863 | ** |
| 1864 | ** https://en.wikipedia.org/wiki/Kahan_summation_algorithm |
| 1865 | ** |
| 1866 | ** Variables are marked "volatile" to defeat c89 x86 floating point |
| 1867 | ** optimizations can mess up this algorithm. |
| 1868 | */ |
| 1869 | static void kahanBabuskaNeumaierStep( |
| 1870 | volatile SumCtx *pSum, |
| 1871 | volatile double r |
| 1872 | ){ |
drh | 26cd8bc | 2023-07-06 14:45:53 | [diff] [blame] | 1873 | volatile double s = pSum->rSum; |
| 1874 | volatile double t = s + r; |
| 1875 | if( fabs(s) > fabs(r) ){ |
| 1876 | pSum->rErr += (s - t) + r; |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1877 | }else{ |
drh | 26cd8bc | 2023-07-06 14:45:53 | [diff] [blame] | 1878 | pSum->rErr += (r - t) + s; |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1879 | } |
| 1880 | pSum->rSum = t; |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | ** Add a (possibly large) integer to the running sum. |
| 1885 | */ |
| 1886 | static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){ |
drh | 4c40b7b | 2023-07-08 17:42:24 | [diff] [blame] | 1887 | if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){ |
drh | 7d0103b | 2023-07-06 20:34:06 | [diff] [blame] | 1888 | i64 iBig, iSm; |
| 1889 | iSm = iVal % 16384; |
| 1890 | iBig = iVal - iSm; |
| 1891 | kahanBabuskaNeumaierStep(pSum, iBig); |
| 1892 | kahanBabuskaNeumaierStep(pSum, iSm); |
| 1893 | }else{ |
| 1894 | kahanBabuskaNeumaierStep(pSum, (double)iVal); |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | ** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer |
| 1900 | */ |
| 1901 | static void kahanBabuskaNeumaierInit( |
drh | 7d0103b | 2023-07-06 20:34:06 | [diff] [blame] | 1902 | volatile SumCtx *p, |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1903 | i64 iVal |
| 1904 | ){ |
drh | 4c40b7b | 2023-07-08 17:42:24 | [diff] [blame] | 1905 | if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){ |
drh | 7d0103b | 2023-07-06 20:34:06 | [diff] [blame] | 1906 | i64 iSm = iVal % 16384; |
| 1907 | p->rSum = (double)(iVal - iSm); |
| 1908 | p->rErr = (double)iSm; |
| 1909 | }else{ |
| 1910 | p->rSum = (double)iVal; |
| 1911 | p->rErr = 0.0; |
| 1912 | } |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /* |
drh | a97fdd3 | 2006-01-12 22:17:50 | [diff] [blame] | 1916 | ** Routines used to compute the sum, average, and total. |
| 1917 | ** |
| 1918 | ** The SUM() function follows the (broken) SQL standard which means |
| 1919 | ** that it returns NULL if it sums over no inputs. TOTAL returns |
| 1920 | ** 0.0 in that case. In addition, TOTAL always returns a float where |
| 1921 | ** SUM might return an integer if it never encounters a floating point |
stephan | 129203b | 2025-02-27 03:23:33 | [diff] [blame] | 1922 | ** value. TOTAL never fails, but SUM might throw an exception if |
drh | c806d85 | 2006-05-11 13:25:39 | [diff] [blame] | 1923 | ** it overflows an integer. |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 1924 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 1925 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 1926 | SumCtx *p; |
drh | 3d1d95e | 2005-09-08 10:37:01 | [diff] [blame] | 1927 | int type; |
drh | 3f219f4 | 2005-09-08 19:45:57 | [diff] [blame] | 1928 | assert( argc==1 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 | [diff] [blame] | 1929 | UNUSED_PARAMETER(argc); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1930 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 29d7210 | 2006-02-09 22:13:41 | [diff] [blame] | 1931 | type = sqlite3_value_numeric_type(argv[0]); |
drh | 3d1d95e | 2005-09-08 10:37:01 | [diff] [blame] | 1932 | if( p && type!=SQLITE_NULL ){ |
drh | 739105c | 2002-05-29 23:22:23 | [diff] [blame] | 1933 | p->cnt++; |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1934 | if( p->approx==0 ){ |
| 1935 | if( type!=SQLITE_INTEGER ){ |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1936 | kahanBabuskaNeumaierInit(p, p->iSum); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1937 | p->approx = 1; |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1938 | kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1939 | }else{ |
| 1940 | i64 x = p->iSum; |
| 1941 | if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){ |
| 1942 | p->iSum = x; |
| 1943 | }else{ |
| 1944 | p->ovrfl = 1; |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1945 | kahanBabuskaNeumaierInit(p, p->iSum); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1946 | p->approx = 1; |
drh | 37fd50d | 2023-07-19 09:52:10 | [diff] [blame] | 1947 | kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0])); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1948 | } |
| 1949 | } |
drh | 29d7210 | 2006-02-09 22:13:41 | [diff] [blame] | 1950 | }else{ |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1951 | if( type==SQLITE_INTEGER ){ |
| 1952 | kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0])); |
| 1953 | }else{ |
| 1954 | p->ovrfl = 0; |
| 1955 | kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); |
| 1956 | } |
drh | 3f219f4 | 2005-09-08 19:45:57 | [diff] [blame] | 1957 | } |
drh | 739105c | 2002-05-29 23:22:23 | [diff] [blame] | 1958 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 1959 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 1960 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | c3a20c1 | 2018-05-23 20:55:37 | [diff] [blame] | 1961 | static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){ |
| 1962 | SumCtx *p; |
| 1963 | int type; |
| 1964 | assert( argc==1 ); |
| 1965 | UNUSED_PARAMETER(argc); |
| 1966 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 1967 | type = sqlite3_value_numeric_type(argv[0]); |
drh | fd4b728 | 2018-07-07 19:47:21 | [diff] [blame] | 1968 | /* p is always non-NULL because sumStep() will have been called first |
| 1969 | ** to initialize it */ |
| 1970 | if( ALWAYS(p) && type!=SQLITE_NULL ){ |
drh | a546ef2 | 2018-07-07 20:55:16 | [diff] [blame] | 1971 | assert( p->cnt>0 ); |
dan | c3a20c1 | 2018-05-23 20:55:37 | [diff] [blame] | 1972 | p->cnt--; |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1973 | if( !p->approx ){ |
drh | 802b042 | 2025-02-04 02:38:23 | [diff] [blame] | 1974 | if( sqlite3SubInt64(&p->iSum, sqlite3_value_int64(argv[0])) ){ |
| 1975 | p->ovrfl = 1; |
| 1976 | p->approx = 1; |
| 1977 | } |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1978 | }else if( type==SQLITE_INTEGER ){ |
| 1979 | i64 iVal = sqlite3_value_int64(argv[0]); |
| 1980 | if( iVal!=SMALLEST_INT64 ){ |
| 1981 | kahanBabuskaNeumaierStepInt64(p, -iVal); |
| 1982 | }else{ |
drh | 26cd8bc | 2023-07-06 14:45:53 | [diff] [blame] | 1983 | kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64); |
| 1984 | kahanBabuskaNeumaierStepInt64(p, 1); |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 1985 | } |
| 1986 | }else{ |
| 1987 | kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0])); |
dan | c3a20c1 | 2018-05-23 20:55:37 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 1991 | #else |
| 1992 | # define sumInverse 0 |
| 1993 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 1994 | static void sumFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 1995 | SumCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 | [diff] [blame] | 1996 | p = sqlite3_aggregate_context(context, 0); |
drh | c2bd913 | 2005-09-08 20:37:43 | [diff] [blame] | 1997 | if( p && p->cnt>0 ){ |
drh | 12b198f | 2023-06-26 19:35:20 | [diff] [blame] | 1998 | if( p->approx ){ |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 1999 | if( p->ovrfl ){ |
drh | 12b198f | 2023-06-26 19:35:20 | [diff] [blame] | 2000 | sqlite3_result_error(context,"integer overflow",-1); |
drh | 5ed044e | 2024-03-19 10:16:17 | [diff] [blame] | 2001 | }else if( !sqlite3IsOverflow(p->rErr) ){ |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 2002 | sqlite3_result_double(context, p->rSum+p->rErr); |
drh | bd953df | 2023-08-28 12:20:18 | [diff] [blame] | 2003 | }else{ |
| 2004 | sqlite3_result_double(context, p->rSum); |
drh | 12b198f | 2023-06-26 19:35:20 | [diff] [blame] | 2005 | } |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2006 | }else{ |
| 2007 | sqlite3_result_int64(context, p->iSum); |
drh | c2bd913 | 2005-09-08 20:37:43 | [diff] [blame] | 2008 | } |
drh | 3d1d95e | 2005-09-08 10:37:01 | [diff] [blame] | 2009 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 2010 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 2011 | static void avgFinalize(sqlite3_context *context){ |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 2012 | SumCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 | [diff] [blame] | 2013 | p = sqlite3_aggregate_context(context, 0); |
drh | 739105c | 2002-05-29 23:22:23 | [diff] [blame] | 2014 | if( p && p->cnt>0 ){ |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2015 | double r; |
| 2016 | if( p->approx ){ |
drh | 7bb5a6d | 2023-08-30 16:03:27 | [diff] [blame] | 2017 | r = p->rSum; |
drh | 5ed044e | 2024-03-19 10:16:17 | [diff] [blame] | 2018 | if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr; |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2019 | }else{ |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 2020 | r = (double)(p->iSum); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2021 | } |
| 2022 | sqlite3_result_double(context, r/(double)p->cnt); |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 2023 | } |
| 2024 | } |
drh | a97fdd3 | 2006-01-12 22:17:50 | [diff] [blame] | 2025 | static void totalFinalize(sqlite3_context *context){ |
| 2026 | SumCtx *p; |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2027 | double r = 0.0; |
drh | a97fdd3 | 2006-01-12 22:17:50 | [diff] [blame] | 2028 | p = sqlite3_aggregate_context(context, 0); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2029 | if( p ){ |
| 2030 | if( p->approx ){ |
drh | 7bb5a6d | 2023-08-30 16:03:27 | [diff] [blame] | 2031 | r = p->rSum; |
drh | 5ed044e | 2024-03-19 10:16:17 | [diff] [blame] | 2032 | if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr; |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2033 | }else{ |
drh | 45d7562 | 2023-07-06 13:19:10 | [diff] [blame] | 2034 | r = (double)(p->iSum); |
drh | 48114d0 | 2023-06-30 14:01:09 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | sqlite3_result_double(context, r); |
drh | a97fdd3 | 2006-01-12 22:17:50 | [diff] [blame] | 2038 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 2039 | |
| 2040 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2041 | ** The following structure keeps track of state information for the |
| 2042 | ** count() aggregate function. |
| 2043 | */ |
| 2044 | typedef struct CountCtx CountCtx; |
| 2045 | struct CountCtx { |
drh | fc6ad39 | 2006-02-09 13:38:19 | [diff] [blame] | 2046 | i64 n; |
dan | 7262ca9 | 2018-07-02 12:07:32 | [diff] [blame] | 2047 | #ifdef SQLITE_DEBUG |
| 2048 | int bInverse; /* True if xInverse() ever called */ |
| 2049 | #endif |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2050 | }; |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 2051 | |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2052 | /* |
| 2053 | ** Routines to implement the count() aggregate function. |
| 2054 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 2055 | static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2056 | CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 2057 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 | [diff] [blame] | 2058 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2059 | p->n++; |
| 2060 | } |
drh | 2e79c3d | 2009-04-08 23:04:14 | [diff] [blame] | 2061 | |
drh | d3264c7 | 2009-04-15 13:39:47 | [diff] [blame] | 2062 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 2e79c3d | 2009-04-08 23:04:14 | [diff] [blame] | 2063 | /* The sqlite3_aggregate_count() function is deprecated. But just to make |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2064 | ** sure it still operates correctly, verify that its count agrees with our |
drh | 2e79c3d | 2009-04-08 23:04:14 | [diff] [blame] | 2065 | ** internal count when using count(*) and when the total count can be |
| 2066 | ** expressed as a 32-bit integer. */ |
dan | 7262ca9 | 2018-07-02 12:07:32 | [diff] [blame] | 2067 | assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse |
drh | 2e79c3d | 2009-04-08 23:04:14 | [diff] [blame] | 2068 | || p->n==sqlite3_aggregate_count(context) ); |
drh | d3264c7 | 2009-04-15 13:39:47 | [diff] [blame] | 2069 | #endif |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2070 | } |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 | [diff] [blame] | 2071 | static void countFinalize(sqlite3_context *context){ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2072 | CountCtx *p; |
drh | abfcea2 | 2005-09-06 20:36:48 | [diff] [blame] | 2073 | p = sqlite3_aggregate_context(context, 0); |
drh | fc6ad39 | 2006-02-09 13:38:19 | [diff] [blame] | 2074 | sqlite3_result_int64(context, p ? p->n : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2075 | } |
dan | 7262ca9 | 2018-07-02 12:07:32 | [diff] [blame] | 2076 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2077 | static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){ |
| 2078 | CountCtx *p; |
| 2079 | p = sqlite3_aggregate_context(ctx, sizeof(*p)); |
drh | fd4b728 | 2018-07-07 19:47:21 | [diff] [blame] | 2080 | /* p is always non-NULL since countStep() will have been called first */ |
| 2081 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){ |
dan | 7262ca9 | 2018-07-02 12:07:32 | [diff] [blame] | 2082 | p->n--; |
| 2083 | #ifdef SQLITE_DEBUG |
| 2084 | p->bInverse = 1; |
| 2085 | #endif |
| 2086 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2087 | } |
dan | 6b4b882 | 2018-07-02 15:03:50 | [diff] [blame] | 2088 | #else |
| 2089 | # define countInverse 0 |
| 2090 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2091 | |
| 2092 | /* |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2093 | ** Routines to implement min() and max() aggregate functions. |
| 2094 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 2095 | static void minmaxStep( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2096 | sqlite3_context *context, |
| 2097 | int NotUsed, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 2098 | sqlite3_value **argv |
| 2099 | ){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 | [diff] [blame] | 2100 | Mem *pArg = (Mem *)argv[0]; |
drh | 9eb516c | 2004-07-18 20:52:32 | [diff] [blame] | 2101 | Mem *pBest; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 | [diff] [blame] | 2102 | UNUSED_PARAMETER(NotUsed); |
drh | 9eb516c | 2004-07-18 20:52:32 | [diff] [blame] | 2103 | |
drh | 9eb516c | 2004-07-18 20:52:32 | [diff] [blame] | 2104 | pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 | [diff] [blame] | 2105 | if( !pBest ) return; |
drh | 268380c | 2004-02-25 13:47:31 | [diff] [blame] | 2106 | |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2107 | if( sqlite3_value_type(pArg)==SQLITE_NULL ){ |
drh | 94a6d99 | 2012-02-02 18:42:09 | [diff] [blame] | 2108 | if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); |
| 2109 | }else if( pBest->flags ){ |
drh | 9eb516c | 2004-07-18 20:52:32 | [diff] [blame] | 2110 | int max; |
| 2111 | int cmp; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 2112 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 | [diff] [blame] | 2113 | /* This step function is used for both the min() and max() aggregates, |
| 2114 | ** the only difference between the two being that the sense of the |
| 2115 | ** comparison is inverted. For the max() aggregate, the |
| 2116 | ** sqlite3_user_data() function returns (void *)-1. For min() it |
| 2117 | ** returns (void *)db, where db is the sqlite3* database pointer. |
| 2118 | ** Therefore the next statement sets variable 'max' to 1 for the max() |
| 2119 | ** aggregate, or 0 for min(). |
| 2120 | */ |
drh | 309b338 | 2007-03-17 17:52:42 | [diff] [blame] | 2121 | max = sqlite3_user_data(context)!=0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 | [diff] [blame] | 2122 | cmp = sqlite3MemCompare(pBest, pArg, pColl); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 | [diff] [blame] | 2123 | if( (max && cmp<0) || (!max && cmp>0) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 2124 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 7a95789 | 2012-02-02 17:35:43 | [diff] [blame] | 2125 | }else{ |
| 2126 | sqlite3SkipAccumulatorLoad(context); |
danielk1977 | 8820805 | 2004-05-25 01:13:20 | [diff] [blame] | 2127 | } |
drh | 268380c | 2004-02-25 13:47:31 | [diff] [blame] | 2128 | }else{ |
drh | 035e563 | 2014-09-16 14:16:31 | [diff] [blame] | 2129 | pBest->db = sqlite3_context_db_handle(context); |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 2130 | sqlite3VdbeMemCopy(pBest, pArg); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2131 | } |
| 2132 | } |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2133 | static void minMaxValueFinalize(sqlite3_context *context, int bValue){ |
danielk1977 | 8820805 | 2004-05-25 01:13:20 | [diff] [blame] | 2134 | sqlite3_value *pRes; |
drh | abfcea2 | 2005-09-06 20:36:48 | [diff] [blame] | 2135 | pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); |
| 2136 | if( pRes ){ |
drh | 94a6d99 | 2012-02-02 18:42:09 | [diff] [blame] | 2137 | if( pRes->flags ){ |
drh | abfcea2 | 2005-09-06 20:36:48 | [diff] [blame] | 2138 | sqlite3_result_value(context, pRes); |
| 2139 | } |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2140 | if( bValue==0 ) sqlite3VdbeMemRelease(pRes); |
drh | 0bce835 | 2002-02-28 00:41:10 | [diff] [blame] | 2141 | } |
| 2142 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 2143 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2144 | static void minMaxValue(sqlite3_context *context){ |
drh | c7bf571 | 2018-07-09 22:49:01 | [diff] [blame] | 2145 | minMaxValueFinalize(context, 1); |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2146 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 2147 | #else |
| 2148 | # define minMaxValue 0 |
| 2149 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2150 | static void minMaxFinalize(sqlite3_context *context){ |
drh | c7bf571 | 2018-07-09 22:49:01 | [diff] [blame] | 2151 | minMaxValueFinalize(context, 0); |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2152 | } |
drh | dd5baa9 | 2002-02-27 19:50:59 | [diff] [blame] | 2153 | |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2154 | /* |
| 2155 | ** group_concat(EXPR, ?SEPARATOR?) |
drh | d5e040b | 2023-10-20 20:19:30 | [diff] [blame] | 2156 | ** string_agg(EXPR, SEPARATOR) |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2157 | ** |
drh | 90fa4c7 | 2024-08-31 14:31:17 | [diff] [blame] | 2158 | ** Content is accumulated in GroupConcatCtx.str with the SEPARATOR |
| 2159 | ** coming before the EXPR value, except for the first entry which |
| 2160 | ** omits the SEPARATOR. |
| 2161 | ** |
| 2162 | ** It is tragic that the SEPARATOR goes before the EXPR string. The |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2163 | ** groupConcatInverse() implementation would have been easier if the |
| 2164 | ** SEPARATOR were appended after EXPR. And the order is undocumented, |
| 2165 | ** so we could change it, in theory. But the old behavior has been |
| 2166 | ** around for so long that we dare not, for fear of breaking something. |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2167 | */ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2168 | typedef struct { |
| 2169 | StrAccum str; /* The accumulated concatenation */ |
| 2170 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2171 | int nAccum; /* Number of strings presently concatenated */ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2172 | int nFirstSepLength; /* Used to detect separator length change */ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2173 | /* If pnSepLengths!=0, refs an array of inter-string separator lengths, |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2174 | ** stored as actually incorporated into presently accumulated result. |
| 2175 | ** (Hence, its slots in use number nAccum-1 between method calls.) |
| 2176 | ** If pnSepLengths==0, nFirstSepLength is the length used throughout. |
| 2177 | */ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2178 | int *pnSepLengths; |
| 2179 | #endif |
| 2180 | } GroupConcatCtx; |
| 2181 | |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2182 | static void groupConcatStep( |
| 2183 | sqlite3_context *context, |
| 2184 | int argc, |
| 2185 | sqlite3_value **argv |
| 2186 | ){ |
| 2187 | const char *zVal; |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2188 | GroupConcatCtx *pGCC; |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2189 | const char *zSep; |
drh | 07d3117 | 2009-02-02 21:57:05 | [diff] [blame] | 2190 | int nVal, nSep; |
| 2191 | assert( argc==1 || argc==2 ); |
| 2192 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2193 | pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC)); |
| 2194 | if( pGCC ){ |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 2195 | sqlite3 *db = sqlite3_context_db_handle(context); |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2196 | int firstTerm = pGCC->str.mxAlloc==0; |
| 2197 | pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2198 | if( argc==1 ){ |
| 2199 | if( !firstTerm ){ |
| 2200 | sqlite3_str_appendchar(&pGCC->str, 1, ','); |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2201 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2202 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2203 | else{ |
| 2204 | pGCC->nFirstSepLength = 1; |
| 2205 | } |
| 2206 | #endif |
| 2207 | }else if( !firstTerm ){ |
| 2208 | zSep = (char*)sqlite3_value_text(argv[1]); |
| 2209 | nSep = sqlite3_value_bytes(argv[1]); |
| 2210 | if( zSep ){ |
| 2211 | sqlite3_str_append(&pGCC->str, zSep, nSep); |
| 2212 | } |
| 2213 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2214 | else{ |
| 2215 | nSep = 0; |
| 2216 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2217 | if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2218 | int *pnsl = pGCC->pnSepLengths; |
| 2219 | if( pnsl == 0 ){ |
| 2220 | /* First separator length variation seen, start tracking them. */ |
| 2221 | pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int)); |
| 2222 | if( pnsl!=0 ){ |
| 2223 | int i = 0, nA = pGCC->nAccum-1; |
| 2224 | while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength; |
| 2225 | } |
| 2226 | }else{ |
| 2227 | pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int)); |
| 2228 | } |
| 2229 | if( pnsl!=0 ){ |
drh | 3dd0111 | 2021-10-01 02:45:48 | [diff] [blame] | 2230 | if( ALWAYS(pGCC->nAccum>0) ){ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2231 | pnsl[pGCC->nAccum-1] = nSep; |
| 2232 | } |
| 2233 | pGCC->pnSepLengths = pnsl; |
| 2234 | }else{ |
| 2235 | sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM); |
| 2236 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2237 | } |
| 2238 | #endif |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2239 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2240 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2241 | else{ |
drh | 3dd0111 | 2021-10-01 02:45:48 | [diff] [blame] | 2242 | pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]); |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2243 | } |
| 2244 | pGCC->nAccum += 1; |
| 2245 | #endif |
drh | 07d3117 | 2009-02-02 21:57:05 | [diff] [blame] | 2246 | zVal = (char*)sqlite3_value_text(argv[0]); |
| 2247 | nVal = sqlite3_value_bytes(argv[0]); |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2248 | if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal); |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2249 | } |
| 2250 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2251 | |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 2252 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 03854d2 | 2018-06-08 11:45:28 | [diff] [blame] | 2253 | static void groupConcatInverse( |
| 2254 | sqlite3_context *context, |
| 2255 | int argc, |
| 2256 | sqlite3_value **argv |
| 2257 | ){ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2258 | GroupConcatCtx *pGCC; |
drh | c7bf571 | 2018-07-09 22:49:01 | [diff] [blame] | 2259 | assert( argc==1 || argc==2 ); |
drh | 260ff08 | 2021-10-01 22:48:52 | [diff] [blame] | 2260 | (void)argc; /* Suppress unused parameter warning */ |
dan | 03854d2 | 2018-06-08 11:45:28 | [diff] [blame] | 2261 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2262 | pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC)); |
| 2263 | /* pGCC is always non-NULL since groupConcatStep() will have always |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2264 | ** run first to initialize it */ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2265 | if( ALWAYS(pGCC) ){ |
drh | 90fa4c7 | 2024-08-31 14:31:17 | [diff] [blame] | 2266 | int nVS; /* Number of characters to remove */ |
drh | 4fc8067 | 2021-10-12 22:55:04 | [diff] [blame] | 2267 | /* Must call sqlite3_value_text() to convert the argument into text prior |
| 2268 | ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */ |
| 2269 | (void)sqlite3_value_text(argv[0]); |
| 2270 | nVS = sqlite3_value_bytes(argv[0]); |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2271 | pGCC->nAccum -= 1; |
| 2272 | if( pGCC->pnSepLengths!=0 ){ |
| 2273 | assert(pGCC->nAccum >= 0); |
| 2274 | if( pGCC->nAccum>0 ){ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2275 | nVS += *pGCC->pnSepLengths; |
| 2276 | memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1, |
| 2277 | (pGCC->nAccum-1)*sizeof(int)); |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2278 | } |
dan | 683b0ff | 2018-07-05 18:19:29 | [diff] [blame] | 2279 | }else{ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2280 | /* If removing single accumulated string, harmlessly over-do. */ |
| 2281 | nVS += pGCC->nFirstSepLength; |
dan | 03854d2 | 2018-06-08 11:45:28 | [diff] [blame] | 2282 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2283 | if( nVS>=(int)pGCC->str.nChar ){ |
| 2284 | pGCC->str.nChar = 0; |
dan | 03854d2 | 2018-06-08 11:45:28 | [diff] [blame] | 2285 | }else{ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2286 | pGCC->str.nChar -= nVS; |
| 2287 | memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar); |
dan | 03854d2 | 2018-06-08 11:45:28 | [diff] [blame] | 2288 | } |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2289 | if( pGCC->str.nChar==0 ){ |
| 2290 | pGCC->str.mxAlloc = 0; |
| 2291 | sqlite3_free(pGCC->pnSepLengths); |
| 2292 | pGCC->pnSepLengths = 0; |
| 2293 | } |
dan | 03854d2 | 2018-06-08 11:45:28 | [diff] [blame] | 2294 | } |
| 2295 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 2296 | #else |
| 2297 | # define groupConcatInverse 0 |
| 2298 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2299 | static void groupConcatFinalize(sqlite3_context *context){ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2300 | GroupConcatCtx *pGCC |
| 2301 | = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0); |
| 2302 | if( pGCC ){ |
drh | 5bf4715 | 2021-10-03 00:12:43 | [diff] [blame] | 2303 | sqlite3ResultStrAccum(context, &pGCC->str); |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2304 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2305 | sqlite3_free(pGCC->pnSepLengths); |
| 2306 | #endif |
drh | b068969 | 2007-11-01 17:38:30 | [diff] [blame] | 2307 | } |
| 2308 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 2309 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | e2f781b | 2018-05-17 19:24:08 | [diff] [blame] | 2310 | static void groupConcatValue(sqlite3_context *context){ |
larrybr | dde13e6 | 2021-09-29 00:32:13 | [diff] [blame] | 2311 | GroupConcatCtx *pGCC |
| 2312 | = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0); |
| 2313 | if( pGCC ){ |
| 2314 | StrAccum *pAccum = &pGCC->str; |
dan | e2f781b | 2018-05-17 19:24:08 | [diff] [blame] | 2315 | if( pAccum->accError==SQLITE_TOOBIG ){ |
| 2316 | sqlite3_result_error_toobig(context); |
| 2317 | }else if( pAccum->accError==SQLITE_NOMEM ){ |
| 2318 | sqlite3_result_error_nomem(context); |
drh | e6d3c57 | 2024-05-23 23:26:04 | [diff] [blame] | 2319 | }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){ |
| 2320 | sqlite3_result_text(context, "", 1, SQLITE_STATIC); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2321 | }else{ |
dan | e2f781b | 2018-05-17 19:24:08 | [diff] [blame] | 2322 | const char *zText = sqlite3_str_value(pAccum); |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 2323 | sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT); |
dan | e2f781b | 2018-05-17 19:24:08 | [diff] [blame] | 2324 | } |
| 2325 | } |
| 2326 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 | [diff] [blame] | 2327 | #else |
| 2328 | # define groupConcatValue 0 |
| 2329 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 | [diff] [blame] | 2330 | |
drh | d3a149e | 2002-02-24 17:12:53 | [diff] [blame] | 2331 | /* |
drh | a474184 | 2010-04-25 20:58:37 | [diff] [blame] | 2332 | ** This routine does per-connection function registration. Most |
| 2333 | ** of the built-in functions above are part of the global function set. |
| 2334 | ** This routine only deals with those that are not global. |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 2335 | */ |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2336 | void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ |
drh | a474184 | 2010-04-25 20:58:37 | [diff] [blame] | 2337 | int rc = sqlite3_overload_function(db, "MATCH", 2); |
| 2338 | assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); |
| 2339 | if( rc==SQLITE_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 2340 | sqlite3OomFault(db); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 | [diff] [blame] | 2341 | } |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* |
drh | 08652b5 | 2019-05-08 17:27:18 | [diff] [blame] | 2345 | ** Re-register the built-in LIKE functions. The caseSensitive |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2346 | ** parameter determines whether or not the LIKE operator is case |
drh | 08652b5 | 2019-05-08 17:27:18 | [diff] [blame] | 2347 | ** sensitive. |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2348 | */ |
| 2349 | void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ |
drh | 00eee07 | 2023-08-15 11:58:22 | [diff] [blame] | 2350 | FuncDef *pDef; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2351 | struct compareInfo *pInfo; |
drh | ea5c040 | 2019-05-08 19:32:33 | [diff] [blame] | 2352 | int flags; |
drh | 00eee07 | 2023-08-15 11:58:22 | [diff] [blame] | 2353 | int nArg; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2354 | if( caseSensitive ){ |
| 2355 | pInfo = (struct compareInfo*)&likeInfoAlt; |
drh | ea5c040 | 2019-05-08 19:32:33 | [diff] [blame] | 2356 | flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2357 | }else{ |
| 2358 | pInfo = (struct compareInfo*)&likeInfoNorm; |
drh | ea5c040 | 2019-05-08 19:32:33 | [diff] [blame] | 2359 | flags = SQLITE_FUNC_LIKE; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2360 | } |
drh | 00eee07 | 2023-08-15 11:58:22 | [diff] [blame] | 2361 | for(nArg=2; nArg<=3; nArg++){ |
| 2362 | sqlite3CreateFunc(db, "like", nArg, SQLITE_UTF8, pInfo, likeFunc, |
| 2363 | 0, 0, 0, 0, 0); |
| 2364 | pDef = sqlite3FindFunction(db, "like", nArg, SQLITE_UTF8, 0); |
| 2365 | pDef->funcFlags |= flags; |
| 2366 | pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE; |
| 2367 | } |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | /* |
| 2371 | ** pExpr points to an expression which implements a function. If |
| 2372 | ** it is appropriate to apply the LIKE optimization to that function |
drh | 1d42ea7 | 2017-07-27 20:24:29 | [diff] [blame] | 2373 | ** then set aWc[0] through aWc[2] to the wildcard characters and the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2374 | ** escape character and then return TRUE. If the function is not a |
drh | 1d42ea7 | 2017-07-27 20:24:29 | [diff] [blame] | 2375 | ** LIKE-style function then return FALSE. |
| 2376 | ** |
| 2377 | ** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE |
| 2378 | ** operator if c is a string literal that is exactly one byte in length. |
| 2379 | ** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is |
| 2380 | ** no ESCAPE clause. |
drh | 1689707 | 2015-03-07 00:57:37 | [diff] [blame] | 2381 | ** |
| 2382 | ** *pIsNocase is set to true if uppercase and lowercase are equivalent for |
| 2383 | ** the function (default for LIKE). If the function makes the distinction |
| 2384 | ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to |
| 2385 | ** false. |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2386 | */ |
drh | d64fe2f | 2005-08-28 17:00:23 | [diff] [blame] | 2387 | int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2388 | FuncDef *pDef; |
drh | 1d42ea7 | 2017-07-27 20:24:29 | [diff] [blame] | 2389 | int nExpr; |
drh | 17988aa | 2021-01-22 20:28:30 | [diff] [blame] | 2390 | assert( pExpr!=0 ); |
| 2391 | assert( pExpr->op==TK_FUNCTION ); |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 2392 | assert( ExprUseXList(pExpr) ); |
drh | 17988aa | 2021-01-22 20:28:30 | [diff] [blame] | 2393 | if( !pExpr->x.pList ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2394 | return 0; |
| 2395 | } |
drh | 1d42ea7 | 2017-07-27 20:24:29 | [diff] [blame] | 2396 | nExpr = pExpr->x.pList->nExpr; |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 2397 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | 1d42ea7 | 2017-07-27 20:24:29 | [diff] [blame] | 2398 | pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0); |
drh | 78b5220 | 2020-01-22 23:08:19 | [diff] [blame] | 2399 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 2400 | if( pDef==0 ) return 0; |
| 2401 | #endif |
drh | d36e104 | 2013-09-06 13:10:12 | [diff] [blame] | 2402 | if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2403 | return 0; |
| 2404 | } |
| 2405 | |
| 2406 | /* The memcpy() statement assumes that the wildcard characters are |
| 2407 | ** the first three statements in the compareInfo structure. The |
| 2408 | ** asserts() that follow verify that assumption |
| 2409 | */ |
| 2410 | memcpy(aWc, pDef->pUserData, 3); |
| 2411 | assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); |
| 2412 | assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); |
| 2413 | assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); |
drh | 589c787 | 2020-03-19 18:13:28 | [diff] [blame] | 2414 | |
| 2415 | if( nExpr<3 ){ |
| 2416 | aWc[3] = 0; |
| 2417 | }else{ |
| 2418 | Expr *pEscape = pExpr->x.pList->a[2].pExpr; |
| 2419 | char *zEscape; |
| 2420 | if( pEscape->op!=TK_STRING ) return 0; |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 2421 | assert( !ExprHasProperty(pEscape, EP_IntValue) ); |
drh | 589c787 | 2020-03-19 18:13:28 | [diff] [blame] | 2422 | zEscape = pEscape->u.zToken; |
| 2423 | if( zEscape[0]==0 || zEscape[1]!=0 ) return 0; |
| 2424 | if( zEscape[0]==aWc[0] ) return 0; |
| 2425 | if( zEscape[0]==aWc[1] ) return 0; |
| 2426 | aWc[3] = zEscape[0]; |
| 2427 | } |
| 2428 | |
drh | d36e104 | 2013-09-06 13:10:12 | [diff] [blame] | 2429 | *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; |
drh | 55ef4d9 | 2005-08-14 01:20:37 | [diff] [blame] | 2430 | return 1; |
drh | dc04c58 | 2002-02-24 01:55:15 | [diff] [blame] | 2431 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 | [diff] [blame] | 2432 | |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2433 | /* Mathematical Constants */ |
| 2434 | #ifndef M_PI |
| 2435 | # define M_PI 3.141592653589793238462643383279502884 |
| 2436 | #endif |
| 2437 | #ifndef M_LN10 |
| 2438 | # define M_LN10 2.302585092994045684017991454684364208 |
| 2439 | #endif |
| 2440 | #ifndef M_LN2 |
| 2441 | # define M_LN2 0.693147180559945309417232121458176568 |
| 2442 | #endif |
| 2443 | |
| 2444 | |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2445 | /* Extra math functions that require linking with -lm |
| 2446 | */ |
| 2447 | #ifdef SQLITE_ENABLE_MATH_FUNCTIONS |
| 2448 | /* |
| 2449 | ** Implementation SQL functions: |
| 2450 | ** |
| 2451 | ** ceil(X) |
| 2452 | ** ceiling(X) |
| 2453 | ** floor(X) |
| 2454 | ** |
| 2455 | ** The sqlite3_user_data() pointer is a pointer to the libm implementation |
| 2456 | ** of the underlying C function. |
| 2457 | */ |
| 2458 | static void ceilingFunc( |
| 2459 | sqlite3_context *context, |
| 2460 | int argc, |
| 2461 | sqlite3_value **argv |
| 2462 | ){ |
| 2463 | assert( argc==1 ); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2464 | switch( sqlite3_value_numeric_type(argv[0]) ){ |
| 2465 | case SQLITE_INTEGER: { |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2466 | sqlite3_result_int64(context, sqlite3_value_int64(argv[0])); |
| 2467 | break; |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2468 | } |
| 2469 | case SQLITE_FLOAT: { |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2470 | double (*x)(double) = (double(*)(double))sqlite3_user_data(context); |
| 2471 | sqlite3_result_double(context, x(sqlite3_value_double(argv[0]))); |
| 2472 | break; |
| 2473 | } |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2474 | default: { |
| 2475 | break; |
| 2476 | } |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | /* |
drh | 4fd4a7a | 2021-01-01 01:44:06 | [diff] [blame] | 2481 | ** On some systems, ceil() and floor() are intrinsic function. You are |
drh | c2dbf35 | 2021-01-07 16:10:14 | [diff] [blame] | 2482 | ** unable to take a pointer to these functions. Hence, we here wrap them |
drh | 4fd4a7a | 2021-01-01 01:44:06 | [diff] [blame] | 2483 | ** in our own actual functions. |
| 2484 | */ |
| 2485 | static double xCeil(double x){ return ceil(x); } |
| 2486 | static double xFloor(double x){ return floor(x); } |
| 2487 | |
| 2488 | /* |
drh | 3c2688d | 2023-02-23 01:52:54 | [diff] [blame] | 2489 | ** Some systems do not have log2() and log10() in their standard math |
| 2490 | ** libraries. |
| 2491 | */ |
| 2492 | #if defined(HAVE_LOG10) && HAVE_LOG10==0 |
| 2493 | # define log10(X) (0.4342944819032517867*log(X)) |
| 2494 | #endif |
| 2495 | #if defined(HAVE_LOG2) && HAVE_LOG2==0 |
| 2496 | # define log2(X) (1.442695040888963456*log(X)) |
| 2497 | #endif |
| 2498 | |
| 2499 | |
| 2500 | /* |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2501 | ** Implementation of SQL functions: |
| 2502 | ** |
| 2503 | ** ln(X) - natural logarithm |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2504 | ** log(X) - log X base 10 |
| 2505 | ** log10(X) - log X base 10 |
| 2506 | ** log(B,X) - log X base B |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2507 | */ |
| 2508 | static void logFunc( |
| 2509 | sqlite3_context *context, |
| 2510 | int argc, |
| 2511 | sqlite3_value **argv |
| 2512 | ){ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2513 | double x, b, ans; |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2514 | assert( argc==1 || argc==2 ); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2515 | switch( sqlite3_value_numeric_type(argv[0]) ){ |
| 2516 | case SQLITE_INTEGER: |
| 2517 | case SQLITE_FLOAT: |
| 2518 | x = sqlite3_value_double(argv[0]); |
drh | 02d6f9b | 2021-01-29 16:20:16 | [diff] [blame] | 2519 | if( x<=0.0 ) return; |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2520 | break; |
| 2521 | default: |
| 2522 | return; |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2523 | } |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2524 | if( argc==2 ){ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2525 | switch( sqlite3_value_numeric_type(argv[0]) ){ |
| 2526 | case SQLITE_INTEGER: |
| 2527 | case SQLITE_FLOAT: |
drh | 02d6f9b | 2021-01-29 16:20:16 | [diff] [blame] | 2528 | b = log(x); |
| 2529 | if( b<=0.0 ) return; |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2530 | x = sqlite3_value_double(argv[1]); |
drh | 02d6f9b | 2021-01-29 16:20:16 | [diff] [blame] | 2531 | if( x<=0.0 ) return; |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2532 | break; |
| 2533 | default: |
| 2534 | return; |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2535 | } |
drh | 02d6f9b | 2021-01-29 16:20:16 | [diff] [blame] | 2536 | ans = log(x)/b; |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2537 | }else{ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2538 | switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){ |
| 2539 | case 1: |
drh | 3c1572d | 2022-11-17 14:40:33 | [diff] [blame] | 2540 | ans = log10(x); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2541 | break; |
| 2542 | case 2: |
drh | 3c1572d | 2022-11-17 14:40:33 | [diff] [blame] | 2543 | ans = log2(x); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2544 | break; |
| 2545 | default: |
drh | 3c1572d | 2022-11-17 14:40:33 | [diff] [blame] | 2546 | ans = log(x); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2547 | break; |
| 2548 | } |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2549 | } |
| 2550 | sqlite3_result_double(context, ans); |
| 2551 | } |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2552 | |
| 2553 | /* |
| 2554 | ** Functions to converts degrees to radians and radians to degrees. |
| 2555 | */ |
| 2556 | static double degToRad(double x){ return x*(M_PI/180.0); } |
| 2557 | static double radToDeg(double x){ return x*(180.0/M_PI); } |
| 2558 | |
| 2559 | /* |
| 2560 | ** Implementation of 1-argument SQL math functions: |
| 2561 | ** |
| 2562 | ** exp(X) - Compute e to the X-th power |
| 2563 | */ |
| 2564 | static void math1Func( |
| 2565 | sqlite3_context *context, |
| 2566 | int argc, |
| 2567 | sqlite3_value **argv |
| 2568 | ){ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2569 | int type0; |
| 2570 | double v0, ans; |
| 2571 | double (*x)(double); |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2572 | assert( argc==1 ); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2573 | type0 = sqlite3_value_numeric_type(argv[0]); |
| 2574 | if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; |
| 2575 | v0 = sqlite3_value_double(argv[0]); |
| 2576 | x = (double(*)(double))sqlite3_user_data(context); |
| 2577 | ans = x(v0); |
| 2578 | sqlite3_result_double(context, ans); |
| 2579 | } |
| 2580 | |
| 2581 | /* |
| 2582 | ** Implementation of 2-argument SQL math functions: |
| 2583 | ** |
| 2584 | ** power(X,Y) - Compute X to the Y-th power |
| 2585 | */ |
| 2586 | static void math2Func( |
| 2587 | sqlite3_context *context, |
| 2588 | int argc, |
| 2589 | sqlite3_value **argv |
| 2590 | ){ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2591 | int type0, type1; |
| 2592 | double v0, v1, ans; |
| 2593 | double (*x)(double,double); |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2594 | assert( argc==2 ); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2595 | type0 = sqlite3_value_numeric_type(argv[0]); |
| 2596 | if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; |
| 2597 | type1 = sqlite3_value_numeric_type(argv[1]); |
| 2598 | if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return; |
| 2599 | v0 = sqlite3_value_double(argv[0]); |
| 2600 | v1 = sqlite3_value_double(argv[1]); |
| 2601 | x = (double(*)(double,double))sqlite3_user_data(context); |
| 2602 | ans = x(v0, v1); |
| 2603 | sqlite3_result_double(context, ans); |
| 2604 | } |
| 2605 | |
| 2606 | /* |
drh | 81e5a9a | 2021-04-16 11:05:19 | [diff] [blame] | 2607 | ** Implementation of 0-argument pi() function. |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2608 | */ |
| 2609 | static void piFunc( |
| 2610 | sqlite3_context *context, |
| 2611 | int argc, |
| 2612 | sqlite3_value **argv |
| 2613 | ){ |
| 2614 | assert( argc==0 ); |
drh | 3547e49 | 2022-12-23 14:49:24 | [diff] [blame] | 2615 | (void)argv; |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2616 | sqlite3_result_double(context, M_PI); |
| 2617 | } |
| 2618 | |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2619 | #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ |
| 2620 | |
drh | 70a8ca3 | 2008-08-21 18:49:27 | [diff] [blame] | 2621 | /* |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2622 | ** Implementation of sign(X) function. |
| 2623 | */ |
| 2624 | static void signFunc( |
| 2625 | sqlite3_context *context, |
| 2626 | int argc, |
| 2627 | sqlite3_value **argv |
| 2628 | ){ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2629 | int type0; |
| 2630 | double x; |
drh | e5baf5c | 2020-12-16 14:20:45 | [diff] [blame] | 2631 | UNUSED_PARAMETER(argc); |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2632 | assert( argc==1 ); |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2633 | type0 = sqlite3_value_numeric_type(argv[0]); |
| 2634 | if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; |
| 2635 | x = sqlite3_value_double(argv[0]); |
| 2636 | sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); |
| 2637 | } |
| 2638 | |
drh | 453be55 | 2023-07-01 18:33:26 | [diff] [blame] | 2639 | #ifdef SQLITE_DEBUG |
| 2640 | /* |
| 2641 | ** Implementation of fpdecode(x,y,z) function. |
| 2642 | ** |
| 2643 | ** x is a real number that is to be decoded. y is the precision. |
drh | 74672ac | 2024-10-06 15:01:31 | [diff] [blame] | 2644 | ** z is the maximum real precision. Return a string that shows the |
| 2645 | ** results of the sqlite3FpDecode() function. |
| 2646 | ** |
| 2647 | ** Used for testing and debugging only, specifically testing and debugging |
| 2648 | ** of the sqlite3FpDecode() function. This SQL function does not appear |
| 2649 | ** in production builds. This function is not an API and is subject to |
| 2650 | ** modification or removal in future versions of SQLite. |
drh | 453be55 | 2023-07-01 18:33:26 | [diff] [blame] | 2651 | */ |
| 2652 | static void fpdecodeFunc( |
| 2653 | sqlite3_context *context, |
| 2654 | int argc, |
| 2655 | sqlite3_value **argv |
| 2656 | ){ |
| 2657 | FpDecode s; |
| 2658 | double x; |
| 2659 | int y, z; |
| 2660 | char zBuf[100]; |
| 2661 | UNUSED_PARAMETER(argc); |
| 2662 | assert( argc==3 ); |
| 2663 | x = sqlite3_value_double(argv[0]); |
| 2664 | y = sqlite3_value_int(argv[1]); |
| 2665 | z = sqlite3_value_int(argv[2]); |
drh | 34e4c6f | 2024-06-10 12:43:03 | [diff] [blame] | 2666 | if( z<=0 ) z = 1; |
drh | 453be55 | 2023-07-01 18:33:26 | [diff] [blame] | 2667 | sqlite3FpDecode(&s, x, y, z); |
| 2668 | if( s.isSpecial==2 ){ |
| 2669 | sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN"); |
| 2670 | }else{ |
| 2671 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP); |
| 2672 | } |
| 2673 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 2674 | } |
| 2675 | #endif /* SQLITE_DEBUG */ |
| 2676 | |
drh | 74672ac | 2024-10-06 15:01:31 | [diff] [blame] | 2677 | #ifdef SQLITE_DEBUG |
| 2678 | /* |
| 2679 | ** Implementation of parseuri(uri,flags) function. |
| 2680 | ** |
| 2681 | ** Required Arguments: |
| 2682 | ** "uri" The URI to parse. |
| 2683 | ** "flags" Bitmask of flags, as if to sqlite3_open_v2(). |
| 2684 | ** |
| 2685 | ** Additional arguments beyond the first two make calls to |
| 2686 | ** sqlite3_uri_key() for integers and sqlite3_uri_parameter for |
| 2687 | ** anything else. |
| 2688 | ** |
| 2689 | ** The result is a string showing the results of calling sqlite3ParseUri(). |
| 2690 | ** |
| 2691 | ** Used for testing and debugging only, specifically testing and debugging |
| 2692 | ** of the sqlite3ParseUri() function. This SQL function does not appear |
| 2693 | ** in production builds. This function is not an API and is subject to |
| 2694 | ** modification or removal in future versions of SQLite. |
| 2695 | */ |
| 2696 | static void parseuriFunc( |
| 2697 | sqlite3_context *ctx, |
| 2698 | int argc, |
| 2699 | sqlite3_value **argv |
| 2700 | ){ |
| 2701 | sqlite3_str *pResult; |
| 2702 | const char *zVfs; |
| 2703 | const char *zUri; |
| 2704 | unsigned int flgs; |
| 2705 | int rc; |
| 2706 | sqlite3_vfs *pVfs = 0; |
| 2707 | char *zFile = 0; |
| 2708 | char *zErr = 0; |
| 2709 | |
| 2710 | if( argc<2 ) return; |
| 2711 | pVfs = sqlite3_vfs_find(0); |
| 2712 | assert( pVfs ); |
| 2713 | zVfs = pVfs->zName; |
| 2714 | zUri = (const char*)sqlite3_value_text(argv[0]); |
| 2715 | if( zUri==0 ) return; |
| 2716 | flgs = (unsigned int)sqlite3_value_int(argv[1]); |
| 2717 | rc = sqlite3ParseUri(zVfs, zUri, &flgs, &pVfs, &zFile, &zErr); |
| 2718 | pResult = sqlite3_str_new(0); |
| 2719 | if( pResult ){ |
| 2720 | int i; |
| 2721 | sqlite3_str_appendf(pResult, "rc=%d", rc); |
| 2722 | sqlite3_str_appendf(pResult, ", flags=0x%x", flgs); |
| 2723 | sqlite3_str_appendf(pResult, ", vfs=%Q", pVfs ? pVfs->zName: 0); |
| 2724 | sqlite3_str_appendf(pResult, ", err=%Q", zErr); |
| 2725 | sqlite3_str_appendf(pResult, ", file=%Q", zFile); |
| 2726 | if( zFile ){ |
| 2727 | const char *z = zFile; |
| 2728 | z += sqlite3Strlen30(z)+1; |
| 2729 | while( z[0] ){ |
| 2730 | sqlite3_str_appendf(pResult, ", %Q", z); |
| 2731 | z += sqlite3Strlen30(z)+1; |
| 2732 | } |
| 2733 | for(i=2; i<argc; i++){ |
| 2734 | const char *zArg; |
| 2735 | if( sqlite3_value_type(argv[i])==SQLITE_INTEGER ){ |
| 2736 | int k = sqlite3_value_int(argv[i]); |
| 2737 | sqlite3_str_appendf(pResult, ", '%d:%q'",k,sqlite3_uri_key(zFile, k)); |
| 2738 | }else if( (zArg = (const char*)sqlite3_value_text(argv[i]))!=0 ){ |
| 2739 | sqlite3_str_appendf(pResult, ", '%q:%q'", |
| 2740 | zArg, sqlite3_uri_parameter(zFile,zArg)); |
| 2741 | }else{ |
| 2742 | sqlite3_str_appendf(pResult, ", NULL"); |
| 2743 | } |
| 2744 | } |
| 2745 | } |
| 2746 | sqlite3_result_text(ctx, sqlite3_str_finish(pResult), -1, sqlite3_free); |
| 2747 | } |
| 2748 | sqlite3_free_filename(zFile); |
| 2749 | sqlite3_free(zErr); |
| 2750 | } |
| 2751 | #endif /* SQLITE_DEBUG */ |
| 2752 | |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2753 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 2754 | ** All of the FuncDef structures in the aBuiltinFunc[] array above |
drh | 777c538 | 2008-08-21 20:21:34 | [diff] [blame] | 2755 | ** to the global function hash table. This occurs at start-time (as |
| 2756 | ** a consequence of calling sqlite3_initialize()). |
| 2757 | ** |
| 2758 | ** After this routine runs |
drh | 70a8ca3 | 2008-08-21 18:49:27 | [diff] [blame] | 2759 | */ |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2760 | void sqlite3RegisterBuiltinFunctions(void){ |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2761 | /* |
| 2762 | ** The following array holds FuncDef structures for all of the functions |
| 2763 | ** defined in this file. |
| 2764 | ** |
| 2765 | ** The array cannot be constant since changes are made to the |
| 2766 | ** FuncDef.pHash elements at start-time. The elements of this array |
| 2767 | ** are read-only after initialization is complete. |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2768 | ** |
| 2769 | ** For peak efficiency, put the most frequently used function last. |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2770 | */ |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2771 | static FuncDef aBuiltinFunc[] = { |
drh | 171c50e | 2020-01-01 15:43:30 | [diff] [blame] | 2772 | /***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/ |
drh | 3780f9a | 2021-09-17 13:07:15 | [diff] [blame] | 2773 | #if !defined(SQLITE_UNTESTABLE) |
drh | 171c50e | 2020-01-01 15:43:30 | [diff] [blame] | 2774 | TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0), |
| 2775 | TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0), |
| 2776 | TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0), |
drh | 3780f9a | 2021-09-17 13:07:15 | [diff] [blame] | 2777 | TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0), |
| 2778 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
drh | 171c50e | 2020-01-01 15:43:30 | [diff] [blame] | 2779 | /***** Regular functions *****/ |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2780 | #ifdef SQLITE_SOUNDEX |
| 2781 | FUNCTION(soundex, 1, 0, 0, soundexFunc ), |
| 2782 | #endif |
| 2783 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 64de2a5 | 2019-12-31 18:39:23 | [diff] [blame] | 2784 | SFUNCTION(load_extension, 1, 0, 0, loadExt ), |
| 2785 | SFUNCTION(load_extension, 2, 0, 0, loadExt ), |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2786 | #endif |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2787 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 2788 | DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), |
| 2789 | DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), |
| 2790 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
drh | 25c4296 | 2020-01-01 13:55:08 | [diff] [blame] | 2791 | INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
| 2792 | INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
| 2793 | INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
drh | 092457b | 2017-12-29 15:04:49 | [diff] [blame] | 2794 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
drh | 645682a | 2022-06-01 11:05:59 | [diff] [blame] | 2795 | INLINE_FUNC(sqlite_offset, 1, INLINEFUNC_sqlite_offset, 0 ), |
drh | 092457b | 2017-12-29 15:04:49 | [diff] [blame] | 2796 | #endif |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2797 | FUNCTION(ltrim, 1, 1, 0, trimFunc ), |
| 2798 | FUNCTION(ltrim, 2, 1, 0, trimFunc ), |
| 2799 | FUNCTION(rtrim, 1, 2, 0, trimFunc ), |
| 2800 | FUNCTION(rtrim, 2, 2, 0, trimFunc ), |
| 2801 | FUNCTION(trim, 1, 3, 0, trimFunc ), |
| 2802 | FUNCTION(trim, 2, 3, 0, trimFunc ), |
drh | 2e899cc | 2025-01-21 15:12:00 | [diff] [blame] | 2803 | FUNCTION(min, -3, 0, 1, minmaxFunc ), |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2804 | WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2805 | SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ), |
drh | 2e899cc | 2025-01-21 15:12:00 | [diff] [blame] | 2806 | FUNCTION(max, -3, 1, 1, minmaxFunc ), |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2807 | WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0, |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2808 | SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ), |
drh | a748fdc | 2012-03-28 01:34:47 | [diff] [blame] | 2809 | FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), |
drh | f7f78a6 | 2024-10-07 18:06:17 | [diff] [blame] | 2810 | FUNCTION2(subtype, 1, 0, 0, subtypeFunc, |
| 2811 | SQLITE_FUNC_TYPEOF|SQLITE_SUBTYPE), |
drh | a748fdc | 2012-03-28 01:34:47 | [diff] [blame] | 2812 | FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), |
drh | 077efc2 | 2023-06-22 21:19:37 | [diff] [blame] | 2813 | FUNCTION2(octet_length, 1, 0, 0, bytelengthFunc,SQLITE_FUNC_BYTELEN), |
drh | d55e072 | 2012-10-25 03:07:29 | [diff] [blame] | 2814 | FUNCTION(instr, 2, 0, 0, instrFunc ), |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 2815 | FUNCTION(printf, -1, 0, 0, printfFunc ), |
drh | 6bcd585 | 2022-01-08 21:00:38 | [diff] [blame] | 2816 | FUNCTION(format, -1, 0, 0, printfFunc ), |
drh | d495d8c | 2013-02-22 19:34:25 | [diff] [blame] | 2817 | FUNCTION(unicode, 1, 0, 0, unicodeFunc ), |
| 2818 | FUNCTION(char, -1, 0, 0, charFunc ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2819 | FUNCTION(abs, 1, 0, 0, absFunc ), |
drh | 453be55 | 2023-07-01 18:33:26 | [diff] [blame] | 2820 | #ifdef SQLITE_DEBUG |
| 2821 | FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ), |
drh | 74672ac | 2024-10-06 15:01:31 | [diff] [blame] | 2822 | FUNCTION(parseuri, -1, 0, 0, parseuriFunc ), |
drh | 453be55 | 2023-07-01 18:33:26 | [diff] [blame] | 2823 | #endif |
shane | fbd60f8 | 2009-02-04 03:59:25 | [diff] [blame] | 2824 | #ifndef SQLITE_OMIT_FLOATING_POINT |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2825 | FUNCTION(round, 1, 0, 0, roundFunc ), |
| 2826 | FUNCTION(round, 2, 0, 0, roundFunc ), |
shane | fbd60f8 | 2009-02-04 03:59:25 | [diff] [blame] | 2827 | #endif |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2828 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 2829 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2830 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
dan | e3c11d5 | 2023-01-23 14:11:34 | [diff] [blame] | 2831 | FUNCTION(unhex, 1, 0, 0, unhexFunc ), |
dan | a50db43 | 2023-01-24 17:19:47 | [diff] [blame] | 2832 | FUNCTION(unhex, 2, 0, 0, unhexFunc ), |
drh | 2e899cc | 2025-01-21 15:12:00 | [diff] [blame] | 2833 | FUNCTION(concat, -3, 0, 0, concatFunc ), |
| 2834 | FUNCTION(concat_ws, -4, 0, 0, concatwsFunc ), |
drh | ffe421c | 2020-05-13 17:26:38 | [diff] [blame] | 2835 | INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ), |
drh | b1fba28 | 2013-11-21 14:33:48 | [diff] [blame] | 2836 | VFUNCTION(random, 0, 0, 0, randomFunc ), |
| 2837 | VFUNCTION(randomblob, 1, 0, 0, randomBlob ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2838 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
drh | 03bf26d | 2015-08-31 21:16:36 | [diff] [blame] | 2839 | DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 2840 | DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
drh | 840561f | 2011-04-27 18:08:42 | [diff] [blame] | 2841 | FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ), |
drh | 4d70dba | 2025-02-22 23:18:38 | [diff] [blame] | 2842 | FUNCTION(unistr, 1, 0, 0, unistrFunc ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2843 | FUNCTION(quote, 1, 0, 0, quoteFunc ), |
drh | b6205d4 | 2025-02-24 13:51:24 | [diff] [blame] | 2844 | FUNCTION(unistr_quote, 1, 1, 0, quoteFunc ), |
drh | b1fba28 | 2013-11-21 14:33:48 | [diff] [blame] | 2845 | VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), |
| 2846 | VFUNCTION(changes, 0, 0, 0, changes ), |
| 2847 | VFUNCTION(total_changes, 0, 0, 0, total_changes ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2848 | FUNCTION(replace, 3, 0, 0, replaceFunc ), |
| 2849 | FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2850 | FUNCTION(substr, 2, 0, 0, substrFunc ), |
| 2851 | FUNCTION(substr, 3, 0, 0, substrFunc ), |
drh | 1335ec7 | 2020-11-23 14:50:43 | [diff] [blame] | 2852 | FUNCTION(substring, 2, 0, 0, substrFunc ), |
| 2853 | FUNCTION(substring, 3, 0, 0, substrFunc ), |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2854 | WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0), |
| 2855 | WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0), |
| 2856 | WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0), |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2857 | WAGGREGATE(count, 0,0,0, countStep, |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2858 | countFinalize, countFinalize, countInverse, |
| 2859 | SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ), |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2860 | WAGGREGATE(count, 1,0,0, countStep, |
drh | bb30123 | 2021-07-15 19:29:43 | [diff] [blame] | 2861 | countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ), |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2862 | WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2863 | groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2864 | WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, |
dan | 6fb2b54 | 2018-06-19 17:13:11 | [diff] [blame] | 2865 | groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), |
drh | d5e040b | 2023-10-20 20:19:30 | [diff] [blame] | 2866 | WAGGREGATE(string_agg, 2, 0, 0, groupConcatStep, |
| 2867 | groupConcatFinalize, groupConcatValue, groupConcatInverse, 0), |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2868 | |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2869 | LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 2870 | #ifdef SQLITE_CASE_SENSITIVE_LIKE |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2871 | LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
| 2872 | LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 2873 | #else |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2874 | LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), |
| 2875 | LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), |
drh | cc15313 | 2016-08-04 12:35:17 | [diff] [blame] | 2876 | #endif |
| 2877 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 2878 | FUNCTION(unknown, -1, 0, 0, unknownFunc ), |
| 2879 | #endif |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2880 | #ifdef SQLITE_ENABLE_MATH_FUNCTIONS |
drh | 4fd4a7a | 2021-01-01 01:44:06 | [diff] [blame] | 2881 | MFUNCTION(ceil, 1, xCeil, ceilingFunc ), |
| 2882 | MFUNCTION(ceiling, 1, xCeil, ceilingFunc ), |
| 2883 | MFUNCTION(floor, 1, xFloor, ceilingFunc ), |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2884 | #if SQLITE_HAVE_C99_MATH_FUNCS |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2885 | MFUNCTION(trunc, 1, trunc, ceilingFunc ), |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2886 | #endif |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2887 | FUNCTION(ln, 1, 0, 0, logFunc ), |
| 2888 | FUNCTION(log, 1, 1, 0, logFunc ), |
| 2889 | FUNCTION(log10, 1, 1, 0, logFunc ), |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2890 | FUNCTION(log2, 1, 2, 0, logFunc ), |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2891 | FUNCTION(log, 2, 0, 0, logFunc ), |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2892 | MFUNCTION(exp, 1, exp, math1Func ), |
| 2893 | MFUNCTION(pow, 2, pow, math2Func ), |
| 2894 | MFUNCTION(power, 2, pow, math2Func ), |
| 2895 | MFUNCTION(mod, 2, fmod, math2Func ), |
| 2896 | MFUNCTION(acos, 1, acos, math1Func ), |
| 2897 | MFUNCTION(asin, 1, asin, math1Func ), |
| 2898 | MFUNCTION(atan, 1, atan, math1Func ), |
| 2899 | MFUNCTION(atan2, 2, atan2, math2Func ), |
| 2900 | MFUNCTION(cos, 1, cos, math1Func ), |
| 2901 | MFUNCTION(sin, 1, sin, math1Func ), |
| 2902 | MFUNCTION(tan, 1, tan, math1Func ), |
| 2903 | MFUNCTION(cosh, 1, cosh, math1Func ), |
| 2904 | MFUNCTION(sinh, 1, sinh, math1Func ), |
| 2905 | MFUNCTION(tanh, 1, tanh, math1Func ), |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2906 | #if SQLITE_HAVE_C99_MATH_FUNCS |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2907 | MFUNCTION(acosh, 1, acosh, math1Func ), |
| 2908 | MFUNCTION(asinh, 1, asinh, math1Func ), |
| 2909 | MFUNCTION(atanh, 1, atanh, math1Func ), |
mistachkin | d97a4c0 | 2020-12-09 23:35:51 | [diff] [blame] | 2910 | #endif |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2911 | MFUNCTION(sqrt, 1, sqrt, math1Func ), |
| 2912 | MFUNCTION(radians, 1, degToRad, math1Func ), |
| 2913 | MFUNCTION(degrees, 1, radToDeg, math1Func ), |
drh | ddc764b | 2024-10-07 21:04:57 | [diff] [blame] | 2914 | MFUNCTION(pi, 0, 0, piFunc ), |
drh | f6e904b | 2020-12-07 17:15:32 | [diff] [blame] | 2915 | #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ |
drh | 63f8f98 | 2020-12-07 21:13:06 | [diff] [blame] | 2916 | FUNCTION(sign, 1, 0, 0, signFunc ), |
drh | 2e899cc | 2025-01-21 15:12:00 | [diff] [blame] | 2917 | INLINE_FUNC(coalesce, -4, INLINEFUNC_coalesce, 0 ), |
drh | 36279c2 | 2025-01-28 20:32:48 | [diff] [blame] | 2918 | INLINE_FUNC(iif, -4, INLINEFUNC_iif, 0 ), |
| 2919 | INLINE_FUNC(if, -4, INLINEFUNC_iif, 0 ), |
danielk1977 | 93ce741 | 2008-09-01 19:14:02 | [diff] [blame] | 2920 | }; |
drh | 545f587 | 2010-04-24 14:02:59 | [diff] [blame] | 2921 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 2922 | sqlite3AlterFunctions(); |
| 2923 | #endif |
dan | dfa552f | 2018-06-02 21:04:28 | [diff] [blame] | 2924 | sqlite3WindowFunctions(); |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2925 | sqlite3RegisterDateTimeFunctions(); |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 2926 | sqlite3RegisterJsonFunctions(); |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2927 | sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); |
| 2928 | |
| 2929 | #if 0 /* Enable to print out how the built-in functions are hashed */ |
| 2930 | { |
| 2931 | int i; |
| 2932 | FuncDef *p; |
| 2933 | for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ |
| 2934 | printf("FUNC-HASH %02d:", i); |
| 2935 | for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){ |
| 2936 | int n = sqlite3Strlen30(p->zName); |
| 2937 | int h = p->zName[0] + n; |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 2938 | assert( p->funcFlags & SQLITE_FUNC_BUILTIN ); |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 2939 | printf(" %s(%d)", p->zName, h); |
| 2940 | } |
| 2941 | printf("\n"); |
| 2942 | } |
| 2943 | } |
| 2944 | #endif |
drh | 70a8ca3 | 2008-08-21 18:49:27 | [diff] [blame] | 2945 | } |