diff options
Diffstat (limited to 'dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp')
3 files changed, 105 insertions, 0 deletions
diff --git a/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/Makefile b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/Makefile new file mode 100644 index 0000000..1ef69a6 --- /dev/null +++ b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/Makefile @@ -0,0 +1,27 @@ +ifeq ($(OS),Windows_NT) +EXE=extension.exe +LIB_EXT=dll +RM=cmd /c del +LDFLAG= +else +EXE=extension +ifeq ($(shell uname -s),Darwin) +LIB_EXT=dylib +else +LIB_EXT=so +endif +RM=rm -f +LDFLAG=-fPIC +endif +LIB=sqlite3_mod_regexp.$(LIB_EXT) + +all : $(EXE) $(LIB) + +$(EXE) : extension.go + go build $< + +$(LIB) : sqlite3_mod_regexp.c + gcc $(LDFLAG) -shared -o $@ $< -lsqlite3 -lpcre + +clean : + @-$(RM) $(EXE) $(LIB) diff --git a/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/extension.go b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/extension.go new file mode 100644 index 0000000..61ceb55 --- /dev/null +++ b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/extension.go @@ -0,0 +1,43 @@ +package main + +import ( + "database/sql" + "fmt" + "github.com/mattn/go-sqlite3" + "log" +) + +func main() { + sql.Register("sqlite3_with_extensions", + &sqlite3.SQLiteDriver{ + Extensions: []string{ + "sqlite3_mod_regexp", + }, + }) + + db, err := sql.Open("sqlite3_with_extensions", ":memory:") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + // Force db to make a new connection in pool + // by putting the original in a transaction + tx, err := db.Begin() + if err != nil { + log.Fatal(err) + } + defer tx.Commit() + + // New connection works (hopefully!) + rows, err := db.Query("select 'hello world' where 'hello world' regexp '^hello.*d$'") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + for rows.Next() { + var helloworld string + rows.Scan(&helloworld) + fmt.Println(helloworld) + } +} diff --git a/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/sqlite3_mod_regexp.c b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/sqlite3_mod_regexp.c new file mode 100644 index 0000000..d3ad149 --- /dev/null +++ b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/mod_regexp/sqlite3_mod_regexp.c @@ -0,0 +1,35 @@ +#include <pcre.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); + if (!re) { + sqlite3_result_error(context, errstr, 0); + return; + } + rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); + if (rc <= 0) { + sqlite3_result_int(context, 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); +} |