{"id":16326619,"url":"https://github.com/izolate/yesql","last_synced_at":"2025-07-15T07:33:52.723Z","repository":{"id":64846599,"uuid":"460984300","full_name":"izolate/yesql","owner":"izolate","description":"A tool to write raw SQL in Go more effectively","archived":false,"fork":false,"pushed_at":"2023-02-20T23:28:20.000Z","size":83,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-14T22:12:14.786Z","etag":null,"topics":["go","named-arguments","sql","templates","utilities"],"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/izolate.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":"2022-02-18T19:43:45.000Z","updated_at":"2023-07-11T19:05:05.000Z","dependencies_parsed_at":"2024-06-21T02:03:50.953Z","dependency_job_id":"1099b856-b6c2-427f-8e2b-53ad9af4d096","html_url":"https://github.com/izolate/yesql","commit_stats":{"total_commits":77,"total_committers":1,"mean_commits":77.0,"dds":0.0,"last_synced_commit":"5d0049007f695877d9f9352ce3eb9497c812327f"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/izolate/yesql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izolate%2Fyesql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izolate%2Fyesql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izolate%2Fyesql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izolate%2Fyesql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izolate","download_url":"https://codeload.github.com/izolate/yesql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izolate%2Fyesql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262502589,"owners_count":23321168,"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":["go","named-arguments","sql","templates","utilities"],"created_at":"2024-10-10T23:09:02.777Z","updated_at":"2025-06-28T21:33:18.343Z","avatar_url":"https://github.com/izolate.png","language":"Go","readme":"# yesql\n\nyesql is a lightweight Go library that adds convenience features to the standard library\npackage `database/sql`. It's specifically designed for writing raw SQL queries,\nwith a focus on providing greater efficiency and ease-of-use.\n\nyesql leans on the standard library, keeping the same API, making it an ideal\ndrop-in replacement for `database/sql`.\n\nIt's a WIP and currently provides the following features:\n\n* Named arguments (e.g. `SELECT * FROM foo WHERE id = @ID`)\n* Templates for query building (cached and thread-safe)\n* Statement logging\n* Same API as `database/sql`\n\n### The Elevator Pitch™\n\n\u003e yesql is like a child born from the union of `database/sql` and\n\u003e `text/templates`.\n\n## Quick start\n\nStart by opening a connection to a database and initializing a database driver:\n\n```go\npackage foo\n\nimport (\n    \"github.com/izolate/yesql\"\n    _ \"github.com/lib/pq\"\n)\n\nfunc main() {\n    db, err := yesql.Open(\"postgres\", \"host=localhost user=foo sslmode=disable\")\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\n### `Exec` / `ExecContext`\n\nYou can use the `Exec` function to execute a query without returning data. Named\nparameters (`@Foo`) allow you to bind arguments to map (or struct) fields\nwithout the risk of SQL injection.\n\n```go\ntype Book struct {\n    ID     string\n    Title  string\n    Author string\n}\n\nfunc InsertBook(b Book) error {\n    q := `INSERT INTO users (id, title, author) VALUES (@ID, @Title, @Author)`\n    _, err := db.Exec(q, b)\n    return err\n}\n```\n\n\u003e :frog: NOTE: Named parameters only work with exported fields.\n\n### `Query` / `QueryContext`\n\nUse `Query` to execute a query and return data. Templates allow you to perform\ncomplex logic without string concatenation or query building.\n\nThe templates alter the final SQL statement based on the input provided.\n\n```go\ntype BookSearch struct {\n    Author string    \n    Title  string\n    Genre  string\n}\n\nconst sqlSearchBooks = `\nSELECT * FROM books\nWHERE author = @Author\n{{if .Title}}AND title ILIKE @Title{{end}}\n{{if .Genre}}AND genre = @Genre{{end}}\n`\n\nfunc SearchBooks(s BookSearch) ([]Book, error) {\n    rows, err := db.Query(sqlSearchBooks, s)\n    if err != nil {\n        return nil, err\n    }\n    books := []Book{}\n    for rows.Next() {\n        var b Book\n        if err := rows.Scan(\u0026b.ID, \u0026b.Title, \u0026b.Author, \u0026b.Genre); err != nil {\n            return nil, err\n        }\n        books = append(books, b)\n    }\n    return books, nil\n}\n```\n\nPositional scanning is inflexible. Instead, use `db` struct tags and\n`rows.ScanStruct()` to scan into a struct:\n\n```go\ntype Book struct {\n    ID     string `db:\"id\"`\n    Title  string `db:\"title\"`\n    Author string `db:\"author\"`\n}\n\nfunc SearchBooks(s BookSearch) ([]Book, error) {\n    rows, err := db.Query(sqlSearchBooks, s)\n    if err != nil {\n        return nil, err\n    }\n    books := []Book{}\n    for rows.Next() {\n        var b Book\n        if err := rows.ScanStruct(\u0026b); err != nil {\n            return nil, err\n        }\n        books = append(books, b)\n    }\n    return books, nil\n}\n```\n\n## Configuration\n\nYou can easily configure yesql using a list of functional config setters, which\nare accepted by all entry points. For instance, to disable statement logging,\nyou can pass in the `OptQuiet` setter as follows:\n\n```go\ndb, err := yesql.Open(\"postgres\", \"host=localhost user=foo sslmode=disable\", yesql.OptQuiet())\n```\n\nThis will ensure that statement logs are not printed during execution.\n\n## Feature checklist\n\n- [x] Templated SQL statements\n- [x] Named arguments (bindvars)\n- [x] Statement logging\n- [ ] Query tracing\n- [x] Struct scanning\n- [x] Unicode support\n- [x] Postgres support\n- [ ] Prepared statements\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizolate%2Fyesql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizolate%2Fyesql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizolate%2Fyesql/lists"}