From 40e248789e4ece0bab8680434a36a9a6dc43139e Mon Sep 17 00:00:00 2001 From: s441433 Date: Fri, 28 Dec 2018 22:14:29 +0100 Subject: [PATCH] Create sqlite3_mod_regexp.c --- .../_example/mod_regexp/sqlite3_mod_regexp.c | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/github.com/mattn/go-sqlite3/_example/mod_regexp/sqlite3_mod_regexp.c diff --git a/src/github.com/mattn/go-sqlite3/_example/mod_regexp/sqlite3_mod_regexp.c b/src/github.com/mattn/go-sqlite3/_example/mod_regexp/sqlite3_mod_regexp.c new file mode 100644 index 0000000..277764d --- /dev/null +++ b/src/github.com/mattn/go-sqlite3/_example/mod_regexp/sqlite3_mod_regexp.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +SQLITE_EXTENSION_INIT1 +static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) { + if (argc >= 2) { + const char *target = (const char *)sqlite3_value_text(argv[1]); + const char *pattern = (const char *)sqlite3_value_text(argv[0]); + const char* errstr = NULL; + int erroff = 0; + int vec[500]; + int n, rc; + pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL); + rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); + if (rc <= 0) { + sqlite3_result_error(context, errstr, 0); + return; + } + sqlite3_result_int(context, 1); + } +} + +#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, "regexp", 2, SQLITE_UTF8, (void*)db, regexp_func, NULL, NULL); +}