{"id":18248047,"url":"https://github.com/kanmu/go-sqlfmt","last_synced_at":"2025-08-13T22:04:13.090Z","repository":{"id":38274467,"uuid":"176641867","full_name":"kanmu/go-sqlfmt","owner":"kanmu","description":"A SQL formatter written in Go","archived":false,"fork":false,"pushed_at":"2022-01-20T02:24:03.000Z","size":3525,"stargazers_count":144,"open_issues_count":7,"forks_count":25,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-20T14:56:10.053Z","etag":null,"topics":["go","sql"],"latest_commit_sha":null,"homepage":null,"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/kanmu.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":"2019-03-20T03:04:27.000Z","updated_at":"2025-02-11T12:39:36.000Z","dependencies_parsed_at":"2022-07-29T23:09:36.098Z","dependency_job_id":null,"html_url":"https://github.com/kanmu/go-sqlfmt","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanmu%2Fgo-sqlfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanmu%2Fgo-sqlfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanmu%2Fgo-sqlfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanmu%2Fgo-sqlfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kanmu","download_url":"https://codeload.github.com/kanmu/go-sqlfmt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247202965,"owners_count":20900880,"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":["go","sql"],"created_at":"2024-11-05T09:35:23.758Z","updated_at":"2025-04-04T15:31:55.411Z","avatar_url":"https://github.com/kanmu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlfmt\n\n[![Build Status](https://travis-ci.org/kanmu/go-sqlfmt.svg?branch=master)](https://travis-ci.org/kanmu/go-sqlfmt)\n[![Go Report Card](https://goreportcard.com/badge/github.com/kanmu/go-sqlfmt)](https://goreportcard.com/report/github.com/kanmu/go-sqlfmt)\n\n## Description\n\nThe sqlfmt formats PostgreSQL statements in `.go` files into a consistent style.\n\n## Example\n\n_Unformatted SQL in a `.go` file_\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n)\n\n\nfunc sendSQL() int {\n\tvar id int\n\tvar db *sql.DB\n\tdb.QueryRow(`\n\tselect xxx ,xxx ,xxx\n\t, case\n\twhen xxx is null then xxx\n\telse true\nend as xxx\nfrom xxx as xxx join xxx on xxx = xxx join xxx as xxx on xxx = xxx\nleft outer join xxx as xxx\non xxx = xxx\nwhere xxx in ( select xxx from ( select xxx from xxx ) as xxx where xxx = xxx )\nand xxx in ($2, $3) order by xxx`).Scan(\u0026id)\n\treturn id\n}\n```\n\nThe above will be formatted into the following:\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n)\n\nfunc sendSQL() int {\n\tvar id int\n\tvar db *sql.DB\n\tdb.QueryRow(`\nSELECT\n  xxx\n  , xxx\n  , xxx\n  , CASE\n       WHEN xxx IS NULL THEN xxx\n       ELSE true\n    END AS xxx\nFROM xxx AS xxx\nJOIN xxx\nON xxx = xxx\nJOIN xxx AS xxx\nON xxx = xxx\nLEFT OUTER JOIN xxx AS xxx\nON xxx = xxx\nWHERE xxx IN (\n  SELECT\n    xxx\n  FROM (\n    SELECT\n      xxx\n    FROM xxx\n  ) AS xxx\n  WHERE xxx = xxx\n)\nAND xxx IN ($2, $3)\nORDER BY\n  xxx`).Scan(\u0026id)\n\treturn id\n}\n```\n\n## Installation\n\n```bash\nrun git clone and go build -o sqlfmt \n```\n## Usage\n\n- Provide flags and input files or directory  \n  ```bash\n  $ sqlfmt -w input_file.go \n  ```\n\n## Flags\n```\n  -l\n\t\tDo not print reformatted sources to standard output.\n\t\tIf a file's formatting is different from src, print its name\n\t\tto standard output.\n  -d\n\t\tDo not print reformatted sources to standard output.\n\t\tIf a file's formatting is different than src, print diffs\n\t\tto standard output.\n  -w\n                Do not print reformatted sources to standard output.\n                If a file's formatting is different from src, overwrite it\n                with gofmt style.\n  -distance     \n                Write the distance from the edge to the begin of SQL statements\n```\n\n## Limitations\n\n- The `sqlfmt` is only able to format SQL statements that are surrounded with **back quotes** and values in **`QueryRow`**, **`Query`**, **`Exec`**  functions from the `\"database/sql\"` package.\n\n  The following SQL statements will be formatted:\n\n  ```go\n  func sendSQL() int {\n  \tvar id int\n  \tvar db *sql.DB\n  \tdb.QueryRow(`select xxx from xxx`).Scan(\u0026id)\n  \treturn id\n  }\n  ```\n\n  The following SQL statements will NOT be formatted:\n\n  ```go\n  // values in fmt.Println() are not formatting targets\n  func sendSQL() int {\n      fmt.Println(`select * from xxx`)\n  }\n\n  // nor are statements surrounded with double quotes\n  func sendSQL() int {\n      var id int\n      var db *sql.DB\n      db.QueryRow(\"select xxx from xxx\").Scan(\u0026id)\n      return id\n  }\n  ```\n\n## Not Supported\n\n- `IS DISTINCT FROM`\n- `WITHIN GROUP`\n- `DISTINCT ON(xxx)`\n- `select(array)`\n- Comments after commna such as \n`select xxxx, --comment\n        xxxx\n`\n- Nested square brackets or braces such as `[[xx], xx]`\n  - Currently being formatted into this: `[[ xx], xx]`\n  - Ideally, it should be formatted into this: `[[xx], xx]`\n\n- Nested functions such as `sum(average(xxx))`\n  - Currently being formatted into this: `SUM( AVERAGE(xxx))`\n  - Ideally, it should be formatted into this: `SUM(AVERAGE(xxx))`\n  \n \n\n## Future Work\n\n- [ ] Refactor\n- [ ] Turn it into a plug-in or an extension for editors\n\n## Contribution\n\nThank you for thinking of contributing to the sqlfmt!\nPull Requests are welcome!\n\n1. Fork ([https://github.com/kanmu/go-sqlfmt))\n2. Create a feature branch\n3. Commit your changes\n4. Rebase your local changes against the master branch\n5. Create new Pull Request\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkanmu%2Fgo-sqlfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkanmu%2Fgo-sqlfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkanmu%2Fgo-sqlfmt/lists"}