forked from mirror/go-sqlite3
parent
3fb3c0de37
commit
1fbedab173
12
sqlite3.go
12
sqlite3.go
|
@ -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.
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
Loading…
Reference in New Issue