akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

SqlFmt - Format and pretty print SQL queries

Howdy howdy everyone!

After running into some issues formatting SQL for my EctoDbg library, I decided to go a new route and leverage the Rust SQL formatting library sqlformat in order to pretty print queries. That functionality was pulled out of EctoDbg and put into its own library as it has wider application than just my debugging library…so announcing SqlFmt!

SqlFmt is a precompiled Ruslter NIF that wraps the sqlformat Rust library so you can format SQL queries. It also provides a Mix Formatter plugin so you can format SQL files in your project. You can even format SQL statements in your code if you use the included ~SQL sigil. The Mix Formatter plugin takes care of both files and sigils for you and provides some customization options as well if you don’t like the defaults.

Simple example

I’m particularly excited to use this in my Ecto migrations because you can take queries like this:

    execute ~SQL"""
    insert into
      Test (Test_Date, Testno, Examno, Serialno, TYPE, Hours)
    select
      S.Test_Date,         E.Testno,    S.Examno,    S.Serialno,    'Non-Flight',
      (F.STARTED - F.ENDED) AS Hours
    FROM      Semester S,      TIME F,      TESTPAPERS e
    WHERE
      S.Testno = F.Testno
      AND E.Testno = 1
    """

and turn them into this by running mix format:

    execute ~SQL"""
    INSERT INTO
      Test (Test_Date, Testno, Examno, Serialno, TYPE, Hours)
    SELECT
      S.Test_Date,
      E.Testno,
      S.Examno,
      S.Serialno,
      'Non-Flight',
      (F.STARTED - F.ENDED) AS Hours
    FROM
      Semester S,
      TIME F,
      TESTPAPERS e
    WHERE
      S.Testno = F.Testno
      AND E.Testno = 1
    """

Enjoy!

Hex: sql_fmt | Hex
Github:

Where Next?

Popular in Libraries Top

scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Sub Categories:

We're in Beta

About us Mission Statement