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

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
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

We're in Beta

About us Mission Statement