package hostbox import ( "database/sql" "net/url" "path/filepath" ) func OpenStopInventoryDB(path string) (*StopInventoryDB, error) { if !filepath.IsAbs(path) { return nil, ErrTypedInventory } if err := checkJournalPath(path); err != nil { return nil, err } // busy_timeout is a connection DSN pragma, in force before the first // statement and on any replacement connection. db, err := sql.Open("sqlite", "file:"+url.PathEscape(path)+"?_pragma=busy_timeout(5000)") if err != nil { return nil, err } db.SetMaxOpenConns(1) 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))"} { if _, err := db.Exec(q); err != nil { db.Close() return nil, err } } return &StopInventoryDB{db: db}, nil }