From 1cb59310e57976055d6c5e82735a730707373457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Hock=20Isaza?= Date: Thu, 24 Jan 2019 17:04:51 -0500 Subject: [PATCH] Add `OpenFunc()` function This commit adds the `OpenFunc()` function that allows us to handle how the DB connection will be opened. You can pass any function that has the same signature as `sql.Open()` and it'll use it to start the connection. The idea is to open the connections with DataDog [SQL wrapper][1] to start collecting APM metrics for our queries. [1]: https://godoc.org/gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql --- db.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/db.go b/db.go index fca8ff4..7427880 100644 --- a/db.go +++ b/db.go @@ -25,7 +25,12 @@ type Db struct { // Open opens a new database connection. func Open(driverName, dataSourceName string) (*Db, error) { - db, err := sql.Open(driverName, dataSourceName) + return OpenFunc(driverName, dataSourceName, sql.Open) +} + +// OpenFunc opens a new database connection by using the passed `fn`. +func OpenFunc(driverName, dataSourceName string, fn func(string, string) (*sql.DB, error)) (*Db, error) { + db, err := fn(driverName, dataSourceName) if err != nil { return nil, err }