{"id":38643907,"url":"https://github.com/go-simpler/queries","last_synced_at":"2026-01-17T09:11:21.589Z","repository":{"id":235522963,"uuid":"790859247","full_name":"go-simpler/queries","owner":"go-simpler","description":"🛢️ Convenience helpers for working with SQL queries","archived":false,"fork":false,"pushed_at":"2025-06-15T09:16:09.000Z","size":68,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-11T16:07:02.481Z","etag":null,"topics":["dependency-free","go","library","query-builder","sql","sql-query"],"latest_commit_sha":null,"homepage":"https://go-simpler.org/queries","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-simpler.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-04-23T16:55:50.000Z","updated_at":"2025-08-31T08:34:26.000Z","dependencies_parsed_at":"2025-02-23T22:23:29.921Z","dependency_job_id":"7bfac7bc-32e2-4f44-aed0-4589e4409809","html_url":"https://github.com/go-simpler/queries","commit_stats":null,"previous_names":["go-simpler/queries"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/go-simpler/queries","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-simpler%2Fqueries","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-simpler%2Fqueries/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-simpler%2Fqueries/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-simpler%2Fqueries/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-simpler","download_url":"https://codeload.github.com/go-simpler/queries/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-simpler%2Fqueries/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28504912,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T06:57:29.758Z","status":"ssl_error","status_checked_at":"2026-01-17T06:56:03.931Z","response_time":85,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["dependency-free","go","library","query-builder","sql","sql-query"],"created_at":"2026-01-17T09:11:21.065Z","updated_at":"2026-01-17T09:11:21.541Z","avatar_url":"https://github.com/go-simpler.png","language":"Go","readme":"# queries\n\n[![checks](https://github.com/go-simpler/queries/actions/workflows/checks.yml/badge.svg)](https://github.com/go-simpler/queries/actions/workflows/checks.yml)\n[![pkg.go.dev](https://pkg.go.dev/badge/go-simpler.org/queries.svg)](https://pkg.go.dev/go-simpler.org/queries)\n[![goreportcard](https://goreportcard.com/badge/go-simpler.org/queries)](https://goreportcard.com/report/go-simpler.org/queries)\n[![codecov](https://codecov.io/gh/go-simpler/queries/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/queries)\n\nConvenience helpers for working with SQL queries.\n\n## 🚀 Features\n\n- Builder: a `strings.Builder` wrapper with added support for placeholder verbs to easily build raw SQL queries conditionally.\n- Scanner: `sql.DB.Query/QueryRow` wrappers that automatically scan `sql.Rows` into the given struct. Inspired by [golang/go#61637][1].\n- Interceptor: a `driver.Driver` wrapper to easily add instrumentation (logs, metrics, traces) to the database layer. Similar to [gRPC interceptors][2].\n\n## 📦 Install\n\nGo 1.24+\n\n```shell\ngo get go-simpler.org/queries\n```\n\n## 📋 Usage\n\n### Builder\n\n```go\ncolumns := []string{\"id\", \"name\"}\n\nvar qb queries.Builder\nqb.Appendf(\"SELECT %s FROM users\", strings.Join(columns, \", \"))\n\nif role != nil { // \"admin\"\n    qb.Appendf(\" WHERE role = %$\", *role)\n}\nif orderBy != nil { // \"name\"\n    qb.Appendf(\" ORDER BY %$\", *orderBy)\n}\nif limit != nil { // 10\n    qb.Appendf(\" LIMIT %$\", *limit)\n}\n\nquery, args := qb.Build()\ndb.QueryContext(ctx, query, args...)\n// Query: \"SELECT id, name FROM users WHERE role = $1 ORDER BY $2 LIMIT $3\"\n// Args: [\"admin\", \"name\", 10]\n```\n\nThe following database placeholders are supported:\n- `?` (used by MySQL and SQLite)\n- `$1`, `$2`, ..., `$N` (used by PostgreSQL)\n- `@p1`, `@p2`, ..., `@pN` (used by Microsoft SQL Server)\n- `:1`, `:2`, ..., `:N` (used by Oracle Database):\n\n### Scanner\n\n```go\ntype User struct {\n    ID   int    `sql:\"id\"`\n    Name string `sql:\"name\"`\n}\n\n// single column, single row:\nname, _ := queries.QueryRow[string](ctx, db, \"SELECT name FROM users WHERE id = 1\")\n\n// single column, multiple rows:\nnames, _ := queries.Collect(queries.Query[string](ctx, db, \"SELECT name FROM users\"))\n\n// multiple columns, single row:\nuser, _ := queries.QueryRow[User](ctx, db, \"SELECT id, name FROM users WHERE id = 1\")\n\n// multiple columns, multiple rows:\nfor user, _ := range queries.Query[User](ctx, db, \"SELECT id, name FROM users\") {\n    // ...\n}\n```\n\n### Interceptor\n\n```go\ninterceptor := queries.Interceptor{\n    Driver: // database driver of your choice.\n    ExecContext: func(ctx context.Context, query string, args []driver.NamedValue, execer driver.ExecerContext) (driver.Result, error) {\n        slog.InfoContext(ctx, \"ExecContext\", \"query\", query)\n        return execer.ExecContext(ctx, query, args)\n    },\n    QueryContext: func(ctx context.Context, query string, args []driver.NamedValue, queryer driver.QueryerContext) (driver.Rows, error) {\n        slog.InfoContext(ctx, \"QueryContext\", \"query\", query)\n        return queryer.QueryContext(ctx, query, args)\n    },\n}\n\nsql.Register(\"interceptor\", interceptor)\ndb, _ := sql.Open(\"interceptor\", \"dsn\")\n\ndb.ExecContext(ctx, \"INSERT INTO users VALUES (1, 'John Doe')\")\n// stderr: INFO ExecContext query=\"INSERT INTO users VALUES (1, 'John Doe')\"\n\ndb.QueryContext(ctx, \"SELECT id, name FROM users\")\n// stderr: INFO QueryContext query=\"SELECT id, name FROM users\"\n```\n\nIntegration tests cover the following databases and drivers:\n- PostgreSQL with [lib/pq][3], [jackx/pgx][4]\n- MySQL with [go-sql-driver/mysql][5]\n- SQLite with [modernc.org/sqlite][6]\n- Microsoft SQL Server with [microsoft/go-mssqldb][7]\n- Oracle Database with [sijms/go-ora][8]\n\nSee [integration_test.go](tests/integration_test.go) for details.\n\n[1]: https://github.com/golang/go/issues/61637\n[2]: https://grpc.io/docs/guides/interceptors\n[3]: https://github.com/lib/pq\n[4]: https://github.com/jackc/pgx\n[5]: https://github.com/go-sql-driver/mysql\n[6]: https://gitlab.com/cznic/sqlite\n[7]: https://github.com/microsoft/go-mssqldb\n[8]: https://github.com/sijms/go-ora\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-simpler%2Fqueries","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-simpler%2Fqueries","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-simpler%2Fqueries/lists"}