{"id":23213723,"url":"https://github.com/x-ream/sqlxb","last_synced_at":"2025-08-19T05:33:11.151Z","repository":{"id":37690018,"uuid":"496892526","full_name":"x-ream/sqlxb","owner":"x-ream","description":"sql query builder of golang, build sql for go framework: sqlx, gorp... while ignore building nil or empty string","archived":false,"fork":false,"pushed_at":"2024-07-15T10:24:04.000Z","size":140,"stargazers_count":20,"open_issues_count":32,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-15T12:25:11.021Z","etag":null,"topics":["builder","go","golang","orm","query-builder","sql","sqlx"],"latest_commit_sha":null,"homepage":"http://sqlxb.xream.io","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/x-ream.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2022-05-27T06:50:25.000Z","updated_at":"2024-07-15T10:23:26.000Z","dependencies_parsed_at":"2023-11-25T03:19:45.176Z","dependency_job_id":"a47c3c6c-b4f3-4256-b15b-013fa9a74224","html_url":"https://github.com/x-ream/sqlxb","commit_stats":null,"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqlxb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqlxb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqlxb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqlxb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/x-ream","download_url":"https://codeload.github.com/x-ream/sqlxb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230326773,"owners_count":18209050,"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":["builder","go","golang","orm","query-builder","sql","sqlx"],"created_at":"2024-12-18T19:18:45.488Z","updated_at":"2024-12-18T19:18:46.121Z","avatar_url":"https://github.com/x-ream.png","language":"Go","readme":"# sqlxb  \n[![OSCS Status](https://www.oscs1024.com/platform/badge/x-ream/sqlxb.svg?size=small)](https://www.oscs1024.com/project/x-ream/sqlxb?ref=badge_small)\n![workflow build](https://github.com/x-ream/sqlxb/actions/workflows/go.yml/badge.svg)\n[![GitHub tag](https://img.shields.io/github/tag/x-ream/sqlxb.svg?style=flat)](https://github.com/x-ream/sqlxb/tags)\n[![Go Report Card](https://goreportcard.com/badge/github.com/x-ream/sqlxb)](https://goreportcard.com/report/github.com/x-ream/sqlxb)\n\na tool of sql query builder, build sql for sql.DB, [sqlx](https://github.com/jmoiron/sqlx), [gorp](https://github.com/go-gorp/gorp),\nor build condition sql for some orm framework, like [xorm](https://github.com/go-xorm/xorm), [gorm](https://github.com/go-gorm/gorm)....\n\n## Program feature:\n* ignore building nil or empty string\n\n## Available field of struct:\n    \n* base: string, *bool, *int64, *float64, time.Time....\n* json: struct, map, array, slice\n* bytes: []byte\n\n## Example\n\n    SELECT * FROM t_cat WHERE id \u003e ? AND (price \u003e= ? OR is_sold = ?)\n\n    var Db *sqlx.DB\n    ....\n\n    var c Cat\n\tbuilder := sqlxb.Of(\u0026c).Gt(\"id\", 10000).And(func(cb *CondBuilder) {\n\t\tcb.Gte(\"price\", catRo.Price).OR().Eq(\"is_sold\", catRo.IsSold))\n    })\n\n    countSql, dataSql, vs, _ := builder.Build().SqlOfPage()\n    var catList []Cat\n\terr = Db.Select(\u0026catList, dataSql, vs...)\n\n\n## Contributing\n\nContributors are welcomed to join the sqlxb project. \u003cbr\u003e\nPlease check [CONTRIBUTING](./CONTRIBUTING.md)\n\n## Quickstart\n\n* [Single Example](#single-example)\n* [Join Example](#join-example)\n\n\n### Single Example\n\n```Go\n\nimport (\n    . \"github.com/x-ream/sqlxb\"\n)\n\ntype Cat struct {\n\tId       uint64    `db:\"id\"`\n\tName     string    `db:\"name\"`\n\tAge      uint      `db:\"age\"`\n\tColor    string    `db:\"color\"`\n\tWeight   float64   `db:\"weight\"`\n\tIsSold   *bool     `db:\"is_sold\"`\n\tPrice    *float64  `db:\"price\"`\n\tCreateAt time.Time `db:\"create_at\"`\n}\n\nfunc (*Cat) TableName() string {\n\treturn \"t_cat\"\n}\n\n// IsSold, Price, fields can be zero, must be pointer, like Java Boolean....\n// sqlxb has func: Bool(true), Int(v) ....\n// sqlxb no relect, not support omitempty, should rewrite ro, dto\ntype CatRo struct {\n\tName   string   `json:\"name, string\"`\n\tIsSold *bool    `json:\"isSold, *bool\"`\n\tPrice  *float64 `json:\"price, *float64\"`\n\tAge    uint     `json:\"age\", unit`\n}\n\nfunc main() {\n\tcat := Cat{\n\t\tId:       100002,\n\t\tName:     \"Tuanzi\",\n\t\tAge:      1,\n\t\tColor:    \"B\",\n\t\tWeight:   8.5,\n\t\tIsSold:   Bool(true),\n\t\tPrice:    Float64(10000.00),\n\t\tCreateAt: time.Now(),\n\t}\n    // INSERT .....\n\n    // PREPARE TO QUERY\n\tcatRo := CatRo{\n\t\tName:\t\"Tu\",\n\t\tIsSold: nil,\n\t\tPrice:  Float64(5000.00),\n\t\tAge:    1,\n\t}\n\n\tpreCondition := func() bool {\n\t\tif cat.Color == \"W\" {\n\t\t\treturn true\n\t\t} else if cat.Weight \u003c= 3 {\n\t\t\treturn false\n\t\t} else {\n\t\t\treturn true\n\t\t}\n\t}\n\n\tvar c Cat\n\tvar builder = Of(\u0026c)\n\tbuilder.LikeLeft(\"name\",catRo.Name)\n\tbuilder.X(\"weight \u003c\u003e ?\", 0) //X(k, v...), hardcode func, value 0 and nil will NOT ignore\n    //Eq,Ne,Gt.... value 0 and nil will ignore, like as follow: OR().Eq(\"is_sold\", catRo.IsSold)\n\tbuilder.And(func(cb *CondBuilder) {\n            cb.Gte(\"price\", catRo.Price).OR().Gte(\"age\", catRo.Age).OR().Eq(\"is_sold\", catRo.IsSold))\n\t    })\n    //func Bool NOT designed for value nil or 0; designed to convert complex logic to bool\n    //Decorator pattern suggest to use func Bool preCondition, like:\n    //myBoolDecorator := NewMyBoolDecorator(para)\n    //builder.Bool(myBoolDecorator.fooCondition, func(cb *CondBuilder) {\n\tbuilder.Bool(preCondition, func(cb *CondBuilder) {\n            cb.Or(func(cb *CondBuilder) {\n                cb.Lt(\"price\", 5000)\n            })\n\t})\n\tbuilder.Sort(\"id\", ASC)\n        builder.Paged(func(pb *PageBuilder) {\n                pb.Page(1).Rows(10).IgnoreTotalRows()\n            })\n    countSql, dataSql, vs, _ := builder.Build().SqlOfPage()\n    // ....\n\n    //dataSql: SELECT * FROM t_cat WHERE id \u003e ? AND name LIKE ? AND weight \u003c\u003e 0 AND (price \u003e= ? OR age \u003e= ?) OR (price \u003c ?)\n    //ORDER BY id ASC LIMIT 10\n\n\t//.IgnoreTotalRows(), will not output countSql\n    //countSql: SELECT COUNT(*) FROM t_cat WHERE name LIKE ? AND weight \u003c\u003e 0 AND (price \u003e= ? OR age \u003e= ?) OR (price \u003c ?)\n    \n    //sqlx: \terr = Db.Select(\u0026catList, dataSql,vs...)\n\tjoinSql, condSql, cvs := builder.Build().SqlOfCond()\n    \n    //conditionSql: id \u003e ? AND name LIKE ? AND weight \u003c\u003e 0 AND (price \u003e= ? OR age \u003e= ?) OR (price \u003c ?)\n\n}\n```\n\n\n### Join Example\n\n```Go\nimport (\n        . \"github.com/x-ream/sqlxb\"\n    )\n    \nfunc main() {\n\t\n\tsub := func(sb *BuilderX) {\n                sb.Select(\"id\",\"type\").From(\"t_pet\").Gt(\"id\", 10000) //....\n            }\n\t\n        builder := X().\n\t\tSelect(\"p.id\",\"p.weight\").\n\t\tFromX(func(fb *FromBuilder) {\n                    fb.\n                        Sub(sub).As(\"p\").\n                        JOIN(INNER).Of(\"t_dog\").As(\"d\").On(\"d.pet_id = p.id\").\n                        JOIN(LEFT).Of(\"t_cat\").As(\"c\").On(\"c.pet_id = p.id\").\n                            Cond(func(on *ON) {\n                                on.Gt(\"c.id\", ro.MinCatId)\n                            })\n\t\t    }).\n\t        Ne(\"p.type\",\"PIG\").\n                Having(func(cb *CondBuilderX) {\n                    cb.Sub(\"p.weight \u003e ?\", func(sb *BuilderX) {\n                        sb.Select(\"AVG(weight)\").From(\"t_dog\")\n                    })\n                })\n    \n}\n\n\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fx-ream%2Fsqlxb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fx-ream%2Fsqlxb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fx-ream%2Fsqlxb/lists"}