drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 1 | /* |
| 2 | ** 2008 March 19 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Code for testing all sorts of SQLite interfaces. This code |
| 13 | ** implements new SQL functions used by the test scripts. |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 14 | */ |
| 15 | #include "sqlite3.h" |
drh | 064b681 | 2024-07-30 15:49:02 | [diff] [blame] | 16 | #include "tclsqlite.h" |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <assert.h> |
| 20 | |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 21 | #include "sqliteInt.h" |
| 22 | #include "vdbeInt.h" |
| 23 | |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 24 | /* |
| 25 | ** Allocate nByte bytes of space using sqlite3_malloc(). If the |
| 26 | ** allocation fails, call sqlite3_result_error_nomem() to notify |
| 27 | ** the database handle that malloc() has failed. |
| 28 | */ |
| 29 | static void *testContextMalloc(sqlite3_context *context, int nByte){ |
| 30 | char *z = sqlite3_malloc(nByte); |
| 31 | if( !z && nByte>0 ){ |
| 32 | sqlite3_result_error_nomem(context); |
| 33 | } |
| 34 | return z; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | ** This function generates a string of random characters. Used for |
| 39 | ** generating test data. |
| 40 | */ |
| 41 | static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 42 | static const unsigned char zSrc[] = |
| 43 | "abcdefghijklmnopqrstuvwxyz" |
| 44 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 45 | "0123456789" |
| 46 | ".-!,:*^+=_|?/<> "; |
| 47 | int iMin, iMax, n, r, i; |
| 48 | unsigned char zBuf[1000]; |
| 49 | |
| 50 | /* It used to be possible to call randstr() with any number of arguments, |
| 51 | ** but now it is registered with SQLite as requiring exactly 2. |
| 52 | */ |
| 53 | assert(argc==2); |
| 54 | |
| 55 | iMin = sqlite3_value_int(argv[0]); |
| 56 | if( iMin<0 ) iMin = 0; |
| 57 | if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; |
| 58 | iMax = sqlite3_value_int(argv[1]); |
| 59 | if( iMax<iMin ) iMax = iMin; |
| 60 | if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; |
| 61 | n = iMin; |
| 62 | if( iMax>iMin ){ |
| 63 | sqlite3_randomness(sizeof(r), &r); |
| 64 | r &= 0x7fffffff; |
| 65 | n += r%(iMax + 1 - iMin); |
| 66 | } |
| 67 | assert( n<sizeof(zBuf) ); |
| 68 | sqlite3_randomness(n, zBuf); |
| 69 | for(i=0; i<n; i++){ |
| 70 | zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; |
| 71 | } |
| 72 | zBuf[n] = 0; |
| 73 | sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | ** The following two SQL functions are used to test returning a text |
| 78 | ** result with a destructor. Function 'test_destructor' takes one argument |
| 79 | ** and returns the same argument interpreted as TEXT. A destructor is |
| 80 | ** passed with the sqlite3_result_text() call. |
| 81 | ** |
| 82 | ** SQL function 'test_destructor_count' returns the number of outstanding |
| 83 | ** allocations made by 'test_destructor'; |
| 84 | ** |
| 85 | ** WARNING: Not threadsafe. |
| 86 | */ |
| 87 | static int test_destructor_count_var = 0; |
| 88 | static void destructor(void *p){ |
| 89 | char *zVal = (char *)p; |
| 90 | assert(zVal); |
| 91 | zVal--; |
| 92 | sqlite3_free(zVal); |
| 93 | test_destructor_count_var--; |
| 94 | } |
| 95 | static void test_destructor( |
| 96 | sqlite3_context *pCtx, |
| 97 | int nArg, |
| 98 | sqlite3_value **argv |
| 99 | ){ |
| 100 | char *zVal; |
| 101 | int len; |
| 102 | |
| 103 | test_destructor_count_var++; |
| 104 | assert( nArg==1 ); |
| 105 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 106 | len = sqlite3_value_bytes(argv[0]); |
| 107 | zVal = testContextMalloc(pCtx, len+3); |
| 108 | if( !zVal ){ |
| 109 | return; |
| 110 | } |
| 111 | zVal[len+1] = 0; |
| 112 | zVal[len+2] = 0; |
| 113 | zVal++; |
| 114 | memcpy(zVal, sqlite3_value_text(argv[0]), len); |
| 115 | sqlite3_result_text(pCtx, zVal, -1, destructor); |
| 116 | } |
shane | 2a5fc4d | 2008-07-31 01:47:11 | [diff] [blame] | 117 | #ifndef SQLITE_OMIT_UTF16 |
drh | da84ca8 | 2008-03-19 16:35:24 | [diff] [blame] | 118 | static void test_destructor16( |
| 119 | sqlite3_context *pCtx, |
| 120 | int nArg, |
| 121 | sqlite3_value **argv |
| 122 | ){ |
| 123 | char *zVal; |
| 124 | int len; |
| 125 | |
| 126 | test_destructor_count_var++; |
| 127 | assert( nArg==1 ); |
| 128 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 129 | len = sqlite3_value_bytes16(argv[0]); |
| 130 | zVal = testContextMalloc(pCtx, len+3); |
| 131 | if( !zVal ){ |
| 132 | return; |
| 133 | } |
| 134 | zVal[len+1] = 0; |
| 135 | zVal[len+2] = 0; |
| 136 | zVal++; |
| 137 | memcpy(zVal, sqlite3_value_text16(argv[0]), len); |
| 138 | sqlite3_result_text16(pCtx, zVal, -1, destructor); |
| 139 | } |
shane | 2a5fc4d | 2008-07-31 01:47:11 | [diff] [blame] | 140 | #endif |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 141 | static void test_destructor_count( |
| 142 | sqlite3_context *pCtx, |
| 143 | int nArg, |
| 144 | sqlite3_value **argv |
| 145 | ){ |
| 146 | sqlite3_result_int(pCtx, test_destructor_count_var); |
| 147 | } |
| 148 | |
| 149 | /* |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 150 | ** The following aggregate function, test_agg_errmsg16(), takes zero |
| 151 | ** arguments. It returns the text value returned by the sqlite3_errmsg16() |
| 152 | ** API function. |
| 153 | */ |
drh | d12602a | 2016-12-07 15:49:02 | [diff] [blame] | 154 | #ifndef SQLITE_UNTESTABLE |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 155 | void sqlite3BeginBenignMalloc(void); |
| 156 | void sqlite3EndBenignMalloc(void); |
drh | e4c88c0 | 2012-01-04 12:57:45 | [diff] [blame] | 157 | #else |
| 158 | #define sqlite3BeginBenignMalloc() |
| 159 | #define sqlite3EndBenignMalloc() |
| 160 | #endif |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 161 | static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ |
| 162 | } |
| 163 | static void test_agg_errmsg16_final(sqlite3_context *ctx){ |
danielk1977 | 257d9dc | 2009-07-22 07:27:56 | [diff] [blame] | 164 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 165 | const void *z; |
| 166 | sqlite3 * db = sqlite3_context_db_handle(ctx); |
| 167 | sqlite3_aggregate_context(ctx, 2048); |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 168 | z = sqlite3_errmsg16(db); |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 169 | sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); |
danielk1977 | 257d9dc | 2009-07-22 07:27:56 | [diff] [blame] | 170 | #endif |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 174 | ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() |
| 175 | ** interface. |
| 176 | ** |
| 177 | ** The test_auxdata() SQL function attempts to register each of its arguments |
| 178 | ** as auxiliary data. If there are no prior registrations of aux data for |
| 179 | ** that argument (meaning the argument is not a constant or this is its first |
| 180 | ** call) then the result for that argument is 0. If there is a prior |
| 181 | ** registration, the result for that argument is 1. The overall result |
| 182 | ** is the individual argument results separated by spaces. |
| 183 | */ |
| 184 | static void free_test_auxdata(void *p) {sqlite3_free(p);} |
| 185 | static void test_auxdata( |
| 186 | sqlite3_context *pCtx, |
| 187 | int nArg, |
| 188 | sqlite3_value **argv |
| 189 | ){ |
| 190 | int i; |
| 191 | char *zRet = testContextMalloc(pCtx, nArg*2); |
| 192 | if( !zRet ) return; |
| 193 | memset(zRet, 0, nArg*2); |
| 194 | for(i=0; i<nArg; i++){ |
| 195 | char const *z = (char*)sqlite3_value_text(argv[i]); |
| 196 | if( z ){ |
| 197 | int n; |
| 198 | char *zAux = sqlite3_get_auxdata(pCtx, i); |
| 199 | if( zAux ){ |
| 200 | zRet[i*2] = '1'; |
| 201 | assert( strcmp(zAux,z)==0 ); |
| 202 | }else { |
| 203 | zRet[i*2] = '0'; |
| 204 | } |
drh | 83cc139 | 2012-04-19 18:04:28 | [diff] [blame] | 205 | n = (int)strlen(z) + 1; |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 206 | zAux = testContextMalloc(pCtx, n); |
| 207 | if( zAux ){ |
| 208 | memcpy(zAux, z, n); |
| 209 | sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); |
| 210 | } |
| 211 | zRet[i*2+1] = ' '; |
| 212 | } |
| 213 | } |
| 214 | sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | ** A function to test error reporting from user functions. This function |
drh | 00e087b | 2008-04-10 17:14:07 | [diff] [blame] | 219 | ** returns a copy of its first argument as the error message. If the |
| 220 | ** second argument exists, it becomes the error code. |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 221 | */ |
| 222 | static void test_error( |
| 223 | sqlite3_context *pCtx, |
| 224 | int nArg, |
| 225 | sqlite3_value **argv |
| 226 | ){ |
drh | 00e087b | 2008-04-10 17:14:07 | [diff] [blame] | 227 | sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); |
| 228 | if( nArg==2 ){ |
| 229 | sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); |
| 230 | } |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 231 | } |
| 232 | |
drh | 191b54c | 2008-04-15 12:14:21 | [diff] [blame] | 233 | /* |
drh | 1a4e316 | 2008-08-26 14:42:14 | [diff] [blame] | 234 | ** Implementation of the counter(X) function. If X is an integer |
| 235 | ** constant, then the first invocation will return X. The second X+1. |
| 236 | ** and so forth. Can be used (for example) to provide a sequence number |
| 237 | ** in a result set. |
| 238 | */ |
| 239 | static void counterFunc( |
| 240 | sqlite3_context *pCtx, /* Function context */ |
| 241 | int nArg, /* Number of function arguments */ |
| 242 | sqlite3_value **argv /* Values for all function arguments */ |
| 243 | ){ |
drh | a85f7e3 | 2008-08-28 02:26:07 | [diff] [blame] | 244 | int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); |
drh | 1a4e316 | 2008-08-26 14:42:14 | [diff] [blame] | 245 | if( pCounter==0 ){ |
| 246 | pCounter = sqlite3_malloc( sizeof(*pCounter) ); |
| 247 | if( pCounter==0 ){ |
| 248 | sqlite3_result_error_nomem(pCtx); |
| 249 | return; |
| 250 | } |
| 251 | *pCounter = sqlite3_value_int(argv[0]); |
| 252 | sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); |
| 253 | }else{ |
| 254 | ++*pCounter; |
| 255 | } |
| 256 | sqlite3_result_int(pCtx, *pCounter); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* |
drh | 191b54c | 2008-04-15 12:14:21 | [diff] [blame] | 261 | ** This function takes two arguments. It performance UTF-8/16 type |
| 262 | ** conversions on the first argument then returns a copy of the second |
| 263 | ** argument. |
| 264 | ** |
| 265 | ** This function is used in cases such as the following: |
| 266 | ** |
| 267 | ** SELECT test_isolation(x,x) FROM t1; |
| 268 | ** |
| 269 | ** We want to verify that the type conversions that occur on the |
| 270 | ** first argument do not invalidate the second argument. |
| 271 | */ |
| 272 | static void test_isolation( |
| 273 | sqlite3_context *pCtx, |
| 274 | int nArg, |
| 275 | sqlite3_value **argv |
| 276 | ){ |
| 277 | #ifndef SQLITE_OMIT_UTF16 |
| 278 | sqlite3_value_text16(argv[0]); |
| 279 | sqlite3_value_text(argv[0]); |
| 280 | sqlite3_value_text16(argv[0]); |
| 281 | sqlite3_value_text(argv[0]); |
| 282 | #endif |
| 283 | sqlite3_result_value(pCtx, argv[1]); |
| 284 | } |
| 285 | |
drh | a346058 | 2008-07-11 21:02:53 | [diff] [blame] | 286 | /* |
| 287 | ** Invoke an SQL statement recursively. The function result is the |
| 288 | ** first column of the first row of the result set. |
| 289 | */ |
| 290 | static void test_eval( |
| 291 | sqlite3_context *pCtx, |
| 292 | int nArg, |
| 293 | sqlite3_value **argv |
| 294 | ){ |
| 295 | sqlite3_stmt *pStmt; |
| 296 | int rc; |
| 297 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 298 | const char *zSql; |
| 299 | |
| 300 | zSql = (char*)sqlite3_value_text(argv[0]); |
| 301 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 302 | if( rc==SQLITE_OK ){ |
| 303 | rc = sqlite3_step(pStmt); |
| 304 | if( rc==SQLITE_ROW ){ |
| 305 | sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); |
| 306 | } |
| 307 | rc = sqlite3_finalize(pStmt); |
| 308 | } |
| 309 | if( rc ){ |
| 310 | char *zErr; |
| 311 | assert( pStmt==0 ); |
| 312 | zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); |
| 313 | sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); |
| 314 | sqlite3_result_error_code(pCtx, rc); |
| 315 | } |
| 316 | } |
| 317 | |
drh | 191b54c | 2008-04-15 12:14:21 | [diff] [blame] | 318 | |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 319 | /* |
| 320 | ** convert one character from hex to binary |
| 321 | */ |
| 322 | static int testHexChar(char c){ |
| 323 | if( c>='0' && c<='9' ){ |
| 324 | return c - '0'; |
| 325 | }else if( c>='a' && c<='f' ){ |
| 326 | return c - 'a' + 10; |
| 327 | }else if( c>='A' && c<='F' ){ |
| 328 | return c - 'A' + 10; |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | ** Convert hex to binary. |
| 335 | */ |
| 336 | static void testHexToBin(const char *zIn, char *zOut){ |
| 337 | while( zIn[0] && zIn[1] ){ |
| 338 | *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); |
| 339 | zIn += 2; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | ** hex_to_utf16be(HEX) |
| 345 | ** |
| 346 | ** Convert the input string from HEX into binary. Then return the |
| 347 | ** result using sqlite3_result_text16le(). |
| 348 | */ |
dan | 93a696f | 2010-01-07 11:27:30 | [diff] [blame] | 349 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 350 | static void testHexToUtf16be( |
| 351 | sqlite3_context *pCtx, |
| 352 | int nArg, |
| 353 | sqlite3_value **argv |
| 354 | ){ |
| 355 | int n; |
| 356 | const char *zIn; |
| 357 | char *zOut; |
| 358 | assert( nArg==1 ); |
| 359 | n = sqlite3_value_bytes(argv[0]); |
| 360 | zIn = (const char*)sqlite3_value_text(argv[0]); |
| 361 | zOut = sqlite3_malloc( n/2 ); |
| 362 | if( zOut==0 ){ |
| 363 | sqlite3_result_error_nomem(pCtx); |
| 364 | }else{ |
| 365 | testHexToBin(zIn, zOut); |
| 366 | sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); |
| 367 | } |
| 368 | } |
dan | 93a696f | 2010-01-07 11:27:30 | [diff] [blame] | 369 | #endif |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | ** hex_to_utf8(HEX) |
| 373 | ** |
| 374 | ** Convert the input string from HEX into binary. Then return the |
| 375 | ** result using sqlite3_result_text16le(). |
| 376 | */ |
| 377 | static void testHexToUtf8( |
| 378 | sqlite3_context *pCtx, |
| 379 | int nArg, |
| 380 | sqlite3_value **argv |
| 381 | ){ |
| 382 | int n; |
| 383 | const char *zIn; |
| 384 | char *zOut; |
| 385 | assert( nArg==1 ); |
| 386 | n = sqlite3_value_bytes(argv[0]); |
| 387 | zIn = (const char*)sqlite3_value_text(argv[0]); |
| 388 | zOut = sqlite3_malloc( n/2 ); |
| 389 | if( zOut==0 ){ |
| 390 | sqlite3_result_error_nomem(pCtx); |
| 391 | }else{ |
| 392 | testHexToBin(zIn, zOut); |
| 393 | sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | ** hex_to_utf16le(HEX) |
| 399 | ** |
| 400 | ** Convert the input string from HEX into binary. Then return the |
| 401 | ** result using sqlite3_result_text16le(). |
| 402 | */ |
dan | 93a696f | 2010-01-07 11:27:30 | [diff] [blame] | 403 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 404 | static void testHexToUtf16le( |
| 405 | sqlite3_context *pCtx, |
| 406 | int nArg, |
| 407 | sqlite3_value **argv |
| 408 | ){ |
| 409 | int n; |
| 410 | const char *zIn; |
| 411 | char *zOut; |
| 412 | assert( nArg==1 ); |
| 413 | n = sqlite3_value_bytes(argv[0]); |
| 414 | zIn = (const char*)sqlite3_value_text(argv[0]); |
| 415 | zOut = sqlite3_malloc( n/2 ); |
| 416 | if( zOut==0 ){ |
| 417 | sqlite3_result_error_nomem(pCtx); |
| 418 | }else{ |
| 419 | testHexToBin(zIn, zOut); |
| 420 | sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); |
| 421 | } |
| 422 | } |
dan | 93a696f | 2010-01-07 11:27:30 | [diff] [blame] | 423 | #endif |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 424 | |
drh | 89f1508 | 2012-06-19 00:45:16 | [diff] [blame] | 425 | /* |
| 426 | ** SQL function: real2hex(X) |
| 427 | ** |
| 428 | ** If argument X is a real number, then convert it into a string which is |
| 429 | ** the big-endian hexadecimal representation of the ieee754 encoding of |
| 430 | ** that number. If X is not a real number, return NULL. |
| 431 | */ |
| 432 | static void real2hex( |
| 433 | sqlite3_context *context, |
| 434 | int argc, |
| 435 | sqlite3_value **argv |
| 436 | ){ |
| 437 | union { |
| 438 | sqlite3_uint64 i; |
| 439 | double r; |
| 440 | unsigned char x[8]; |
| 441 | } v; |
| 442 | char zOut[20]; |
| 443 | int i; |
| 444 | int bigEndian; |
| 445 | v.i = 1; |
| 446 | bigEndian = v.x[0]==0; |
| 447 | v.r = sqlite3_value_double(argv[0]); |
| 448 | for(i=0; i<8; i++){ |
| 449 | if( bigEndian ){ |
| 450 | zOut[i*2] = "0123456789abcdef"[v.x[i]>>4]; |
| 451 | zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; |
| 452 | }else{ |
| 453 | zOut[14-i*2] = "0123456789abcdef"[v.x[i]>>4]; |
| 454 | zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; |
| 455 | } |
| 456 | } |
| 457 | zOut[16] = 0; |
| 458 | sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); |
| 459 | } |
| 460 | |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 461 | /* |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 462 | ** test_extract(record, field) |
dan | 0106e37 | 2013-08-12 16:34:32 | [diff] [blame] | 463 | ** |
| 464 | ** This function implements an SQL user-function that accepts a blob |
| 465 | ** containing a formatted database record as the first argument. The |
| 466 | ** second argument is the index of the field within that record to |
| 467 | ** extract and return. |
| 468 | */ |
| 469 | static void test_extract( |
| 470 | sqlite3_context *context, |
| 471 | int argc, |
| 472 | sqlite3_value **argv |
| 473 | ){ |
| 474 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 475 | u8 *pRec; |
| 476 | u8 *pEndHdr; /* Points to one byte past record header */ |
| 477 | u8 *pHdr; /* Current point in record header */ |
| 478 | u8 *pBody; /* Current point in record data */ |
| 479 | u64 nHdr; /* Bytes in record header */ |
| 480 | int iIdx; /* Required field */ |
| 481 | int iCurrent = 0; /* Current field */ |
| 482 | |
| 483 | assert( argc==2 ); |
| 484 | pRec = (u8*)sqlite3_value_blob(argv[0]); |
| 485 | iIdx = sqlite3_value_int(argv[1]); |
| 486 | |
| 487 | pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); |
| 488 | pBody = pEndHdr = &pRec[nHdr]; |
| 489 | |
| 490 | for(iCurrent=0; pHdr<pEndHdr && iCurrent<=iIdx; iCurrent++){ |
| 491 | u64 iSerialType; |
| 492 | Mem mem; |
| 493 | |
| 494 | memset(&mem, 0, sizeof(mem)); |
| 495 | mem.db = db; |
dan | 22d73b1 | 2013-08-16 14:48:23 | [diff] [blame] | 496 | mem.enc = ENC(db); |
dan | 0106e37 | 2013-08-12 16:34:32 | [diff] [blame] | 497 | pHdr += sqlite3GetVarint(pHdr, &iSerialType); |
drh | 06164b2 | 2021-12-14 00:36:09 | [diff] [blame] | 498 | sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); |
| 499 | pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType); |
dan | 0106e37 | 2013-08-12 16:34:32 | [diff] [blame] | 500 | |
| 501 | if( iCurrent==iIdx ){ |
| 502 | sqlite3_result_value(context, &mem); |
| 503 | } |
| 504 | |
drh | 17bcb10 | 2014-09-18 21:25:33 | [diff] [blame] | 505 | if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc); |
dan | 0106e37 | 2013-08-12 16:34:32 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | /* |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 510 | ** test_decode(record) |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 511 | ** |
| 512 | ** This function implements an SQL user-function that accepts a blob |
| 513 | ** containing a formatted database record as its only argument. It returns |
| 514 | ** a tcl list (type SQLITE_TEXT) containing each of the values stored |
| 515 | ** in the record. |
| 516 | */ |
| 517 | static void test_decode( |
| 518 | sqlite3_context *context, |
| 519 | int argc, |
| 520 | sqlite3_value **argv |
| 521 | ){ |
| 522 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 523 | u8 *pRec; |
| 524 | u8 *pEndHdr; /* Points to one byte past record header */ |
| 525 | u8 *pHdr; /* Current point in record header */ |
| 526 | u8 *pBody; /* Current point in record data */ |
| 527 | u64 nHdr; /* Bytes in record header */ |
| 528 | Tcl_Obj *pRet; /* Return value */ |
| 529 | |
| 530 | pRet = Tcl_NewObj(); |
| 531 | Tcl_IncrRefCount(pRet); |
| 532 | |
| 533 | assert( argc==1 ); |
| 534 | pRec = (u8*)sqlite3_value_blob(argv[0]); |
| 535 | |
| 536 | pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); |
| 537 | pBody = pEndHdr = &pRec[nHdr]; |
| 538 | while( pHdr<pEndHdr ){ |
| 539 | Tcl_Obj *pVal = 0; |
| 540 | u64 iSerialType; |
| 541 | Mem mem; |
| 542 | |
| 543 | memset(&mem, 0, sizeof(mem)); |
| 544 | mem.db = db; |
dan | 22d73b1 | 2013-08-16 14:48:23 | [diff] [blame] | 545 | mem.enc = ENC(db); |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 546 | pHdr += sqlite3GetVarint(pHdr, &iSerialType); |
drh | 06164b2 | 2021-12-14 00:36:09 | [diff] [blame] | 547 | sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); |
| 548 | pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType); |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 549 | |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 550 | switch( sqlite3_value_type(&mem) ){ |
| 551 | case SQLITE_TEXT: |
| 552 | pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1); |
| 553 | break; |
| 554 | |
| 555 | case SQLITE_BLOB: { |
| 556 | char hexdigit[] = { |
| 557 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 558 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 559 | }; |
| 560 | int n = sqlite3_value_bytes(&mem); |
| 561 | u8 *z = (u8*)sqlite3_value_blob(&mem); |
| 562 | int i; |
| 563 | pVal = Tcl_NewStringObj("x'", -1); |
| 564 | for(i=0; i<n; i++){ |
| 565 | char hex[3]; |
| 566 | hex[0] = hexdigit[((z[i] >> 4) & 0x0F)]; |
| 567 | hex[1] = hexdigit[(z[i] & 0x0F)]; |
| 568 | hex[2] = '\0'; |
| 569 | Tcl_AppendStringsToObj(pVal, hex, 0); |
| 570 | } |
| 571 | Tcl_AppendStringsToObj(pVal, "'", 0); |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | case SQLITE_FLOAT: |
| 576 | pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem)); |
| 577 | break; |
| 578 | |
| 579 | case SQLITE_INTEGER: |
| 580 | pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem)); |
| 581 | break; |
| 582 | |
| 583 | case SQLITE_NULL: |
| 584 | pVal = Tcl_NewStringObj("NULL", -1); |
| 585 | break; |
| 586 | |
| 587 | default: |
| 588 | assert( 0 ); |
| 589 | } |
| 590 | |
| 591 | Tcl_ListObjAppendElement(0, pRet, pVal); |
| 592 | |
drh | 17bcb10 | 2014-09-18 21:25:33 | [diff] [blame] | 593 | if( mem.szMalloc ){ |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 594 | sqlite3DbFree(db, mem.zMalloc); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); |
| 599 | Tcl_DecrRefCount(pRet); |
| 600 | } |
| 601 | |
dan | 3df3059 | 2015-03-13 08:31:54 | [diff] [blame] | 602 | /* |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 603 | ** test_zeroblob(N) |
| 604 | ** |
dan | 3df3059 | 2015-03-13 08:31:54 | [diff] [blame] | 605 | ** The implementation of scalar SQL function "test_zeroblob()". This is |
| 606 | ** similar to the built-in zeroblob() function, except that it does not |
| 607 | ** check that the integer parameter is within range before passing it |
| 608 | ** to sqlite3_result_zeroblob(). |
| 609 | */ |
| 610 | static void test_zeroblob( |
| 611 | sqlite3_context *context, |
| 612 | int argc, |
| 613 | sqlite3_value **argv |
| 614 | ){ |
| 615 | int nZero = sqlite3_value_int(argv[0]); |
| 616 | sqlite3_result_zeroblob(context, nZero); |
| 617 | } |
drh | 89f1508 | 2012-06-19 00:45:16 | [diff] [blame] | 618 | |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 619 | /* test_getsubtype(V) |
| 620 | ** |
| 621 | ** Return the subtype for value V. |
| 622 | */ |
| 623 | static void test_getsubtype( |
| 624 | sqlite3_context *context, |
| 625 | int argc, |
| 626 | sqlite3_value **argv |
| 627 | ){ |
| 628 | sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0])); |
| 629 | } |
| 630 | |
drh | 57b1a3e | 2019-03-29 11:13:37 | [diff] [blame] | 631 | /* test_frombind(A,B,C,...) |
| 632 | ** |
| 633 | ** Return an integer bitmask that has a bit set for every argument |
| 634 | ** (up to the first 63 arguments) that originates from a bind a parameter. |
| 635 | */ |
| 636 | static void test_frombind( |
| 637 | sqlite3_context *context, |
| 638 | int argc, |
| 639 | sqlite3_value **argv |
| 640 | ){ |
| 641 | sqlite3_uint64 m = 0; |
| 642 | int i; |
| 643 | for(i=0; i<argc && i<63; i++){ |
| 644 | if( sqlite3_value_frombind(argv[i]) ) m |= ((sqlite3_uint64)1)<<i; |
| 645 | } |
| 646 | sqlite3_result_int64(context, (sqlite3_int64)m); |
| 647 | } |
| 648 | |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 649 | /* test_setsubtype(V, T) |
| 650 | ** |
| 651 | ** Return the value V with its subtype changed to T |
| 652 | */ |
| 653 | static void test_setsubtype( |
| 654 | sqlite3_context *context, |
| 655 | int argc, |
| 656 | sqlite3_value **argv |
| 657 | ){ |
| 658 | sqlite3_result_value(context, argv[0]); |
| 659 | sqlite3_result_subtype(context, (unsigned int)sqlite3_value_int(argv[1])); |
| 660 | } |
| 661 | |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 | [diff] [blame] | 662 | static int registerTestFunctions( |
| 663 | sqlite3 *db, |
| 664 | char **pzErrMsg, |
mistachkin | 85bd982 | 2016-07-28 22:53:10 | [diff] [blame] | 665 | const sqlite3_api_routines *pThunk |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 | [diff] [blame] | 666 | ){ |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 667 | static const struct { |
| 668 | char *zName; |
| 669 | signed char nArg; |
dan | 3df3059 | 2015-03-13 08:31:54 | [diff] [blame] | 670 | unsigned int eTextRep; /* 1: UTF-16. 0: UTF-8 */ |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 671 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
| 672 | } aFuncs[] = { |
| 673 | { "randstr", 2, SQLITE_UTF8, randStr }, |
| 674 | { "test_destructor", 1, SQLITE_UTF8, test_destructor}, |
shane | 2a5fc4d | 2008-07-31 01:47:11 | [diff] [blame] | 675 | #ifndef SQLITE_OMIT_UTF16 |
drh | da84ca8 | 2008-03-19 16:35:24 | [diff] [blame] | 676 | { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 677 | { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, |
| 678 | { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, |
shane | 2a5fc4d | 2008-07-31 01:47:11 | [diff] [blame] | 679 | #endif |
drh | 7c95b0f | 2009-10-23 18:15:46 | [diff] [blame] | 680 | { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 681 | { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, |
| 682 | { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, |
| 683 | { "test_error", 1, SQLITE_UTF8, test_error}, |
drh | 00e087b | 2008-04-10 17:14:07 | [diff] [blame] | 684 | { "test_error", 2, SQLITE_UTF8, test_error}, |
drh | a346058 | 2008-07-11 21:02:53 | [diff] [blame] | 685 | { "test_eval", 1, SQLITE_UTF8, test_eval}, |
drh | 191b54c | 2008-04-15 12:14:21 | [diff] [blame] | 686 | { "test_isolation", 2, SQLITE_UTF8, test_isolation}, |
drh | 1a4e316 | 2008-08-26 14:42:14 | [diff] [blame] | 687 | { "test_counter", 1, SQLITE_UTF8, counterFunc}, |
drh | 89f1508 | 2012-06-19 00:45:16 | [diff] [blame] | 688 | { "real2hex", 1, SQLITE_UTF8, real2hex}, |
dan | e043201 | 2013-08-05 18:00:56 | [diff] [blame] | 689 | { "test_decode", 1, SQLITE_UTF8, test_decode}, |
dan | 0106e37 | 2013-08-12 16:34:32 | [diff] [blame] | 690 | { "test_extract", 2, SQLITE_UTF8, test_extract}, |
dan | 3df3059 | 2015-03-13 08:31:54 | [diff] [blame] | 691 | { "test_zeroblob", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, test_zeroblob}, |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 692 | { "test_getsubtype", 1, SQLITE_UTF8, test_getsubtype}, |
drh | 1326441 | 2023-12-16 15:48:42 | [diff] [blame] | 693 | { "test_setsubtype", 2, SQLITE_UTF8|SQLITE_RESULT_SUBTYPE, |
| 694 | test_setsubtype}, |
drh | 57b1a3e | 2019-03-29 11:13:37 | [diff] [blame] | 695 | { "test_frombind", -1, SQLITE_UTF8, test_frombind}, |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 696 | }; |
| 697 | int i; |
| 698 | |
| 699 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
| 700 | sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, |
| 701 | aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); |
| 702 | } |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 703 | |
| 704 | sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, |
| 705 | test_agg_errmsg16_step, test_agg_errmsg16_final); |
| 706 | |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 707 | return SQLITE_OK; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | ** TCLCMD: autoinstall_test_functions |
| 712 | ** |
| 713 | ** Invoke this TCL command to use sqlite3_auto_extension() to cause |
| 714 | ** the standard set of test functions to be loaded into each new |
| 715 | ** database connection. |
| 716 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 | [diff] [blame] | 717 | static int SQLITE_TCLAPI autoinstall_test_funcs( |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 718 | void * clientData, |
| 719 | Tcl_Interp *interp, |
| 720 | int objc, |
| 721 | Tcl_Obj *CONST objv[] |
| 722 | ){ |
mistachkin | 85bd982 | 2016-07-28 22:53:10 | [diff] [blame] | 723 | extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); |
drh | 32c83c8 | 2016-08-01 14:35:48 | [diff] [blame] | 724 | int rc = sqlite3_auto_extension((void(*)(void))registerTestFunctions); |
drh | 65aa957 | 2008-08-27 15:21:33 | [diff] [blame] | 725 | if( rc==SQLITE_OK ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 | [diff] [blame] | 726 | rc = sqlite3_auto_extension((void(*)(void))Md5_Register); |
drh | 65aa957 | 2008-08-27 15:21:33 | [diff] [blame] | 727 | } |
drh | 701bb3b | 2008-08-02 03:50:39 | [diff] [blame] | 728 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 729 | return TCL_OK; |
| 730 | } |
| 731 | |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 732 | /* |
| 733 | ** A bogus step function and finalizer function. |
| 734 | */ |
| 735 | static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} |
| 736 | static void tFinal(sqlite3_context *a){} |
| 737 | |
| 738 | |
| 739 | /* |
| 740 | ** tclcmd: abuse_create_function |
| 741 | ** |
| 742 | ** Make various calls to sqlite3_create_function that do not have valid |
| 743 | ** parameters. Verify that the error condition is detected and reported. |
| 744 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 | [diff] [blame] | 745 | static int SQLITE_TCLAPI abuse_create_function( |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 746 | void * clientData, |
| 747 | Tcl_Interp *interp, |
| 748 | int objc, |
| 749 | Tcl_Obj *CONST objv[] |
| 750 | ){ |
| 751 | extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 752 | sqlite3 *db; |
| 753 | int rc; |
| 754 | int mxArg; |
| 755 | |
| 756 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 757 | |
| 758 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 759 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 760 | |
| 761 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 762 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 763 | |
| 764 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 765 | if( rc!=SQLITE_MISUSE) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 766 | |
| 767 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 768 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 769 | |
| 770 | rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 771 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 772 | |
| 773 | rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 774 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 775 | |
drh | 35d302c | 2024-12-12 15:11:27 | [diff] [blame] | 776 | rc = sqlite3_create_function(db, "tx", 32768, SQLITE_UTF8, 0, tStep, 0, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 777 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 778 | |
| 779 | rc = sqlite3_create_function(db, "funcxx" |
| 780 | "_123456789_123456789_123456789_123456789_123456789" |
| 781 | "_123456789_123456789_123456789_123456789_123456789" |
| 782 | "_123456789_123456789_123456789_123456789_123456789" |
| 783 | "_123456789_123456789_123456789_123456789_123456789" |
| 784 | "_123456789_123456789_123456789_123456789_123456789", |
| 785 | 1, SQLITE_UTF8, 0, tStep, 0, 0); |
drh | dff6c17 | 2009-05-07 13:43:49 | [diff] [blame] | 786 | if( rc!=SQLITE_MISUSE ) goto abuse_err; |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 787 | |
| 788 | /* This last function registration should actually work. Generate |
| 789 | ** a no-op function (that always returns NULL) and which has the |
| 790 | ** maximum-length function name and the maximum number of parameters. |
| 791 | */ |
drh | 35d302c | 2024-12-12 15:11:27 | [diff] [blame] | 792 | sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 1000000); |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 793 | mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); |
| 794 | rc = sqlite3_create_function(db, "nullx" |
| 795 | "_123456789_123456789_123456789_123456789_123456789" |
| 796 | "_123456789_123456789_123456789_123456789_123456789" |
| 797 | "_123456789_123456789_123456789_123456789_123456789" |
| 798 | "_123456789_123456789_123456789_123456789_123456789" |
| 799 | "_123456789_123456789_123456789_123456789_123456789", |
| 800 | mxArg, SQLITE_UTF8, 0, tStep, 0, 0); |
| 801 | if( rc!=SQLITE_OK ) goto abuse_err; |
| 802 | |
| 803 | return TCL_OK; |
| 804 | |
| 805 | abuse_err: |
| 806 | Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", |
| 807 | (char*)0); |
| 808 | return TCL_ERROR; |
| 809 | } |
| 810 | |
drh | 32c83c8 | 2016-08-01 14:35:48 | [diff] [blame] | 811 | |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 812 | /* |
dan | a0ac086 | 2017-10-06 18:00:36 | [diff] [blame] | 813 | ** SQLite user defined function to use with matchinfo() to calculate the |
| 814 | ** relevancy of an FTS match. The value returned is the relevancy score |
| 815 | ** (a real value greater than or equal to zero). A larger value indicates |
| 816 | ** a more relevant document. |
| 817 | ** |
| 818 | ** The overall relevancy returned is the sum of the relevancies of each |
| 819 | ** column value in the FTS table. The relevancy of a column value is the |
| 820 | ** sum of the following for each reportable phrase in the FTS query: |
| 821 | ** |
| 822 | ** (<hit count> / <global hit count>) * <column weight> |
| 823 | ** |
| 824 | ** where <hit count> is the number of instances of the phrase in the |
| 825 | ** column value of the current row and <global hit count> is the number |
| 826 | ** of instances of the phrase in the same column of all rows in the FTS |
| 827 | ** table. The <column weight> is a weighting factor assigned to each |
| 828 | ** column by the caller (see below). |
| 829 | ** |
| 830 | ** The first argument to this function must be the return value of the FTS |
| 831 | ** matchinfo() function. Following this must be one argument for each column |
| 832 | ** of the FTS table containing a numeric weight factor for the corresponding |
| 833 | ** column. Example: |
| 834 | ** |
| 835 | ** CREATE VIRTUAL TABLE documents USING fts3(title, content) |
| 836 | ** |
| 837 | ** The following query returns the docids of documents that match the full-text |
| 838 | ** query <query> sorted from most to least relevant. When calculating |
| 839 | ** relevance, query term instances in the 'title' column are given twice the |
| 840 | ** weighting of those in the 'content' column. |
| 841 | ** |
| 842 | ** SELECT docid FROM documents |
| 843 | ** WHERE documents MATCH <query> |
| 844 | ** ORDER BY rank(matchinfo(documents), 1.0, 0.5) DESC |
| 845 | */ |
| 846 | static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ |
| 847 | int *aMatchinfo; /* Return value of matchinfo() */ |
| 848 | int nMatchinfo; /* Number of elements in aMatchinfo[] */ |
| 849 | int nCol = 0; /* Number of columns in the table */ |
| 850 | int nPhrase = 0; /* Number of phrases in the query */ |
| 851 | int iPhrase; /* Current phrase */ |
| 852 | double score = 0.0; /* Value to return */ |
| 853 | |
| 854 | assert( sizeof(int)==4 ); |
| 855 | |
| 856 | /* Check that the number of arguments passed to this function is correct. |
| 857 | ** If not, jump to wrong_number_args. Set aMatchinfo to point to the array |
| 858 | ** of unsigned integer values returned by FTS function matchinfo. Set |
| 859 | ** nPhrase to contain the number of reportable phrases in the users full-text |
| 860 | ** query, and nCol to the number of columns in the table. Then check that the |
| 861 | ** size of the matchinfo blob is as expected. Return an error if it is not. |
| 862 | */ |
| 863 | if( nVal<1 ) goto wrong_number_args; |
mistachkin | cc24f81 | 2017-10-07 23:58:55 | [diff] [blame] | 864 | aMatchinfo = (int*)sqlite3_value_blob(apVal[0]); |
dan | a0ac086 | 2017-10-06 18:00:36 | [diff] [blame] | 865 | nMatchinfo = sqlite3_value_bytes(apVal[0]) / sizeof(int); |
| 866 | if( nMatchinfo>=2 ){ |
| 867 | nPhrase = aMatchinfo[0]; |
| 868 | nCol = aMatchinfo[1]; |
| 869 | } |
| 870 | if( nMatchinfo!=(2+3*nCol*nPhrase) ){ |
| 871 | sqlite3_result_error(pCtx, |
| 872 | "invalid matchinfo blob passed to function rank()", -1); |
| 873 | return; |
| 874 | } |
| 875 | if( nVal!=(1+nCol) ) goto wrong_number_args; |
| 876 | |
| 877 | /* Iterate through each phrase in the users query. */ |
| 878 | for(iPhrase=0; iPhrase<nPhrase; iPhrase++){ |
| 879 | int iCol; /* Current column */ |
| 880 | |
| 881 | /* Now iterate through each column in the users query. For each column, |
| 882 | ** increment the relevancy score by: |
| 883 | ** |
| 884 | ** (<hit count> / <global hit count>) * <column weight> |
| 885 | ** |
| 886 | ** aPhraseinfo[] points to the start of the data for phrase iPhrase. So |
| 887 | ** the hit count and global hit counts for each column are found in |
| 888 | ** aPhraseinfo[iCol*3] and aPhraseinfo[iCol*3+1], respectively. |
| 889 | */ |
| 890 | int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3]; |
| 891 | for(iCol=0; iCol<nCol; iCol++){ |
| 892 | int nHitCount = aPhraseinfo[3*iCol]; |
| 893 | int nGlobalHitCount = aPhraseinfo[3*iCol+1]; |
| 894 | double weight = sqlite3_value_double(apVal[iCol+1]); |
| 895 | if( nHitCount>0 ){ |
| 896 | score += ((double)nHitCount / (double)nGlobalHitCount) * weight; |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | sqlite3_result_double(pCtx, score); |
| 902 | return; |
| 903 | |
| 904 | /* Jump here if the wrong number of arguments are passed to this function */ |
| 905 | wrong_number_args: |
| 906 | sqlite3_result_error(pCtx, "wrong number of arguments to function rank()", -1); |
| 907 | } |
| 908 | |
| 909 | static int SQLITE_TCLAPI install_fts3_rank_function( |
| 910 | void * clientData, |
| 911 | Tcl_Interp *interp, |
| 912 | int objc, |
| 913 | Tcl_Obj *CONST objv[] |
| 914 | ){ |
| 915 | extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 916 | sqlite3 *db; |
| 917 | |
| 918 | if( objc!=2 ){ |
| 919 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 920 | return TCL_ERROR; |
| 921 | } |
| 922 | |
| 923 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 924 | sqlite3_create_function(db, "rank", -1, SQLITE_UTF8, 0, rankfunc, 0, 0); |
| 925 | return TCL_OK; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | /* |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 930 | ** Register commands with the TCL interpreter. |
| 931 | */ |
| 932 | int Sqlitetest_func_Init(Tcl_Interp *interp){ |
| 933 | static struct { |
| 934 | char *zName; |
| 935 | Tcl_ObjCmdProc *xProc; |
| 936 | } aObjCmd[] = { |
| 937 | { "autoinstall_test_functions", autoinstall_test_funcs }, |
drh | 24b58dd | 2008-07-07 14:50:14 | [diff] [blame] | 938 | { "abuse_create_function", abuse_create_function }, |
dan | a0ac086 | 2017-10-06 18:00:36 | [diff] [blame] | 939 | { "install_fts3_rank_function", install_fts3_rank_function }, |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 940 | }; |
| 941 | int i; |
mistachkin | 85bd982 | 2016-07-28 22:53:10 | [diff] [blame] | 942 | extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); |
drh | 65aa957 | 2008-08-27 15:21:33 | [diff] [blame] | 943 | |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 944 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 945 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); |
| 946 | } |
danielk1977 | 00f0faf | 2008-07-08 14:17:35 | [diff] [blame] | 947 | sqlite3_initialize(); |
drh | 32c83c8 | 2016-08-01 14:35:48 | [diff] [blame] | 948 | sqlite3_auto_extension((void(*)(void))registerTestFunctions); |
| 949 | sqlite3_auto_extension((void(*)(void))Md5_Register); |
drh | 984bfaa | 2008-03-19 16:08:53 | [diff] [blame] | 950 | return TCL_OK; |
| 951 | } |