Skip to content

Commit

Permalink
Merge pull request #26 from shogo82148/print-conn-addr
Browse files Browse the repository at this point in the history
print the address of the connection.
  • Loading branch information
shogo82148 committed Mar 5, 2018
2 parents d11245d + d333d50 commit f4671c3
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 49 deletions.
4 changes: 4 additions & 0 deletions conn.go
Expand Up @@ -72,6 +72,7 @@ func (conn *Conn) PrepareContext(c context.Context, query string) (driver.Stmt,
Stmt: stmt,
QueryString: query,
Proxy: conn.Proxy,
Conn: conn,
}, nil
}

Expand Down Expand Up @@ -160,6 +161,7 @@ func (conn *Conn) BeginTx(c context.Context, opts driver.TxOptions) (driver.Tx,
return &Tx{
Tx: tx,
Proxy: conn.Proxy,
Conn: conn,
ctx: c,
}, nil
}
Expand Down Expand Up @@ -193,6 +195,7 @@ func (conn *Conn) ExecContext(c context.Context, query string, args []driver.Nam
stmt = &Stmt{
QueryString: query,
Proxy: conn.Proxy,
Conn: conn,
}
defer func() { hooks.postExec(c, ctx, stmt, args, result, err) }()
if ctx, err = hooks.preExec(c, stmt, args); err != nil {
Expand Down Expand Up @@ -256,6 +259,7 @@ func (conn *Conn) QueryContext(c context.Context, query string, args []driver.Na
stmt := &Stmt{
QueryString: query,
Proxy: conn.Proxy,
Conn: conn,
}
defer func() { hooks.postQuery(c, ctx, stmt, args, rows, err) }()
if ctx, err = hooks.preQuery(c, stmt, args); err != nil {
Expand Down
9 changes: 5 additions & 4 deletions connector.go
Expand Up @@ -20,14 +20,15 @@ func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
var err error
var myctx interface{}
var conn driver.Conn
var myconn *Conn
hooks := c.Proxy.getHooks(ctx)

if hooks != nil {
// Setup PostOpen. This needs to be a closure like this
// or otherwise changes to the `ctx` and `conn` parameters
// within this Open() method does not get applied at the
// time defer is fired
defer func() { hooks.postOpen(ctx, myctx, conn, err) }()
defer func() { hooks.postOpen(ctx, myctx, myconn, err) }()
if myctx, err = hooks.preOpen(ctx, c.Name); err != nil {
return nil, err
}
Expand All @@ -37,18 +38,18 @@ func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
return nil, err
}

conn = &Conn{
myconn = &Conn{
Conn: conn,
Proxy: c.Proxy,
}

if hooks != nil {
if err = hooks.open(ctx, myctx, conn); err != nil {
if err = hooks.open(ctx, myctx, myconn); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
return myconn, nil
}

// Driver returns the underlying Driver of the Connector.
Expand Down
4 changes: 2 additions & 2 deletions logging_hook_test.go
Expand Up @@ -47,14 +47,14 @@ func (h *loggingHook) preOpen(c context.Context, name string) (interface{}, erro
return nil, nil
}

func (h *loggingHook) open(c context.Context, ctx interface{}, conn driver.Conn) error {
func (h *loggingHook) open(c context.Context, ctx interface{}, conn *Conn) error {
h.mu.Lock()
defer h.mu.Unlock()
fmt.Fprintln(h, "[Open]")
return nil
}

func (h *loggingHook) postOpen(c context.Context, ctx interface{}, conn driver.Conn, err error) error {
func (h *loggingHook) postOpen(c context.Context, ctx interface{}, conn *Conn, err error) error {
h.mu.Lock()
defer h.mu.Unlock()
fmt.Fprintln(h, "[PostOpen]")
Expand Down
35 changes: 17 additions & 18 deletions proxy.go
Expand Up @@ -22,8 +22,8 @@ type hooks interface {
ping(c context.Context, ctx interface{}, conn *Conn) error
postPing(c context.Context, ctx interface{}, conn *Conn, err error) error
preOpen(c context.Context, name string) (interface{}, error)
open(c context.Context, ctx interface{}, conn driver.Conn) error
postOpen(c context.Context, ctx interface{}, conn driver.Conn, err error) error
open(c context.Context, ctx interface{}, conn *Conn) error
postOpen(c context.Context, ctx interface{}, conn *Conn, err error) error
preExec(c context.Context, stmt *Stmt, args []driver.NamedValue) (interface{}, error)
exec(c context.Context, ctx interface{}, stmt *Stmt, args []driver.NamedValue, result driver.Result) error
postExec(c context.Context, ctx interface{}, stmt *Stmt, args []driver.NamedValue, result driver.Result, err error) error
Expand Down Expand Up @@ -104,14 +104,14 @@ type HooksContext struct {
// 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 driver.Conn) error
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 driver.Conn, err error) error
PostOpen func(c context.Context, ctx interface{}, conn *Conn, err error) error

// PreExec is a callback that gets called prior to calling
// `Stmt.Exec`, and is ALWAYS called. If this callback returns an
Expand Down Expand Up @@ -365,14 +365,14 @@ func (h *HooksContext) preOpen(c context.Context, name string) (interface{}, err
return h.PreOpen(c, name)
}

func (h *HooksContext) open(c context.Context, ctx interface{}, conn driver.Conn) error {
func (h *HooksContext) open(c context.Context, ctx interface{}, conn *Conn) error {
if h == nil || h.Open == nil {
return nil
}
return h.Open(c, ctx, conn)
}

func (h *HooksContext) postOpen(c context.Context, ctx interface{}, conn driver.Conn, err error) error {
func (h *HooksContext) postOpen(c context.Context, ctx interface{}, conn *Conn, err error) error {
if h == nil || h.PostOpen == nil {
return nil
}
Expand Down Expand Up @@ -584,14 +584,14 @@ type Hooks struct {
// 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 driver.Conn) error
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 driver.Conn) error
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
Expand Down Expand Up @@ -868,14 +868,14 @@ func (h *Hooks) preOpen(c context.Context, name string) (interface{}, error) {
return h.PreOpen(name)
}

func (h *Hooks) open(c context.Context, ctx interface{}, conn driver.Conn) error {
func (h *Hooks) open(c context.Context, ctx interface{}, conn *Conn) error {
if h == nil || h.Open == nil {
return nil
}
return h.Open(ctx, conn)
}

func (h *Hooks) postOpen(c context.Context, ctx interface{}, conn driver.Conn, err error) error {
func (h *Hooks) postOpen(c context.Context, ctx interface{}, conn *Conn, err error) error {
if h == nil || h.PostOpen == nil {
return nil
}
Expand Down Expand Up @@ -1115,13 +1115,13 @@ func (h multipleHooks) preOpen(c context.Context, name string) (interface{}, err
})
}

func (h multipleHooks) open(c context.Context, ctx interface{}, conn driver.Conn) error {
func (h multipleHooks) open(c context.Context, ctx interface{}, conn *Conn) error {
return h.do(ctx, func(h hooks, ctx interface{}) error {
return h.open(c, ctx, conn)
})
}

func (h multipleHooks) postOpen(c context.Context, ctx interface{}, conn driver.Conn, err error) error {
func (h multipleHooks) postOpen(c context.Context, ctx interface{}, conn *Conn, err error) error {
return h.postDo(ctx, err, func(h hooks, ctx interface{}, err error) error {
return h.postOpen(c, ctx, conn, err)
})
Expand Down Expand Up @@ -1260,7 +1260,6 @@ func NewProxy(driver driver.Driver, hs ...*Hooks) *Proxy {
case len(hs) == 0:
return &Proxy{
Driver: driver,
hooks: (*Hooks)(nil),
}
case len(hs) == 1 && hs[0] != nil:
return &Proxy{
Expand Down Expand Up @@ -1342,15 +1341,15 @@ func (p *Proxy) Open(name string) (driver.Conn, error) {
c := context.Background()
var err error
var ctx interface{}

var conn driver.Conn
var myconn *Conn

if p.hooks != nil {
// Setup PostOpen. This needs to be a closure like this
// or otherwise changes to the `ctx` and `conn` parameters
// within this Open() method does not get applied at the
// time defer is fired
defer func() { p.hooks.postOpen(c, ctx, conn, err) }()
defer func() { p.hooks.postOpen(c, ctx, myconn, err) }()

if ctx, err = p.hooks.preOpen(c, name); err != nil {
return nil, err
Expand All @@ -1361,16 +1360,16 @@ func (p *Proxy) Open(name string) (driver.Conn, error) {
return nil, err
}

conn = &Conn{
myconn = &Conn{
Conn: conn,
Proxy: p,
}

if p.hooks != nil {
if err = p.hooks.open(c, ctx, conn); err != nil {
if err = p.hooks.open(c, ctx, myconn); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
return myconn, nil
}
8 changes: 4 additions & 4 deletions proxy_go110_test.go
Expand Up @@ -131,11 +131,11 @@ func TestHooksContext(t *testing.T) {
PreOpen: func(c context.Context, name string) (interface{}, error) {
return ctx0, nil
},
Open: func(c context.Context, ctx interface{}, conn driver.Conn) error {
Open: func(c context.Context, ctx interface{}, conn *Conn) error {
checkCtx("Open", ctx)
return nil
},
PostOpen: func(c context.Context, ctx interface{}, conn driver.Conn, err error) error {
PostOpen: func(c context.Context, ctx interface{}, conn *Conn, err error) error {
checkCtx("PostOpen", ctx)
return err
},
Expand Down Expand Up @@ -252,11 +252,11 @@ func TestHooks(t *testing.T) {
PreOpen: func(name string) (interface{}, error) {
return ctx0, nil
},
Open: func(ctx interface{}, conn driver.Conn) error {
Open: func(ctx interface{}, conn *Conn) error {
checkCtx("Open", ctx)
return nil
},
PostOpen: func(ctx interface{}, conn driver.Conn) error {
PostOpen: func(ctx interface{}, conn *Conn) error {
checkCtx("PostOpen", ctx)
return nil
},
Expand Down
8 changes: 4 additions & 4 deletions proxy_go19_test.go
Expand Up @@ -93,11 +93,11 @@ func TestHooksContext(t *testing.T) {
PreOpen: func(c context.Context, name string) (interface{}, error) {
return ctx0, nil
},
Open: func(c context.Context, ctx interface{}, conn driver.Conn) error {
Open: func(c context.Context, ctx interface{}, conn *Conn) error {
checkCtx("Open", ctx)
return nil
},
PostOpen: func(c context.Context, ctx interface{}, conn driver.Conn, err error) error {
PostOpen: func(c context.Context, ctx interface{}, conn *Conn, err error) error {
checkCtx("PostOpen", ctx)
return err
},
Expand Down Expand Up @@ -181,11 +181,11 @@ func TestHooks(t *testing.T) {
PreOpen: func(name string) (interface{}, error) {
return ctx0, nil
},
Open: func(ctx interface{}, conn driver.Conn) error {
Open: func(ctx interface{}, conn *Conn) error {
checkCtx("Open", ctx)
return nil
},
PostOpen: func(ctx interface{}, conn driver.Conn) error {
PostOpen: func(ctx interface{}, conn *Conn) error {
checkCtx("PostOpen", ctx)
return nil
},
Expand Down
1 change: 1 addition & 0 deletions stmt.go
Expand Up @@ -13,6 +13,7 @@ type Stmt struct {

QueryString string
Proxy *Proxy
Conn *Conn
}

// Close closes the statement.
Expand Down

0 comments on commit f4671c3

Please sign in to comment.