{"id":20275155,"url":"https://github.com/samuelbankstech/go-postgresql-query-builder","last_synced_at":"2025-04-11T05:23:34.209Z","repository":{"id":41903045,"uuid":"455216736","full_name":"SamuelBanksTech/Go-Postgresql-Query-Builder","owner":"SamuelBanksTech","description":"A query builder for Postgresql in Go","archived":false,"fork":false,"pushed_at":"2022-12-01T14:48:37.000Z","size":53,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T03:27:21.309Z","etag":null,"topics":["golang","postgres","postgresql","postgresql-database","query-builder"],"latest_commit_sha":null,"homepage":"","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/SamuelBanksTech.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-02-03T15:22:36.000Z","updated_at":"2024-06-25T13:47:48.000Z","dependencies_parsed_at":"2023-01-22T10:20:31.195Z","dependency_job_id":null,"html_url":"https://github.com/SamuelBanksTech/Go-Postgresql-Query-Builder","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelBanksTech%2FGo-Postgresql-Query-Builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelBanksTech%2FGo-Postgresql-Query-Builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelBanksTech%2FGo-Postgresql-Query-Builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelBanksTech%2FGo-Postgresql-Query-Builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SamuelBanksTech","download_url":"https://codeload.github.com/SamuelBanksTech/Go-Postgresql-Query-Builder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248346450,"owners_count":21088460,"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":["golang","postgres","postgresql","postgresql-database","query-builder"],"created_at":"2024-11-14T13:08:14.959Z","updated_at":"2025-04-11T05:23:34.185Z","avatar_url":"https://github.com/SamuelBanksTech.png","language":"Go","readme":"# Postgresql Query Builder for Go\n\n[![GoReportCard](https://goreportcard.com/badge/github.com/SamuelBanksTech/Go-Postgresql-Query-Builder)](https://goreportcard.com/report/github.com/SamuelBanksTech/Go-Postgresql-Query-Builder)\n[![made-with-Go](https://img.shields.io/badge/Made%20with-Go-1f425f.svg)](https://go.dev/)\n[![GoDoc reference example](https://img.shields.io/badge/godoc-reference-blue.svg)](https://pkg.go.dev/github.com/SamuelBanksTech/Go-Postgresql-Query-Builder/pqb)\n\nThis query builder aims to make complex queries for postgres easier to breakdown, put together, and read. The project is very much in its infancy, but is also in a very usable and functional state. \n\nHowever, please feel free to test, fork, or submit PRs. \n\n:-)\n\n## Install\n\n`go get github.com/SamuelBanksTech/Go-Postgresql-Query-Builder`\n\n## Usage\n\nEvery example of usage would be unrealistic to show in this readme, but once you become familiar, it becomes quite intuitive. \n\nThis query builder is best used with [`pgx by jackc`](https://github.com/jackc/pgx) but realistically this can be used with any postgres connection.\n\n#### Basic Example\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/SamuelBanksTech/Go-Postgresql-Query-Builder/pqb\"\n\t\"github.com/jackc/pgx/v4\"\n)\n\nfunc main() {\n\t// urlExample := \"postgres://username:password@localhost:5432/database_name\"\n\tconn, err := pgx.Connect(context.Background(), os.Getenv(\"DATABASE_URL\"))\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"Unable to connect to database: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer conn.Close(context.Background())\n\n\tvar qb pqb.Sqlbuilder\n\n\tpgQuery, queryArgs := qb.\n\t\tFrom(`myschema.widgets`).\n\t\tSelect(`name`, `weight`).\n\t\tWhere(`id`, `=`, `1`).\n\t\tBuild()\n\n\tvar name string\n\tvar weight int64\n\terr = conn.QueryRow(context.Background(), pgQuery, queryArgs...).Scan(\u0026name, \u0026weight)\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"QueryRow failed: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\n\tfmt.Println(name, weight)\n}\n```\npgQuery Output:\n\n`SELECT \"name\", \"weight\" FROM \"myschema\".\"widgets\" WHERE \"id\" = $1`\n\nqueryArgs Output:\n\n[1]\n\n\n\n#### Slightly More Advanced Example\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/SamuelBanksTech/Go-Postgresql-Query-Builder/pqb\"\n\t\"github.com/jackc/pgx/v4\"\n)\n\nfunc main() {\n\t// urlExample := \"postgres://username:password@localhost:5432/database_name\"\n\tconn, err := pgx.Connect(context.Background(), os.Getenv(\"DATABASE_URL\"))\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"Unable to connect to database: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer conn.Close(context.Background())\n\n\tvar qb pqb.Sqlbuilder\n\n\tpgQuery, queryArgs := qb.\n\t\tFrom(`myschema.tasks`).\n\t\tLeftJoin(`myschema.users`, `users`, `myschema.tasks.user_id = users.id`).\n\t\tWhere(`users.active`, `=`, `1`).\n\t\tWhere(`myschema.tasks.completed`, `=`, `0`).\n\t\tSelect(`myschema.tasks.task_details`, `users.name`, `users.email`).\n\t\tBuild()\n\n\n\trows, _ := conn.Query(context.Background(), pgQuery, queryArgs...)\n\n\tfor rows.Next() {\n\t\tvar taskData string\n\t\tvar userName string\n\t\tvar userEmail string\n\t\t\n\t\terr := rows.Scan(\u0026taskData, \u0026userName, \u0026userEmail)\n\t\tif err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tfmt.Printf(\"%s - %s - %s\\n\", taskData, userName, userEmail)\n\t}\n}\n```\n\n\n\n#### Even More Advanced Example Using Programmatic Query Clauses \n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/SamuelBanksTech/Go-Postgresql-Query-Builder/pqb\"\n\t\"github.com/jackc/pgx/v4\"\n)\n\ntype SearchFilters struct {\n\tIncludeAuthorDetails bool\n\tTitleSearch          string\n\tAuthorSearch         string\n}\n\nfunc main() {\n\turlExample := \"postgres://username:password@localhost:5432/database_name\"\n\tconn, err := pgx.Connect(context.Background(), urlExample)\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"Unable to connect to database: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\n\tdefer conn.Close(context.Background())\n\n\tfilters := SearchFilters{\n\t\tIncludeAuthorDetails: true,\n\t\tTitleSearch:          \"revenge gopher\",\n\t\tAuthorSearch:         \"\",\n\t}\n\n\tpgQuery, queryArgs := filterQuery(filters)\n\n\trows, _ := conn.Query(context.Background(), pgQuery, queryArgs...)\n\n\tfor rows.Next() {\n\t\tvar bookId int\n\t\tvar bookTitle string\n\n\t\tif filters.IncludeAuthorDetails {\n\t\t\tvar authorName string\n\t\t\tvar authorEmail string\n\n\t\t\terr := rows.Scan(\u0026bookId, \u0026bookTitle, \u0026authorName, \u0026authorEmail)\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\tfmt.Printf(\"%d - %s - %s - %s\\n\", bookId, bookTitle, authorName, authorEmail)\n\n\t\t} else {\n\t\t\terr := rows.Scan(\u0026bookId, \u0026bookTitle)\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\tfmt.Printf(\"%d - %s\\n\", bookId, bookTitle)\n\n\t\t}\n\t}\n}\n\nfunc filterQuery(filters SearchFilters) (string, []interface{}) {\n\n\tvar query pqb.Sqlbuilder\n\n\tquery.\n\t\tFrom(`myschema.books`).\n\t\tWhere(`myschema.books.deleted`, `=`, `0`).\n\t\tSelect(`myschema.books.id`, `myschema.books.title`)\n\n\tif filters.IncludeAuthorDetails {\n\t\tquery.\n\t\t\tLeftJoin(`myschema.authors`, `authors`, `myschema.books.author_id = authors.id`).\n\t\t\tSelect(`authors.name`, `authors.email`)\n\t}\n\n\tif len(filters.TitleSearch) \u003e 0 {\n\t\ttitleSearchWords := strings.Fields(filters.TitleSearch)\n\n\t\tquery.WhereStringMatchAny(`myschema.books.title`, titleSearchWords)\n\t}\n\n\tif len(filters.AuthorSearch) \u003e 0 {\n\t\tauthorSeachWords := strings.Fields(filters.AuthorSearch)\n\n\t\tif !filters.IncludeAuthorDetails {\n\t\t\tquery.LeftJoin(`myschema.authors`, `authors`, `myschema.books.author_id = authors.id`)\n\t\t}\n\n\t\tquery.WhereStringMatchAny(`authors.name`, authorSeachWords)\n\t}\n\n\treturn query.Build()\n}\n\n\n```\n\n\n\n#### Insert Example\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/SamuelBanksTech/Go-Postgresql-Query-Builder/pqb\"\n\t\"github.com/jackc/pgx/v4\"\n)\n\ntype BookData struct {\n\tTitle string\n\tAuthor string `pqb:\"writer\"` // notice the pqb field tag override\n}\n\nfunc main() {\n\t// urlExample := \"postgres://username:password@localhost:5432/database_name\"\n\tconn, err := pgx.Connect(context.Background(), os.Getenv(\"DATABASE_URL\"))\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"Unable to connect to database: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer conn.Close(context.Background())\n\n\tbd := BookData{\n\t\tTitle:  \"Revenge of the Gophers\",\n\t\tAuthor: \"Mr Cool Dev\",\n\t}\n\t\n\tvar qb pqb.Sqlbuilder\n\tpgQuery, err := qb.BuildInsert(`myschema.books`, bd, ``)\n\tif err != nil {\n\t\tlog.Fatal(err)\n    }\n\n\t_, err = conn.Exec(context.Background(), pgQuery)\n\tif err != nil {\n\t\tlog.Fatal(err)\n    }\n}\n```\nQuery Output:\n\n`INSERT INTO \"myschema\".\"books\" (\"title\", \"writer\") VALUES ('Revenge of the Gophers', 'Mr Cool Dev') `\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelbankstech%2Fgo-postgresql-query-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamuelbankstech%2Fgo-postgresql-query-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelbankstech%2Fgo-postgresql-query-builder/lists"}