Code Monkey home page Code Monkey logo

sqlformat's Introduction

Melpa Status Melpa Stable Status Build Status Support me

sqlformat.el

This Emacs library provides commands and a minor mode for easily reformatting SQL using any one of several popular SQL formatters (see Usage below for a list).

Installation

If you choose not to use one of the convenient packages in MELPA, you'll need to add the directory containing sqlformat.el to your load-path, and then (require 'sqlformat).

Usage

To set your preferred formatter, customise the sqlformat-command variable or set it as a file/directory local variable. The variable should be assigned one of the following symbol values:

For example, to use pgformatter (i.e., the pg_format command) with two-character indent and no statement grouping,

(setq sqlformat-command 'pgformatter)
;; Optional additional args
(setq sqlformat-args '("-s2" "-g"))

Then call sqlformat, sqlformat-buffer or sqlformat-region as convenient.

Enable sqlformat-on-save-mode in SQL buffers like this:

(add-hook 'sql-mode-hook 'sqlformat-on-save-mode)

or locally to your project with a form in your .dir-locals.el like this:

((sql-mode
   (mode . sqlformat-on-save)))

You might like to bind sqlformat or sqlformat-buffer to a key, e.g. with:

(define-key sql-mode-map (kbd "C-c C-f") 'sqlformat)

💝 Support this project and my other Open Source work

💼 LinkedIn profile

✍ sanityinc.com

🐦 @sanityinc

sqlformat's People

Contributors

purcell avatar yatsky avatar trisk3lion avatar rjspotter avatar okomestudio avatar agasparovic avatar chiply avatar sandinmyjoints avatar dvzubarev avatar

Stargazers

Daiki Shiozawa avatar KARASZI István avatar  avatar  avatar RJ Sheperd avatar David avatar Andrea Crotti avatar kinXdle avatar Philipp avatar  avatar Robert Weiner avatar Ev Dolzhenko avatar Antares avatar Denis Glotov avatar  avatar Shae Erisson avatar vinurs avatar 大関 金城 秀喜 カシオ avatar Yuji Kanagawa avatar  avatar  avatar Felix Geller avatar Kyrylo Baibula avatar Luiz Tagliaferro avatar Philipp Fehre avatar Kadir Can Çetin avatar Phan Tấn Thắng avatar  avatar Jon avatar Thibaut avatar Jeremy Taylor avatar Micah Elliott avatar Andrey Stepanov avatar John Hamelink avatar Space avatar Nacho Barrientos avatar Maor Kadosh avatar Chad Walstrom avatar Daniel Luna avatar Hiroaki Honda avatar 10sr avatar Daw-Ran Liou avatar Bruno Bigras avatar Delon R. Newman avatar Ignacy Moryc avatar Vinícius da Costa Simões avatar Claus Klingberg avatar  avatar Alcides Flores Pineda avatar Carin Meier avatar Gustav avatar Vineet Naik avatar Kiichi avatar  avatar  avatar Mahesh Narayanamurthi avatar Ryan Prior avatar Romain Viallard avatar Oliver Martell Núñez avatar Paulus Esterhazy avatar James Chochlinski avatar Andy Shevchenko avatar Carvin avatar Nicolas G. Querol avatar Jonathan McHugh avatar Yanko Simeonoff avatar niten2 avatar Zoltán Szeredi avatar Gurkaran Singh avatar Günter Zöchbauer avatar Avishek Kumar  avatar salotz avatar Daniel Berg avatar yqrashawn avatar Alexandr avatar Henry Linder avatar Jack Angers avatar Joseph LaFreniere avatar Yanko Simeonoff avatar  avatar Daniel Roux avatar Motoshi Nishihira avatar Michael Griffiths avatar Zhao Xiaohong avatar Jacksoncy avatar Bruno Furtado avatar Oleg Pykhalov avatar Jim Myhrberg avatar Dmitry Bushev avatar sballert avatar Juri Hahn avatar Patrick K Long avatar Greg Lucas avatar Chris Morgan avatar Jumpei KAWAMI avatar Akira Komamura avatar Juan A. Ruz avatar Andrew avatar  avatar andrea denisse avatar

Watchers

 avatar James Cloos avatar  avatar

sqlformat's Issues

Args with double dash is not recognized

Problem

Setting sqlformat-args w/ a double-dash option breaks the command execution.

Steps

pgformatter

If using pgformatter version 5.5:

(setq sqlformat-command 'pgformatter)
(setq sqlformat-args '("-s4" "-B" "-w80" "-k" "-f2" "-U2" "--extra-keyword ~/tmp/oracle-keywords))

Error:

Uncaught exception: Deadly warning: Unknown option: extra-keyword ~/workspace/tmp/oracle-keywords
 at /usr/local/bin/pg_format line 27.
	main::__ANON__("Unknown option: extra-keyword ~/workspace/tmp/oracle-keywords\x{a}") called at /usr/lib/x86_64-linux-gnu/perl-base/Getopt/Long.pm line 1074
	Getopt::Long::FindOption(ARRAY(0x55f2740efcf0), "(--|-|\\+)", "--", "--extra-keyword ~/workspace/tmp/oracle-keywords", HASH(0x55f274592b38)) called at /usr/lib/x86_64-linux-gnu/perl-base/Getopt/Long.pm line 529
	Getopt::Long::GetOptionsFromArray(undef, undef, undef, undef, undef, undef, undef, undef, ...) called at /usr/local/share/perl/5.34.0/pgFormatter/CLI.pm line 388
	pgFormatter::CLI::get_command_line_args(pgFormatter::CLI=HASH(0x55f2740c5890)) called at /usr/local/share/perl/5.34.0/pgFormatter/CLI.pm line 62
	pgFormatter::CLI::run(pgFormatter::CLI=HASH(0x55f2740c5890)) called at /usr/local/bin/pg_format line 49
 at /usr/local/bin/pg_format line 19.
	main::__ANON__("Deadly warning: Unknown option: extra-keyword ~/workspace/tmp"...) called at /usr/lib/x86_64-linux-gnu/perl-base/Carp.pm line 290
	Carp::confess("Deadly warning: Unknown option: extra-keyword ~/workspace/tmp"...) called at /usr/local/bin/pg_format line 27
	main::__ANON__("Unknown option: extra-keyword ~/workspace/tmp/oracle-keywords\x{a}") called at /usr/lib/x86_64-linux-gnu/perl-base/Getopt/Long.pm line 1074
	Getopt::Long::FindOption(ARRAY(0x55f2740efcf0), "(--|-|\\+)", "--", "--extra-keyword ~/workspace/tmp/oracle-keywords", HASH(0x55f274592b38)) called at /usr/lib/x86_64-linux-gnu/perl-base/Getopt/Long.pm line 529
	Getopt::Long::GetOptionsFromArray(undef, undef, undef, undef, undef, undef, undef, undef, ...) called at /usr/local/share/perl/5.34.0/pgFormatter/CLI.pm line 388
	pgFormatter::CLI::get_command_line_args(pgFormatter::CLI=HASH(0x55f2740c5890)) called at /usr/local/share/perl/5.34.0/pgFormatter/CLI.pm line 62
	pgFormatter::CLI::run(pgFormatter::CLI=HASH(0x55f2740c5890)) called at /usr/local/bin/pg_format line 49

sqlformat

Similarly, sqlformat supports many args in double-dash format:

sqlformat -k upper -i upper -s --wrap_after 80 --indent_width 4 --indent_after_first

Executing in shell works. But setting in emacs doesn't.

(setq sqlformat-command 'sqlformat)
(setq sqlformat-args '("-k" "upper" "-i" "upper" "-s" "--wrap_after 80" "--indent_width" "4" "--indent_after_first"))

Error:

sqlformat: error: unrecognized arguments: -

Expect

Not sure it's a documentation issue or code issue. I can't find in doc an example of using double-dash args. As seen in the sqlformat example above, I tried two different styles:

  1. "--wrap_after 80": all in one
  2. "--indent_width" "4": separated in two

I don't think it makes any difference since it's a LISP list, and should be passed onto to CLI as-is. But apparently neither worked.

Warning (bytecomp): function ‘sqlformat’ defined multiple times in this file

I'm seeing this error on compilation:

Compiling file /Users/edward/.emacs.d/elpa/sqlformat-20210305.209/sqlformat.el at Mon Jun 20 19:27:40 2022
Entering directory ‘/Users/edward/.emacs.d/elpa/sqlformat-20210305.209/’

Compiling internal form(s) at Mon Jun 20 19:27:41 2022
Warning (bytecomp): function ‘sqlformat’ defined multiple times in this file

Still seems to work OK, but just wanted to post in case it's helpful. I only see defgroup sqlformat and defun sqlformat in sqlformat.el so I guess it's complaining about those being the same.

My emacs:

GNU Emacs 28.1 (build 1, aarch64-apple-darwin21.3.0, NS appkit-2113.30 Version 12.2.1 (Build 21D62))
 of 2022-04-14

Selecting command

I have both sqlformat and pgformatter installed. How to force sqlformat to use pg_format command?

Can I use the full path of a tool with sqlformat-command?

I would like to do this with nix instead of adding sqlfluff in my path.

{
  sqlformat = {
    enable = true;
    hook = [
      "(sql-mode-hook . sqlformat-on-save-mode)"
    ];
    config = ''
    (setq sqlformat-command "${pkgs.sqlfluff}/bin/sqlfluff")
    (setq sqlformat-args '("fix" "-f" "-"))
  '';
  };
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.