Skip to content

Commit

Permalink
Reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chain committed Jun 2, 2016
1 parent 1c1f270 commit be15218
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 61 deletions.
33 changes: 33 additions & 0 deletions context.go
@@ -0,0 +1,33 @@
package sqlhooks

type Context struct {
Error error
Query string
Args []interface{}

values map[string]interface{}
}

func NewContext() *Context {
return &Context{}
}

func (ctx *Context) Get(key string) interface{} {
if ctx.values == nil {
ctx.values = make(map[string]interface{})
}

if v, ok := ctx.values[key]; ok {
return v
}

return nil
}

func (ctx *Context) Set(key string, value interface{}) {
if ctx.values == nil {
ctx.values = make(map[string]interface{})
}

ctx.values[key] = value
}
61 changes: 0 additions & 61 deletions driver.go
Expand Up @@ -21,67 +21,6 @@ func interfaceToDriver(args []interface{}) []driver.Value {
return r
}

type Context struct {
Error error
Query string
Args []interface{}

values map[string]interface{}
}

func NewContext() *Context {
return &Context{
values: make(map[string]interface{}),
}
}

func (ctx *Context) Get(key string) interface{} {
if v, ok := ctx.values[key]; ok {
return v
}
return nil
}

func (ctx *Context) Set(key string, value interface{}) {
ctx.values[key] = value
}

type Beginner interface {
BeforeBegin(c *Context) error
AfterBegin(c *Context) error
}

type Commiter interface {
BeforeCommit(c *Context) error
AfterCommit(c *Context) error
}

type Rollbacker interface {
BeforeRollback(c *Context) error
AfterRollback(c *Context) error
}

type Stmter interface {
BeforePrepare(c *Context) error
AfterPrepare(c *Context) error

BeforeStmtQuery(c *Context) error
AfterStmtQuery(c *Context) error

BeforeStmtExec(c *Context) error
AfterStmtExec(c *Context) error
}

type Queryer interface {
BeforeQuery(c *Context) error
AfterQuery(c *Context) error
}

type Execer interface {
BeforeExec(c *Context) error
AfterExec(c *Context) error
}

type tx struct {
driver.Tx
hooks interface{}
Expand Down
42 changes: 42 additions & 0 deletions sqlhooks.go
Expand Up @@ -74,3 +74,45 @@ func Open(driverName, dsn string, hooks interface{}) (*sql.DB, error) {

return sql.Open(registeredName, dsn)
}

// Beginner is the interface implemented by objects that wants to hook to Begin function
type Beginner interface {
BeforeBegin(c *Context) error
AfterBegin(c *Context) error
}

// Commiter is the interface implemented by objects that wants to hook to Commit function
type Commiter interface {
BeforeCommit(c *Context) error
AfterCommit(c *Context) error
}

// Rollbacker is the interface implemented by objects that wants to hook to Rollback function
type Rollbacker interface {
BeforeRollback(c *Context) error
AfterRollback(c *Context) error
}

// Stmter is the interface implemented by objects that wants to hook to Statement related functions
type Stmter interface {
BeforePrepare(*Context) error
AfterPrepare(*Context) error

BeforeStmtQuery(*Context) error
AfterStmtQuery(*Context) error

BeforeStmtExec(*Context) error
AfterStmtExec(*Context) error
}

// Queryer is the interface implemented by objects that wants to hook to Query function
type Queryer interface {
BeforeQuery(c *Context) error
AfterQuery(c *Context) error
}

// Execer is the interface implemented by objects that wants to hook to Exec function
type Execer interface {
BeforeExec(c *Context) error
AfterExec(c *Context) error
}

0 comments on commit be15218

Please sign in to comment.