Packages

A SQL formatter that provides consistent and readable formatting for SQL queries. Built with Rust for performance and integrated with ExUnit for better SQL diff output in tests.

Current section

Files

Jump to
sql_formatter lib sql_formatter.ex
Raw

lib/sql_formatter.ex

defmodule SQLFormatter do
alias SQLFormatter.NIF
alias SQLFormatter.Opts
@type opts :: [
indent: non_neg_integer(),
uppercase: boolean(),
lines_between_queries: non_neg_integer()
]
@doc """
Formats a SQL string.
## Options
- `:indent`: The number of spaces to indent the formatted SQL. Defaults to 2.
- `:uppercase`: Whether to use uppercase for keywords. Defaults to `false`.
- `:lines_between_queries`: The number of lines to insert between queries. Defaults to 1.
## Examples
```elixir
SQLFormatter.format("SELECT * FROM users")
#=>
\"""
SELECT
*
FROM
users
\"""
```
```elixir
SQLFormatter.format("SELECT * FROM users", indent: 4)
#=>
\"""
SELECT
*
FROM
users
\"""
```
"""
@spec format(String.t(), opts) :: String.t()
def format(sql, opts \\ []) do
NIF.format(sql, struct(Opts, opts))
end
end