From 515ee18092e0b3ae17130032dd019fd9c08e69bc Mon Sep 17 00:00:00 2001
From: Abdelhak Bougouffa <abougouffa@cryptolab.net>
Date: Thu, 23 Nov 2023 23:03:37 +0100
Subject: Add support for SQL using `pgformatter` (#247)

Add support for [`pgFormatter`](https://github.com/darold/pgFormatter)
for `sql-mode`.

<!--

To expedite the pull request process, please see the contributor guide
for my projects:

  <https://github.com/raxod502/contributor-guide>

-->
---
 CHANGELOG.md                                   | 2 ++
 apheleia-formatters.el                         | 4 ++++
 test/formatters/installers/pgformatter.bash    | 1 +
 test/formatters/samplecode/pgformatter/in.sql  | 1 +
 test/formatters/samplecode/pgformatter/out.sql | 9 +++++++++
 5 files changed, 17 insertions(+)
 create mode 100644 test/formatters/installers/pgformatter.bash
 create mode 100644 test/formatters/samplecode/pgformatter/in.sql
 create mode 100644 test/formatters/samplecode/pgformatter/out.sql

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2e763fb..4c36575 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -84,6 +84,7 @@ The format is based on [Keep a Changelog].
 * [`ormolu`](https://github.com/tweag/ormolu) for haskell.
 * [`perltidy`](https://perltidy.sourceforge.net/) for
   [perl](https://www.perl.org/) ([#175]).
+* [`pgformatter`](https://github.com/darold/pgFormatter) for [SQL](https://en.wikipedia.org/wiki/SQL) ([#247])
 * [purs-tidy](https://github.com/natefaubion/purescript-tidy) for PureScript ([#182]).
 * [`rubocop`](https://github.com/rubocop/rubocop) for [ruby](https://www.ruby-lang.org/en/) ([#176]).
 * [`ruby-standard`](https://github.com/standardrb/standard) for
@@ -122,6 +123,7 @@ The format is based on [Keep a Changelog].
 [#236]: https://github.com/radian-software/apheleia/pull/236
 [#242]: https://github.com/radian-software/apheleia/pull/242
 [#253]: https://github.com/radian-software/apheleia/pull/253
+[#247]: https://github.com/radian-software/apheleia/pull/247
 
 ## 3.2 (released 2023-02-25)
 ### Features
diff --git a/apheleia-formatters.el b/apheleia-formatters.el
index bb8bb1e..bb20b8b 100644
--- a/apheleia-formatters.el
+++ b/apheleia-formatters.el
@@ -81,6 +81,9 @@
                     "--enable-outside-detected-project"))
     (ormolu . ("ormolu"))
     (perltidy . ("perltidy" "--quiet" "--standard-error-output"))
+    (pgformatter . ("pg_format"
+                    (apheleia-formatters-indent "--tabs" "--spaces" 'tab-width)
+                    (apheleia-formatters-fill-column "--wrap-limit")))
     (phpcs . ("apheleia-phpcs"))
     (prettier
      . ("apheleia-npx" "prettier" "--stdin-filepath" filepath
@@ -306,6 +309,7 @@ rather than using this system."
     (rust-mode . rustfmt)
     (rust-ts-mode . rustfmt)
     (scss-mode . prettier-scss)
+    (sql-mode . pgformatter)
     (svelte-mode . prettier-svelte)
     (terraform-mode . terraform)
     (TeX-latex-mode . latexindent)
diff --git a/test/formatters/installers/pgformatter.bash b/test/formatters/installers/pgformatter.bash
new file mode 100644
index 0000000..60375b8
--- /dev/null
+++ b/test/formatters/installers/pgformatter.bash
@@ -0,0 +1 @@
+apt-get install -y pgformatter
diff --git a/test/formatters/samplecode/pgformatter/in.sql b/test/formatters/samplecode/pgformatter/in.sql
new file mode 100644
index 0000000..74b50eb
--- /dev/null
+++ b/test/formatters/samplecode/pgformatter/in.sql
@@ -0,0 +1 @@
+create table person (primary key id int, firstname varchar(500), lastname varchar(500), email varchar(500), password_sha256 varchar(200), password_seed varchar(200));
diff --git a/test/formatters/samplecode/pgformatter/out.sql b/test/formatters/samplecode/pgformatter/out.sql
new file mode 100644
index 0000000..3853714
--- /dev/null
+++ b/test/formatters/samplecode/pgformatter/out.sql
@@ -0,0 +1,9 @@
+CREATE TABLE person (
+        PRIMARY KEY id int,
+        firstname varchar(500),
+        lastname varchar(500),
+        email varchar(500),
+        password_sha256 varchar(200),
+        password_seed varchar(200)
+);
+
-- 
cgit v1.0

