carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostbox
2
3import (
4 "database/sql"
5 "net/url"
6 "path/filepath"
7)
8
9func OpenStopInventoryDB(path string) (*StopInventoryDB, error) {
10 if !filepath.IsAbs(path) {
11 return nil, ErrTypedInventory
12 }
13 if err := checkJournalPath(path); err != nil {
14 return nil, err
15 }
16 // busy_timeout is a connection DSN pragma, in force before the first
17 // statement and on any replacement connection.
18 db, err := sql.Open("sqlite", "file:"+url.PathEscape(path)+"?_pragma=busy_timeout(5000)")
19 if err != nil {
20 return nil, err
21 }
22 db.SetMaxOpenConns(1)
23 for _, q := range []string{"PRAGMA busy_timeout=5000", "PRAGMA journal_mode=WAL", inventoryVersionsSQL, "PRAGMA synchronous=FULL", "CREATE TABLE IF NOT EXISTS stop_inventory(host TEXT NOT NULL,incarnation TEXT NOT NULL,stamp INTEGER NOT NULL,body BLOB NOT NULL,PRIMARY KEY(host,incarnation,stamp))"} {
24 if _, err := db.Exec(q); err != nil {
25 db.Close()
26 return nil, err
27 }
28 }
29 return &StopInventoryDB{db: db}, nil
30}
31