{"id":30139728,"url":"https://github.com/hnh/qry","last_synced_at":"2025-08-11T02:53:52.829Z","repository":{"id":46683863,"uuid":"203339018","full_name":"HnH/qry","owner":"HnH","description":"Write your SQL queries in raw files with all benefits of modern IDEs, use them in an easy way inside your application with all the profit of compile time constants","archived":false,"fork":false,"pushed_at":"2024-02-20T18:44:16.000Z","size":21,"stargazers_count":35,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:49:56.815Z","etag":null,"topics":["go","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HnH.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":"2019-08-20T09:01:00.000Z","updated_at":"2024-02-27T10:37:40.000Z","dependencies_parsed_at":"2024-02-20T19:47:52.766Z","dependency_job_id":"1d8b9fed-f5a3-44fc-b6c0-977932cc9ded","html_url":"https://github.com/HnH/qry","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/HnH/qry","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HnH%2Fqry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HnH%2Fqry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HnH%2Fqry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HnH%2Fqry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HnH","download_url":"https://codeload.github.com/HnH/qry/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HnH%2Fqry/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269823338,"owners_count":24480778,"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","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["go","golang","sql"],"created_at":"2025-08-11T02:53:49.207Z","updated_at":"2025-08-11T02:53:52.804Z","avatar_url":"https://github.com/HnH.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![buddy pipeline](https://eu.buddy.works/treinis/qry/pipelines/pipeline/191916/badge.svg?token=b46003623254cc1f4e162f3911b6075d87b9121676c88ffebfbe34d562d75d96 \"buddy pipeline\")](https://eu.buddy.works/treinis/qry/pipelines/pipeline/191916)\n[![codecov](https://codecov.io/gh/HnH/qry/branch/master/graph/badge.svg)](https://codecov.io/gh/HnH/qry)\n[![Go Report Card](https://goreportcard.com/badge/github.com/HnH/qry)](https://goreportcard.com/report/github.com/HnH/qry)\n[![Godoc Reference](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/HnH/qry)\n\n# About\n\n**qry** is a general purpose library for storing your raw database queries in .sql files with all benefits of modern IDEs, instead of strings and constants in the code, and using them in an easy way inside your application with all the profit of compile time constants.\n\n**qry** recursively loads all .sql files from a specified folder, parses them according to predefined rules and returns a reusable object, which is actually just a `map[string]string` with some sugar. Multiple queries inside a single file are separated with standard SQL comment syntax: `-- qry: QueryName`. A `QueryName` must match `[A-Za-z_]+`.\n\n[gen](https://github.com/HnH/qry/tree/master/cmd/qry-gen) tool is used for automatic generation of constants for all user specified `query_names`.\n\n# Installation\n\n`go install github.com/HnH/qry/cmd/qry-gen@latest` \n\n# Usage\n\nPrepare sql files: `queries/one.sql`:\n\n```sql\n-- qry: InsertUser\nINSERT INTO `users` (`name`) VALUES (?);\n\n-- qry: GetUserById\nSELECT * FROM `users` WHERE `user_id` = ?;\n```\n\nAnd the second one `queries/two.sql`:\n\n```sql\n-- qry: DeleteUsersByIds\nDELETE FROM `users` WHERE `user_id` IN ({ids});\n```\n\n[Gen](https://github.com/HnH/qry/tree/master/cmd/qry-gen)erate constants: `qry-gen -dir=./queries -pkg=/path/to/your/go/pkg` Will produce `/path/to/your/go/pkg/qry.go` with:\n\n```go\npackage pkg\n\nconst (\n\t// one.sql\n\tInsertUser  = \"INSERT INTO `users` (`name`) VALUES (?);\"\n\tGetUserById = \"SELECT * FROM `users` WHERE `user_id` = ?;\"\n\n\t// two.sql\n\tDeleteUsersByIds = \"DELETE FROM `users` WHERE `user_id` IN ({ids});\"\n)\n```\n\nAs a best practice include this qry-gen call in your source code with go:generate prefix: `//go:generate qry-gen -dir=./queries -pkg=/path/to/your/go/pkg` and just execute `go generate` before each build.\nNow it's time to use **qry** inside your project:\n\n```go\nfunc main() {\n\t/**\n\t * The most obvious way is to use generated constants in the source code\n\t */\n\t \n\t// INSERT INTO `users` (`name`) VALUES (?);\n\tprintln(pkg.InsertUser)\n\t\n\t// DELETE FROM `users` WHERE `user_id` IN (?,?,?);\n\tprintln(qry.Query(pkg.DeleteUsersByIds).Replace(\"{ids}\", qry.In(3)))\n\t\n\t/**\n\t * As an alternative you can manually parse .sql files in the directory and work with output\n\t */\n\tif q, err := qry.Dir(\"/path/to/your/go/pkg/queries\"); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// SELECT * FROM `users` WHERE `user_id` = ?;\n\tprintln(q[\"one.sql\"][\"GetUserById\"])\n  \n\t// DELETE FROM `users` WHERE `user_id` IN (?,?,?);\n\tprintln(q[\"two.sql\"][\"DeleteUsersByIds\"].Replace(\"{ids}\", qry.In(3)))\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhnh%2Fqry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhnh%2Fqry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhnh%2Fqry/lists"}