-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockstore_cgo.go
53 lines (44 loc) · 1.28 KB
/
blockstore_cgo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//go:build cgo
package sqlite3bs
import (
"database/sql"
"fmt"
"sync/atomic"
"github.com/mattn/go-sqlite3"
)
// counter of sqlite drivers registered; guarded by atomic.
var counter int64
// Open creates a new sqlite3-backed blockstore.
func Open(path string, _ Options) (*Blockstore, error) {
driver := fmt.Sprintf("sqlite3_blockstore_%d", atomic.AddInt64(&counter, 1))
sql.Register(driver,
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
// Execute pragmas on connection creation.
for _, p := range pragmas {
if _, err := conn.Exec(p, nil); err != nil {
return fmt.Errorf("failed to execute sqlite3 init pragma: %s; err: %w", p, err)
}
}
return nil
},
})
db, err := sql.Open(driver, path+"?mode=rwc")
if err != nil {
return nil, fmt.Errorf("failed to open sqlite3 database: %w", err)
}
// Execute init DDLs.
for _, ddl := range initDDL {
if _, err := db.Exec(ddl); err != nil {
return nil, fmt.Errorf("failed to execute sqlite3 init DDL: %s; err: %w", ddl, err)
}
}
bs := &Blockstore{db: db}
// Prepare all statements.
for i, p := range statements {
if bs.prepared[i], err = db.Prepare(p); err != nil {
return nil, fmt.Errorf("failed to prepare statement: %s; err: %w", p, err)
}
}
return bs, nil
}