Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support vfs for Open #877

Merged
merged 1 commit into from Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion sqlite3.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
}
Expand All @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions sqlite3_test.go
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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"

Expand Down