RudManusachi

RudManusachi

SQL CASE with Ecto

Hi there!

Recently I was playing around with extracting and updating data in the DB and for fun challenged myself to try to implement a nice-looking function/macro to do SQL CASE with Ecto.

Wanted to share with you the results and thought it might be a good point to start blogging.

Any feedback is appreciated =)

Spoiler alert, throughout the blog post we are putting up together a sql_case/2 macro that could be used as:

from(m in "movies",
  select:
    {m.title,
     sql_case(m.rating, [
       [when: "G", then: "General Audiences"],
       [when: "R", then: "Restricted"],
       [when: "PG", then: "Parental Guidance Suggested"],
       [when: "NC-17", then: "Clearly Adult"],
       [else: m.rating]
     ])}
)

Most Liked

AstonJ

AstonJ

Looks good Rudolf!

Just a quick note that when you start creating a thread here, if you paste the link into the title field first this will add the link as a ‘featured link’ to your thread - these are the links show on our homepage and go some way towards your SEO as we don’t add a rel-nofollow to user-submitted blog posts :sunglasses: I have added it manually for you for this post :023:

Where Next?

Popular Backend topics Top

First poster: bot
We all know how to teach recursion. We’ve done it for decades. We pick some honored, time-tested examples—Fibonacci numbers and factorial...
New
First poster: bot
Why Zig When There is Already C++, D, and Rust? No hidden control flow No hidden allocations First-class support for no standard library...
New
Rainer
Just wrote a short post, more a memo to myself, but maybe someone find it useful :stuck_out_tongue: https://dwarfte.ch/2021/02/03/giving...
New
AstonJ
Just listening to this now… Totally agree with @FrancescoC’s and @thompson_si’s comment “learn to learn” :sunglasses: In our talk we’...
New
prajaut
Being a part of the tech industry, it would be good to share thoughts on specific technologies. Having surrounded by skilled and experie...
/go
New
First poster: bot
Over the last few years, due in large part to the hype surrounding blockchain and cryptocurrencies, decentralized applications have gaine...
New
First poster: bot
This post explains why Scala projects are difficult to maintain. Scala is a powerful programming language that can make certain small te...
New
First poster: bot
At Grammarly, the foundation of our business, our core grammar engine, is written in Common Lisp. It currently processes more than a thou...
New
pablocostass
Todos coñecemos os focos de Erlang/Elixir máis renomeados do mundo, como a Suecia, o Brasil, a California ou Londres. Mais a comunidade, ...
New
wolf4earth
Louis Pilfold is the creator of the Gleam programming language. He explains what Gleam is and tells us where it came from. He then dives...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1021 17094 374
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25503 755
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New