You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: interceptor.go
+31-21Lines changed: 31 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -10,53 +10,63 @@ var (
10
10
_ driver.DriverContext=Interceptor{}
11
11
)
12
12
13
-
// TODO: document that database/sql falls back to Prepare if the driver returns ErrSkip for Exec/Query.
14
-
15
-
// Interceptor is a [driver.Driver] wrapper that allows to register callbacks for database queries.
16
-
// It must first be registered with [sql.Register] with the same name that is then passed to [sql.Open]:
13
+
// Interceptor is a [driver.Driver] wrapper that allows to register callbacks for SQL queries.
14
+
// The main use case is to instrument code with logs, metrics, and traces without introducing an [sql.DB] wrapper.
15
+
// An interceptor must first be registered with [sql.Register] using the same name that is then passed to [sql.Open]:
17
16
//
18
17
// interceptor := queries.Interceptor{...}
19
18
// sql.Register("interceptor", interceptor)
20
19
// db, err := sql.Open("interceptor", "dsn")
20
+
//
21
+
// Only the Driver field must be set; all callbacks are optional.
22
+
//
23
+
// Note that some drivers only partially implement [driver.ExecerContext] and [driver.QueryerContext].
24
+
// A driver may return [driver.ErrSkip], which [sql.DB] interprets as a signal to fall back to a prepared statement.
25
+
// For example, the [go-sql-driver/mysql] driver only executes a query within [sql.DB.ExecContext] or [sql.DB.QueryContext] if the query has no arguments.
26
+
// Otherwise, it prepares a [driver.Stmt] using [driver.ConnPrepareContext], executes it, and closes it.
27
+
// In such cases, you may want to implement both the PrepareContext and ExecContext/QueryContext callbacks,
28
+
// even if you don't prepare statements manually via [sql.DB.PrepareContext].
29
+
// TODO: provide an example of such an implementation.
0 commit comments