aboutsummaryrefslogtreecommitdiff
path: root/example/extension/sqlite3_mod_regexp.c
diff options
context:
space:
mode:
authormattn <mattn.jp@gmail.com>2013-08-23 09:59:23 +0900
committermattn <mattn.jp@gmail.com>2013-08-23 09:59:23 +0900
commite6690f40af081594fe34a680e8bfb028952c2174 (patch)
treefa613c8e213fa3335e30dabca041065a88da2b43 /example/extension/sqlite3_mod_regexp.c
parentAdd example for sqlite3_with_extensions (diff)
downloadgolite-e6690f40af081594fe34a680e8bfb028952c2174.tar.gz
golite-e6690f40af081594fe34a680e8bfb028952c2174.tar.xz
Add example for sqlite3 extension
Diffstat (limited to 'example/extension/sqlite3_mod_regexp.c')
-rw-r--r--example/extension/sqlite3_mod_regexp.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/example/extension/sqlite3_mod_regexp.c b/example/extension/sqlite3_mod_regexp.c
new file mode 100644
index 0000000..364c146
--- /dev/null
+++ b/example/extension/sqlite3_mod_regexp.c
@@ -0,0 +1,27 @@
+#include <regex.h>
+#include <string.h>
+#include <stdio.h>
+#include <sqlite3ext.h>
+
+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);
+ }
+}
+__declspec(dllexport) 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);
+}