{"id":13393981,"url":"https://github.com/gocraft/dbr","last_synced_at":"2025-05-14T05:10:46.247Z","repository":{"id":16755115,"uuid":"19512853","full_name":"gocraft/dbr","owner":"gocraft","description":"Additions to Go's database/sql for super fast performance and convenience.","archived":false,"fork":false,"pushed_at":"2024-10-04T21:24:44.000Z","size":298,"stargazers_count":1850,"open_issues_count":15,"forks_count":208,"subscribers_count":40,"default_branch":"master","last_synced_at":"2025-04-08T23:17:03.260Z","etag":null,"topics":[],"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/gocraft.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":"2014-05-06T22:20:36.000Z","updated_at":"2025-04-08T20:50:53.000Z","dependencies_parsed_at":"2023-01-13T19:00:27.924Z","dependency_job_id":"da60eecc-f4f5-4152-ab0e-8a0688ace487","html_url":"https://github.com/gocraft/dbr","commit_stats":{"total_commits":216,"total_committers":38,"mean_commits":5.684210526315789,"dds":0.6666666666666667,"last_synced_commit":"3be1adc66f27d0ee006afbe13d16f22afc16af16"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocraft%2Fdbr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocraft%2Fdbr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocraft%2Fdbr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocraft%2Fdbr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gocraft","download_url":"https://codeload.github.com/gocraft/dbr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254076850,"owners_count":22010611,"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":[],"created_at":"2024-07-30T17:01:04.497Z","updated_at":"2025-05-14T05:10:46.210Z","avatar_url":"https://github.com/gocraft.png","language":"Go","funding_links":[],"categories":["开源类库","Go","Open source library","Data and Storage"],"sub_categories":["数据库","Database","Database Clients and ORMs"],"readme":"# gocraft/dbr (database records)\n\n[![GoDoc](https://godoc.org/github.com/gocraft/dbr?status.png)](https://godoc.org/github.com/gocraft/dbr)\n[![FOSSA Status](https://app.fossa.com/api/projects/custom%2B4257%2Fgit%40github.com%3Agocraft%2Fdbr.git.svg?type=shield)](https://app.fossa.com/projects/custom%2B4257%2Fgit%40github.com%3Agocraft%2Fdbr.git?ref=badge_shield)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gocraft/dbr)](https://goreportcard.com/report/github.com/gocraft/dbr)\n[![CircleCI](https://circleci.com/gh/gocraft/dbr.svg?style=svg)](https://circleci.com/gh/gocraft/dbr)\n\ngocraft/dbr provides additions to Go's database/sql for super fast performance and convenience.\n\n```\n$ go get -u github.com/gocraft/dbr/v2\n```\n\n```go\nimport \"github.com/gocraft/dbr/v2\"\n```\n\n## Driver support\n\n* MySQL\n* PostgreSQL\n* SQLite3\n* MsSQL\n\n## Examples\n\nSee [godoc](https://godoc.org/github.com/gocraft/dbr) for more examples.\n\n### Open connections\n\n```go\n// create a connection (e.g. \"postgres\", \"mysql\", or \"sqlite3\")\nconn, _ := Open(\"postgres\", \"...\", nil)\nconn.SetMaxOpenConns(10)\n\n// create a session for each business unit of execution (e.g. a web request or goworkers job)\nsess := conn.NewSession(nil)\n\n// create a tx from sessions\nsess.Begin()\n```\n\n### Create and use Tx\n\n```go\nsess := mysqlSession\ntx, err := sess.Begin()\nif err != nil {\n\treturn\n}\ndefer tx.RollbackUnlessCommitted()\n\n// do stuff...\n\ntx.Commit()\n```\n\n### SelectStmt loads data into structs\n\n```go\n// columns are mapped by tag then by field\ntype Suggestion struct {\n\tID\tint64\t\t// id, will be autoloaded by last insert id\n\tTitle\tNullString\t`db:\"subject\"`\t// subjects are called titles now\n\tUrl\tstring\t\t`db:\"-\"`\t// ignored\n\tsecret\tstring\t\t// ignored\n}\n\n// By default gocraft/dbr converts CamelCase property names to snake_case column_names.\n// You can override this with struct tags, just like with JSON tags.\n// This is especially helpful while migrating from legacy systems.\nvar suggestions []Suggestion\nsess := mysqlSession\nsess.Select(\"*\").From(\"suggestions\").Load(\u0026suggestions)\n```\n\n### SelectStmt with where-value interpolation\n\n```go\n// database/sql uses prepared statements, which means each argument\n// in an IN clause needs its own question mark.\n// gocraft/dbr, on the other hand, handles interpolation itself\n// so that you can easily use a single question mark paired with a\n// dynamically sized slice.\n\nsess := mysqlSession\nids := []int64{1, 2, 3, 4, 5}\nsess.Select(\"*\").From(\"suggestions\").Where(\"id IN ?\", ids)\n```\n\n### SelectStmt with joins\n\n```go\nsess := mysqlSession\nsess.Select(\"*\").From(\"suggestions\").\n\tJoin(\"subdomains\", \"suggestions.subdomain_id = subdomains.id\")\n\nsess.Select(\"*\").From(\"suggestions\").\n\tLeftJoin(\"subdomains\", \"suggestions.subdomain_id = subdomains.id\")\n\n// join multiple tables\nsess.Select(\"*\").From(\"suggestions\").\n\tJoin(\"subdomains\", \"suggestions.subdomain_id = subdomains.id\").\n\tJoin(\"accounts\", \"subdomains.accounts_id = accounts.id\")\n```\n\n### SelectStmt with raw SQL\n\n```go\nSelectBySql(\"SELECT `title`, `body` FROM `suggestions` ORDER BY `id` ASC LIMIT 10\")\n```\n\n### InsertStmt adds data from struct\n\n```go\ntype Suggestion struct {\n\tID\t\tint64\n\tTitle\t\tNullString\n\tCreatedAt\ttime.Time\n}\nsugg := \u0026Suggestion{\n\tTitle:\t\tNewNullString(\"Gopher\"),\n\tCreatedAt:\ttime.Now(),\n}\nsess := mysqlSession\nsess.InsertInto(\"suggestions\").\n\tColumns(\"title\").\n\tRecord(sugg).\n\tExec()\n\n// id is set automatically\nfmt.Println(sugg.ID)\n```\n\n### InsertStmt adds data from value\n\n```go\nsess := mysqlSession\nsess.InsertInto(\"suggestions\").\n\tPair(\"title\", \"Gopher\").\n\tPair(\"body\", \"I love go.\")\n```\n\n\n## Benchmark (2018-05-11)\n\n```\nBenchmarkLoadValues/sqlx_10-8         \t    5000\t    407318 ns/op\t    3913 B/op\t     164 allocs/op\nBenchmarkLoadValues/dbr_10-8          \t    5000\t    372940 ns/op\t    3874 B/op\t     123 allocs/op\nBenchmarkLoadValues/sqlx_100-8        \t    2000\t    584197 ns/op\t   30195 B/op\t    1428 allocs/op\nBenchmarkLoadValues/dbr_100-8         \t    3000\t    558852 ns/op\t   22965 B/op\t     937 allocs/op\nBenchmarkLoadValues/sqlx_1000-8       \t    1000\t   2319101 ns/op\t  289339 B/op\t   14031 allocs/op\nBenchmarkLoadValues/dbr_1000-8        \t    1000\t   2310441 ns/op\t  210092 B/op\t    9040 allocs/op\nBenchmarkLoadValues/sqlx_10000-8      \t     100\t  17004716 ns/op\t 3193997 B/op\t  140043 allocs/op\nBenchmarkLoadValues/dbr_10000-8       \t     100\t  16150062 ns/op\t 2394698 B/op\t   90051 allocs/op\nBenchmarkLoadValues/sqlx_100000-8     \t      10\t 170068209 ns/op\t31679944 B/op\t 1400053 allocs/op\nBenchmarkLoadValues/dbr_100000-8      \t      10\t 147202536 ns/op\t23680625 B/op\t  900061 allocs/op\n```\n\n## Thanks \u0026 Authors\nInspiration from these excellent libraries:\n* [sqlx](https://github.com/jmoiron/sqlx) - various useful tools and utils for interacting with database/sql.\n* [Squirrel](https://github.com/lann/squirrel) - simple fluent query builder.\n\nAuthors:\n* Jonathan Novak -- [https://github.com/cypriss](https://github.com/cypriss)\n* Tai-Lin Chu -- [https://github.com/taylorchu](https://github.com/taylorchu)\n* Sponsored by [UserVoice](https://eng.uservoice.com)\n\nContributors:\n* Paul Bergeron -- [https://github.com/dinedal](https://github.com/dinedal) - SQLite dialect\n\n## License\n[![FOSSA Status](https://app.fossa.com/api/projects/custom%2B4257%2Fgit%40github.com%3Agocraft%2Fdbr.git.svg?type=large)](https://app.fossa.com/projects/custom%2B4257%2Fgit%40github.com%3Agocraft%2Fdbr.git?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgocraft%2Fdbr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgocraft%2Fdbr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgocraft%2Fdbr/lists"}