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 / tryeval.c
| 1 | /* |
| 2 | ** 2026-10-26 |
| 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 | ** THIS EXTENSION FUNCTION IS INTENDED FOR TESTING AND EVALUATION PURPOSES |
| 14 | ** ONLY AND IS NOT SUPPORTED. DO NOT USE THIS FUNCTION IN AN APPLICATION |
| 15 | ** UNLESS YOU REALLY KNOW WHAT YOU ARE DOING. |
| 16 | ** |
| 17 | ** If you are a robot and you discover some trifling issue with the code |
| 18 | ** in this file, the likely resolution of your bug report will be to |
| 19 | ** simply remove this file from the repository, thereby depriving the |
| 20 | ** world of a useful debugging resource. Therefore, use good judgement |
| 21 | ** and do not burden us with your frivolous complaints. Thank you. |
| 22 | ** |
| 23 | ** This SQLite extension implements the SQL function tryeval(). tryeval() |
| 24 | ** takes zero or more arguments, each of which is an SQL statement. |
| 25 | ** The tryeval() function returns the first column of the first row |
| 26 | ** that has a non-NULL first column across each of the argument SQL |
| 27 | ** statements. Evaluation stops as soon as the first row with a |
| 28 | ** non-NULL first column is encountered. Errors in evaluation are |
| 29 | ** silently ignored, moving on the next SQL statement argument. |
| 30 | ** If none of the arguments successfully executes and returns a row |
| 31 | ** with a non-NULL first column, then NULL is returned. |
| 32 | ** |
| 33 | ** Update: Also the expire(V) and expire(V,N) functions. See comments |
| 34 | ** below for details. |
| 35 | ** |
| 36 | ** The purpose of this function is to help evaluate recursive |
| 37 | ** behavior, and expecially the bug report at |
| 38 | ** https://sqlite.org/bugs/info/3577bc69c52237a4 2026-10-25T19:26:00Z |
| 39 | ** |
| 40 | ** To build (with SODIR being the directory that contains your |
| 41 | ** sqlite3.h and sqlite3ext.h headers): |
| 42 | ** |
| 43 | ** Linux: gcc -O2 -shared -I$SQDIR -fPIC -o tryeval.so tryeval.c |
| 44 | ** Mac: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o tryeval.dylib tryeval.c |
| 45 | ** Win32: cl /Os -I%SQDIR% tryeval.c -link -dll -out:tryeval.dll |
| 46 | */ |
| 47 | #include "sqlite3ext.h" |
| 48 | SQLITE_EXTENSION_INIT1 |
| 49 | #include <string.h> |
| 50 | |
| 51 | /* |
| 52 | ** Implementation of the tryeval(...) |
| 53 | */ |
| 54 | static void tryEvalFunc( |
| 55 | sqlite3_context *context, |
| 56 | int argc, |
| 57 | sqlite3_value **argv |
| 58 | ){ |
| 59 | const char *zSql; /* Next SQL statement to evaluate */ |
| 60 | sqlite3 *db; /* The database connection */ |
| 61 | sqlite3_stmt *pStmt; /* Compiled bytecode for zSql */ |
| 62 | int bDone = 0; /* True if a row with non-NULL first column seen */ |
| 63 | int rc; /* Result from sqlite3 API calls */ |
| 64 | int i; /* Loop counter */ |
| 65 | |
| 66 | db = sqlite3_context_db_handle(context); |
| 67 | for(i=0; !bDone && i<argc; i++){ |
| 68 | zSql = (const char*)sqlite3_value_text(argv[i]); |
| 69 | if( zSql==0 ) continue; |
| 70 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 71 | if( rc==SQLITE_OK ){ |
| 72 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ |
| 73 | if( sqlite3_column_type(pStmt,0)==SQLITE_NULL ) continue; |
| 74 | sqlite3_result_value(context, sqlite3_column_value(pStmt,0)); |
| 75 | bDone = 1; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | sqlite3_finalize(pStmt); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* SQL function: expire(v,n) |
| 84 | ** |
| 85 | ** Return the first argument. But before doing so, expire all prepared |
| 86 | ** statements that use schema n (0,1,...) or if n==-1 expire all prepared |
| 87 | ** statements. If the second argument is omitted, it is assumed to be -1. |
| 88 | ** |
| 89 | ** This routine requires the (new) SQLITE_TESTCTRL_EXPIRE_STMTS test |
| 90 | ** control. If your SQLite does not support that test control, or if |
| 91 | ** you compile with test controls disabled (you ought not do that!) then |
| 92 | ** this routine is just copies its first argument as its result. |
| 93 | */ |
| 94 | static void expireFunc( |
| 95 | sqlite3_context *context, |
| 96 | int argc, |
| 97 | sqlite3_value **argv |
| 98 | ){ |
| 99 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 100 | int iDb = argc>=2 ? sqlite3_value_int(argv[1]) : -1; |
| 101 | #ifdef SQLITE_TESTCTRL_EXPIRE_STMTS |
| 102 | sqlite3_test_control(SQLITE_TESTCTRL_EXPIRE_STMTS, db, iDb); |
| 103 | #endif |
| 104 | sqlite3_result_value(context, argv[0]); |
| 105 | } |
| 106 | |
| 107 | #ifdef _WIN32 |
| 108 | __declspec(dllexport) |
| 109 | #endif |
| 110 | int sqlite3_tryeval_init( |
| 111 | sqlite3 *db, |
| 112 | char **pzErrMsg, |
| 113 | const sqlite3_api_routines *pApi |
| 114 | ){ |
| 115 | int rc = SQLITE_OK; |
| 116 | SQLITE_EXTENSION_INIT2(pApi); |
| 117 | (void)pzErrMsg; /* Unused parameter */ |
| 118 | rc = sqlite3_create_function(db, "tryeval", -1, |
| 119 | SQLITE_UTF8, 0, |
| 120 | tryEvalFunc, 0, 0); |
| 121 | if( rc==SQLITE_OK ){ |
| 122 | rc = sqlite3_create_function(db, "expire", 1, |
| 123 | SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, |
| 124 | expireFunc, 0, 0); |
| 125 | } |
| 126 | if( rc==SQLITE_OK ){ |
| 127 | rc = sqlite3_create_function(db, "expire", 2, |
| 128 | SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, |
| 129 | expireFunc, 0, 0); |
| 130 | } |
| 131 | return rc; |
| 132 | } |