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