Skip to content

Commit 9eb751e

Browse files
committed
Add doc
1 parent 5fb02bd commit 9eb751e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

doc.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Package sqlite3 provides interface to SQLite3 databases.
3+
4+
This works as driver for database/sql.
5+
6+
Installation
7+
8+
go get github.com/mattn/go-sqlite3
9+
10+
Supported Types
11+
12+
Currently, go-sqlite3 support 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+
29+
You can write your own extension module for sqlite3. For example, below is a
30+
extension for Regexp matcher operation.
31+
32+
#include <pcre.h>
33+
#include <string.h>
34+
#include <stdio.h>
35+
#include <sqlite3ext.h>
36+
37+
SQLITE_EXTENSION_INIT1
38+
static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
39+
if (argc >= 2) {
40+
const char *target = (const char *)sqlite3_value_text(argv[1]);
41+
const char *pattern = (const char *)sqlite3_value_text(argv[0]);
42+
const char* errstr = NULL;
43+
int erroff = 0;
44+
int vec[500];
45+
int n, rc;
46+
pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL);
47+
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
48+
if (rc <= 0) {
49+
sqlite3_result_error(context, errstr, 0);
50+
return;
51+
}
52+
sqlite3_result_int(context, 1);
53+
}
54+
}
55+
56+
#ifdef _WIN32
57+
__declspec(dllexport)
58+
#endif
59+
int sqlite3_extension_init(sqlite3 *db, char **errmsg,
60+
const sqlite3_api_routines *api) {
61+
SQLITE_EXTENSION_INIT2(api);
62+
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8,
63+
(void*)db, regexp_func, NULL, NULL);
64+
}
65+
66+
It need to build as so/dll shared library. And you need to register
67+
extension module like below.
68+
69+
sql.Register("sqlite3_with_extensions",
70+
&sqlite3.SQLiteDriver{
71+
Extensions: []string{
72+
"sqlite3_mod_regexp",
73+
},
74+
})
75+
76+
Then, you can use this extension.
77+
78+
rows, err := db.Query("select text from mytable where name regexp '^golang'")
79+
80+
Connection Hook
81+
82+
You can hook and inject your codes when connection established. database/sql
83+
doesn't provide the way to get native go-sqlite3 interfaces. So if you want,
84+
you need to hook ConnectHook and get the SQLiteConn.
85+
86+
sql.Register("sqlite3_with_hook_example",
87+
&sqlite3.SQLiteDriver{
88+
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
89+
sqlite3conn = append(sqlite3conn, conn)
90+
return nil
91+
},
92+
})
93+
94+
*/
95+
package sqlite3

0 commit comments

Comments
 (0)