### Install sqlhooks/v2 Source: https://github.com/qustavo/sqlhooks/blob/master/README.md Installs the latest version of the sqlhooks library using go get. Requires Go version 1.14 or higher. ```bash go get github.com/qustavo/sqlhooks/v2 ``` -------------------------------- ### Install Older Versions of sqlhooks Source: https://github.com/qustavo/sqlhooks/blob/master/README.md Installs older, non-backward compatible versions of the sqlhooks library. These can be fetched using go modules or directly from gopkg.in. ```bash go get github.com/qustavo/sqlhooks go get gopkg.in/qustavo/sqlhooks.v1 ``` -------------------------------- ### Instrument SQL Queries with Hooks in Go Source: https://github.com/qustavo/sqlhooks/blob/master/README.md Demonstrates how to attach custom hooks to SQL queries in Go to measure execution time. It defines a Hooks struct that implements the Before and After methods to log query start and duration. ```go // 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/qustavo/sqlhooks/v2" "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 */ ``` -------------------------------- ### SQLHooks Benchmarks Source: https://github.com/qustavo/sqlhooks/blob/master/README.md Compares the performance of database operations with and without sqlhooks across different database drivers (SQLite3, MySQL, Postgres). The benchmarks show minimal performance overhead when using hooks. ```bash go test -bench=. -benchmem goos: linux goarch: amd64 pkg: github.com/qustavo/sqlhooks/v2 cpu: Intel(R) Xeon(R) W-10885M CPU @ 2.40GHz BenchmarkSQLite3/Without_Hooks-16 191196 6163 ns/op 456 B/op 14 allocs/op BenchmarkSQLite3/With_Hooks-16 189997 6329 ns/op 456 B/op 14 allocs/op BenchmarkMySQL/Without_Hooks-16 13278 83462 ns/op 309 B/op 7 allocs/op BenchmarkMySQL/With_Hooks-16 13460 87331 ns/op 309 B/op 7 allocs/op BenchmarkPostgres/Without_Hooks-16 13016 91421 ns/op 401 B/op 10 allocs/op BenchmarkPostgres/With_Hooks-16 12339 94033 ns/op 401 B/op 10 allocs/op PASS ok github.com/qustavo/sqlhooks/v2 10.294s ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.