{"id":28518041,"url":"https://github.com/sourcegraph/querygen","last_synced_at":"2026-03-15T22:41:53.501Z","repository":{"id":243386605,"uuid":"812289705","full_name":"sourcegraph/querygen","owner":"sourcegraph","description":"CLI + tiny library for more readable SQL queries","archived":false,"fork":false,"pushed_at":"2024-06-09T12:35:50.000Z","size":56,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-03-08T19:55:35.191Z","etag":null,"topics":["golang","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sourcegraph.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}},"created_at":"2024-06-08T13:23:21.000Z","updated_at":"2025-04-04T04:40:35.000Z","dependencies_parsed_at":"2024-06-19T03:00:38.147Z","dependency_job_id":"d4085d9b-4d16-44b7-99c8-47eb7a1ba5d1","html_url":"https://github.com/sourcegraph/querygen","commit_stats":null,"previous_names":["sourcegraph/querygen"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/sourcegraph/querygen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcegraph%2Fquerygen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcegraph%2Fquerygen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcegraph%2Fquerygen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcegraph%2Fquerygen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sourcegraph","download_url":"https://codeload.github.com/sourcegraph/querygen/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcegraph%2Fquerygen/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30553324,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-15T15:03:43.933Z","status":"ssl_error","status_checked_at":"2026-03-15T15:03:37.630Z","response_time":61,"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":["golang","sql"],"created_at":"2025-06-09T05:35:19.077Z","updated_at":"2026-03-15T22:41:53.496Z","avatar_url":"https://github.com/sourcegraph.png","language":"Go","readme":"# querygen: CLI + tiny library for readable SQL queries\n\n`querygen` enables writing [parameterized SQL queries](https://en.wikipedia.org/wiki/Prepared_statement)\nusing string interpolation-like syntax. Since Go lacks\nfeatures like macros, quasiquotes or native string interpolation,\nwe resort to ~heinous crimes~ codegen.\n\n## Usage\n\nWrite a SQL query in your Go code using interpolation-like syntax:\n\n```go\npackage cakes // cakes.go\n\nconst partyAttendeesQuery = `\nSELECT person_name\nFROM party_attendees\nWHERE party = {{partyId : int}}\n`\n\nconst bestChoiceCakeQuery = `\nWITH attendees AS (` + partyAttendeesQuery + `)\n\nSELECT fave_cakes.cake_type\nFROM attendees JOIN fave_cakes ON attendees.person_name = fave_cakes.person_name\n-- Need to allow host to exclude one cake they don't like\nWHERE fave_cake.cake_type != {{excludedCakeType : string}}\nGROUP BY fave_cake.cake_type \nORDER BY COUNT(fave_cake.cake_type) DESC\nLIMIT 1\n`\n```\n\nGet the [`querygen` CLI](https://github.com/sourcegraph/querygen/releases)\nand run it on the package.\n\n```bash\ngo install github.com/sourcegraph/querygen/cmd/querygen@latest\nquerygen ./...\n```\n\nThis will generate a file next to the original file:\n\n```go\n// Code generated by querygen.\n// You may only edit import statements.\npackage cakes // cakes_query_gen.go\n\nimport (\n\t\"github.com/sourcegraph/querygen/lib/interpolate\"\n)\n\ntype partyAttendeesQueryVars struct {\n\tpartyId int\n}\n\nvar _ interpolate.QueryVars = \u0026partyAttendeesQueryVars{}\n\n// methods omitted...\n\ntype bestChoiceCakeQueryVars struct {\n\tpartyId          int\n\texcludedCakeType string\n}\n\nvar _ interpolate.QueryVars = \u0026bestChoiceCakeQueryVars{}\n\n// methods omitted...\n```\n\nYou can use these structs with `interpolate.Do(myQuery, \u0026myQueryVars{...})` function\nto generate a [`*sqlf.Query`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/keegancsmith/sqlf%24%40master+file:sqlf.go+type:symbol+Query\u0026patternType=keyword\u0026sm=0)\nwhich can then be executed. The `interpolate.Do` function replaces the `sqlf.Sprintf`\nfunction.\n\nFor more complex usage, see [Reference.md](docs/Reference.md).\n\n## Motivation and Comparison\n\nThe Sourcegraph monorepo largely uses the [`sqlf`](github.com/keegancsmith/sqlf) library\nfor constructing SQL queries. `querygen` sits on top of `sqlf` to enable:\n\n1. Incrementally improving the readability of existing queries\n2. Add compile-time checking for names and number of arguments\n\nwithout having to switch to an alternate SQL driver.\n\n| Tool/Library                          |    `sqlf`    |  `querygen`   |  `pgx`   |    `sqlc`     |\n|---------------------------------------|:------------:|:-------------:|:--------:|:-------------:|\n| Operation                             |   Run-time   | ~Compile-time | Run-time | ~Compile-time |\n| Named parameters in query syntax      |      ❌       |       ✅       |    ✅     |       ✅       |\n| Static parameter name checking        |      ❌       |       ✅       |    ❌     |       ✅       | \n| Static binding var count checking (†) |      ❌       |       ✅       |    ❌     |       ✅       |\n| Arbitrary dynamic query fragments     |      ✅       |       ✅       |    ✅     |       ❌       |\n| Static query validation               |      ❌       |       ❌       |    ❌     |       ✅       |\n| Statically checked row scanning       |      ❌       |       ❌       |    ❌     |       ✅       |\n\n(†) Caveat: Static binding var count checking requires using\nthe [exhaustruct](https://golangci-lint.run/usage/linters/#exhaustruct)\nlinter or similar for the types generated for query variables.\n\nWe could probably use `sqlc` for common cases,\nbut the Sourcegraph codebase has some [complex dynamically assembled queries](https://sourcegraph.com/github.com/sourcegraph/sourcegraph@d288874197bed9c219c20a01cd7786e1d2aa6e11/-/blob/internal/batches/store/batch_changes.go?L612-697)\nwhich cannot be statically analyzed by `sqlc`.\nSo those would need to be either re-written to fit into\n`sqlc`'s model, or you'd need some sophisticated\n[abstract interpretation](https://en.wikipedia.org/wiki/Abstract_interpretation)\nto extract all possible queries and analyze them individually.\n\n## Contributing\n\nSee [Development.md](docs/Development.md) for build instructions etc.\n\nAt the moment, this is made primarily for Sourcegraph's internal use.\nUsage outside Sourcegraph is not supported.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourcegraph%2Fquerygen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsourcegraph%2Fquerygen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourcegraph%2Fquerygen/lists"}