Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

osv/sql-formatter-cli

Repository files navigation

Console util for formatting SQL using lib sql-formatter. Zero dependencies


Not maintained

This package is not maintained anymore because sql-formatter contains command line client too now. Install sql-formatter and refer to it's manual how to use it.

SYNOPSIS

$ sql-formatter-cli -h
SQL formatter

Options:
  -i, --file  Load a file. "-" - stdin                                                                       [default: "-"]
  -s, --sql   Sql dialect: "sql" Standard SQL, "n1ql" Couchbase N1QL, "db2" IBM DB2, "pl/sql" Oracle PL/SQL  [default: "sql"]
  -o, --out   Output file name. "-" - stdout                                                                 [default: "-"]

$ echo "SELECT * FROM databases WHERE category='NoSQL'" | sql-formatter-cli
SELECT
  *
FROM
  databases
WHERE
  category = 'NoSQL'

INSTALL

This tool require Node.js to be installed. After you can install using npm:

npm i -g sql-formatter-cli

THANKS

Many thanks to authors of sql-formatter lib. I spent few hours for searching SQL query prettifier but most of them was awful or require JAVA/ruby (I failed to add "comma last" for anbt-sql-formatter).

The reason why I don't create PR for adding cli for sql-formatter lib is that sql-formatter use lodash dependency while I want to have minified version without any dependencies what is done by rollup :)

EMACS SNIPPET

Here you can find more, but below is config that I use for formatting selected region.

  (defun sql-beautify-region (beg end)
    "Beautify SQL in region between beg and END.
Dependency:
npm i -g sql-formatter-cli"
    (interactive "r")
    (save-excursion
      (shell-command-on-region beg end "sql-formatter-cli" nil t)))
  (defun sql-beautify-buffer ()
    "Beautify SQL in buffer."
    (interactive)
    (sql-beautify-region (point-min) (point-max)))
  (add-hook 'sql-mode-hook '(lambda ()
                              ;; beautify region or buffer
                              (local-set-key (kbd "C-c t") 'sql-beautify-region)))

TODO

  • Add version option support
  • Add integrity tests
  • Use yarn instead of npm