{"id":37214120,"url":"https://github.com/llklkl/sqlbuilder","last_synced_at":"2026-01-15T00:45:48.555Z","repository":{"id":193738982,"uuid":"689240623","full_name":"llklkl/sqlbuilder","owner":"llklkl","description":"a simple SQL statement construction tool using golang","archived":false,"fork":false,"pushed_at":"2023-09-12T17:16:49.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-09-13T02:17:57.914Z","etag":null,"topics":["golang","sqlbuilder"],"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/llklkl.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}},"created_at":"2023-09-09T07:32:38.000Z","updated_at":"2023-09-12T02:39:53.000Z","dependencies_parsed_at":"2023-09-09T19:00:42.319Z","dependency_job_id":null,"html_url":"https://github.com/llklkl/sqlbuilder","commit_stats":null,"previous_names":["llklkl/sqlbuilder"],"tags_count":0,"template":null,"template_full_name":null,"purl":"pkg:github/llklkl/sqlbuilder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llklkl%2Fsqlbuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llklkl%2Fsqlbuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llklkl%2Fsqlbuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llklkl%2Fsqlbuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/llklkl","download_url":"https://codeload.github.com/llklkl/sqlbuilder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llklkl%2Fsqlbuilder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28440267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:34:46.850Z","status":"ssl_error","status_checked_at":"2026-01-15T00:34:46.551Z","response_time":107,"last_error":"SSL_read: 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":["golang","sqlbuilder"],"created_at":"2026-01-15T00:45:47.101Z","updated_at":"2026-01-15T00:45:48.485Z","avatar_url":"https://github.com/llklkl.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlbuilder\n\n[![Coverage Status](https://coveralls.io/repos/github/llklkl/sqlbuilder/badge.svg?branch=main)](https://coveralls.io/github/llklkl/sqlbuilder?branch=main)\n[![Go Report Card](https://goreportcard.com/badge/github.com/llklkl/sqlbuilder)](https://goreportcard.com/report/github.com/llklkl/sqlbuilder)\n\n[简体中文](README_zh.md)\n\nA DML SQL simple statement construction tool, which supports chained calls.\nSupports generating `SELECT`, `UPDATE`, `DELETE` and `INSERT` simple statements.\n\n**hint**:\n\n+ **Do not cache any intermediate results in the chain call process, as this will cause errors in the final generated\n  SQL statement**\n+ `SqlBuilder` is not thread-safe and cannot be called concurrently\n\n## Contents\n\n\u003c!-- TOC --\u003e\n* [sqlbuilder](#sqlbuilder)\n  * [Contents](#contents)\n  * [Install](#install)\n  * [Usage](#usage)\n    * [insert statement](#insert-statement)\n      * [insert a single piece of data](#insert-a-single-piece-of-data)\n      * [insert multiple pieces of data](#insert-multiple-pieces-of-data)\n    * [select statement](#select-statement)\n    * [select statement](#select-statement-1)\n    * [delete statement](#delete-statement)\n    * [construct where condition](#construct-where-condition)\n  * [Some special functions](#some-special-functions)\n    * [func T(args ...string) *Table](#func-targs-string-table)\n    * [func F(args ...string) *Field](#func-fargs-string-field)\n    * [func E(args ...string) *Expr](#func-eargs-string-expr)\n    * [func O(field any, direction OrderDirection) *OrderSpec](#func-ofield-any-direction-orderdirection-orderspec)\n  * [License](#license)\n\u003c!-- TOC --\u003e\n\n## Install\n\n```shell\ngo get github.com/llklkl/sqlbuilder@latest\n```\n\n## Usage\n\n### insert statement\n\n#### insert a single piece of data\n\nthe original sql:\n\n```sql\nINSERT INTO `demo` (`name`, `age`)\nVALUES (?, ?)\n```\n\nconstruct using `sqlbuilder`:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tsb \"github.com/llklkl/sqlbuilder\"\n)\n\nfunc main() {\n\tsql, args := sb.New().Insert().Into(\"demo\").\n\t\tFields(\"name\", \"age\").\n\t\tValues(\"alice\", 20).Build()\n\tfmt.Println(sql)\n\tfmt.Println(args)\n}\n```\n\n#### insert multiple pieces of data\n\nthe original sql:\n\n```sql\nINSERT INTO `demo` (`name`, `age`)\nVALUES (?, ?),\n       (?, ?),\n       (?, ?) ON DUPLICATE KEY\nUPDATE `name`=?,`age`=`age`+1\n```\n\nconstruct using `sqlbuilder`:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tsb \"github.com/llklkl/sqlbuilder\"\n)\n\ntype Student struct {\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\tstudents := []*Student{\n\t\t{Name: \"alice\", Age: 19},\n\t\t{Name: \"bob\", Age: 20},\n\t\t{Name: \"carol\", Age: 21},\n\t}\n\tsql, args := sb.New().Insert().Into(\"demo\").\n\t\tFields(\"name\", \"age\").\n\t\tBulk(len(students), func(index int) []any {\n\t\t\treturn []any{students[index].Name, students[index].Age}\n\t\t}).\n\t\tOnDuplicate(\n\t\t\tsb.Set(sb.F(\"name\"), \"duplicate\"),\n\t\t\tsb.Value(\"`age`=`age`+1\"),\n\t\t).Build()\n\tfmt.Println(sql)\n\tfmt.Println(args)\n}\n\n```\n\n### select statement\n\nthe original sql:\n\n```sql\nSELECT `c`.`class_name`, `s`.`name`, `s`.`score`\nFROM `t_student` AS `s`\n         RIGHT JOIN `t_class` AS `c` USING (`class_id`)\nWHERE `c`.`class_name` = ?\n  AND `s`.`score` \u003e= ?\nORDER BY `s`.`name` ASC LIMIT ?,?\n```\n\nconstruct using `sqlbuilder`:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tsb \"github.com/llklkl/sqlbuilder\"\n)\n\nfunc main() {\n\tsql, args := sb.New().Select().\n\t\tField(\n\t\t\tsb.F(\"c\", \"class_name\"),\n\t\t\tsb.F(\"s\", \"name\"),\n\t\t\tsb.F(\"s\", \"score\"),\n\t\t).\n\t\tFromT(sb.T(\"t_student\", \"s\")).\n\t\tRightJoin(sb.T(\"t_class\", \"c\")).Using(\"class_id\").\n\t\tWhere(\n\t\t\tsb.Eq(sb.F(\"c\", \"class_name\"), \"class1\"),\n\t\t\tsb.Ge(sb.F(\"s\", \"score\"), 85),\n\t\t).\n\t\tOrderBy(sb.O(sb.F(\"s\", \"name\"), sb.Asc)).\n\t\tLimitOffset(0, 10).Build()\n\tfmt.Println(sql)\n\tfmt.Println(args)\n}\n\n```\n\n### select statement\n\nthe original sql:\n\n```sql\nUPDATE `demo`\nSET `name`=?,\n    `age`=?\nWHERE `name` = ? LIMIT ?\n```\n\nconstruct using `sqlbuilder`:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tsb \"github.com/llklkl/sqlbuilder\"\n)\n\nfunc main() {\n\tsql, args := sb.New().Update().Table(\"demo\").\n\t\tSet(\n\t\t\tsb.Set(sb.F(\"name\"), \"alice\"),\n\t\t\tsb.Set(sb.F(\"age\"), 22),\n\t\t).Where(sb.Eq(sb.F(\"name\"), \"bob\")).\n\t\tLimit(5).Build()\n\tfmt.Println(sql)\n\tfmt.Println(args)\n}\n\n```\n\n### delete statement\n\nthe original sql:\n\n```sql\nDELETE\nFROM `demo`\nWHERE `age` \u003e= ? ORDER BY `name` DESC LIMIT ?\n```\n\nconstruct using `sqlbuilder`:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tsb \"github.com/llklkl/sqlbuilder\"\n)\n\nfunc main() {\n\tsql, args := sb.New().Delete().From(\"demo\").\n\t\tWhere(sb.Ge(sb.F(\"age\"), 20)).\n\t\tOrder(sb.O(sb.F(\"name\"), sb.Desc)).\n\t\tLimit(10).Build()\n\tfmt.Println(sql)\n\tfmt.Println(args)\n}\n\n```\n\n### construct where condition\n\nThe `Where` method in the `SELECT` statement connects multiple conditions in an `AND` manner by default. Multiple\nconditions can be nested through the `And`, `Or` methods.\n\nCurrently, the following where conditions are supported:\n\n+ And: Multiple where conditions can be nested and connected with `AND`\n+ Or: Multiple where conditions can be nested and connected using `OR`\n+ Lt\n+ Le\n+ Eq\n+ Gt\n+ Ge\n+ Ne\n+ Between And\n+ Like\n+ IsNull\n+ NotNull\n+ In\n+ Not In\n+ Exists: supports subquery statement\n+ Not Exists: supports subquery statement\n+ Condition: supports customizing arbitrary conditions. For example, `Condition(\"file_sha=UNHEX(?)\", fileSha)` defines a\n  condition of `file_sha=UNHEX(?)`\n+ ...\n\n## Some special functions\n\n### func T(args ...string) *Table\n\nThis function defines a `Table`, which is used to define the table in SQL statements.\n\nThis function will interpret the parameters differently depending on the number of parameters:\n\n+ When the number of parameters is 1, it is equivalent to `func (table string) *Table`\n+ When the number of parameters is 2, it is equivalent to `func (table, alias string) *Table`\n+ When the number of parameters is 3, it is equivalent to `func (database, table, alias string) *Table`\n\n### func F(args ...string) *Field\n\nThis function defines a `Field`, usually used for conditional filtering, or `SELECT` query fields.\n\nThis function will interpret the parameters differently depending on the number of parameters:\n\n+ When the number of parameters is 1, it is equivalent to `func (field string) *Field`\n+ When the number of parameters is 2, it is equivalent to `func (table, field string) *Field`\n+ When the number of parameters is 3, it is equivalent to `func (table, field, alias string) *Field`\n\n### func E(args ...string) *Expr\n\nThis function defines an `Expr`, which is used when the `SELECT` statement requires a built-in function expression.\n\nThis function will interpret the parameters differently depending on the number of parameters:\n\n+ When the number of parameters is 1, it is equivalent to `func (expr string) *Expr`\n+ When the number of parameters is 2, it is equivalent to `func (expr, alias string) *Expr`\n\n### func O(field any, direction OrderDirection) *OrderSpec\n\nThis function defines an OrderSpec for Select...Order to specify the sorting field.\n\n## License\n\n[MIT](https://github.com/sunyctf/ChineseREADME/blob/main/LICENSE) © llklkl\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fllklkl%2Fsqlbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fllklkl%2Fsqlbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fllklkl%2Fsqlbuilder/lists"}