Support vfs for Open (#877)

Closes #876
This commit is contained in:
mattn 2020-11-17 01:54:21 +09:00 committed by GitHub
parent 3fb3c0de37
commit 1fbedab173
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -1041,6 +1041,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
secureDelete := "DEFAULT"
synchronousMode := "NORMAL"
writableSchema := -1
vfsName := ""
pos := strings.IndexRune(dsn, '?')
if pos >= 1 {
@ -1364,6 +1365,10 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
}
}
if val := params.Get("vfs"); val != "" {
vfsName = val
}
if !strings.HasPrefix(dsn, "file:") {
dsn = dsn[:pos]
}
@ -1372,9 +1377,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
var db *C.sqlite3
name := C.CString(dsn)
defer C.free(unsafe.Pointer(name))
var vfs *C.char
if vfsName != "" {
vfs = C.CString(vfsName)
defer C.free(unsafe.Pointer(vfs))
}
rv := C._sqlite3_open_v2(name, &db,
mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
nil)
vfs)
if rv != 0 {
// Save off the error _before_ closing the database.
// This is safe even if db is nil.

View File

@ -19,6 +19,7 @@ import (
"os"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
@ -101,6 +102,43 @@ func TestOpen(t *testing.T) {
}
}
func TestOpenWithVFS(t *testing.T) {
filename := t.Name() + ".sqlite"
if err := os.Remove(filename); err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
defer os.Remove(filename)
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?vfs=hello", filename))
if err != nil {
t.Fatal("Failed to open", err)
}
err = db.Ping()
if err == nil {
t.Fatal("Failed to open", err)
}
db.Close()
defer os.Remove(filename)
var vfs string
if runtime.GOOS == "windows" {
vfs = "win32-none"
} else {
vfs = "unix-none"
}
db, err = sql.Open("sqlite3", fmt.Sprintf("file:%s?vfs=%s", filename, vfs))
if err != nil {
t.Fatal("Failed to open", err)
}
err = db.Ping()
if err != nil {
t.Fatal("Failed to ping", err)
}
db.Close()
}
func TestOpenNoCreate(t *testing.T) {
filename := t.Name() + ".sqlite"