Documentation
¶
Index ¶
- Variables
- func NewConnector(c driver.Connector, hs ...*HooksContext) driver.Connector
- func RegisterProxy()
- func RegisterTracer()
- func WithHooks(ctx context.Context, hs ...*HooksContext) context.Context
- type Conn
- func (conn *Conn) Begin() (driver.Tx, error)
- func (conn *Conn) BeginTx(c context.Context, opts driver.TxOptions) (driver.Tx, error)
- func (conn *Conn) CheckNamedValue(nv *driver.NamedValue) (err error)
- func (conn *Conn) Close() error
- func (conn *Conn) Exec(query string, args []driver.Value) (driver.Result, error)
- func (conn *Conn) ExecContext(c context.Context, query string, args []driver.NamedValue) (driver.Result, error)
- func (conn *Conn) IsValid() bool
- func (conn *Conn) Ping(c context.Context) error
- func (conn *Conn) Prepare(query string) (driver.Stmt, error)
- func (conn *Conn) PrepareContext(c context.Context, query string) (driver.Stmt, error)
- func (conn *Conn) Query(query string, args []driver.Value) (driver.Rows, error)
- func (conn *Conn) QueryContext(c context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
- func (conn *Conn) ResetSession(ctx context.Context) error
- type Connector
- type Filter
- type Hooks
- type HooksContext
- type Outputter
- type PackageFilter
- type Proxy
- type Stmt
- func (stmt *Stmt) CheckNamedValue(nv *driver.NamedValue) (err error)
- func (stmt *Stmt) Close() error
- func (stmt *Stmt) ColumnConverter(idx int) driver.ValueConverter
- func (stmt *Stmt) Exec(args []driver.Value) (driver.Result, error)
- func (stmt *Stmt) ExecContext(c 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(c context.Context, args []driver.NamedValue) (driver.Rows, error)
- type TracerOptions
- type Tx
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultPackageFilter = PackageFilter{
"database/sql": struct{}{},
"github.com/shogo82148/go-sql-proxy": struct{}{},
}
DefaultPackageFilter ignores some database util package.
Functions ¶
func NewConnector ¶
func NewConnector(c driver.Connector, hs ...*HooksContext) driver.Connector
NewConnector creates new proxied Connector.
func RegisterProxy ¶
func RegisterProxy()
RegisterProxy creates proxies that do not anything by default, and registers the proxies as sql driver. Use `proxy.WithHooks(ctx, hooks)` to hook query execution. The proxies' names have suffix ":proxy".
Example ¶
package main
import (
"context"
"database/sql"
"fmt"
"log"
proxy "github.com/cdleo/go-sql-proxy"
)
func main() {
proxy.RegisterProxy()
db, err := sql.Open("fakedb:proxy", `{"name":"trace"}`)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// proxy.RegisterProxy register hook points.
// do nothing by default.
if err := db.PingContext(ctx); err != nil {
log.Fatal(err)
}
// proxy.WithHooks enables the hooks in this context.
ctx = proxy.WithHooks(context.Background(), &proxy.HooksContext{
Ping: func(c context.Context, ctx interface{}, conn *proxy.Conn) error {
fmt.Println("Ping")
return nil
},
})
if err := db.PingContext(ctx); err != nil {
log.Fatal(err)
}
if err := db.Close(); err != nil {
log.Fatal(err)
}
}
Output: Ping
func RegisterTracer ¶
func RegisterTracer()
RegisterTracer creates proxies that log queries from the sql drivers already registered, and registers the proxies as sql driver. The proxies' names have suffix ":trace".
Types ¶
type Conn ¶
Conn adds hook points into "database/sql/driver".Conn.
func (*Conn) Begin ¶
Begin starts and returns a new transaction which is wrapped by Tx. It will trigger PreBegin, Begin, PostBegin hooks. NOT SUPPORTED: use BeginContext instead
func (*Conn) BeginTx ¶
BeginTx starts and returns a new transaction which is wrapped by Tx. It will trigger PreBegin, Begin, PostBegin hooks.
func (*Conn) CheckNamedValue ¶
func (conn *Conn) CheckNamedValue(nv *driver.NamedValue) (err error)
CheckNamedValue for implementing driver.NamedValueChecker This function may be unnecessary because `proxy.Stmt` already implements `NamedValueChecker`, but it is implemented just in case.
func (*Conn) Exec ¶
Exec calls the original Exec method of the connection. It will trigger PreExec, Exec, PostExec hooks.
If the original connection does not satisfy "database/sql/driver".Execer, it return ErrSkip error. NOT SUPPORTED: use ExecContext instead
func (*Conn) ExecContext ¶
func (conn *Conn) ExecContext(c context.Context, query string, args []driver.NamedValue) (driver.Result, error)
ExecContext calls the original ExecContext (or Exec as a fallback) method of the connection. It will trigger PreExec, Exec, PostExec hooks.
If the original connection does not satisfy "database/sql/driver".ExecerContext nor "database/sql/driver".Execer, it return ErrSkip error.
func (*Conn) IsValid ¶
IsValid implements driver.Validator. It calls the IsValid method of the original connection. If the original connection does not satisfy "database/sql/driver".Validator, it always returns true.
func (*Conn) Ping ¶
Ping verifies a connection to the database is still alive. It will trigger PrePing, Ping, PostPing hooks.
If the original connection does not satisfy "database/sql/driver".Pinger, it does nothing.
func (*Conn) Prepare ¶
Prepare returns a prepared statement which is wrapped by Stmt. NOT SUPPORTED: use PrepareContext instead
func (*Conn) PrepareContext ¶
PrepareContext returns a prepared statement which is wrapped by Stmt.
func (*Conn) Query ¶
Query executes a query that may return rows. It wil trigger PreQuery, Query, PostQuery hooks.
If the original connection does not satisfy "database/sql/driver".Queryer, it return ErrSkip error. NOT SUPPORTED: use QueryContext instead
func (*Conn) QueryContext ¶
func (conn *Conn) QueryContext(c context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
QueryContext executes a query that may return rows. It wil trigger PreQuery, Query, PostQuery hooks.
If the original connection does not satisfy "database/sql/driver".QueryerContext nor "database/sql/driver".Queryer, it return ErrSkip error.
type Connector ¶
Connector adds hook points into "database/sql/driver".Connector.
func (*Connector) Close ¶
Close closes the c.Connector if it implements the io.Closer interface. It is called by the DB.Close method from Go 1.17.
type Filter ¶
Filter is used by the tracing proxy for skipping database libraries (e.g. O/R mapper).
type Hooks ¶
type Hooks struct {
// PrePing is a callback that gets called prior to calling
// `Conn.Ping`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Ping` and `Hooks.Ping` methods
// are not called.
//
// The first return value is passed to both `Hooks.Ping` and
// `Hooks.PostPing` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Ping` method and `Hooks.Ping`
// methods are not called.
PrePing func(conn *Conn) (interface{}, error)
// Ping is called after the underlying driver's `Conn.Exec` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePing` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Ping` method.
Ping func(ctx interface{}, conn *Conn) error
// PostPing is a callback that gets called at the end of
// the call to `Conn.Ping`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePing` method, and may be nil.
PostPing func(ctx interface{}, conn *Conn, err error) error
// PreOpen is a callback that gets called before any
// attempt to open the sql connection is made, and is ALWAYS
// called.
//
// The first return value is passed to both `Hooks.Open` and
// `Hooks.PostOpen` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Driver.Open` method and `Hooks.Open`
// methods are not called.
PreOpen func(name string) (interface{}, error)
// Open is called after the underlying driver's `Driver.Open` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreOpen` method, and may be nil.
//
// If this callback returns an error, then the `conn` object is
// closed by calling the `Close` method, and the error from this
// callback is returned by the `db.Open` method.
Open func(ctx interface{}, conn *Conn) error
// PostOpen is a callback that gets called at the end of
// the call to `db.Open(). It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreOpen` method, and may be nil.
PostOpen func(ctx interface{}, conn *Conn) error
// PreExec is a callback that gets called prior to calling
// `Stmt.Exec`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Stmt.Exec` and `Hooks.Exec` methods
// are not called.
//
// The first return value is passed to both `Hooks.Exec` and
// `Hooks.PostExec` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Driver.Exec` method and `Hooks.Exec`
// methods are not called.
PreExec func(stmt *Stmt, args []driver.Value) (interface{}, error)
// Exec is called after the underlying driver's `Driver.Exec` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreExec` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Stmt.Exec` method.
Exec func(ctx interface{}, stmt *Stmt, args []driver.Value, result driver.Result) error
// PostExec is a callback that gets called at the end of
// the call to `Stmt.Exec`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreExec` method, and may be nil.
PostExec func(ctx interface{}, stmt *Stmt, args []driver.Value, result driver.Result) error
// PreQuery is a callback that gets called prior to calling
// `Stmt.Query`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Stmt.Query` and `Hooks.Query` methods
// are not called.
//
// The first return value is passed to both `Hooks.Query` and
// `Hooks.PostQuery` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Stmt.Query` method and `Hooks.Query`
// methods are not called.
PreQuery func(stmt *Stmt, args []driver.Value) (interface{}, error)
// Query is called after the underlying driver's `Stmt.Query` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreQuery` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Stmt.Query` method.
Query func(ctx interface{}, stmt *Stmt, args []driver.Value, rows driver.Rows) error
// PostQuery is a callback that gets called at the end of
// the call to `Stmt.Query`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreQuery` method, and may be nil.
PostQuery func(ctx interface{}, stmt *Stmt, args []driver.Value, rows driver.Rows) error
// PreBegin is a callback that gets called prior to calling
// `Stmt.Begin`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Begin` and `Hooks.Begin` methods
// are not called.
//
// The first return value is passed to both `Hooks.Begin` and
// `Hooks.PostBegin` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Begin` method and `Hooks.Begin`
// methods are not called.
PreBegin func(conn *Conn) (interface{}, error)
// Begin is called after the underlying driver's `Conn.Begin` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreBegin` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Begin` method.
Begin func(ctx interface{}, conn *Conn) error
// PostBegin is a callback that gets called at the end of
// the call to `Conn.Begin`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreBegin` method, and may be nil.
PostBegin func(ctx interface{}, conn *Conn) error
// PreCommit is a callback that gets called prior to calling
// `Tx.Commit`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Tx.Commit` and `Hooks.Commit` methods
// are not called.
//
// The first return value is passed to both `Hooks.Commit` and
// `Hooks.PostCommit` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Tx.Commit` method and `Hooks.Commit`
// methods are not called.
PreCommit func(tx *Tx) (interface{}, error)
// Commit is called after the underlying driver's `Tx.Commit` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreCommit` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Tx.Commit` method.
Commit func(ctx interface{}, tx *Tx) error
// PostCommit is a callback that gets called at the end of
// the call to `Tx.Commit`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreCommit` method, and may be nil.
PostCommit func(ctx interface{}, tx *Tx) error
// PreRollback is a callback that gets called prior to calling
// `Tx.Rollback`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Tx.Rollback` and `Hooks.Rollback` methods
// are not called.
//
// The first return value is passed to both `Hooks.Rollback` and
// `Hooks.PostRollback` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Tx.Rollback` method and `Hooks.Rollback`
PreRollback func(tx *Tx) (interface{}, error)
// Rollback is called after the underlying driver's `Tx.Rollback` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreRollback` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Tx.Rollback` method.
Rollback func(ctx interface{}, tx *Tx) error
// PostRollback is a callback that gets called at the end of
// the call to `Tx.Rollback`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreRollback` method, and may be nil.
PostRollback func(ctx interface{}, tx *Tx) error
// PreClose is a callback that gets called prior to calling
// `Conn.Close`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Close` and `Hooks.Close` methods
// are not called.
//
// The first return value is passed to both `Hooks.Close` and
// `Hooks.PostClose` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Close` method and `Hooks.Close`
// methods are not called.
PreClose func(conn *Conn) (interface{}, error)
// Close is called after the underlying driver's `Conn.Close` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreClose` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Close` method.
Close func(ctx interface{}, conn *Conn) error
// PostClose is a callback that gets called at the end of
// the call to `Conn.Close`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreClose` method, and may be nil.
PostClose func(ctx interface{}, conn *Conn, err error) error
// PreResetSession is a callback that gets called prior to calling
// `Conn.ResetSession`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.ResetSession` and `Hooks.ResetSession` methods
// are not called.
//
// The first return value is passed to both `Hooks.ResetSession` and
// `Hooks.PostResetSession` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.ResetSession` method and `Hooks.ResetSession`
// methods are not called.
PreResetSession func(conn *Conn) (interface{}, error)
// ResetSession is called after the underlying driver's `Conn.ResetSession` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreResetSession` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.ResetSession` method.
ResetSession func(ctx interface{}, conn *Conn) error
// PostResetSession is a callback that gets called at the end of
// the call to `Conn.ResetSession`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreResetSession` method, and may be nil.
PostResetSession func(ctx interface{}, conn *Conn, err error) error
}
Hooks is callback functions for the proxy. Deprecated: You should use HooksContext instead.
type HooksContext ¶
type HooksContext struct {
// PrePing is a callback that gets called prior to calling
// `Conn.Ping`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Ping` and `Hooks.Ping` methods
// are not called.
//
// The first return value is passed to both `Hooks.Ping` and
// `Hooks.PostPing` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Ping` method and `Hooks.Ping`
// methods are not called.
PrePing func(c context.Context, conn *Conn) (interface{}, error)
// Ping is called after the underlying driver's `Conn.Ping` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePing` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Ping` method.
Ping func(c context.Context, ctx interface{}, conn *Conn) error
// PostPing is a callback that gets called at the end of
// the call to `Conn.Ping`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePing` method, and may be nil.
PostPing func(c context.Context, ctx interface{}, conn *Conn, err error) error
// PreOpen is a callback that gets called before any
// attempt to open the sql connection is made, and is ALWAYS
// called.
//
// The first return value is passed to both `Hooks.Open` and
// `Hooks.PostOpen` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Driver.Open` method and `Hooks.Open`
// methods are not called.
PreOpen func(c context.Context, name string) (interface{}, error)
// Open is called after the underlying driver's `Driver.Open` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreOpen` method, and may be nil.
//
// If this callback returns an error, then the `conn` object is
// closed by calling the `Close` method, and the error from this
// callback is returned by the `db.Open` method.
Open func(c context.Context, ctx interface{}, conn *Conn) error
// PostOpen is a callback that gets called at the end of
// the call to `db.Open(). It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreOpen` method, and may be nil.
PostOpen func(c context.Context, ctx interface{}, conn *Conn, err error) error
// PrePrepare is a callback that gets called prior to calling
// `db.Prepare`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `db.Exec` and `Hooks.Prepare` methods
// are not called.
//
// The first return value is passed to both `Hooks.Prepare` and
// `Hooks.PostPrepare` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook.
PrePrepare func(c context.Context, stmt *Stmt) (interface{}, error)
// Prepare is called after the underlying driver's `db.Prepare` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePrepare` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `db.Prepare` method.
Prepare func(c context.Context, ctx interface{}, stmt *Stmt) error
// PostPrepare is a callback that gets called at the end of
// the call to `db.Prepare`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePrepare` method, and may be nil.
PostPrepare func(c context.Context, ctx interface{}, stmt *Stmt, err error) error
// PreExec is a callback that gets called prior to calling
// `Stmt.Exec`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Stmt.Exec` and `Hooks.Exec` methods
// are not called.
//
// The first return value is passed to both `Hooks.Exec` and
// `Hooks.PostExec` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Driver.Exec` method and `Hooks.Exec`
// methods are not called.
PreExec func(c context.Context, stmt *Stmt, args []driver.NamedValue) (interface{}, error)
// Exec is called after the underlying driver's `Driver.Exec` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreExec` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Stmt.Exec` method.
Exec func(c context.Context, ctx interface{}, stmt *Stmt, args []driver.NamedValue, result driver.Result) error
// PostExec is a callback that gets called at the end of
// the call to `Stmt.Exec`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreExec` method, and may be nil.
PostExec func(c context.Context, ctx interface{}, stmt *Stmt, args []driver.NamedValue, result driver.Result, err error) error
// PreQuery is a callback that gets called prior to calling
// `Stmt.Query`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Stmt.Query` and `Hooks.Query` methods
// are not called.
//
// The first return value is passed to both `Hooks.Query` and
// `Hooks.PostQuery` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Stmt.Query` method and `Hooks.Query`
// methods are not called.
PreQuery func(c context.Context, stmt *Stmt, args []driver.NamedValue) (interface{}, error)
// Query is called after the underlying driver's `Stmt.Query` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreQuery` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Stmt.Query` method.
Query func(c context.Context, ctx interface{}, stmt *Stmt, args []driver.NamedValue, rows driver.Rows) error
// PostQuery is a callback that gets called at the end of
// the call to `Stmt.Query`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreQuery` method, and may be nil.
PostQuery func(c context.Context, ctx interface{}, stmt *Stmt, args []driver.NamedValue, rows driver.Rows, err error) error
// PreBegin is a callback that gets called prior to calling
// `Stmt.Begin`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Begin` and `Hooks.Begin` methods
// are not called.
//
// The first return value is passed to both `Hooks.Begin` and
// `Hooks.PostBegin` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Begin` method and `Hooks.Begin`
// methods are not called.
PreBegin func(c context.Context, conn *Conn) (interface{}, error)
// Begin is called after the underlying driver's `Conn.Begin` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreBegin` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Begin` method.
Begin func(c context.Context, ctx interface{}, conn *Conn) error
// PostBegin is a callback that gets called at the end of
// the call to `Conn.Begin`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreBegin` method, and may be nil.
PostBegin func(c context.Context, ctx interface{}, conn *Conn, err error) error
// PreCommit is a callback that gets called prior to calling
// `Tx.Commit`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Tx.Commit` and `Hooks.Commit` methods
// are not called.
//
// The first return value is passed to both `Hooks.Commit` and
// `Hooks.PostCommit` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Tx.Commit` method and `Hooks.Commit`
// methods are not called.
PreCommit func(c context.Context, tx *Tx) (interface{}, error)
// Commit is called after the underlying driver's `Tx.Commit` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreCommit` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Tx.Commit` method.
Commit func(c context.Context, ctx interface{}, tx *Tx) error
// PostCommit is a callback that gets called at the end of
// the call to `Tx.Commit`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreCommit` method, and may be nil.
PostCommit func(c context.Context, ctx interface{}, tx *Tx, err error) error
// PreRollback is a callback that gets called prior to calling
// `Tx.Rollback`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Tx.Rollback` and `Hooks.Rollback` methods
// are not called.
//
// The first return value is passed to both `Hooks.Rollback` and
// `Hooks.PostRollback` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Tx.Rollback` method and `Hooks.Rollback`
PreRollback func(c context.Context, tx *Tx) (interface{}, error)
// Rollback is called after the underlying driver's `Tx.Rollback` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreRollback` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Tx.Rollback` method.
Rollback func(c context.Context, ctx interface{}, tx *Tx) error
// PostRollback is a callback that gets called at the end of
// the call to `Tx.Rollback`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreRollback` method, and may be nil.
PostRollback func(c context.Context, ctx interface{}, tx *Tx, err error) error
// PreClose is a callback that gets called prior to calling
// `Conn.Close`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Close` and `Hooks.Close` methods
// are not called.
//
// The first return value is passed to both `Hooks.Close` and
// `Hooks.PostClose` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Close` method and `Hooks.Close`
// methods are not called.
PreClose func(c context.Context, conn *Conn) (interface{}, error)
// Close is called after the underlying driver's `Conn.Close` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreClose` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Close` method.
Close func(c context.Context, ctx interface{}, conn *Conn) error
// PostClose is a callback that gets called at the end of
// the call to `Conn.Close`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreClose` method, and may be nil.
PostClose func(c context.Context, ctx interface{}, conn *Conn, err error) error
// PreResetSession is a callback that gets called prior to calling
// `Conn.ResetSession`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.ResetSession` and `Hooks.ResetSession` methods
// are not called.
//
// The first return value is passed to both `Hooks.ResetSession` and
// `Hooks.PostResetSession` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.ResetSession` method and `Hooks.ResetSession`
// methods are not called.
PreResetSession func(c context.Context, conn *Conn) (interface{}, error)
// ResetSession is called after the underlying driver's `Conn.ResetSession` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreResetSession` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.ResetSession` method.
ResetSession func(c context.Context, ctx interface{}, conn *Conn) error
// PostResetSession is a callback that gets called at the end of
// the call to `Conn.ResetSession`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreResetSession` method, and may be nil.
PostResetSession func(c context.Context, ctx interface{}, conn *Conn, err error) error
// PreIsValid is a callback that gets called prior to calling
// `Conn.IsValid`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.IsValid` and `Hooks.IsValid` methods
// are not called.
//
// The first return value is passed to both `Hooks.IsValid` and
// `Hooks.PostIsValid` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the connection is marked as invalid.
PreIsValid func(conn *Conn) (interface{}, error)
// IsValid is called after the underlying driver's `Conn.IsValid` method
// returns true.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreIsValid` method, and may be nil.
//
// If this callback returns an error, the connection is marked as invalid.
IsValid func(ctx interface{}, conn *Conn) error
// PostPostIsValid is a callback that gets called at the end of
// the call to `Conn.PostIsValid`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PrePostIsValid` method, and may be nil.
PostIsValid func(ctx interface{}, conn *Conn, valid bool) error
// OnError is a callback that gets called when a sql.DB operation
// returns an error, and give you the opportunity of change/modify the error
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreXXXXXX` method, and may be nil.
OnError func(ctx interface{}, err error) error
}
HooksContext is callback functions with context.Context for the proxy.
func NewTraceHooks ¶
func NewTraceHooks(opt TracerOptions) *HooksContext
NewTraceHooks creates new HooksContext which trace SQL queries.
type Outputter ¶
Outputter is what is used by the tracing proxy created via `NewTraceProxy`. Anything that implements a `log.Logger` style `Output` method will satisfy this interface.
type PackageFilter ¶
type PackageFilter map[string]struct{}
PackageFilter is an implementation of Filter.
func (PackageFilter) DoOutput ¶
func (f PackageFilter) DoOutput(packageName string) bool
DoOutput returns false if the package is in the ignored list.
func (PackageFilter) Ignore ¶
func (f PackageFilter) Ignore(packageName string)
Ignore add the package into the ignored list.
type Proxy ¶
Proxy is a sql driver. It adds hook points to other sql drivers.
func NewProxy ¶
NewProxy creates new Proxy driver. Deprecated: You should use NewProxyContext instead.
func NewProxyContext ¶
func NewProxyContext(driver driver.Driver, hs ...*HooksContext) *Proxy
NewProxyContext creates new Proxy driver.
func NewTraceProxy ¶
NewTraceProxy generates a proxy that logs queries.
func NewTraceProxyWithFilter ¶
NewTraceProxyWithFilter generates a proxy that logs queries.
type Stmt ¶
type Stmt struct {
// Stmt is the original statement.
// It may be nil because some sql drivers support skipping Prepare.
Stmt driver.Stmt
QueryString string
Proxy *Proxy
Conn *Conn
}
Stmt adds hook points into "database/sql/driver".Stmt.
func (*Stmt) CheckNamedValue ¶
func (stmt *Stmt) CheckNamedValue(nv *driver.NamedValue) (err error)
CheckNamedValue for implementing NamedValueChecker
func (*Stmt) ColumnConverter ¶
func (stmt *Stmt) ColumnConverter(idx int) driver.ValueConverter
ColumnConverter returns a ValueConverter for the provided column index. If the original statement does not satisfy ColumnConverter, it returns driver.DefaultParameterConverter.
func (*Stmt) Exec ¶
Exec executes a query that doesn't return rows. It will trigger PreExec, Exec, PostExec hooks. NOT SUPPORTED: use ExecContext instead
func (*Stmt) ExecContext ¶
ExecContext executes a query that doesn't return rows. It will trigger PreExec, Exec, PostExec hooks.
func (*Stmt) NumInput ¶
NumInput returns the number of placeholder parameters. It just calls the original NumInput method.
func (*Stmt) Query ¶
Query executes a query that may return rows. It wil trigger PreQuery, Query, PostQuery hooks. NOT SUPPORTED: use QueryContext instead
func (*Stmt) QueryContext ¶
QueryContext executes a query that may return rows. It wil trigger PreQuery, Query, PostQuery hooks.
type TracerOptions ¶
type TracerOptions struct {
// Outputter is the output of the log.
// If is nil nil, log.Output is used.
Outputter Outputter
// Filter is used by the tracing proxy for skipping database libraries (e.g. O/R mapper).
// If it is nil, DefaultPackageFilter is used.
Filter Filter
// SlowQuery is a threshold duration to output into log.
// output all queries if SlowQuery is zero.
SlowQuery time.Duration
}
TracerOptions holds the tracing option.