{"id":13412289,"url":"https://github.com/qustavo/sqlhooks","last_synced_at":"2026-01-29T08:07:49.215Z","repository":{"id":41174656,"uuid":"56711473","full_name":"qustavo/sqlhooks","owner":"qustavo","description":"Attach hooks to any database/sql driver","archived":false,"fork":false,"pushed_at":"2024-06-27T16:54:20.000Z","size":110,"stargazers_count":655,"open_issues_count":9,"forks_count":42,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-10T05:14:58.404Z","etag":null,"topics":["database","driver","golang","instrumentation","opentracing","sql","sql-driver","tracing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qustavo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-04-20T18:37:14.000Z","updated_at":"2025-03-18T15:41:38.000Z","dependencies_parsed_at":"2024-10-26T08:35:58.154Z","dependency_job_id":"8ad64c53-8501-4b59-af41-759cd0bd65c8","html_url":"https://github.com/qustavo/sqlhooks","commit_stats":{"total_commits":108,"total_committers":11,"mean_commits":9.818181818181818,"dds":0.2592592592592593,"last_synced_commit":"7875602513fa745b19df8c03f4d31c00b61be6d4"},"previous_names":["gchaincl/sqlhooks"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qustavo%2Fsqlhooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qustavo%2Fsqlhooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qustavo%2Fsqlhooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qustavo%2Fsqlhooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qustavo","download_url":"https://codeload.github.com/qustavo/sqlhooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248695438,"owners_count":21146954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","driver","golang","instrumentation","opentracing","sql","sql-driver","tracing"],"created_at":"2024-07-30T20:01:23.013Z","updated_at":"2026-01-29T08:07:44.162Z","avatar_url":"https://github.com/qustavo.png","language":"Go","readme":"# sqlhooks\n![Build Status](https://github.com/qustavo/sqlhooks/actions/workflows/test.yml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/qustavo/sqlhooks)](https://goreportcard.com/report/github.com/qustavo/sqlhooks)\n[![Coverage Status](https://coveralls.io/repos/github/qustavo/sqlhooks/badge.svg?branch=master)](https://coveralls.io/github/qustavo/sqlhooks?branch=master)\n\nAttach hooks to any database/sql driver.\n\nThe 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.\n\n# Install\n```bash\ngo get github.com/qustavo/sqlhooks/v2\n```\nRequires Go \u003e= 1.14.x\n\n## Breaking changes\n`V2` isn't backward compatible with previous versions, if you want to fetch old versions, you can use go modules or get them from [gopkg.in](http://gopkg.in/)\n```bash\ngo get github.com/qustavo/sqlhooks\ngo get gopkg.in/qustavo/sqlhooks.v1\n```\n\n# Usage [![GoDoc](https://godoc.org/github.com/qustavo/dotsql?status.svg)](https://godoc.org/github.com/qustavo/sqlhooks)\n\n```go\n// This example shows how to instrument sql queries in order to display the time that they consume\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/qustavo/sqlhooks/v2\"\n\t\"github.com/mattn/go-sqlite3\"\n)\n\n// Hooks satisfies the sqlhook.Hooks interface\ntype Hooks struct {}\n\n// Before hook will print the query with it's args and return the context with the timestamp\nfunc (h *Hooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {\n\tfmt.Printf(\"\u003e %s %q\", query, args)\n\treturn context.WithValue(ctx, \"begin\", time.Now()), nil\n}\n\n// After hook will get the timestamp registered on the Before hook and print the elapsed time\nfunc (h *Hooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {\n\tbegin := ctx.Value(\"begin\").(time.Time)\n\tfmt.Printf(\". took: %s\\n\", time.Since(begin))\n\treturn ctx, nil\n}\n\nfunc main() {\n\t// First, register the wrapper\n\tsql.Register(\"sqlite3WithHooks\", sqlhooks.Wrap(\u0026sqlite3.SQLiteDriver{}, \u0026Hooks{}))\n\n\t// Connect to the registered wrapped driver\n\tdb, _ := sql.Open(\"sqlite3WithHooks\", \":memory:\")\n\n\t// Do you're stuff\n\tdb.Exec(\"CREATE TABLE t (id INTEGER, text VARCHAR(16))\")\n\tdb.Exec(\"INSERT into t (text) VALUES(?), (?)\", \"foo\", \"bar\")\n\tdb.Query(\"SELECT id, text FROM t\")\n}\n\n/*\nOutput should look like:\n\u003e CREATE TABLE t (id INTEGER, text VARCHAR(16)) []. took: 121.238µs\n\u003e INSERT into t (text) VALUES(?), (?) [\"foo\" \"bar\"]. took: 36.364µs\n\u003e SELECT id, text FROM t []. took: 4.653µs\n*/\n```\n\n# Benchmarks\n```\n go test -bench=. -benchmem\n goos: linux\n goarch: amd64\n pkg: github.com/qustavo/sqlhooks/v2\n cpu: Intel(R) Xeon(R) W-10885M CPU @ 2.40GHz\n BenchmarkSQLite3/Without_Hooks-16                 191196              6163 ns/op             456 B/op         14 allocs/op\n BenchmarkSQLite3/With_Hooks-16                    189997              6329 ns/op             456 B/op         14 allocs/op\n BenchmarkMySQL/Without_Hooks-16                    13278             83462 ns/op             309 B/op          7 allocs/op\n BenchmarkMySQL/With_Hooks-16                       13460             87331 ns/op             309 B/op          7 allocs/op\n BenchmarkPostgres/Without_Hooks-16                 13016             91421 ns/op             401 B/op         10 allocs/op\n BenchmarkPostgres/With_Hooks-16                    12339             94033 ns/op             401 B/op         10 allocs/op\n PASS\n ok      github.com/qustavo/sqlhooks/v2  10.294s\n```\n","funding_links":[],"categories":["Relational Databases","数据库驱动程序","Database Drivers","Data Integration Frameworks","Generators","Go"],"sub_categories":["关系数据库驱动程序","Relational Database Drivers"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqustavo%2Fsqlhooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqustavo%2Fsqlhooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqustavo%2Fsqlhooks/lists"}