akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

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? Top

Trending in Announcing Top

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
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews