Skip to content

Use proxy_sqlite3_* wrappers and canonical proxy symbol TU#22

Merged
renecannao merged 8 commits intov3.1-MCP2_RAG1from
sqlite3-proxy-replacements
Jan 21, 2026
Merged

Use proxy_sqlite3_* wrappers and canonical proxy symbol TU#22
renecannao merged 8 commits intov3.1-MCP2_RAG1from
sqlite3-proxy-replacements

Conversation

@renecannao
Copy link
Copy Markdown

This PR adds lib/proxy_sqlite3_symbols.cpp to provide a single translation unit that defines proxy_sqlite3_* function pointers, updates lib/Makefile to include it, and removes MAIN_PROXY_SQLITE3 from src/main.cpp to avoid multiple-definition issues. Build was validated with 'make debug'.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 20, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This PR expands the SQLite3 proxy interface by adding 8 new function pointers for blob operations, column data access, and database lifecycle management. It introduces a new module to initialize and bind these proxy functions to native SQLite symbols, integrates proxy wrappers throughout the anomaly detector, and removes legacy test infrastructure.

Changes

Cohort / File(s) Summary
Proxy Interface Extension
include/sqlite3db.h
Added 8 new proxy function pointer declarations in both extern and non-extern sections: bind_blob, column_int64, column_double, last_insert_rowid, errstr, db_handle, enable_load_extension, auto_extension.
Proxy Symbol Bindings
lib/proxy_sqlite3_symbols.cpp
New translation unit implementing 35+ proxy function pointer initializations bound to native SQLite symbols. Includes weak-alias mechanism for proxy_sqlite3_enable_load_extension via proxy_sqlite3_enable_load_extension_alias.
SQLite3 Dynamic Loading
lib/sqlite3db.cpp
Extended dynamic loader with initialization and runtime binding of 8 new proxy pointers; added NULL initializations, symbol assignments from native SQLite, and assertion checks for loaded proxies.
Anomaly Detector Integration
lib/Anomaly_Detector.cpp
Systematically replaced all direct SQLite API calls with proxy wrapper invocations across embedding similarity checks, threat pattern operations, and statistics gathering (25 replacements).
Build Configuration
lib/Makefile
Added proxy_sqlite3_symbols.oo to _OBJ_CXX object compilation list.
Macro Configuration
src/main.cpp
Removed MAIN_PROXY_SQLITE3 preprocessor macro definition.
Test Infrastructure Removal
test/build_rag_test.sh, test/test_rag_schema.cpp
Deleted legacy test build script and RAG schema validation test program.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 Eight proxies hop into the fray,
Binding blobs and columns at play,
SQLite now dances through wrapper gates,
Where symbols bind and the detector awaits. 🎯

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: introducing proxy SQLite3 wrappers and a canonical proxy symbol translation unit.
Description check ✅ Passed The description is related to the changeset, explaining the addition of proxy_sqlite3_symbols.cpp, Makefile updates, and removal of MAIN_PROXY_SQLITE3 macro.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @renecannao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors how SQLite3 functions are accessed within the codebase by centralizing their proxy symbol definitions into a dedicated translation unit. This change addresses and prevents multiple-definition linker errors, ensuring a robust and consistent mechanism for interacting with the SQLite3 library. The modifications involve updating header declarations, adapting existing code to use the new proxied calls, and adjusting the build system accordingly. The overall impact is improved code maintainability and stability for SQLite3 integrations.

Highlights

  • Centralized SQLite3 Proxy Symbol Definitions: A new source file, lib/proxy_sqlite3_symbols.cpp, has been introduced to serve as the canonical translation unit for defining all proxy_sqlite3_* function pointers, resolving potential multiple-definition issues.
  • Expanded SQLite3 Function Proxying: The include/sqlite3db.h header now declares additional proxy_sqlite3_* function pointers, and lib/sqlite3db.cpp has been updated to initialize and assert these new pointers, ensuring comprehensive proxy coverage for SQLite3 interactions.
  • Migration to Proxied SQLite3 Calls: The lib/Anomaly_Detector.cpp file has been refactored to utilize the (*proxy_sqlite3_*) function pointers for all SQLite3 operations, promoting consistent and controlled access to the database API.
  • Build System Updates: The lib/Makefile has been modified to include the new proxy_sqlite3_symbols.cpp in the build process, and the MAIN_PROXY_SQLITE3 macro has been removed from src/main.cpp to prevent linker errors.
  • Test File Removal: Two test files, test/build_rag_test.sh and test/test_rag_schema.cpp, have been removed from the repository.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully refactors the SQLite proxy mechanism by centralizing the definition of proxy_sqlite3_* function pointers into a dedicated translation unit (lib/proxy_sqlite3_symbols.cpp). This change correctly addresses potential multiple-definition issues by ensuring that src/main.cpp no longer defines these symbols directly, but instead relies on the extern declarations in include/sqlite3db.h. All usages of sqlite3_* functions in lib/Anomaly_Detector.cpp have been appropriately updated to use the new proxied function pointers. The lib/Makefile has been updated to include the new source file, and lib/sqlite3db.cpp correctly handles the initialization and assertion of the new function pointers. The removal of test/build_rag_test.sh and test/test_rag_schema.cpp indicates a cleanup or refactoring of test infrastructure, which is acceptable if no longer needed. Overall, the changes are well-implemented and directly resolve the stated problem without introducing new issues.

@renecannao
Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 20, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (4)
lib/Anomaly_Detector.cpp (4)

748-779: Use proxy functions consistently for all SQLite operations on this statement.

Lines 771-772 and 776 call sqlite3_errmsg() and sqlite3_finalize() directly after using proxy functions for prepare, bind, and step operations. This mixing is unsafe in ProxySQL where SQLite symbols may be provided by a plugin. Replace with proxy counterparts.

Proposed fix
-        proxy_error("Anomaly: Failed to insert pattern: %s\n", sqlite3_errmsg(db));
-        sqlite3_finalize(stmt);
+        proxy_error("Anomaly: Failed to insert pattern: %s\n", (*proxy_sqlite3_errmsg)(db));
+        (*proxy_sqlite3_finalize)(stmt);
         return -1;
     }

-    sqlite3_finalize(stmt);
+    (*proxy_sqlite3_finalize)(stmt);

911-939: Keep stats queries entirely on the proxy API.

proxy_sqlite3_step is now used, but sqlite3_prepare_v2, sqlite3_column_int, sqlite3_column_text, and sqlite3_finalize are still direct calls. This mixes implementations in plugin mode. Use proxy functions consistently throughout.

🐛 Proposed fix
-        int rc = sqlite3_prepare_v2(db, count_query, -1, &stmt, NULL);
+        int rc = (*proxy_sqlite3_prepare_v2)(db, count_query, -1, &stmt, NULL);

         if (rc == SQLITE_OK) {
             rc = (*proxy_sqlite3_step)(stmt);
             if (rc == SQLITE_ROW) {
-                stats["threat_patterns_count"] = sqlite3_column_int(stmt, 0);
+                stats["threat_patterns_count"] = (*proxy_sqlite3_column_int)(stmt, 0);
             }
-            sqlite3_finalize(stmt);
+            (*proxy_sqlite3_finalize)(stmt);
         }
@@
-        rc = sqlite3_prepare_v2(db, type_query, -1, &stmt, NULL);
+        rc = (*proxy_sqlite3_prepare_v2)(db, type_query, -1, &stmt, NULL);

         if (rc == SQLITE_OK) {
             json by_type = json::object();
             while ((*proxy_sqlite3_step)(stmt) == SQLITE_ROW) {
-                const char* type = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
-                int count = sqlite3_column_int(stmt, 1);
+                const char* type = reinterpret_cast<const char*>((*proxy_sqlite3_column_text)(stmt, 0));
+                int count = (*proxy_sqlite3_column_int)(stmt, 1);
                 if (type) {
                     by_type[type] = count;
                 }
             }
-            sqlite3_finalize(stmt);
+            (*proxy_sqlite3_finalize)(stmt);
             stats["threat_patterns_by_type"] = by_type;
         }

449-500: Keep statement lifecycle on the same SQLite API surface.

The statement is prepared, stepped, and accessed via proxy functions (proxy_sqlite3_prepare_v2, proxy_sqlite3_step, proxy_sqlite3_column_*) but finalized via direct sqlite3_finalize at line 61. This creates an inconsistency that can cross library boundaries in plugin mode. Use the proxy finalize for consistency with the rest of the statement lifecycle.

🐛 Proposed fix
-    sqlite3_finalize(stmt);
+    (*proxy_sqlite3_finalize)(stmt);

810-836: Use proxy prepare/errmsg/finalize to match proxy step/column calls.

The loop uses proxy_sqlite3_step and proxy column accessors, but the statement preparation, error handling, and finalization use direct sqlite3_* APIs. This mixing breaks the abstraction when SQLite is provided by a plugin. Lines 452 and 755 in the same file correctly use (*proxy_sqlite3_prepare_v2) and (*proxy_sqlite3_errmsg)—follow that pattern here.

🐛 Proposed fix
-    int rc = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
+    int rc = (*proxy_sqlite3_prepare_v2)(db, query, -1, &stmt, NULL);

     if (rc != SQLITE_OK) {
-        proxy_error("Anomaly: Failed to query threat patterns: %s\n", sqlite3_errmsg(db));
+        proxy_error("Anomaly: Failed to query threat patterns: %s\n", (*proxy_sqlite3_errmsg)(db));
         return "[]";
     }
@@
-    sqlite3_finalize(stmt);
+    (*proxy_sqlite3_finalize)(stmt);
🤖 Fix all issues with AI agents
In `@include/sqlite3db.h`:
- Around line 31-44: Add the missing definition for
proxy_sqlite3_enable_load_extension inside the MAIN_PROXY_SQLITE3 block so the
symbol declared as extern (int (*proxy_sqlite3_enable_load_extension)(sqlite3*,
int)) has a corresponding definition; follow the pattern used by the other proxy
symbols in that block (declare/initialize the function pointer there, e.g., to
NULL or wired to the real sqlite3_enable_load_extension during init) so test
compilation units that define MAIN_PROXY_SQLITE3 get the symbol.

Comment on lines +31 to +44
extern int (*proxy_sqlite3_bind_blob)(sqlite3_stmt*, int, const void*, int, void(*)(void*));
extern const char *(*proxy_sqlite3_column_name)(sqlite3_stmt*, int N);
extern const unsigned char *(*proxy_sqlite3_column_text)(sqlite3_stmt*, int iCol);
extern int (*proxy_sqlite3_column_bytes)(sqlite3_stmt*, int iCol);
extern int (*proxy_sqlite3_column_type)(sqlite3_stmt*, int iCol);
extern int (*proxy_sqlite3_column_count)(sqlite3_stmt *pStmt);
extern int (*proxy_sqlite3_column_int)(sqlite3_stmt*, int iCol);
extern sqlite3_int64 (*proxy_sqlite3_column_int64)(sqlite3_stmt*, int iCol);
extern double (*proxy_sqlite3_column_double)(sqlite3_stmt*, int iCol);
extern sqlite3_int64 (*proxy_sqlite3_last_insert_rowid)(sqlite3*);
extern const char *(*proxy_sqlite3_errstr)(int);
extern sqlite3* (*proxy_sqlite3_db_handle)(sqlite3_stmt*);
extern int (*proxy_sqlite3_enable_load_extension)(sqlite3*, int);
extern int (*proxy_sqlite3_auto_extension)(void(*)(void));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

rg -n '\bMAIN_PROXY_SQLITE3\b'

Repository: ProxySQL/proxysql-vec

Length of output: 186


🏁 Script executed:

head -150 include/sqlite3db.h | cat -n

Repository: ProxySQL/proxysql-vec

Length of output: 7784


🏁 Script executed:

rg -n 'define\s+MAIN_PROXY_SQLITE3' -t c -t cpp -t h

Repository: ProxySQL/proxysql-vec

Length of output: 254


🏁 Script executed:

rg -l 'MAIN_PROXY_SQLITE3' | head -20

Repository: ProxySQL/proxysql-vec

Length of output: 186


🏁 Script executed:

sed -n '82,139p' include/sqlite3db.h | grep -n 'enable_load_extension'

Repository: ProxySQL/proxysql-vec

Length of output: 47


🏁 Script executed:

sed -n '82,139p' include/sqlite3db.h

Repository: ProxySQL/proxysql-vec

Length of output: 2902


Add proxy_sqlite3_enable_load_extension definition to the MAIN_PROXY_SQLITE3 block.

The extern declaration exists at line 43, but the MAIN_PROXY_SQLITE3 definition block (lines 82–139) is missing the corresponding definition. Since MAIN_PROXY_SQLITE3 is actively defined in test files (test/tap/tests/setparser_test.cpp, test/tap/tests/sqlite3-t.cpp, test/tap/tests/setparser_test_common.h), this asymmetry will cause missing symbol definitions in those compilation units.

Fix
sqlite3* (*proxy_sqlite3_db_handle)(sqlite3_stmt*);
+int (*proxy_sqlite3_enable_load_extension)(sqlite3*, int);
int (*proxy_sqlite3_auto_extension)(void(*)(void));
🤖 Prompt for AI Agents
In `@include/sqlite3db.h` around lines 31 - 44, Add the missing definition for
proxy_sqlite3_enable_load_extension inside the MAIN_PROXY_SQLITE3 block so the
symbol declared as extern (int (*proxy_sqlite3_enable_load_extension)(sqlite3*,
int)) has a corresponding definition; follow the pattern used by the other proxy
symbols in that block (declare/initialize the function pointer there, e.g., to
NULL or wired to the real sqlite3_enable_load_extension during init) so test
compilation units that define MAIN_PROXY_SQLITE3 get the symbol.

@renecannao renecannao force-pushed the sqlite3-proxy-replacements branch from da030a8 to 6ce0538 Compare January 21, 2026 01:21
@renecannao renecannao merged commit ceaaa01 into v3.1-MCP2_RAG1 Jan 21, 2026
1 check passed
renecannao added a commit that referenced this pull request Jan 22, 2026
This addresses an issue from PR #22 where LoadPlugin() was completely
disabled. The function performs necessary initialization even when no
plugin is loaded (initializes built-in sqlite3 function pointers).

Changes:
- Added `const bool allow_load_plugin = false` flag in LoadPlugin()
- Modified `if (plugin_name)` to `if (plugin_name && allow_load_plugin == true)`
- Re-enabled the LoadPlugin() call in LoadPlugins()

The plugin loading code remains disabled (allow_load_plugin=false) while
the function pointer initialization from built-in SQLite3 now works correctly.

TODO: Revisit plugin loading safety mechanism to allow actual plugin loading.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant