Documentation ¶
Overview ¶
This example shows how to instrument sql queries in order to display the time that they consume package main
import (
"context" "database/sql" "fmt" "time" "github.com/gchaincl/sqlhooks/v2" "github.com/mattn/go-sqlite3"
)
// Hooks satisfies the sqlhook.Hooks interface type Hooks struct {}
// Before hook will print the query with it's args and return the context with the timestamp
func (h *Hooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) { fmt.Printf("> %s %q", query, args) return context.WithValue(ctx, "begin", time.Now()), nil }
// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *Hooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) { begin := ctx.Value("begin").(time.Time) fmt.Printf(". took: %s\n", time.Since(begin)) return ctx, nil }
func main() { // First, register the wrapper sql.Register("sqlite3WithHooks", sqlhooks.Wrap(&sqlite3.SQLiteDriver{}, &Hooks{})) // Connect to the registered wrapped driver db, _ := sql.Open("sqlite3WithHooks", ":memory:") // Do you're stuff db.Exec("CREATE TABLE t (id INTEGER, text VARCHAR(16))") db.Exec("INSERT into t (text) VALUES(?), (?)", "foo", "bar") db.Query("SELECT id, text FROM t") }
/* Output should look like: > CREATE TABLE t (id INTEGER, text VARCHAR(16)) []. took: 121.238µs > INSERT into t (text) VALUES(?), (?) ["foo" "bar"]. took: 36.364µs > SELECT id, text FROM t []. took: 4.653µs */
Index ¶
- func Wrap(driver driver.Driver, hooks Hooks) driver.Driver
- type Conn
- func (conn *Conn) Begin() (driver.Tx, error)
- func (conn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)
- func (conn *Conn) Close() error
- func (conn *Conn) Prepare(query string) (driver.Stmt, error)
- func (conn *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)
- type Driver
- type ErrorHook
- type ExecerContext
- type ExecerQueryerContext
- type ExecerQueryerContextWithSessionResetter
- type Hook
- type Hooks
- type MultipleErrors
- type OnErrorer
- type QueryerContext
- type SessionResetter
- type Stmt
- func (stmt *Stmt) Close() error
- func (stmt *Stmt) Exec(args []driver.Value) (driver.Result, error)
- func (stmt *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)
- func (stmt *Stmt) NumInput() int
- func (stmt *Stmt) Query(args []driver.Value) (driver.Rows, error)
- func (stmt *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ExecerContext ¶
type ExecerContext struct {
*Conn
}
ExecerContext implements a database/sql.driver.ExecerContext
func (*ExecerContext) ExecContext ¶
func (conn *ExecerContext) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
type ExecerQueryerContext ¶
type ExecerQueryerContext struct { *Conn *ExecerContext *QueryerContext }
ExecerQueryerContext implements database/sql.driver.ExecerContext and database/sql.driver.QueryerContext
type ExecerQueryerContextWithSessionResetter ¶
type ExecerQueryerContextWithSessionResetter struct { *Conn *ExecerContext *QueryerContext *SessionResetter }
ExecerQueryerContext implements database/sql.driver.ExecerContext and database/sql.driver.QueryerContext
type Hooks ¶
type Hooks interface { Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) }
Hooks instances may be passed to Wrap() to define an instrumented driver
func Compose ¶
Compose allows for composing multiple Hooks into one. It runs every callback on every hook in argument order, even if previous hooks return an error. If multiple hooks return errors, the error return value will be MultipleErrors, which allows for introspecting the errors if necessary.
type MultipleErrors ¶
type MultipleErrors []error
MultipleErrors is an error that contains multiple errors.
func (MultipleErrors) Error ¶
func (m MultipleErrors) Error() string
type OnErrorer ¶
type OnErrorer interface {
OnError(ctx context.Context, err error, query string, args ...interface{}) error
}
OnErrorer instances will be called if any error happens
type QueryerContext ¶
type QueryerContext struct {
*Conn
}
QueryerContext implements a database/sql.driver.QueryerContext
func (*QueryerContext) QueryContext ¶
func (conn *QueryerContext) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
type SessionResetter ¶
type SessionResetter struct {
*Conn
}
func (*SessionResetter) ResetSession ¶
func (s *SessionResetter) ResetSession(ctx context.Context) error
type Stmt ¶
Stmt implements a database/sql/driver.Stmt