{"id":17914196,"url":"https://github.com/knadh/goyesql","last_synced_at":"2025-03-23T23:30:45.413Z","repository":{"id":57485372,"uuid":"194046260","full_name":"knadh/goyesql","owner":"knadh","description":"Parse SQL files with multiple named queries and automatically prepare and scan them into structs.","archived":false,"fork":false,"pushed_at":"2022-12-06T10:58:58.000Z","size":27,"stargazers_count":61,"open_issues_count":0,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T00:17:58.547Z","etag":null,"topics":["mysql","postgresql","prepared-statements","sql","sql-files"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/knadh.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}},"created_at":"2019-06-27T07:35:54.000Z","updated_at":"2025-03-06T20:58:29.000Z","dependencies_parsed_at":"2022-08-26T11:10:34.408Z","dependency_job_id":null,"html_url":"https://github.com/knadh/goyesql","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fgoyesql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fgoyesql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fgoyesql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fgoyesql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/knadh","download_url":"https://codeload.github.com/knadh/goyesql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245186437,"owners_count":20574551,"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":["mysql","postgresql","prepared-statements","sql","sql-files"],"created_at":"2024-10-28T19:56:42.188Z","updated_at":"2025-03-23T23:30:45.014Z","avatar_url":"https://github.com/knadh.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca href=\"https://zerodha.tech\"\u003e\u003cimg src=\"https://zerodha.tech/static/images/github-badge.svg\" align=\"right\" /\u003e\u003c/a\u003e\n\n# goyesql\n\n\u003e This package is based on [nleof/goyesql](https://github.com/nleof/goyesql) but is not compatible with it any more. This package introduces support for arbitrary tag types and changes structs and error types.\n\nParses a file and associate SQL queries to a map. Useful for separating SQL from code logic.\n\n# Installation\n\n```\n$ go get -u github.com/knadh/goyesql/v2\n```\n\n## Usage\n\nCreate a file containing your SQL queries\n\n```sql\n-- queries.sql\n\n-- name: list\n-- some: param\n-- some_other: param\nSELECT *\nFROM foo;\n\n-- name: get\nSELECT *\nFROM foo\nWHERE bar = $1;\n```\n\nAnd just call them in your code!\n\n```go\nqueries := goyesql.MustParseFile(\"queries.sql\")\n// use queries[\"list\"] with sql/database, sqlx ...\n// queries[\"list\"].Query is the parsed SQL query string\n// queries[\"list\"].Tags is the list of arbitrary tags (some=param, some_other=param)\n```\n\n## Scanning\n\nOften, it's necessary to scan multiple queries from a SQL file, prepare them into \\*sql.Stmt and use them throught the application. goyesql comes with a helper function that helps with this. Given a goyesql map of queries, it can turn the queries into prepared statements and scan them into a struct that can be passed around.\n\n```go\ntype MySQLQueries struct {\n\t// This will be prepared.\n\tList *sql.Stmt `query:\"list\"`\n\n\t// This will not be prepared.\n\tGet  string    `query:\"get\"`\n}\n\ntype MySQLxQueries struct {\n\t// These will be prepared.\n\tList *sqlx.Stmt `query:\"list\"`\n\tNamedList *sqlx.NamedStmt `query:\"named_list\"`\n\n\t// This will not be prepared.\n\tGet  string    `query:\"get\"`\n}\n\nvar (\n\tq  MySQLQueries\n\tqx MySQLxQueries\n)\n\n// Here, db (*sql.DB) is your live DB connection.\nerr := goyesql.ScanToStruct(\u0026q, queries, db)\nif err != nil {\n\tlog.Fatal(err)\n}\n\n// Here, db (*sqlx.DB) is your live DB connection.\nerr := goyesqlx.ScanToStruct(\u0026qx, queries, db)\nif err != nil {\n\tlog.Fatal(err)\n}\n\n// Then, q.Exec(), q.QueryRow() etc.\n\n```\n\n## Embedding\n\nYou can use [stuffbin](https://github.com/knadh/stuffbin) and `ParseBytes()` for embedding SQL queries in your binary.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknadh%2Fgoyesql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknadh%2Fgoyesql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknadh%2Fgoyesql/lists"}