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:

https://github.com/akoutmos/sql_fmt

Where Next?

Popular in Announcing Top

danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps w...
New
josevalim
Hello everyone, We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
New
zachdaniel
Ash Framework What is Ash? Ash Framework is a declarative, resource-oriented application development framework for Elixir. A resource can...
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement