{"id":37323107,"url":"https://github.com/hojabri/querybuilder","last_synced_at":"2026-01-16T03:25:31.854Z","repository":{"id":57663180,"uuid":"478610201","full_name":"hojabri/querybuilder","owner":"hojabri","description":"SQL query builder","archived":false,"fork":false,"pushed_at":"2025-07-13T17:12:27.000Z","size":119,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-13T18:39:34.174Z","etag":null,"topics":["golang","query","query-builder","sql"],"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/hojabri.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":"2022-04-06T15:04:54.000Z","updated_at":"2025-07-13T17:12:23.000Z","dependencies_parsed_at":"2022-09-13T07:00:50.707Z","dependency_job_id":null,"html_url":"https://github.com/hojabri/querybuilder","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/hojabri/querybuilder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hojabri%2Fquerybuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hojabri%2Fquerybuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hojabri%2Fquerybuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hojabri%2Fquerybuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hojabri","download_url":"https://codeload.github.com/hojabri/querybuilder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hojabri%2Fquerybuilder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28477203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T03:13:13.607Z","status":"ssl_error","status_checked_at":"2026-01-16T03:11:47.863Z","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","query","query-builder","sql"],"created_at":"2026-01-16T03:25:31.339Z","updated_at":"2026-01-16T03:25:31.843Z","avatar_url":"https://github.com/hojabri.png","language":"Go","readme":"![GitHub CI](https://github.com/hojabri/querybuilder/actions/workflows/go.yml/badge.svg)\n# querybuilder\nquerybuilder simply builds SQL queries for you.\n\nIf you need to create complex and dynamic queries (runtime queries based on some conditions), you can use this library to create them at runtime.\n\n## install\n\n    go get github.com/hojabri/querybuilder\n\n## usage\nThere are four types of SQL queries which can be built: **SELECT**, **INSERT**, **UPDATE** and **DELETE**\n\nImport library\n\n    import \"github.com/hojabri/querybuilder\"\n\nbelow are examples to create these queries:\n\n### SELECT\nTo build SELECT queries, you need to first call `querybuilder.Select(name string)` which `name` is table name and then use a combination of below functions:\n\n- `Columns(query string, args ...interface{})` gets the name of columns in the `query` parameter and optional arguments in the `args` parameter\n\n- `Joins(tableName string, on string, joinType JoinType, args ...interface{})` to specify join tables. It gets the name of join table in the `tableName` parameter, join condition in the `on` parameter, join type in the `joinType` parameter and optional args in the `args` parameter.\n\njoin types can be one of:\n`JoinInner`, `JoinLeft` or `JoinRight`\n\n- `Where(query string, args ...interface{})` specifies the condition for the SELECT query. you can define the condition in the `query` parameter and it's arguments in the optional `args` parameter.\n\n_Note:_ you can have many `Where` functions in any order\n\n- `Having(query string, args ...interface{})` to use a Having conditions for SELECT queries with Groups. the parameter usage is the same as `Where` function.\n\n_Note:_ you can have many `Having` functions in any order\n\n- `Group(query string)` to specify GROUP BY queries. (Samples in the examples section)\n- `Order(column string, direction OrderDirection)` to specify ORDER BY part of the SELECT queries. It gets column name in the `column` parameter and order direction in the `direction` parameter.\ndirection can be one of `OrderAsc` or `OrderDesc`\n- `Limit(limit int64)` specifies LIMIT part of the SELECT query to have pagination. It accepts an `int64` value.\n- `Offset(offset int64)` specifies OFFSET part of the SELECT query to have pagination. It accepts an `int64` value.\n- `Build()` after specifying all SELECT functions, you need to call this method to create your final query string and also final arguments.\n\n\n### Sample 1\n```go\n\tquery, args, err := querybuilder.Select(\"table1\").Build()\n\n```\n\nOutput:\n\n    query:  SELECT * FROM table1\n    args:   []\n\n### Sample 2\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2,c3\").\n\t\tBuild()\n```\n\nOutput:\n\n    query:  SELECT c1,c2,c3 FROM table1\n    args:   []\n### Sample 3\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2,c3\").\n\t\tWhere(\"c1=true\").\n\t\tWhere(\"c2=?\", 10).\n\t\tBuild()\n```\n\nOutput:\n\n    query:  SELECT c1,c2,c3 FROM table1 WHERE (c1=true) AND (c2=?)\n    args:   [10]\n\n### Sample 4\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2,c3\").\n\t\tWhere(\"c1=true\").\n\t\tWhere(\"c2=? OR c3\u003e?\", 10, 20).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2,c3 FROM table1 WHERE (c1=true) AND (c2=? OR c3\u003e?)\n    args:   [10 20]\n### Sample 5\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2,c3\").\n\t\tWhere(\"c1=?\", true).\n\t\tWhere(querybuilder.In(\"c2\", 10, 20)).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2,c3 FROM table1 WHERE (c1=?) AND (c2 IN (?,?))\n    args:   [true 10 20]\n### Sample 6\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2,SUM(c3) AS total\").\n\t\tWhere(\"c1=?\", 1).\n\t\tGroup(\"c1,c2\").\n\t\tHaving(\"SUM(c3)\u003e?\", 100).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2,SUM(c3) AS total FROM table1 WHERE (c1=?) GROUP BY c1,c2 HAVING (SUM(c3)\u003e?)\n    args:   [1 100]\n### Sample 7\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2,SUM(c3) AS total,AVG(c4) AS average\").\n\t\tWhere(\"c1=?\", 1).\n\t\tWhere(\"c2=?\", true).\n\t\tGroup(\"c1,c2\").\n\t\tHaving(\"SUM(c3)\u003e?\", 100).\n\t\tHaving(\"AVG(c4)\u003c?\", 0.1).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2,SUM(c3) AS total,AVG(c4) AS average FROM table1 WHERE (c1=?) AND (c2=?) GROUP BY c1,c2 HAVING (SUM(c3)\u003e?) AND (AVG(c4)\u003c?)\n    args:   [1 true 100 0.1]\n### Sample 8\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"id,c1,c2,c3\").\n\t\tJoins(\"table2\", \"table1.id = table2.t_id\", querybuilder.JoinLeft).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT id,c1,c2,c3 FROM table1 LEFT JOIN table2 ON table1.id = table2.t_id\n    args:   []\n### Sample 9\n```go\n\tquery, args, err = querybuilder.Select().\n\t\tTable(\"table1 t1\").\n\t\tColumns(\"t1.id,t2.c3\").\n\t\tJoins(\"table2 t2\", \"t1.id = t2.t_id\", querybuilder.JoinInner).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT t1.id,t2.c3 FROM table1 t1 JOIN table2 t2 ON t1.id = t2.t_id\n    args:   []\n### Sample 10\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2\").\n\t\tOrder(\"c1\", querybuilder.OrderDesc).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2 FROM table1 ORDER BY c1 DESC\n    args:   []\n### Sample 11\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2\").\n\t\tOrder(\"c1\", querybuilder.OrderDesc).\n\t\tOrder(\"c2\", querybuilder.OrderAsc).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2 FROM table1 ORDER BY c1 DESC,c2 ASC\n    args:   []\n### Sample 12\n```go\n\tquery, args, err = querybuilder.Select(\"table1\").\n\t\tColumns(\"c1,c2\").\n\t\tLimit(20).\n\t\tOffset(0).\n\t\tBuild()\n```\nOutput:\n\n    query:  SELECT c1,c2 FROM table1 LIMIT 20 OFFSET 0\n    args:   []\n\n### INSERT\nTo build INSERT queries, you need to first call `querybuilder.Insert(name string)` which `name` is table name and then use a combination of below functions:\n- `MapValues(columnValues map[string]interface{})` you can specify columns and values to be inserted to table as a `map` object. (column name in string as the `key` of the map and the value in the `value` of the map)\n- `StructValues(structure interface{})` another and in some case better choice is to use any existing struct as an input for this function. It automatically extracts all columns and values from the `struct` type.\nthe column name will the same as Struct field name, except you specify them in the struct `db` tags. For example:\n```go\n\ttype sampleStructType struct {\n\t\tName  string      `json:\"name,omitempty\" db:\"name\"`\n\t\tEmail string      `json:\"email,omitempty\" db:\"email\"`\n\t\tID    interface{} `json:\"id,omitempty\"`\n\t\tOrder float32     `json:\"order\" db:\"-\"`\n\t\tImage *[]byte     `json:\"image\" db:\"image\"`\n\t\tGrade int         `json:\"grade\" db:\"grade\"`\n}\n```\n_Note:_ if you want to skip a column to be used for insert query, you can use `\"-\"` for the `db` tag.\n\n- `Build()` after specifying all INSERT functions, you need to call this method to create your final query string and also final arguments.\n\n\nSample struct type for insert examples\n```go\n\ttype sampleStructType struct {\n\t\tName  string      `json:\"name,omitempty\" db:\"name\"`\n\t\tEmail string      `json:\"email,omitempty\" db:\"email\"`\n\t\tID    interface{} `json:\"id,omitempty\"`\n\t\tOrder float32     `json:\"order\" db:\"-\"`\n\t\tImage *[]byte     `json:\"image\" db:\"image\"`\n\t\tGrade int         `json:\"grade\" db:\"grade\"`\n\t}\n\tsampleImage := []byte(\"img\")\n```\n### Sample 1\n```go\n\tquery, args, err := querybuilder.Insert(\"table1\").\n\t\tMapValues(map[string]interface{}{\"field1\": \"value1\", \"field2\": 10}).\n\t\tBuild()\n```\nOutput:\n\n    query:  INSERT INTO table1(field1,field2) VALUES(?,?)\n    args:   [value1 10]\n### Sample 2\n```go\n\tquery, args, err = querybuilder.Insert(\"table1\").\n\t\tStructValues(sampleStructType{\n\t\t\tName:  \"Omid\",\n\t\t\tEmail: \"o.hojabri@gmail.com\",\n\t\t\tID:    nil,\n\t\t\tOrder: 1,\n\t\t\tImage: \u0026sampleImage,\n\t\t\tGrade: 10,\n\t\t}).\n\t\tBuild()\n```\nOutput:\n\n    query:  INSERT INTO table1(name,email,image,grade) VALUES(?,?,?,?)\n    args:   [Omid o.hojabri@gmail.com [105 109 103] 10]\n### Sample 3\n```go\n\tquery, args, err = querybuilder.Insert(\"table1\").\n\t\tStructValues(sampleStructType{\n\t\t\tName:  \"Omid\",\n\t\t\tEmail: \"o.hojabri@gmail.com\",\n\t\t\tID:    nil,\n\t\t\tOrder: 1,\n\t\t\tGrade: 10,\n\t\t}).\n\t\tBuild()\n```\nOutput:\n\n    query:  INSERT INTO table1(name,email,grade) VALUES(?,?,?)\n    args:   [Omid o.hojabri@gmail.com 10]\n### UPDATE\nTo build UPDATE queries, you need to first call `querybuilder.UPDATE(name string)` which `name` is table name and then use a combination of below functions:\n- `MapValues(columnValues map[string]interface{})` you can specify columns and values to be updated in the table as a `map` object. (column name in string as the `key` of the map and the value in the `value` of the map)\n- `StructValues(structure interface{})` another and in some case better choice is to use any existing struct as an input for this function. It automatically extracts all columns and values from the `struct` type.\n  the column name will the same as Struct field name, except you specify them in the struct `db` tags. For example:\n```go\n\ttype sampleStructType struct {\n\t\tName  string      `json:\"name,omitempty\" db:\"name\"`\n\t\tEmail string      `json:\"email,omitempty\" db:\"email\"`\n\t\tID    interface{} `json:\"id,omitempty\"`\n\t\tOrder float32     `json:\"order\" db:\"-\"`\n\t\tImage *[]byte     `json:\"image\" db:\"image\"`\n\t\tGrade int         `json:\"grade\" db:\"grade\"`\n}\n```\n_Note:_ if you want to skip a column to be used for update query, you can use `\"-\"` for the `db` tag.\n\n- `Where(query string, args ...interface{})` specifies the condition for the UPDATE query. you can define the condition in the `query` parameter and it's arguments in the optional `args` parameter.\n\n_Note:_ you can have many `Where` functions in any order\n\n- `Build()` after specifying all UPDATE functions, you need to call this method to create your final query string and also final arguments.\n\n\n\nSample struct type for update examples\n```go\n\ttype sampleStructType struct {\n\t\tName  string      `json:\"name,omitempty\" db:\"name\"`\n\t\tEmail string      `json:\"email,omitempty\" db:\"email\"`\n\t\tID    interface{} `json:\"id,omitempty\"`\n\t\tOrder float32     `json:\"order\" db:\"-\"`\n\t\tImage *[]byte     `json:\"image\" db:\"image\"`\n\t\tGrade int         `json:\"grade\" db:\"grade\"`\n\t}\n\tsampleImage := []byte(\"img\")\n```\n### Sample 1\n```go\n\tquery, args, err := querybuilder.Update(\"table1\").\n\t\tMapValues(map[string]interface{}{\"field1\": \"value1\", \"field2\": 10}).\n\t\tBuild()\n```\nOutput:\n\n    query:  UPDATE table1 SET field1=?,field2=?\n    args:   [value1 10]\n### Sample 2\n```go\n\tquery, args, err = querybuilder.Update(\"table1\").\n\t\tStructValues(sampleStructType{\n\t\t\tName:  \"Omid\",\n\t\t\tEmail: \"o.hojabri@gmail.com\",\n\t\t\tID:    nil,\n\t\t\tOrder: 1,\n\t\t\tImage: \u0026sampleImage,\n\t\t\tGrade: 10,\n\t\t}).\n\t\tBuild()\n```\nOutput:\n\n    query:  UPDATE table1 SET name=?,email=?,image=?,grade=?\n    args:   [Omid o.hojabri@gmail.com [105 109 103] 10]\n### Sample 3\n```go\n\tquery, args, err = querybuilder.Update(\"table1\").\n\t\tStructValues(sampleStructType{\n\t\t\tName:  \"Omid\",\n\t\t\tEmail: \"o.hojabri@gmail.com\",\n\t\t\tID:    nil,\n\t\t\tOrder: 1,\n\t\t\tGrade: 10,\n\t\t}).\n\t\tBuild()\n```\nOutput:\n\n    query:  UPDATE table1 SET name=?,email=?,grade=?\n    args:   [Omid o.hojabri@gmail.com 10]\n### DELETE\nTo build DELETE queries, you need to first call `querybuilder.DELETE(name string)` which `name` is table name and then use a combination of below functions:\n- `Where(query string, args ...interface{})` specifies the condition for the DELETE query. you can define the condition in the `query` parameter and it's arguments in the optional `args` parameter.\n- `Build()` after specifying all DELETE functions, you need to call this method to create your final query string and also final arguments.\n\n_Note:_ you can have many `Where` functions in any order\n### Sample 1\n```go\n\tquery, args, err := querybuilder.Delete(\"table1\").\n\t\tWhere(\"id=?\", 10).\n\t\tBuild()\n```\nOutput:\n\n    query:  DELETE FROM table1 WHERE (id=?)\n    args:   [10]\n### Sample 2\n```go\n\tquery, args, err = querybuilder.Delete(\"table1\").\n\t\tWhere(\"id=?\", 10).\n\t\tWhere(\"email=? OR name=?\", \"o.hojabri@gmail.com\", \"Omid\").\n\t\tBuild()\n```\nOutput:\n\n    query:  DELETE FROM table1 WHERE (id=?) AND (email=? OR name=?)\n    args:   [10 o.hojabri@gmail.com Omid]\n\n### Specifying database driver\nIf you want to use the `querybuilder.Rebind(query string)` function to rebinding the argument place-holders in your query, you need first specify the database driver.\n\n```go\nquerybuilder.Driver = _driver_name_\n```\n\ndriver name can be one of:\n```go\nDriverPostgres         = \"postgres\"\nDriverPGX              = \"pgx\"\nDriverPqTimeout        = \"pq-timeouts\"\nDriverCloudSqlPostgres = \"cloudsqlpostgres\"\nDriverMySQL            = \"mysql\"\nDriverSqlite3          = \"sqlite3\"\nDriverOCI8             = \"oci8\"\nDriverORA              = \"ora\"\nDriverGORACLE          = \"goracle\"\nDriverSqlServer        = \"sqlserver\"`\n```\n\nFor example:\n```go\n    querybuilder.Driver = querybuilder.DriverPostgres\n\tquery, args, err = querybuilder.Insert(\"table1\").\n\t\tStructValues(sampleStructType{\n\t\t\tName:  \"Omid\",\n\t\t\tEmail: \"o.hojabri@gmail.com\",\n\t\t\tID:    nil,\n\t\t\tOrder: 1,\n\t\t\tImage: \u0026sampleImage,\n\t\t\tGrade: 10,\n\t\t}).\n\t\tBuild()\n\tif err != nil {\n\t\tlog.Printf(\"err: %s\", err)\n\t}\n\t// query: INSERT INTO table1(name,email,grade) VALUES(?,?,?)\n\tquery = querybuilder.Rebind(query)\n\t// query: INSERT INTO table1(name,email,grade) VALUES($1,$2,$3)\n```\n\n`querybuilder.Rebind(query string)` after your final query string is ready, you can call this method to rebind your query string based on the database driver.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhojabri%2Fquerybuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhojabri%2Fquerybuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhojabri%2Fquerybuilder/lists"}