podman
134 строки · 3.5 Кб
1/*
2Package sqlite3 provides interface to SQLite3 databases.
3
4This works as a driver for database/sql.
5
6Installation
7
8go get github.com/mattn/go-sqlite3
9
10# Supported Types
11
12Currently, go-sqlite3 supports the following data types.
13
14+------------------------------+
15|go | sqlite3 |
16|----------|-------------------|
17|nil | null |
18|int | integer |
19|int64 | integer |
20|float64 | float |
21|bool | integer |
22|[]byte | blob |
23|string | text |
24|time.Time | timestamp/datetime|
25+------------------------------+
26
27# SQLite3 Extension
28
29You can write your own extension module for sqlite3. For example, below is an
30extension for a Regexp matcher operation.
31
32#include <pcre.h>
33#include <string.h>
34#include <stdio.h>
35#include <sqlite3ext.h>
36
37SQLITE_EXTENSION_INIT1
38static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
39if (argc >= 2) {
40const char *target = (const char *)sqlite3_value_text(argv[1]);
41const char *pattern = (const char *)sqlite3_value_text(argv[0]);
42const char* errstr = NULL;
43int erroff = 0;
44int vec[500];
45int n, rc;
46pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL);
47rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
48if (rc <= 0) {
49sqlite3_result_error(context, errstr, 0);
50return;
51}
52sqlite3_result_int(context, 1);
53}
54}
55
56#ifdef _WIN32
57__declspec(dllexport)
58#endif
59int sqlite3_extension_init(sqlite3 *db, char **errmsg,
60const sqlite3_api_routines *api) {
61SQLITE_EXTENSION_INIT2(api);
62return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8,
63(void*)db, regexp_func, NULL, NULL);
64}
65
66It needs to be built as a so/dll shared library. And you need to register
67the extension module like below.
68
69sql.Register("sqlite3_with_extensions",
70&sqlite3.SQLiteDriver{
71Extensions: []string{
72"sqlite3_mod_regexp",
73},
74})
75
76Then, you can use this extension.
77
78rows, err := db.Query("select text from mytable where name regexp '^golang'")
79
80# Connection Hook
81
82You can hook and inject your code when the connection is established by setting
83ConnectHook to get the SQLiteConn.
84
85sql.Register("sqlite3_with_hook_example",
86&sqlite3.SQLiteDriver{
87ConnectHook: func(conn *sqlite3.SQLiteConn) error {
88sqlite3conn = append(sqlite3conn, conn)
89return nil
90},
91})
92
93You can also use database/sql.Conn.Raw (Go >= 1.13):
94
95conn, err := db.Conn(context.Background())
96// if err != nil { ... }
97defer conn.Close()
98err = conn.Raw(func (driverConn any) error {
99sqliteConn := driverConn.(*sqlite3.SQLiteConn)
100// ... use sqliteConn
101})
102// if err != nil { ... }
103
104# Go SQlite3 Extensions
105
106If you want to register Go functions as SQLite extension functions
107you can make a custom driver by calling RegisterFunction from
108ConnectHook.
109
110regex = func(re, s string) (bool, error) {
111return regexp.MatchString(re, s)
112}
113sql.Register("sqlite3_extended",
114&sqlite3.SQLiteDriver{
115ConnectHook: func(conn *sqlite3.SQLiteConn) error {
116return conn.RegisterFunc("regexp", regex, true)
117},
118})
119
120You can then use the custom driver by passing its name to sql.Open.
121
122var i int
123conn, err := sql.Open("sqlite3_extended", "./foo.db")
124if err != nil {
125panic(err)
126}
127err = db.QueryRow(`SELECT regexp("foo.*", "seafood")`).Scan(&i)
128if err != nil {
129panic(err)
130}
131
132See the documentation of RegisterFunc for more details.
133*/
134package sqlite3
135