Support overriding the database file.

This commit is contained in:
Qi Xiao 2016-02-13 00:35:33 +01:00
parent f454cfb386
commit 56490fcd31
2 changed files with 10 additions and 5 deletions

View File

@ -39,6 +39,7 @@ func usage() {
var (
debuglog = flag.String("debuglog", "", "a file to write debug log to")
dbname = flag.String("db", "", "path to the database")
help = flag.Bool("help", false, "show usage help and quit")
)
@ -212,7 +213,11 @@ func newEvalerAndStore() (*eval.Evaler, *store.Store) {
var st *store.Store
if err == nil {
st, err = store.NewStore(dataDir)
db := *dbname
if db == "" {
db = dataDir + "/db"
}
st, err = store.NewStore(db)
if err != nil {
fmt.Fprintln(os.Stderr, "Warning: cannot connect to store:", err)
}

View File

@ -17,15 +17,15 @@ type Store struct {
var initTable = map[string](func(*sql.DB) error){}
// DefaultDB returns the default database for storage.
func DefaultDB(dataDir string) (*sql.DB, error) {
uri := "file:" + url.QueryEscape(dataDir+"/db") +
func DefaultDB(dbname string) (*sql.DB, error) {
uri := "file:" + url.QueryEscape(dbname) +
"?mode=rwc&cache=shared&vfs=unix-dotfile"
return sql.Open("sqlite3", uri)
}
// NewStore creates a new Store with the default database.
func NewStore(dataDir string) (*Store, error) {
db, err := DefaultDB(dataDir)
func NewStore(dbname string) (*Store, error) {
db, err := DefaultDB(dbname)
if err != nil {
return nil, err
}