I am trying to write custom sqlite3 extension in cpp.Not much hands on cpp. I have written a extension below:-
#include <regex>
#include <string>
#include <iostream>
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
static void regex_match_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
if (argc >= 2) {
const char *pattern = reinterpret_cast<const char *>(sqlite3_value_text(argv[0]));
const char *target = reinterpret_cast<const char *>(sqlite3_value_text(argv[1]));
try {
std::regex re(pattern);
bool match = std::regex_match(target, re);
sqlite3_result_int(context, match ? 1 : 0);
} catch (const std::regex_error& e) {
sqlite3_result_error(context, e.what(), -1);
}
} else {
sqlite3_result_error(context, "Two arguments required: pattern and target", -1);
}
}
//entry point for
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_extension_init(sqlite3 *db, char **errmsg,
const sqlite3_api_routines *api) {
SQLITE_EXTENSION_INIT2(api);
return sqlite3_create_function(db, "regex_match", 3, SQLITE_UTF8,
(void*)db, regex_match_func, NULL, NULL);
}
my scenario is that I want to run regex_match() queries on db select operation I am going to perform form go lang
example query:-
SELECT DISTINCT uid FROM regtable WHERE ( lower(reg_key) like regex_match (lower(reg_key), lower('\REGISTRY\MACHINE\SYSTEM\CONTROLSET[0-9]+\CONTROL\SECURITYPROVIDERS\SECURITYPROVIDERS'), 0)
Now when I try to load extension getting below error
sqlite> .load ./sqlite3_regex_match
Error: The specified procedure could not be found.
Can anyone help me here. This is very new topic for me.