{"id":22540619,"url":"https://github.com/dolmen-go/sqlfunc","last_synced_at":"2026-05-05T05:38:34.679Z","repository":{"id":57514338,"uuid":"242862074","full_name":"dolmen-go/sqlfunc","owner":"dolmen-go","description":"Stronger typing for database/sql prepared statements","archived":false,"fork":false,"pushed_at":"2024-03-28T01:08:00.000Z","size":67,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T23:51:37.381Z","etag":null,"topics":["database","golang-module","golang-package","hacktoberfest","sql"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/dolmen-go/sqlfunc","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/dolmen-go.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":"2020-02-24T22:58:33.000Z","updated_at":"2022-02-18T18:15:23.000Z","dependencies_parsed_at":"2022-09-06T04:00:43.347Z","dependency_job_id":"97fdb242-c693-4175-a9c1-19db45783f09","html_url":"https://github.com/dolmen-go/sqlfunc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fsqlfunc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fsqlfunc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fsqlfunc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fsqlfunc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dolmen-go","download_url":"https://codeload.github.com/dolmen-go/sqlfunc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245984963,"owners_count":20704891,"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":["database","golang-module","golang-package","hacktoberfest","sql"],"created_at":"2024-12-07T12:11:43.296Z","updated_at":"2026-05-05T05:38:34.673Z","avatar_url":"https://github.com/dolmen-go.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlfunc - Stronger typing for [database/sql](https://pkg.go.dev/database/sql) prepared statements\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/dolmen-go/sqlfunc.svg)](https://pkg.go.dev/github.com/dolmen-go/sqlfunc)\n[![CI](https://github.com/dolmen-go/sqlfunc/actions/workflows/test.yml/badge.svg)](https://github.com/dolmen-go/sqlfunc/actions)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n[![Go Report Card](https://goreportcard.com/badge/github.com/dolmen-go/sqlfunc)](https://goreportcard.com/report/github.com/dolmen-go/sqlfunc)\n\n\u003c!--\n[![Coverage](https://codecov.io/gh/dolmen-go/sqlfunc/branch/master/graph/badge.svg)](https://app.codecov.io/gh/dolmen-go/sqlfunc)\n--\u003e\n\n`sqlfunc` is a Go library that simplifies the use of `database/sql` by binding SQL queries directly to strongly-typed Go functions. By leveraging Go generics and reflection, it provides an idiomatic and type-safe API that reduces boilerplate and minimizes common errors like incorrect column scanning or argument count mismatches.\n\n### Key Features\n\n- **Strongly-Typed Query Binding**: Bind `INSERT`, `UPDATE`, `DELETE`, and `SELECT` statements directly to Go function variables. The function signature defines the SQL parameters and the expected result types.\n- **[`Exec`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#Exec), [`QueryRow`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#QueryRow), and [`Query`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#Query)**: Dedicated functions for different SQL operations, ensuring that the bound functions return the appropriate results (`sql.Result`, individual columns, or `*sql.Rows`).\n- **[`ForEach`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#ForEach) Iteration**: A high-level helper for iterating over `*sql.Rows` that automatically scans columns into the arguments of a provided callback function.\n- **Flexible [`Scan`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#Scan) Functions**: Generate reusable functions to scan a single row into pointers or return values, improving readability and reuse.\n- **Transaction Support**: Generated functions can optionally accept a `*sql.Tx` as an argument, allowing them to participate seamlessly in transactions.\n- **`Any` API**: Provides a more flexible, reflection-heavy API for use cases where function signatures are determined at runtime.\n- **Performance-Oriented Registry**: Includes a registry system to cache reflective implementations, designed with future code generation in mind to achieve performance comparable to manual `database/sql` code.\n\n### Example: Binding a Query to a Function\n\n```go\nimport (\n    \"context\"\n    \"database/sql\"\n    \"github.com/dolmen-go/sqlfunc\"\n)\n\n// Define a function variable with the desired signature\nvar getPOI func(ctx context.Context, name string) (lat float64, lon float64, err error)\n\n// Bind the SQL query to the function variable\ncloseStmt, err := sqlfunc.QueryRow(ctx, db,\n    \"SELECT lat, lon FROM poi WHERE name = ?\",\n    \u0026getPOI,\n)\nif err != nil {\n    // handle error\n}\ndefer closeStmt()\n\n// Execute the query using the strongly-typed function\nlat, lon, err := getPOI(ctx, \"Château de Versailles\")\n```\n\n### Example: Using `ForEach` for Iteration\n\n```go\nrows, err := db.QueryContext(ctx, \"SELECT name, lat, lon FROM poi\")\nif err != nil { /* ... */ }\n\n// Scan rows directly into callback arguments\nerr = sqlfunc.ForEach(rows, func(name string, lat, lon float64) {\n    fmt.Printf(\"%s: %.4f, %.4f\\n\", name, lat, lon)\n})\n```\n\n## Documentation\n\n* [Package `sqlfunc`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc)\n* [`sqlfunc.ForEach`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#ForEach)\n* [`sqlfunc.Query`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#Query)\n* [`sqlfunc.QueryRow`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#QueryRow)\n* [`sqlfunc.Exec`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#Exec)\n* [`sqlfunc.Scan`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#Scan)\n* [`sqlfunc.Any.ForEach`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#AnyAPI.ForEach)\n* [`sqlfunc.Any.Query`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#AnyAPI.Query)\n* [`sqlfunc.Any.QueryRow`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#AnyAPI.QueryRow)\n* [`sqlfunc.Any.Exec`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#AnyAPI.Exec)\n* [`sqlfunc.Any.Scan`](https://pkg.go.dev/github.com/dolmen-go/sqlfunc#AnyAPI.Scan)\n\n## Status\n\nProduction ready.\n\nCheck [code coverage by the testsuite](https://app.codecov.io/gh/dolmen-go/sqlfunc).\n\n### Known issues\n\n* There is a speed/memory penalty in using the `sqlfunc` wrappers\n  (check `go test -bench B -benchmem github.com/dolmen-go/sqlfunc`).\n  It is recommended to do your own benchmarks. However there is **work in\n  progress** to add a code generator to reduce cost of runtime `reflect`.\n  Check the [`experiment-gen`](https://github.com/dolmen-go/sqlfunc/commits/experiment-gen/)\n  branch.\n\n### Quality reports\n\nSee quality reports on [SonarQube](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc).\n\n[![SonarQube Cloud](https://sonarcloud.io/images/project_badges/sonarcloud-highlight.svg)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=bugs)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=security_rating)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=dolmen-go_sqlfunc\u0026metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=dolmen-go_sqlfunc)\n\n## License\n\nCopyright 2026 Olivier Mengué\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdolmen-go%2Fsqlfunc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdolmen-go%2Fsqlfunc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdolmen-go%2Fsqlfunc/lists"}