sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.34k stars 403 forks source link

PostgreSQL support #71

Closed mcandre closed 3 years ago

mcandre commented 5 years ago

Could sql-formatter support PostgreSQL syntax?

Deisss commented 5 years ago

+1 on this

moltar commented 4 years ago

From testing this with PosgreSQL, the main issue seems to be the placeholder support.

Placeholders get disjointed:

INSERT INTO
  "user"("first_name", "last_name", "is_admin")
VALUES
  ($ 1, $ 2, $ 3) RETURNING "id",
  "is_admin"

Should be:

INSERT INTO
  "user"("first_name", "last_name", "is_admin")
VALUES
  ($1, $2, $3)
RETURNING
  "id", "is_admin"
estar commented 4 years ago

For me, the main issue would be dollar-quoted strings, with extra work required in the tokenizer (because the start/token is variable). As of commit 2f3e9a88441a7084f56aedf32c5ec26b5362389d, using StandardSqlFormatter:

    Expected value to be (using ===):
      "SELECT $a$String example: $$Hello world!$$ - Did it work?$a$"
    Received:
      "SELECT
      $ a $ String example: $ $ Hello world ! $ $ - Did it work ? $ a $"
nene commented 3 years ago

Initial postgresql support has now been released in 4.0.0-beta.1.

To enable it, one can pass "postgresql" as language option:

format("SELECT *", {language: "postgresql"});