forked from mirror/go-sqlite3
Possible to register custom driver
This commit is contained in:
parent
e6690f40af
commit
e6850435ff
|
@ -3,11 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{true, nil})
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3_with_extensions", ":memory:")
|
db, err := sql.Open("sqlite3_with_extensions", ":memory:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
16
sqlite3.go
16
sqlite3.go
|
@ -72,13 +72,13 @@ var SQLiteTimestampFormats = []string{
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sql.Register("sqlite3", &SQLiteDriver{false})
|
sql.Register("sqlite3", &SQLiteDriver{false, nil})
|
||||||
sql.Register("sqlite3_with_extensions", &SQLiteDriver{true})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver struct.
|
// Driver struct.
|
||||||
type SQLiteDriver struct {
|
type SQLiteDriver struct {
|
||||||
enableLoadExtentions bool
|
EnableLoadExtentions bool
|
||||||
|
ConnectHook func(*SQLiteConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conn struct.
|
// Conn struct.
|
||||||
|
@ -179,7 +179,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
enableLoadExtentions := 0
|
enableLoadExtentions := 0
|
||||||
if d.enableLoadExtentions {
|
if d.EnableLoadExtentions {
|
||||||
enableLoadExtentions = 1
|
enableLoadExtentions = 1
|
||||||
}
|
}
|
||||||
rv = C.sqlite3_enable_load_extension(db, C.int(enableLoadExtentions))
|
rv = C.sqlite3_enable_load_extension(db, C.int(enableLoadExtentions))
|
||||||
|
@ -187,7 +187,13 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
||||||
return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
|
return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SQLiteConn{db}, nil
|
conn := &SQLiteConn{db}
|
||||||
|
|
||||||
|
if d.ConnectHook != nil {
|
||||||
|
d.ConnectHook(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the connection.
|
// Close the connection.
|
||||||
|
|
Loading…
Reference in New Issue