Skip to content

Commit

Permalink
Rewrite for 1.8 context
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chain committed May 5, 2017
1 parent 51cae7f commit c2b9c72
Show file tree
Hide file tree
Showing 20 changed files with 453 additions and 2,171 deletions.
57 changes: 0 additions & 57 deletions README.md
Expand Up @@ -10,60 +10,3 @@ go get github.com/gchaincl/sqlhooks
```

# Usage [![GoDoc](https://godoc.org/github.com/gchaincl/dotsql?status.svg)](https://godoc.org/github.com/gchaincl/sqlhooks)
```go
package main

import (
"log"
"time"

"github.com/gchaincl/sqlhooks"
_ "github.com/mattn/go-sqlite3"
)

// Hooks satisfies sqlhooks.Queryer interface
type Hooks struct {
count int
}

func (h *Hooks) BeforeQuery(ctx *sqlhooks.Context) error {
h.count++
ctx.Set("t", time.Now())
ctx.Set("id", h.count)
log.Printf("[query#%d] %s, args: %v", ctx.Get("id").(int), ctx.Query, ctx.Args)
return nil
}

func (h *Hooks) AfterQuery(ctx *sqlhooks.Context) error {
d := time.Since(ctx.Get("t").(time.Time))
log.Printf("[query#%d] took %s (err: %v)", ctx.Get("id").(int), d, ctx.Error)
return ctx.Error
}

func main() {
db, _ := sqlhooks.Open("sqlite3", ":memory:", &Hooks{})

// 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")
db.Query("Invalid Query")
}

```

```
2016/06/02 14:28:24 [query#1] SELECT id, text FROM t, args: []
2016/06/02 14:28:24 [query#1] took 122.406µs (err: <nil>)
2016/06/02 14:28:24 [query#2] Invalid Query, args: []
2016/06/02 14:28:24 [query#2] took 23.148µs (err: near "Invalid": syntax error)
```

# Benchmark
```
PASS
BenchmarkExec-4 500000 4604 ns/op
BenchmarkExecWithSQLHooks-4 300000 5726 ns/op
BenchmarkPreparedExec-4 1000000 1820 ns/op
BenchmarkPreparedExecWithSQLHooks-4 1000000 2088 ns/op
```
81 changes: 0 additions & 81 deletions benchmark_test.go

This file was deleted.

33 changes: 0 additions & 33 deletions context.go

This file was deleted.

0 comments on commit c2b9c72

Please sign in to comment.