sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2025-10-14 |
| 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 | ** Test the ability of run-time extension loading to use the |
| 14 | ** very latest interfaces. |
| 15 | ** |
| 16 | ** Compile something like this: |
| 17 | ** |
| 18 | ** Linux: gcc -g -fPIC shared testloadext.c -o testloadext.so |
| 19 | ** |
| 20 | ** Mac: cc -g -fPIC -dynamiclib testloadext.c -o testloadext.dylib |
| 21 | ** |
| 22 | ** Win11: cl testloadext.c -link -dll -out:testloadext.dll |
| 23 | */ |
| 24 | #include "sqlite3ext.h" |
| 25 | SQLITE_EXTENSION_INIT1 |
| 26 | #include <assert.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | /* |
| 30 | ** Implementation of the set_errmsg(CODE,MSG) SQL function. |
| 31 | ** |
| 32 | ** Raise an error that has numeric code CODE and text message MSG |
| 33 | ** using the sqlite3_set_errmsg() API. |
| 34 | */ |
| 35 | static void seterrmsgfunc( |
| 36 | sqlite3_context *context, |
| 37 | int argc, |
| 38 | sqlite3_value **argv |
| 39 | ){ |
| 40 | sqlite3 *db; |
| 41 | char *zRes; |
| 42 | int rc; |
| 43 | assert( argc==2 ); |
| 44 | db = sqlite3_context_db_handle(context); |
| 45 | rc = sqlite3_set_errmsg(db, |
| 46 | sqlite3_value_int(argv[0]), |
| 47 | sqlite3_value_text(argv[1])); |
| 48 | zRes = sqlite3_mprintf("%d %d %s", |
| 49 | rc, sqlite3_errcode(db), sqlite3_errmsg(db)); |
| 50 | sqlite3_result_text64(context, zRes, strlen(zRes), |
| 51 | SQLITE_TRANSIENT, SQLITE_UTF8); |
| 52 | sqlite3_free(zRes); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | ** Implementation of the tempbuf_spill() SQL function. |
| 57 | ** |
| 58 | ** Return the value of SQLITE_DBSTATUS_TEMPBUF_SPILL. |
| 59 | */ |
| 60 | static void tempbuf_spill_func( |
| 61 | sqlite3_context *context, |
| 62 | int argc, |
| 63 | sqlite3_value **argv |
| 64 | ){ |
| 65 | sqlite3 *db; |
| 66 | sqlite3_int64 iHi = 0, iCur = 0; |
| 67 | int rc; |
| 68 | int bReset; |
| 69 | assert( argc==1 ); |
| 70 | bReset = sqlite3_value_int(argv[0]); |
| 71 | db = sqlite3_context_db_handle(context); |
| 72 | (void)sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, |
| 73 | &iCur, &iHi, bReset); |
| 74 | sqlite3_result_int64(context, iCur); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | #ifdef _WIN32 |
| 79 | __declspec(dllexport) |
| 80 | #endif |
| 81 | int sqlite3_testloadext_init( |
| 82 | sqlite3 *db, |
| 83 | char **pzErrMsg, |
| 84 | const sqlite3_api_routines *pApi |
| 85 | ){ |
| 86 | int rc = SQLITE_OK; |
| 87 | SQLITE_EXTENSION_INIT2(pApi); |
| 88 | (void)pzErrMsg; /* Unused parameter */ |
| 89 | rc = sqlite3_create_function(db, "set_errmsg", 2, |
| 90 | SQLITE_UTF8, |
| 91 | 0, seterrmsgfunc, 0, 0); |
| 92 | if( rc ) return rc; |
| 93 | rc = sqlite3_create_function(db, "tempbuf_spill", 1, |
| 94 | SQLITE_UTF8, |
| 95 | 0, tempbuf_spill_func, 0, 0); |
| 96 | if( rc ) return rc; |
| 97 | return SQLITE_OK; |
| 98 | } |