8000 GitHub - pnezis/sql_fmt: Format and pretty print SQL queries
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

pnezis/sql_fmt

 
 

Repository files navigation

sql_fmt Logo sql_fmt title

Format and pretty print SQL queries

Hex.pm GitHub Workflow Status (master) Coveralls master branch Support the project


Contents

Installation

Available in Hex, the package can be installed by adding sql_fmt to your list of dependencies in mix.exs:

def deps do
  [
    {:sql_fmt, "~> 0.1.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/sql_fmt.

Example Output

After setting up SqlFmt in your application you can use the SqlFmt functions in order to format queries. Here are a couple examples of queries with having parameters inline and with passing in the parameters separately:

iex(1)> {:ok, formatted_sql} = SqlFmt.format_query("select * from businesses where id in ('c6f5c5f1-a1fc-4c9a-91f7-6aa40f1e233d', 'f339d4ce-96b6-4440-a541-28a0fb611139');")
{:ok, "SELECT\n  *\nFROM\n  businesses\nWHERE\n  id IN (\n    'c6f5c5f1-a1fc-4c9a-91f7-6aa40f1e233d',\n    'f339d4ce-96b6-4440-a541-28a0fb611139'\n  );"}

iex(2)> IO.puts(formatted_sql)
SELECT
  *
FROM
  businesses
WHERE
  id IN (
    'c6f5c5f1-a1fc-4c9a-91f7-6aa40f1e233d',
    'f339d4ce-96b6-4440-a541-28a0fb611139'
  );
:ok
iex(1)> {:ok, formatted_sql} = SqlFmt.format_query_with_params("select * from help where help.\"col\" in $1;", ["'asdf'"])
{:ok, "SELECT\n  *\nFROM\n  help\nWHERE\n  help.\"col\" IN 'asdf';"}

iex(2)> IO.puts(formatted_sql)
SELECT
  *
FROM
  help
WHERE
  help."col" IN 'asdf';
:ok

Be sure to checkout the HexDocs as you can also provide formatting options to the functions to tailor the output to your liking.

Supporting SqlFmt

If you rely on this library help you debug your Ecto/SQL queries, it would much appreciated if you can give back to the project in order to help ensure its continued development.

Checkout my GitHub Sponsorship page if you want to help out!

Gold Sponsors

Support the project

Silver Sponsors

Support the project

Bronze Sponsors

Support the project

Attribution

  • The logo for the project is an edited version of an SVG image from the unDraw project.
  • The SqlFmt library leans on the Rust library sqlformat-rs for SQL statement formatting.

About

Format and pretty print SQL queries

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 90.1%
  • Rust 9.9%
0