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:

Popular Backend topics Top

New
Scorpil
I dabbled in Phoenix for a while now, but never really got my hands dirty with it right up until now. Apart from the whole framework bein...
New
New
First poster: bot
It’s easy to view yourself as “not a real programmer.” There are programs out there that everyone uses, and it’s easy to put their develo...
/c
New
paulanthonywilson
I had a bit of a mini-adventure following Sobelow’s advice on adding a CSP to a Phoenix App. If you want to follow along, or want to add ...
New
elbrujohalcon
A long time ago, I wrote an article about The Asymmetry of ++, thanks to Fede Bergero’s findings. Let’s add a few more asymmetries to th...
New
New
First poster: AstonJ
Ruby’s Struct is one of several powerful core classes which is often overlooked and under utilized compared to the more popular Hash clas...
New
tonyxrandall
As DoorDash transitioned from Python monolith to Kotlin microservices, our engineering team was presented with a lot of opportunities to ...
New
ragamuf
Does the world need another How to create a blog article? Maybe not. But then again, creating something out of nothing is what we love....
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
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
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Latest in Blogs/Talks

View all threads ❯