8000 feat: support multiple hooks by chenquan · Pull Request #2 · chenquan/sqlplus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: support multiple hooks #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type conn struct {
}

func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (result driver.Result, err error) {
ctx, err = c.BeforeExecContext(ctx, query, args)
ctx, query, args, err = c.BeforeExecContext(ctx, query, args, nil)
defer func() {
_, result, err = c.AfterExecContext(ctx, query, args, result, err)
}()
Expand Down Expand Up @@ -48,7 +48,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
}

func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
ctx, err = c.BeforeQueryContext(ctx, query, args)
ctx, query, args, err = c.BeforeQueryContext(ctx, query, args, nil)
defer func() {
_, rows, err = c.AfterQueryContext(ctx, query, args, rows, err)
}()
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
}

func (c *conn) PrepareContext(ctx context.Context, query string) (s driver.Stmt, err error) {
ctx, err = c.BeforePrepareContext(ctx, query)
ctx, query, err = c.BeforePrepareContext(ctx, query, nil)
defer func() {
_, s, err = c.AfterPrepareContex 8000 t(ctx, query, s, err)
}()
Expand All @@ -101,7 +101,7 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (s driver.Stmt,
}

func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (dd driver.Tx, err error) {
ctx, err = c.BeforeBeginTx(ctx, opts)
ctx, opts, err = c.BeforeBeginTx(ctx, opts, nil)
defer func() {
_, dd, err = c.AfterBeginTx(ctx, opts, dd, err)
}()
Expand Down
2 changes: 1 addition & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type connector struct {
}

func (c *connector) Connect(ctx context.Context) (dc driver.Conn, err error) {
ctx, err = c.BeforeConnect(ctx)
ctx, err = c.BeforeConnect(ctx, nil)
defer func() {
_, dc, err = c.AfterConnect(ctx, dc, err)
}()
Expand Down
207 changes: 180 additions & 27 deletions hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,192 @@ import (
"database/sql/driver"
)

type Hook interface {
ConnectorHook
ConnHook
TxHook
StmtHook
type (
Hook interface {
ConnectorHook
ConnHook
TxHook
StmtHook
}
ConnHook interface {
BeforeExecContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, string, []driver.NamedValue, error)
AfterExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error)

BeforeBeginTx(ctx context.Context, opts driver.TxOptions, err error) (context.Context, driver.TxOptions, error)
AfterBeginTx(ctx context.Context, opts driver.TxOptions, dd driver.Tx, err error) (context.Context, driver.Tx, error)

BeforeQueryContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, string, []driver.NamedValue, error)
AfterQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error)

BeforePrepareContext(ctx context.Context, query string, err error) (context.Context, string, error)
AfterPrepareContext(ctx context.Context, query string, s driver.Stmt, err error) (context.Context, driver.Stmt, error)
}
ConnectorHook interface {
BeforeConnect(ctx context.Context, err error) (context.Context, error)
AfterConnect(ctx context.Context, dc driver.Conn, err error) (context.Context, driver.Conn, error)
}
TxHook interface {
BeforeCommit(ctx context.Context, err error) (context.Context, error)
AfterCommit(ctx context.Context, err error) (context.Context, error)
BeforeRollback(ctx context.Context, err error) (context.Context, error)
AfterRollback(ctx context.Context, err error) (context.Context, error)
}
StmtHook interface {
BeforeStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, []driver.NamedValue, error)
AfterStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error)
BeforeStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, []driver.NamedValue, error)
AfterStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error)
}
Hooks struct {
hooks []Hook
}
)

func NewMultiHook(hooks ...Hook) Hook {
return &Hooks{hooks: hooks}
}

func (h *Hooks) BeforeConnect(ctx context.Context, err error) (context.Context, error) {
for _, hook := range h.hooks {
ctx, err = hook.BeforeConnect(ctx, err)
}

return ctx, err
}

func (h *Hooks) AfterConnect(ctx context.Context, dc driver.Conn, err error) (context.Context, driver.Conn, error) {
for _, hook := range h.hooks {
ctx, dc, err = hook.AfterConnect(ctx, dc, err)
}

return ctx, dc, err
}

func (h *Hooks) BeforeExecContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, string, []driver.NamedValue, error) {
for _, hook := range h.hooks {
ctx, query, args, err = hook.BeforeExecContext(ctx, query, args, err)
}

return ctx, query, args, err
}

func (h *Hooks) AfterExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error) {
for _, hook := range h.hooks {
ctx, r, err = hook.AfterExecContext(ctx, query, args, r, err)
}

return ctx, r, err
}

func (h *Hooks) BeforeBeginTx(ctx context.Context, opts driver.TxOptions, err error) (context.Context, driver.TxOptions, error) {
for _, hook := range h.hooks {
ctx, opts, err = hook.BeforeBeginTx(ctx, opts, err)
}

return ctx, opts, err
}

func (h *Hooks) AfterBeginTx(ctx context.Context, opts driver.TxOptions, dd driver.Tx, err error) (context.Context, driver.Tx, error) {
for _, hook := range h.hooks {
ctx, dd, err = hook.AfterBeginTx(ctx, opts, dd, err)
}

return ctx, dd, err
}

func (h *Hooks) BeforeQueryContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, string, []driver.NamedValue, error) {
for _, hook := range h.hooks {
ctx, query, args, err = hook.BeforeQueryContext(ctx, query, args, err)
}

return ctx, query, args, err
}

type ConnHook interface {
BeforeExecContext(ctx context.Context, query string, args []driver.NamedValue) (context.Context, error)
AfterExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error)
BeforeBeginTx(ctx context.Context, opts driver.TxOptions) (context.Context, error)
AfterBeginTx(ctx context.Context, opts driver.TxOptions, dd driver.Tx, err error) (context.Context, driver.Tx, error)
BeforeQueryContext(ctx context.Context, query string, args []driver.NamedValue) (context.Context, error)
AfterQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error)
BeforePrepareContext(ctx context.Context, query string) (context.Context, error)
AfterPrepareContext(ctx context.Context, query string, s driver.Stmt, err error) (context.Context, driver.Stmt, error)
func (h *Hooks) AfterQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error) {
for _, hook := range h.hooks {
ctx, rows, err = hook.AfterQueryContext(ctx, query, args, rows, err)

}

return ctx, rows, err
}

func (h *Hooks) BeforePrepareContext(ctx context.Context, query string, err error) (context.Context, string, error) {
for _, hook := range h.hooks {
ctx, query, err = hook.BeforePrepareContext(ctx, query, err)
}

return ctx, query, err
}

func (h *Hooks) AfterPrepareContext(ctx context.Context, query string, s driver.Stmt, err error) (context.Context, driver.Stmt, error) {
for _, hook := range h.hooks {
ctx, s, err = hook.AfterPrepareContext(ctx, query, s, err)
}

return ctx, s, err
}

func (h *Hooks) BeforeCommit(ctx context.Context, err error) (context.Context, error) {
for _, hook := range h.hooks {
ctx, err = hook.BeforeCommit(ctx, err)
}

8000 return ctx, err
}

func (h *Hooks) AfterCommit(ctx context.Context, err error) (context.Context, error) {
for _, hook := range h.hooks {
ctx, err = hook.AfterCommit(ctx, err)
}

return ctx, err
}

type ConnectorHook interface {
BeforeConnect(ctx context.Context) (context.Context, error)
AfterConnect(ctx context.Context, dc driver.Conn, err error) (context.Context, driver.Conn, error)
func (h *Hooks) BeforeRollback(ctx context.Context, err error) (context.Context, error) {
for _, hook := range h.hooks {
ctx, err = hook.BeforeRollback(ctx, err)
}

return ctx, err
}

type TxHook interface {
BeforeCommit(ctx context.Context) (context.Context, error)
AfterCommit(ctx context.Context, err error) (context.Context, error)
BeforeRollback(ctx context.Context) (context.Context, error)
AfterRollback(ctx context.Context, err error) (context.Context, error)
func (h *Hooks) AfterRollback(ctx context.Context, err error) (context.Context, error) {
for _, hook := range h.hooks {
ctx, err = hook.AfterRollback(ctx, err)
}

return ctx, err
}

type StmtHook interface {
BeforeStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue) (context.Context, error)
AfterStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error)
BeforeStmtExecContext(ctx context.Context, query string, args []driver.NamedValue) (context.Context, error)
AfterStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error)
func (h *Hooks) BeforeStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, []driver.NamedValue, error) {
for _, hook := range h.hooks {
ctx, args, err = hook.BeforeStmtQueryContext(ctx, query, args, err)
}

return ctx, args, err
}

func (h *Hooks) AfterStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error) {
for _, hook := range h.hooks {
ctx, rows, err = hook.AfterStmtQueryContext(ctx, query, args, rows, err)
}

return ctx, rows, err
}

func (h *Hooks) BeforeStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, err error) (context.Context, []driver.NamedValue, error) {
for _, hook := range h.hooks {
ctx, args, err = hook.BeforeStmtExecContext(ctx, query, args, err)
}

return ctx, args, err
}

func (h *Hooks) AfterStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error) {
for _, hook := range h.hooks {
ctx, r, err = hook.AfterStmtExecContext(ctx, query, args, r, err)
}

return ctx, r, err
}
23 changes: 9 additions & 14 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type stmt struct {
}

func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
ctx, err = s.BeforeStmtQueryContext(ctx, s.query, args)
query := s.query
ctx, args, err = s.BeforeStmtQueryContext(ctx, query, args, nil)
defer func() {
_, rows, err = s.AfterStmtQueryContext(ctx, s.query, args, rows, err)
_, rows, err = s.AfterStmtQueryContext(ctx, query, args, rows, err)
}()
if err != nil {
return nil, err
Expand All @@ -29,22 +30,20 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows
switch ss := s.Stmt.(type) {
case driver.StmtQueryContext:
return ss.QueryContext(ctx, args)
case interface {
Query(args []driver.Value) (driver.Rows, error)
}:
default:
value, err := namedValueToValue(args)
if err != nil {
return nil, err
}

return ss.Query(value)
return s.Query(value)
}

return nil, errNoInterfaceImplementation
}

func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) {
ctx, err = s.BeforeStmtExecContext(ctx, s.query, args)

ctx, args, err = s.BeforeStmtExecContext(ctx, s.query, args, nil)
defer func() {
_, r, err = s.AfterStmtExecContext(ctx, s.query, args, r, err)
}()
Expand All @@ -55,16 +54,12 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (r dri
switch ss := s.Stmt.(type) {
case driver.StmtExecContext:
return ss.ExecContext(ctx, args)
case interface {
Exec(args []driver.Value) (driver.Result, error)
}:
default:
value, err := namedValueToValue(args)
if err != nil {
return nil, err
}

return ss.Exec(value)
return s.Exec(value)
}

return nil, errNoInterfaceImplementation
}
4 changes: 2 additions & 2 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type tx struct {

func (t *tx) Commit() (err error) {
ctx := context.Background()
ctx, err = t.BeforeCommit(ctx)
ctx, err = t.BeforeCommit(ctx, nil)
defer func() {
_, err = t.AfterCommit(ctx, err)
}()
Expand All @@ -30,7 +30,7 @@ func (t *tx) Commit() (err error) {

func (t *tx) Rollback() (err error) {
ctx := context.Background()
ctx, err = t.BeforeRollback(ctx)
ctx, err = t.BeforeRollback(ctx, nil)
defer func() {
_, err = t.AfterRollback(ctx, err)
}()
Expand Down
0