{"id":38340319,"url":"https://github.com/mattmeyers/qb","last_synced_at":"2026-01-17T02:59:37.429Z","repository":{"id":57504964,"uuid":"214284272","full_name":"mattmeyers/qb","owner":"mattmeyers","description":"A simple query builder","archived":false,"fork":false,"pushed_at":"2020-07-16T02:01:13.000Z","size":72,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T16:51:03.648Z","etag":null,"topics":["go","golang","golang-library","query-builder","sql"],"latest_commit_sha":null,"homepage":null,"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/mattmeyers.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-10-10T20:57:25.000Z","updated_at":"2020-07-16T02:01:16.000Z","dependencies_parsed_at":"2022-09-19T10:01:32.258Z","dependency_job_id":null,"html_url":"https://github.com/mattmeyers/qb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mattmeyers/qb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmeyers%2Fqb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmeyers%2Fqb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmeyers%2Fqb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmeyers%2Fqb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattmeyers","download_url":"https://codeload.github.com/mattmeyers/qb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmeyers%2Fqb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28492593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T02:39:23.645Z","status":"ssl_error","status_checked_at":"2026-01-17T02:34:19.649Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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","golang-library","query-builder","sql"],"created_at":"2026-01-17T02:59:36.749Z","updated_at":"2026-01-17T02:59:37.421Z","avatar_url":"https://github.com/mattmeyers.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# qb\n\n\n## *qb is still in active development. The API is unstable. Use with caution.*\n\n`qb` is a simple SQL query builder. Every method returns a struct of the same type allowing an entire query to be built in one function call chain. This library's only job is to generate properly formatted SQL queries.\n\nEvery query builder struct implements the Builder interface defined as\n\n```go\ntype Builder interface {\n    Build() (string, []interface{}, error)\n}\n```\n\nAfter setting all of the query's values, call `Build()` to retrieve the query and a slice of correctly ordered parameters.\n\n## Install\n\n```\ngo get -u github.com/mattmeyers/qb\n```\n\n## Generating Queries\n\nQueries are built using unexported structs.  Therefore, methods should be chained following the initialization function for the given type of query.\n\n### Select\n\nA select query can be initialized with the `Select(cols ...string)` function.  The struct returned from this function call can then call the following functions:\n\n- `Select(cols ...string) *selectQuery`\n- `Distinct(cols ...string) *selectQuery`\n- `SetCols(cols ...string) *selectQuery`\n- `From(table interface{}) *selectQuery`\n- `InnerJoin(table string, condition interface{}) *selectQuery`\n- `LeftJoin(table string, condition interface{}) *selectQuery`\n- `RightJoin(table string, condition interface{}) *selectQuery`\n- `FullJoin(table string, condition interface{}) *selectQuery`\n- `CrossJoin(table string, condition interface{}) *selectQuery`\n- `Where(pred Builder) *selectQuery`\n- `Limit(l int) *selectQuery`\n- `ClearLimit() *selectQuery`\n- `Offset(o int) *selectQuery`\n- `ClearOffset() *selectQuery`\n- `GroupBy(cols ...string) *selectQuery`\n- `Having(pred Builder) *selectQuery`\n- `OrderBy(col string, dir OrderDir) *selectQuery`\n- `RebindWith(r Rebinder) *selectQuery`\n- `String() string`\n- `Build() (string, []interface{}, error)`\n\n\nFor example, in order to generate the query\n\n```sql\nSELECT id, display_name FROM products WHERE (item_number=? OR item_number=?) AND backordered=?\n```\n\nuse the following code:\n\n```go\nqb.Select(\"id\", \"display_name\").\n   From(\"products\").\n   Where(\"item_number\", \"=\", \"a123\").\n   Where(qb.Or{\n      qb.Eq(\"item_number\", \"a123\"),\n      qb.Eq(\"item_number\", \"b456\"),\n   }).\n   Where(qb.Eq(\"backordered\", false)),\n   Build()\n```\n\n### Insert\n\nAn insert query can be initialized with the `InsertInto(table string)` function.  The struct returned from this function call can then call the following functions:\n\n- `Col(col string, val interface{}) *insertQuery`\n- `Cols(cols []string, vals ...interface{}) *insertQuery`\n- `OnConflict(target, action interface{}) *insertQuery`\n- `Returning(cols ...string) *insertQuery`\n- `RebindWith(r Rebinder) *insertQuery`\n- `String() string`\n- `Build() (string, []interface{}, error)`\n\nCalling `Columns` or `Values` mulitple times will append the passed values to the columns and values arrays.  This can be handy when inserting optional columns. For example, in order to generate the query\n\n```sql\nINSERT INTO products (name, qty) VALUES (?, ?)\n```\n\nuse the following code:\n\n```go\nqb.InsertInto(\"products\").\n   Columns(\"name\", \"qty\").\n   Values(\"Hammer\", 5).\n   String()\n```\n\nIf using PostgreSQL, the `OnConflict` function can be used to generate an `ON CONFLICT target action` clause.  The provided target should be of type `TargetColumn`, `TargetConstraint`, or `whereClause`.  The provided action should be of type `ActionDoNothing` or `*updateQuery`.  For example, to generate the query\n\n```sql\nINSERT INTO products (name, item_number) VALUES (?, ?) ON CONFLICT (item_number) DO UPDATE SET item_number=123\n```\n\nuse the following code:\n\n```go\nqb.InsertInto(\"products\").\n   Col(\"name\", \"Hammer\").\n   Col(\"item_number\", 456).\n   OnConflict(\n     qb.TargetColumn(\"item_number\"),\n     qb.Update(\"\").Set(\"item_number\", 123),\n   ).\n   String()\n```\n\n### Update\n\nAn update query can be initialized with the `Update(table string)` function.  The struct returned from this function call can then call the following functions:\n\n- `Set(col string, val interface{})`\n- `Where(col, cmp string, val interface{})`\n- `OrWhere(col, cmp string, val interface{})`\n\nCalling `Set` with the same col value will update the previous value.  For example, in order to generate the query\n\n```sql\nUPDATE products SET name=?, qty=? WHERE item_id=?\n```\n\nuse the following code:\n\n```go\nqb.Update(\"products\").\n   Set(\"name\", \"Screwdriver\").\n   Set(\"qty\", 10).\n   Where(\"item_id\", \"=\", \"a123\").\n   String()\n```\n\n### Delete\n\nA delete query can be initialized with the `DeleteFrom(table string)` function.  The struct returned from this function call can then call the following functions:\n\n- `Where(col, cmp string, val interface{})`\n- `OrWhere(col, cmp string, val interface{})`\n\nFor example, in order to generate the query\n\n```sql\nDELETE FROM products WHERE item_number=? AND qty\u003c? OR backordered=?\n```\n\nuse the following code:\n\n```go\nqb.DeleteFrom(\"products\").\n   Where(\"item_number\", \"=\", \"a123\").\n   Where(\"qty\", \"\u003c\", 5).\n   OrWhere(\"backordered\", \"=\", true).\n   String()\n```\n\n## Error Handling\n\nCalling the `String()` function returns an `error` as its third return value.  This error will describe any missing values.  The following error constants are defined in the package and can be used with `errors.Is()` if using Go 1.13+.\n\n```go\nErrMissingTable    = Error(\"no table specified\")\nErrMissingSetPairs = Error(\"no set pairs provided\")\nErrColValMismatch  = Error(\"the number of columns and values do not match\")\nErrInvalidConflictTarget = Error(\"invalid conflict target\")\nErrInvalidConflictAction = Error(\"invalid conflict action\")\n```\n\n## Acknowledgments\n\nThis library was heavily inspired by the following libraries:\n* [squirrel](https://github.com/masterminds/squirrel)\n* [dbr](https://github.com/gocraft/dbr)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattmeyers%2Fqb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattmeyers%2Fqb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattmeyers%2Fqb/lists"}