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

doc.go: you can use Conn.Raw to get *SQLiteConn #882

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
16 changes: 13 additions & 3 deletions doc.go
Expand Up @@ -79,9 +79,8 @@ Then, you can use this extension.

Connection Hook

You can hook and inject your code when the connection is established. database/sql
doesn't provide a way to get native go-sqlite3 interfaces. So if you want,
you need to set ConnectHook and get the SQLiteConn.
You can hook and inject your code when the connection is established by setting
ConnectHook to get the SQLiteConn.

sql.Register("sqlite3_with_hook_example",
&sqlite3.SQLiteDriver{
Expand All @@ -91,6 +90,17 @@ you need to set ConnectHook and get the SQLiteConn.
},
})

You can also use database/sql.Conn.Raw:

conn, err := db.Conn(context.Background())
// if err != nil { ... }
defer conn.Close()
err = conn.Raw(func (driverConn interface{}) error {
sqliteConn := driverConn.(*sqlite3.SQLiteConn)
// ... use sqliteConn
})
// if err != nil { ... }

Go SQlite3 Extensions

If you want to register Go functions as SQLite extension functions,
Expand Down
30 changes: 30 additions & 0 deletions sqlite3_test.go
Expand Up @@ -9,6 +9,7 @@ package sqlite3

import (
"bytes"
"context"
"database/sql"
"database/sql/driver"
"errors"
Expand Down Expand Up @@ -2352,3 +2353,32 @@ func benchmarkStmtRows(b *testing.B) {
}
}
}

func TestConnRawIsSQLiteConn(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal("Failed to open db:", err)
}
defer db.Close()

conn, err := db.Conn(context.Background())
if err != nil {
t.Fatal("Failed to get conn:", err)
}
defer conn.Close()
err = conn.Raw(func(driverConn interface{}) error {
sqliteConn, ok := driverConn.(*SQLiteConn)
if !ok {
t.Errorf("driverConn type=%T; expected to be *SQLiteConn", driverConn)
return nil
}
// call a private SQLite API to confirm the raw conn "works"
if sqliteConn.AuthEnabled() {
t.Error("sqliteConn.AuthEnabled()=true; expected false")
}
return nil
})
if err != nil {
t.Error("conn.Raw() returned err:", err)
}
}