{"id":36446368,"url":"https://github.com/crayxn/sql-generator","last_synced_at":"2026-01-11T22:45:39.456Z","repository":{"id":64301374,"uuid":"573335822","full_name":"crayxn/sql-generator","owner":"crayxn","description":"simple sql generator","archived":false,"fork":false,"pushed_at":"2022-12-05T06:42:55.000Z","size":14,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-02T15:04:05.369Z","etag":null,"topics":["go","go-zero","golang","mysql","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crayxn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-12-02T08:12:51.000Z","updated_at":"2023-02-22T10:00:40.000Z","dependencies_parsed_at":"2023-01-15T09:30:44.141Z","dependency_job_id":null,"html_url":"https://github.com/crayxn/sql-generator","commit_stats":null,"previous_names":["crayoon/sql_generator","crayoon/sql-generator"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/crayxn/sql-generator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crayxn%2Fsql-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crayxn%2Fsql-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crayxn%2Fsql-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crayxn%2Fsql-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crayxn","download_url":"https://codeload.github.com/crayxn/sql-generator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crayxn%2Fsql-generator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28326144,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T22:11:01.104Z","status":"ssl_error","status_checked_at":"2026-01-11T22:10:58.990Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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","go-zero","golang","mysql","sql"],"created_at":"2026-01-11T22:45:38.903Z","updated_at":"2026-01-11T22:45:39.450Z","avatar_url":"https://github.com/crayxn.png","language":"Go","readme":"# SQL Generator\n前言：使用go-zero的时候只能使用写原生语句查询，故写了这个比较简单的sql语句生成器\n\n## 基础使用\n```go\n// 基础查询\n// select * from table_name where id = ? limit 1 offset 0\nsql, vars := NewSql(func(q *Query) {\n    q.Where(\"id\", \"=\", 1)\n\tq.Find()\n}, \"table_name\")\n\n// 更新\n// update table_name set status=? where id = ?\nsql, vars := NewSql(func(q *Query) {\n    q.Where(\"id\", \"=\", 1)\n    q.Update(map[string]interface{}{\n        \"status\": 2,\n    })\n}, \"table_name\")\n\n// 新增\n// insert into table_name (status, title) values (?, ?)\nsql, vars := NewSql(func(q *Query) {\n    q.Insert(map[string]interface{}{\n        \"status\": 2,\n        \"title\":  \"test\",\n    })\n}, \"table_name\")\n\n//删除\n//delete from table_name where id = ?\nsql, vars := NewSql(func(q *Query) {\n    q.Where(\"id\", \"=\", 1)\n    q.Delete()\n}, \"table_name\")\n\n//支持链式\nsql, vars := NewSql(func(q *Query) {\n    q.Where(\"id\", \"=\", 1).Where(\"status\", \"\u003e\", 1).Find()\n}, \"table_name\")\n```\n## 条件查询\n支持基本的 =、\u003c、\u003e、like、in、between、or、is null、is not null\n#### func (w *Wheres) Where(field string, operator string, value interface{}) *Wheres\n#### func (w *Wheres) WhereIn(field string, value []interface{}) *Wheres\n#### func (w *Wheres) WhereBetween(field string, first interface{}, second interface{}) *Wheres\n#### func (w *Wheres) WhereNull(field string) *Wheres\n#### func (w *Wheres) WhereNoNull(field string) *Wheres\n\n```go\nq.Where(\"id\", \"=\", 1)\n// id = ?\nq.Where(\"count\", \"\u003e\", 0)\n// count \u003e ?\nq.Where(\"title\", \"like\", \"%test%\")          //模糊\n// title like ?\nq.WhereIn(\"status\", []interface{}{1, 2, 3}) //In\n// status in (?,?,?)\nq.WhereBetween(\"status\", 1, 3)              // between\n// status between (?,?)\nq.WhereOr(func(or *where.Wheres) {\n    or.Where(\"id\",\"=\",1)\n    or.WhereIn(\"status\", []interface{}{ 1, 3 })\n})\n// (id = ? or status in (?,?))\n```\n## Limit\n#### func (q *Query) Limit(offset int64, limit int64) *Query\n```go \nsql, vars := NewSql(func(q *Query) {\n    q.Limit(0, 10)\t\n    // select * from table_name limit 10 offest 0\n}, \"table_name\")\n```\n## OrderBy\n#### func (q *Query) OrderBy(field string, direction string) *Query\n```go\nsql, vars := NewSql(func(q *Query) {\n    q.OrderBy(\"id\", \"desc\") \n    //select * from table_name order by id desc\n    q.OrderBy(\"status\", \"asc\") \n    //select * from table_name order by id desc, status asc\n}, \"table_name\")\n```\n## GroupBy / Having\n#### func (q *Query) GroupBy(field string) *Query\n#### func (q *Query) Having(do func(having *where.Wheres)) *Query\n```go\nsql, vars := NewSql(func(q *Query) {\n    q.GroupBy(\"status\").Having(func(having *where.Wheres) {\n        having.Where(\"count(id)\", \"\u003e\", 5)\n    })\n// select * from table_name order by status having ( count(id) \u003e ? )\n}, \"table_name\")\n```\n## Join\n#### func (q *Query) Join(item *join.Joins, do func(join *join.Joins)) *Query\n```go\nsql, vars := NewSql(func(q *Query) {\n    q.Join(\u0026join.Joins{\n        Typ:   join.InnerJoin,\n        Table: \"table_name2 as t2\",\n    }, func(join *join.Joins) {\n        join.On(\"t1.id\", \"=\", \"t2.table_name_id\")\n        join.Where(func(w *where.Wheres) {\n            w.Where(\"t2.status\", \"=\", 1)\n        })\n    })\n    q.Limit(0, 10)\n    q.Select(\"q1.id, q2.id\")\n}, \"table_name as t1\")\n\n// select q1.id, q2.id from table_name as t1 inner join table_name2 as t2 on t1.id = t2.table_name_id and t2.status = ? limit 10 offset 0\n\n```\n## 搭配使用（go-zero）\n```go\nproductList := make([]*Product, 0)\nsearchSql, vars := query.NewSql(func(q *query.Query) {\n    q.UseSoftDelete()\n    // q.Select(productRows)\n    q.Limit(0, 10)\n    q.Where(\"status\", \"=\", 1)\n}, m.table)\n// select * from product where status = ? and delete_time is null limit 10 offset 0\nif err := m.QueryRowsNoCache(\u0026productList, searchSql, vars...); err != nil {\n    return 0, nil, err\n}\n```\n\n## 性能\n因为实在简单，没有多少东西 10w 句sql生成量只需大约 100ms\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrayxn%2Fsql-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrayxn%2Fsql-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrayxn%2Fsql-generator/lists"}