SqlFmt.MixFormatter (SqlFmt v0.4.0)

View Source

Format SQL queries from .sql files or ~SQL sigils.

This is a mix format plugin.

Setup

Add it as a plugin to your .formatter.exs file. If you want it to run automatically for all your project's sql files make sure to put the proper pattern in the inputs option.

[
  plugins: [SqlFmt.MixFormatter],
  inputs: ["**/*.sql"],
  # ...
]

Options

The following options are supported. You should specify them under a :sql_fmt key in your .formatter.exs.

  • :indent - specifies how many spaces are used for indents. Defaults to 2.

  • :uppercase - specifies if the SQL reserved words will be capitalized. Defaults to true.

  • :lines_between_queries - specifies how many line breaks should be present after a query. Applicable only for input containing multiple queries. Defaults to 1.

  • :ignore_case_convert - comma separated list of strings that should not be case converted. If not set all reserved keywords are capitalized.

  • :extensions - change the default file extensions to be considered. Defaults to [".sql"]

An example configuration object with all supported options set follows.

[
  # ...omitted
  sql_fmt: [
    indent: 4,
    uppercase: true,
    lines_between_queries: 2,
    ignore_case_convert: ["where", "into"],
    extensions: [".sql", ".query"]
  ]
]

Formatting

The formatter uses SqlFmt.format_query/2 in order to format the input SQL queries or inline ~SQL sigils.

Single line ~SQL sigils

Notice that if an ~SQL sigil with a single delimiter is used then this is respected and only the reserved keyowrds are capitalized. For example:

query = ~SQL"select * from users"

would be formatted as:

query = ~SQL"SELECT * FROM users"