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 README.md
Raw

README.md

# SqlFormatter
**TODO: Add description**
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `sql_formatter` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:sql_formatter, "~> 1.0.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/sql_formatter>.
## Usage
``` elixir
SQLFormatter.format("SELECT * FROM users")
#=>
"""
SELECT
*
FROM
users
"""
```
## Tests
This library provides functionality to test sql generation and compare it in a readable format.
First you'll need to configure your ExUnit formatter. In `test/test_helper.exs` add to the top:
``` elixir
ExUnit.configure(formatters: [SQLFormatter.ExUnit.CLIFormatter])
```
In test modules you can then import assertions:
``` elixir
import SQLFormatter.Assertions
```
Then assert equality between sql strings:
``` elixir
assert_sql_equal "SELECT * FROM users", "SELECT * FROM users"
refute_sql_equal "SELECT * FROM users", "SELECT * FROM peons"
```
When used like this any SQL is formatted before comparison and when there are errors they're shown in a more readable format.
*NOTE: Color diffing not shown below*
``` text
1) test formats simple SQL (SQLFormatterTest)
test/sql_formatter_test.exs:7
SQL strings do not match
code: assert_sql_equal(unformatted_sql, formatted_sql)
left: """
SELECT
*
FROM
users
WHERE
id = 1
"""
right: """
SELECT
*
FROM
user
WHERE
id = 1
"""
stacktrace:
test/sql_formatter_test.exs:20: (test)
```