Skip to content

Commit

Permalink
Added a ConnectContext method
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Dec 30, 2016
1 parent fe3256d commit 909e40f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions sqlx_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"reflect"
)

// ConnectContext to a database and verify with a ping.
func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error) {
db, err := Open(driverName, dataSourceName)
if err != nil {
return db, err
}
err = db.PingContext(ctx)
return db, err
}

// QueryerContext is an interface used by GetContext and SelectContext
type QueryerContext interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Expand Down
2 changes: 1 addition & 1 deletion sqlx_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ func TestUsageContext(t *testing.T) {
// tests that sqlx will not panic when the wrong driver is passed because
// of an automatic nil dereference in sqlx.Open(), which was fixed.
func TestDoNotPanicOnConnectContext(t *testing.T) {
_, err := Connect("bogus", "hehe")
_, err := ConnectContext(context.Background(), "bogus", "hehe")
if err == nil {
t.Errorf("Should return error when using bogus driverName")
}
Expand Down

0 comments on commit 909e40f

Please sign in to comment.