Skip to content
Attach hooks to any database/sql driver
Go
Branch: master
Clone or download
gchaincl Add go mod support and update travis (#26)
Add go mod support and update travis
Latest commit a494b89 Nov 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
hooks add error handler [ErrHook] Jan 30, 2018
.gitignore Initial commit Apr 20, 2016
.travis.yml Add go mod support and update travis (#26) Nov 5, 2019
CHANGELOG.md Update CHANGELOG.md May 9, 2017
LICENSE
README.md Include Go version dependency Apr 27, 2018
benchmark_test.go Add benchmarks, closes #9 May 9, 2017
doc.go Add example to godoc May 8, 2017
go.mod Add go mod support and update travis (#26) Nov 5, 2019
go.sum
sqlhooks.go Add QueryerContext interface (#23) Feb 28, 2019
sqlhooks_1_10.go
sqlhooks_1_10_interface_test.go Add QueryerContext interface (#23) Feb 28, 2019
sqlhooks_interface_test.go Add QueryerContext interface (#23) Feb 28, 2019
sqlhooks_mysql_test.go
sqlhooks_postgres_test.go add error handler [ErrHook] Jan 30, 2018
sqlhooks_pre_1_10.go Add QueryerContext interface (#23) Feb 28, 2019
sqlhooks_sqlite3_test.go add error handler [ErrHook] Jan 30, 2018
sqlhooks_test.go add error handler [ErrHook] Jan 30, 2018

README.md

sqlhooks Build Status Coverage Status Go Report Card

Attach hooks to any database/sql driver.

The purpose of sqlhooks is to provide a way to instrument your sql statements, making really easy to log queries or measure execution time without modifying your actual code.

Install

go get github.com/gchaincl/sqlhooks

Requires Go >= 1.8.x

Breaking changes

V1 isn't backward compatible with previous versions, if you want to fetch old versions, you can get them from gopkg.in

go get gopkg.in/gchaincl/sqlhooks.v0

Usage GoDoc

// This example shows how to instrument sql queries in order to display the time that they consume
package main

import (
	"context"
	"database/sql"
	"fmt"
	"time"

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

// Hooks satisfies the sqlhook.Hooks interface
type Hooks struct {}

// Before hook will print the query with it's args and return the context with the timestamp
func (h *Hooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	fmt.Printf("> %s %q", query, args)
	return context.WithValue(ctx, "begin", time.Now()), nil
}

// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *Hooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	begin := ctx.Value("begin").(time.Time)
	fmt.Printf(". took: %s\n", time.Since(begin))
	return ctx, nil
}

func main() {
	// First, register the wrapper
	sql.Register("sqlite3WithHooks", sqlhooks.Wrap(&sqlite3.SQLiteDriver{}, &Hooks{}))

	// Connect to the registered wrapped driver
	db, _ := sql.Open("sqlite3WithHooks", ":memory:")

	// 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")
}

/*
Output should look like:
> CREATE TABLE t (id INTEGER, text VARCHAR(16)) []. took: 121.238µs
> INSERT into t (text) VALUES(?), (?) ["foo" "bar"]. took: 36.364µs
> SELECT id, text FROM t []. took: 4.653µs
*/

Benchmarks

 go test -bench=. -benchmem
 BenchmarkSQLite3/Without_Hooks-4                  200000              8572 ns/op             627 B/op         16 allocs/op
 BenchmarkSQLite3/With_Hooks-4                     200000             10231 ns/op             738 B/op         18 allocs/op
 BenchmarkMySQL/Without_Hooks-4                     10000            108421 ns/op             437 B/op         10 allocs/op
 BenchmarkMySQL/With_Hooks-4                        10000            226085 ns/op             597 B/op         13 allocs/op
 BenchmarkPostgres/Without_Hooks-4                  10000            125718 ns/op             649 B/op         17 allocs/op
 BenchmarkPostgres/With_Hooks-4                      5000            354831 ns/op            1122 B/op         27 allocs/op
 PASS
 ok      github.com/gchaincl/sqlhooks    11.713s
You can’t perform that action at this time.