sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / stmtrand.c
| 1 | /* |
| 2 | ** 2024-05-24 |
| 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 | ** |
| 13 | ** An SQL function that return pseudo-random non-negative integers. |
| 14 | ** |
| 15 | ** SELECT stmtrand(123); |
| 16 | ** |
| 17 | ** A special feature of this function is that the same sequence of random |
| 18 | ** integers is returned for each invocation of the statement. This makes |
| 19 | ** the results repeatable, and hence useful for testing. The argument is |
| 20 | ** an integer which is the seed for the random number sequence. The seed |
| 21 | ** is used by the first invocation of this function only and is ignored |
| 22 | ** for all subsequent calls within the same statement. |
| 23 | ** |
| 24 | ** Resetting a statement (sqlite3_reset()) also resets the random number |
| 25 | ** sequence. |
| 26 | */ |
| 27 | #include "sqlite3ext.h" |
| 28 | SQLITE_EXTENSION_INIT1 |
| 29 | #include <assert.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /* State of the pseudo-random number generator */ |
| 33 | typedef struct Stmtrand { |
| 34 | unsigned int x, y; |
| 35 | } Stmtrand; |
| 36 | |
| 37 | /* auxdata key */ |
| 38 | #define STMTRAND_KEY (-4418371) |
| 39 | |
| 40 | /* |
| 41 | ** Function: stmtrand(SEED) |
| 42 | ** |
| 43 | ** Return a pseudo-random number. |
| 44 | */ |
| 45 | static void stmtrandFunc( |
| 46 | sqlite3_context *context, |
| 47 | int argc, |
| 48 | sqlite3_value **argv |
| 49 | ){ |
| 50 | Stmtrand *p; |
| 51 | |
| 52 | p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY); |
| 53 | if( p==0 ){ |
| 54 | unsigned int seed; |
| 55 | p = sqlite3_malloc64( sizeof(*p) ); |
| 56 | if( p==0 ){ |
| 57 | sqlite3_result_error_nomem(context); |
| 58 | return; |
| 59 | } |
| 60 | if( argc>=1 ){ |
| 61 | seed = (unsigned int)sqlite3_value_int(argv[0]); |
| 62 | }else{ |
| 63 | seed = 0; |
| 64 | } |
| 65 | p->x = seed | 1; |
| 66 | p->y = seed; |
| 67 | sqlite3_set_auxdata(context, STMTRAND_KEY, p, sqlite3_free); |
| 68 | p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY); |
| 69 | if( p==0 ){ |
| 70 | sqlite3_result_error_nomem(context); |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | p->x = (p->x>>1) ^ ((1+~(p->x&1)) & 0xd0000001); |
| 75 | p->y = p->y*1103515245 + 12345; |
| 76 | sqlite3_result_int(context, (int)((p->x ^ p->y)&0x7fffffff)); |
| 77 | } |
| 78 | |
| 79 | #ifdef _WIN32 |
| 80 | __declspec(dllexport) |
| 81 | #endif |
| 82 | int sqlite3_stmtrand_init( |
| 83 | sqlite3 *db, |
| 84 | char **pzErrMsg, |
| 85 | const sqlite3_api_routines *pApi |
| 86 | ){ |
| 87 | int rc = SQLITE_OK; |
| 88 | SQLITE_EXTENSION_INIT2(pApi); |
| 89 | (void)pzErrMsg; /* Unused parameter */ |
| 90 | rc = sqlite3_create_function(db, "stmtrand", 1, SQLITE_UTF8, 0, |
| 91 | stmtrandFunc, 0, 0); |
| 92 | if( rc==SQLITE_OK ){ |
| 93 | rc = sqlite3_create_function(db, "stmtrand", 0, SQLITE_UTF8, 0, |
| 94 | stmtrandFunc, 0, 0); |
| 95 | } |
| 96 | return rc; |
| 97 | } |