2014-07-04 00:54:28 +00:00
|
|
|
/*
|
|
|
|
Package sqlite3 provides interface to SQLite3 databases.
|
|
|
|
|
2016-02-23 06:20:57 +00:00
|
|
|
This works as a driver for database/sql.
|
2014-07-04 00:54:28 +00:00
|
|
|
|
|
|
|
Installation
|
|
|
|
|
|
|
|
go get github.com/mattn/go-sqlite3
|
|
|
|
|
|
|
|
Supported Types
|
|
|
|
|
2016-02-23 06:20:57 +00:00
|
|
|
Currently, go-sqlite3 supports the following data types.
|
2014-07-04 00:54:28 +00:00
|
|
|
|
|
|
|
+------------------------------+
|
|
|
|
|go | sqlite3 |
|
|
|
|
|----------|-------------------|
|
|
|
|
|nil | null |
|
|
|
|
|int | integer |
|
|
|
|
|int64 | integer |
|
|
|
|
|float64 | float |
|
|
|
|
|bool | integer |
|
|
|
|
|[]byte | blob |
|
|
|
|
|string | text |
|
|
|
|
|time.Time | timestamp/datetime|
|
|
|
|
+------------------------------+
|
|
|
|
|
|
|
|
SQLite3 Extension
|
|
|
|
|
2016-02-23 06:20:57 +00:00
|
|
|
You can write your own extension module for sqlite3. For example, below is an
|
|
|
|
extension for a Regexp matcher operation.
|
2014-07-04 00:54:28 +00:00
|
|
|
|
|
|
|
#include <pcre.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sqlite3ext.h>
|
2015-08-21 06:08:48 +00:00
|
|
|
|
2014-07-04 00:54:28 +00:00
|
|
|
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);
|
2015-08-21 06:08:48 +00:00
|
|
|
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
|
2014-07-04 00:54:28 +00:00
|
|
|
if (rc <= 0) {
|
|
|
|
sqlite3_result_error(context, errstr, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sqlite3_result_int(context, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 06:08:48 +00:00
|
|
|
|
2014-07-04 00:54:28 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2016-02-23 06:20:57 +00:00
|
|
|
It needs to be built as a so/dll shared library. And you need to register
|
|
|
|
the extension module like below.
|
2014-07-04 00:54:28 +00:00
|
|
|
|
|
|
|
sql.Register("sqlite3_with_extensions",
|
|
|
|
&sqlite3.SQLiteDriver{
|
|
|
|
Extensions: []string{
|
|
|
|
"sqlite3_mod_regexp",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
Then, you can use this extension.
|
|
|
|
|
|
|
|
rows, err := db.Query("select text from mytable where name regexp '^golang'")
|
|
|
|
|
|
|
|
Connection Hook
|
|
|
|
|
2020-11-16 14:42:00 +00:00
|
|
|
You can hook and inject your code when the connection is established by setting
|
|
|
|
ConnectHook to get the SQLiteConn.
|
2014-07-04 00:54:28 +00:00
|
|
|
|
|
|
|
sql.Register("sqlite3_with_hook_example",
|
|
|
|
&sqlite3.SQLiteDriver{
|
|
|
|
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
|
|
|
sqlite3conn = append(sqlite3conn, conn)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-11-16 16:52:26 +00:00
|
|
|
You can also use database/sql.Conn.Raw (Go >= 1.13):
|
2020-11-16 14:42:00 +00:00
|
|
|
|
|
|
|
conn, err := db.Conn(context.Background())
|
|
|
|
// if err != nil { ... }
|
|
|
|
defer conn.Close()
|
|
|
|
err = conn.Raw(func (driverConn interface{}) error {
|
|
|
|
sqliteConn := driverConn.(*sqlite3.SQLiteConn)
|
|
|
|
// ... use sqliteConn
|
|
|
|
})
|
|
|
|
// if err != nil { ... }
|
|
|
|
|
2015-08-21 06:08:48 +00:00
|
|
|
Go SQlite3 Extensions
|
|
|
|
|
2020-11-16 14:40:44 +00:00
|
|
|
If you want to register Go functions as SQLite extension functions
|
|
|
|
you can make a custom driver by calling RegisterFunction from
|
|
|
|
ConnectHook.
|
2015-08-21 06:08:48 +00:00
|
|
|
|
|
|
|
regex = func(re, s string) (bool, error) {
|
|
|
|
return regexp.MatchString(re, s)
|
|
|
|
}
|
2020-11-16 14:40:44 +00:00
|
|
|
sql.Register("sqlite3_extended",
|
2015-08-21 06:08:48 +00:00
|
|
|
&sqlite3.SQLiteDriver{
|
|
|
|
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
2016-03-24 15:58:39 +00:00
|
|
|
return conn.RegisterFunc("regexp", regex, true)
|
2015-08-21 06:08:48 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-11-16 14:40:44 +00:00
|
|
|
You can then use the custom driver by passing its name to sql.Open.
|
|
|
|
|
|
|
|
var i int
|
|
|
|
conn, err := sql.Open("sqlite3_extended", "./foo.db")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = db.QueryRow(`SELECT regexp("foo.*", "seafood")`).Scan(&i)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-08-21 06:08:48 +00:00
|
|
|
See the documentation of RegisterFunc for more details.
|
|
|
|
|
2014-07-04 00:54:28 +00:00
|
|
|
*/
|
|
|
|
package sqlite3
|